Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / plaf / basic / BasicColorChooserUI.java
blob5a872ae6368217306e89d8905e2eca3a42134220
1 /* BasicColorChooserUI.java --
2 Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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.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;
58 /**
59 * This is the UI Class for the JColorChooser in the Basic Look and Feel.
61 public class BasicColorChooserUI extends ColorChooserUI
63 /**
64 * This helper class handles property changes from the JColorChooser.
66 public class PropertyHandler implements PropertyChangeListener
68 /**
69 * This method is called when any of the properties of the JColorChooser
70 * change.
72 * @param e The PropertyChangeEvent.
74 public void propertyChange(PropertyChangeEvent e)
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();
84 chooser.repaint();
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.
92 private class PreviewListener implements ChangeListener
94 /**
95 * This method is called whenever the JColorChooser's color changes.
97 * @param e The ChangeEvent.
99 public void stateChanged(ChangeEvent e)
101 if (pane != null)
103 AbstractColorChooserPanel panel = (AbstractColorChooserPanel) pane
104 .getSelectedComponent();
105 if (panel != null)
106 panel.updateChooser();
108 chooser.repaint();
113 * This helper class listens to the JTabbedPane that is used for tab
114 * changes.
116 private class TabPaneListener implements ChangeListener
119 * This method is called whenever a different tab is selected in the
120 * JTabbedPane.
122 * @param e The ChangeEvent.
124 public void stateChanged(ChangeEvent e)
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();
134 /** An array of default choosers to use in the JColorChooser. */
135 protected AbstractColorChooserPanel[] defaultChoosers;
137 /** The listener for the preview panel. */
138 protected ChangeListener previewListener;
140 /** The PropertyChangeListener for the JColorChooser. */
141 protected PropertyChangeListener propertyChangeListener;
144 * The JColorChooser.
145 * This is package-private to avoid an accessor method.
147 JColorChooser chooser;
149 /** The JTabbedPane that is used. */
150 JTabbedPane pane;
152 /** The Container that holds the preview panel. */
153 private Container prevContainer;
156 * Creates a new BasicColorChooserUI object.
158 public BasicColorChooserUI()
160 super();
164 * This method creates a new UI Component for the given JComponent.
166 * @param c The JComponent to create an UI for.
168 * @return A new BasicColorChooserUI.
170 public static ComponentUI createUI(JComponent c)
172 return new BasicColorChooserUI();
176 * This method creates the default chooser panels for the JColorChooser.
178 * @return The default chooser panels.
180 protected AbstractColorChooserPanel[] createDefaultChoosers()
182 return ColorChooserComponentFactory.getDefaultChooserPanels();
186 * This method installs the UI Component for the given JComponent.
188 * @param c The JComponent to install this UI for.
190 public void installUI(JComponent c)
192 if (c instanceof JColorChooser)
194 chooser = (JColorChooser) c;
195 chooser.setLayout(new BorderLayout());
197 // Do this first, so we avoid doing work for property change events.
198 defaultChoosers = createDefaultChoosers();
199 chooser.setChooserPanels(defaultChoosers);
200 pane = new JTabbedPane();
202 pane.addChangeListener(new ChangeListener()
204 public void stateChanged(ChangeEvent e)
206 pane.repaint();
210 makeTabs(defaultChoosers);
212 chooser.add(pane, BorderLayout.NORTH);
214 installPreviewPanel();
216 installDefaults();
217 installListeners();
222 * This method adds tabs to the JTabbedPane for the chooserPanels defined in
223 * the JColorChooser.
224 * This is package-private to avoid an accessor method.
226 * @param panels The Panels that need tabs to be made for them.
228 void makeTabs(AbstractColorChooserPanel[] panels)
230 pane.removeAll();
231 for (int i = 0; i < panels.length; i++)
232 pane.addTab(panels[i].getDisplayName(), panels[i].getSmallDisplayIcon(),
233 panels[i]);
237 * This method uninstalls this UI for the given JComponent.
239 * @param c The JComponent that will have this UI removed.
241 public void uninstallUI(JComponent c)
243 uninstallListeners();
244 uninstallDefaults();
245 uninstallDefaultChoosers();
247 pane = null;
248 chooser = null;
252 * Uninstalls the default color choosers that have been installed by this UI.
254 protected void uninstallDefaultChoosers()
256 defaultChoosers = null;
260 * This method installs the preview panel for the JColorChooser.
262 protected void installPreviewPanel()
264 updatePreviewPanel(ColorChooserComponentFactory.getPreviewPanel());
268 * This is a helper method that swaps the existing preview panel with the
269 * given panel.
270 * This is package-private to avoid an accessor method.
272 * @param preview The new preview panel.
274 void updatePreviewPanel(JComponent preview)
276 if (prevContainer == null)
278 prevContainer = new JPanel();
279 prevContainer.setLayout(new BorderLayout());
280 chooser.add(prevContainer, BorderLayout.CENTER);
282 prevContainer.removeAll();
283 prevContainer.add(preview, BorderLayout.CENTER);
287 * This method installs the default properties given by the Basic Look and
288 * Feel.
290 protected void installDefaults()
292 LookAndFeel.installColorsAndFont(chooser, "ColorChooser.background",
293 "ColorChooser.foreground",
294 "ColorChooser.font");
298 * This method uninstalls the default properties given by the Basic Look and
299 * Feel.
301 protected void uninstallDefaults()
303 chooser.setBackground(null);
304 chooser.setForeground(null);
305 chooser.setFont(null);
309 * This method installs any listeners required for this UI to function.
311 protected void installListeners()
313 propertyChangeListener = createPropertyChangeListener();
314 previewListener = new PreviewListener();
316 chooser.addPropertyChangeListener(propertyChangeListener);
317 chooser.getSelectionModel().addChangeListener(previewListener);
319 pane.addChangeListener(new TabPaneListener());
323 * This method creates the PropertyChangeListener used for listening to the
324 * JColorChooser.
326 * @return A PropertyChangeListener.
328 protected PropertyChangeListener createPropertyChangeListener()
330 return new PropertyHandler();
334 * This method uninstalls any listeners that were previously installed by
335 * the UI.
337 protected void uninstallListeners()
339 chooser.removePropertyChangeListener(propertyChangeListener);
340 chooser.getSelectionModel().removeChangeListener(previewListener);
342 previewListener = null;
343 propertyChangeListener = null;