KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > looks > DefaultLook


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 package org.netbeans.spi.looks;
21
22 import javax.swing.Action JavaDoc;
23 import java.beans.BeanInfo JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import org.netbeans.modules.looks.RegistryBridge;
28 import org.openide.util.Utilities;
29 import org.openide.util.Lookup;
30
31 /**
32  * Utility class for providing Looks. Some methods are linked together to
33  * make it easier for subclasses to implement reasonable functionality
34  * (iconBase, actionBase), etc.
35  *
36  * @author Jaroslav Tulach
37  */

38 public abstract class DefaultLook extends Look {
39     /** messages to create a resource identification for each type of
40     * icon from the base name for the icon.
41     */

42     private static final MessageFormat JavaDoc[] icons = {
43         // color 16x16
44
new MessageFormat JavaDoc ("{0}.gif"), // NOI18N
45
// color 32x32
46
new MessageFormat JavaDoc ("{0}32.gif"), // NOI18N
47
// mono 16x16
48
new MessageFormat JavaDoc ("{0}.gif"), // NOI18N
49
// mono 32x32
50
new MessageFormat JavaDoc ("{0}32.gif"), // NOI18N
51
// opened color 16x16
52
new MessageFormat JavaDoc ("{0}Open.gif"), // NOI18N
53
// opened color 32x32
54
new MessageFormat JavaDoc ("{0}Open32.gif"), // NOI18N
55
// opened mono 16x16
56
new MessageFormat JavaDoc ("{0}Open.gif"), // NOI18N
57
// opened mono 32x32
58
new MessageFormat JavaDoc ("{0}Open32.gif"), // NOI18N
59
};
60     /** To index normal icon from previous array use
61     * + ICON_BASE.
62     */

63     private static final int ICON_BASE = -1;
64     /** for indexing opened icons */
65     private static final int OPENED_ICON_BASE = 3;
66
67     private RegistryBridge registryBridge;
68     
69     /** Creates new instance of look does no work.
70      * @param name the name to assign to the look
71      */

72     public DefaultLook(String JavaDoc name) {
73         this( RegistryBridge.getDefault( null ), name );
74     }
75     
76     DefaultLook(RegistryBridge registryBridge, String JavaDoc name) {
77         super( name );
78         this.registryBridge = registryBridge;
79     }
80     
81     // Methods for STYLE -------------------------------------------------------
82

83     /** Finds icon using the value returned from <link>iconBase</link>
84      * @param representedObject Parameter is ignored.
85      * @param type Icon type constant from {@link java.beans.BeanInfo}
86      * @param env Parameter is ignored.
87      * @return an icon or <CODE>null</CODE> if not found
88      */

89     public java.awt.Image JavaDoc getIcon(Object JavaDoc representedObject, int type, Lookup env ) {
90         return findIcon ( representedObject, type, ICON_BASE, env);
91     }
92
93     /** Finds icon using the value returned from <link>iconBase</link>
94      * @param representedObject Parameter is ignored.
95      * @param type Icon type constant from {@link java.beans.BeanInfo}
96      * @param env Parameter is ignored.
97      * @return an icon or <CODE>null</CODE> if not found
98      */

99     public java.awt.Image JavaDoc getOpenedIcon(Object JavaDoc representedObject, int type, Lookup env ) {
100         return findIcon ( representedObject, type, OPENED_ICON_BASE, env);
101     }
102
103     // Methods for ACTIONS & NEW TYPES -----------------------------------------
104

105     /** Calls actionBase (substitute, false) and extracts actions
106      * from that context.
107      * @param representedObject Parameter is ignored.
108      * @param env Parameter is ignored.
109      * @return the actions at the context or <CODE>null</CODE>
110      */

111     public Action JavaDoc[] getActions(Object JavaDoc representedObject, Lookup env ) {
112         return actionsForContext (registryBridge, actionBase (representedObject, false, env ));
113     }
114
115     /** Calls actionBase (substitute, true) and extracts actions
116      * from that context.
117      * @param representedObject Parameter is ignored.
118      * @param env Parameter is ignored.
119      * @return the actions at the context or <CODE>null</CODE>
120      */

121     public Action JavaDoc[] getContextActions(Object JavaDoc representedObject, Lookup env ) {
122         return actionsForContext (registryBridge, actionBase (representedObject, true, env ));
123     }
124
125     /** Extracts the first action from getActions, if any.
126      * @param representedObject Parameter is ignored.
127      * @param env Parameter is ignored.
128      * @return the action or <CODE>null</CODE>
129      */

130     public Action JavaDoc getDefaultAction(Object JavaDoc representedObject, Lookup env ) {
131         Action JavaDoc[] arr = getActions (representedObject, env );
132         return arr != null && arr.length > 0 ? arr[0] : null;
133     }
134
135     // Methods for PROPERTIES AND CUSTOMIZER -----------------------------------
136

137     /** Check whether the customizer for the represented object is available.
138      * I.e. whether
139      * getCustomizer (substitute) returns non-null value
140      * @param representedObject Parameter is ignored.
141      * @param env Parameter is ignored.
142      * @return true if the customizer is available, false otherwise
143      */

144     public boolean hasCustomizer(Object JavaDoc representedObject, Lookup env ) {
145         return getCustomizer (representedObject, env ) != null;
146     }
147
148
149     // Icon management ---------------------------------------------------------
150

151     /** Allows subclasses to specify an icon in easier way without need to
152      * load the actual objects.
153      *
154      * <p>For example, if the returned base is <code>/resource/MyIcon</code>, the
155      * following images may be used according to the icon state and
156      * {@link java.beans.BeanInfo#getIcon presentation type}:
157      *
158      * <ul><li><code>resource/MyIcon.gif</code><li><code>resource/MyIconOpen.gif</code>
159      * <li><code>resource/MyIcon32.gif</code><li><code>resource/MyIconOpen32.gif</code></ul>
160      *
161      * <P>
162      * The default implementation returns null.
163      * @param representedObject The substitute to locate icon for.
164      * @param env Environement of the object.
165      * @return the base for icon search (no initial slash) or <code>null</code> if this look does not provide an icon
166      */

167     protected String JavaDoc iconBase ( Object JavaDoc representedObject, Lookup env ) {
168         return null;
169     }
170
171     /** Allows subclasses to specify actions in a easy way - by providing a
172     * name of a context name where to find the javax.swing.Action objects.
173     *
174     * <p>By default the method returns name of this class (separated by slashes)
175     * with a prefix Looks/Actions. So for a class <code>org.nb.mymod.MyLook</code>
176     * the default action context is <em>Looks/Actions/org/nb/mymod/MyLook</em>.
177     * As a result it is not necessary to override this method in many cases.
178     *
179     * @param representedObject The object to work on.
180     * @param context false if <code>getActions</code> was called,
181     * true if <code>getContextActions</code> was called
182     * @param env Environement for the object.
183     * @return the name of a context
184     */

185     protected String JavaDoc actionBase ( Object JavaDoc representedObject, boolean context, Lookup env ) {
186         return "Looks/Actions/" + getClass ().getName ().replace ('.', '/'); // NOI18N
187
}
188
189     // Private methods ---------------------------------------------------------
190

191     /** Reads actions from a context.
192      * @param name of the context.
193      * @return array of actions
194      */

195     private static Action JavaDoc[] actionsForContext (RegistryBridge registryBridge, String JavaDoc name) {
196         Enumeration JavaDoc en = registryBridge.getObjects(name, null);
197         if (!en.hasMoreElements ()) {
198             return null;
199         }
200
201         ArrayList JavaDoc arr = new ArrayList JavaDoc ();
202         while (en.hasMoreElements()) {
203             arr.add (en.nextElement ());
204         }
205
206         return (Action JavaDoc[])arr.toArray (new Action JavaDoc[arr.size ()]);
207     }
208
209     /** Tries to find the right icon for the iconbase.
210     * @param type type of icon (from BeanInfo constants)
211     * @param ib base where to scan in the array
212     * @return icon or null
213     */

214     private java.awt.Image JavaDoc findIcon ( Object JavaDoc representedObject, int type, int ib, Lookup env) {
215         String JavaDoc[] base = { iconBase (representedObject, env ) };
216         if (base[0] == null) {
217             return null;
218         }
219
220         String JavaDoc res = icons[type + ib].format (base);
221         java.awt.Image JavaDoc im = Utilities.loadImage (res);
222
223         if (im != null) return im;
224
225         // try the first icon
226
res = icons[BeanInfo.ICON_COLOR_16x16 + ib].format (base);
227
228         im = Utilities.loadImage (res);
229
230         if (im != null) return im;
231
232         if (ib == OPENED_ICON_BASE) {
233             // try closed icon also
234
return findIcon (representedObject, type, ICON_BASE, env);
235         }
236
237         // if still not found return default icon
238
return null;
239     }
240
241 }
242
Popular Tags