1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Graphics;
25 import java.awt.Insets;
26 import java.awt.event.ActionEvent;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29
30 import javax.swing.AbstractAction;
31 import javax.swing.Action;
32 import javax.swing.BoxLayout;
33 import javax.swing.DefaultListCellRenderer;
34 import javax.swing.Icon;
35 import javax.swing.JButton;
36 import javax.swing.JColorChooser;
37 import javax.swing.JComboBox;
38 import javax.swing.JList;
39 import javax.swing.JPanel;
40
41 import org.xnap.XNap;
42 import org.xnap.gui.util.IconHelper;
43
44 /***
45 * Provides a panel that displays a {@link JComboBox} with predefined colors
46 * and a button that can launch a {@link JColorChooser}.
47 */
48 public class ColorPanel extends JPanel
49 {
50
51
52
53
54
55 private Color customColor = new Color(254, 254, 254);
56 private JComboBox jcbColor;
57 private Hashtable namesByColor = new Hashtable();
58
59
60
61 public ColorPanel(Color newColor)
62 {
63 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
64
65 jcbColor = new JComboBox();
66 jcbColor.setRenderer(new ColorCellRenderer());
67 add(jcbColor);
68
69 namesByColor.put(Color.black, XNap.tr("Black"));
70 namesByColor.put(Color.darkGray, XNap.tr("DarkGray"));
71 namesByColor.put(Color.gray, XNap.tr("Gray"));
72 namesByColor.put(Color.lightGray, XNap.tr("LightGray"));
73 namesByColor.put(Color.blue, XNap.tr("Blue"));
74 namesByColor.put(Color.cyan, XNap.tr("Cyan"));
75 namesByColor.put(Color.magenta, XNap.tr("Magenta"));
76 namesByColor.put(Color.pink, XNap.tr("Pink"));
77 namesByColor.put(Color.red, XNap.tr("Red"));
78 namesByColor.put(Color.orange, XNap.tr("Orange"));
79 namesByColor.put(Color.green, XNap.tr("Green"));
80 namesByColor.put(Color.yellow, XNap.tr("Yellow"));
81 namesByColor.put(Color.white, XNap.tr("White"));
82
83 for (Enumeration e = namesByColor.keys(); e.hasMoreElements();) {
84 jcbColor.addItem(e.nextElement());
85 }
86 jcbColor.addItem(customColor);
87
88 JButton jb = new XNapButton(new ColorAction());
89 jb.setMargin(new Insets(1, 1, 1, 1));
90 add(jb);
91
92 setSelectedColor(newColor);
93 }
94
95
96
97 public Color getSelectedColor()
98 {
99 return (Color)jcbColor.getSelectedItem();
100 }
101
102 /***
103 * Makes sure that color is always != null.
104 */
105 public void setSelectedColor(Color newValue)
106 {
107 if (namesByColor.get(newValue) != null) {
108 jcbColor.setSelectedItem(newValue);
109 }
110 else {
111 jcbColor.removeItem(customColor);
112 customColor = newValue;
113 jcbColor.addItem(customColor);
114 jcbColor.setSelectedItem(customColor);
115 }
116 }
117
118 /***
119 *
120 */
121 private class ColorAction extends AbstractAction {
122
123 public ColorAction()
124 {
125 putValue(Action.SHORT_DESCRIPTION,
126 XNap.tr("Opens color selection dialog."));
127 putValue(IconHelper.XNAP_ICON, "palette_color.png");
128 }
129
130 public void actionPerformed(ActionEvent event)
131 {
132 Color c = JColorChooser.showDialog(ColorPanel.this,
133 XNap.tr("Choose color"),
134 getSelectedColor());
135 if (c != null) {
136 setSelectedColor(c);
137 }
138 }
139
140 }
141
142 protected class ColorCellRenderer extends DefaultListCellRenderer {
143
144 public ColorIcon icon = new ColorIcon(Color.white);
145
146 public Component getListCellRendererComponent
147 (JList list, Object val, int idx, boolean isSel, boolean hasFocus)
148 {
149 super.getListCellRendererComponent
150 (list, val, idx, isSel, hasFocus);
151
152 String colorName = (String)namesByColor.get(val);
153 if (colorName == null) {
154 colorName = XNap.tr("Custom");
155 }
156 setText(colorName);
157 icon.setColor((Color)val);
158 setIcon(icon);
159
160 return this;
161 }
162 }
163
164 /***
165 *
166 */
167 protected class ColorIcon implements Icon
168 {
169
170
171
172 private int height = 15;
173 private int width = 15;
174 private Color color;
175
176
177
178 public ColorIcon(Color color)
179 {
180 this.color = color;
181 }
182
183
184
185 public int getIconHeight()
186 {
187 return height;
188 }
189
190 public int getIconWidth()
191 {
192 return width;
193 }
194
195 public void setColor(Color newValue)
196 {
197 this.color = newValue;
198 }
199
200 public void paintIcon(Component c, Graphics g, int x, int y)
201 {
202 g.setColor(Color.black);
203 g.drawRect(x, y, width, height);
204 g.setColor(color);
205 g.fillRect(x + 1, y + 1, width - 1, height - 1);
206 }
207 }
208 }
209