1 /* DefaultRGHChooserPanel.java --
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.swing
.colorchooser
;
41 import java
.awt
.Color
;
42 import java
.awt
.Graphics
;
43 import java
.awt
.GridBagConstraints
;
44 import java
.awt
.GridBagLayout
;
46 import javax
.swing
.Icon
;
47 import javax
.swing
.JColorChooser
;
48 import javax
.swing
.JLabel
;
49 import javax
.swing
.JSlider
;
50 import javax
.swing
.JSpinner
;
51 import javax
.swing
.SpinnerNumberModel
;
52 import javax
.swing
.SwingConstants
;
53 import javax
.swing
.event
.ChangeEvent
;
54 import javax
.swing
.event
.ChangeListener
;
57 * This is the default RGB panel for the JColorChooser. The color is selected
58 * using three sliders that represent the RGB values.
60 class DefaultRGBChooserPanel
extends AbstractColorChooserPanel
63 * This class handles the slider value changes for all three sliders.
65 class SliderHandler
implements ChangeListener
68 * This method is called whenever any of the slider values change.
70 * @param e The ChangeEvent.
72 public void stateChanged(ChangeEvent e
)
77 int color
= R
.getValue() << 16 | G
.getValue() << 8 | B
.getValue();
80 getColorSelectionModel().setSelectedColor(new Color(color
));
86 * This class handles the Spinner values changing.
88 class SpinnerHandler
implements ChangeListener
91 * This method is called whenever any of the JSpinners change values.
93 * @param e The ChangeEvent.
95 public void stateChanged(ChangeEvent e
)
100 int red
= ((Number
) RSpinner
.getValue()).intValue();
101 int green
= ((Number
) GSpinner
.getValue()).intValue();
102 int blue
= ((Number
) BSpinner
.getValue()).intValue();
104 int color
= red
<< 16 | green
<< 8 | blue
;
106 spinnerChange
= true;
107 getColorSelectionModel().setSelectedColor(new Color(color
));
108 spinnerChange
= false;
112 /** Whether the color change was initiated by the spinners.
113 * This is package-private to avoid an accessor method. */
114 transient boolean spinnerChange
= false;
116 /** Whether the color change was initiated by the sliders.
117 * This is package-private to avoid an accessor method. */
118 transient boolean sliderChange
= false;
121 * Whether the change was forced by the chooser (meaning the color has
122 * already been changed).
123 * This is package-private to avoid an accessor method.
125 transient boolean updateChange
= false;
127 /** The ChangeListener for the sliders. */
128 private transient ChangeListener colorChanger
;
130 /** The ChangeListener for the spinners. */
131 private transient ChangeListener spinnerHandler
;
133 /** The slider that handles the red values.
134 * This is package-private to avoid an accessor method. */
137 /** The slider that handles the green values.
138 * This is package-private to avoid an accessor method. */
141 /** The slider that handles the blue values.
142 * This is package-private to avoid an accessor method. */
145 /** The label for the red slider. */
146 private transient JLabel RLabel
;
148 /** The label for the green slider. */
149 private transient JLabel GLabel
;
151 /** The label for the blue slider. */
152 private transient JLabel BLabel
;
154 /** The spinner that handles the red values.
155 * This is package-private to avoid an accessor method. */
156 transient JSpinner RSpinner
;
158 /** The spinner that handles the green values.
159 * This is package-private to avoid an accessor method. */
160 transient JSpinner GSpinner
;
162 /** The spinner that handles the blue values.
163 * This is package-private to avoid an accessor method. */
164 transient JSpinner BSpinner
;
167 * Creates a new DefaultRGBChooserPanel object.
169 public DefaultRGBChooserPanel()
175 * This method returns the name displayed in the JTabbedPane.
177 * @return The name displayed in the JTabbedPane.
179 public String
getDisplayName()
185 * This method updates the chooser panel with the new color chosen in the
188 public void updateChooser()
190 Color c
= getColorFromModel();
191 int rgb
= c
.getRGB();
193 int red
= rgb
>> 16 & 0xff;
194 int green
= rgb
>> 8 & 0xff;
195 int blue
= rgb
& 0xff;
210 if (GSpinner
!= null)
211 GSpinner
.setValue(new Integer(green
));
212 if (RSpinner
!= null)
213 RSpinner
.setValue(new Integer(red
));
214 if (BSpinner
!= null)
215 BSpinner
.setValue(new Integer(blue
));
218 updateChange
= false;
225 * This method builds the chooser panel.
227 protected void buildChooser()
229 setLayout(new GridBagLayout());
231 RLabel
= new JLabel("Red");
232 RLabel
.setDisplayedMnemonic('d');
233 GLabel
= new JLabel("Green");
234 GLabel
.setDisplayedMnemonic('n');
235 BLabel
= new JLabel("Blue");
236 BLabel
.setDisplayedMnemonic('B');
238 R
= new JSlider(SwingConstants
.HORIZONTAL
, 0, 255, 255);
239 G
= new JSlider(SwingConstants
.HORIZONTAL
, 0, 255, 255);
240 B
= new JSlider(SwingConstants
.HORIZONTAL
, 0, 255, 255);
242 R
.setPaintTicks(true);
243 R
.setSnapToTicks(false);
244 G
.setPaintTicks(true);
245 G
.setSnapToTicks(false);
246 B
.setPaintTicks(true);
247 B
.setSnapToTicks(false);
249 R
.setLabelTable(R
.createStandardLabels(85));
250 R
.setPaintLabels(true);
251 G
.setLabelTable(G
.createStandardLabels(85));
252 G
.setPaintLabels(true);
253 B
.setLabelTable(B
.createStandardLabels(85));
254 B
.setPaintLabels(true);
256 R
.setMajorTickSpacing(85);
257 G
.setMajorTickSpacing(85);
258 B
.setMajorTickSpacing(85);
260 R
.setMinorTickSpacing(17);
261 G
.setMinorTickSpacing(17);
262 B
.setMinorTickSpacing(17);
264 RSpinner
= new JSpinner(new SpinnerNumberModel(R
.getValue(),
267 GSpinner
= new JSpinner(new SpinnerNumberModel(G
.getValue(),
270 BSpinner
= new JSpinner(new SpinnerNumberModel(B
.getValue(),
274 RLabel
.setLabelFor(R
);
275 GLabel
.setLabelFor(G
);
276 BLabel
.setLabelFor(B
);
278 GridBagConstraints bag
= new GridBagConstraints();
279 bag
.fill
= GridBagConstraints
.VERTICAL
;
315 * This method uninstalls the chooser panel from the JColorChooser.
317 * @param chooser The JColorChooser to remove this chooser panel from.
319 public void uninstallChooserPanel(JColorChooser chooser
)
321 uninstallListeners();
332 super.uninstallChooserPanel(chooser
);
336 * This method uninstalls any listeners that were added by the chooser
339 private void uninstallListeners()
341 R
.removeChangeListener(colorChanger
);
342 G
.removeChangeListener(colorChanger
);
343 B
.removeChangeListener(colorChanger
);
347 RSpinner
.removeChangeListener(spinnerHandler
);
348 GSpinner
.removeChangeListener(spinnerHandler
);
349 BSpinner
.removeChangeListener(spinnerHandler
);
351 spinnerHandler
= null;
355 * This method installs any listeners that the chooser panel needs to
358 private void installListeners()
360 colorChanger
= new SliderHandler();
362 R
.addChangeListener(colorChanger
);
363 G
.addChangeListener(colorChanger
);
364 B
.addChangeListener(colorChanger
);
366 spinnerHandler
= new SpinnerHandler();
368 RSpinner
.addChangeListener(spinnerHandler
);
369 GSpinner
.addChangeListener(spinnerHandler
);
370 BSpinner
.addChangeListener(spinnerHandler
);
374 * This method returns the small display icon.
376 * @return The small display icon.
378 public Icon
getSmallDisplayIcon()
384 * This method returns the large display icon.
386 * @return The large display icon.
388 public Icon
getLargeDisplayIcon()
394 * This method paints the default RGB chooser panel.
396 * @param g The Graphics object to paint with.
398 public void paint(Graphics g
)