| 1 | package be.nikiroo.utils.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Dimension; |
| 5 | |
| 6 | import javax.swing.JCheckBox; |
| 7 | import javax.swing.JLabel; |
| 8 | import javax.swing.JPanel; |
| 9 | import javax.swing.JTextField; |
| 10 | |
| 11 | import be.nikiroo.utils.resources.Bundle; |
| 12 | import be.nikiroo.utils.resources.Meta.Format; |
| 13 | import be.nikiroo.utils.resources.MetaInfo; |
| 14 | |
| 15 | /** |
| 16 | * A graphical item that reflect a configuration option from the given |
| 17 | * {@link Bundle}. |
| 18 | * |
| 19 | * @author niki |
| 20 | * |
| 21 | * @param <E> |
| 22 | * the type of {@link Bundle} to edit |
| 23 | */ |
| 24 | public class ConfigItem<E extends Enum<E>> extends JPanel { |
| 25 | private static final long serialVersionUID = 1L; |
| 26 | |
| 27 | public ConfigItem(final MetaInfo<E> info) { |
| 28 | this.setLayout(new BorderLayout()); |
| 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() |
| 43 | + "\", we consider it is FALSE"); |
| 44 | state = false; |
| 45 | } |
| 46 | |
| 47 | field.setSelected(state); |
| 48 | |
| 49 | info.addReloadedListener(new Runnable() { |
| 50 | @Override |
| 51 | public void run() { |
| 52 | Boolean state = info.getBoolean(); |
| 53 | if (state == null) { |
| 54 | info.getDefaultBoolean(); |
| 55 | } |
| 56 | if (state == null) { |
| 57 | state = false; |
| 58 | } |
| 59 | |
| 60 | field.setSelected(state); |
| 61 | } |
| 62 | }); |
| 63 | info.addSaveListener(new Runnable() { |
| 64 | @Override |
| 65 | public void run() { |
| 66 | info.setBoolean(field.isSelected()); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | field.setText(info.getName()); |
| 71 | this.add(field, BorderLayout.CENTER); |
| 72 | } else { |
| 73 | final JTextField field = new JTextField(); |
| 74 | field.setToolTipText(info.getDescription()); |
| 75 | field.setText(info.getString()); |
| 76 | |
| 77 | info.addReloadedListener(new Runnable() { |
| 78 | @Override |
| 79 | public void run() { |
| 80 | field.setText(info.getString()); |
| 81 | } |
| 82 | }); |
| 83 | info.addSaveListener(new Runnable() { |
| 84 | @Override |
| 85 | public void run() { |
| 86 | info.setString(field.getText()); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | this.add(label(info.getName()), BorderLayout.WEST); |
| 91 | this.add(field, BorderLayout.CENTER); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Create a label which width is constrained in lock steps. |
| 97 | * |
| 98 | * @param text |
| 99 | * the text of the label |
| 100 | * |
| 101 | * @return the label |
| 102 | */ |
| 103 | private JLabel label(String text) { |
| 104 | final JLabel label = new JLabel(text); |
| 105 | |
| 106 | Dimension ps = label.getPreferredSize(); |
| 107 | if (ps == null) { |
| 108 | ps = label.getSize(); |
| 109 | } |
| 110 | |
| 111 | int w = ps.width; |
| 112 | int step = 80; |
| 113 | for (int i = 2 * step; i < 10 * step; i += step) { |
| 114 | if (w < i) { |
| 115 | w = i; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | ps.width = w; |
| 121 | label.setSize(ps); |
| 122 | label.setPreferredSize(ps); |
| 123 | |
| 124 | return label; |
| 125 | } |
| 126 | } |