1 /* DefaultRGHChooserPanel.java --
2 Copyright (C) 2004 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., 59 Temple Place, Suite 330, 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. */
38 package javax.swing.colorchooser;
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.FlowLayout;
43 import java.awt.Graphics;
44 import java.awt.GridBagConstraints;
45 import java.awt.GridBagLayout;
46 import javax.swing.Icon;
47 import javax.swing.JColorChooser;
48 import javax.swing.JLabel;
49 import javax.swing.JPanel;
50 import javax.swing.JSlider;
51 import javax.swing.JSpinner;
52 import javax.swing.SpinnerNumberModel;
53 import javax.swing.SwingConstants;
54 import javax.swing.event.ChangeEvent;
55 import javax.swing.event.ChangeListener;
59 * This is the default RGB panel for the JColorChooser. The color is selected
60 * using three sliders that represent the RGB values.
62 public class DefaultRGBChooserPanel extends AbstractColorChooserPanel
65 * This class handles the slider value changes for all three sliders.
67 class SliderHandler implements ChangeListener
70 * This method is called whenever any of the slider values change.
72 * @param e The ChangeEvent.
74 public void stateChanged(ChangeEvent e)
78 int color = R.getValue() << 16 | G.getValue() << 8 | B.getValue();
80 getColorSelectionModel().setSelectedColor(new Color(color));
85 * This class handles the Spinner values changing.
87 class SpinnerHandler implements ChangeListener
90 * This method is called whenever any of the JSpinners change values.
92 * @param e The ChangeEvent.
94 public void stateChanged(ChangeEvent e)
98 int red = ((Number) RSpinner.getValue()).intValue();
99 int green = ((Number) GSpinner.getValue()).intValue();
100 int blue = ((Number) BSpinner.getValue()).intValue();
102 int color = red << 16 | green << 8 | blue;
104 getColorSelectionModel().setSelectedColor(new Color(color));
109 * Whether the color change was initiated from the slider or spinner rather
112 private transient boolean internalChange = false;
114 /** The ChangeListener for the sliders. */
115 private transient ChangeListener colorChanger;
117 /** The ChangeListener for the spinners. */
118 private transient ChangeListener spinnerHandler;
120 /** The slider that handles the red values. */
121 private transient JSlider R;
123 /** The slider that handles the green values. */
124 private transient JSlider G;
126 /** The slider that handles the blue values. */
127 private transient JSlider B;
129 /** The label for the red slider. */
130 private transient JLabel RLabel;
132 /** The label for the green slider. */
133 private transient JLabel GLabel;
135 /** The label for the blue slider. */
136 private transient JLabel BLabel;
138 /** The spinner that handles the red values. */
139 private transient JSpinner RSpinner;
141 /** The spinner that handles the green values. */
142 private transient JSpinner GSpinner;
144 /** The spinner that handles the blue values. */
145 private transient JSpinner BSpinner;
148 * Creates a new DefaultRGBChooserPanel object.
150 public DefaultRGBChooserPanel()
156 * This method returns the name displayed in the JTabbedPane.
158 * @return The name displayed in the JTabbedPane.
160 public String getDisplayName()
166 * This method updates the chooser panel with the new color chosen in the
169 public void updateChooser()
171 Color c = getColorFromModel();
172 int rgb = c.getRGB();
174 int red = rgb >> 16 & 0xff;
175 int green = rgb >> 8 & 0xff;
176 int blue = rgb & 0xff;
178 internalChange = true;
182 if (RSpinner != null)
183 RSpinner.setValue(new Integer(red));
186 if (GSpinner != null)
187 GSpinner.setValue(new Integer(green));
190 if (BSpinner != null)
191 BSpinner.setValue(new Integer(blue));
193 internalChange = false;
200 * This method builds the chooser panel.
202 protected void buildChooser()
204 setLayout(new GridBagLayout());
206 RLabel = new JLabel("Red");
207 RLabel.setDisplayedMnemonic('d');
208 GLabel = new JLabel("Green");
209 GLabel.setDisplayedMnemonic('n');
210 BLabel = new JLabel("Blue");
211 BLabel.setDisplayedMnemonic('B');
213 R = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
214 G = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
215 B = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
217 R.setPaintTicks(true);
218 R.setSnapToTicks(false);
219 G.setPaintTicks(true);
220 G.setSnapToTicks(false);
221 B.setPaintTicks(true);
222 B.setSnapToTicks(false);
224 R.setLabelTable(R.createStandardLabels(85));
225 R.setPaintLabels(true);
226 G.setLabelTable(G.createStandardLabels(85));
227 G.setPaintLabels(true);
228 B.setLabelTable(B.createStandardLabels(85));
229 B.setPaintLabels(true);
231 R.setMajorTickSpacing(85);
232 G.setMajorTickSpacing(85);
233 B.setMajorTickSpacing(85);
235 R.setMinorTickSpacing(17);
236 G.setMinorTickSpacing(17);
237 B.setMinorTickSpacing(17);
239 RSpinner = new JSpinner(new SpinnerNumberModel(R.getValue(),
242 GSpinner = new JSpinner(new SpinnerNumberModel(G.getValue(),
245 BSpinner = new JSpinner(new SpinnerNumberModel(B.getValue(),
249 RLabel.setLabelFor(R);
250 GLabel.setLabelFor(G);
251 BLabel.setLabelFor(B);
253 GridBagConstraints bag = new GridBagConstraints();
254 bag.fill = GridBagConstraints.VERTICAL;
290 * This method uninstalls the chooser panel from the JColorChooser.
292 * @param chooser The JColorChooser to remove this chooser panel from.
294 public void uninstallChooserPanel(JColorChooser chooser)
296 uninstallListeners();
307 super.uninstallChooserPanel(chooser);
311 * This method uninstalls any listeners that were added by the chooser
314 private void uninstallListeners()
316 R.removeChangeListener(colorChanger);
317 G.removeChangeListener(colorChanger);
318 B.removeChangeListener(colorChanger);
322 RSpinner.removeChangeListener(spinnerHandler);
323 GSpinner.removeChangeListener(spinnerHandler);
324 BSpinner.removeChangeListener(spinnerHandler);
326 spinnerHandler = null;
330 * This method installs any listeners that the chooser panel needs to
333 private void installListeners()
335 colorChanger = new SliderHandler();
337 R.addChangeListener(colorChanger);
338 G.addChangeListener(colorChanger);
339 B.addChangeListener(colorChanger);
341 spinnerHandler = new SpinnerHandler();
343 RSpinner.addChangeListener(spinnerHandler);
344 GSpinner.addChangeListener(spinnerHandler);
345 BSpinner.addChangeListener(spinnerHandler);
349 * This method returns the small display icon.
351 * @return The small display icon.
353 public Icon getSmallDisplayIcon()
359 * This method returns the large display icon.
361 * @return The large display icon.
363 public Icon getLargeDisplayIcon()
369 * This method paints the default RGB chooser panel.
371 * @param g The Graphics object to paint with.
373 public void paint(Graphics g)