| 1 | package be.nikiroo.utils.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | |
| 5 | import javax.swing.JCheckBox; |
| 6 | import javax.swing.JPanel; |
| 7 | import javax.swing.JTextField; |
| 8 | import javax.swing.border.EmptyBorder; |
| 9 | |
| 10 | import be.nikiroo.utils.resources.Bundle; |
| 11 | import be.nikiroo.utils.resources.Meta.Format; |
| 12 | import be.nikiroo.utils.resources.MetaInfo; |
| 13 | |
| 14 | /** |
| 15 | * A graphical item that reflect a configuration option from the given |
| 16 | * {@link Bundle}. |
| 17 | * |
| 18 | * @author niki |
| 19 | * |
| 20 | * @param <E> |
| 21 | * the type of {@link Bundle} to edit |
| 22 | */ |
| 23 | public class ConfigItem<E extends Enum<E>> extends JPanel { |
| 24 | private static final long serialVersionUID = 1L; |
| 25 | |
| 26 | public ConfigItem(final MetaInfo<E> info) { |
| 27 | this.setLayout(new BorderLayout()); |
| 28 | this.setBorder(new EmptyBorder(2, 10, 2, 10)); |
| 29 | |
| 30 | if (info.getFormat() == Format.BOOLEAN) { |
| 31 | final JCheckBox field = new JCheckBox(); |
| 32 | field.setToolTipText(info.getDescription()); |
| 33 | Boolean state = info.getBoolean(); |
| 34 | if (state == null) { |
| 35 | info.getDefaultBoolean(); |
| 36 | } |
| 37 | |
| 38 | // Should not happen! |
| 39 | if (state == null) { |
| 40 | System.err |
| 41 | .println("No default value given for BOOLEAN parameter " |
| 42 | + info.getName() + ", we consider it is FALSE"); |
| 43 | state = false; |
| 44 | } |
| 45 | |
| 46 | field.setSelected(state); |
| 47 | |
| 48 | info.addReloadListener(new Runnable() { |
| 49 | @Override |
| 50 | public void run() { |
| 51 | field.setText(info.getString()); |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | this.add(field, BorderLayout.CENTER); |
| 56 | } else { |
| 57 | final JTextField field = new JTextField(); |
| 58 | field.setToolTipText(info.getDescription()); |
| 59 | field.setText(info.getString()); |
| 60 | |
| 61 | info.addReloadListener(new Runnable() { |
| 62 | @Override |
| 63 | public void run() { |
| 64 | field.setText(info.getString()); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | this.add(field, BorderLayout.CENTER); |
| 69 | } |
| 70 | } |
| 71 | } |