package be.nikiroo.utils.ui;
import java.awt.BorderLayout;
+import java.awt.Color;
import java.awt.Dimension;
-import java.util.ArrayList;
-import java.util.List;
-
+import java.awt.Graphics2D;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.image.BufferedImage;
+
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
-import javax.swing.border.EmptyBorder;
import be.nikiroo.utils.resources.Bundle;
-import be.nikiroo.utils.resources.Meta;
+import be.nikiroo.utils.resources.Meta.Format;
+import be.nikiroo.utils.resources.MetaInfo;
/**
* A graphical item that reflect a configuration option from the given
*/
public class ConfigItem<E extends Enum<E>> extends JPanel {
private static final long serialVersionUID = 1L;
- private Class<E> type;
- private final Bundle<E> bundle;
- private final E id;
- private Meta meta;
- private String value;
+ public ConfigItem(final MetaInfo<E> info) {
+ this.setLayout(new BorderLayout());
- private JTextField valueField;
+ if (info.getFormat() == Format.BOOLEAN) {
+ final JCheckBox field = new JCheckBox();
+ field.setToolTipText(info.getDescription());
+ Boolean state = info.getBoolean();
+ if (state == null) {
+ info.getDefaultBoolean();
+ }
- public ConfigItem(Class<E> type, Bundle<E> bundle, E id) {
- this.type = type;
- this.bundle = bundle;
- this.id = id;
+ // 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;
+ }
- try {
- this.meta = type.getDeclaredField(id.name()).getAnnotation(
- Meta.class);
- } catch (NoSuchFieldException e) {
- } catch (SecurityException e) {
+ field.setSelected(state);
+
+ info.addReloadedListener(new Runnable() {
+ @Override
+ public void run() {
+ Boolean state = info.getBoolean();
+ if (state == null) {
+ info.getDefaultBoolean();
+ }
+ if (state == null) {
+ state = false;
+ }
+
+ field.setSelected(state);
+ }
+ });
+ info.addSaveListener(new Runnable() {
+ @Override
+ public void run() {
+ info.setBoolean(field.isSelected());
+ }
+ });
+
+ field.setText(info.getName());
+ this.add(field, BorderLayout.CENTER);
+ } else if (info.getFormat() == Format.COLOR) {
+ 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());
+ }
+ });
+
+ this.add(label(info.getName()), BorderLayout.WEST);
+ JPanel pane = new JPanel(new BorderLayout());
+
+ 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);
+ } else {
+ 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());
+ }
+ });
+
+ this.add(label(info.getName()), BorderLayout.WEST);
+ this.add(field, BorderLayout.CENTER);
}
+ }
- this.setLayout(new BorderLayout());
- this.setBorder(new EmptyBorder(2, 10, 2, 10));
+ /**
+ * Create a label which width is constrained in lock steps.
+ *
+ * @param text
+ * the text of the label
+ *
+ * @return the label
+ */
+ private JLabel label(String text) {
+ final JLabel label = new JLabel(text);
- String tooltip = null;
- if (bundle.getDescriptionBundle() != null) {
- tooltip = bundle.getDescriptionBundle().getString(id);
- if (tooltip != null && tooltip.trim().isEmpty()) {
- tooltip = null;
- }
+ Dimension ps = label.getPreferredSize();
+ if (ps == null) {
+ ps = label.getSize();
}
- String name = id.toString();
- if (name.length() > 1) {
- name = name.substring(0, 1) + name.substring(1).toLowerCase();
- name = name.replace("_", " ");
+ int w = ps.width;
+ int step = 80;
+ for (int i = 2 * step; i < 10 * step; i += step) {
+ if (w < i) {
+ w = i;
+ break;
+ }
}
- JLabel nameLabel = new JLabel(name);
- nameLabel.setToolTipText(tooltip);
- nameLabel.setPreferredSize(new Dimension(400, 0));
- this.add(nameLabel, BorderLayout.WEST);
+ ps.width = w;
+ label.setSize(ps);
+ label.setPreferredSize(ps);
- valueField = new JTextField();
- valueField.setText(value);
-
- reload();
- this.add(valueField, BorderLayout.CENTER);
- }
-
- /**
- * Reload the value from the {@link Bundle}.
- */
- public void reload() {
- value = bundle.getString(id);
- valueField.setText(value);
- }
-
- /**
- * Save the current value to the {@link Bundle}.
- */
- public void save() {
- value = valueField.getText();
- bundle.setString(id, value);
+ return label;
}
/**
- * Create a list of {@link ConfigItem}, one for each of the item in the
- * given {@link Bundle}.
+ * Return an {@link Icon} to use as a colour badge for the colour field
+ * controls.
*
- * @param <E>
- * the type of {@link Bundle} to edit
- * @param type
- * a class instance of the item type to work on
- * @param bundle
- * the {@link Bundle} to sort through
+ * @param size
+ * the size of the badge
+ * @param color
+ * the colour of the badge
*
- * @return the list
+ * @return the badge
*/
- static public <E extends Enum<E>> List<ConfigItem<E>> getItems(
- Class<E> type, Bundle<E> bundle) {
- List<ConfigItem<E>> list = new ArrayList<ConfigItem<E>>();
- for (E id : type.getEnumConstants()) {
- list.add(new ConfigItem<E>(type, bundle, id));
+ 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);
+
+ 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 list;
+ return new ImageIcon(img);
}
}