KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)AbstractColorChooserPanel.java 1.20 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 java.awt.*;
11 import java.io.Serializable JavaDoc;
12 import javax.swing.*;
13 import javax.swing.event.*;
14
15 /**
16  * This is the abstract superclass for color choosers. If you want to add
17  * a new color chooser panel into a <code>JColorChooser</code>, subclass
18  * this class.
19  * <p>
20  * <strong>Warning:</strong>
21  * Serialized objects of this class will not be compatible with
22  * future Swing releases. The current serialization support is
23  * appropriate for short term storage or RMI between applications running
24  * the same version of Swing. As of 1.4, support for long term storage
25  * of all JavaBeans<sup><font size="-2">TM</font></sup>
26  * has been added to the <code>java.beans</code> package.
27  * Please see {@link java.beans.XMLEncoder}.
28  *
29  * @version 1.20 12/19/03
30  * @author Tom Santos
31  * @author Steve Wilson
32  */

33 public abstract class AbstractColorChooserPanel extends JPanel {
34
35     /**
36      *
37      */

38     private JColorChooser chooser;
39
40     /**
41      *
42      */

43     private ChangeListener colorListener;
44
45     /**
46      *
47      */

48     private boolean dirty = true;
49
50
51     /**
52       * Invoked automatically when the model's state changes.
53       * It is also called by <code>installChooserPanel</code> to allow
54       * you to set up the initial state of your chooser.
55       * Override this method to update your <code>ChooserPanel</code>.
56       */

57     public abstract void updateChooser();
58
59     /**
60      * Builds a new chooser panel.
61      */

62     protected abstract void buildChooser();
63
64     /**
65      * Returns a string containing the display name of the panel.
66      * @return the name of the display panel
67      */

68     public abstract String JavaDoc getDisplayName();
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 returns 0, indicating the
79      * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
80      * subclasses wishing a mnemonic will need to override this.
81      *
82      * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
83      * mnemonic
84      * @see #getDisplayedMnemonicIndex
85      * @since 1.4
86      */

87     public int getMnemonic() {
88         return 0;
89     }
90
91     /**
92      * Provides a hint to the look and feel as to the index of the character in
93      * <code>getDisplayName</code> that should be visually identified as the
94      * mnemonic. The look and feel should only use this if
95      * <code>getMnemonic</code> returns a value > 0.
96      * <p>
97      * The return value here is a hint, it is ultimately up to the look
98      * and feel to honor the return value in some meaningful way. For example,
99      * a look and feel may wish to render each
100      * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
101      * and further use this return value to underline a character in
102      * the <code>getDisplayName</code>.
103      * <p>
104      * This implementation returns -1, indicating the
105      * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
106      * subclasses wishing a mnemonic will need to override this.
107      *
108      * @return Character index to render mnemonic for; -1 to provide no
109      * visual identifier for this panel.
110      * @see #getMnemonic
111      * @since 1.4
112      */

113     public int getDisplayedMnemonicIndex() {
114         return -1;
115     }
116
117     /**
118      * Returns the small display icon for the panel.
119      * @return the small display icon
120      */

121     public abstract Icon getSmallDisplayIcon();
122
123     /**
124      * Returns the large display icon for the panel.
125      * @return the large display icon
126      */

127     public abstract Icon getLargeDisplayIcon();
128
129     /**
130      * Invoked when the panel is added to the chooser.
131      * If you override this, be sure to call <code>super</code>.
132      * @param enclosingChooser the panel to be added
133      * @exception RuntimeException if the chooser panel has already been
134      * installed
135      */

136     public void installChooserPanel(JColorChooser enclosingChooser) {
137         if (chooser != null) {
138         throw new RuntimeException JavaDoc ("This chooser panel is already installed");
139         }
140         chooser = enclosingChooser;
141     buildChooser();
142     updateChooser();
143     colorListener = new ModelListener();
144     getColorSelectionModel().addChangeListener(colorListener);
145     }
146
147     /**
148      * Invoked when the panel is removed from the chooser.
149      * If override this, be sure to call <code>super</code>.
150      */

151   public void uninstallChooserPanel(JColorChooser enclosingChooser) {
152         getColorSelectionModel().removeChangeListener(colorListener);
153         chooser = null;
154     }
155
156     /**
157       * Returns the model that the chooser panel is editing.
158       * @return the <code>ColorSelectionModel</code> model this panel
159       * is editing
160       */

161     public ColorSelectionModel JavaDoc getColorSelectionModel() {
162         return chooser.getSelectionModel();
163     }
164
165     /**
166      * Returns the color that is currently selected.
167      * @return the <code>Color</code> that is selected
168      */

169     protected Color getColorFromModel() {
170         return getColorSelectionModel().getSelectedColor();
171     }
172
173     /**
174      * Draws the panel.
175      * @param g the <code>Graphics</code> object
176      */

177     public void paint(Graphics g) {
178     if (dirty) {
179         updateChooser();
180         dirty = false;
181     }
182         super.paint(g);
183     }
184
185     /**
186      * Returns an integer from the defaults table. If <code>key</code> does
187      * not map to a valid <code>Integer</code>, <code>default</code> is
188      * returned.
189      *
190      * @param key an <code>Object</code> specifying the int
191      * @param defaultValue Returned value if <code>key</code> is not available,
192      * or is not an Integer
193      * @return the int
194      */

195     static int getInt(Object JavaDoc key, int defaultValue) {
196         Object JavaDoc value = UIManager.get(key);
197
198         if (value instanceof Integer JavaDoc) {
199             return ((Integer JavaDoc)value).intValue();
200         }
201         if (value instanceof String JavaDoc) {
202             try {
203                 return Integer.parseInt((String JavaDoc)value);
204             } catch (NumberFormatException JavaDoc nfe) {}
205         }
206         return defaultValue;
207     }
208
209     /**
210      *
211      */

212     class ModelListener implements ChangeListener, Serializable JavaDoc {
213         public void stateChanged(ChangeEvent e) {
214       if (isShowing()) { // isVisible
215
updateChooser();
216         dirty = false;
217         } else {
218             dirty = true;
219         }
220     }
221     }
222 }
223
Popular Tags