1 /* |
|
2 * Copyright 1998-2004 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.event.*; |
|
30 import java.awt.*; |
|
31 import java.util.Locale; |
|
32 |
|
33 /** |
|
34 * The standard RGB chooser. |
|
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 Steve Wilson |
|
46 * @author Mark Davidson |
|
47 * @see JColorChooser |
|
48 * @see AbstractColorChooserPanel |
|
49 */ |
|
50 class DefaultRGBChooserPanel extends AbstractColorChooserPanel implements ChangeListener { |
|
51 |
|
52 protected JSlider redSlider; |
|
53 protected JSlider greenSlider; |
|
54 protected JSlider blueSlider; |
|
55 protected JSpinner redField; |
|
56 protected JSpinner blueField; |
|
57 protected JSpinner greenField; |
|
58 |
|
59 private final int minValue = 0; |
|
60 private final int maxValue = 255; |
|
61 |
|
62 private boolean isAdjusting = false; // indicates the fields are being set internally |
|
63 |
|
64 public DefaultRGBChooserPanel() { |
|
65 super(); |
|
66 setInheritsPopupMenu(true); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Sets the values of the controls to reflect the color |
|
71 */ |
|
72 private void setColor( Color newColor ) { |
|
73 int red = newColor.getRed(); |
|
74 int blue = newColor.getBlue(); |
|
75 int green = newColor.getGreen(); |
|
76 |
|
77 if (redSlider.getValue() != red) { |
|
78 redSlider.setValue(red); |
|
79 } |
|
80 if (greenSlider.getValue() != green) { |
|
81 greenSlider.setValue(green); |
|
82 } |
|
83 if (blueSlider.getValue() != blue) { |
|
84 blueSlider.setValue(blue); |
|
85 } |
|
86 |
|
87 if (((Integer)redField.getValue()).intValue() != red) |
|
88 redField.setValue(new Integer(red)); |
|
89 if (((Integer)greenField.getValue()).intValue() != green) |
|
90 greenField.setValue(new Integer(green)); |
|
91 if (((Integer)blueField.getValue()).intValue() != blue ) |
|
92 blueField.setValue(new Integer(blue)); |
|
93 } |
|
94 |
|
95 public String getDisplayName() { |
|
96 return UIManager.getString("ColorChooser.rgbNameText", getLocale()); |
|
97 } |
|
98 |
|
99 /** |
|
100 * Provides a hint to the look and feel as to the |
|
101 * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to |
|
102 * access the panel. A return value <= 0 indicates there is no mnemonic. |
|
103 * <p> |
|
104 * The return value here is a hint, it is ultimately up to the look |
|
105 * and feel to honor the return value in some meaningful way. |
|
106 * <p> |
|
107 * This implementation looks up the value from the default |
|
108 * <code>ColorChooser.rgbMnemonic</code>, or if it |
|
109 * isn't available (or not an <code>Integer</code>) returns -1. |
|
110 * The lookup for the default is done through the <code>UIManager</code>: |
|
111 * <code>UIManager.get("ColorChooser.rgbMnemonic");</code>. |
|
112 * |
|
113 * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no |
|
114 * mnemonic |
|
115 * @see #getDisplayedMnemonicIndex |
|
116 * @since 1.4 |
|
117 */ |
|
118 public int getMnemonic() { |
|
119 return getInt("ColorChooser.rgbMnemonic", -1); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Provides a hint to the look and feel as to the index of the character in |
|
124 * <code>getDisplayName</code> that should be visually identified as the |
|
125 * mnemonic. The look and feel should only use this if |
|
126 * <code>getMnemonic</code> returns a value > 0. |
|
127 * <p> |
|
128 * The return value here is a hint, it is ultimately up to the look |
|
129 * and feel to honor the return value in some meaningful way. For example, |
|
130 * a look and feel may wish to render each |
|
131 * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>, |
|
132 * and further use this return value to underline a character in |
|
133 * the <code>getDisplayName</code>. |
|
134 * <p> |
|
135 * This implementation looks up the value from the default |
|
136 * <code>ColorChooser.rgbDisplayedMnemonicIndex</code>, or if it |
|
137 * isn't available (or not an <code>Integer</code>) returns -1. |
|
138 * The lookup for the default is done through the <code>UIManager</code>: |
|
139 * <code>UIManager.get("ColorChooser.rgbDisplayedMnemonicIndex");</code>. |
|
140 * |
|
141 * @return Character index to render mnemonic for; -1 to provide no |
|
142 * visual identifier for this panel. |
|
143 * @see #getMnemonic |
|
144 * @since 1.4 |
|
145 */ |
|
146 public int getDisplayedMnemonicIndex() { |
|
147 return getInt("ColorChooser.rgbDisplayedMnemonicIndex", -1); |
|
148 } |
|
149 |
|
150 public Icon getSmallDisplayIcon() { |
|
151 return null; |
|
152 } |
|
153 |
|
154 public Icon getLargeDisplayIcon() { |
|
155 return null; |
|
156 } |
|
157 |
|
158 /** |
|
159 * The background color, foreground color, and font are already set to the |
|
160 * defaults from the defaults table before this method is called. |
|
161 */ |
|
162 public void installChooserPanel(JColorChooser enclosingChooser) { |
|
163 super.installChooserPanel(enclosingChooser); |
|
164 } |
|
165 |
|
166 protected void buildChooser() { |
|
167 |
|
168 Locale locale = getLocale(); |
|
169 String redString = UIManager.getString("ColorChooser.rgbRedText", locale); |
|
170 String greenString = UIManager.getString("ColorChooser.rgbGreenText", locale); |
|
171 String blueString = UIManager.getString("ColorChooser.rgbBlueText", locale); |
|
172 |
|
173 setLayout( new BorderLayout() ); |
|
174 Color color = getColorFromModel(); |
|
175 |
|
176 |
|
177 JPanel enclosure = new JPanel(); |
|
178 enclosure.setLayout( new SmartGridLayout( 3, 3 ) ); |
|
179 enclosure.setInheritsPopupMenu(true); |
|
180 |
|
181 // The panel that holds the sliders |
|
182 |
|
183 add( enclosure, BorderLayout.CENTER ); |
|
184 // sliderPanel.setBorder(new LineBorder(Color.black)); |
|
185 |
|
186 // The row for the red value |
|
187 JLabel l = new JLabel(redString); |
|
188 l.setDisplayedMnemonic(getInt("ColorChooser.rgbRedMnemonic", -1)); |
|
189 enclosure.add(l); |
|
190 redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getRed()); |
|
191 redSlider.setMajorTickSpacing( 85 ); |
|
192 redSlider.setMinorTickSpacing( 17 ); |
|
193 redSlider.setPaintTicks( true ); |
|
194 redSlider.setPaintLabels( true ); |
|
195 redSlider.setInheritsPopupMenu(true); |
|
196 enclosure.add( redSlider ); |
|
197 redField = new JSpinner( |
|
198 new SpinnerNumberModel(color.getRed(), minValue, maxValue, 1)); |
|
199 l.setLabelFor(redSlider); |
|
200 redField.setInheritsPopupMenu(true); |
|
201 JPanel redFieldHolder = new JPanel(new CenterLayout()); |
|
202 redFieldHolder.setInheritsPopupMenu(true); |
|
203 redField.addChangeListener(this); |
|
204 redFieldHolder.add(redField); |
|
205 enclosure.add(redFieldHolder); |
|
206 |
|
207 |
|
208 // The row for the green value |
|
209 l = new JLabel(greenString); |
|
210 l.setDisplayedMnemonic(getInt("ColorChooser.rgbGreenMnemonic", -1)); |
|
211 enclosure.add(l); |
|
212 greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getGreen()); |
|
213 greenSlider.setMajorTickSpacing( 85 ); |
|
214 greenSlider.setMinorTickSpacing( 17 ); |
|
215 greenSlider.setPaintTicks( true ); |
|
216 greenSlider.setPaintLabels( true ); |
|
217 greenSlider.setInheritsPopupMenu(true); |
|
218 enclosure.add(greenSlider); |
|
219 greenField = new JSpinner( |
|
220 new SpinnerNumberModel(color.getGreen(), minValue, maxValue, 1)); |
|
221 l.setLabelFor(greenSlider); |
|
222 greenField.setInheritsPopupMenu(true); |
|
223 JPanel greenFieldHolder = new JPanel(new CenterLayout()); |
|
224 greenFieldHolder.add(greenField); |
|
225 greenFieldHolder.setInheritsPopupMenu(true); |
|
226 greenField.addChangeListener(this); |
|
227 enclosure.add(greenFieldHolder); |
|
228 |
|
229 // The slider for the blue value |
|
230 l = new JLabel(blueString); |
|
231 l.setDisplayedMnemonic(getInt("ColorChooser.rgbBlueMnemonic", -1)); |
|
232 enclosure.add(l); |
|
233 blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getBlue()); |
|
234 blueSlider.setMajorTickSpacing( 85 ); |
|
235 blueSlider.setMinorTickSpacing( 17 ); |
|
236 blueSlider.setPaintTicks( true ); |
|
237 blueSlider.setPaintLabels( true ); |
|
238 blueSlider.setInheritsPopupMenu(true); |
|
239 enclosure.add(blueSlider); |
|
240 blueField = new JSpinner( |
|
241 new SpinnerNumberModel(color.getBlue(), minValue, maxValue, 1)); |
|
242 l.setLabelFor(blueSlider); |
|
243 blueField.setInheritsPopupMenu(true); |
|
244 JPanel blueFieldHolder = new JPanel(new CenterLayout()); |
|
245 blueFieldHolder.add(blueField); |
|
246 blueField.addChangeListener(this); |
|
247 blueFieldHolder.setInheritsPopupMenu(true); |
|
248 enclosure.add(blueFieldHolder); |
|
249 |
|
250 redSlider.addChangeListener( this ); |
|
251 greenSlider.addChangeListener( this ); |
|
252 blueSlider.addChangeListener( this ); |
|
253 |
|
254 redSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); |
|
255 greenSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); |
|
256 blueSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); |
|
257 } |
|
258 |
|
259 public void uninstallChooserPanel(JColorChooser enclosingChooser) { |
|
260 super.uninstallChooserPanel(enclosingChooser); |
|
261 removeAll(); |
|
262 } |
|
263 |
|
264 public void updateChooser() { |
|
265 if (!isAdjusting) { |
|
266 isAdjusting = true; |
|
267 |
|
268 setColor(getColorFromModel()); |
|
269 |
|
270 isAdjusting = false; |
|
271 } |
|
272 } |
|
273 |
|
274 public void stateChanged( ChangeEvent e ) { |
|
275 if ( e.getSource() instanceof JSlider && !isAdjusting) { |
|
276 |
|
277 int red = redSlider.getValue(); |
|
278 int green = greenSlider.getValue(); |
|
279 int blue = blueSlider.getValue() ; |
|
280 Color color = new Color (red, green, blue); |
|
281 |
|
282 getColorSelectionModel().setSelectedColor(color); |
|
283 } else if (e.getSource() instanceof JSpinner && !isAdjusting) { |
|
284 |
|
285 int red = ((Integer)redField.getValue()).intValue(); |
|
286 int green = ((Integer)greenField.getValue()).intValue(); |
|
287 int blue = ((Integer)blueField.getValue()).intValue(); |
|
288 Color color = new Color (red, green, blue); |
|
289 |
|
290 getColorSelectionModel().setSelectedColor(color); |
|
291 } |
|
292 } |
|
293 |
|
294 } |
|