package be.nikiroo.utils.ui;
import java.awt.BorderLayout;
-import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
-import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
-import java.io.File;
import java.io.IOException;
+import java.util.List;
-import javax.swing.Icon;
+import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
-import javax.swing.JCheckBox;
-import javax.swing.JColorChooser;
-import javax.swing.JComboBox;
import javax.swing.JComponent;
-import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
-import javax.swing.JPasswordField;
-import javax.swing.JSpinner;
import javax.swing.JTextField;
import be.nikiroo.utils.Image;
import be.nikiroo.utils.StringUtils;
import be.nikiroo.utils.StringUtils.Alignment;
import be.nikiroo.utils.resources.Bundle;
-import be.nikiroo.utils.resources.Meta.Format;
import be.nikiroo.utils.resources.MetaInfo;
/**
* @param <E>
* the type of {@link Bundle} to edit
*/
-public class ConfigItem<E extends Enum<E>> extends JPanel {
+public abstract class ConfigItem<E extends Enum<E>> extends JPanel {
private static final long serialVersionUID = 1L;
- /** A small (?) blue in PNG, base64 encoded. */
- private static String infoImage64 = //
+ private static int minimumHeight = -1;
+
+ /** A small 16x16 "?" blue in PNG, base64 encoded. */
+ private static String img64info = //
""
+ "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI"
+ "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wURFRg6IrtcdgAAATdJREFUOMvtkj8sQ1EUxr9z/71G"
+ "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA"
+ "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC";
- /** The original value before current changes. */
- private Object orig;
+ /** A small 16x16 "+" image with colours */
+ private static String img64add = //
+ ""
+ + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI"
+ + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wUeES0QBFvvnAAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl"
+ + "YXRlZCB3aXRoIEdJTVBkLmUHAAACH0lEQVQ4y42Tz0sVURTHP+fMmC7CQMpH1EjgIimCsEVBEIg/"
+ + "qIbcBAW2Uai1m/oH2rlJXLQpeRJt2gQhTO0iTTKC1I2JBf5gKCJCRPvhPOed22LmvV70Fn7hwr3c"
+ + "+z3ne+73HCFHEClxaASRHgduA91AW369BkwDI3Foy0GkEofmACQnSxyaCyItAkMClMzYdeCAJgVP"
+ + "tJJrPA7tVoUjNZlngXMAiRmXClfoK/Tjq09x7T6LW+8RxOVJ5+LQzgSRojm5WCEDlMrQVbjIQNtN"
+ + "rh0d5FTzaTLBmWKgM4h0Ig4NzWseohYCJUuqx123Sx0MBpF2+MAdyWUnlqX4lf4bIDHjR+rwJJPR"
+ + "qNCgCjDsA10lM/oKIRcO9lByCYklnG/pqQa4euQ6J5tPoKI0yD6ef33Ku40Z80R7CSJNWyZxT+Ki"
+ + "2ytGP911hyZxQaRp1RtPPPYKD4+sGJwPrDUp7Q9Xxnj9fYrUUnaszEAwQHfrZQAerT/g7cYMiuCp"
+ + "z8LmLI0qBqz6wLQn2v5he57FrXkAtlPH2ZZOuskCzG2+4dnnx3iSuSgCKqLAlAIjmXPiVIRsgYjU"
+ + "usrfO0Gq7cA9jUNbBsZrmiQnac1e6n3FeBzakpf39OSBG9IPHAZwzlFoagVg5edHXn57wZed9dpA"
+ + "C3FoYRDpf8M0AQwKwu9yubxjeA7Y72ENqlp3mOqMcwcwDPQCx8gGchV4BYzGoS1V3gL8AVA5C5/0"
+ + "oRFoAAAAAElFTkSuQmCC";
+
+ /** A small 32x32 "-" image with colours */
+ private static String img64remove = //
+ ""
+ + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI"
+ + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wUeESw5X/JGsQAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl"
+ + "YXRlZCB3aXRoIEdJTVBkLmUHAAACKUlEQVQ4y5WTO2iTYRSG3+//v/+SJrG5SSABh1JQBHFJNUNR"
+ + "YodCLoMoTkK0YKhQtBmsl01wKVZRBwcrgosg3SwFW9Cippe0VmlpB6uYqYIaNSZtbv/lOKRx0iR9"
+ + "4YOzvOc8vOd8wLbG4nYGAKP9tshKr3Pq0zFXORt0UzbopvUeZ2ml1/niUcIWAYBzwwqr+xgAjCSt"
+ + "wpXjWzx105Ha+1XsMgT8U6IJfPAacyfO50OXJi3VwbtbxMbidtZ3tiClbzi/eAuCmxgai4AfNvNn"
+ + "KJn3X5xWKgwA0lHHYud3MdDUXMcmIOMx0oGJXJCN9tuiJ98p4//DbtTk2cFKhB/OSBcMgQHVMkir"
+ + "AqwJBhGYrIIkCQc2eJK3aewI9Crko2FIh0K1Jo0mcwmV6XFUlmfRXhK7eXuRKaRVIYdiUGKnW8Kn"
+ + "0ia0t6/hKHJVqCcLzncQgLhtIvBfbWbZZahq+cl96AuvQLre2Mw59NUlkCwjZ6USL0uYgSj26B/X"
+ + "oK+vtkYgMAhMRF4x5oWlPdod0UQtfUFo7YEBBKz59BEGAAtRx1xHVgzu5AYyHmMmMJHrZolhhU3t"
+ + "05XJe7s2PJuCq9k1MgKyNjOXiBf8kWW5JDy4XKHBl2ql6+pvX8ZjzDOqrcWsFQAAE/T3H3z2GG/6"
+ + "zhT8sfdKeehWkUQAeJ7WcH23xTz1uPBwf1hclA3mBZjPojFOIOSsVPpmN1OznfpA+Gn+2kCHqg/d"
+ + "LhIA/AFU5d0V6gTjtQAAAABJRU5ErkJggg==";
+
+ /** The code base */
+ private final ConfigItemBase<JComponent, E> base;
+
+ /** The main panel with all the fields in it. */
+ private JPanel main;
/**
- * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
+ * Prepare a new {@link ConfigItem} instance, linked to the given
+ * {@link MetaInfo}.
*
* @param info
- * the {@link MetaInfo}
- * @param nhgap
- * negative horisontal gap in pixel to use for the label, i.e.,
- * the step lock sized labels will start smaller by that amount
- * (the use case would be to align controls that start at a
- * different horisontal position)
+ * the info
+ * @param autoDirtyHandling
+ * TRUE to automatically manage the setDirty/Save operations,
+ * FALSE if you want to do it yourself via
+ * {@link ConfigItem#setDirtyItem(int)}
*/
- public ConfigItem(MetaInfo<E> info, int nhgap) {
- this.setLayout(new BorderLayout());
-
- // TODO: support arrays
- Format fmt = info.getFormat();
- if (info.isArray()) {
- fmt = Format.STRING;
- }
-
- switch (fmt) {
- case BOOLEAN:
- addBooleanField(info, nhgap);
- break;
- case COLOR:
- addColorField(info, nhgap);
- break;
- case FILE:
- addBrowseField(info, nhgap, false);
- break;
- case DIRECTORY:
- addBrowseField(info, nhgap, true);
- break;
- case COMBO_LIST:
- addComboboxField(info, nhgap, true);
- break;
- case FIXED_LIST:
- addComboboxField(info, nhgap, false);
- break;
- case INT:
- addIntField(info, nhgap);
- break;
- case PASSWORD:
- addPasswordField(info, nhgap);
- break;
- case STRING:
- case LOCALE: // TODO?
- default:
- addStringField(info, nhgap);
- break;
- }
- }
-
- private void reload(Object value) {
- // We consider "" and NULL to be equals
- if ("".equals(value)) {
- value = null;
- }
- orig = value;
- }
-
- private boolean isChanged(Object newValue) {
- // We consider "" and NULL to be equals
- if ("".equals(newValue)) {
- newValue = null;
- }
-
- if (newValue == null) {
- return orig != null;
- }
-
- return !newValue.equals(orig);
- }
-
- private void addStringField(final MetaInfo<E> info, int nhgap) {
- final JTextField field = new JTextField();
- field.setToolTipText(info.getDescription());
- String value = info.getString(false);
- reload(value);
- field.setText(value);
-
- info.addReloadedListener(new Runnable() {
+ protected ConfigItem(MetaInfo<E> info, boolean autoDirtyHandling) {
+ base = new ConfigItemBase<JComponent, E>(info, autoDirtyHandling) {
@Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
+ protected JComponent createEmptyField(int item) {
+ return ConfigItem.this.createEmptyField(item);
}
- });
- info.addSaveListener(new Runnable() {
+
@Override
- public void run() {
- String value = field.getText();
- if (isChanged(value)) {
- info.setString(value);
- }
+ protected Object getFromInfo(int item) {
+ return ConfigItem.this.getFromInfo(item);
}
- });
-
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
-
- setPreferredSize(field);
- }
- private void addBooleanField(final MetaInfo<E> info, int nhgap) {
- final JCheckBox field = new JCheckBox();
- field.setToolTipText(info.getDescription());
- Boolean state = info.getBoolean(true);
-
- // 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;
- }
-
- reload(state);
- field.setSelected(state);
-
- info.addReloadedListener(new Runnable() {
@Override
- public void run() {
- Boolean state = info.getBoolean(true);
- if (state == null) {
- state = false;
- }
-
- reload(state);
- field.setSelected(state);
+ protected void setToInfo(Object value, int item) {
+ ConfigItem.this.setToInfo(value, item);
}
- });
- info.addSaveListener(new Runnable() {
+
@Override
- public void run() {
- boolean state = field.isSelected();
- if (isChanged(state)) {
- info.setBoolean(state);
- }
+ protected Object getFromField(int item) {
+ return ConfigItem.this.getFromField(item);
}
- });
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ @Override
+ protected void setToField(Object value, int item) {
+ ConfigItem.this.setToField(value, item);
+ }
- setPreferredSize(field);
- }
+ @Override
+ public JComponent createField(int item) {
+ JComponent field = super.createField(item);
- private void addColorField(final MetaInfo<E> info, int nhgap) {
- final JTextField field = new JTextField();
- field.setToolTipText(info.getDescription());
- String value = info.getString(false);
- reload(value);
- field.setText(value);
+ int height = Math.max(getMinimumHeight(),
+ field.getMinimumSize().height);
+ field.setPreferredSize(new Dimension(200, height));
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
+ return field;
}
- });
- info.addSaveListener(new Runnable() {
+
@Override
- public void run() {
- String value = field.getText();
- if (isChanged(value)) {
- info.setString(value);
+ public List<JComponent> reload() {
+ List<JComponent> removed = base.reload();
+ if (!removed.isEmpty()) {
+ for (JComponent c : removed) {
+ main.remove(c);
+ }
+ main.revalidate();
+ main.repaint();
}
- }
- });
- this.add(label(info, nhgap), BorderLayout.WEST);
- JPanel pane = new JPanel(new BorderLayout());
+ return removed;
+ }
- final JButton colorWheel = new JButton();
- colorWheel.setIcon(getIcon(17, info.getColor(true)));
- colorWheel.addActionListener(new ActionListener() {
@Override
- public void actionPerformed(ActionEvent e) {
- Integer icol = info.getColor(true);
- if (icol == null) {
- icol = new Color(255, 255, 255, 255).getRGB();
- }
- Color initialColor = new Color(icol, true);
- Color newColor = JColorChooser.showDialog(ConfigItem.this,
- info.getName(), initialColor);
- if (newColor != null) {
- info.setColor(newColor.getRGB());
- field.setText(info.getString(false));
- colorWheel.setIcon(getIcon(17, info.getColor(true)));
- }
- }
- });
- pane.add(colorWheel, BorderLayout.WEST);
- pane.add(field, BorderLayout.CENTER);
- this.add(pane, BorderLayout.CENTER);
+ protected JComponent removeItem(int item) {
+ JComponent removed = super.removeItem(item);
+ main.remove(removed);
+ main.revalidate();
+ main.repaint();
- setPreferredSize(pane);
+ return removed;
+ }
+ };
}
- private void addBrowseField(final MetaInfo<E> info, int nhgap,
- final boolean dir) {
- final JTextField field = new JTextField();
- field.setToolTipText(info.getDescription());
- String value = info.getString(false);
- reload(value);
- field.setText(value);
+ /**
+ * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
+ *
+ * @param nhgap
+ * negative horisontal gap in pixel to use for the label, i.e.,
+ * the step lock sized labels will start smaller by that amount
+ * (the use case would be to align controls that start at a
+ * different horisontal position)
+ */
+ public void init(int nhgap) {
+ if (getInfo().isArray()) {
+ this.setLayout(new BorderLayout());
+ add(label(nhgap), BorderLayout.WEST);
+
+ main = new JPanel();
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
+ main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
+ int size = getInfo().getListSize(false);
+ for (int i = 0; i < size; i++) {
+ addItemWithMinusPanel(i);
}
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- String value = field.getText();
- if (isChanged(value)) {
- info.setString(value);
+ main.revalidate();
+ main.repaint();
+
+ final JButton add = new JButton();
+ setImage(add, img64add, "+");
+
+ add.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ addItemWithMinusPanel(base.getFieldsSize());
+ main.revalidate();
+ main.repaint();
}
- }
- });
+ });
- JButton browseButton = new JButton("...");
- browseButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setCurrentDirectory(null);
- chooser.setFileSelectionMode(dir ? JFileChooser.DIRECTORIES_ONLY
- : JFileChooser.FILES_ONLY);
- if (chooser.showOpenDialog(ConfigItem.this) == JFileChooser.APPROVE_OPTION) {
- File file = chooser.getSelectedFile();
- if (file != null) {
- String value = file.getAbsolutePath();
- if (isChanged(value)) {
- info.setString(value);
- }
- field.setText(value);
- }
- }
- }
- });
+ JPanel tmp = new JPanel(new BorderLayout());
+ tmp.add(add, BorderLayout.WEST);
- JPanel pane = new JPanel(new BorderLayout());
- this.add(label(info, nhgap), BorderLayout.WEST);
- pane.add(browseButton, BorderLayout.WEST);
- pane.add(field, BorderLayout.CENTER);
- this.add(pane, BorderLayout.CENTER);
+ JPanel mainPlus = new JPanel(new BorderLayout());
+ mainPlus.add(main, BorderLayout.CENTER);
+ mainPlus.add(tmp, BorderLayout.SOUTH);
- setPreferredSize(pane);
+ add(mainPlus, BorderLayout.CENTER);
+ } else {
+ this.setLayout(new BorderLayout());
+ add(label(nhgap), BorderLayout.WEST);
+
+ JComponent field = base.createField(-1);
+ add(field, BorderLayout.CENTER);
+ }
}
- private void addComboboxField(final MetaInfo<E> info, int nhgap,
- boolean editable) {
- // rawtypes for Java 1.6 (and 1.7 ?) support
- @SuppressWarnings({ "rawtypes", "unchecked" })
- final JComboBox field = new JComboBox(info.getAllowedValues());
- field.setEditable(editable);
- String value = info.getString(false);
- reload(value);
- field.setSelectedItem(value);
-
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setSelectedItem(value);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- String value = field.getSelectedItem().toString();
- if (isChanged(value)) {
- info.setString(value);
- }
- }
- });
+ /** The {@link MetaInfo} linked to the field. */
+ public MetaInfo<E> getInfo() {
+ return base.getInfo();
+ }
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ /**
+ * Retrieve the associated graphical component that was created with
+ * {@link ConfigItemBase#createEmptyField(int)}.
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ *
+ * @return the graphical component
+ */
+ protected JComponent getField(int item) {
+ return base.getField(item);
+ }
- setPreferredSize(field);
+ /**
+ * Manually specify that the given item is "dirty" and thus should be saved
+ * when asked.
+ * <p>
+ * Has no effect if the class is using automatic dirty handling (see
+ * {@link ConfigItemBase#ConfigItem(MetaInfo, boolean)}).
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ */
+ protected void setDirtyItem(int item) {
+ base.setDirtyItem(item);
}
- private void addPasswordField(final MetaInfo<E> info, int nhgap) {
- final JPasswordField field = new JPasswordField();
- field.setToolTipText(info.getDescription());
- String value = info.getString(false);
- reload(value);
- field.setText(value);
+ /**
+ * Check if the value changed since the last load/save into the linked
+ * {@link MetaInfo}.
+ * <p>
+ * Note that we consider NULL and an Empty {@link String} to be equals.
+ *
+ * @param value
+ * the value to test
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ *
+ * @return TRUE if it has
+ */
+ protected boolean hasValueChanged(Object value, int item) {
+ return base.hasValueChanged(value, item);
+ }
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
- }
- });
- info.addSaveListener(new Runnable() {
+ private void addItemWithMinusPanel(int item) {
+ JPanel minusPanel = createMinusPanel(item);
+ JComponent field = base.addItem(item, minusPanel);
+ minusPanel.add(field, BorderLayout.CENTER);
+ }
+
+ private JPanel createMinusPanel(final int item) {
+ JPanel minusPanel = new JPanel(new BorderLayout());
+
+ final JButton remove = new JButton();
+ setImage(remove, img64remove, "-");
+
+ remove.addActionListener(new ActionListener() {
@Override
- public void run() {
- String value = new String(field.getPassword());
- if (isChanged(value)) {
- info.setString(value);
- }
+ public void actionPerformed(ActionEvent e) {
+ base.removeItem(item);
}
});
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ minusPanel.add(remove, BorderLayout.EAST);
+
+ main.add(minusPanel);
+ main.revalidate();
+ main.repaint();
- setPreferredSize(field);
+ return minusPanel;
}
- private void addIntField(final MetaInfo<E> info, int nhgap) {
- final JSpinner field = new JSpinner();
- field.setToolTipText(info.getDescription());
- int value = info.getInteger(true) == null ? 0 : info.getInteger(true);
- reload(value);
- field.setValue(value);
+ /**
+ * Create an empty graphical component to be used later by
+ * {@link ConfigItem#createField(int)}.
+ * <p>
+ * Note that {@link ConfigItem#reload(int)} will be called after it was
+ * created by {@link ConfigItem#createField(int)}.
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ *
+ * @return the graphical component
+ */
+ abstract protected JComponent createEmptyField(int item);
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- int value = info.getInteger(true) == null ? 0 : info
- .getInteger(true);
- reload(value);
- field.setValue(value);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- int value = field.getValue() == null ? 0 : (Integer) field
- .getValue();
- if (isChanged(value)) {
- info.setInteger(value);
- }
- }
- });
+ /**
+ * Get the information from the {@link MetaInfo} in the subclass preferred
+ * format.
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ *
+ * @return the information in the subclass preferred format
+ */
+ abstract protected Object getFromInfo(int item);
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ /**
+ * Set the value to the {@link MetaInfo}.
+ *
+ * @param value
+ * the value in the subclass preferred format
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ */
+ abstract protected void setToInfo(Object value, int item);
- setPreferredSize(field);
- }
+ /**
+ * The value present in the given item's related field in the subclass
+ * preferred format.
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ *
+ * @return the value present in the given item's related field in the
+ * subclass preferred format
+ */
+ abstract protected Object getFromField(int item);
+
+ /**
+ * Set the value (in the subclass preferred format) into the field.
+ *
+ * @param value
+ * the value in the subclass preferred format
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ */
+ abstract protected void setToField(Object value, int item);
/**
* Create a label which width is constrained in lock steps.
*
- * @param info
- * the {@link MetaInfo} for which we want to add a label
* @param nhgap
* negative horisontal gap in pixel to use for the label, i.e.,
* the step lock sized labels will start smaller by that amount
*
* @return the label
*/
- private JComponent label(final MetaInfo<E> info, int nhgap) {
- final JLabel label = new JLabel(info.getName());
+ protected JComponent label(int nhgap) {
+ final JLabel label = new JLabel(getInfo().getName());
Dimension ps = label.getPreferredSize();
if (ps == null) {
ps = label.getSize();
}
+ ps.height = Math.max(ps.height, getMinimumHeight());
+
int w = ps.width;
int step = 150;
for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
@Override
public void run() {
StringBuilder builder = new StringBuilder();
- String text = (info.getDescription().replace("\\n", "\n"))
+ String text = (getInfo().getDescription().replace("\\n", "\n"))
.trim();
for (String line : StringUtils.justifyText(text, 80,
Alignment.LEFT)) {
builder.append(line);
}
text = builder.toString();
- JOptionPane.showMessageDialog(ConfigItem.this, text,
- info.getName(), JOptionPane.INFORMATION_MESSAGE);
+ JOptionPane.showMessageDialog(ConfigItem.this, text, getInfo()
+ .getName(), JOptionPane.INFORMATION_MESSAGE);
}
};
JLabel help = new JLabel("");
help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- try {
- Image img = new Image(infoImage64);
- try {
- BufferedImage bImg = ImageUtilsAwt.fromImage(img);
- help.setIcon(new ImageIcon(bImg));
- } finally {
- img.close();
- }
- } catch (IOException e) {
- // This is an hard-coded image, should not happen
- help.setText("?");
- }
+ setImage(help, img64info, "?");
help.addMouseListener(new MouseAdapter() {
@Override
pane2.add(help, BorderLayout.WEST);
pane2.add(new JLabel(" "), BorderLayout.CENTER);
- JPanel pane = new JPanel(new BorderLayout());
- pane.add(label, BorderLayout.WEST);
- pane.add(pane2, BorderLayout.CENTER);
+ JPanel contentPane = new JPanel(new BorderLayout());
+ contentPane.add(label, BorderLayout.WEST);
+ contentPane.add(pane2, BorderLayout.CENTER);
ps.width = w + 30; // 30 for the (?) sign
- pane.setSize(ps);
- pane.setPreferredSize(ps);
+ contentPane.setSize(ps);
+ contentPane.setPreferredSize(ps);
+
+ JPanel pane = new JPanel(new BorderLayout());
+ pane.add(contentPane, BorderLayout.NORTH);
return pane;
}
/**
- * Return an {@link Icon} to use as a colour badge for the colour field
- * controls.
+ * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
*
- * @param size
- * the size of the badge
- * @param color
- * the colour of the badge, which can be NULL (will return
- * transparent white)
+ * @param <E>
+ * the type of {@link Bundle} to edit
+ *
+ * @param info
+ * the {@link MetaInfo}
+ * @param nhgap
+ * negative horisontal gap in pixel to use for the label, i.e.,
+ * the step lock sized labels will start smaller by that amount
+ * (the use case would be to align controls that start at a
+ * different horisontal position)
*
- * @return the badge
+ * @return the new {@link ConfigItem}
*/
- private Icon getIcon(int size, Integer color) {
- // Allow null values
- if (color == null) {
- color = new Color(255, 255, 255, 255).getRGB();
- }
+ static public <E extends Enum<E>> ConfigItem<E> createItem(
+ MetaInfo<E> info, int nhgap) {
- Color c = new Color(color, true);
- int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
- Color border = (avg >= 128 ? Color.BLACK : Color.WHITE);
+ ConfigItem<E> configItem;
+ switch (info.getFormat()) {
+ case BOOLEAN:
+ configItem = new ConfigItemBoolean<E>(info);
+ break;
+ case COLOR:
+ configItem = new ConfigItemColor<E>(info);
+ break;
+ case FILE:
+ configItem = new ConfigItemBrowse<E>(info, false);
+ break;
+ case DIRECTORY:
+ configItem = new ConfigItemBrowse<E>(info, true);
+ break;
+ case COMBO_LIST:
+ configItem = new ConfigItemCombobox<E>(info, true);
+ break;
+ case FIXED_LIST:
+ configItem = new ConfigItemCombobox<E>(info, false);
+ break;
+ case INT:
+ configItem = new ConfigItemInteger<E>(info);
+ break;
+ case PASSWORD:
+ configItem = new ConfigItemPassword<E>(info);
+ break;
+ case LOCALE:
+ configItem = new ConfigItemLocale<E>(info);
+ break;
+ case STRING:
+ default:
+ configItem = new ConfigItemString<E>(info);
+ break;
+ }
- BufferedImage img = new BufferedImage(size, size,
- BufferedImage.TYPE_4BYTE_ABGR);
+ configItem.init(nhgap);
+ return configItem;
+ }
- Graphics2D g = img.createGraphics();
+ /**
+ * Set an image to the given {@link JButton}, with a fallback text if it
+ * fails.
+ *
+ * @param button
+ * the button to set
+ * @param image64
+ * the image in BASE64 (should be PNG or similar)
+ * @param fallbackText
+ * text to use in case the image cannot be created
+ */
+ static protected void setImage(JLabel button, String image64,
+ String fallbackText) {
try {
- g.setColor(c);
- g.fillRect(0, 0, img.getWidth(), img.getHeight());
- g.setColor(border);
- g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
- } finally {
- g.dispose();
+ Image img = new Image(image64);
+ try {
+ BufferedImage bImg = ImageUtilsAwt.fromImage(img);
+ button.setIcon(new ImageIcon(bImg));
+ } finally {
+ img.close();
+ }
+ } catch (IOException e) {
+ // This is an hard-coded image, should not happen
+ button.setText(fallbackText);
}
+ }
- return new ImageIcon(img);
+ /**
+ * Set an image to the given {@link JButton}, with a fallback text if it
+ * fails.
+ *
+ * @param button
+ * the button to set
+ * @param image64
+ * the image in BASE64 (should be PNG or similar)
+ * @param fallbackText
+ * text to use in case the image cannot be created
+ */
+ static protected void setImage(JButton button, String image64,
+ String fallbackText) {
+ try {
+ Image img = new Image(image64);
+ try {
+ BufferedImage bImg = ImageUtilsAwt.fromImage(img);
+ button.setIcon(new ImageIcon(bImg));
+ } finally {
+ img.close();
+ }
+ } catch (IOException e) {
+ // This is an hard-coded image, should not happen
+ button.setText(fallbackText);
+ }
}
- private void setPreferredSize(JComponent field) {
- JTextField a = new JTextField("Test");
- int height = Math.max(a.getMinimumSize().height,
- field.getMinimumSize().height);
- setPreferredSize(new Dimension(200, height));
+ static private int getMinimumHeight() {
+ if (minimumHeight < 0) {
+ minimumHeight = new JTextField("Test").getMinimumSize().height;
+ }
+
+ return minimumHeight;
}
}