KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > colorchooser > DefaultRGBChooserPanel


1 /*
2  * @(#)DefaultRGBChooserPanel.java 1.31 03/01/23
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.colorchooser;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import java.awt.*;
13
14 /**
15  * The standard RGB chooser.
16  * <p>
17  * <strong>Warning:</strong>
18  * Serialized objects of this class will not be compatible with
19  * future Swing releases. The current serialization support is
20  * appropriate for short term storage or RMI between applications running
21  * the same version of Swing. As of 1.4, support for long term storage
22  * of all JavaBeans<sup><font size="-2">TM</font></sup>
23  * has been added to the <code>java.beans</code> package.
24  * Please see {@link java.beans.XMLEncoder}.
25  *
26  * @version 1.31 01/23/03
27  * @author Steve Wilson
28  * @author Mark Davidson
29  * @see JColorChooser
30  * @see AbstractColorChooserPanel
31  */

32 class DefaultRGBChooserPanel extends AbstractColorChooserPanel JavaDoc implements ChangeListener {
33
34     protected JSlider redSlider;
35     protected JSlider greenSlider;
36     protected JSlider blueSlider;
37     protected JSpinner redField;
38     protected JSpinner blueField;
39     protected JSpinner greenField;
40
41     private final int minValue = 0;
42     private final int maxValue = 255;
43
44     private boolean isAdjusting = false; // indicates the fields are being set internally
45

46     public DefaultRGBChooserPanel() {
47         super();
48     }
49
50     /**
51      * Sets the values of the controls to reflect the color
52      */

53     private void setColor( Color newColor ) {
54         int red = newColor.getRed();
55         int blue = newColor.getBlue();
56         int green = newColor.getGreen();
57
58         if (redSlider.getValue() != red) {
59             redSlider.setValue(red);
60         }
61         if (greenSlider.getValue() != green) {
62             greenSlider.setValue(green);
63         }
64         if (blueSlider.getValue() != blue) {
65             blueSlider.setValue(blue);
66         }
67
68         if (((Integer JavaDoc)redField.getValue()).intValue() != red)
69             redField.setValue(new Integer JavaDoc(red));
70         if (((Integer JavaDoc)greenField.getValue()).intValue() != green)
71             greenField.setValue(new Integer JavaDoc(green));
72         if (((Integer JavaDoc)blueField.getValue()).intValue() != blue )
73             blueField.setValue(new Integer JavaDoc(blue));
74     }
75     
76     public String JavaDoc getDisplayName() {
77         return UIManager.getString("ColorChooser.rgbNameText");
78     }
79
80     /**
81      * Provides a hint to the look and feel as to the
82      * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
83      * access the panel. A return value <= 0 indicates there is no mnemonic.
84      * <p>
85      * The return value here is a hint, it is ultimately up to the look
86      * and feel to honor the return value in some meaningful way.
87      * <p>
88      * This implementation looks up the value from the default
89      * <code>ColorChooser.rgbMnemonic</code>, or if it
90      * isn't available (or not an <code>Integer</code>) returns -1.
91      * The lookup for the default is done through the <code>UIManager</code>:
92      * <code>UIManager.get("ColorChooser.rgbMnemonic");</code>.
93      *
94      * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
95      * mnemonic
96      * @see #getDisplayedMnemonicIndex
97      * @since 1.4
98      */

99     public int getMnemonic() {
100         return getInt("ColorChooser.rgbMnemonic", -1);
101     }
102
103     /**
104      * Provides a hint to the look and feel as to the index of the character in
105      * <code>getDisplayName</code> that should be visually identified as the
106      * mnemonic. The look and feel should only use this if
107      * <code>getMnemonic</code> returns a value > 0.
108      * <p>
109      * The return value here is a hint, it is ultimately up to the look
110      * and feel to honor the return value in some meaningful way. For example,
111      * a look and feel may wish to render each
112      * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
113      * and further use this return value to underline a character in
114      * the <code>getDisplayName</code>.
115      * <p>
116      * This implementation looks up the value from the default
117      * <code>ColorChooser.rgbDisplayedMnemonicIndex</code>, or if it
118      * isn't available (or not an <code>Integer</code>) returns -1.
119      * The lookup for the default is done through the <code>UIManager</code>:
120      * <code>UIManager.get("ColorChooser.rgbDisplayedMnemonicIndex");</code>.
121      *
122      * @return Character index to render mnemonic for; -1 to provide no
123      * visual identifier for this panel.
124      * @see #getMnemonic
125      * @since 1.4
126      */

127     public int getDisplayedMnemonicIndex() {
128         return getInt("ColorChooser.rgbDisplayedMnemonicIndex", -1);
129     }
130
131     public Icon getSmallDisplayIcon() {
132         return null;
133     }
134
135     public Icon getLargeDisplayIcon() {
136         return null;
137     }
138        
139     /**
140      * The background color, foreground color, and font are already set to the
141      * defaults from the defaults table before this method is called.
142      */

143     public void installChooserPanel(JColorChooser enclosingChooser) {
144         super.installChooserPanel(enclosingChooser);
145     }
146
147     protected void buildChooser() {
148       
149         String JavaDoc redString = UIManager.getString("ColorChooser.rgbRedText");
150         String JavaDoc greenString = UIManager.getString("ColorChooser.rgbGreenText");
151         String JavaDoc blueString = UIManager.getString("ColorChooser.rgbBlueText");
152
153         setLayout( new BorderLayout() );
154         Color color = getColorFromModel();
155
156
157         JPanel enclosure = new JPanel();
158         enclosure.setLayout( new SmartGridLayout JavaDoc( 3, 3 ) );
159
160         // The panel that holds the sliders
161

162         add( enclosure, BorderLayout.CENTER );
163         // sliderPanel.setBorder(new LineBorder(Color.black));
164

165         // The row for the red value
166
JLabel l = new JLabel(redString);
167         l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbRedMnemonic", -1));
168         enclosure.add(l);
169         redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getRed());
170         redSlider.setMajorTickSpacing( 85 );
171         redSlider.setMinorTickSpacing( 17 );
172         redSlider.setPaintTicks( true );
173         redSlider.setPaintLabels( true );
174         enclosure.add( redSlider );
175         redField = new JSpinner(
176             new SpinnerNumberModel(color.getRed(), minValue, maxValue, 1));
177         l.setLabelFor(redSlider);
178         JPanel redFieldHolder = new JPanel(new CenterLayout JavaDoc());
179         redField.addChangeListener(this);
180         redFieldHolder.add(redField);
181         enclosure.add(redFieldHolder);
182
183
184         // The row for the green value
185
l = new JLabel(greenString);
186         l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbGreenMnemonic", -1));
187         enclosure.add(l);
188         greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getGreen());
189         greenSlider.setMajorTickSpacing( 85 );
190         greenSlider.setMinorTickSpacing( 17 );
191         greenSlider.setPaintTicks( true );
192         greenSlider.setPaintLabels( true );
193         enclosure.add(greenSlider);
194         greenField = new JSpinner(
195             new SpinnerNumberModel(color.getGreen(), minValue, maxValue, 1));
196         l.setLabelFor(greenSlider);
197         JPanel greenFieldHolder = new JPanel(new CenterLayout JavaDoc());
198         greenFieldHolder.add(greenField);
199         greenField.addChangeListener(this);
200         enclosure.add(greenFieldHolder);
201
202         // The slider for the blue value
203
l = new JLabel(blueString);
204         l.setDisplayedMnemonic(AbstractColorChooserPanel.getInt("ColorChooser.rgbBlueMnemonic", -1));
205         enclosure.add(l);
206         blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, color.getBlue());
207         blueSlider.setMajorTickSpacing( 85 );
208         blueSlider.setMinorTickSpacing( 17 );
209         blueSlider.setPaintTicks( true );
210         blueSlider.setPaintLabels( true );
211         enclosure.add(blueSlider);
212         blueField = new JSpinner(
213             new SpinnerNumberModel(color.getBlue(), minValue, maxValue, 1));
214         l.setLabelFor(blueSlider);
215         JPanel blueFieldHolder = new JPanel(new CenterLayout JavaDoc());
216         blueFieldHolder.add(blueField);
217         blueField.addChangeListener(this);
218         enclosure.add(blueFieldHolder);
219
220         redSlider.addChangeListener( this );
221         greenSlider.addChangeListener( this );
222         blueSlider.addChangeListener( this );
223         
224         redSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
225         greenSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
226         blueSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
227     }
228
229     public void uninstallChooserPanel(JColorChooser enclosingChooser) {
230         super.uninstallChooserPanel(enclosingChooser);
231         removeAll();
232     }
233
234     public void updateChooser() {
235         if (!isAdjusting) {
236             isAdjusting = true;
237
238             setColor(getColorFromModel());
239
240             isAdjusting = false;
241         }
242     }
243
244     public void stateChanged( ChangeEvent e ) {
245         if ( e.getSource() instanceof JSlider && !isAdjusting) {
246
247             int red = redSlider.getValue();
248             int green = greenSlider.getValue();
249             int blue = blueSlider.getValue() ;
250             Color color = new Color (red, green, blue);
251     
252             getColorSelectionModel().setSelectedColor(color);
253         } else if (e.getSource() instanceof JSpinner && !isAdjusting) {
254
255             int red = ((Integer JavaDoc)redField.getValue()).intValue();
256             int green = ((Integer JavaDoc)greenField.getValue()).intValue();
257             int blue = ((Integer JavaDoc)blueField.getValue()).intValue();
258             Color color = new Color (red, green, blue);
259             
260             getColorSelectionModel().setSelectedColor(color);
261         }
262     }
263
264 }
265
266
267
Popular Tags