X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FConfigItem.java;h=f974969335c2a5b2d725e5682cfe04e8e47420ad;hb=d5026c096121da14c20d69893520594a36d088bb;hp=9769ab164eb7e1377b34d744bb10873ac3ce8d51;hpb=76b51de96af0b8dfa9615db37823fffd093fcfe3;p=fanfix.git diff --git a/src/be/nikiroo/utils/ui/ConfigItem.java b/src/be/nikiroo/utils/ui/ConfigItem.java index 9769ab1..f974969 100644 --- a/src/be/nikiroo/utils/ui/ConfigItem.java +++ b/src/be/nikiroo/utils/ui/ConfigItem.java @@ -1,34 +1,30 @@ 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.InputVerifier; 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.JTextField; -import javax.swing.plaf.basic.BasicArrowButton; +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; /** @@ -47,335 +43,393 @@ import be.nikiroo.utils.resources.MetaInfo; public class ConfigItem> extends JPanel { private static final long serialVersionUID = 1L; + private static int minimumHeight = -1; + + /** A small (?) blue in PNG, base64 encoded. */ + private static String infoImage64 = // + "" + + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI" + + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wURFRg6IrtcdgAAATdJREFUOMvtkj8sQ1EUxr9z/71G" + + "m1RDogYxq7WDDYMYTSajSG4n6YRYzSaSLibWbiaDIGwdiLIYDFKDNJEgKu969xi8UNHy7H7LPcN3" + + "v/Odcy+hG9oOIeIcBCJS9MAvlZtOMtHxsrFrJHGqe0RVGnHAHpcIbPlng8BS3HmKBJYzabGUzcrJ" + + "XK+ckIrqANYR2JEv2nYDEVck0WKGfHzyq82Go+btxoX3XAcAIqTj8wPqOH6mtMeM4bGCLhyfhTMA" + + "qlLhKHqujCfaweCAmV0p50dPzsNpEKpK01V/n55HIvTnfDC2odKlfeYadZN/T+AqDACUsnkhqaU1" + + "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA" + + "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC"; + + /** The original value before current changes. */ + private Object orig; + private List dirtyBits; + + protected MetaInfo info; + + private JComponent field; + private List fields = new ArrayList(); + /** * Create a new {@link ConfigItem} for 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) */ - public ConfigItem(MetaInfo info) { - this.setLayout(new BorderLayout()); - - // TODO: support arrays - Format fmt = info.getFormat(); - if (info.isArray()) { - fmt = Format.STRING; - } + public ConfigItem(MetaInfo info, int nhgap) { + this(info, true); - switch (fmt) { + ConfigItem configItem; + switch (info.getFormat()) { case BOOLEAN: - addBooleanField(info); + configItem = new ConfigItemBoolean(info); break; case COLOR: - addColorField(info); + configItem = new ConfigItemColor(info); break; case FILE: - addBrowseField(info, false); + configItem = new ConfigItemBrowse(info, false); break; case DIRECTORY: - addBrowseField(info, true); + configItem = new ConfigItemBrowse(info, true); break; case COMBO_LIST: - addComboboxField(info, true); + configItem = new ConfigItemCombobox(info, true); break; case FIXED_LIST: - addComboboxField(info, false); + configItem = new ConfigItemCombobox(info, false); break; case INT: - addIntField(info); + configItem = new ConfigItemInteger(info); break; case PASSWORD: - addPasswordField(info); + configItem = new ConfigItemPassword(info); break; case STRING: case LOCALE: // TODO? default: - addStringField(info); + configItem = new ConfigItemString(info); break; } - } - private void addStringField(final MetaInfo info) { - final JTextField field = new JTextField(); - field.setToolTipText(info.getDescription()); - field.setText(info.getString()); - - info.addReloadedListener(new Runnable() { - @Override - public void run() { - field.setText(info.getString()); - } - }); - info.addSaveListener(new Runnable() { - @Override - public void run() { - info.setString(field.getText()); + 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); } - }); - this.add(label(info), BorderLayout.WEST); - this.add(field, BorderLayout.CENTER); - } + // TODO: image + final JButton add = new JButton("+"); + final ConfigItem fconfigItem = configItem; + add.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JComponent field = fconfigItem + .createComponent(fconfigItem.info + .getListSize(false)); + main.add(field); + + // TODO this doesn't woooooorkk + add.invalidate(); + field.invalidate(); + main.invalidate(); + ConfigItem.this.repaint(); + ConfigItem.this.validate(); + ConfigItem.this.repaint(); + } + }); + + JPanel tmp = new JPanel(new BorderLayout()); + tmp.add(add, BorderLayout.WEST); + + JPanel mainPlus = new JPanel(new BorderLayout()); + mainPlus.add(main, BorderLayout.CENTER); + mainPlus.add(tmp, BorderLayout.SOUTH); - private void addBooleanField(final MetaInfo info) { - final JCheckBox field = new JCheckBox(); - field.setToolTipText(info.getDescription()); - Boolean state = info.getBoolean(); - if (state == null) { - info.getDefaultBoolean(); + add(mainPlus, BorderLayout.CENTER); + } else { + this.setLayout(new BorderLayout()); + add(label(nhgap), BorderLayout.WEST); + + JComponent field = configItem.createComponent(-1); + add(field, BorderLayout.CENTER); } + } - // 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 info, boolean autoDirtyHandling) { + this.info = info; + if (!autoDirtyHandling) { + dirtyBits = new ArrayList(); } + } - field.setSelected(state); + /** + * Create an empty graphical component to be used later by + * {@link ConfigItem#getField(int)}. + *

+ * 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; + } - info.addReloadedListener(new Runnable() { - @Override - public void run() { - Boolean state = info.getBoolean(); - if (state == null) { - info.getDefaultBoolean(); - } - if (state == null) { - state = false; - } + /** + * 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; + } - field.setSelected(state); - } - }); - info.addSaveListener(new Runnable() { - @Override - public void run() { - info.setBoolean(field.isSelected()); - } - }); + /** + * 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 + } - field.setText(info.getName()); - this.add(field, BorderLayout.CENTER); + /** + * @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; } - private void addColorField(final MetaInfo info) { - final JTextField field = new JTextField(); - field.setToolTipText(info.getDescription()); - field.setText(info.getString()); + /** + * 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 + } - info.addReloadedListener(new Runnable() { - @Override - public void run() { - field.setText(info.getString()); - } - }); - info.addSaveListener(new Runnable() { - @Override - public void run() { - info.setString(field.getText()); - } - }); + /** + * 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; + } - this.add(label(info), BorderLayout.WEST); - JPanel pane = new JPanel(new BorderLayout()); + for (int i = fields.size(); i <= item; i++) { + fields.add(null); + } - final JButton colorWheel = new JButton(); - colorWheel.setIcon(getIcon(17, info.getColor())); - colorWheel.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - Color initialColor = new Color(info.getColor(), true); - Color newColor = JColorChooser.showDialog(ConfigItem.this, - info.getName(), initialColor); - if (newColor != null) { - info.setColor(newColor.getRGB()); - field.setText(info.getString()); - colorWheel.setIcon(getIcon(17, info.getColor())); - } - } - }); - pane.add(colorWheel, BorderLayout.WEST); - pane.add(field, BorderLayout.CENTER); - this.add(pane, BorderLayout.CENTER); + fields.set(item, field); } - private void addBrowseField(final MetaInfo info, final boolean dir) { - final JTextField field = new JTextField(); - field.setToolTipText(info.getDescription()); - field.setText(info.getString()); + /** + * 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; + } - info.addReloadedListener(new Runnable() { - @Override - public void run() { - field.setText(info.getString()); - } - }); - info.addSaveListener(new Runnable() { - @Override - public void run() { - info.setString(field.getText()); - } - }); + if (item < fields.size()) { + return fields.get(item); + } - 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) { - info.setString(file.getAbsolutePath()); - field.setText(info.getString()); - } - } - } - }); + return null; + } - JPanel pane = new JPanel(new BorderLayout()); - this.add(label(info), BorderLayout.WEST); - pane.add(browseButton, BorderLayout.WEST); - pane.add(field, BorderLayout.CENTER); - this.add(pane, BorderLayout.CENTER); + /** + * Manually specify that the given item is "dirty" and thus should be saved + * when asked. + *

+ * 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); + } } - private void addComboboxField(final MetaInfo info, boolean editable) { - // rawtypes for Java 1.6 (and 1.7 ?) support - @SuppressWarnings({ "rawtypes", "unchecked" }) - final JComboBox field = new JComboBox(info.getAllowedValues()); - field.setEditable(editable); - field.setSelectedItem(info.getString()); + /** + * Check if the value changed since the last load/save into the linked + * {@link MetaInfo}. + *

+ * 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); + } - info.addReloadedListener(new Runnable() { - @Override - public void run() { - field.setSelectedItem(info.getString()); - } - }); - info.addSaveListener(new Runnable() { - @Override - public void run() { - info.setString(field.getSelectedItem().toString()); - } - }); + /** + * 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); - this.add(label(info), BorderLayout.WEST); - this.add(field, BorderLayout.CENTER); + // We consider "" and NULL to be equals + orig = (value == null ? "" : value); } - private void addPasswordField(final MetaInfo info) { - final JPasswordField field = new JPasswordField(); - field.setToolTipText(info.getDescription()); - field.setText(info.getString()); - - info.addReloadedListener(new Runnable() { - @Override - public void run() { - field.setText(info.getString()); - } - }); - info.addSaveListener(new Runnable() { - @Override - public void run() { - info.setString(new String(field.getPassword())); - } - }); + /** + * 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. + *

+ * This method does not 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); + } - this.add(label(info), BorderLayout.WEST); - this.add(field, BorderLayout.CENTER); + if (dirty) { + info.setDirty(); + setToInfo(value, item); + orig = (value == null ? "" : value); + } } - private void addIntField(final MetaInfo info) { - final JTextField field = new JTextField(); - field.setToolTipText(info.getDescription()); - field.setText(info.getString()); - field.setInputVerifier(new InputVerifier() { - @Override - public boolean verify(JComponent input) { - String text = field.getText().trim(); - if (text.startsWith("-")) { - text = text.substring(1).trim(); - } - - return text.replaceAll("[0-9]", "").isEmpty(); - } - }); + /** + * + * @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 + */ + protected JComponent createComponent(final int item) { + setField(item, createField(item)); + reload(item); info.addReloadedListener(new Runnable() { @Override public void run() { - field.setText(info.getString()); + reload(item); } }); info.addSaveListener(new Runnable() { @Override public void run() { - info.setString(field.getText()); - Integer value = info.getInteger(); - if (value == null) { - info.setString(""); - } else { - info.setInteger(value); - } - field.setText(info.getString()); + save(item); } }); - JButton up = new BasicArrowButton(BasicArrowButton.NORTH); - JButton down = new BasicArrowButton(BasicArrowButton.SOUTH); - - up.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent ae) { - int value = 0; - try { - value = Integer.parseInt(field.getText()); - } catch (NumberFormatException e) { - } - - field.setText(Integer.toString(value + 1)); - } - }); - - down.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent ae) { - int value = 0; - try { - value = Integer.parseInt(field.getText()); - } catch (NumberFormatException e) { - } - - field.setText(Integer.toString(value - 1)); - } - }); + JComponent field = getField(item); + setPreferredSize(field); - JPanel upDown = new JPanel(new BorderLayout()); - upDown.add(up, BorderLayout.NORTH); - upDown.add(down, BorderLayout.SOUTH); - - JPanel pane = new JPanel(new BorderLayout()); - pane.add(upDown, BorderLayout.WEST); - pane.add(field, BorderLayout.CENTER); - - this.add(label(info), BorderLayout.WEST); - this.add(pane, BorderLayout.CENTER); + 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 + * (the use case would be to align controls that start at a + * different horisontal position) * * @return the label */ - private JComponent label(final MetaInfo info) { + protected JComponent label(int nhgap) { final JLabel label = new JLabel(info.getName()); Dimension ps = label.getPreferredSize(); @@ -383,22 +437,23 @@ public class ConfigItem> extends JPanel { ps = label.getSize(); } + ps.height = Math.max(ps.height, getMinimumHeight()); + int w = ps.width; - int step = 80; - for (int i = 2 * step; i < 10 * step; i += step) { + int step = 150; + for (int i = 2 * step - nhgap; i < 10 * step; i += step) { if (w < i) { w = i; break; } } - // TODO: image - JButton help = new JButton("?"); - help.addActionListener(new ActionListener() { + final Runnable showInfo = new Runnable() { @Override - public void actionPerformed(ActionEvent e) { + public void run() { StringBuilder builder = new StringBuilder(); - String text = info.getDescription().replace("\\n", "\n"); + String text = (info.getDescription().replace("\\n", "\n")) + .trim(); for (String line : StringUtils.justifyText(text, 80, Alignment.LEFT)) { if (builder.length() > 0) { @@ -407,7 +462,30 @@ public class ConfigItem> extends JPanel { builder.append(line); } text = builder.toString(); - JOptionPane.showMessageDialog(ConfigItem.this, text); + JOptionPane.showMessageDialog(ConfigItem.this, text, + info.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("?"); + } + + help.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + showInfo.run(); } }); @@ -415,46 +493,31 @@ public class ConfigItem> extends JPanel { 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. - * - * @param size - * the size of the badge - * @param color - * the colour of the badge - * - * @return the badge - */ - private Icon getIcon(int size, int color) { - 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); + protected void setPreferredSize(JComponent field) { + int height = Math + .max(getMinimumHeight(), field.getMinimumSize().height); + setPreferredSize(new Dimension(200, height)); + } - 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(); + static private int getMinimumHeight() { + if (minimumHeight < 0) { + minimumHeight = new JTextField("Test").getMinimumSize().height; } - return new ImageIcon(img); + return minimumHeight; } }