description = null;
}
}
-
if (description == null) {
description = meta.description();
if (description == null) {
description = "";
}
- if (meta.info() != null && !meta.info().isEmpty()) {
- if (!description.isEmpty()) {
- description += "\n\n";
- }
- description += meta.info();
+ }
+
+ String name = idToName(id, null);
+
+ // Special rules for groups:
+ if (meta.group()) {
+ String groupName = description.split("\n")[0];
+ description = description.substring(groupName.length()).trim();
+ if (!groupName.isEmpty()) {
+ name = groupName;
}
}
- String name = id.toString();
- if (name.length() > 1) {
- name = name.substring(0, 1) + name.substring(1).toLowerCase();
- name = name.replace("_", " ");
+ if (meta.def() != null && !meta.def().isEmpty()) {
+ if (!description.isEmpty()) {
+ description += "\n\n";
+ }
+ description += "(Default value: " + meta.def() + ")";
}
this.name = name;
}
/**
- * The name of this item, deduced from its ID.
+ * For normal items, this is the name of this item, deduced from its ID (or
+ * in other words, it is the ID but presented in a displayable form).
+ * <p>
+ * For group items, this is the first line of the description if it is not
+ * empty (else, it is the ID in the same way as normal items).
* <p>
- * In other words, it is the ID but presented in a displayable form.
+ * Never NULL.
*
- * @return the name
+ *
+ * @return the name, never NULL
*/
public String getName() {
return name;
}
/**
- * The description of this item (information to present to the user).
+ * A description for this item: what it is or does, how to explain that item
+ * to the user including what can be used here (i.e., %s = file name, %d =
+ * file size...).
+ * <p>
+ * For group, the first line ('\\n'-separated) will be used as a title while
+ * the rest will be the description.
+ * <p>
+ * If a default value is known, it will be specified here, too.
+ * <p>
+ * Never NULL.
*
- * @return the description
+ * @return the description, not NULL
*/
public String getDescription() {
return description;
* The allowed list of values that a {@link Format#FIXED_LIST} item is
* allowed to be, or a list of suggestions for {@link Format#COMBO_LIST}
* items.
+ * <p>
+ * Will always allow an empty string in addition to the rest.
*
* @return the list of values
*/
public String[] getAllowedValues() {
- return meta.list();
+ String[] list = meta.list();
+
+ String[] withEmpty = new String[list.length + 1];
+ withEmpty[0] = "";
+ for (int i = 0; i < list.length; i++) {
+ withEmpty[i + 1] = list[i];
+ }
+
+ return withEmpty;
}
/**
/**
* The value stored by this item, as a {@link String}.
*
+ * @param useDefaultIfEmpty
+ * use the default value instead of NULL if the setting is not
+ * set
+ *
* @return the value
*/
- public String getString() {
+ public String getString(boolean useDefaultIfEmpty) {
+ if (value == null && useDefaultIfEmpty) {
+ return getDefaultString();
+ }
+
return value;
}
/**
* The value stored by this item, as a {@link Boolean}.
*
+ * @param useDefaultIfEmpty
+ * use the default value instead of NULL if the setting is not
+ * set
+ *
* @return the value
*/
- public Boolean getBoolean() {
- return BundleHelper.parseBoolean(getString());
+ public Boolean getBoolean(boolean useDefaultIfEmpty) {
+ return BundleHelper.parseBoolean(getString(useDefaultIfEmpty));
}
/**
/**
* The value stored by this item, as a {@link Character}.
*
+ * @param useDefaultIfEmpty
+ * use the default value instead of NULL if the setting is not
+ * set
+ *
* @return the value
*/
- public Character getCharacter() {
- return BundleHelper.parseCharacter(getString());
+ public Character getCharacter(boolean useDefaultIfEmpty) {
+ return BundleHelper.parseCharacter(getString(useDefaultIfEmpty));
}
/**
/**
* The value stored by this item, as an {@link Integer}.
*
+ * @param useDefaultIfEmpty
+ * use the default value instead of NULL if the setting is not
+ * set
+ *
* @return the value
*/
- public Integer getInteger() {
- return BundleHelper.parseInteger(getString());
+ public Integer getInteger(boolean useDefaultIfEmpty) {
+ return BundleHelper.parseInteger(getString(useDefaultIfEmpty));
}
/**
* <p>
* The returned colour value is an ARGB value.
*
+ * @param useDefaultIfEmpty
+ * use the default value instead of NULL if the setting is not
+ * set
+ *
* @return the value
*/
- public Integer getColor() {
- return BundleHelper.parseColor(getString());
+ public Integer getColor(boolean useDefaultIfEmpty) {
+ return BundleHelper.parseColor(getString(useDefaultIfEmpty));
}
/**
* The list of values is comma-separated and each value is surrounded by
* double-quotes; backslashes and double-quotes are escaped by a backslash.
*
+ * @param useDefaultIfEmpty
+ * use the default value instead of NULL if the setting is not
+ * set
+ *
* @return the value
*/
- public List<String> getList() {
- return BundleHelper.parseList(getString());
+ public List<String> getList(boolean useDefaultIfEmpty) {
+ return BundleHelper.parseList(getString(useDefaultIfEmpty));
}
/**
if (parent != null) {
list.remove(i--);
parent.children.add(info);
+ info.name = idToName(info.id, parent.id);
}
}
return group;
}
+
+ static private <E extends Enum<E>> String idToName(E id, E prefix) {
+ String name = id.toString();
+ if (prefix != null && name.startsWith(prefix.toString())) {
+ name = name.substring(prefix.toString().length());
+ }
+
+ if (name.length() > 0) {
+ name = name.substring(0, 1).toUpperCase()
+ + name.substring(1).toLowerCase();
+ }
+
+ name = name.replace("_", " ");
+
+ return name.trim();
+ }
}
package be.nikiroo.utils.ui;
import java.awt.BorderLayout;
+import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
+import javax.swing.border.TitledBorder;
+import be.nikiroo.utils.StringUtils;
import be.nikiroo.utils.resources.Bundle;
import be.nikiroo.utils.resources.MetaInfo;
this.add(scroll, BorderLayout.CENTER);
main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
-
+ main.setBorder(new EmptyBorder(5, 5, 5, 5));
+
main.add(new JLabel(title));
items = MetaInfo.getItems(type, bundle);
for (MetaInfo<E> item : items) {
- addItem(main, item);
+ addItem(main, item, 0);
}
main.add(createButton("Reset", new ActionListener() {
}));
}
- private void addItem(JPanel main, MetaInfo<E> item) {
+ private void addItem(JPanel main, MetaInfo<E> item, int nhgap) {
if (item.isGroup()) {
- // TODO
+ JPanel bpane = new JPanel(new BorderLayout());
+ bpane.setBorder(new TitledBorder(item.getName()));
+ JPanel pane = new JPanel();
+ pane.setBorder(new EmptyBorder(5, 5, 5, 5));
+ pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
+
+ String info = item.getDescription();
+ info = StringUtils.justifyTexts(info, 100);
+ if (!info.isEmpty()) {
+ JTextArea text = new JTextArea(info);
+ text.setWrapStyleWord(true);
+ text.setOpaque(false);
+ text.setForeground(new Color(100, 100, 180));
+ text.setEditable(false);
+ pane.add(text);
+ }
+
for (MetaInfo<E> subitem : item) {
- addItem(main, subitem);
+ addItem(pane, subitem, nhgap + 11);
}
+ bpane.add(pane, BorderLayout.CENTER);
+ main.add(bpane);
} else {
- main.add(new ConfigItem<E>(item));
+ main.add(new ConfigItem<E>(item, nhgap));
}
}
import javax.swing.Icon;
import javax.swing.ImageIcon;
-import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
+import javax.swing.JSpinner;
import javax.swing.JTextField;
-import javax.swing.plaf.basic.BasicArrowButton;
import be.nikiroo.utils.Image;
import be.nikiroo.utils.StringUtils;
*
* @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<E> info) {
+ public ConfigItem(MetaInfo<E> info, int nhgap) {
this.setLayout(new BorderLayout());
// TODO: support arrays
switch (fmt) {
case BOOLEAN:
- addBooleanField(info);
+ addBooleanField(info, nhgap);
break;
case COLOR:
- addColorField(info);
+ addColorField(info, nhgap);
break;
case FILE:
- addBrowseField(info, false);
+ addBrowseField(info, nhgap, false);
break;
case DIRECTORY:
- addBrowseField(info, true);
+ addBrowseField(info, nhgap, true);
break;
case COMBO_LIST:
- addComboboxField(info, true);
+ addComboboxField(info, nhgap, true);
break;
case FIXED_LIST:
- addComboboxField(info, false);
+ addComboboxField(info, nhgap, false);
break;
case INT:
- addIntField(info);
+ addIntField(info, nhgap);
break;
case PASSWORD:
- addPasswordField(info);
+ addPasswordField(info, nhgap);
break;
case STRING:
case LOCALE: // TODO?
default:
- addStringField(info);
+ addStringField(info, nhgap);
break;
}
}
- private void addStringField(final MetaInfo<E> info) {
+ private void addStringField(final MetaInfo<E> info, int nhgap) {
final JTextField field = new JTextField();
field.setToolTipText(info.getDescription());
- field.setText(info.getString());
+ field.setText(info.getString(false));
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- field.setText(info.getString());
+ field.setText(info.getString(false));
}
});
info.addSaveListener(new Runnable() {
}
});
- this.add(label(info), BorderLayout.WEST);
+ this.add(label(info, nhgap), BorderLayout.WEST);
this.add(field, BorderLayout.CENTER);
+
+ setPreferredSize(field);
}
- private void addBooleanField(final MetaInfo<E> info) {
+ private void addBooleanField(final MetaInfo<E> info, int nhgap) {
final JCheckBox field = new JCheckBox();
field.setToolTipText(info.getDescription());
- Boolean state = info.getBoolean();
- if (state == null) {
- info.getDefaultBoolean();
- }
+ Boolean state = info.getBoolean(true);
// Should not happen!
if (state == null) {
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- Boolean state = info.getBoolean();
- if (state == null) {
- info.getDefaultBoolean();
- }
+ Boolean state = info.getBoolean(true);
if (state == null) {
state = false;
}
}
});
- this.add(label(info), BorderLayout.WEST);
+ this.add(label(info, nhgap), BorderLayout.WEST);
this.add(field, BorderLayout.CENTER);
+
+ setPreferredSize(field);
}
- private void addColorField(final MetaInfo<E> info) {
+ private void addColorField(final MetaInfo<E> info, int nhgap) {
final JTextField field = new JTextField();
field.setToolTipText(info.getDescription());
- field.setText(info.getString());
+ field.setText(info.getString(false));
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- field.setText(info.getString());
+ field.setText(info.getString(false));
}
});
info.addSaveListener(new Runnable() {
}
});
- this.add(label(info), BorderLayout.WEST);
+ this.add(label(info, nhgap), BorderLayout.WEST);
JPanel pane = new JPanel(new BorderLayout());
final JButton colorWheel = new JButton();
- colorWheel.setIcon(getIcon(17, info.getColor()));
+ colorWheel.setIcon(getIcon(17, info.getColor(true)));
colorWheel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- Color initialColor = new Color(info.getColor(), true);
+ Color initialColor = new Color(info.getColor(true), 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()));
+ 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);
+
+ setPreferredSize(pane);
}
- private void addBrowseField(final MetaInfo<E> info, final boolean dir) {
+ private void addBrowseField(final MetaInfo<E> info, int nhgap,
+ final boolean dir) {
final JTextField field = new JTextField();
field.setToolTipText(info.getDescription());
- field.setText(info.getString());
+ field.setText(info.getString(false));
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- field.setText(info.getString());
+ field.setText(info.getString(false));
}
});
info.addSaveListener(new Runnable() {
File file = chooser.getSelectedFile();
if (file != null) {
info.setString(file.getAbsolutePath());
- field.setText(info.getString());
+ field.setText(info.getString(false));
}
}
}
});
JPanel pane = new JPanel(new BorderLayout());
- this.add(label(info), BorderLayout.WEST);
+ this.add(label(info, nhgap), BorderLayout.WEST);
pane.add(browseButton, BorderLayout.WEST);
pane.add(field, BorderLayout.CENTER);
this.add(pane, BorderLayout.CENTER);
+
+ setPreferredSize(pane);
}
- private void addComboboxField(final MetaInfo<E> info, boolean editable) {
+ 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);
- field.setSelectedItem(info.getString());
+ field.setSelectedItem(info.getString(false));
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- field.setSelectedItem(info.getString());
+ field.setSelectedItem(info.getString(false));
}
});
info.addSaveListener(new Runnable() {
}
});
- this.add(label(info), BorderLayout.WEST);
+ this.add(label(info, nhgap), BorderLayout.WEST);
this.add(field, BorderLayout.CENTER);
+
+ setPreferredSize(field);
}
- private void addPasswordField(final MetaInfo<E> info) {
+ private void addPasswordField(final MetaInfo<E> info, int nhgap) {
final JPasswordField field = new JPasswordField();
field.setToolTipText(info.getDescription());
- field.setText(info.getString());
+ field.setText(info.getString(true));
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- field.setText(info.getString());
+ field.setText(info.getString(false));
}
});
info.addSaveListener(new Runnable() {
}
});
- this.add(label(info), BorderLayout.WEST);
+ this.add(label(info, nhgap), BorderLayout.WEST);
this.add(field, BorderLayout.CENTER);
+
+ setPreferredSize(field);
}
- private void addIntField(final MetaInfo<E> info) {
- final JTextField field = new JTextField();
+ private void addIntField(final MetaInfo<E> info, int nhgap) {
+ final JSpinner field = new JSpinner();
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();
- }
- });
+ field.setValue(info.getInteger(true) == null ? 0 : info
+ .getInteger(true));
info.addReloadedListener(new Runnable() {
@Override
public void run() {
- field.setText(info.getString());
+ field.setValue(info.getInteger(true) == null ? 0 : info
+ .getInteger(true));
}
});
info.addSaveListener(new Runnable() {
@Override
public void run() {
- info.setString(field.getText());
- Integer value = info.getInteger();
+ info.setInteger((Integer) field.getValue());
+ Integer value = info.getInteger(false);
if (value == null) {
- info.setString("");
- } else {
- info.setInteger(value);
+ field.setValue(0);
}
- field.setText(info.getString());
}
});
- 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));
- }
- });
-
- 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, nhgap), BorderLayout.WEST);
+ this.add(field, BorderLayout.CENTER);
- this.add(label(info), BorderLayout.WEST);
- this.add(pane, BorderLayout.CENTER);
+ setPreferredSize(field);
}
/**
*
* @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<E> info) {
+ private JComponent label(final MetaInfo<E> info, int nhgap) {
final JLabel label = new JLabel(info.getName());
Dimension ps = label.getPreferredSize();
int w = ps.width;
int step = 150;
- for (int i = 2 * step; i < 10 * step; i += step) {
+ for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
if (w < i) {
w = i;
break;
@Override
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) {
return new ImageIcon(img);
}
+
+ 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));
+ }
}