Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / colorchooser / DefaultRGBChooserPanel.java
blob39a1732502ddaab512cdc02bc839fd1b771b27d8
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
56 /**
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
62 /**
63 * This class handles the slider value changes for all three sliders.
65 class SliderHandler implements ChangeListener
67 /**
68 * This method is called whenever any of the slider values change.
70 * @param e The ChangeEvent.
72 public void stateChanged(ChangeEvent e)
74 if (updateChange)
75 return;
77 int color = R.getValue() << 16 | G.getValue() << 8 | B.getValue();
79 sliderChange = true;
80 getColorSelectionModel().setSelectedColor(new Color(color));
81 sliderChange = false;
85 /**
86 * This class handles the Spinner values changing.
88 class SpinnerHandler implements ChangeListener
90 /**
91 * This method is called whenever any of the JSpinners change values.
93 * @param e The ChangeEvent.
95 public void stateChanged(ChangeEvent e)
97 if (updateChange)
98 return;
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. */
135 transient JSlider R;
137 /** The slider that handles the green values.
138 * This is package-private to avoid an accessor method. */
139 transient JSlider G;
141 /** The slider that handles the blue values.
142 * This is package-private to avoid an accessor method. */
143 transient JSlider B;
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()
171 super();
175 * This method returns the name displayed in the JTabbedPane.
177 * @return The name displayed in the JTabbedPane.
179 public String getDisplayName()
181 return "RGB";
185 * This method updates the chooser panel with the new color chosen in the
186 * JColorChooser.
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;
197 updateChange = true;
199 if (! sliderChange)
201 if (R != null)
202 R.setValue(red);
203 if (G != null)
204 G.setValue(green);
205 if (B != null)
206 B.setValue(blue);
208 if (! spinnerChange)
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;
220 revalidate();
221 repaint();
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(),
265 R.getMinimum(),
266 R.getMaximum(), 1));
267 GSpinner = new JSpinner(new SpinnerNumberModel(G.getValue(),
268 G.getMinimum(),
269 G.getMaximum(), 1));
270 BSpinner = new JSpinner(new SpinnerNumberModel(B.getValue(),
271 B.getMinimum(),
272 B.getMaximum(), 1));
274 RLabel.setLabelFor(R);
275 GLabel.setLabelFor(G);
276 BLabel.setLabelFor(B);
278 GridBagConstraints bag = new GridBagConstraints();
279 bag.fill = GridBagConstraints.VERTICAL;
281 bag.gridx = 0;
282 bag.gridy = 0;
283 add(RLabel, bag);
285 bag.gridx = 1;
286 add(R, bag);
288 bag.gridx = 2;
289 add(RSpinner, bag);
291 bag.gridx = 0;
292 bag.gridy = 1;
293 add(GLabel, bag);
295 bag.gridx = 1;
296 add(G, bag);
298 bag.gridx = 2;
299 add(GSpinner, bag);
301 bag.gridx = 0;
302 bag.gridy = 2;
303 add(BLabel, bag);
305 bag.gridx = 1;
306 add(B, bag);
308 bag.gridx = 2;
309 add(BSpinner, bag);
311 installListeners();
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();
322 removeAll();
324 R = null;
325 G = null;
326 B = null;
328 RSpinner = null;
329 GSpinner = null;
330 BSpinner = null;
332 super.uninstallChooserPanel(chooser);
336 * This method uninstalls any listeners that were added by the chooser
337 * panel.
339 private void uninstallListeners()
341 R.removeChangeListener(colorChanger);
342 G.removeChangeListener(colorChanger);
343 B.removeChangeListener(colorChanger);
345 colorChanger = null;
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
356 * operate.
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()
380 return null;
384 * This method returns the large display icon.
386 * @return The large display icon.
388 public Icon getLargeDisplayIcon()
390 return null;
394 * This method paints the default RGB chooser panel.
396 * @param g The Graphics object to paint with.
398 public void paint(Graphics g)
400 super.paint(g);