1 |
772 |
jeremybenn |
/* DefaultSwatchChooserPanel.java --
|
2 |
|
|
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.swing.colorchooser;
|
40 |
|
|
|
41 |
|
|
import java.awt.BorderLayout;
|
42 |
|
|
import java.awt.Color;
|
43 |
|
|
import java.awt.Component;
|
44 |
|
|
import java.awt.Container;
|
45 |
|
|
import java.awt.Dimension;
|
46 |
|
|
import java.awt.Graphics;
|
47 |
|
|
import java.awt.Insets;
|
48 |
|
|
import java.awt.LayoutManager;
|
49 |
|
|
import java.awt.event.MouseAdapter;
|
50 |
|
|
import java.awt.event.MouseEvent;
|
51 |
|
|
import java.awt.event.MouseListener;
|
52 |
|
|
|
53 |
|
|
import javax.swing.Icon;
|
54 |
|
|
import javax.swing.JColorChooser;
|
55 |
|
|
import javax.swing.JLabel;
|
56 |
|
|
import javax.swing.JPanel;
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* This class is the DefaultSwatchChooserPanel. This chooser panel displays a
|
60 |
|
|
* set of colors that can be picked. Recently picked items will go into a
|
61 |
|
|
* side panel so the user can see the history of the chosen colors.
|
62 |
|
|
*/
|
63 |
|
|
class DefaultSwatchChooserPanel extends AbstractColorChooserPanel
|
64 |
|
|
{
|
65 |
|
|
/** The main panel that holds the set of choosable colors. */
|
66 |
|
|
MainSwatchPanel mainPalette;
|
67 |
|
|
|
68 |
|
|
/** A panel that holds the recent colors. */
|
69 |
|
|
RecentSwatchPanel recentPalette;
|
70 |
|
|
|
71 |
|
|
/** The mouse handlers for the panels. */
|
72 |
|
|
MouseListener mouseHandler;
|
73 |
|
|
|
74 |
|
|
/**
|
75 |
|
|
* This the base class for all swatch panels. Swatch panels are panels that
|
76 |
|
|
* hold a set of blocks where colors are displayed.
|
77 |
|
|
*/
|
78 |
|
|
abstract static class SwatchPanel extends JPanel
|
79 |
|
|
{
|
80 |
|
|
/** The width of each block. */
|
81 |
|
|
protected int cellWidth = 10;
|
82 |
|
|
|
83 |
|
|
/** The height of each block. */
|
84 |
|
|
protected int cellHeight = 10;
|
85 |
|
|
|
86 |
|
|
/** The gap between blocks. */
|
87 |
|
|
protected int gap = 1;
|
88 |
|
|
|
89 |
|
|
/** The number of rows in the swatch panel. */
|
90 |
|
|
protected int numRows;
|
91 |
|
|
|
92 |
|
|
/** The number of columns in the swatch panel. */
|
93 |
|
|
protected int numCols;
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Creates a new SwatchPanel object.
|
97 |
|
|
*/
|
98 |
|
|
SwatchPanel()
|
99 |
|
|
{
|
100 |
|
|
super();
|
101 |
|
|
setBackground(Color.WHITE);
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* This method returns the preferred size of the swatch panel based on the
|
106 |
|
|
* number of rows and columns and the size of each cell.
|
107 |
|
|
*
|
108 |
|
|
* @return The preferred size of the swatch panel.
|
109 |
|
|
*/
|
110 |
|
|
public Dimension getPreferredSize()
|
111 |
|
|
{
|
112 |
|
|
int height = numRows * cellHeight + (numRows - 1) * gap;
|
113 |
|
|
int width = numCols * cellWidth + (numCols - 1) * gap;
|
114 |
|
|
Insets insets = getInsets();
|
115 |
|
|
|
116 |
|
|
return new Dimension(width + insets.left + insets.right,
|
117 |
|
|
height + insets.top + insets.bottom);
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* This method returns the color for the given position.
|
122 |
|
|
*
|
123 |
|
|
* @param x The x coordinate of the position.
|
124 |
|
|
* @param y The y coordinate of the position.
|
125 |
|
|
*
|
126 |
|
|
* @return The color at the given position.
|
127 |
|
|
*/
|
128 |
|
|
public abstract Color getColorForPosition(int x, int y);
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* This method initializes the colors for the swatch panel.
|
132 |
|
|
*/
|
133 |
|
|
protected abstract void initializeColors();
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/**
|
137 |
|
|
* This is the main swatch panel. This panel sits in the middle and allows a
|
138 |
|
|
* set of colors to be picked which will move to the recent swatch panel.
|
139 |
|
|
*/
|
140 |
|
|
static class MainSwatchPanel extends SwatchPanel
|
141 |
|
|
{
|
142 |
|
|
/** The color describing (204, 255, 255) */
|
143 |
|
|
public static final Color C204255255 = new Color(204, 204, 255);
|
144 |
|
|
|
145 |
|
|
/** The color describing (255, 204, 204) */
|
146 |
|
|
public static final Color C255204204 = new Color(255, 204, 204);
|
147 |
|
|
|
148 |
|
|
/** The color describing (204, 255, 204) */
|
149 |
|
|
public static final Color C204255204 = new Color(204, 255, 204);
|
150 |
|
|
|
151 |
|
|
/** The color describing (204, 204, 204) */
|
152 |
|
|
public static final Color C204204204 = new Color(204, 204, 204);
|
153 |
|
|
|
154 |
|
|
/** The color (153, 153, 255). */
|
155 |
|
|
public static final Color C153153255 = new Color(153, 153, 255);
|
156 |
|
|
|
157 |
|
|
/** The color (51, 51, 255). */
|
158 |
|
|
public static final Color C051051255 = new Color(51, 51, 255);
|
159 |
|
|
|
160 |
|
|
/** The color (153, 0, 153). */
|
161 |
|
|
public static final Color C153000153 = new Color(153, 0, 153);
|
162 |
|
|
|
163 |
|
|
/** The color (0, 51, 51). */
|
164 |
|
|
public static final Color C000051051 = new Color(0, 51, 51);
|
165 |
|
|
|
166 |
|
|
/** The color (51, 0, 51). */
|
167 |
|
|
public static final Color C051000051 = new Color(51, 0, 51);
|
168 |
|
|
|
169 |
|
|
/** The color (51, 51, 0). */
|
170 |
|
|
public static final Color C051051000 = new Color(51, 51, 0);
|
171 |
|
|
|
172 |
|
|
/** The color (102, 102, 0). */
|
173 |
|
|
public static final Color C102102000 = new Color(102, 102, 0);
|
174 |
|
|
|
175 |
|
|
/** The color (153, 255, 153). */
|
176 |
|
|
public static final Color C153255153 = new Color(153, 255, 153);
|
177 |
|
|
|
178 |
|
|
/** The color (102, 255, 102). */
|
179 |
|
|
public static final Color C102255102 = new Color(102, 255, 102);
|
180 |
|
|
|
181 |
|
|
/** The color (0, 102, 102). */
|
182 |
|
|
public static final Color C000102102 = new Color(0, 102, 102);
|
183 |
|
|
|
184 |
|
|
/** The color (102, 0, 102). */
|
185 |
|
|
public static final Color C102000102 = new Color(102, 0, 102);
|
186 |
|
|
|
187 |
|
|
/** The color (0, 153, 153). */
|
188 |
|
|
public static final Color C000153153 = new Color(0, 153, 153);
|
189 |
|
|
|
190 |
|
|
/** The color (153, 153, 0). */
|
191 |
|
|
public static final Color C153153000 = new Color(153, 153, 0);
|
192 |
|
|
|
193 |
|
|
/** The color (204, 204, 0). */
|
194 |
|
|
public static final Color C204204000 = new Color(204, 204, 0);
|
195 |
|
|
|
196 |
|
|
/** The color (204, 0, 204). */
|
197 |
|
|
public static final Color C204000204 = new Color(204, 0, 204);
|
198 |
|
|
|
199 |
|
|
/** The color (0, 204, 204). */
|
200 |
|
|
public static final Color C000204204 = new Color(0, 204, 204);
|
201 |
|
|
|
202 |
|
|
/** The color (51, 255, 51). */
|
203 |
|
|
public static final Color C051255051 = new Color(51, 255, 51);
|
204 |
|
|
|
205 |
|
|
/** The color (255, 51, 51). */
|
206 |
|
|
public static final Color C255051051 = new Color(255, 51, 51);
|
207 |
|
|
|
208 |
|
|
/** The color (255, 102, 102). */
|
209 |
|
|
public static final Color C255102102 = new Color(255, 102, 102);
|
210 |
|
|
|
211 |
|
|
/** The color (102, 102, 255). */
|
212 |
|
|
public static final Color C102102255 = new Color(102, 102, 255);
|
213 |
|
|
|
214 |
|
|
/** The color (255, 153, 153). */
|
215 |
|
|
public static final Color C255153153 = new Color(255, 153, 153);
|
216 |
|
|
static Color[] colors =
|
217 |
|
|
{
|
218 |
|
|
// Row 1
|
219 |
|
|
Color.WHITE, new Color(204, 255, 255), C204255255, C204255255, C204255255,
|
220 |
|
|
C204255255, C204255255, C204255255, C204255255,
|
221 |
|
|
C204255255, C204255255, new Color(255, 204, 255),
|
222 |
|
|
C255204204, C255204204, C255204204, C255204204,
|
223 |
|
|
C255204204, C255204204, C255204204, C255204204,
|
224 |
|
|
C255204204, new Color(255, 255, 204), C204255204,
|
225 |
|
|
C204255204, C204255204, C204255204, C204255204,
|
226 |
|
|
C204255204, C204255204, C204255204, C204255204,
|
227 |
|
|
|
228 |
|
|
// Row 2
|
229 |
|
|
C204204204, new Color(153, 255, 255), new Color(153, 204, 255), C153153255,
|
230 |
|
|
C153153255, C153153255, C153153255, C153153255,
|
231 |
|
|
C153153255, C153153255, new Color(204, 153, 255),
|
232 |
|
|
new Color(255, 153, 255),
|
233 |
|
|
new Color(255, 153, 204), C255153153, C255153153,
|
234 |
|
|
C255153153, C255153153, C255153153, C255153153,
|
235 |
|
|
C255153153, new Color(255, 204, 153),
|
236 |
|
|
new Color(255, 255, 153),
|
237 |
|
|
new Color(204, 255, 153), C153255153, C153255153,
|
238 |
|
|
C153255153, C153255153, C153255153, C153255153,
|
239 |
|
|
C153255153, new Color(153, 255, 204),
|
240 |
|
|
|
241 |
|
|
// Row 3
|
242 |
|
|
C204204204, new Color(102, 255, 255), new Color(102, 204, 255),
|
243 |
|
|
new Color(102, 153, 255), C102102255, C102102255,
|
244 |
|
|
C102102255, C102102255, C102102255,
|
245 |
|
|
new Color(153, 102, 255),
|
246 |
|
|
new Color(204, 102, 255),
|
247 |
|
|
new Color(255, 102, 255),
|
248 |
|
|
new Color(255, 102, 204),
|
249 |
|
|
new Color(255, 102, 153), C255102102, C255102102,
|
250 |
|
|
C255102102, C255102102, C255102102,
|
251 |
|
|
new Color(255, 153, 102),
|
252 |
|
|
new Color(255, 204, 102),
|
253 |
|
|
new Color(255, 255, 102),
|
254 |
|
|
new Color(204, 255, 102),
|
255 |
|
|
new Color(153, 255, 102), C102255102, C102255102,
|
256 |
|
|
C102255102, C102255102, C102255102,
|
257 |
|
|
new Color(102, 255, 153),
|
258 |
|
|
new Color(102, 255, 204),
|
259 |
|
|
|
260 |
|
|
// Row 4
|
261 |
|
|
new Color(153, 153, 153), new Color(51, 255, 255), new Color(51, 204, 255),
|
262 |
|
|
new Color(51, 153, 255), new Color(51, 102, 255),
|
263 |
|
|
C051051255, C051051255, C051051255,
|
264 |
|
|
new Color(102, 51, 255), new Color(153, 51, 255),
|
265 |
|
|
new Color(204, 51, 255), new Color(255, 51, 255),
|
266 |
|
|
new Color(255, 51, 204), new Color(255, 51, 153),
|
267 |
|
|
new Color(255, 51, 102), C255051051, C255051051,
|
268 |
|
|
C255051051, new Color(255, 102, 51),
|
269 |
|
|
new Color(255, 153, 51), new Color(255, 204, 51),
|
270 |
|
|
new Color(255, 255, 51), new Color(204, 255, 51),
|
271 |
|
|
new Color(153, 255, 51), new Color(102, 255, 51),
|
272 |
|
|
C051255051, C051255051, C051255051,
|
273 |
|
|
new Color(51, 255, 102), new Color(51, 255, 153),
|
274 |
|
|
new Color(51, 255, 204),
|
275 |
|
|
|
276 |
|
|
// Row 5
|
277 |
|
|
new Color(153, 153, 153), new Color(0, 255, 255), new Color(0, 204, 255),
|
278 |
|
|
new Color(0, 153, 255), new Color(0, 102, 255),
|
279 |
|
|
new Color(0, 51, 255), new Color(0, 0, 255),
|
280 |
|
|
new Color(51, 0, 255), new Color(102, 0, 255),
|
281 |
|
|
new Color(153, 0, 255), new Color(204, 0, 255),
|
282 |
|
|
new Color(255, 0, 255), new Color(255, 0, 204),
|
283 |
|
|
new Color(255, 0, 153), new Color(255, 0, 102),
|
284 |
|
|
new Color(255, 0, 51), new Color(255, 0, 0),
|
285 |
|
|
new Color(255, 51, 0), new Color(255, 102, 0),
|
286 |
|
|
new Color(255, 153, 0), new Color(255, 204, 0),
|
287 |
|
|
new Color(255, 255, 0), new Color(204, 255, 0),
|
288 |
|
|
new Color(153, 255, 0), new Color(102, 255, 0),
|
289 |
|
|
new Color(51, 255, 0), new Color(0, 255, 0),
|
290 |
|
|
new Color(0, 255, 51), new Color(0, 255, 102),
|
291 |
|
|
new Color(0, 255, 153), new Color(0, 255, 204),
|
292 |
|
|
|
293 |
|
|
// Row 6
|
294 |
|
|
new Color(102, 102, 102), C000204204, C000204204, new Color(0, 153, 204),
|
295 |
|
|
new Color(0, 102, 204), new Color(0, 51, 204),
|
296 |
|
|
new Color(0, 0, 204), new Color(51, 0, 204),
|
297 |
|
|
new Color(102, 0, 204), new Color(153, 0, 204),
|
298 |
|
|
C204000204, C204000204, C204000204,
|
299 |
|
|
new Color(204, 0, 153), new Color(204, 0, 102),
|
300 |
|
|
new Color(204, 0, 51), new Color(204, 0, 0),
|
301 |
|
|
new Color(204, 51, 0), new Color(204, 102, 0),
|
302 |
|
|
new Color(204, 153, 0), C204204000, C204204000,
|
303 |
|
|
C204204000, new Color(153, 204, 0),
|
304 |
|
|
new Color(102, 204, 0), new Color(51, 204, 0),
|
305 |
|
|
new Color(0, 204, 0), new Color(0, 204, 51),
|
306 |
|
|
new Color(0, 204, 102), new Color(0, 204, 153),
|
307 |
|
|
new Color(0, 204, 204),
|
308 |
|
|
|
309 |
|
|
// Row 7
|
310 |
|
|
new Color(102, 102, 102), C000153153, C000153153, C000153153,
|
311 |
|
|
new Color(0, 102, 153), new Color(0, 51, 153),
|
312 |
|
|
new Color(0, 0, 153), new Color(51, 0, 153),
|
313 |
|
|
new Color(102, 0, 153), C153000153, C153000153,
|
314 |
|
|
C153000153, C153000153, C153000153,
|
315 |
|
|
new Color(153, 0, 102), new Color(153, 0, 51),
|
316 |
|
|
new Color(153, 0, 0), new Color(153, 51, 0),
|
317 |
|
|
new Color(153, 102, 0), C153153000, C153153000,
|
318 |
|
|
C153153000, C153153000, C153153000,
|
319 |
|
|
new Color(102, 153, 0), new Color(51, 153, 0),
|
320 |
|
|
new Color(0, 153, 0), new Color(0, 153, 51),
|
321 |
|
|
new Color(0, 153, 102), C000153153, C000153153,
|
322 |
|
|
|
323 |
|
|
// Row 8
|
324 |
|
|
new Color(51, 51, 51), C000102102, C000102102, C000102102, C000102102,
|
325 |
|
|
new Color(0, 51, 102), new Color(0, 0, 102),
|
326 |
|
|
new Color(51, 0, 102), C102000102, C102000102,
|
327 |
|
|
C102000102, C102000102, C102000102, C102000102,
|
328 |
|
|
C102000102, new Color(102, 0, 51),
|
329 |
|
|
new Color(102, 0, 0), new Color(102, 51, 0),
|
330 |
|
|
C102102000, C102102000, C102102000, C102102000,
|
331 |
|
|
C102102000, C102102000, C102102000,
|
332 |
|
|
new Color(51, 102, 0), new Color(0, 102, 0),
|
333 |
|
|
new Color(0, 102, 51), C000102102, C000102102,
|
334 |
|
|
C000102102,
|
335 |
|
|
|
336 |
|
|
// Row 9.
|
337 |
|
|
Color.BLACK, C000051051, C000051051, C000051051, C000051051, C000051051,
|
338 |
|
|
new Color(0, 0, 51), C051000051, C051000051,
|
339 |
|
|
C051000051, C051000051, C051000051, C051000051,
|
340 |
|
|
C051000051, C051000051, C051000051,
|
341 |
|
|
new Color(51, 0, 0), C051051000, C051051000,
|
342 |
|
|
C051051000, C051051000, C051051000, C051051000,
|
343 |
|
|
C051051000, C051051000, new Color(0, 51, 0),
|
344 |
|
|
C000051051, C000051051, C000051051, C000051051,
|
345 |
|
|
new Color(51, 51, 51)
|
346 |
|
|
};
|
347 |
|
|
|
348 |
|
|
/**
|
349 |
|
|
* Creates a new MainSwatchPanel object.
|
350 |
|
|
*/
|
351 |
|
|
MainSwatchPanel()
|
352 |
|
|
{
|
353 |
|
|
super();
|
354 |
|
|
numCols = 31;
|
355 |
|
|
numRows = 9;
|
356 |
|
|
initializeColors();
|
357 |
|
|
revalidate();
|
358 |
|
|
}
|
359 |
|
|
|
360 |
|
|
/**
|
361 |
|
|
* This method returns the color for the given position.
|
362 |
|
|
*
|
363 |
|
|
* @param x The x location for the position.
|
364 |
|
|
* @param y The y location for the position.
|
365 |
|
|
*
|
366 |
|
|
* @return The color for the given position.
|
367 |
|
|
*/
|
368 |
|
|
public Color getColorForPosition(int x, int y)
|
369 |
|
|
{
|
370 |
|
|
if (x % (cellWidth + gap) > cellWidth
|
371 |
|
|
|| y % (cellHeight + gap) > cellHeight)
|
372 |
|
|
// position is located in gap.
|
373 |
|
|
return null;
|
374 |
|
|
|
375 |
|
|
int row = y / (cellHeight + gap);
|
376 |
|
|
int col = x / (cellWidth + gap);
|
377 |
|
|
return colors[row * numCols + col];
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
/**
|
381 |
|
|
* This method initializes the colors for the main swatch panel.
|
382 |
|
|
*/
|
383 |
|
|
protected void initializeColors()
|
384 |
|
|
{
|
385 |
|
|
// Unnecessary
|
386 |
|
|
}
|
387 |
|
|
|
388 |
|
|
/**
|
389 |
|
|
* This method paints the main graphics panel with the given Graphics
|
390 |
|
|
* object.
|
391 |
|
|
*
|
392 |
|
|
* @param graphics The Graphics object to paint with.
|
393 |
|
|
*/
|
394 |
|
|
public void paint(Graphics graphics)
|
395 |
|
|
{
|
396 |
|
|
int index = 0;
|
397 |
|
|
Insets insets = getInsets();
|
398 |
|
|
int currX = insets.left;
|
399 |
|
|
int currY = insets.top;
|
400 |
|
|
Color saved = graphics.getColor();
|
401 |
|
|
|
402 |
|
|
for (int i = 0; i < numRows; i++)
|
403 |
|
|
{
|
404 |
|
|
for (int j = 0; j < numCols; j++)
|
405 |
|
|
{
|
406 |
|
|
graphics.setColor(colors[index++]);
|
407 |
|
|
graphics.fill3DRect(currX, currY, cellWidth, cellHeight, true);
|
408 |
|
|
currX += gap + cellWidth;
|
409 |
|
|
}
|
410 |
|
|
currX = insets.left;
|
411 |
|
|
currY += gap + cellHeight;
|
412 |
|
|
}
|
413 |
|
|
graphics.setColor(saved);
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
/**
|
417 |
|
|
* This method returns the tooltip text for the given MouseEvent.
|
418 |
|
|
*
|
419 |
|
|
* @param e The MouseEvent to find tooltip text for.
|
420 |
|
|
*
|
421 |
|
|
* @return The tooltip text.
|
422 |
|
|
*/
|
423 |
|
|
public String getToolTipText(MouseEvent e)
|
424 |
|
|
{
|
425 |
|
|
Color c = getColorForPosition(e.getX(), e.getY());
|
426 |
|
|
if (c == null)
|
427 |
|
|
return null;
|
428 |
|
|
return (c.getRed() + "," + c.getGreen() + "," + c.getBlue());
|
429 |
|
|
}
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
/**
|
433 |
|
|
* This class is the recent swatch panel. It holds recently selected colors.
|
434 |
|
|
*/
|
435 |
|
|
static class RecentSwatchPanel extends SwatchPanel
|
436 |
|
|
{
|
437 |
|
|
/** The array for storing recently stored colors. */
|
438 |
|
|
Color[] colors;
|
439 |
|
|
|
440 |
|
|
/** The default color. */
|
441 |
|
|
public static final Color defaultColor = Color.GRAY;
|
442 |
|
|
|
443 |
|
|
/** The index of the array that is the start. */
|
444 |
|
|
int start = 0;
|
445 |
|
|
|
446 |
|
|
/**
|
447 |
|
|
* Creates a new RecentSwatchPanel object.
|
448 |
|
|
*/
|
449 |
|
|
RecentSwatchPanel()
|
450 |
|
|
{
|
451 |
|
|
super();
|
452 |
|
|
numCols = 5;
|
453 |
|
|
numRows = 7;
|
454 |
|
|
initializeColors();
|
455 |
|
|
revalidate();
|
456 |
|
|
}
|
457 |
|
|
|
458 |
|
|
/**
|
459 |
|
|
* This method returns the color for the given position.
|
460 |
|
|
*
|
461 |
|
|
* @param x The x coordinate of the position.
|
462 |
|
|
* @param y The y coordinate of the position.
|
463 |
|
|
*
|
464 |
|
|
* @return The color for the given position.
|
465 |
|
|
*/
|
466 |
|
|
public Color getColorForPosition(int x, int y)
|
467 |
|
|
{
|
468 |
|
|
if (x % (cellWidth + gap) > cellWidth
|
469 |
|
|
|| y % (cellHeight + gap) > cellHeight)
|
470 |
|
|
// position is located in gap.
|
471 |
|
|
return null;
|
472 |
|
|
|
473 |
|
|
int row = y / (cellHeight + gap);
|
474 |
|
|
int col = x / (cellWidth + gap);
|
475 |
|
|
|
476 |
|
|
return colors[getIndexForCell(row, col)];
|
477 |
|
|
}
|
478 |
|
|
|
479 |
|
|
/**
|
480 |
|
|
* This method initializes the colors for the recent swatch panel.
|
481 |
|
|
*/
|
482 |
|
|
protected void initializeColors()
|
483 |
|
|
{
|
484 |
|
|
colors = new Color[numRows * numCols];
|
485 |
|
|
for (int i = 0; i < colors.length; i++)
|
486 |
|
|
colors[i] = defaultColor;
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
/**
|
490 |
|
|
* This method returns the array index for the given row and column.
|
491 |
|
|
*
|
492 |
|
|
* @param row The row.
|
493 |
|
|
* @param col The column.
|
494 |
|
|
*
|
495 |
|
|
* @return The array index for the given row and column.
|
496 |
|
|
*/
|
497 |
|
|
private int getIndexForCell(int row, int col)
|
498 |
|
|
{
|
499 |
|
|
return ((row * numCols) + col + start) % (numRows * numCols);
|
500 |
|
|
}
|
501 |
|
|
|
502 |
|
|
/**
|
503 |
|
|
* This method adds the given color to the beginning of the swatch panel.
|
504 |
|
|
* Package-private to avoid an accessor method.
|
505 |
|
|
*
|
506 |
|
|
* @param c The color to add.
|
507 |
|
|
*/
|
508 |
|
|
void addColorToQueue(Color c)
|
509 |
|
|
{
|
510 |
|
|
if (--start == -1)
|
511 |
|
|
start = numRows * numCols - 1;
|
512 |
|
|
|
513 |
|
|
colors[start] = c;
|
514 |
|
|
}
|
515 |
|
|
|
516 |
|
|
/**
|
517 |
|
|
* This method paints the panel with the given Graphics object.
|
518 |
|
|
*
|
519 |
|
|
* @param g The Graphics object to paint with.
|
520 |
|
|
*/
|
521 |
|
|
public void paint(Graphics g)
|
522 |
|
|
{
|
523 |
|
|
Color saved = g.getColor();
|
524 |
|
|
Insets insets = getInsets();
|
525 |
|
|
int currX = insets.left;
|
526 |
|
|
int currY = insets.top;
|
527 |
|
|
|
528 |
|
|
for (int i = 0; i < numRows; i++)
|
529 |
|
|
{
|
530 |
|
|
for (int j = 0; j < numCols; j++)
|
531 |
|
|
{
|
532 |
|
|
g.setColor(colors[getIndexForCell(i, j)]);
|
533 |
|
|
g.fill3DRect(currX, currY, cellWidth, cellHeight, true);
|
534 |
|
|
currX += cellWidth + gap;
|
535 |
|
|
}
|
536 |
|
|
currX = insets.left;
|
537 |
|
|
currY += cellWidth + gap;
|
538 |
|
|
}
|
539 |
|
|
}
|
540 |
|
|
|
541 |
|
|
/**
|
542 |
|
|
* This method returns the tooltip text for the given MouseEvent.
|
543 |
|
|
*
|
544 |
|
|
* @param e The MouseEvent.
|
545 |
|
|
*
|
546 |
|
|
* @return The tooltip text.
|
547 |
|
|
*/
|
548 |
|
|
public String getToolTipText(MouseEvent e)
|
549 |
|
|
{
|
550 |
|
|
Color c = getColorForPosition(e.getX(), e.getY());
|
551 |
|
|
if (c == null)
|
552 |
|
|
return null;
|
553 |
|
|
return c.getRed() + "," + c.getGreen() + "," + c.getBlue();
|
554 |
|
|
}
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
/**
|
558 |
|
|
* This class handles mouse events for the two swatch panels.
|
559 |
|
|
*/
|
560 |
|
|
class MouseHandler extends MouseAdapter
|
561 |
|
|
{
|
562 |
|
|
/**
|
563 |
|
|
* This method is called whenever the mouse is pressed.
|
564 |
|
|
*
|
565 |
|
|
* @param e The MouseEvent.
|
566 |
|
|
*/
|
567 |
|
|
public void mousePressed(MouseEvent e)
|
568 |
|
|
{
|
569 |
|
|
SwatchPanel panel = (SwatchPanel) e.getSource();
|
570 |
|
|
Color c = panel.getColorForPosition(e.getX(), e.getY());
|
571 |
|
|
recentPalette.addColorToQueue(c);
|
572 |
|
|
DefaultSwatchChooserPanel.this.getColorSelectionModel().setSelectedColor(c);
|
573 |
|
|
DefaultSwatchChooserPanel.this.repaint();
|
574 |
|
|
}
|
575 |
|
|
}
|
576 |
|
|
|
577 |
|
|
/**
|
578 |
|
|
* This is the layout manager for the main panel.
|
579 |
|
|
*/
|
580 |
|
|
static class MainPanelLayout implements LayoutManager
|
581 |
|
|
{
|
582 |
|
|
/**
|
583 |
|
|
* This method is called when a new component is added to the container.
|
584 |
|
|
*
|
585 |
|
|
* @param name The name of the component.
|
586 |
|
|
* @param comp The added component.
|
587 |
|
|
*/
|
588 |
|
|
public void addLayoutComponent(String name, Component comp)
|
589 |
|
|
{
|
590 |
|
|
// Nothing to do here.
|
591 |
|
|
}
|
592 |
|
|
|
593 |
|
|
/**
|
594 |
|
|
* This method is called to set the size and position of the child
|
595 |
|
|
* components for the given container.
|
596 |
|
|
*
|
597 |
|
|
* @param parent The container to lay out.
|
598 |
|
|
*/
|
599 |
|
|
public void layoutContainer(Container parent)
|
600 |
|
|
{
|
601 |
|
|
Component[] comps = parent.getComponents();
|
602 |
|
|
Insets insets = parent.getInsets();
|
603 |
|
|
Dimension[] pref = new Dimension[comps.length];
|
604 |
|
|
|
605 |
|
|
int xpos = 0;
|
606 |
|
|
int ypos = 0;
|
607 |
|
|
int maxHeight = 0;
|
608 |
|
|
int totalWidth = 0;
|
609 |
|
|
|
610 |
|
|
for (int i = 0; i < comps.length; i++)
|
611 |
|
|
{
|
612 |
|
|
pref[i] = comps[i].getPreferredSize();
|
613 |
|
|
if (pref[i] == null)
|
614 |
|
|
return;
|
615 |
|
|
maxHeight = Math.max(maxHeight, pref[i].height);
|
616 |
|
|
totalWidth += pref[i].width;
|
617 |
|
|
}
|
618 |
|
|
|
619 |
|
|
ypos = (parent.getSize().height - maxHeight) / 2 + insets.top;
|
620 |
|
|
xpos = insets.left + (parent.getSize().width - totalWidth) / 2;
|
621 |
|
|
|
622 |
|
|
for (int i = 0; i < comps.length; i++)
|
623 |
|
|
{
|
624 |
|
|
if (pref[i] == null)
|
625 |
|
|
continue;
|
626 |
|
|
comps[i].setBounds(xpos, ypos, pref[i].width, pref[i].height);
|
627 |
|
|
xpos += pref[i].width;
|
628 |
|
|
}
|
629 |
|
|
}
|
630 |
|
|
|
631 |
|
|
/**
|
632 |
|
|
* This method is called when a component is removed from the container.
|
633 |
|
|
*
|
634 |
|
|
* @param comp The component that was removed.
|
635 |
|
|
*/
|
636 |
|
|
public void removeLayoutComponent(Component comp)
|
637 |
|
|
{
|
638 |
|
|
// Nothing to do here.
|
639 |
|
|
}
|
640 |
|
|
|
641 |
|
|
/**
|
642 |
|
|
* This methods calculates the minimum layout size for the container.
|
643 |
|
|
*
|
644 |
|
|
* @param parent The container.
|
645 |
|
|
*
|
646 |
|
|
* @return The minimum layout size.
|
647 |
|
|
*/
|
648 |
|
|
public Dimension minimumLayoutSize(Container parent)
|
649 |
|
|
{
|
650 |
|
|
return preferredLayoutSize(parent);
|
651 |
|
|
}
|
652 |
|
|
|
653 |
|
|
/**
|
654 |
|
|
* This method returns the preferred layout size for the given container.
|
655 |
|
|
*
|
656 |
|
|
* @param parent The container.
|
657 |
|
|
*
|
658 |
|
|
* @return The preferred layout size.
|
659 |
|
|
*/
|
660 |
|
|
public Dimension preferredLayoutSize(Container parent)
|
661 |
|
|
{
|
662 |
|
|
int xmax = 0;
|
663 |
|
|
int ymax = 0;
|
664 |
|
|
|
665 |
|
|
Component[] comps = parent.getComponents();
|
666 |
|
|
Dimension pref;
|
667 |
|
|
|
668 |
|
|
for (int i = 0; i < comps.length; i++)
|
669 |
|
|
{
|
670 |
|
|
pref = comps[i].getPreferredSize();
|
671 |
|
|
if (pref == null)
|
672 |
|
|
continue;
|
673 |
|
|
xmax += pref.width;
|
674 |
|
|
ymax = Math.max(ymax, pref.height);
|
675 |
|
|
}
|
676 |
|
|
|
677 |
|
|
Insets insets = parent.getInsets();
|
678 |
|
|
|
679 |
|
|
return new Dimension(insets.left + insets.right + xmax,
|
680 |
|
|
insets.top + insets.bottom + ymax);
|
681 |
|
|
}
|
682 |
|
|
}
|
683 |
|
|
|
684 |
|
|
/**
|
685 |
|
|
* This is the layout manager for the recent swatch panel.
|
686 |
|
|
*/
|
687 |
|
|
static class RecentPanelLayout implements LayoutManager
|
688 |
|
|
{
|
689 |
|
|
/**
|
690 |
|
|
* This method is called when a component is added to the container.
|
691 |
|
|
*
|
692 |
|
|
* @param name The name of the component.
|
693 |
|
|
* @param comp The added component.
|
694 |
|
|
*/
|
695 |
|
|
public void addLayoutComponent(String name, Component comp)
|
696 |
|
|
{
|
697 |
|
|
// Nothing needs to be done.
|
698 |
|
|
}
|
699 |
|
|
|
700 |
|
|
/**
|
701 |
|
|
* This method sets the size and position of the child components of the
|
702 |
|
|
* given container.
|
703 |
|
|
*
|
704 |
|
|
* @param parent The container to lay out.
|
705 |
|
|
*/
|
706 |
|
|
public void layoutContainer(Container parent)
|
707 |
|
|
{
|
708 |
|
|
Component[] comps = parent.getComponents();
|
709 |
|
|
Dimension parentSize = parent.getSize();
|
710 |
|
|
Insets insets = parent.getInsets();
|
711 |
|
|
int currY = insets.top;
|
712 |
|
|
Dimension pref;
|
713 |
|
|
|
714 |
|
|
for (int i = 0; i < comps.length; i++)
|
715 |
|
|
{
|
716 |
|
|
pref = comps[i].getPreferredSize();
|
717 |
|
|
if (pref == null)
|
718 |
|
|
continue;
|
719 |
|
|
comps[i].setBounds(insets.left, currY, pref.width, pref.height);
|
720 |
|
|
currY += pref.height;
|
721 |
|
|
}
|
722 |
|
|
}
|
723 |
|
|
|
724 |
|
|
/**
|
725 |
|
|
* This method calculates the minimum layout size for the given container.
|
726 |
|
|
*
|
727 |
|
|
* @param parent The container.
|
728 |
|
|
*
|
729 |
|
|
* @return The minimum layout size.
|
730 |
|
|
*/
|
731 |
|
|
public Dimension minimumLayoutSize(Container parent)
|
732 |
|
|
{
|
733 |
|
|
return preferredLayoutSize(parent);
|
734 |
|
|
}
|
735 |
|
|
|
736 |
|
|
/**
|
737 |
|
|
* This method calculates the preferred layout size for the given
|
738 |
|
|
* container.
|
739 |
|
|
*
|
740 |
|
|
* @param parent The container.
|
741 |
|
|
*
|
742 |
|
|
* @return The preferred layout size.
|
743 |
|
|
*/
|
744 |
|
|
public Dimension preferredLayoutSize(Container parent)
|
745 |
|
|
{
|
746 |
|
|
int width = 0;
|
747 |
|
|
int height = 0;
|
748 |
|
|
Insets insets = parent.getInsets();
|
749 |
|
|
Component[] comps = parent.getComponents();
|
750 |
|
|
Dimension pref;
|
751 |
|
|
for (int i = 0; i < comps.length; i++)
|
752 |
|
|
{
|
753 |
|
|
pref = comps[i].getPreferredSize();
|
754 |
|
|
if (pref != null)
|
755 |
|
|
{
|
756 |
|
|
width = Math.max(width, pref.width);
|
757 |
|
|
height += pref.height;
|
758 |
|
|
}
|
759 |
|
|
}
|
760 |
|
|
|
761 |
|
|
return new Dimension(width + insets.left + insets.right,
|
762 |
|
|
height + insets.top + insets.bottom);
|
763 |
|
|
}
|
764 |
|
|
|
765 |
|
|
/**
|
766 |
|
|
* This method is called whenever a component is removed from the
|
767 |
|
|
* container.
|
768 |
|
|
*
|
769 |
|
|
* @param comp The removed component.
|
770 |
|
|
*/
|
771 |
|
|
public void removeLayoutComponent(Component comp)
|
772 |
|
|
{
|
773 |
|
|
// Nothing needs to be done.
|
774 |
|
|
}
|
775 |
|
|
}
|
776 |
|
|
|
777 |
|
|
/**
|
778 |
|
|
* Creates a new DefaultSwatchChooserPanel object.
|
779 |
|
|
*/
|
780 |
|
|
DefaultSwatchChooserPanel()
|
781 |
|
|
{
|
782 |
|
|
super();
|
783 |
|
|
}
|
784 |
|
|
|
785 |
|
|
/**
|
786 |
|
|
* This method updates the chooser panel with the new value from the
|
787 |
|
|
* JColorChooser.
|
788 |
|
|
*/
|
789 |
|
|
public void updateChooser()
|
790 |
|
|
{
|
791 |
|
|
// Nothing to do here yet.
|
792 |
|
|
}
|
793 |
|
|
|
794 |
|
|
/**
|
795 |
|
|
* This method builds the chooser panel.
|
796 |
|
|
*/
|
797 |
|
|
protected void buildChooser()
|
798 |
|
|
{
|
799 |
|
|
// The structure of the swatch panel is:
|
800 |
|
|
// One large panel (minus the insets).
|
801 |
|
|
// Inside that panel, there are two panels, one holds the palette.
|
802 |
|
|
// The other holds the label and the recent colors palette.
|
803 |
|
|
// The two palettes are two custom swatch panels.
|
804 |
|
|
setLayout(new MainPanelLayout());
|
805 |
|
|
|
806 |
|
|
JPanel mainPaletteHolder = new JPanel();
|
807 |
|
|
JPanel recentPaletteHolder = new JPanel();
|
808 |
|
|
|
809 |
|
|
mainPalette = new MainSwatchPanel();
|
810 |
|
|
recentPalette = new RecentSwatchPanel();
|
811 |
|
|
JLabel label = new JLabel("Recent:");
|
812 |
|
|
|
813 |
|
|
mouseHandler = new MouseHandler();
|
814 |
|
|
mainPalette.addMouseListener(mouseHandler);
|
815 |
|
|
recentPalette.addMouseListener(mouseHandler);
|
816 |
|
|
|
817 |
|
|
mainPaletteHolder.setLayout(new BorderLayout());
|
818 |
|
|
mainPaletteHolder.add(mainPalette, BorderLayout.CENTER);
|
819 |
|
|
|
820 |
|
|
recentPaletteHolder.setLayout(new RecentPanelLayout());
|
821 |
|
|
recentPaletteHolder.add(label);
|
822 |
|
|
recentPaletteHolder.add(recentPalette);
|
823 |
|
|
|
824 |
|
|
JPanel main = new JPanel();
|
825 |
|
|
main.add(mainPaletteHolder);
|
826 |
|
|
main.add(recentPaletteHolder);
|
827 |
|
|
|
828 |
|
|
this.add(main);
|
829 |
|
|
}
|
830 |
|
|
|
831 |
|
|
/**
|
832 |
|
|
* This method removes the chooser panel from the JColorChooser.
|
833 |
|
|
*
|
834 |
|
|
* @param chooser The JColorChooser this panel is being removed from.
|
835 |
|
|
*/
|
836 |
|
|
public void uninstallChooserPanel(JColorChooser chooser)
|
837 |
|
|
{
|
838 |
|
|
recentPalette = null;
|
839 |
|
|
mainPalette = null;
|
840 |
|
|
|
841 |
|
|
removeAll();
|
842 |
|
|
super.uninstallChooserPanel(chooser);
|
843 |
|
|
}
|
844 |
|
|
|
845 |
|
|
/**
|
846 |
|
|
* This method returns the JTabbedPane displayed name.
|
847 |
|
|
*
|
848 |
|
|
* @return The name displayed in the JTabbedPane.
|
849 |
|
|
*/
|
850 |
|
|
public String getDisplayName()
|
851 |
|
|
{
|
852 |
|
|
return "Swatches";
|
853 |
|
|
}
|
854 |
|
|
|
855 |
|
|
/**
|
856 |
|
|
* This method returns the small display icon.
|
857 |
|
|
*
|
858 |
|
|
* @return The small display icon.
|
859 |
|
|
*/
|
860 |
|
|
public Icon getSmallDisplayIcon()
|
861 |
|
|
{
|
862 |
|
|
return null;
|
863 |
|
|
}
|
864 |
|
|
|
865 |
|
|
/**
|
866 |
|
|
* This method returns the large display icon.
|
867 |
|
|
*
|
868 |
|
|
* @return The large display icon.
|
869 |
|
|
*/
|
870 |
|
|
public Icon getLargeDisplayIcon()
|
871 |
|
|
{
|
872 |
|
|
return null;
|
873 |
|
|
}
|
874 |
|
|
|
875 |
|
|
/**
|
876 |
|
|
* This method paints the chooser panel with the given Graphics object.
|
877 |
|
|
*
|
878 |
|
|
* @param g The Graphics object to paint with.
|
879 |
|
|
*/
|
880 |
|
|
public void paint(Graphics g)
|
881 |
|
|
{
|
882 |
|
|
super.paint(g);
|
883 |
|
|
}
|
884 |
|
|
|
885 |
|
|
/**
|
886 |
|
|
* This method returns the tooltip text for the given MouseEvent.
|
887 |
|
|
*
|
888 |
|
|
* @param e The MouseEvent.
|
889 |
|
|
*
|
890 |
|
|
* @return The tooltip text.
|
891 |
|
|
*/
|
892 |
|
|
public String getToolTipText(MouseEvent e)
|
893 |
|
|
{
|
894 |
|
|
return null;
|
895 |
|
|
}
|
896 |
|
|
}
|