1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
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
;
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
;
20 import be
.nikiroo
.utils
.resources
.MetaInfo
;
22 public class ConfigItemColor
<E
extends Enum
<E
>> extends ConfigItem
<E
> {
23 private static final long serialVersionUID
= 1L;
25 private Map
<JComponent
, JTextField
> fields
= new HashMap
<JComponent
, JTextField
>();
28 * Create a new {@link ConfigItemColor} for the given {@link MetaInfo}.
31 * the {@link MetaInfo}
33 public ConfigItemColor(MetaInfo
<E
> info
) {
38 protected Object
getFromField(int item
) {
39 JTextField field
= fields
.get(getField(item
));
41 return field
.getText();
48 protected Object
getFromInfo(int item
) {
49 return info
.getString(item
, false);
53 protected void setToField(Object value
, int item
) {
54 JTextField field
= fields
.get(getField(item
));
56 field
.setText(value
== null ?
"" : value
.toString());
58 // TODO: change color too
62 protected void setToInfo(Object value
, int item
) {
63 info
.setString((String
) value
, item
);
66 private int getFromInfoColor(int item
) {
67 Integer color
= info
.getColor(item
, true);
69 return new Color(255, 255, 255, 255).getRGB();
76 protected JComponent
createField(final int item
) {
77 final JPanel pane
= new JPanel(new BorderLayout());
78 final JTextField field
= new JTextField();
80 final JButton colorWheel
= new JButton();
81 colorWheel
.setIcon(getIcon(17, getFromInfoColor(item
)));
82 colorWheel
.addActionListener(new ActionListener() {
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)));
97 pane
.add(colorWheel
, BorderLayout
.WEST
);
98 pane
.add(field
, BorderLayout
.CENTER
);
100 fields
.put(pane
, field
);
105 * Return an {@link Icon} to use as a colour badge for the colour field
109 * the size of the badge
111 * the colour of the badge, which can be NULL (will return
116 static private Icon
getIcon(int size
, Integer color
) {
119 color
= new Color(255, 255, 255, 255).getRGB();
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
);
126 BufferedImage img
= new BufferedImage(size
, size
,
127 BufferedImage
.TYPE_4BYTE_ABGR
);
129 Graphics2D g
= img
.createGraphics();
132 g
.fillRect(0, 0, img
.getWidth(), img
.getHeight());
134 g
.drawRect(0, 0, img
.getWidth() - 1, img
.getHeight() - 1);
139 return new ImageIcon(img
);