KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > util > UIBootstrapValue


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * UIBootstrapValue.java
21  *
22  * Created on March 29, 2004, 4:17 PM
23  */

24
25 package org.netbeans.swing.plaf.util;
26
27 import javax.swing.*;
28
29 /**
30  * Value that can be placed into UIDefaults, which will put additional items into
31  * UIDefaults when its value is requested.
32  * <p>
33  * The idea is to avoid putting a lot of things that may not be used into
34  * UIDefaults on startup. So, for example, the first time a tab control's UI
35  * is requested, this value will return the string from which the UI can be
36  * fetched - but first it will put the assorted keys and values that that UI
37  * will need into UIDefaults.
38  * <p>
39  * Since multiple UIs may require the same things in UIDefaults, there is the
40  * method createShared(), which will create another instance (really an inner
41  * class instance) that shares the code and key/value pairs. Whichever is
42  * asked for first will initialize the keys and values required. So the usage
43  * pattern is something like:
44  * <pre>
45  * Object someKeysAndValues = new Object[] {"fooColor", Color.BLUE, "barColor", Color.RED};
46  * UIBootstrapValue bv = new UIBootstrapValue ("com.foo.FnordUIClass", someKeysAndValues);
47  * Object next = bv.createShared ("com.foo.ThiptUIClass");
48  * UIManager.put ("FnordUI", bv);
49  * UIManager.put ("ThiptUI", next);
50  * </pre>
51  *
52  * @author Tim Boudreau
53  */

54 public class UIBootstrapValue implements UIDefaults.LazyValue {
55     private boolean installed = false;
56     private final String JavaDoc uiClassName;
57     protected Object JavaDoc[] defaults;
58     /** Creates a new instance of UIBootstrapValue */
59     public UIBootstrapValue(String JavaDoc uiClassName, Object JavaDoc[] defaults) {
60         this.defaults = defaults;
61         this.uiClassName = uiClassName;
62     }
63     
64     /** Create the value that UIDefaults will return. If the keys and values
65      * the UI class we're representing requires have not yet been installed,
66      * this will install them.
67      */

68     public Object JavaDoc createValue(UIDefaults uidefaults) {
69         if (!installed) {
70             installKeysAndValues(uidefaults);
71         }
72         return uiClassName;
73     }
74     
75     /** Install the defaults the UI we're representing will need to function */
76     private void installKeysAndValues(UIDefaults ui) {
77         ui.putDefaults (getKeysAndValues());
78         installed = true;
79     }
80
81     public Object JavaDoc[] getKeysAndValues() {
82         return defaults;
83     }
84
85     public void uninstall() {
86         if (defaults == null) {
87             return;
88         }
89         for (int i=0; i < defaults.length; i+=2) {
90             UIManager.put (defaults[i], null);
91         }
92         //null defaults so a Meta instance won't cause us to do work twice
93
defaults = null;
94     }
95
96     public String JavaDoc toString() {
97         return getClass() + " for " + uiClassName; //NOI18N
98
}
99
100     /** Create another entry value to put in UIDefaults, which will also
101      * trigger installing the keys and values, to ensure that they are only
102      * added once, by whichever entry is asked for the value first. */

103     public UIDefaults.LazyValue createShared (String JavaDoc uiClassName) {
104         return new Meta (uiClassName);
105     }
106     
107     private class Meta implements UIDefaults.LazyValue {
108         private String JavaDoc name;
109         public Meta (String JavaDoc uiClassName) {
110             this.name = uiClassName;
111         }
112         
113         public Object JavaDoc createValue(javax.swing.UIDefaults JavaDoc uidefaults) {
114             if (!installed) {
115                 installKeysAndValues(uidefaults);
116             }
117             return name;
118         }
119
120         public String JavaDoc toString() {
121             return "Meta-" + super.toString() + " for " + uiClassName; //NOI18N
122
}
123     }
124
125     public abstract static class Lazy extends UIBootstrapValue implements UIDefaults.LazyValue {
126         public Lazy (String JavaDoc uiClassName) {
127             super (uiClassName, null);
128         }
129
130         public Object JavaDoc[] getKeysAndValues() {
131             if (defaults == null) {
132                 defaults = createKeysAndValues();
133             }
134             return defaults;
135         }
136
137         public abstract Object JavaDoc[] createKeysAndValues();
138
139     }
140 }
141
Popular Tags