KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > colorchooser > DefaultSwatchChooserPanel


1 /*
2  * @(#)DefaultSwatchChooserPanel.java 1.26 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.colorchooser;
9
10 import javax.swing.*;
11 import javax.swing.border.*;
12 import javax.swing.event.*;
13 import java.awt.*;
14 import java.awt.image.*;
15 import java.awt.event.*;
16 import java.beans.PropertyChangeEvent JavaDoc;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.io.Serializable JavaDoc;
19
20
21 /**
22  * The standard color swatch chooser.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @version 1.26 12/19/03
34  * @author Steve Wilson
35  */

36 class DefaultSwatchChooserPanel extends AbstractColorChooserPanel JavaDoc {
37
38     SwatchPanel swatchPanel;
39     RecentSwatchPanel recentSwatchPanel;
40     MouseListener mainSwatchListener;
41     MouseListener recentSwatchListener;
42
43     private static String JavaDoc recentStr = UIManager.getString("ColorChooser.swatchesRecentText");
44
45     public DefaultSwatchChooserPanel() {
46         super();
47     }
48
49     public String JavaDoc getDisplayName() {
50         return UIManager.getString("ColorChooser.swatchesNameText");
51     }
52
53     /**
54      * Provides a hint to the look and feel as to the
55      * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
56      * access the panel. A return value <= 0 indicates there is no mnemonic.
57      * <p>
58      * The return value here is a hint, it is ultimately up to the look
59      * and feel to honor the return value in some meaningful way.
60      * <p>
61      * This implementation looks up the value from the default
62      * <code>ColorChooser.swatchesMnemonic</code>, or if it
63      * isn't available (or not an <code>Integer</code>) returns -1.
64      * The lookup for the default is done through the <code>UIManager</code>:
65      * <code>UIManager.get("ColorChooser.swatchesMnemonic");</code>.
66      *
67      * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
68      * mnemonic
69      * @see #getDisplayedMnemonicIndex
70      * @since 1.4
71      */

72     public int getMnemonic() {
73         return getInt("ColorChooser.swatchesMnemonic", -1);
74     }
75
76     /**
77      * Provides a hint to the look and feel as to the index of the character in
78      * <code>getDisplayName</code> that should be visually identified as the
79      * mnemonic. The look and feel should only use this if
80      * <code>getMnemonic</code> returns a value > 0.
81      * <p>
82      * The return value here is a hint, it is ultimately up to the look
83      * and feel to honor the return value in some meaningful way. For example,
84      * a look and feel may wish to render each
85      * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
86      * and further use this return value to underline a character in
87      * the <code>getDisplayName</code>.
88      * <p>
89      * This implementation looks up the value from the default
90      * <code>ColorChooser.rgbDisplayedMnemonicIndex</code>, or if it
91      * isn't available (or not an <code>Integer</code>) returns -1.
92      * The lookup for the default is done through the <code>UIManager</code>:
93      * <code>UIManager.get("ColorChooser.swatchesDisplayedMnemonicIndex");</code>.
94      *
95      * @return Character index to render mnemonic for; -1 to provide no
96      * visual identifier for this panel.
97      * @see #getMnemonic
98      * @since 1.4
99      */

100     public int getDisplayedMnemonicIndex() {
101         return getInt("ColorChooser.swatchesDisplayedMnemonicIndex", -1);
102     }
103
104     public Icon getSmallDisplayIcon() {
105         return null;
106     }
107
108     public Icon getLargeDisplayIcon() {
109         return null;
110     }
111
112     /**
113      * The background color, foreground color, and font are already set to the
114      * defaults from the defaults table before this method is called.
115      */

116     public void installChooserPanel(JColorChooser enclosingChooser) {
117         super.installChooserPanel(enclosingChooser);
118     }
119
120     protected void buildChooser() {
121       
122         GridBagLayout gb = new GridBagLayout();
123         GridBagConstraints gbc = new GridBagConstraints();
124         JPanel superHolder = new JPanel(gb);
125
126         swatchPanel = new MainSwatchPanel();
127     swatchPanel.getAccessibleContext().setAccessibleName(getDisplayName());
128
129     recentSwatchPanel = new RecentSwatchPanel();
130     recentSwatchPanel.getAccessibleContext().setAccessibleName(recentStr);
131
132     mainSwatchListener = new MainSwatchListener();
133     swatchPanel.addMouseListener(mainSwatchListener);
134     recentSwatchListener = new RecentSwatchListener();
135     recentSwatchPanel.addMouseListener(recentSwatchListener);
136
137
138     Border border = new CompoundBorder( new LineBorder(Color.black),
139                         new LineBorder(Color.white) );
140     swatchPanel.setBorder(border);
141         gbc.weightx = 1.0;
142         gbc.gridwidth = 2;
143         gbc.gridheight = 2;
144         gbc.weighty = 1.0;
145         superHolder.add( swatchPanel, gbc );
146
147     recentSwatchPanel.addMouseListener(recentSwatchListener);
148     recentSwatchPanel.setBorder(border);
149     JPanel recentLabelHolder = new JPanel(new BorderLayout());
150     JLabel l = new JLabel(recentStr);
151     l.setLabelFor(recentSwatchPanel);
152     recentLabelHolder.add(l, BorderLayout.NORTH);
153         gbc.weighty = 0.0;
154         gbc.gridwidth = GridBagConstraints.REMAINDER;
155         gbc.gridheight = 1;
156     superHolder.add( recentLabelHolder, gbc );
157         superHolder.add( recentSwatchPanel, gbc );
158
159     add(superHolder);
160     
161     }
162
163     public void uninstallChooserPanel(JColorChooser enclosingChooser) {
164         super.uninstallChooserPanel(enclosingChooser);
165     swatchPanel.removeMouseListener(mainSwatchListener);
166     recentSwatchPanel.removeMouseListener(recentSwatchListener);
167     swatchPanel = null;
168     recentSwatchPanel = null;
169     mainSwatchListener = null;
170     recentSwatchListener = null;
171     removeAll(); // strip out all the sub-components
172
}
173
174     public void updateChooser() {
175
176     }
177
178
179     class RecentSwatchListener extends MouseAdapter implements Serializable JavaDoc {
180         public void mousePressed(MouseEvent e) {
181         Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
182         getColorSelectionModel().setSelectedColor(color);
183
184     }
185     }
186
187     class MainSwatchListener extends MouseAdapter implements Serializable JavaDoc {
188         public void mousePressed(MouseEvent e) {
189         Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
190         getColorSelectionModel().setSelectedColor(color);
191         recentSwatchPanel.setMostRecentColor(color);
192
193     }
194     }
195
196 }
197
198
199
200 class SwatchPanel extends JPanel {
201
202     protected Color[] colors;
203     protected Dimension swatchSize;
204     protected Dimension numSwatches;
205     protected Dimension gap;
206
207     public SwatchPanel() {
208         initValues();
209         initColors();
210     setToolTipText(""); // register for events
211
setOpaque(true);
212     setBackground(Color.white);
213     setRequestFocusEnabled(false);
214     }
215
216     public boolean isFocusTraversable() {
217         return false;
218     }
219
220     protected void initValues() {
221
222     }
223
224     public void paintComponent(Graphics g) {
225          g.setColor(getBackground());
226          g.fillRect(0,0,getWidth(), getHeight());
227      for (int row = 0; row < numSwatches.height; row++) {
228         for (int column = 0; column < numSwatches.width; column++) {
229
230           g.setColor( getColorForCell(column, row) );
231         int x;
232         if ((!this.getComponentOrientation().isLeftToRight()) &&
233             (this instanceof RecentSwatchPanel)) {
234             x = (numSwatches.width - column - 1) * (swatchSize.width + gap.width);
235         } else {
236             x = column * (swatchSize.width + gap.width);
237         }
238         int y = row * (swatchSize.height + gap.height);
239             g.fillRect( x, y, swatchSize.width, swatchSize.height);
240         g.setColor(Color.black);
241         g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1);
242         g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.height-1);
243         }
244      }
245     }
246
247     public Dimension getPreferredSize() {
248         int x = numSwatches.width * (swatchSize.width + gap.width) -1;
249     int y = numSwatches.height * (swatchSize.height + gap.height) -1;
250         return new Dimension( x, y );
251     }
252
253     protected void initColors() {
254
255       
256     }
257
258     public String JavaDoc getToolTipText(MouseEvent e) {
259         Color color = getColorForLocation(e.getX(), e.getY());
260         return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue();
261     }
262
263     public Color getColorForLocation( int x, int y ) {
264         int column;
265         if ((!this.getComponentOrientation().isLeftToRight()) &&
266             (this instanceof RecentSwatchPanel)) {
267             column = numSwatches.width - x / (swatchSize.width + gap.width) - 1;
268         } else {
269             column = x / (swatchSize.width + gap.width);
270         }
271         int row = y / (swatchSize.height + gap.height);
272     return getColorForCell(column, row);
273     }
274
275     private Color getColorForCell( int column, int row) {
276     return colors[ (row * numSwatches.width) + column ]; // (STEVE) - change data orientation here
277
}
278
279
280
281
282 }
283
284 class RecentSwatchPanel extends SwatchPanel {
285     protected void initValues() {
286         swatchSize = UIManager.getDimension("ColorChooser.swatchesRecentSwatchSize");
287     numSwatches = new Dimension( 5, 7 );
288         gap = new Dimension(1, 1);
289     }
290
291
292     protected void initColors() {
293         Color defaultRecentColor = UIManager.getColor("ColorChooser.swatchesDefaultRecentColor");
294         int numColors = numSwatches.width * numSwatches.height;
295     
296     colors = new Color[numColors];
297     for (int i = 0; i < numColors ; i++) {
298         colors[i] = defaultRecentColor;
299     }
300     }
301
302     public void setMostRecentColor(Color c) {
303
304     System.arraycopy( colors, 0, colors, 1, colors.length-1);
305         colors[0] = c;
306     repaint();
307     }
308
309 }
310
311 class MainSwatchPanel extends SwatchPanel {
312
313
314     protected void initValues() {
315         swatchSize = UIManager.getDimension("ColorChooser.swatchesSwatchSize");
316     numSwatches = new Dimension( 31, 9 );
317         gap = new Dimension(1, 1);
318     }
319
320     protected void initColors() {
321         int[] rawValues = initRawValues();
322         int numColors = rawValues.length / 3;
323     
324     colors = new Color[numColors];
325     for (int i = 0; i < numColors ; i++) {
326         colors[i] = new Color( rawValues[(i*3)], rawValues[(i*3)+1], rawValues[(i*3)+2] );
327     }
328     }
329
330     private int[] initRawValues() {
331
332         int[] rawValues = {
333 255, 255, 255, // first row.
334
204, 255, 255,
335 204, 204, 255,
336 204, 204, 255,
337 204, 204, 255,
338 204, 204, 255,
339 204, 204, 255,
340 204, 204, 255,
341 204, 204, 255,
342 204, 204, 255,
343 204, 204, 255,
344 255, 204, 255,
345 255, 204, 204,
346 255, 204, 204,
347 255, 204, 204,
348 255, 204, 204,
349 255, 204, 204,
350 255, 204, 204,
351 255, 204, 204,
352 255, 204, 204,
353 255, 204, 204,
354 255, 255, 204,
355 204, 255, 204,
356 204, 255, 204,
357 204, 255, 204,
358 204, 255, 204,
359 204, 255, 204,
360 204, 255, 204,
361 204, 255, 204,
362 204, 255, 204,
363 204, 255, 204,
364 204, 204, 204, // second row.
365
153, 255, 255,
366 153, 204, 255,
367 153, 153, 255,
368 153, 153, 255,
369 153, 153, 255,
370 153, 153, 255,
371 153, 153, 255,
372 153, 153, 255,
373 153, 153, 255,
374 204, 153, 255,
375 255, 153, 255,
376 255, 153, 204,
377 255, 153, 153,
378 255, 153, 153,
379 255, 153, 153,
380 255, 153, 153,
381 255, 153, 153,
382 255, 153, 153,
383 255, 153, 153,
384 255, 204, 153,
385 255, 255, 153,
386 204, 255, 153,
387 153, 255, 153,
388 153, 255, 153,
389 153, 255, 153,
390 153, 255, 153,
391 153, 255, 153,
392 153, 255, 153,
393 153, 255, 153,
394 153, 255, 204,
395 204, 204, 204, // third row
396
102, 255, 255,
397 102, 204, 255,
398 102, 153, 255,
399 102, 102, 255,
400 102, 102, 255,
401 102, 102, 255,
402 102, 102, 255,
403 102, 102, 255,
404 153, 102, 255,
405 204, 102, 255,
406 255, 102, 255,
407 255, 102, 204,
408 255, 102, 153,
409 255, 102, 102,
410 255, 102, 102,
411 255, 102, 102,
412 255, 102, 102,
413 255, 102, 102,
414 255, 153, 102,
415 255, 204, 102,
416 255, 255, 102,
417 204, 255, 102,
418 153, 255, 102,
419 102, 255, 102,
420 102, 255, 102,
421 102, 255, 102,
422 102, 255, 102,
423 102, 255, 102,
424 102, 255, 153,
425 102, 255, 204,
426 153, 153, 153, // fourth row
427
51, 255, 255,
428 51, 204, 255,
429 51, 153, 255,
430 51, 102, 255,
431 51, 51, 255,
432 51, 51, 255,
433 51, 51, 255,
434 102, 51, 255,
435 153, 51, 255,
436 204, 51, 255,
437 255, 51, 255,
438 255, 51, 204,
439 255, 51, 153,
440 255, 51, 102,
441 255, 51, 51,
442 255, 51, 51,
443 255, 51, 51,
444 255, 102, 51,
445 255, 153, 51,
446 255, 204, 51,
447 255, 255, 51,
448 204, 255, 51,
449 153, 244, 51,
450 102, 255, 51,
451 51, 255, 51,
452 51, 255, 51,
453 51, 255, 51,
454 51, 255, 102,
455 51, 255, 153,
456 51, 255, 204,
457 153, 153, 153, // Fifth row
458
0, 255, 255,
459 0, 204, 255,
460 0, 153, 255,
461 0, 102, 255,
462 0, 51, 255,
463 0, 0, 255,
464 51, 0, 255,
465 102, 0, 255,
466 153, 0, 255,
467 204, 0, 255,
468 255, 0, 255,
469 255, 0, 204,
470 255, 0, 153,
471 255, 0, 102,
472 255, 0, 51,
473 255, 0 , 0,
474 255, 51, 0,
475 255, 102, 0,
476 255, 153, 0,
477 255, 204, 0,
478 255, 255, 0,
479 204, 255, 0,
480 153, 255, 0,
481 102, 255, 0,
482 51, 255, 0,
483 0, 255, 0,
484 0, 255, 51,
485 0, 255, 102,
486 0, 255, 153,
487 0, 255, 204,
488 102, 102, 102, // sixth row
489
0, 204, 204,
490 0, 204, 204,
491 0, 153, 204,
492 0, 102, 204,
493 0, 51, 204,
494 0, 0, 204,
495 51, 0, 204,
496 102, 0, 204,
497 153, 0, 204,
498 204, 0, 204,
499 204, 0, 204,
500 204, 0, 204,
501 204, 0, 153,
502 204, 0, 102,
503 204, 0, 51,
504 204, 0, 0,
505 204, 51, 0,
506 204, 102, 0,
507 204, 153, 0,
508 204, 204, 0,
509 204, 204, 0,
510 204, 204, 0,
511 153, 204, 0,
512 102, 204, 0,
513 51, 204, 0,
514 0, 204, 0,
515 0, 204, 51,
516 0, 204, 102,
517 0, 204, 153,
518 0, 204, 204,
519 102, 102, 102, // seventh row
520
0, 153, 153,
521 0, 153, 153,
522 0, 153, 153,
523 0, 102, 153,
524 0, 51, 153,
525 0, 0, 153,
526 51, 0, 153,
527 102, 0, 153,
528 153, 0, 153,
529 153, 0, 153,
530 153, 0, 153,
531 153, 0, 153,
532 153, 0, 153,
533 153, 0, 102,
534 153, 0, 51,
535 153, 0, 0,
536 153, 51, 0,
537 153, 102, 0,
538 153, 153, 0,
539 153, 153, 0,
540 153, 153, 0,
541 153, 153, 0,
542 153, 153, 0,
543 102, 153, 0,
544 51, 153, 0,
545 0, 153, 0,
546 0, 153, 51,
547 0, 153, 102,
548 0, 153, 153,
549 0, 153, 153,
550 51, 51, 51, // eigth row
551
0, 102, 102,
552 0, 102, 102,
553 0, 102, 102,
554 0, 102, 102,
555 0, 51, 102,
556 0, 0, 102,
557 51, 0, 102,
558 102, 0, 102,
559 102, 0, 102,
560 102, 0, 102,
561 102, 0, 102,
562 102, 0, 102,
563 102, 0, 102,
564 102, 0, 102,
565 102, 0, 51,
566 102, 0, 0,
567 102, 51, 0,
568 102, 102, 0,
569 102, 102, 0,
570 102, 102, 0,
571 102, 102, 0,
572 102, 102, 0,
573 102, 102, 0,
574 102, 102, 0,
575 51, 102, 0,
576 0, 102, 0,
577 0, 102, 51,
578 0, 102, 102,
579 0, 102, 102,
580 0, 102, 102,
581 0, 0, 0, // ninth row
582
0, 51, 51,
583 0, 51, 51,
584 0, 51, 51,
585 0, 51, 51,
586 0, 51, 51,
587 0, 0, 51,
588 51, 0, 51,
589 51, 0, 51,
590 51, 0, 51,
591 51, 0, 51,
592 51, 0, 51,
593 51, 0, 51,
594 51, 0, 51,
595 51, 0, 51,
596 51, 0, 51,
597 51, 0, 0,
598 51, 51, 0,
599 51, 51, 0,
600 51, 51, 0,
601 51, 51, 0,
602 51, 51, 0,
603 51, 51, 0,
604 51, 51, 0,
605 51, 51, 0,
606 0, 51, 0,
607 0, 51, 51,
608 0, 51, 51,
609 0, 51, 51,
610 0, 51, 51,
611 51, 51, 51 };
612     return rawValues;
613     }
614 }
615
Popular Tags