Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicColorChooserUI.java
blob61af43bbf70bff30c8e2a62b9c05bf1b0dc1a6a2
1 /* BasicColorChooserUI.java --
2 Copyright (C) 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.plaf.basic;
41 import java.awt.BorderLayout;
42 import java.awt.Container;
43 import java.beans.PropertyChangeEvent;
44 import java.beans.PropertyChangeListener;
46 import javax.swing.JColorChooser;
47 import javax.swing.JComponent;
48 import javax.swing.JPanel;
49 import javax.swing.JTabbedPane;
50 import javax.swing.UIDefaults;
51 import javax.swing.UIManager;
52 import javax.swing.colorchooser.AbstractColorChooserPanel;
53 import javax.swing.colorchooser.ColorChooserComponentFactory;
54 import javax.swing.event.ChangeEvent;
55 import javax.swing.event.ChangeListener;
56 import javax.swing.plaf.ColorChooserUI;
57 import javax.swing.plaf.ComponentUI;
59 /**
60 * This is the UI Class for the JColorChooser in the Basic Look and Feel.
62 public class BasicColorChooserUI extends ColorChooserUI
64 /**
65 * This helper class handles property changes from the JColorChooser.
67 public class PropertyHandler implements PropertyChangeListener
69 /**
70 * This method is called when any of the properties of the JColorChooser
71 * change.
73 * @param e The PropertyChangeEvent.
75 public void propertyChange(PropertyChangeEvent e)
77 if (e.getPropertyName() == JColorChooser.CHOOSER_PANELS_PROPERTY)
78 makeTabs(chooser.getChooserPanels());
79 else if (e.getPropertyName() == JColorChooser.PREVIEW_PANEL_PROPERTY)
80 updatePreviewPanel(chooser.getPreviewPanel());
81 else if (e.getPropertyName() == JColorChooser.SELECTION_MODEL_PROPERTY)
82 ((AbstractColorChooserPanel) pane.getSelectedComponent())
83 .updateChooser();
85 chooser.repaint();
89 /**
90 * This is a helper class that listens to the Model of the JColorChooser for
91 * color change events so it can update the preview panel.
93 private class PreviewListener implements ChangeListener
95 /**
96 * This method is called whenever the JColorChooser's color changes.
98 * @param e The ChangeEvent.
100 public void stateChanged(ChangeEvent e)
102 if (pane != null)
104 AbstractColorChooserPanel panel = (AbstractColorChooserPanel) pane
105 .getSelectedComponent();
106 if (panel != null)
107 panel.updateChooser();
109 chooser.repaint();
114 * This helper class listens to the JTabbedPane that is used for tab
115 * changes.
117 private class TabPaneListener implements ChangeListener
120 * This method is called whenever a different tab is selected in the
121 * JTabbedPane.
123 * @param e The ChangeEvent.
125 public void stateChanged(ChangeEvent e)
127 // Need to do this because we don't update all the tabs when they're not
128 // visible, so they are not informed of new colors when they're hidden.
129 AbstractColorChooserPanel comp = (AbstractColorChooserPanel) pane
130 .getSelectedComponent();
131 comp.updateChooser();
135 /** An array of default choosers to use in the JColorChooser. */
136 protected AbstractColorChooserPanel[] defaultChoosers;
138 /** The listener for the preview panel. */
139 protected ChangeListener previewListener;
141 /** The PropertyChangeListener for the JColorChooser. */
142 protected PropertyChangeListener propertyChangeListener;
144 /** The JColorChooser. */
145 private JColorChooser chooser;
147 /** The JTabbedPane that is used. */
148 private JTabbedPane pane;
150 /** The Container that holds the preview panel. */
151 private Container prevContainer;
154 * Creates a new BasicColorChooserUI object.
156 public BasicColorChooserUI()
158 super();
162 * This method creates a new UI Component for the given JComponent.
164 * @param c The JComponent to create an UI for.
166 * @return A new BasicColorChooserUI.
168 public static ComponentUI createUI(JComponent c)
170 return new BasicColorChooserUI();
174 * This method creates the default chooser panels for the JColorChooser.
176 * @return The default chooser panels.
178 protected AbstractColorChooserPanel[] createDefaultChoosers()
180 return ColorChooserComponentFactory.getDefaultChooserPanels();
184 * This method installs the UI Component for the given JComponent.
186 * @param c The JComponent to install this UI for.
188 public void installUI(JComponent c)
190 if (c instanceof JColorChooser)
192 chooser = (JColorChooser) c;
193 chooser.setLayout(new BorderLayout());
195 // Do this first, so we avoid doing work for property change events.
196 defaultChoosers = createDefaultChoosers();
197 chooser.setChooserPanels(defaultChoosers);
198 pane = new JTabbedPane();
200 pane.addChangeListener(new ChangeListener()
202 public void stateChanged(ChangeEvent e)
204 pane.repaint();
208 makeTabs(defaultChoosers);
210 chooser.add(pane, BorderLayout.NORTH);
212 installPreviewPanel();
214 installDefaults();
215 installListeners();
220 * This method adds tabs to the JTabbedPane for the chooserPanels defined in
221 * the JColorChooser.
223 * @param panels The Panels that need tabs to be made for them.
225 private void makeTabs(AbstractColorChooserPanel[] panels)
227 pane.removeAll();
228 for (int i = 0; i < panels.length; i++)
229 pane.addTab(panels[i].getDisplayName(), panels[i].getSmallDisplayIcon(),
230 panels[i]);
234 * This method uninstalls this UI for the given JComponent.
236 * @param c The JComponent that will have this UI removed.
238 public void uninstallUI(JComponent c)
240 uninstallListeners();
241 uninstallDefaults();
243 pane = null;
244 chooser = null;
248 * This method installs the preview panel for the JColorChooser.
250 protected void installPreviewPanel()
252 updatePreviewPanel(ColorChooserComponentFactory.getPreviewPanel());
256 * This is a helper method that swaps the existing preview panel with the
257 * given panel.
259 * @param preview The new preview panel.
261 private void updatePreviewPanel(JComponent preview)
263 if (prevContainer == null)
265 prevContainer = new JPanel();
266 prevContainer.setLayout(new BorderLayout());
267 chooser.add(prevContainer, BorderLayout.CENTER);
269 prevContainer.removeAll();
270 prevContainer.add(preview, BorderLayout.CENTER);
274 * This method installs the default properties given by the Basic Look and
275 * Feel.
277 protected void installDefaults()
279 UIDefaults defaults = UIManager.getLookAndFeelDefaults();
281 chooser.setFont(defaults.getFont("ColorChooser.font"));
282 chooser.setForeground(defaults.getColor("ColorChooser.foreground"));
283 chooser.setBackground(defaults.getColor("ColorChooser.background"));
287 * This method uninstalls the default properties given by the Basic Look and
288 * Feel.
290 protected void uninstallDefaults()
292 chooser.setBackground(null);
293 chooser.setForeground(null);
294 chooser.setFont(null);
298 * This method installs any listeners required for this UI to function.
300 protected void installListeners()
302 propertyChangeListener = createPropertyChangeListener();
303 previewListener = new PreviewListener();
305 chooser.addPropertyChangeListener(propertyChangeListener);
306 chooser.getSelectionModel().addChangeListener(previewListener);
308 pane.addChangeListener(new TabPaneListener());
312 * This method creates the PropertyChangeListener used for listening to the
313 * JColorChooser.
315 * @return A PropertyChangeListener.
317 protected PropertyChangeListener createPropertyChangeListener()
319 return new PropertyHandler();
323 * This method uninstalls any listeners that were previously installed by
324 * the UI.
326 protected void uninstallListeners()
328 chooser.removePropertyChangeListener(propertyChangeListener);
329 chooser.getSelectionModel().removeChangeListener(previewListener);
331 previewListener = null;
332 propertyChangeListener = null;