1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util.swing;
23 
24 import java.awt.*;
25 import javax.swing.*;
26 import java.awt.event.ActionListener;
27 import java.awt.event.ActionEvent;
28 import javax.swing.event.ChangeListener;
29 import javax.swing.event.ChangeEvent;
30 
31 
32 /**
33  * A component which allows the user to select a color.
34  */
35 
36 public class ColorChooser extends JComponent implements Mnemonicable{
37 
38 
39 
40   /**
41    * The button we're using.
42    */
43 
44   private final JButton button;
45 
46 
47 
48   /**
49    * The label we're using.
50    */
51 
52   private final JLabel label;
53 
54 
55 
56   /**
57    * The icon's size.
58    */
59 
60   private static final Dimension ICON_SIZE = new Dimension(25, 10);
61 
62 
63 
64   /**
65    * The color of the icon's border.
66    */
67 
68   private static final Color ICON_BORDER_COLOR = Color.GRAY;
69 
70 
71 
72   /**
73    * The current color.
74    */
75 
76   private Color color;
77 
78 
79 
80   /**
81    * The sole ChangeEvent we need.
82    */
83 
84   private final ChangeEvent changeEvent = new ChangeEvent(this);
85 
86 
87 
88   /**
89    * Creates a new <code>ColorChooser</code> with no text and initial color of black.
90    */
91 
ColorChooser()92   public ColorChooser(){
93     this(null, Color.black);
94   }
95 
96 
97 
98   /**
99    * Creates a new <code>ColorChooser</code> with the specified text and the
100    * initial color of black.
101    */
102 
ColorChooser(String text)103   public ColorChooser(String text){
104     this(text, Color.black);
105   }
106 
107 
108 
109   /**
110    * Creates a new <code>ColorChooser</code> with no text and the given initial
111    * color.
112    */
113 
ColorChooser(Color initialColor)114   public ColorChooser(Color initialColor){
115     this(null, initialColor);
116   }
117 
118 
119 
120   /**
121    * Creates a new <code>ColorChooser</code> with the given text and initial
122    * color.
123    */
124 
ColorChooser(String text, Color initialColor)125   public ColorChooser(String text, Color initialColor){
126     label = new JLabel();
127     button = new JButton();
128 
129     setText(text);
130     setColor(initialColor);
131 
132     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
133     add(label);
134     add(Box.createHorizontalStrut(20));
135     add(Box.createHorizontalGlue());
136     add(button);
137 
138     label.setLabelFor(button);
139     button.setDefaultCapable(false);
140 
141     if (SwingUtils.isMacLnF())
142       button.setMargin(new Insets(5, 5, 5, 5));
143     else
144       button.setMargin(new Insets(3, 3, 3, 3));
145 
146 
147     color = initialColor;
148 
149     button.addActionListener(new ActionListener(){
150       public void actionPerformed(ActionEvent evt){
151         Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(ColorChooser.this),
152             LocalizationService.getForClass(ColorChooser.class).getString("dialogTitle"), color); //$NON-NLS-1$
153         if (newColor != null)
154           setColor(newColor);
155       }
156     });
157   }
158 
159 
160 
161   /**
162    * Sets the text of the label.
163    */
164 
setText(String text)165   public void setText(String text){
166     label.setText(text);
167   }
168 
169 
170 
171   /**
172    * Returns the text of the label.
173    */
174 
getText()175   public String getText(){
176     return label.getText();
177   }
178 
179 
180 
181   /**
182    * Sets the displayed mnemonic index.
183    */
184 
setDisplayedMnemonicIndex(int mnemonicIndex)185   public void setDisplayedMnemonicIndex(int mnemonicIndex){
186     label.setDisplayedMnemonicIndex(mnemonicIndex);
187   }
188 
189 
190 
191   /**
192    * Sets the enabled state of this color chooser.
193    */
194 
setEnabled(boolean enabled)195   public void setEnabled(boolean enabled){
196     label.setEnabled(enabled);
197     button.setEnabled(enabled);
198 
199     super.setEnabled(enabled);
200   }
201 
202 
203 
204 
205   /**
206    * Adds a ChangeListener to the list of listeners receiving notifications when
207    * one of the text properties changes.
208    */
209 
addChangeListener(ChangeListener listener)210   public void addChangeListener(ChangeListener listener){
211     listenerList.add(ChangeListener.class, listener);
212   }
213 
214 
215 
216   /**
217    * Removes the given Changelistener from the list of listeners receiving
218    * notifications when one of the text properties changes.
219    */
220 
removeChangeListener(ChangeListener listener)221   public void removeChangeListener(ChangeListener listener){
222     listenerList.remove(ChangeListener.class, listener);
223   }
224 
225 
226 
227   /**
228    * Fires a ChangeEvent to all interested listeners.
229    */
230 
fireStateChanged()231   protected void fireStateChanged(){
232     Object [] listeners = listenerList.getListenerList();
233     for (int i = 0; i < listeners.length; i += 2){
234       if (listeners[i] == ChangeListener.class){
235         ChangeListener listener = (ChangeListener)listeners[i+1];
236         listener.stateChanged(changeEvent);
237       }
238     }
239   }
240 
241 
242 
243   /**
244    * Returns the currently selected color.
245    */
246 
getColor()247   public Color getColor(){
248     return color;
249   }
250 
251 
252 
253   /**
254    * Sets the current color.
255    */
256 
setColor(Color color)257   public void setColor(Color color){
258     this.color = color;
259 
260     Icon colorIcon = new SolidColorRectangleIcon(ICON_SIZE, color);
261     button.setIcon(new BorderIcon(colorIcon, ICON_BORDER_COLOR));
262 
263     fireStateChanged();
264   }
265 
266 
267 
268 }
269