1 |
772 |
jeremybenn |
/* BasicColorChooserUI.java --
|
2 |
|
|
Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 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 |
|
|
|
39 |
|
|
package javax.swing.plaf.basic;
|
40 |
|
|
|
41 |
|
|
import java.awt.BorderLayout;
|
42 |
|
|
import java.awt.Container;
|
43 |
|
|
import java.beans.PropertyChangeEvent;
|
44 |
|
|
import java.beans.PropertyChangeListener;
|
45 |
|
|
|
46 |
|
|
import javax.swing.JColorChooser;
|
47 |
|
|
import javax.swing.JComponent;
|
48 |
|
|
import javax.swing.JPanel;
|
49 |
|
|
import javax.swing.JTabbedPane;
|
50 |
|
|
import javax.swing.LookAndFeel;
|
51 |
|
|
import javax.swing.colorchooser.AbstractColorChooserPanel;
|
52 |
|
|
import javax.swing.colorchooser.ColorChooserComponentFactory;
|
53 |
|
|
import javax.swing.event.ChangeEvent;
|
54 |
|
|
import javax.swing.event.ChangeListener;
|
55 |
|
|
import javax.swing.plaf.ColorChooserUI;
|
56 |
|
|
import javax.swing.plaf.ComponentUI;
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* This is the UI Class for the JColorChooser in the Basic Look and Feel.
|
60 |
|
|
*/
|
61 |
|
|
public class BasicColorChooserUI extends ColorChooserUI
|
62 |
|
|
{
|
63 |
|
|
/**
|
64 |
|
|
* This helper class handles property changes from the JColorChooser.
|
65 |
|
|
*/
|
66 |
|
|
public class PropertyHandler implements PropertyChangeListener
|
67 |
|
|
{
|
68 |
|
|
/**
|
69 |
|
|
* This method is called when any of the properties of the JColorChooser
|
70 |
|
|
* change.
|
71 |
|
|
*
|
72 |
|
|
* @param e The PropertyChangeEvent.
|
73 |
|
|
*/
|
74 |
|
|
public void propertyChange(PropertyChangeEvent e)
|
75 |
|
|
{
|
76 |
|
|
if (e.getPropertyName() == JColorChooser.CHOOSER_PANELS_PROPERTY)
|
77 |
|
|
makeTabs(chooser.getChooserPanels());
|
78 |
|
|
else if (e.getPropertyName() == JColorChooser.PREVIEW_PANEL_PROPERTY)
|
79 |
|
|
updatePreviewPanel(chooser.getPreviewPanel());
|
80 |
|
|
else if (e.getPropertyName() == JColorChooser.SELECTION_MODEL_PROPERTY)
|
81 |
|
|
((AbstractColorChooserPanel) pane.getSelectedComponent())
|
82 |
|
|
.updateChooser();
|
83 |
|
|
|
84 |
|
|
chooser.repaint();
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
|
|
* This is a helper class that listens to the Model of the JColorChooser for
|
90 |
|
|
* color change events so it can update the preview panel.
|
91 |
|
|
*/
|
92 |
|
|
private class PreviewListener implements ChangeListener
|
93 |
|
|
{
|
94 |
|
|
/**
|
95 |
|
|
* This method is called whenever the JColorChooser's color changes.
|
96 |
|
|
*
|
97 |
|
|
* @param e The ChangeEvent.
|
98 |
|
|
*/
|
99 |
|
|
public void stateChanged(ChangeEvent e)
|
100 |
|
|
{
|
101 |
|
|
if (pane != null)
|
102 |
|
|
{
|
103 |
|
|
AbstractColorChooserPanel panel = (AbstractColorChooserPanel) pane
|
104 |
|
|
.getSelectedComponent();
|
105 |
|
|
if (panel != null)
|
106 |
|
|
panel.updateChooser();
|
107 |
|
|
}
|
108 |
|
|
chooser.repaint();
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* This helper class listens to the JTabbedPane that is used for tab
|
114 |
|
|
* changes.
|
115 |
|
|
*/
|
116 |
|
|
private class TabPaneListener implements ChangeListener
|
117 |
|
|
{
|
118 |
|
|
/**
|
119 |
|
|
* This method is called whenever a different tab is selected in the
|
120 |
|
|
* JTabbedPane.
|
121 |
|
|
*
|
122 |
|
|
* @param e The ChangeEvent.
|
123 |
|
|
*/
|
124 |
|
|
public void stateChanged(ChangeEvent e)
|
125 |
|
|
{
|
126 |
|
|
// Need to do this because we don't update all the tabs when they're not
|
127 |
|
|
// visible, so they are not informed of new colors when they're hidden.
|
128 |
|
|
AbstractColorChooserPanel comp = (AbstractColorChooserPanel) pane
|
129 |
|
|
.getSelectedComponent();
|
130 |
|
|
comp.updateChooser();
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/** An array of default choosers to use in the JColorChooser. */
|
135 |
|
|
protected AbstractColorChooserPanel[] defaultChoosers;
|
136 |
|
|
|
137 |
|
|
/** The listener for the preview panel. */
|
138 |
|
|
protected ChangeListener previewListener;
|
139 |
|
|
|
140 |
|
|
/** The PropertyChangeListener for the JColorChooser. */
|
141 |
|
|
protected PropertyChangeListener propertyChangeListener;
|
142 |
|
|
|
143 |
|
|
/**
|
144 |
|
|
* The JColorChooser this is installed on.
|
145 |
|
|
*/
|
146 |
|
|
protected JColorChooser chooser;
|
147 |
|
|
|
148 |
|
|
/** The JTabbedPane that is used. */
|
149 |
|
|
JTabbedPane pane;
|
150 |
|
|
|
151 |
|
|
/** The Container that holds the preview panel. */
|
152 |
|
|
private Container prevContainer;
|
153 |
|
|
|
154 |
|
|
/**
|
155 |
|
|
* Creates a new BasicColorChooserUI object.
|
156 |
|
|
*/
|
157 |
|
|
public BasicColorChooserUI()
|
158 |
|
|
{
|
159 |
|
|
super();
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* This method creates a new UI Component for the given JComponent.
|
164 |
|
|
*
|
165 |
|
|
* @param c The JComponent to create an UI for.
|
166 |
|
|
*
|
167 |
|
|
* @return A new BasicColorChooserUI.
|
168 |
|
|
*/
|
169 |
|
|
public static ComponentUI createUI(JComponent c)
|
170 |
|
|
{
|
171 |
|
|
return new BasicColorChooserUI();
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
/**
|
175 |
|
|
* This method creates the default chooser panels for the JColorChooser.
|
176 |
|
|
*
|
177 |
|
|
* @return The default chooser panels.
|
178 |
|
|
*/
|
179 |
|
|
protected AbstractColorChooserPanel[] createDefaultChoosers()
|
180 |
|
|
{
|
181 |
|
|
return ColorChooserComponentFactory.getDefaultChooserPanels();
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* This method installs the UI Component for the given JComponent.
|
186 |
|
|
*
|
187 |
|
|
* @param c The JComponent to install this UI for.
|
188 |
|
|
*/
|
189 |
|
|
public void installUI(JComponent c)
|
190 |
|
|
{
|
191 |
|
|
if (c instanceof JColorChooser)
|
192 |
|
|
{
|
193 |
|
|
chooser = (JColorChooser) c;
|
194 |
|
|
chooser.setLayout(new BorderLayout());
|
195 |
|
|
|
196 |
|
|
// Do this first, so we avoid doing work for property change events.
|
197 |
|
|
defaultChoosers = createDefaultChoosers();
|
198 |
|
|
chooser.setChooserPanels(defaultChoosers);
|
199 |
|
|
pane = new JTabbedPane();
|
200 |
|
|
|
201 |
|
|
pane.addChangeListener(new ChangeListener()
|
202 |
|
|
{
|
203 |
|
|
public void stateChanged(ChangeEvent e)
|
204 |
|
|
{
|
205 |
|
|
pane.repaint();
|
206 |
|
|
}
|
207 |
|
|
});
|
208 |
|
|
|
209 |
|
|
makeTabs(defaultChoosers);
|
210 |
|
|
|
211 |
|
|
chooser.add(pane, BorderLayout.NORTH);
|
212 |
|
|
|
213 |
|
|
installPreviewPanel();
|
214 |
|
|
|
215 |
|
|
installDefaults();
|
216 |
|
|
installListeners();
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/**
|
221 |
|
|
* This method adds tabs to the JTabbedPane for the chooserPanels defined in
|
222 |
|
|
* the JColorChooser.
|
223 |
|
|
* This is package-private to avoid an accessor method.
|
224 |
|
|
*
|
225 |
|
|
* @param panels The Panels that need tabs to be made for them.
|
226 |
|
|
*/
|
227 |
|
|
void makeTabs(AbstractColorChooserPanel[] panels)
|
228 |
|
|
{
|
229 |
|
|
pane.removeAll();
|
230 |
|
|
for (int i = 0; i < panels.length; i++)
|
231 |
|
|
pane.addTab(panels[i].getDisplayName(), panels[i].getSmallDisplayIcon(),
|
232 |
|
|
panels[i]);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/**
|
236 |
|
|
* This method uninstalls this UI for the given JComponent.
|
237 |
|
|
*
|
238 |
|
|
* @param c The JComponent that will have this UI removed.
|
239 |
|
|
*/
|
240 |
|
|
public void uninstallUI(JComponent c)
|
241 |
|
|
{
|
242 |
|
|
uninstallListeners();
|
243 |
|
|
uninstallDefaults();
|
244 |
|
|
uninstallDefaultChoosers();
|
245 |
|
|
|
246 |
|
|
pane = null;
|
247 |
|
|
chooser = null;
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* Uninstalls the default color choosers that have been installed by this UI.
|
252 |
|
|
*/
|
253 |
|
|
protected void uninstallDefaultChoosers()
|
254 |
|
|
{
|
255 |
|
|
defaultChoosers = null;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
/**
|
259 |
|
|
* This method installs the preview panel for the JColorChooser.
|
260 |
|
|
*/
|
261 |
|
|
protected void installPreviewPanel()
|
262 |
|
|
{
|
263 |
|
|
updatePreviewPanel(ColorChooserComponentFactory.getPreviewPanel());
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* This is a helper method that swaps the existing preview panel with the
|
268 |
|
|
* given panel.
|
269 |
|
|
* This is package-private to avoid an accessor method.
|
270 |
|
|
*
|
271 |
|
|
* @param preview The new preview panel.
|
272 |
|
|
*/
|
273 |
|
|
void updatePreviewPanel(JComponent preview)
|
274 |
|
|
{
|
275 |
|
|
if (prevContainer == null)
|
276 |
|
|
{
|
277 |
|
|
prevContainer = new JPanel();
|
278 |
|
|
prevContainer.setLayout(new BorderLayout());
|
279 |
|
|
chooser.add(prevContainer, BorderLayout.CENTER);
|
280 |
|
|
}
|
281 |
|
|
prevContainer.removeAll();
|
282 |
|
|
prevContainer.add(preview, BorderLayout.CENTER);
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/**
|
286 |
|
|
* This method installs the default properties given by the Basic Look and
|
287 |
|
|
* Feel.
|
288 |
|
|
*/
|
289 |
|
|
protected void installDefaults()
|
290 |
|
|
{
|
291 |
|
|
LookAndFeel.installColorsAndFont(chooser, "ColorChooser.background",
|
292 |
|
|
"ColorChooser.foreground",
|
293 |
|
|
"ColorChooser.font");
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
/**
|
297 |
|
|
* This method uninstalls the default properties given by the Basic Look and
|
298 |
|
|
* Feel.
|
299 |
|
|
*/
|
300 |
|
|
protected void uninstallDefaults()
|
301 |
|
|
{
|
302 |
|
|
chooser.setBackground(null);
|
303 |
|
|
chooser.setForeground(null);
|
304 |
|
|
chooser.setFont(null);
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
/**
|
308 |
|
|
* This method installs any listeners required for this UI to function.
|
309 |
|
|
*/
|
310 |
|
|
protected void installListeners()
|
311 |
|
|
{
|
312 |
|
|
propertyChangeListener = createPropertyChangeListener();
|
313 |
|
|
previewListener = new PreviewListener();
|
314 |
|
|
|
315 |
|
|
chooser.addPropertyChangeListener(propertyChangeListener);
|
316 |
|
|
chooser.getSelectionModel().addChangeListener(previewListener);
|
317 |
|
|
|
318 |
|
|
pane.addChangeListener(new TabPaneListener());
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/**
|
322 |
|
|
* This method creates the PropertyChangeListener used for listening to the
|
323 |
|
|
* JColorChooser.
|
324 |
|
|
*
|
325 |
|
|
* @return A PropertyChangeListener.
|
326 |
|
|
*/
|
327 |
|
|
protected PropertyChangeListener createPropertyChangeListener()
|
328 |
|
|
{
|
329 |
|
|
return new PropertyHandler();
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
/**
|
333 |
|
|
* This method uninstalls any listeners that were previously installed by
|
334 |
|
|
* the UI.
|
335 |
|
|
*/
|
336 |
|
|
protected void uninstallListeners()
|
337 |
|
|
{
|
338 |
|
|
chooser.removePropertyChangeListener(propertyChangeListener);
|
339 |
|
|
chooser.getSelectionModel().removeChangeListener(previewListener);
|
340 |
|
|
|
341 |
|
|
previewListener = null;
|
342 |
|
|
propertyChangeListener = null;
|
343 |
|
|
}
|
344 |
|
|
}
|