KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicColorChooserUI


1 /*
2  * @(#)BasicColorChooserUI.java 1.46 03/12/19
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.plaf.basic;
9
10 import javax.swing.*;
11 import javax.swing.colorchooser.*;
12 import javax.swing.event.*;
13 import javax.swing.border.*;
14 import javax.swing.plaf.*;
15 import java.util.*;
16 import java.awt.*;
17 import java.awt.image.*;
18 import java.awt.event.*;
19 import java.beans.PropertyChangeEvent JavaDoc;
20 import java.beans.PropertyChangeListener JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 import sun.swing.DefaultLookup;
24 import sun.swing.UIAction;
25
26 /**
27  * Provides the basic look and feel for a JColorChooser.
28  * <p>
29  * @version 1.46 12/19/03
30  * @author Tom Santos
31  * @author Steve Wilson
32  */

33
34 public class BasicColorChooserUI extends ColorChooserUI
35 {
36     /**
37      * JColorChooser this BasicColorChooserUI is installed on.
38      *
39      * @since 1.5
40      */

41     protected JColorChooser chooser;
42
43     JTabbedPane tabbedPane;
44     JPanel singlePanel;
45
46     JPanel previewPanelHolder;
47     JComponent previewPanel;
48     boolean isMultiPanel = false;
49     private static TransferHandler defaultTransferHandler = new ColorTransferHandler();
50
51     protected AbstractColorChooserPanel[] defaultChoosers;
52
53     protected ChangeListener JavaDoc previewListener;
54     protected PropertyChangeListener JavaDoc propertyChangeListener;
55     private Handler handler;
56
57     public static ComponentUI createUI(JComponent c) {
58     return new BasicColorChooserUI JavaDoc();
59     }
60
61     protected AbstractColorChooserPanel[] createDefaultChoosers() {
62         AbstractColorChooserPanel[] panels = ColorChooserComponentFactory.getDefaultChooserPanels();
63     return panels;
64     }
65
66     protected void uninstallDefaultChoosers() {
67         AbstractColorChooserPanel[] choosers = chooser.getChooserPanels();
68         for( int i = 0 ; i < choosers.length; i++) {
69             chooser.removeChooserPanel( choosers[i] );
70     }
71     }
72
73     public void installUI( JComponent c ) {
74         chooser = (JColorChooser)c;
75
76         super.installUI( c );
77
78     installDefaults();
79     installListeners();
80
81     tabbedPane = new JTabbedPane();
82         tabbedPane.setName("ColorChooser.tabPane");
83     singlePanel = new JPanel(new CenterLayout JavaDoc());
84         singlePanel.setName("ColorChooser.panel");
85
86     chooser.setLayout( new BorderLayout() );
87
88     defaultChoosers = createDefaultChoosers();
89     chooser.setChooserPanels(defaultChoosers);
90
91     previewPanelHolder = new JPanel(new CenterLayout JavaDoc());
92         previewPanelHolder.setName("ColorChooser.previewPanelHolder");
93
94         if (DefaultLookup.getBoolean(chooser, this,
95                                   "ColorChooser.showPreviewPanelText", true)) {
96             String JavaDoc previewString = UIManager.getString(
97                 "ColorChooser.previewText", chooser.getLocale());
98             previewPanelHolder.setBorder(new TitledBorder(previewString));
99         }
100     chooser.add(previewPanelHolder, BorderLayout.SOUTH);
101
102     installPreviewPanel();
103     chooser.applyComponentOrientation(c.getComponentOrientation());
104     }
105
106     public void uninstallUI( JComponent c ) {
107     chooser.remove(tabbedPane);
108     chooser.remove(singlePanel);
109     chooser.remove(previewPanelHolder);
110
111         uninstallDefaultChoosers();
112     uninstallListeners();
113     uninstallDefaults();
114
115     previewPanelHolder.remove(previewPanel);
116     if (previewPanel instanceof UIResource) {
117         chooser.setPreviewPanel(null);
118     }
119
120     previewPanelHolder = null;
121     previewPanel = null;
122     defaultChoosers = null;
123     chooser = null;
124     tabbedPane = null;
125
126         handler = null;
127     }
128
129     protected void installPreviewPanel() {
130     if (previewPanel != null) {
131         previewPanelHolder.remove(previewPanel);
132         previewPanel.removeMouseListener(getHandler());
133     }
134
135     previewPanel = chooser.getPreviewPanel();
136         Dimension layoutSize = new Dimension(); // fix for bug 4759306
137
if (previewPanel != null) {
138             layoutSize = new BorderLayout().minimumLayoutSize(previewPanel);
139             if ((previewPanelHolder != null) && (chooser != null) &&
140             (layoutSize.getWidth() + layoutSize.getHeight() == 0)) {
141               chooser.remove(previewPanelHolder);
142               return;
143             }
144         }
145     if (previewPanel == null || previewPanel instanceof UIResource) {
146       previewPanel = ColorChooserComponentFactory.getPreviewPanel(); // get from table?
147
chooser.setPreviewPanel(previewPanel);
148     }
149     previewPanel.setForeground(chooser.getColor());
150     previewPanelHolder.add(previewPanel);
151     previewPanel.addMouseListener(getHandler());
152     }
153
154     protected void installDefaults() {
155         LookAndFeel.installColorsAndFont(chooser, "ColorChooser.background",
156                                               "ColorChooser.foreground",
157                                               "ColorChooser.font");
158         LookAndFeel.installProperty(chooser, "opaque", Boolean.TRUE);
159     TransferHandler th = chooser.getTransferHandler();
160     if (th == null || th instanceof UIResource) {
161         chooser.setTransferHandler(defaultTransferHandler);
162     }
163     }
164
165     protected void uninstallDefaults() {
166     if (chooser.getTransferHandler() instanceof UIResource) {
167         chooser.setTransferHandler(null);
168     }
169     }
170
171     
172     protected void installListeners() {
173         propertyChangeListener = createPropertyChangeListener();
174     chooser.addPropertyChangeListener( propertyChangeListener );
175
176     previewListener = getHandler();
177     chooser.getSelectionModel().addChangeListener(previewListener);
178     }
179
180     private Handler getHandler() {
181         if (handler == null) {
182             handler = new Handler();
183         }
184         return handler;
185     }
186
187     protected PropertyChangeListener JavaDoc createPropertyChangeListener() {
188         return getHandler();
189     }
190   
191     protected void uninstallListeners() {
192     chooser.removePropertyChangeListener( propertyChangeListener );
193     chooser.getSelectionModel().removeChangeListener(previewListener);
194     previewPanel.removeMouseListener(getHandler());
195     }
196
197
198     private class Handler implements ChangeListener JavaDoc, MouseListener,
199             PropertyChangeListener JavaDoc {
200         //
201
// ChangeListener
202
//
203
public void stateChanged(ChangeEvent JavaDoc evt) {
204             ColorSelectionModel model = (ColorSelectionModel)evt.getSource();
205             if (previewPanel != null) {
206                 previewPanel.setForeground(model.getSelectedColor());
207                 previewPanel.repaint();
208             }
209         }
210
211         //
212
// MouseListener
213
public void mousePressed(MouseEvent evt) {
214             if (chooser.getDragEnabled()) {
215                 TransferHandler th = chooser.getTransferHandler();
216                 th.exportAsDrag(chooser, evt, TransferHandler.COPY);
217             }
218         }
219         public void mouseReleased(MouseEvent evt) {}
220         public void mouseClicked(MouseEvent evt) {}
221         public void mouseEntered(MouseEvent evt) {}
222         public void mouseExited(MouseEvent evt) {}
223
224         //
225
// PropertyChangeListener
226
//
227
public void propertyChange(PropertyChangeEvent JavaDoc evt) {
228             String JavaDoc prop = evt.getPropertyName();
229
230             if (prop == JColorChooser.CHOOSER_PANELS_PROPERTY) {
231                 AbstractColorChooserPanel[] oldPanels =
232                     (AbstractColorChooserPanel[])evt.getOldValue();
233                 AbstractColorChooserPanel[] newPanels =
234                     (AbstractColorChooserPanel[])evt.getNewValue();
235
236                 for (int i = 0; i < oldPanels.length; i++) { // remove old panels
237
Container wrapper = oldPanels[i].getParent();
238                     if (wrapper != null) {
239                       Container parent = wrapper.getParent();
240                       if (parent != null)
241                           parent.remove(wrapper); // remove from hierarchy
242
oldPanels[i].uninstallChooserPanel(chooser); // uninstall
243
}
244                 }
245
246                 int numNewPanels = newPanels.length;
247                 if (numNewPanels == 0) { // removed all panels and added none
248
chooser.remove(tabbedPane);
249                     return;
250                 }
251                 else if (numNewPanels == 1) { // one panel case
252
chooser.remove(tabbedPane);
253                     JPanel centerWrapper = new JPanel( new CenterLayout JavaDoc() );
254                     centerWrapper.add(newPanels[0]);
255                     singlePanel.add(centerWrapper, BorderLayout.CENTER);
256                     chooser.add(singlePanel);
257                 }
258                 else { // multi-panel case
259
if ( oldPanels.length < 2 ) {// moving from single to multiple
260
chooser.remove(singlePanel);
261                         chooser.add(tabbedPane, BorderLayout.CENTER);
262                     }
263
264                     for (int i = 0; i < newPanels.length; i++) {
265                         JPanel centerWrapper = new JPanel( new CenterLayout JavaDoc() );
266                         String JavaDoc name = newPanels[i].getDisplayName();
267                         int mnemonic = newPanels[i].getMnemonic();
268                         centerWrapper.add(newPanels[i]);
269                         tabbedPane.addTab(name, centerWrapper);
270                         if (mnemonic > 0) {
271                             tabbedPane.setMnemonicAt(i, mnemonic);
272                             tabbedPane.setDisplayedMnemonicIndexAt(
273                                   i, newPanels[i].getDisplayedMnemonicIndex());
274                         }
275                     }
276                 }
277                 chooser.applyComponentOrientation(chooser.getComponentOrientation());
278                 for (int i = 0; i < newPanels.length; i++) {
279                     newPanels[i].installChooserPanel(chooser);
280                 }
281             }
282
283             if (prop == JColorChooser.PREVIEW_PANEL_PROPERTY) {
284                 if (evt.getNewValue() != previewPanel) {
285                     installPreviewPanel();
286                 }
287             }
288             if (prop == "componentOrientation") {
289                 ComponentOrientation o =
290                     (ComponentOrientation)evt.getNewValue();
291                 JColorChooser cc = (JColorChooser)evt.getSource();
292                 if (o != (ComponentOrientation)evt.getOldValue()) {
293                     cc.applyComponentOrientation(o);
294                     cc.updateUI();
295                 }
296             }
297         }
298     }
299
300     /**
301      * This inner class is marked &quot;public&quot; due to a compiler bug.
302      * This class should be treated as a &quot;protected&quot; inner class.
303      * Instantiate it only within subclasses of <Foo>.
304      */

305     public class PropertyHandler implements PropertyChangeListener JavaDoc {
306         public void propertyChange(PropertyChangeEvent JavaDoc e) {
307             getHandler().propertyChange(e);
308         }
309     }
310
311     static class ColorTransferHandler extends TransferHandler implements UIResource {
312
313     ColorTransferHandler() {
314         super("color");
315     }
316     }
317     
318 }
319
320
321
322
Popular Tags