X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FConfigItem.java;h=beed66f55f9de312998924c0e85c06f01344a45c;hb=9e834013f84e8797acf26f5418ae3448044ad097;hp=2cec4cab381e300dc53382c3961f508ada6f801f;hpb=49f79f31c728cc9a39b695a559404faa9a15f9b3;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java index 2cec4ca..beed66f 100644 --- a/src/be/nikiroo/utils/ui/ConfigItem.java +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -1,92 +1,71 @@ package be.nikiroo.utils.ui; import java.awt.BorderLayout; -import java.awt.Dimension; -import java.util.ArrayList; -import java.util.List; -import javax.swing.JLabel; +import javax.swing.JCheckBox; 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; +import be.nikiroo.utils.resources.MetaInfo; /** * A graphical item that reflect a configuration option from the given * {@link Bundle}. * * @author niki - * + * * @param * the type of {@link Bundle} to edit */ public class ConfigItem> extends JPanel { private static final long serialVersionUID = 1L; - private final Bundle bundle; - private final E id; - private String value; - - private JTextField valueField; - - public ConfigItem(Class type, Bundle bundle, E id) { - this.bundle = bundle; - this.id = id; + public ConfigItem(final MetaInfo info) { this.setLayout(new BorderLayout()); this.setBorder(new EmptyBorder(2, 10, 2, 10)); - String name = id.toString(); - if (name.length() > 1) { - name = name.substring(0, 1) + name.substring(1).toLowerCase(); - name = name.replace("_", " "); - } + if (info.getFormat() == Format.BOOLEAN) { + final JCheckBox field = new JCheckBox(); + field.setToolTipText(info.getDescription()); + Boolean state = info.getBoolean(); + if (state == null) { + info.getDefaultBoolean(); + } - JLabel nameLabel = new JLabel(name); - nameLabel.setPreferredSize(new Dimension(400, 0)); - this.add(nameLabel, BorderLayout.WEST); + // 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; + } - valueField = new JTextField(); - valueField.setText(value); + field.setSelected(state); - reload(); - this.add(valueField, BorderLayout.CENTER); - } + info.addReloadListener(new Runnable() { + @Override + public void run() { + field.setText(info.getString()); + } + }); - /** - * Reload the value from the {@link Bundle}. - */ - public void reload() { - value = bundle.getString(id); - valueField.setText(value); - } + this.add(field, BorderLayout.CENTER); + } else { + final JTextField field = new JTextField(); + field.setToolTipText(info.getDescription()); + field.setText(info.getString()); - /** - * Save the current value to the {@link Bundle}. - */ - public void save() { - value = valueField.getText(); - bundle.setString(id, value); - } + info.addReloadListener(new Runnable() { + @Override + public void run() { + field.setText(info.getString()); + } + }); - /** - * Create a list of {@link ConfigItem}, one for each of the item in the - * given {@link Bundle}. - * - * @param type - * a class instance of the item type to work on - * @param bundle - * the {@link Bundle} to sort through - * - * @return the list - */ - static public > List> getItems( - Class type, Bundle bundle) { - List> list = new ArrayList>(); - for (E id : type.getEnumConstants()) { - list.add(new ConfigItem(type, bundle, id)); + this.add(field, BorderLayout.CENTER); } - - return list; } }