1 /*
2 * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.swing.colorchooser;
27
28 import javax.swing;
29 import javax.swing.border;
30 import javax.swing.event;
31 import java.awt;
32 import java.awt.image;
33 import java.awt.event;
34 import java.beans.PropertyChangeEvent;
35 import java.beans.PropertyChangeListener;
36 import java.io.Serializable;
37 import javax.accessibility;
38
39
40 /**
41 * The standard color swatch chooser.
42 * <p>
43 * <strong>Warning:</strong>
44 * Serialized objects of this class will not be compatible with
45 * future Swing releases. The current serialization support is
46 * appropriate for short term storage or RMI between applications running
47 * the same version of Swing. As of 1.4, support for long term storage
48 * of all JavaBeans<sup><font size="-2">TM</font></sup>
49 * has been added to the <code>java.beans</code> package.
50 * Please see {@link java.beans.XMLEncoder}.
51 *
52 * @author Steve Wilson
53 */
54 class DefaultSwatchChooserPanel extends AbstractColorChooserPanel {
55
56 SwatchPanel swatchPanel;
57 RecentSwatchPanel recentSwatchPanel;
58 MouseListener mainSwatchListener;
59 MouseListener recentSwatchListener;
60
61 public DefaultSwatchChooserPanel() {
62 super();
63 setInheritsPopupMenu(true);
64 }
65
66 public String getDisplayName() {
67 return UIManager.getString("ColorChooser.swatchesNameText", getLocale());
68 }
69
70 /**
71 * Provides a hint to the look and feel as to the
72 * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
73 * access the panel. A return value <= 0 indicates there is no mnemonic.
74 * <p>
75 * The return value here is a hint, it is ultimately up to the look
76 * and feel to honor the return value in some meaningful way.
77 * <p>
78 * This implementation looks up the value from the default
79 * <code>ColorChooser.swatchesMnemonic</code>, or if it
80 * isn't available (or not an <code>Integer</code>) returns -1.
81 * The lookup for the default is done through the <code>UIManager</code>:
82 * <code>UIManager.get("ColorChooser.swatchesMnemonic");</code>.
83 *
84 * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
85 * mnemonic
86 * @see #getDisplayedMnemonicIndex
87 * @since 1.4
88 */
89 public int getMnemonic() {
90 return getInt("ColorChooser.swatchesMnemonic", -1);
91 }
92
93 /**
94 * Provides a hint to the look and feel as to the index of the character in
95 * <code>getDisplayName</code> that should be visually identified as the
96 * mnemonic. The look and feel should only use this if
97 * <code>getMnemonic</code> returns a value > 0.
98 * <p>
99 * The return value here is a hint, it is ultimately up to the look
100 * and feel to honor the return value in some meaningful way. For example,
101 * a look and feel may wish to render each
102 * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
103 * and further use this return value to underline a character in
104 * the <code>getDisplayName</code>.
105 * <p>
106 * This implementation looks up the value from the default
107 * <code>ColorChooser.rgbDisplayedMnemonicIndex</code>, or if it
108 * isn't available (or not an <code>Integer</code>) returns -1.
109 * The lookup for the default is done through the <code>UIManager</code>:
110 * <code>UIManager.get("ColorChooser.swatchesDisplayedMnemonicIndex");</code>.
111 *
112 * @return Character index to render mnemonic for; -1 to provide no
113 * visual identifier for this panel.
114 * @see #getMnemonic
115 * @since 1.4
116 */
117 public int getDisplayedMnemonicIndex() {
118 return getInt("ColorChooser.swatchesDisplayedMnemonicIndex", -1);
119 }
120
121 public Icon getSmallDisplayIcon() {
122 return null;
123 }
124
125 public Icon getLargeDisplayIcon() {
126 return null;
127 }
128
129 /**
130 * The background color, foreground color, and font are already set to the
131 * defaults from the defaults table before this method is called.
132 */
133 public void installChooserPanel(JColorChooser enclosingChooser) {
134 super.installChooserPanel(enclosingChooser);
135 }
136
137 protected void buildChooser() {
138
139 String recentStr = UIManager.getString("ColorChooser.swatchesRecentText", getLocale());
140
141 GridBagLayout gb = new GridBagLayout();
142 GridBagConstraints gbc = new GridBagConstraints();
143 JPanel superHolder = new JPanel(gb);
144
145 swatchPanel = new MainSwatchPanel();
146 swatchPanel.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,
147 getDisplayName());
148 swatchPanel.setInheritsPopupMenu(true);
149
150 recentSwatchPanel = new RecentSwatchPanel();
151 recentSwatchPanel.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY,
152 recentStr);
153
154 mainSwatchListener = new MainSwatchListener();
155 swatchPanel.addMouseListener(mainSwatchListener);
156 recentSwatchListener = new RecentSwatchListener();
157 recentSwatchPanel.addMouseListener(recentSwatchListener);
158
159 JPanel mainHolder = new JPanel(new BorderLayout());
160 Border border = new CompoundBorder( new LineBorder(Color.black),
161 new LineBorder(Color.white) );
162 mainHolder.setBorder(border);
163 mainHolder.add(swatchPanel, BorderLayout.CENTER);
164
165 gbc.anchor = GridBagConstraints.LAST_LINE_START;
166 gbc.gridwidth = 1;
167 gbc.gridheight = 2;
168 Insets oldInsets = gbc.insets;
169 gbc.insets = new Insets(0, 0, 0, 10);
170 superHolder.add(mainHolder, gbc);
171 gbc.insets = oldInsets;
172
173 recentSwatchPanel.addMouseListener(recentSwatchListener);
174 recentSwatchPanel.setInheritsPopupMenu(true);
175 JPanel recentHolder = new JPanel( new BorderLayout() );
176 recentHolder.setBorder(border);
177 recentHolder.setInheritsPopupMenu(true);
178 recentHolder.add(recentSwatchPanel, BorderLayout.CENTER);
179
180 JLabel l = new JLabel(recentStr);
181 l.setLabelFor(recentSwatchPanel);
182
183 gbc.gridwidth = GridBagConstraints.REMAINDER;
184 gbc.gridheight = 1;
185 gbc.weighty = 1.0;
186 superHolder.add(l, gbc);
187
188 gbc.weighty = 0;
189 gbc.gridheight = GridBagConstraints.REMAINDER;
190 gbc.insets = new Insets(0, 0, 0, 2);
191 superHolder.add(recentHolder, gbc);
192 superHolder.setInheritsPopupMenu(true);
193
194 add(superHolder);
195 }
196
197 public void uninstallChooserPanel(JColorChooser enclosingChooser) {
198 super.uninstallChooserPanel(enclosingChooser);
199 swatchPanel.removeMouseListener(mainSwatchListener);
200 recentSwatchPanel.removeMouseListener(recentSwatchListener);
201 swatchPanel = null;
202 recentSwatchPanel = null;
203 mainSwatchListener = null;
204 recentSwatchListener = null;
205 removeAll(); // strip out all the sub-components
206 }
207
208 public void updateChooser() {
209
210 }
211
212
213 class RecentSwatchListener extends MouseAdapter implements Serializable {
214 public void mousePressed(MouseEvent e) {
215 Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
216 getColorSelectionModel().setSelectedColor(color);
217
218 }
219 }
220
221 class MainSwatchListener extends MouseAdapter implements Serializable {
222 public void mousePressed(MouseEvent e) {
223 Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
224 getColorSelectionModel().setSelectedColor(color);
225 recentSwatchPanel.setMostRecentColor(color);
226
227 }
228 }
229
230 }
231
232
233
234 class SwatchPanel extends JPanel {
235
236 protected Color[] colors;
237 protected Dimension swatchSize;
238 protected Dimension numSwatches;
239 protected Dimension gap;
240
241 public SwatchPanel() {
242 initValues();
243 initColors();
244 setToolTipText(""); // register for events
245 setOpaque(true);
246 setBackground(Color.white);
247 setRequestFocusEnabled(false);
248 setInheritsPopupMenu(true);
249 }
250
251 public boolean isFocusTraversable() {
252 return false;
253 }
254
255 protected void initValues() {
256
257 }
258
259 public void paintComponent(Graphics g) {
260 g.setColor(getBackground());
261 g.fillRect(0,0,getWidth(), getHeight());
262 for (int row = 0; row < numSwatches.height; row++) {
263 int y = row * (swatchSize.height + gap.height);
264 for (int column = 0; column < numSwatches.width; column++) {
265
266 g.setColor( getColorForCell(column, row) );
267 int x;
268 if ((!this.getComponentOrientation().isLeftToRight()) &&
269 (this instanceof RecentSwatchPanel)) {
270 x = (numSwatches.width - column - 1) * (swatchSize.width + gap.width);
271 } else {
272 x = column * (swatchSize.width + gap.width);
273 }
274 g.fillRect( x, y, swatchSize.width, swatchSize.height);
275 g.setColor(Color.black);
276 g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1);
277 g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.height-1);
278 }
279 }
280 }
281
282 public Dimension getPreferredSize() {
283 int x = numSwatches.width * (swatchSize.width + gap.width) - 1;
284 int y = numSwatches.height * (swatchSize.height + gap.height) - 1;
285 return new Dimension( x, y );
286 }
287
288 protected void initColors() {
289
290
291 }
292
293 public String getToolTipText(MouseEvent e) {
294 Color color = getColorForLocation(e.getX(), e.getY());
295 return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue();
296 }
297
298 public Color getColorForLocation( int x, int y ) {
299 int column;
300 if ((!this.getComponentOrientation().isLeftToRight()) &&
301 (this instanceof RecentSwatchPanel)) {
302 column = numSwatches.width - x / (swatchSize.width + gap.width) - 1;
303 } else {
304 column = x / (swatchSize.width + gap.width);
305 }
306 int row = y / (swatchSize.height + gap.height);
307 return getColorForCell(column, row);
308 }
309
310 private Color getColorForCell( int column, int row) {
311 return colors[ (row * numSwatches.width) + column ]; // (STEVE) - change data orientation here
312 }
313
314
315
316
317 }
318
319 class RecentSwatchPanel extends SwatchPanel {
320 protected void initValues() {
321 swatchSize = UIManager.getDimension("ColorChooser.swatchesRecentSwatchSize", getLocale());
322 numSwatches = new Dimension( 5, 7 );
323 gap = new Dimension(1, 1);
324 }
325
326
327 protected void initColors() {
328 Color defaultRecentColor = UIManager.getColor("ColorChooser.swatchesDefaultRecentColor", getLocale());
329 int numColors = numSwatches.width * numSwatches.height;
330
331 colors = new Color[numColors];
332 for (int i = 0; i < numColors ; i++) {
333 colors[i] = defaultRecentColor;
334 }
335 }
336
337 public void setMostRecentColor(Color c) {
338
339 System.arraycopy( colors, 0, colors, 1, colors.length-1);
340 colors[0] = c;
341 repaint();
342 }
343
344 }
345
346 class MainSwatchPanel extends SwatchPanel {
347
348
349 protected void initValues() {
350 swatchSize = UIManager.getDimension("ColorChooser.swatchesSwatchSize", getLocale());
351 numSwatches = new Dimension( 31, 9 );
352 gap = new Dimension(1, 1);
353 }
354
355 protected void initColors() {
356 int[] rawValues = initRawValues();
357 int numColors = rawValues.length / 3;
358
359 colors = new Color[numColors];
360 for (int i = 0; i < numColors ; i++) {
361 colors[i] = new Color( rawValues[(i*3)], rawValues[(i*3)+1], rawValues[(i*3)+2] );
362 }
363 }
364
365 private int[] initRawValues() {
366
367 int[] rawValues = {
368 255, 255, 255, // first row.
369 204, 255, 255,
370 204, 204, 255,
371 204, 204, 255,
372 204, 204, 255,
373 204, 204, 255,
374 204, 204, 255,
375 204, 204, 255,
376 204, 204, 255,
377 204, 204, 255,
378 204, 204, 255,
379 255, 204, 255,
380 255, 204, 204,
381 255, 204, 204,
382 255, 204, 204,
383 255, 204, 204,
384 255, 204, 204,
385 255, 204, 204,
386 255, 204, 204,
387 255, 204, 204,
388 255, 204, 204,
389 255, 255, 204,
390 204, 255, 204,
391 204, 255, 204,
392 204, 255, 204,
393 204, 255, 204,
394 204, 255, 204,
395 204, 255, 204,
396 204, 255, 204,
397 204, 255, 204,
398 204, 255, 204,
399 204, 204, 204, // second row.
400 153, 255, 255,
401 153, 204, 255,
402 153, 153, 255,
403 153, 153, 255,
404 153, 153, 255,
405 153, 153, 255,
406 153, 153, 255,
407 153, 153, 255,
408 153, 153, 255,
409 204, 153, 255,
410 255, 153, 255,
411 255, 153, 204,
412 255, 153, 153,
413 255, 153, 153,
414 255, 153, 153,
415 255, 153, 153,
416 255, 153, 153,
417 255, 153, 153,
418 255, 153, 153,
419 255, 204, 153,
420 255, 255, 153,
421 204, 255, 153,
422 153, 255, 153,
423 153, 255, 153,
424 153, 255, 153,
425 153, 255, 153,
426 153, 255, 153,
427 153, 255, 153,
428 153, 255, 153,
429 153, 255, 204,
430 204, 204, 204, // third row
431 102, 255, 255,
432 102, 204, 255,
433 102, 153, 255,
434 102, 102, 255,
435 102, 102, 255,
436 102, 102, 255,
437 102, 102, 255,
438 102, 102, 255,
439 153, 102, 255,
440 204, 102, 255,
441 255, 102, 255,
442 255, 102, 204,
443 255, 102, 153,
444 255, 102, 102,
445 255, 102, 102,
446 255, 102, 102,
447 255, 102, 102,
448 255, 102, 102,
449 255, 153, 102,
450 255, 204, 102,
451 255, 255, 102,
452 204, 255, 102,
453 153, 255, 102,
454 102, 255, 102,
455 102, 255, 102,
456 102, 255, 102,
457 102, 255, 102,
458 102, 255, 102,
459 102, 255, 153,
460 102, 255, 204,
461 153, 153, 153, // fourth row
462 51, 255, 255,
463 51, 204, 255,
464 51, 153, 255,
465 51, 102, 255,
466 51, 51, 255,
467 51, 51, 255,
468 51, 51, 255,
469 102, 51, 255,
470 153, 51, 255,
471 204, 51, 255,
472 255, 51, 255,
473 255, 51, 204,
474 255, 51, 153,
475 255, 51, 102,
476 255, 51, 51,
477 255, 51, 51,
478 255, 51, 51,
479 255, 102, 51,
480 255, 153, 51,
481 255, 204, 51,
482 255, 255, 51,
483 204, 255, 51,
484 153, 255, 51,
485 102, 255, 51,
486 51, 255, 51,
487 51, 255, 51,
488 51, 255, 51,
489 51, 255, 102,
490 51, 255, 153,
491 51, 255, 204,
492 153, 153, 153, // Fifth row
493 0, 255, 255,
494 0, 204, 255,
495 0, 153, 255,
496 0, 102, 255,
497 0, 51, 255,
498 0, 0, 255,
499 51, 0, 255,
500 102, 0, 255,
501 153, 0, 255,
502 204, 0, 255,
503 255, 0, 255,
504 255, 0, 204,
505 255, 0, 153,
506 255, 0, 102,
507 255, 0, 51,
508 255, 0 , 0,
509 255, 51, 0,
510 255, 102, 0,
511 255, 153, 0,
512 255, 204, 0,
513 255, 255, 0,
514 204, 255, 0,
515 153, 255, 0,
516 102, 255, 0,
517 51, 255, 0,
518 0, 255, 0,
519 0, 255, 51,
520 0, 255, 102,
521 0, 255, 153,
522 0, 255, 204,
523 102, 102, 102, // sixth row
524 0, 204, 204,
525 0, 204, 204,
526 0, 153, 204,
527 0, 102, 204,
528 0, 51, 204,
529 0, 0, 204,
530 51, 0, 204,
531 102, 0, 204,
532 153, 0, 204,
533 204, 0, 204,
534 204, 0, 204,
535 204, 0, 204,
536 204, 0, 153,
537 204, 0, 102,
538 204, 0, 51,
539 204, 0, 0,
540 204, 51, 0,
541 204, 102, 0,
542 204, 153, 0,
543 204, 204, 0,
544 204, 204, 0,
545 204, 204, 0,
546 153, 204, 0,
547 102, 204, 0,
548 51, 204, 0,
549 0, 204, 0,
550 0, 204, 51,
551 0, 204, 102,
552 0, 204, 153,
553 0, 204, 204,
554 102, 102, 102, // seventh row
555 0, 153, 153,
556 0, 153, 153,
557 0, 153, 153,
558 0, 102, 153,
559 0, 51, 153,
560 0, 0, 153,
561 51, 0, 153,
562 102, 0, 153,
563 153, 0, 153,
564 153, 0, 153,
565 153, 0, 153,
566 153, 0, 153,
567 153, 0, 153,
568 153, 0, 102,
569 153, 0, 51,
570 153, 0, 0,
571 153, 51, 0,
572 153, 102, 0,
573 153, 153, 0,
574 153, 153, 0,
575 153, 153, 0,
576 153, 153, 0,
577 153, 153, 0,
578 102, 153, 0,
579 51, 153, 0,
580 0, 153, 0,
581 0, 153, 51,
582 0, 153, 102,
583 0, 153, 153,
584 0, 153, 153,
585 51, 51, 51, // eigth row
586 0, 102, 102,
587 0, 102, 102,
588 0, 102, 102,
589 0, 102, 102,
590 0, 51, 102,
591 0, 0, 102,
592 51, 0, 102,
593 102, 0, 102,
594 102, 0, 102,
595 102, 0, 102,
596 102, 0, 102,
597 102, 0, 102,
598 102, 0, 102,
599 102, 0, 102,
600 102, 0, 51,
601 102, 0, 0,
602 102, 51, 0,
603 102, 102, 0,
604 102, 102, 0,
605 102, 102, 0,
606 102, 102, 0,
607 102, 102, 0,
608 102, 102, 0,
609 102, 102, 0,
610 51, 102, 0,
611 0, 102, 0,
612 0, 102, 51,
613 0, 102, 102,
614 0, 102, 102,
615 0, 102, 102,
616 0, 0, 0, // ninth row
617 0, 51, 51,
618 0, 51, 51,
619 0, 51, 51,
620 0, 51, 51,
621 0, 51, 51,
622 0, 0, 51,
623 51, 0, 51,
624 51, 0, 51,
625 51, 0, 51,
626 51, 0, 51,
627 51, 0, 51,
628 51, 0, 51,
629 51, 0, 51,
630 51, 0, 51,
631 51, 0, 51,
632 51, 0, 0,
633 51, 51, 0,
634 51, 51, 0,
635 51, 51, 0,
636 51, 51, 0,
637 51, 51, 0,
638 51, 51, 0,
639 51, 51, 0,
640 51, 51, 0,
641 0, 51, 0,
642 0, 51, 51,
643 0, 51, 51,
644 0, 51, 51,
645 0, 51, 51,
646 51, 51, 51 };
647 return rawValues;
648 }
649 }