X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FConfigItem.java;h=9ae58bad336f00c3973154f4674d2573a586409c;hb=c637d2e077e7c6e78c27c76bc20eb36624cd310f;hp=691c6198319b24d71ea2710e6f96f7aa348a3c49;hpb=94c44e42bf9d13fb372733318d9667c35af266be;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java index 691c619..9ae58ba 100644 --- a/src/be/nikiroo/utils/ui/ConfigItem.java +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -2,16 +2,15 @@ package be.nikiroo.utils.ui; import java.awt.BorderLayout; import java.awt.Dimension; -import java.util.ArrayList; -import java.util.List; +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; +import be.nikiroo.utils.resources.Meta.Format; +import be.nikiroo.utils.resources.MetaInfo; /** * A graphical item that reflect a configuration option from the given @@ -24,92 +23,104 @@ import be.nikiroo.utils.resources.Meta; */ public class ConfigItem> extends JPanel { private static final long serialVersionUID = 1L; - private Class type; - private final Bundle bundle; - private final E id; - - private Meta meta; - private String value; - - private JTextField valueField; - - public ConfigItem(Class type, Bundle bundle, E id) { - this.type = type; - this.bundle = bundle; - this.id = id; - - try { - this.meta = type.getDeclaredField(id.name()).getAnnotation( - Meta.class); - } catch (NoSuchFieldException e) { - } catch (SecurityException e) { - } + public ConfigItem(final MetaInfo info) { this.setLayout(new BorderLayout()); - this.setBorder(new EmptyBorder(2, 10, 2, 10)); - String tooltip = null; - if (bundle.getDescriptionBundle() != null) { - tooltip = bundle.getDescriptionBundle().getString(id); - if (tooltip != null && tooltip.trim().isEmpty()) { - tooltip = null; + if (info.getFormat() == Format.BOOLEAN) { + final JCheckBox field = new JCheckBox(); + field.setToolTipText(info.getDescription()); + Boolean state = info.getBoolean(); + if (state == null) { + info.getDefaultBoolean(); } - } - - String name = id.toString(); - if (name.length() > 1) { - name = name.substring(0, 1) + name.substring(1).toLowerCase(); - name = name.replace("_", " "); - } - - JLabel nameLabel = new JLabel(name); - nameLabel.setToolTipText(tooltip); - nameLabel.setPreferredSize(new Dimension(400, 0)); - this.add(nameLabel, BorderLayout.WEST); - - valueField = new JTextField(); - valueField.setText(value); - - reload(); - this.add(valueField, BorderLayout.CENTER); - } - /** - * Reload the value from the {@link Bundle}. - */ - public void reload() { - value = bundle.getString(id); - valueField.setText(value); - } + // Should not happen! + if (state == null) { + System.err + .println("No default value given for BOOLEAN parameter \"" + + info.getName() + + "\", we consider it is FALSE"); + state = false; + } - /** - * Save the current value to the {@link Bundle}. - */ - public void save() { - value = valueField.getText(); - bundle.setString(id, value); + field.setSelected(state); + + info.addReloadedListener(new Runnable() { + @Override + public void run() { + 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()); + } + }); + + field.setText(info.getName()); + this.add(field, BorderLayout.CENTER); + } else { + final JTextField field = new JTextField(); + field.setToolTipText(info.getDescription()); + field.setText(info.getString()); + + 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(label(info.getName()), BorderLayout.WEST); + this.add(field, BorderLayout.CENTER); + } } /** - * Create a list of {@link ConfigItem}, one for each of the item in the - * given {@link Bundle}. + * Create a label which width is constrained in lock steps. * - * @param - * the type of {@link Bundle} to edit - * @param type - * a class instance of the item type to work on - * @param bundle - * the {@link Bundle} to sort through + * @param text + * the text of the label * - * @return the list + * @return the label */ - static public > List> getItems( - Class type, Bundle bundle) { - List> list = new ArrayList>(); - for (E id : type.getEnumConstants()) { - list.add(new ConfigItem(type, bundle, id)); + private JLabel label(String text) { + final JLabel label = new JLabel(text); + + Dimension ps = label.getPreferredSize(); + if (ps == null) { + ps = label.getSize(); + } + + int w = ps.width; + int step = 80; + for (int i = 2 * step; i < 10 * step; i += step) { + if (w < i) { + w = i; + break; + } } - return list; + ps.width = w; + label.setSize(ps); + label.setPreferredSize(ps); + + return label; } }