From: Niki Roo Date: Thu, 16 May 2019 07:10:06 +0000 (+0200) Subject: ConfigItem: improve logic, UI X-Git-Tag: nikiroo-utils-4.7.3~24 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=8517b60cb3dfc64f7cec0c4da8d5003837e82bb2;p=nikiroo-utils.git ConfigItem: improve logic, UI --- diff --git a/src/be/nikiroo/utils/resources/MetaInfo.java b/src/be/nikiroo/utils/resources/MetaInfo.java index 3a6e71a..746fd4d 100644 --- a/src/be/nikiroo/utils/resources/MetaInfo.java +++ b/src/be/nikiroo/utils/resources/MetaInfo.java @@ -2,6 +2,9 @@ package be.nikiroo.utils.resources; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; import be.nikiroo.utils.resources.Meta.Format; @@ -21,7 +24,8 @@ public class MetaInfo> { private Meta meta; private String value; - private List reloadListeners = new ArrayList(); + private List reloadedListeners = new ArrayList(); + private List saveListeners = new ArrayList(); private String name; private String description; @@ -50,7 +54,7 @@ public class MetaInfo> { if (description == null) { description = meta.description(); if (meta.info() != null && !meta.info().isEmpty()) { - description += "\n" + meta.info(); + description += " (" + meta.info() + ")"; } } @@ -178,7 +182,7 @@ public class MetaInfo> { */ public void reload() { value = bundle.getString(id); - for (Runnable listener : reloadListeners) { + for (Runnable listener : reloadedListeners) { try { listener.run(); } catch (Exception e) { @@ -188,17 +192,31 @@ public class MetaInfo> { } } - public void addReloadListener(Runnable listener) { - reloadListeners.add(listener); + // listeners will be called AFTER reload + public void addReloadedListener(Runnable listener) { + reloadedListeners.add(listener); } /** * Save the current value to the {@link Bundle}. */ public void save() { + for (Runnable listener : saveListeners) { + try { + listener.run(); + } catch (Exception e) { + // TODO: error management? + e.printStackTrace(); + } + } bundle.setString(id, value); } + // listeners will be called BEFORE save + public void addSaveListener(Runnable listener) { + saveListeners.add(listener); + } + /** * Create a list of {@link MetaInfo}, one for each of the item in the given * {@link Bundle}. @@ -222,5 +240,52 @@ public class MetaInfo> { return list; } - // TODO: by groups, a-là Authors/Sources + // TODO: multiple levels? + static public > Map, List>> getGroupedItems( + Class type, Bundle bundle) { + Map, List>> map = new TreeMap, List>>(); + Map, List>> map1 = new TreeMap, List>>(); + + List> ungrouped = new ArrayList>(); + for (MetaInfo info : getItems(type, bundle)) { + if (info.meta.group()) { + List> list = new ArrayList>(); + map.put(info, list); + map1.put(info, list); + } else { + ungrouped.add(info); + } + } + + for (int i = 0; i < ungrouped.size(); i++) { + MetaInfo info = ungrouped.get(i); + MetaInfo group = findParent(info, map.keySet()); + if (group != null) { + map.get(group).add(info); + ungrouped.remove(i--); + } + } + + if (ungrouped.size() > 0) { + map.put(null, ungrouped); + } + + return map; + } + + static private > MetaInfo findParent(MetaInfo info, + Set> candidates) { + MetaInfo group = null; + for (MetaInfo pcandidate : candidates) { + if (info.id.toString().startsWith(pcandidate.id.toString())) { + if (group == null + || group.id.toString().length() < pcandidate.id + .toString().length()) { + group = pcandidate; + } + } + } + + return group; + } } diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java index beed66f..3780a7e 100644 --- a/src/be/nikiroo/utils/ui/ConfigItem.java +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -3,9 +3,9 @@ package be.nikiroo.utils.ui; import java.awt.BorderLayout; import javax.swing.JCheckBox; +import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; -import javax.swing.border.EmptyBorder; import be.nikiroo.utils.resources.Bundle; import be.nikiroo.utils.resources.Meta.Format; @@ -25,7 +25,7 @@ public class ConfigItem> extends JPanel { public ConfigItem(final MetaInfo info) { this.setLayout(new BorderLayout()); - this.setBorder(new EmptyBorder(2, 10, 2, 10)); + // this.setBorder(new EmptyBorder(2, 10, 2, 10)); if (info.getFormat() == Format.BOOLEAN) { final JCheckBox field = new JCheckBox(); @@ -38,33 +38,56 @@ public class ConfigItem> extends JPanel { // Should not happen! if (state == null) { System.err - .println("No default value given for BOOLEAN parameter " - + info.getName() + ", we consider it is FALSE"); + .println("No default value given for BOOLEAN parameter \"" + + info.getName() + + "\", we consider it is FALSE"); state = false; } field.setSelected(state); - info.addReloadListener(new Runnable() { + info.addReloadedListener(new Runnable() { @Override public void run() { - field.setText(info.getString()); + Boolean state = info.getBoolean(); + if (state == null) { + info.getDefaultBoolean(); + } + if (state == null) { + state = false; + } + + field.setSelected(state); + } + }); + info.addSaveListener(new Runnable() { + @Override + public void run() { + info.setBoolean(field.isSelected()); } }); + this.add(new JLabel(info.getName() + ": "), BorderLayout.WEST); this.add(field, BorderLayout.CENTER); } else { final JTextField field = new JTextField(); field.setToolTipText(info.getDescription()); field.setText(info.getString()); - info.addReloadListener(new Runnable() { + info.addReloadedListener(new Runnable() { @Override public void run() { field.setText(info.getString()); } }); + info.addSaveListener(new Runnable() { + @Override + public void run() { + info.setString(field.getText()); + } + }); + this.add(new JLabel(info.getName() + ": "), BorderLayout.WEST); this.add(field, BorderLayout.CENTER); } }