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.ArrayList;
+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;
/**
private static int minimumHeight = -1;
- /** A small (?) blue in PNG, base64 encoded. */
+ /** A small 16x16 "?" blue in PNG, base64 encoded. */
private static String infoImage64 = //
""
+ "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI"
+ "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA"
+ "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC";
+ // A small 16x16 "+" image with colours
+ private static String addImage64 = //
+ ""
+ + "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 removeImage64 = //
+ ""
+ + "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 original value before current changes. */
private Object orig;
+ private List<Integer> dirtyBits;
+
+ private JComponent field;
+ private List<JComponent> fields = new ArrayList<JComponent>();
+
+ /** The {@link MetaInfo} linked to the field. */
+ protected MetaInfo<E> info;
/**
* Create a new {@link ConfigItem} for the given {@link MetaInfo}.
* different horisontal position)
*/
public ConfigItem(MetaInfo<E> info, int nhgap) {
- this.setLayout(new BorderLayout());
+ this(info, true);
- // TODO: support arrays
- Format fmt = info.getFormat();
- if (info.isArray()) {
- fmt = Format.STRING;
- }
-
- switch (fmt) {
+ ConfigItem<E> configItem;
+ switch (info.getFormat()) {
case BOOLEAN:
- addBooleanField(info, nhgap);
+ configItem = new ConfigItemBoolean<E>(info);
break;
case COLOR:
- addColorField(info, nhgap);
+ configItem = new ConfigItemColor<E>(info);
break;
case FILE:
- addBrowseField(info, nhgap, false);
+ configItem = new ConfigItemBrowse<E>(info, false);
break;
case DIRECTORY:
- addBrowseField(info, nhgap, true);
+ configItem = new ConfigItemBrowse<E>(info, true);
break;
case COMBO_LIST:
- addComboboxField(info, nhgap, true);
+ configItem = new ConfigItemCombobox<E>(info, true);
break;
case FIXED_LIST:
- addComboboxField(info, nhgap, false);
+ configItem = new ConfigItemCombobox<E>(info, false);
break;
case INT:
- addIntField(info, nhgap);
+ configItem = new ConfigItemInteger<E>(info);
break;
case PASSWORD:
- addPasswordField(info, nhgap);
+ configItem = new ConfigItemPassword<E>(info);
+ break;
+ case LOCALE:
+ configItem = new ConfigItemLocale<E>(info);
break;
case STRING:
- case LOCALE: // TODO?
default:
- addStringField(info, nhgap);
+ configItem = new ConfigItemString<E>(info);
break;
}
- }
- private void reload(Object value) {
- // We consider "" and NULL to be equals
- if ("".equals(value)) {
- value = null;
- }
- orig = value;
- }
+ if (info.isArray()) {
+ this.setLayout(new BorderLayout());
+ add(label(nhgap), BorderLayout.WEST);
+
+ final JPanel main = new JPanel();
+ main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
+ int size = info.getListSize(false);
+ for (int i = 0; i < size; i++) {
+ JComponent field = configItem.createComponent(i);
+ main.add(field);
+ }
- private boolean isChanged(Object newValue) {
- // We consider "" and NULL to be equals
- if ("".equals(newValue)) {
- newValue = null;
- }
+ final JButton add = new JButton();
+ setImage(add, addImage64, "+");
- if (newValue == null) {
- return orig != null;
- }
+ final ConfigItem<E> fconfigItem = configItem;
+ add.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ JComponent field = fconfigItem
+ .createComponent(fconfigItem.info
+ .getListSize(false));
+ main.add(field);
- return !newValue.equals(orig);
- }
+ main.revalidate();
+ main.repaint();
+ }
+ });
- 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);
+ JPanel tmp = new JPanel(new BorderLayout());
+ tmp.add(add, BorderLayout.WEST);
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- String value = field.getText();
- if (isChanged(value)) {
- info.setString(value);
- }
- }
- });
+ JPanel mainPlus = new JPanel(new BorderLayout());
+ mainPlus.add(main, BorderLayout.CENTER);
+ mainPlus.add(tmp, BorderLayout.SOUTH);
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ add(mainPlus, BorderLayout.CENTER);
+ } else {
+ this.setLayout(new BorderLayout());
+ add(label(nhgap), BorderLayout.WEST);
- setPreferredSize(field);
+ JComponent field = configItem.createComponent(-1);
+ add(field, BorderLayout.CENTER);
+ }
}
- 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;
+ /**
+ * Prepare a new {@link ConfigItem} instance, linked to the given
+ * {@link MetaInfo}.
+ *
+ * @param info
+ * 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)}
+ */
+ protected ConfigItem(MetaInfo<E> info, boolean autoDirtyHandling) {
+ this.info = info;
+ if (!autoDirtyHandling) {
+ dirtyBits = new ArrayList<Integer>();
}
+ }
- 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);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- boolean state = field.isSelected();
- if (isChanged(state)) {
- info.setBoolean(state);
- }
- }
- });
+ /**
+ * Create an empty graphical component to be used later by
+ * {@link ConfigItem#getField(int)}.
+ * <p>
+ * Note that {@link ConfigItem#reload(int)} will be called after it was
+ * created.
+ *
+ * @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 createField(@SuppressWarnings("unused") int item) {
+ // Not used by the main class, only the sublasses
+ return null;
+ }
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ /**
+ * 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
+ */
+ protected Object getFromInfo(@SuppressWarnings("unused") int item) {
+ // Not used by the main class, only the subclasses
+ return null;
+ }
- setPreferredSize(field);
+ /**
+ * 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)
+ */
+ protected void setToInfo(@SuppressWarnings("unused") Object value,
+ @SuppressWarnings("unused") int item) {
+ // Not used by the main class, only the subclasses
}
- 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);
+ /**
+ * @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
+ */
+ protected Object getFromField(@SuppressWarnings("unused") int item) {
+ // Not used by the main class, only the subclasses
+ return null;
+ }
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- String value = field.getText();
- if (isChanged(value)) {
- info.setString(value);
- }
- }
- });
+ /**
+ * 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)
+ */
+ protected void setToField(@SuppressWarnings("unused") Object value,
+ @SuppressWarnings("unused") int item) {
+ // Not used by the main class, only the subclasses
+ }
- this.add(label(info, nhgap), BorderLayout.WEST);
- JPanel pane = new JPanel(new BorderLayout());
+ /**
+ * Create a new field for the given graphical component at the given index
+ * (note that the component is usually 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)
+ * @param field
+ * the graphical component
+ */
+ private void setField(int item, JComponent field) {
+ if (item < 0) {
+ this.field = field;
+ return;
+ }
- 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);
+ for (int i = fields.size(); i <= item; i++) {
+ fields.add(null);
+ }
- setPreferredSize(pane);
+ fields.set(item, field);
}
- 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);
-
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- String value = field.getText();
- if (isChanged(value)) {
- info.setString(value);
- }
- }
- });
-
- 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);
- }
- }
- }
- });
+ /**
+ * Retrieve the associated graphical component that was created with
+ * {@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
+ */
+ protected JComponent getField(int item) {
+ if (item < 0) {
+ return field;
+ }
- 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);
+ if (item < fields.size()) {
+ return fields.get(item);
+ }
- setPreferredSize(pane);
+ return null;
}
- 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() {
- Object item = field.getSelectedItem();
- String value = item == null ? null : item.toString();
- if (isChanged(value)) {
- info.setString(value);
- }
- }
- });
-
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ /**
+ * 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 ConfigItem#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) {
+ if (dirtyBits != null) {
+ dirtyBits.add(item);
+ }
+ }
- setPreferredSize(field);
+ /**
+ * 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
+ *
+ * @return TRUE if it has
+ */
+ protected boolean hasValueChanged(Object value) {
+ // We consider "" and NULL to be equals
+ return !orig.equals(value == null ? "" : value);
}
- 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);
+ /**
+ * Reload the values to what they currently are in the {@link MetaInfo}.
+ *
+ * @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 reload(int item) {
+ Object value = getFromInfo(item);
+ setToField(value, item);
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- String value = info.getString(false);
- reload(value);
- field.setText(value);
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- String value = new String(field.getPassword());
- if (isChanged(value)) {
- info.setString(value);
- }
- }
- });
+ // We consider "" and NULL to be equals
+ orig = (value == null ? "" : value);
+ }
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
+ /**
+ * If the item has been modified, set the {@link MetaInfo} to dirty then
+ * modify it to, reflect the changes so it can be saved later.
+ * <p>
+ * This method does <b>not</b> call {@link MetaInfo#save(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 save(int item) {
+ Object value = getFromField(item);
+
+ boolean dirty = false;
+ if (dirtyBits != null) {
+ dirty = dirtyBits.remove((Integer) item);
+ } else {
+ // We consider "" and NULL to be equals
+ dirty = hasValueChanged(value);
+ }
- setPreferredSize(field);
+ if (dirty) {
+ info.setDirty();
+ setToInfo(value, item);
+ orig = (value == null ? "" : value);
+ }
}
- 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);
+ /**
+ *
+ * @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)
+ * @param addTo
+ * @param nhgap
+ *
+ * @return
+ */
+ protected JComponent createComponent(final int item) {
+ setField(item, createField(item));
+ reload(item);
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- int value = info.getInteger(true) == null ? 0 : info
- .getInteger(true);
- reload(value);
- field.setValue(value);
+ reload(item);
}
});
info.addSaveListener(new Runnable() {
@Override
public void run() {
- int value = field.getValue() == null ? 0 : (Integer) field
- .getValue();
- if (isChanged(value)) {
- info.setInteger(value);
- }
+ save(item);
}
});
- this.add(label(info, nhgap), BorderLayout.WEST);
- this.add(field, BorderLayout.CENTER);
-
+ JComponent field = getField(item);
setPreferredSize(field);
+
+ return field;
}
/**
* 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) {
+ protected JComponent label(int nhgap) {
final JLabel label = new JLabel(info.getName());
Dimension ps = label.getPreferredSize();
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, infoImage64, "?");
help.addMouseListener(new MouseAdapter() {
@Override
return pane;
}
- /**
- * Return an {@link Icon} to use as a colour badge for the colour field
- * controls.
- *
- * @param size
- * the size of the badge
- * @param color
- * the colour of the badge, which can be NULL (will return
- * transparent white)
- *
- * @return the badge
- */
- private Icon getIcon(int size, Integer color) {
- // Allow null values
- if (color == null) {
- color = new Color(255, 255, 255, 255).getRGB();
- }
-
- Color c = new Color(color, true);
- int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
- Color border = (avg >= 128 ? Color.BLACK : Color.WHITE);
-
- BufferedImage img = new BufferedImage(size, size,
- BufferedImage.TYPE_4BYTE_ABGR);
-
- Graphics2D g = img.createGraphics();
- 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();
- }
-
- return new ImageIcon(img);
- }
-
- private void setPreferredSize(JComponent field) {
+ protected void setPreferredSize(JComponent field) {
int height = Math
.max(getMinimumHeight(), field.getMinimumSize().height);
setPreferredSize(new Dimension(200, height));
return minimumHeight;
}
+
+ /**
+ * 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 private void setImage(JLabel 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);
+ }
+ }
+
+ /**
+ * 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 private 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);
+ }
+ }
}