KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import org.netbeans.modules.looks.RegistryBridge;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28
29 /** Static factory class for different kinds of useful Looks. For looks
30  * which can be constructed using <em>module layer</em>
31  * <code>XMLFileSystem</code>s, a sample of such XML definition is given
32  * in the Javadoc.
33  *
34  * @author Petr Hrebejk, Jaroslav Tulach
35  */

36 public abstract class Looks {
37
38     // Shared attribute names
39
static final String JavaDoc CONTEXT="context";
40     static final String JavaDoc LOOK_SELECTOR = "lookSelector"; // NOI18N
41

42     // Look Attribute names
43
private static final String JavaDoc LOOK = "look"; // NOI18N
44
private static final String JavaDoc DELEGATE = "delegateLook"; // NOI18N
45
private static final String JavaDoc ALL_METHODS = "ALL_METHODS"; // NOI18N
46
private static final String JavaDoc NO_METHODS = "NO_METHODS"; // NOI18N
47

48     /* The only bean look in the system */
49     private static final Look BEAN_LOOK = new org.netbeans.modules.looks.BeanLook( "JavaBeans" ); // NOI18N
50

51     /* The only LookSwitcherLook in the system */
52     // private static final Look LOOK_SWITCHER_LOOK = new org.netbeans.modules.looks.LookSwitcherLook();
53

54     /* Map of FilterLook attributes */
55     private static final HashMap JavaDoc mapFilterMethods = new HashMap JavaDoc (16);
56
57     /** No instances */
58     private Looks() {
59     }
60
61     // Methods to be used from XML layers --------------------------------------
62

63     /** A method to be used from XML layers
64      */

65     static final Look composite(FileObject fo) throws IOException JavaDoc {
66         
67         org.netbeans.modules.looks.RegistryBridge registryBridge = RegistryBridge.getDefault( fo );
68         String JavaDoc contextName = readStringAttribute (fo, CONTEXT);
69         
70                
71         return new org.netbeans.modules.looks.CompositeLook (
72                     fo.getPath(),
73                     new LookSelector( org.netbeans.modules.looks.SelectorImplFactory.context( registryBridge, contextName ) ) );
74     }
75
76     /** A method to be used from XML layers
77      */

78     /*
79     static final Look lookSwitcherLook( FileObject fo ) {
80         return LOOK_SWITCHER_LOOK;
81     }
82     */

83
84     /** A method to be used from XML layers
85      */

86     static final Look filter(FileObject fo) throws IOException JavaDoc {
87         if (mapFilterMethods.isEmpty ())
88             initFilterMethods ();
89
90         Look delegate = readLookAttribute (fo, DELEGATE);
91
92         // proccess mask
93
// 1. default value is ALL_METHODS
94
// 2. read ALL_METHODS and NO_METHODS attributes, set the base level of masking
95
// 3. read a attribute for each method and cut down or enlarge the mask
96
long mask = Look.ALL_METHODS;
97         Boolean JavaDoc helpBool = null;
98         long helpLong;
99         helpBool = readBooleanAttribute (fo, ALL_METHODS);
100         mask = helpBool == null || helpBool.booleanValue () ? Look.ALL_METHODS : Look.NO_METHODS;
101
102         helpBool = readBooleanAttribute (fo, NO_METHODS);
103         mask = helpBool != null && helpBool.booleanValue () ? Look.NO_METHODS : mask;
104
105         String JavaDoc attr;
106         for (Enumeration JavaDoc attrs = fo.getAttributes (); attrs.hasMoreElements (); ) {
107             attr = (String JavaDoc)attrs.nextElement ();
108             if (mapFilterMethods.containsKey (attr)) {
109                 helpBool = readBooleanAttribute (fo, attr);
110                 helpLong = ((Long JavaDoc)mapFilterMethods.get (attr)).longValue ();
111                 mask = helpBool == null ? mask : (helpBool.booleanValue () ?
112                     // mask
113
mask | helpLong :
114                     // unmask
115
mask & ~helpLong
116                     );
117             }
118         }
119
120         return new org.netbeans.modules.looks.FilterLook ( fo.getName(), delegate, mask);
121     }
122
123     /** A method to be used from XML layers
124      */

125     static final Look childrenSelectorProvider(FileObject fo) throws IOException JavaDoc {
126
127         Look look = null;
128         String JavaDoc lookName = readStringAttribute (fo, LOOK);
129
130         // lookup delegate look
131
Object JavaDoc delegateObject = RegistryBridge.getDefault( fo ).resolve(lookName);
132
133         if ((delegateObject != null) && (delegateObject instanceof Look)) {
134             look = (Look)delegateObject;
135         }
136         if (look == null) {
137             ErrorManager.getDefault ().notify (new Exception JavaDoc ("Look not found at " + look)); // NOI18N
138
return null;
139         }
140
141         LookSelector lookSelector = null;
142         String JavaDoc selectorName = readStringAttribute (fo, LOOK_SELECTOR);
143         
144         // lookup delegate look
145
delegateObject = RegistryBridge.getDefault( fo ).resolve (selectorName);
146         if ((delegateObject != null) && (delegateObject instanceof LookSelector)) {
147             lookSelector = (LookSelector)delegateObject;
148         }
149         
150         if (lookSelector == null) {
151             ErrorManager.getDefault ().notify (new Exception JavaDoc ("LookSelector not found at " + lookSelector)); // NOI18N
152
return null;
153         }
154
155         return new org.netbeans.modules.looks.ChildrenSelectorProvider( fo.getName(), look, lookSelector );
156     }
157
158
159     // public factory methods --------------------------------------------------
160

161     /** Look that presents an object as a JavaBean. Uses java.beans.Introspector
162      * to get the properties and delegates to the object all other methods.
163      * <P>
164      * To create this look from an XML layer type:
165      * <pre>
166      * &lt;file name="NameOfYourLook.instance" &gt;
167      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.Look" /&gt;
168      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Looks.bean" /&gt;
169      * &lt;/file&gt;
170      * </pre>
171      *
172      * @see org.openide.nodes.BeanNode BeanNode
173      * @return Look which represents objects as JavaBeans
174      */

175     public static final Look bean() {
176         return BEAN_LOOK;
177     }
178
179     /** Look that filters the features of look which delegates. The filtered
180      * features are specified by mask.
181      *
182      * <P>
183      * To create this filter look from an XML layer type:
184      * <pre>
185      * &lt;file name="NameOfYourLook.instance" &gt;
186      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.Look" /&gt;
187      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Looks.filter" /&gt;
188      * &lt;!--The path to declaration of delegate look.--&gt;
189      * &lt;attr name="delegateLook" stringvalue="Looks/MyLayer/MyLooks/FooLook" /&gt;
190      * &lt;!--Optionally! The mask can be specified in mask attribute. The default mask is ALL_METHODS.--&gt;
191      * &lt;attr name="ALL_METHODS" boolvalue="false" /&gt;
192      * &lt;!--Optionally! Mask specification of a methods individually, can enlarge the mask in detail.--&gt;
193      * &lt;attr name="GET_NAME" boolvalue="false" /&gt;
194      * &lt;attr name="GET_ICON" boolvalue="true" /&gt;
195      * ...
196      * &lt;/file&gt;
197      * </pre>
198      * The <code>&lt;attr name="delegateLook" /&gt;</code> attribute can directly return
199      * an instance of your look (instead of specifying its path). So one can replace
200      * that line with:
201      * <PRE>
202      * &lt;attr name="delegateLook" newvalue="yourpkg.YourLook" /&gt;
203      * </PRE>
204      * or if one wants to use <code>methodvalue</code>:
205      * <PRE>
206      * &lt;attr name="delegateLook" methodvalue="yourpkg.YourFactoryClass.yourMethod" /&gt;
207      * </PRE>
208      * under the condition that there is a YourFactoryClass in yourpkg that contains following
209      * method:
210      * <PRE>
211      * package yourpkg;
212      * public final class YourFactoryClass {
213      * public static Look yourMethod () {
214      * Look l = ...;
215      * return l;
216      * }
217      * }
218      * </PRE>
219      *
220      *
221      * @see org.netbeans.spi.looks.ProxyLook ProxyLook for values for method masking
222      * @param delegate The look the FilterLook will delegate to
223      * @param mask Binary mask where 1 means that the method call will be
224      * forwarded to the delegate look. 0 means that the delegation
225      * will be suppressed
226      * @return new filter look with given mask and delegate
227      */

228     public static final Look filter( String JavaDoc name, Look delegate, long mask ) {
229         return new org.netbeans.modules.looks.FilterLook( name, delegate, mask );
230     }
231
232     /** Look that is composite of the delegates specified by a given context or
233      * composite of array of looks. Composite means that all attributes of the
234      * node are determined by asking all sublooks for the value. If the attribute
235      * only allows one value (e.g. name or icon) first not <CODE>null</CODE> or
236      * not <CODE>false</CODE> value is chosen. If the attribute is multivalued
237      * (e.g. child nodes or properties) the result is set produced by merging
238      * results from all sublooks.
239      *
240      * <P>
241      * To create this composite look from an XML layer type:
242      * <pre>
243      * &lt;file name="NameOfYourNamespaceSelector.instance" &gt;
244      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.Look" /&gt;
245      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Looks.composite" /&gt;
246      * &lt;attr name="context" stringvalue="Looks/Types/" /&gt;
247      * &lt;/file&gt;
248      * </pre>
249      * <BR>
250      * The context attribute should point to some Context (usualy a folder on
251      * system filesystem) which will contain all sublooks of the look. Content
252      * of the folder is equivalent of the delegates parameter.
253      *
254      * @see org.netbeans.spi.looks.ProxyLook ProxyLook for more info about looks
255      * composition.
256      * @param name Name of the composite look.
257      * @param delegates Sublooks of the look.
258      * @return new look composed from sublooks
259      */

260     public static final Look composite( String JavaDoc name, Look[] delegates) {
261         return new org.netbeans.modules.looks.CompositeLook( name, delegates );
262     }
263
264     /* Look that serves for switching the looks on LookNodes. The main feature
265      * of this look is adding the LookNode into it's own lookup. This means
266      * that looking up a LookNode in the Lookup returned from
267      * {@link org.netbeans.spi.looks.Look.NodeSubstitute#getLookup() Look.NodeSubstitute#getLookup()}
268      * will return LookNode which is the NodeSubstitute associated with. This
269      * gives you the opportunity to call
270      * {@link LookNode#getLook() getLook()},
271      * {@link LookNode#getLookSelector() getLookSelector()} and
272      * {@link LookNode#setLook(org.netbeans.spi.looks.Look) setLook(Look)}
273      * to determine set of available looks and to set new look.
274      * <P>
275      * This look also provides an default action and default property sheet
276      * which allows for changing looks. In case you want to implement your
277      * own UI for changing looks consider wrapping this look into a filter look
278      * which will switch of properties and actions.
279      * <P>
280      * Standard usage of lookSwitcherLook is to use it in decorator
281      * on root node of your browser.
282      * <P>
283      * To create this look from an XML layer type:
284      * <pre>
285      * &lt;file name="NameOfYourLook.instance" &gt;
286      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.Look" /&gt;
287      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Looks.lookSwitcherLook" /&gt;
288      * &lt;/file&gt;
289      * </pre>
290      * @see #filter(String,Look,long)
291      * @see #decorator(org.netbeans.spi.looks.LookSelector, org.netbeans.spi.looks.Look, boolean)
292      * @return Look which adds LookNode into NodeSubstitute's lookup and
293      * which provides action and property for switching looks.
294      */

295
296      // !Uncoment javadoc also
297
/*
298       public static final Look lookSwitcher() {
299         return LOOK_SWITCHER_LOOK;
300       }
301      */

302
303     public static final Look childrenSelectorProvider( String JavaDoc name, Look look, LookSelector selector ) {
304         return new org.netbeans.modules.looks.ChildrenSelectorProvider( name, look, selector );
305     }
306     
307
308     // Package private helper methods used from Selectors class ----------------
309

310     static Boolean JavaDoc readBooleanAttribute(FileObject fo, String JavaDoc attribute) throws IOException JavaDoc {
311         Object JavaDoc value = fo.getAttribute (attribute);
312         if (value == null)
313             return null;
314         if (value instanceof Boolean JavaDoc)
315             return (Boolean JavaDoc)value;
316         else
317             throw new IOException JavaDoc ("Attribute " + attribute + " is not Boolean but: " + value); // NOI18N
318
}
319
320     static String JavaDoc readStringAttribute(FileObject fo, String JavaDoc attribute) throws IOException JavaDoc {
321         Object JavaDoc value = fo.getAttribute (attribute);
322         if (value == null)
323             return null;
324         if (value instanceof String JavaDoc)
325             return (String JavaDoc)value;
326         else
327             throw new IOException JavaDoc ("Attribute " + attribute + " is not String but: " + value); // NOI18N
328
}
329
330
331     static Object JavaDoc readLookOrSelectorAttribute (FileObject fo, String JavaDoc attribute, boolean look) throws IOException JavaDoc {
332         Object JavaDoc value = fo.getAttribute (attribute);
333         if (look) {
334             if (value instanceof Look) {
335                 return value;
336             }
337         } else {
338             if (value instanceof LookSelector) {
339                 return value;
340             }
341         }
342
343         if (!(value instanceof String JavaDoc)) {
344             throw new IOException JavaDoc ("Attribute " + attribute + " is not String but: " + value); // NOI18N
345
}
346
347         // lookup delegate
348
Object JavaDoc delegate = RegistryBridge.getDefault( fo ).resolve ((String JavaDoc)value);
349         if (look) {
350             if (delegate instanceof Look) {
351                 return delegate;
352             }
353         }
354         else {
355             if (delegate instanceof LookSelector) {
356                 return delegate;
357             }
358         }
359         
360         // The delegate is either null or has a wrong class
361

362         IOException JavaDoc newEx = new IOException JavaDoc (
363             "Look/LookSelector " + value + " not found. Attribute " + attribute + " on " + fo); // NOI18N
364

365         throw newEx;
366     }
367
368     static Look readLookAttribute (FileObject fo, String JavaDoc attribute) throws IOException JavaDoc {
369         return (Look)readLookOrSelectorAttribute (fo, attribute, true);
370     }
371
372
373     static private final void initFilterMethods () {
374         mapFilterMethods.put ("GET_LOOKUP_ITEMS", new Long JavaDoc (Look.GET_LOOKUP_ITEMS)); // NOI18N
375
mapFilterMethods.put ("GET_NAME", new Long JavaDoc (Look.GET_NAME)); // NOI18N
376
mapFilterMethods.put ("RENAME", new Long JavaDoc (Look.RENAME)); // NOI18N
377
mapFilterMethods.put ("GET_DISPLAY_NAME", new Long JavaDoc (Look.GET_DISPLAY_NAME)); // NOI18N
378
mapFilterMethods.put ("GET_SHORT_DESCRIPTION", new Long JavaDoc (Look.GET_SHORT_DESCRIPTION)); // NOI18N
379
mapFilterMethods.put ("GET_ICON", new Long JavaDoc (Look.GET_ICON)); // NOI18N
380
mapFilterMethods.put ("GET_OPENED_ICON", new Long JavaDoc (Look.GET_OPENED_ICON)); // NOI18N
381
mapFilterMethods.put ("GET_HELP_CTX", new Long JavaDoc (Look.GET_HELP_CTX)); // NOI18N
382
mapFilterMethods.put ("GET_CHILD_OBJECTS", new Long JavaDoc (Look.GET_CHILD_OBJECTS)); // NOI18N
383
mapFilterMethods.put ("GET_NEW_TYPES", new Long JavaDoc (Look.GET_NEW_TYPES)); // NOI18N
384
mapFilterMethods.put ("GET_ACTIONS", new Long JavaDoc (Look.GET_ACTIONS)); // NOI18N
385
mapFilterMethods.put ("GET_CONTEXT_ACTIONS", new Long JavaDoc (Look.GET_CONTEXT_ACTIONS)); // NOI18N
386
mapFilterMethods.put ("GET_DEFAULT_ACTION", new Long JavaDoc (Look.GET_DEFAULT_ACTION)); // NOI18N
387
mapFilterMethods.put ("GET_PROPERTY_SETS", new Long JavaDoc (Look.GET_PROPERTY_SETS)); // NOI18N
388
mapFilterMethods.put ("GET_CUSTOMIZER", new Long JavaDoc (Look.GET_CUSTOMIZER)); // NOI18N
389
mapFilterMethods.put ("CAN_RENAME", new Long JavaDoc (Look.CAN_RENAME)); // NOI18N
390
mapFilterMethods.put ("CAN_DESTROY", new Long JavaDoc (Look.CAN_DESTROY)); // NOI18N
391
mapFilterMethods.put ("CAN_COPY", new Long JavaDoc (Look.CAN_COPY)); // NOI18N
392
mapFilterMethods.put ("CAN_CUT", new Long JavaDoc (Look.CAN_CUT)); // NOI18N
393
mapFilterMethods.put ("GET_PASTE_TYPES", new Long JavaDoc (Look.GET_PASTE_TYPES)); // NOI18N
394
mapFilterMethods.put ("GET_DROP_TYPE", new Long JavaDoc (Look.GET_DROP_TYPE)); // NOI18N
395
mapFilterMethods.put ("CLIPBOARD_COPY", new Long JavaDoc (Look.CLIPBOARD_COPY)); // NOI18N
396
mapFilterMethods.put ("CLIPBOARD_CUT", new Long JavaDoc (Look.CLIPBOARD_CUT)); // NOI18N
397
mapFilterMethods.put ("DRAG", new Long JavaDoc (Look.DRAG)); // NOI18N
398
mapFilterMethods.put ("DESTROY", new Long JavaDoc (Look.DESTROY)); // NOI18N
399
mapFilterMethods.put ("HAS_CUSTOMIZER", new Long JavaDoc (Look.HAS_CUSTOMIZER)); // NOI18N
400
// mapFilterMethods.put ("ALL_METHODS", new Long (Look.ALL_METHODS)); // NOI18N
401
// mapFilterMethods.put ("NO_METHODS", new Long (Look.NO_METHODS)); // NOI18N
402
}
403
404 }
405
Popular Tags