| 1 | package be.nikiroo.utils.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.Graphics2D; |
| 6 | import java.awt.event.ActionEvent; |
| 7 | import java.awt.event.ActionListener; |
| 8 | import java.awt.image.BufferedImage; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Map; |
| 11 | |
| 12 | import javax.swing.Icon; |
| 13 | import javax.swing.ImageIcon; |
| 14 | import javax.swing.JButton; |
| 15 | import javax.swing.JColorChooser; |
| 16 | import javax.swing.JComponent; |
| 17 | import javax.swing.JPanel; |
| 18 | import javax.swing.JTextField; |
| 19 | |
| 20 | import be.nikiroo.utils.resources.MetaInfo; |
| 21 | |
| 22 | public class ConfigItemColor<E extends Enum<E>> extends ConfigItem<E> { |
| 23 | private static final long serialVersionUID = 1L; |
| 24 | |
| 25 | private Map<JComponent, JTextField> fields = new HashMap<JComponent, JTextField>(); |
| 26 | |
| 27 | /** |
| 28 | * Create a new {@link ConfigItemColor} for the given {@link MetaInfo}. |
| 29 | * |
| 30 | * @param info |
| 31 | * the {@link MetaInfo} |
| 32 | */ |
| 33 | public ConfigItemColor(MetaInfo<E> info) { |
| 34 | super(info, true); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | protected Object getFromField(int item) { |
| 39 | JTextField field = fields.get(getField(item)); |
| 40 | if (field != null) { |
| 41 | return field.getText(); |
| 42 | } |
| 43 | |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected Object getFromInfo(int item) { |
| 49 | return info.getString(item, false); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void setToField(Object value, int item) { |
| 54 | JTextField field = fields.get(getField(item)); |
| 55 | if (field != null) { |
| 56 | field.setText(value == null ? "" : value.toString()); |
| 57 | } |
| 58 | // TODO: change color too |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void setToInfo(Object value, int item) { |
| 63 | info.setString((String) value, item); |
| 64 | } |
| 65 | |
| 66 | private int getFromInfoColor(int item) { |
| 67 | Integer color = info.getColor(item, true); |
| 68 | if (color == null) { |
| 69 | return new Color(255, 255, 255, 255).getRGB(); |
| 70 | } |
| 71 | |
| 72 | return color; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected JComponent createField(final int item) { |
| 77 | final JPanel pane = new JPanel(new BorderLayout()); |
| 78 | final JTextField field = new JTextField(); |
| 79 | |
| 80 | final JButton colorWheel = new JButton(); |
| 81 | colorWheel.setIcon(getIcon(17, getFromInfoColor(item))); |
| 82 | colorWheel.addActionListener(new ActionListener() { |
| 83 | @Override |
| 84 | public void actionPerformed(ActionEvent e) { |
| 85 | int icol = getFromInfoColor(item); |
| 86 | Color initialColor = new Color(icol, true); |
| 87 | Color newColor = JColorChooser.showDialog(ConfigItemColor.this, |
| 88 | info.getName(), initialColor); |
| 89 | if (newColor != null) { |
| 90 | info.setColor(newColor.getRGB(), item); |
| 91 | field.setText(info.getString(item, false)); |
| 92 | colorWheel.setIcon(getIcon(17, info.getColor(item, true))); |
| 93 | } |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | pane.add(colorWheel, BorderLayout.WEST); |
| 98 | pane.add(field, BorderLayout.CENTER); |
| 99 | |
| 100 | fields.put(pane, field); |
| 101 | return pane; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Return an {@link Icon} to use as a colour badge for the colour field |
| 106 | * controls. |
| 107 | * |
| 108 | * @param size |
| 109 | * the size of the badge |
| 110 | * @param color |
| 111 | * the colour of the badge, which can be NULL (will return |
| 112 | * transparent white) |
| 113 | * |
| 114 | * @return the badge |
| 115 | */ |
| 116 | static private Icon getIcon(int size, Integer color) { |
| 117 | // Allow null values |
| 118 | if (color == null) { |
| 119 | color = new Color(255, 255, 255, 255).getRGB(); |
| 120 | } |
| 121 | |
| 122 | Color c = new Color(color, true); |
| 123 | int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3; |
| 124 | Color border = (avg >= 128 ? Color.BLACK : Color.WHITE); |
| 125 | |
| 126 | BufferedImage img = new BufferedImage(size, size, |
| 127 | BufferedImage.TYPE_4BYTE_ABGR); |
| 128 | |
| 129 | Graphics2D g = img.createGraphics(); |
| 130 | try { |
| 131 | g.setColor(c); |
| 132 | g.fillRect(0, 0, img.getWidth(), img.getHeight()); |
| 133 | g.setColor(border); |
| 134 | g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1); |
| 135 | } finally { |
| 136 | g.dispose(); |
| 137 | } |
| 138 | |
| 139 | return new ImageIcon(img); |
| 140 | } |
| 141 | } |