1 /*
2 * Copyright 1998-2008 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 java.awt;
29 import javax.swing;
30
31 /**
32 * This is the abstract superclass for color choosers. If you want to add
33 * a new color chooser panel into a <code>JColorChooser</code>, subclass
34 * this class.
35 * <p>
36 * <strong>Warning:</strong>
37 * Serialized objects of this class will not be compatible with
38 * future Swing releases. The current serialization support is
39 * appropriate for short term storage or RMI between applications running
40 * the same version of Swing. As of 1.4, support for long term storage
41 * of all JavaBeans<sup><font size="-2">TM</font></sup>
42 * has been added to the <code>java.beans</code> package.
43 * Please see {@link java.beans.XMLEncoder}.
44 *
45 * @author Tom Santos
46 * @author Steve Wilson
47 */
48 public abstract class AbstractColorChooserPanel extends JPanel {
49
50 /**
51 *
52 */
53 private JColorChooser chooser;
54
55 /**
56 * Invoked automatically when the model's state changes.
57 * It is also called by <code>installChooserPanel</code> to allow
58 * you to set up the initial state of your chooser.
59 * Override this method to update your <code>ChooserPanel</code>.
60 */
61 public abstract void updateChooser();
62
63 /**
64 * Builds a new chooser panel.
65 */
66 protected abstract void buildChooser();
67
68 /**
69 * Returns a string containing the display name of the panel.
70 * @return the name of the display panel
71 */
72 public abstract String getDisplayName();
73
74 /**
75 * Provides a hint to the look and feel as to the
76 * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
77 * access the panel. A return value <= 0 indicates there is no mnemonic.
78 * <p>
79 * The return value here is a hint, it is ultimately up to the look
80 * and feel to honor the return value in some meaningful way.
81 * <p>
82 * This implementation returns 0, indicating the
83 * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
84 * subclasses wishing a mnemonic will need to override this.
85 *
86 * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
87 * mnemonic
88 * @see #getDisplayedMnemonicIndex
89 * @since 1.4
90 */
91 public int getMnemonic() {
92 return 0;
93 }
94
95 /**
96 * Provides a hint to the look and feel as to the index of the character in
97 * <code>getDisplayName</code> that should be visually identified as the
98 * mnemonic. The look and feel should only use this if
99 * <code>getMnemonic</code> returns a value > 0.
100 * <p>
101 * The return value here is a hint, it is ultimately up to the look
102 * and feel to honor the return value in some meaningful way. For example,
103 * a look and feel may wish to render each
104 * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
105 * and further use this return value to underline a character in
106 * the <code>getDisplayName</code>.
107 * <p>
108 * This implementation returns -1, indicating the
109 * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
110 * subclasses wishing a mnemonic will need to override this.
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 -1;
119 }
120
121 /**
122 * Returns the small display icon for the panel.
123 * @return the small display icon
124 */
125 public abstract Icon getSmallDisplayIcon();
126
127 /**
128 * Returns the large display icon for the panel.
129 * @return the large display icon
130 */
131 public abstract Icon getLargeDisplayIcon();
132
133 /**
134 * Invoked when the panel is added to the chooser.
135 * If you override this, be sure to call <code>super</code>.
136 * @param enclosingChooser the panel to be added
137 * @exception RuntimeException if the chooser panel has already been
138 * installed
139 */
140 public void installChooserPanel(JColorChooser enclosingChooser) {
141 if (chooser != null) {
142 throw new RuntimeException ("This chooser panel is already installed");
143 }
144 chooser = enclosingChooser;
145 buildChooser();
146 updateChooser();
147 }
148
149 /**
150 * Invoked when the panel is removed from the chooser.
151 * If override this, be sure to call <code>super</code>.
152 */
153 public void uninstallChooserPanel(JColorChooser enclosingChooser) {
154 chooser = null;
155 }
156
157 /**
158 * Returns the model that the chooser panel is editing.
159 * @return the <code>ColorSelectionModel</code> model this panel
160 * is editing
161 */
162 public ColorSelectionModel getColorSelectionModel() {
163 return chooser.getSelectionModel();
164 }
165
166 /**
167 * Returns the color that is currently selected.
168 * @return the <code>Color</code> that is selected
169 */
170 protected Color getColorFromModel() {
171 return getColorSelectionModel().getSelectedColor();
172 }
173
174 /**
175 * Draws the panel.
176 * @param g the <code>Graphics</code> object
177 */
178 public void paint(Graphics g) {
179 super.paint(g);
180 }
181
182 /**
183 * Returns an integer from the defaults table. If <code>key</code> does
184 * not map to a valid <code>Integer</code>, <code>default</code> is
185 * returned.
186 *
187 * @param key an <code>Object</code> specifying the int
188 * @param defaultValue Returned value if <code>key</code> is not available,
189 * or is not an Integer
190 * @return the int
191 */
192 int getInt(Object key, int defaultValue) {
193 Object value = UIManager.get(key, getLocale());
194
195 if (value instanceof Integer) {
196 return ((Integer)value).intValue();
197 }
198 if (value instanceof String) {
199 try {
200 return Integer.parseInt((String)value);
201 } catch (NumberFormatException nfe) {}
202 }
203 return defaultValue;
204 }
205 }