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