1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.Dimension
;
6 import java
.awt
.Graphics2D
;
7 import java
.awt
.event
.ActionEvent
;
8 import java
.awt
.event
.ActionListener
;
9 import java
.awt
.image
.BufferedImage
;
11 import javax
.swing
.Icon
;
12 import javax
.swing
.ImageIcon
;
13 import javax
.swing
.JButton
;
14 import javax
.swing
.JCheckBox
;
15 import javax
.swing
.JColorChooser
;
16 import javax
.swing
.JLabel
;
17 import javax
.swing
.JPanel
;
18 import javax
.swing
.JTextField
;
20 import be
.nikiroo
.utils
.resources
.Bundle
;
21 import be
.nikiroo
.utils
.resources
.Meta
.Format
;
22 import be
.nikiroo
.utils
.resources
.MetaInfo
;
25 * A graphical item that reflect a configuration option from the given
31 * the type of {@link Bundle} to edit
33 public class ConfigItem
<E
extends Enum
<E
>> extends JPanel
{
34 private static final long serialVersionUID
= 1L;
36 public ConfigItem(final MetaInfo
<E
> info
) {
37 this.setLayout(new BorderLayout());
39 if (info
.getFormat() == Format
.BOOLEAN
) {
40 final JCheckBox field
= new JCheckBox();
41 field
.setToolTipText(info
.getDescription());
42 Boolean state
= info
.getBoolean();
44 info
.getDefaultBoolean();
50 .println("No default value given for BOOLEAN parameter \""
52 + "\", we consider it is FALSE");
56 field
.setSelected(state
);
58 info
.addReloadedListener(new Runnable() {
61 Boolean state
= info
.getBoolean();
63 info
.getDefaultBoolean();
69 field
.setSelected(state
);
72 info
.addSaveListener(new Runnable() {
75 info
.setBoolean(field
.isSelected());
79 field
.setText(info
.getName());
80 this.add(field
, BorderLayout
.CENTER
);
81 } else if (info
.getFormat() == Format
.COLOR
) {
82 final JTextField field
= new JTextField();
83 field
.setToolTipText(info
.getDescription());
84 field
.setText(info
.getString());
86 info
.addReloadedListener(new Runnable() {
89 field
.setText(info
.getString());
92 info
.addSaveListener(new Runnable() {
95 info
.setString(field
.getText());
99 this.add(label(info
.getName()), BorderLayout
.WEST
);
100 JPanel pane
= new JPanel(new BorderLayout());
102 final JButton colorWheel
= new JButton();
103 colorWheel
.setIcon(getIcon(17, info
.getColor()));
104 colorWheel
.addActionListener(new ActionListener() {
106 public void actionPerformed(ActionEvent e
) {
107 Color initialColor
= new Color(info
.getColor(), true);
108 Color newColor
= JColorChooser
.showDialog(ConfigItem
.this,
109 info
.getName(), initialColor
);
110 if (newColor
!= null) {
111 info
.setColor(newColor
.getRGB());
112 field
.setText(info
.getString());
113 colorWheel
.setIcon(getIcon(17, info
.getColor()));
117 pane
.add(colorWheel
, BorderLayout
.WEST
);
118 pane
.add(field
, BorderLayout
.CENTER
);
119 this.add(pane
, BorderLayout
.CENTER
);
121 final JTextField field
= new JTextField();
122 field
.setToolTipText(info
.getDescription());
123 field
.setText(info
.getString());
125 info
.addReloadedListener(new Runnable() {
128 field
.setText(info
.getString());
131 info
.addSaveListener(new Runnable() {
134 info
.setString(field
.getText());
138 this.add(label(info
.getName()), BorderLayout
.WEST
);
139 this.add(field
, BorderLayout
.CENTER
);
144 * Create a label which width is constrained in lock steps.
147 * the text of the label
151 private JLabel
label(String text
) {
152 final JLabel label
= new JLabel(text
);
154 Dimension ps
= label
.getPreferredSize();
156 ps
= label
.getSize();
161 for (int i
= 2 * step
; i
< 10 * step
; i
+= step
) {
170 label
.setPreferredSize(ps
);
176 * Return an {@link Icon} to use as a colour badge for the colour field
180 * the size of the badge
182 * the colour of the badge
186 private Icon
getIcon(int size
, int color
) {
187 Color c
= new Color(color
, true);
188 int avg
= (c
.getRed() + c
.getGreen() + c
.getBlue()) / 3;
189 Color border
= (avg
>= 128 ? Color
.BLACK
: Color
.WHITE
);
191 BufferedImage img
= new BufferedImage(size
, size
,
192 BufferedImage
.TYPE_4BYTE_ABGR
);
194 Graphics2D g
= img
.createGraphics();
197 g
.fillRect(0, 0, img
.getWidth(), img
.getHeight());
199 g
.drawRect(0, 0, img
.getWidth() - 1, img
.getHeight() - 1);
204 return new ImageIcon(img
);