KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > multi > MultiLookAndFeel


1 /*
2  * @(#)MultiLookAndFeel.java 1.34 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 package javax.swing.plaf.multi;
8
9 import java.util.Vector JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import javax.swing.*;
12 import javax.swing.plaf.*;
13
14 /**
15  * <p>A multiplexing look and feel that allows more than one UI
16  * to be associated with a component at the same time.
17  * The primary look and feel is called
18  * the <em>default</em> look and feel,
19  * and the other look and feels are called <em>auxiliary</em>.
20  * <p>
21  *
22  * For further information, see
23  * <a HREF="doc-files/multi_tsc.html" target="_top">Using the
24  * Multiplexing Look and Feel.</a>
25  *
26  * <p>
27  * <strong>Warning:</strong>
28  * Serialized objects of this class will not be compatible with
29  * future Swing releases. The current serialization support is
30  * appropriate for short term storage or RMI between applications running
31  * the same version of Swing. As of 1.4, support for long term storage
32  * of all JavaBeans<sup><font size="-2">TM</font></sup>
33  * has been added to the <code>java.beans</code> package.
34  * Please see {@link java.beans.XMLEncoder}.
35  *
36  * @see UIManager#addAuxiliaryLookAndFeel
37  * @see javax.swing.plaf.multi
38  *
39  * @version 1.34 12/19/03
40  * @author Willie Walker
41  */

42 public class MultiLookAndFeel extends LookAndFeel {
43
44 //////////////////////////////
45
// LookAndFeel methods
46
//////////////////////////////
47

48     /**
49      * Returns a string, suitable for use in menus,
50      * that identifies this look and feel.
51      *
52      * @return a string such as "Multiplexing Look and Feel"
53      */

54     public String JavaDoc getName() {
55         return "Multiplexing Look and Feel";
56     }
57     
58     /**
59      * Returns a string, suitable for use by applications/services,
60      * that identifies this look and feel.
61      *
62      * @return "Multiplex"
63      */

64     public String JavaDoc getID() {
65     return "Multiplex";
66     }
67
68     /**
69      * Returns a one-line description of this look and feel.
70      *
71      * @return a descriptive string such as "Allows multiple UI instances per component instance"
72      */

73     public String JavaDoc getDescription() {
74         return "Allows multiple UI instances per component instance";
75     }
76
77     /**
78      * Returns <code>false</code>;
79      * this look and feel is not native to any platform.
80      *
81      * @return <code>false</code>
82      */

83     public boolean isNativeLookAndFeel() {
84     return false;
85     }
86
87     /**
88      * Returns <code>true</code>;
89      * every platform permits this look and feel.
90      *
91      * @return <code>true</code>
92      */

93     public boolean isSupportedLookAndFeel() {
94     return true;
95     }
96
97     /**
98      * Creates, initializes, and returns
99      * the look and feel specific defaults.
100      * For this look and feel,
101      * the defaults consist solely of
102      * mappings of UI class IDs
103      * (such as "ButtonUI")
104      * to <code>ComponentUI</code> class names
105      * (such as "javax.swing.plaf.multi.MultiButtonUI").
106      *
107      * @return an initialized <code>UIDefaults</code> object
108      * @see javax.swing.JComponent#getUIClassID
109      */

110     public UIDefaults getDefaults() {
111         UIDefaults table = new MultiUIDefaults();
112     String JavaDoc packageName = "javax.swing.plaf.multi.Multi";
113     Object JavaDoc[] uiDefaults = {
114            "ButtonUI", packageName + "ButtonUI",
115      "CheckBoxMenuItemUI", packageName + "MenuItemUI",
116          "CheckBoxUI", packageName + "ButtonUI",
117              "ColorChooserUI", packageName + "ColorChooserUI",
118          "ComboBoxUI", packageName + "ComboBoxUI",
119           "DesktopIconUI", packageName + "DesktopIconUI",
120           "DesktopPaneUI", packageName + "DesktopPaneUI",
121                "EditorPaneUI", packageName + "TextUI",
122               "FileChooserUI", packageName + "FileChooserUI",
123        "FormattedTextFieldUI", packageName + "TextUI",
124         "InternalFrameUI", packageName + "InternalFrameUI",
125             "LabelUI", packageName + "LabelUI",
126              "ListUI", packageName + "ListUI",
127           "MenuBarUI", packageName + "MenuBarUI",
128          "MenuItemUI", packageName + "MenuItemUI",
129              "MenuUI", packageName + "MenuItemUI",
130            "OptionPaneUI", packageName + "OptionPaneUI",
131                 "PanelUI", packageName + "PanelUI",
132         "PasswordFieldUI", packageName + "TextUI",
133        "PopupMenuSeparatorUI", packageName + "SeparatorUI",
134         "PopupMenuUI", packageName + "PopupMenuUI",
135           "ProgressBarUI", packageName + "ProgressBarUI",
136       "RadioButtonMenuItemUI", packageName + "MenuItemUI",
137           "RadioButtonUI", packageName + "ButtonUI",
138              "RootPaneUI", packageName + "RootPaneUI",
139         "ScrollBarUI", packageName + "ScrollBarUI",
140            "ScrollPaneUI", packageName + "ScrollPaneUI",
141         "SeparatorUI", packageName + "SeparatorUI",
142            "SliderUI", packageName + "SliderUI",
143           "SpinnerUI", packageName + "SpinnerUI",
144         "SplitPaneUI", packageName + "SplitPaneUI",
145            "TabbedPaneUI", packageName + "TabbedPaneUI",
146           "TableHeaderUI", packageName + "TableHeaderUI",
147             "TableUI", packageName + "TableUI",
148          "TextAreaUI", packageName + "TextUI",
149         "TextFieldUI", packageName + "TextUI",
150          "TextPaneUI", packageName + "TextUI",
151          "ToggleButtonUI", packageName + "ButtonUI",
152          "ToolBarSeparatorUI", packageName + "SeparatorUI",
153           "ToolBarUI", packageName + "ToolBarUI",
154           "ToolTipUI", packageName + "ToolTipUI",
155              "TreeUI", packageName + "TreeUI",
156          "ViewportUI", packageName + "ViewportUI",
157     };
158
159     table.putDefaults(uiDefaults);
160     return table;
161     }
162
163 ///////////////////////////////
164
// Utility methods for the UI's
165
///////////////////////////////
166

167     /**
168      * Creates the <code>ComponentUI</code> objects
169      * required to present
170      * the <code>target</code> component,
171      * placing the objects in the <code>uis</code> vector and
172      * returning the
173      * <code>ComponentUI</code> object
174      * that best represents the component's UI.
175      * This method finds the <code>ComponentUI</code> objects
176      * by invoking
177      * <code>getDefaults().getUI(target)</code> on each
178      * default and auxiliary look and feel currently in use.
179      * The first UI object this method adds
180      * to the <code>uis</code> vector
181      * is for the default look and feel.
182      * <p>
183      * This method is invoked by the <code>createUI</code> method
184      * of <code>MultiXxxxUI</code> classes.
185      *
186      * @param mui the <code>ComponentUI</code> object
187      * that represents the complete UI
188      * for the <code>target</code> component;
189      * this should be an instance
190      * of one of the <code>MultiXxxxUI</code> classes
191      * @param uis a <code>Vector</code>;
192      * generally this is the <code>uis</code> field
193      * of the <code>mui</code> argument
194      * @param target a component whose UI is represented by <code>mui</code>
195      *
196      * @return <code>mui</code> if the component has any auxiliary UI objects;
197      * otherwise, returns the UI object for the default look and feel
198      * or <code>null</code> if the default UI object couldn't be found
199      *
200      * @see javax.swing.UIManager#getAuxiliaryLookAndFeels
201      * @see javax.swing.UIDefaults#getUI
202      * @see MultiButtonUI#uis
203      * @see MultiButtonUI#createUI
204      */

205     public static ComponentUI createUIs(ComponentUI mui,
206                         Vector JavaDoc uis,
207                             JComponent target) {
208         ComponentUI ui;
209
210         // Make sure we can at least get the default UI
211
//
212
ui = UIManager.getDefaults().getUI(target);
213         if (ui != null) {
214             uis.addElement(ui);
215             LookAndFeel[] auxiliaryLookAndFeels;
216         auxiliaryLookAndFeels = UIManager.getAuxiliaryLookAndFeels();
217             if (auxiliaryLookAndFeels != null) {
218                 for (int i = 0; i < auxiliaryLookAndFeels.length; i++) {
219                     ui = auxiliaryLookAndFeels[i].getDefaults().getUI(target);
220                     if (ui != null) {
221                         uis.addElement(ui);
222                     }
223                 }
224         }
225         } else {
226         return null;
227     }
228
229         // Don't bother returning the multiplexing UI if all we did was
230
// get a UI from just the default look and feel.
231
//
232
if (uis.size() == 1) {
233         return (ComponentUI) uis.elementAt(0);
234     } else {
235         return mui;
236     }
237     }
238
239     /**
240      * Creates an array,
241      * populates it with UI objects from the passed-in vector,
242      * and returns the array.
243      * If <code>uis</code> is null,
244      * this method returns an array with zero elements.
245      * If <code>uis</code> is an empty vector,
246      * this method returns <code>null</code>.
247      * A run-time error occurs if any objects in the <code>uis</code> vector
248      * are not of type <code>ComponentUI</code>.
249      *
250      * @param uis a vector containing <code>ComponentUI</code> objects
251      * @return an array equivalent to the passed-in vector
252      *
253      */

254     protected static ComponentUI[] uisToArray(Vector JavaDoc uis) {
255         if (uis == null) {
256             return new ComponentUI[0];
257         } else {
258             int count = uis.size();
259             if (count > 0) {
260                 ComponentUI[] u = new ComponentUI[count];
261                 for (int i = 0; i < count; i++) {
262                     u[i] = (ComponentUI)uis.elementAt(i);
263                 }
264                 return u;
265             } else {
266                 return null;
267             }
268         }
269     }
270 }
271
272 /**
273  * We want the Multiplexing LookAndFeel to be quiet and fallback
274  * gracefully if it cannot find a UI. This class overrides the
275  * getUIError method of UIDefaults, which is the method that
276  * emits error messages when it cannot find a UI class in the
277  * LAF.
278  */

279 class MultiUIDefaults extends UIDefaults {
280     protected void getUIError(String JavaDoc msg) {
281     System.err.println("Multiplexing LAF: " + msg);
282     }
283 }
284
Popular Tags