OSDN Git Service

missing added files from merge
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / colorchooser / DefaultRGBChooserPanel.java
1 /* DefaultRGHChooserPanel.java --
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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
19 02111-1307 USA.
20
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.
25
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. */
37
38 package javax.swing.colorchooser;
39
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;
56
57
58 /**
59  * This is the default RGB panel for the JColorChooser. The color is selected
60  * using three sliders that represent the RGB values.
61  */
62 public class DefaultRGBChooserPanel extends AbstractColorChooserPanel
63 {
64   /**
65    * This class handles the slider value changes for all three sliders.
66    */
67   class SliderHandler implements ChangeListener
68   {
69     /**
70      * This method is called whenever any of the slider values change.
71      *
72      * @param e The ChangeEvent.
73      */
74     public void stateChanged(ChangeEvent e)
75     {
76       if (internalChange)
77         return;
78       int color = R.getValue() << 16 | G.getValue() << 8 | B.getValue();
79
80       getColorSelectionModel().setSelectedColor(new Color(color));
81     }
82   }
83
84   /**
85    * This class handles the Spinner values changing.
86    */
87   class SpinnerHandler implements ChangeListener
88   {
89     /**
90      * This method is called whenever any of the JSpinners change values.
91      *
92      * @param e The ChangeEvent.
93      */
94     public void stateChanged(ChangeEvent e)
95     {
96       if (internalChange)
97         return;
98       int red = ((Number) RSpinner.getValue()).intValue();
99       int green = ((Number) GSpinner.getValue()).intValue();
100       int blue = ((Number) BSpinner.getValue()).intValue();
101
102       int color = red << 16 | green << 8 | blue;
103
104       getColorSelectionModel().setSelectedColor(new Color(color));
105     }
106   }
107
108   /**
109    * Whether the color change was initiated from the slider or spinner rather
110    * than externally.
111    */
112   private transient boolean internalChange = false;
113
114   /** The ChangeListener for the sliders. */
115   private transient ChangeListener colorChanger;
116
117   /** The ChangeListener for the spinners. */
118   private transient ChangeListener spinnerHandler;
119
120   /** The slider that handles the red values. */
121   private transient JSlider R;
122
123   /** The slider that handles the green values. */
124   private transient JSlider G;
125
126   /** The slider that handles the blue values. */
127   private transient JSlider B;
128
129   /** The label for the red slider. */
130   private transient JLabel RLabel;
131
132   /** The label for the green slider. */
133   private transient JLabel GLabel;
134
135   /** The label for the blue slider. */
136   private transient JLabel BLabel;
137
138   /** The spinner that handles the red values. */
139   private transient JSpinner RSpinner;
140
141   /** The spinner that handles the green values. */
142   private transient JSpinner GSpinner;
143
144   /** The spinner that handles the blue values. */
145   private transient JSpinner BSpinner;
146
147   /**
148    * Creates a new DefaultRGBChooserPanel object.
149    */
150   public DefaultRGBChooserPanel()
151   {
152     super();
153   }
154
155   /**
156    * This method returns the name displayed in the JTabbedPane.
157    *
158    * @return The name displayed in the JTabbedPane.
159    */
160   public String getDisplayName()
161   {
162     return "RGB";
163   }
164
165   /**
166    * This method updates the chooser panel with the new color chosen in the
167    * JColorChooser.
168    */
169   public void updateChooser()
170   {
171     Color c = getColorFromModel();
172     int rgb = c.getRGB();
173
174     int red = rgb >> 16 & 0xff;
175     int green = rgb >> 8 & 0xff;
176     int blue = rgb & 0xff;
177
178     internalChange = true;
179
180     if (R != null)
181       R.setValue(red);
182     if (RSpinner != null)
183       RSpinner.setValue(new Integer(red));
184     if (G != null)
185       G.setValue(green);
186     if (GSpinner != null)
187       GSpinner.setValue(new Integer(green));
188     if (B != null)
189       B.setValue(blue);
190     if (BSpinner != null)
191       BSpinner.setValue(new Integer(blue));
192
193     internalChange = false;
194
195     revalidate();
196     repaint();
197   }
198
199   /**
200    * This method builds the chooser panel.
201    */
202   protected void buildChooser()
203   {
204     setLayout(new GridBagLayout());
205
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');
212
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);
216
217     R.setPaintTicks(true);
218     R.setSnapToTicks(false);
219     G.setPaintTicks(true);
220     G.setSnapToTicks(false);
221     B.setPaintTicks(true);
222     B.setSnapToTicks(false);
223
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);
230
231     R.setMajorTickSpacing(85);
232     G.setMajorTickSpacing(85);
233     B.setMajorTickSpacing(85);
234
235     R.setMinorTickSpacing(17);
236     G.setMinorTickSpacing(17);
237     B.setMinorTickSpacing(17);
238
239     RSpinner = new JSpinner(new SpinnerNumberModel(R.getValue(),
240                                                    R.getMinimum(),
241                                                    R.getMaximum(), 1));
242     GSpinner = new JSpinner(new SpinnerNumberModel(G.getValue(),
243                                                    G.getMinimum(),
244                                                    G.getMaximum(), 1));
245     BSpinner = new JSpinner(new SpinnerNumberModel(B.getValue(),
246                                                    B.getMinimum(),
247                                                    B.getMaximum(), 1));
248
249     RLabel.setLabelFor(R);
250     GLabel.setLabelFor(G);
251     BLabel.setLabelFor(B);
252
253     GridBagConstraints bag = new GridBagConstraints();
254     bag.fill = GridBagConstraints.VERTICAL;
255
256     bag.gridx = 0;
257     bag.gridy = 0;
258     add(RLabel, bag);
259
260     bag.gridx = 1;
261     add(R, bag);
262
263     bag.gridx = 2;
264     add(RSpinner, bag);
265
266     bag.gridx = 0;
267     bag.gridy = 1;
268     add(GLabel, bag);
269
270     bag.gridx = 1;
271     add(G, bag);
272
273     bag.gridx = 2;
274     add(GSpinner, bag);
275
276     bag.gridx = 0;
277     bag.gridy = 2;
278     add(BLabel, bag);
279
280     bag.gridx = 1;
281     add(B, bag);
282
283     bag.gridx = 2;
284     add(BSpinner, bag);
285
286     installListeners();
287   }
288
289   /**
290    * This method uninstalls the chooser panel from the JColorChooser.
291    *
292    * @param chooser The JColorChooser to remove this chooser panel from.
293    */
294   public void uninstallChooserPanel(JColorChooser chooser)
295   {
296     uninstallListeners();
297     removeAll();
298
299     R = null;
300     G = null;
301     B = null;
302
303     RSpinner = null;
304     GSpinner = null;
305     BSpinner = null;
306
307     super.uninstallChooserPanel(chooser);
308   }
309
310   /**
311    * This method uninstalls any listeners that were added by the chooser
312    * panel.
313    */
314   private void uninstallListeners()
315   {
316     R.removeChangeListener(colorChanger);
317     G.removeChangeListener(colorChanger);
318     B.removeChangeListener(colorChanger);
319
320     colorChanger = null;
321
322     RSpinner.removeChangeListener(spinnerHandler);
323     GSpinner.removeChangeListener(spinnerHandler);
324     BSpinner.removeChangeListener(spinnerHandler);
325
326     spinnerHandler = null;
327   }
328
329   /**
330    * This method installs any listeners that the chooser panel needs to
331    * operate.
332    */
333   private void installListeners()
334   {
335     colorChanger = new SliderHandler();
336
337     R.addChangeListener(colorChanger);
338     G.addChangeListener(colorChanger);
339     B.addChangeListener(colorChanger);
340
341     spinnerHandler = new SpinnerHandler();
342
343     RSpinner.addChangeListener(spinnerHandler);
344     GSpinner.addChangeListener(spinnerHandler);
345     BSpinner.addChangeListener(spinnerHandler);
346   }
347
348   /**
349    * This method returns the small display icon.
350    *
351    * @return The small display icon.
352    */
353   public Icon getSmallDisplayIcon()
354   {
355     return null;
356   }
357
358   /**
359    * This method returns the large display icon.
360    *
361    * @return The large display icon.
362    */
363   public Icon getLargeDisplayIcon()
364   {
365     return null;
366   }
367
368   /**
369    * This method paints the default RGB chooser panel.
370    *
371    * @param g The Graphics object to paint with.
372    */
373   public void paint(Graphics g)
374   {
375     super.paint(g);
376   }
377 }