KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.filesystems.FileObject;
24 import org.netbeans.modules.looks.SelectorImplFactory;
25
26 /** Static factory class providing useful look selectors. For looks which
27  * can be constructed using <em>module layer</em>
28  * <code>XMLFileSystem</code>s, a sample of such XML definition is given
29  * in their Javadoc.
30  *
31  * @author Petr Hrebejk
32  */

33 public abstract class Selectors {
34
35     /** no instances */
36     private Selectors() {}
37
38     // Names of attributes
39
private static final String JavaDoc DECORATING_LOOK = "decoratingLook"; // NOI18N
40
private static final String JavaDoc CONTEXT = Looks.CONTEXT;
41     private static final String JavaDoc LOOK_SELECTOR = Looks.LOOK_SELECTOR;
42     // Default values
43
private static final String JavaDoc DEFAULT_CONTEXT = "Looks/Types/"; // NOI18N
44
// private static final String AS_LAST = "asLast"; // NOI18N
45
// private static final String EXCLUDABLE = "excludable"; // NOI18N
46

47     /* The only default selector in the system */
48     private static LookSelector DEFAULT_SELECTOR;
49     
50     // Metheods to be used from XML layers -------------------------------------
51

52     /** A method to be used from XML layers
53      */

54     static final LookSelector namespaceTypes(FileObject fo) throws IOException JavaDoc {
55         String JavaDoc contextPrefix = Looks.readStringAttribute (fo, CONTEXT);
56         if (contextPrefix == null)
57             contextPrefix = DEFAULT_CONTEXT;
58
59         org.netbeans.modules.looks.RegistryBridge rb =
60             org.netbeans.modules.looks.RegistryBridge.getDefault( fo );
61         
62         return new LookSelector( SelectorImplFactory.namespaceTypes( rb, contextPrefix ) );
63     }
64
65     /** A method to be used from XML layers
66      */

67     static final LookSelector decorator(FileObject fo) throws IOException JavaDoc {
68         return createDecoratorSelector (fo);
69     }
70
71     // Methods to be used from code --------------------------------------------
72

73     /** Creates a LookSelector based on LookProvider which will never change
74      * it's content.
75      * @see LookProvider
76      * @param provider The concrete implementation of finding the Looks
77      * @return LookSelector based on given LookProvider
78      */

79     public static synchronized final LookSelector selector( LookProvider provider ) {
80         return new LookSelector( SelectorImplFactory.provider( provider ) );
81     }
82
83     /** Creates a LookSelector which may change it's content.
84      * @see ChangeableLookProvider
85      * @param provider The concrete implementation of finding the Looks and
86      * firing changes of the LookSelector content
87      * @return LookSelector based on given LookProvider
88      */

89     public static synchronized final LookSelector selector( ChangeableLookProvider provider ) {
90         return new LookSelector( SelectorImplFactory.changeableProvider( provider ) );
91     }
92
93     /** Utility method, creates a LookSelector which will allways return
94      * the look put into this method as a parameter.
95      *
96      * @param delegate The Look to be returned from the selector.
97      * @return LookSelector returning Enumeration which contains given look.
98      */

99     public static final LookSelector singleton( Look delegate ) {
100         return new LookSelector( SelectorImplFactory.singleton( delegate ) );
101     }
102
103     /** Utility method, creates a LookSelector which will allways return
104      * the looks put into this method as a parameter.
105      *
106      * @param delegates Array of Looks to be returned from the selector.
107      * @return LookSelector returning Enumeration which contains given looks.
108      */

109     public static final LookSelector array( Look delegates[] ) {
110         return new LookSelector( SelectorImplFactory.array( delegates ) );
111     }
112
113     /** Utility method, creates a LookSelector which selects the first only Look from another
114      * LookSelector which it delegates to.
115      *
116      * @param delegate LookSelector from which the first Look only will be returned
117      * @return LookSelector returning Enumeration which contains the first Look selected by
118      * delegate selector for given represented object
119      */

120     public static final LookSelector first( LookSelector delegate ) {
121         return new LookSelector( SelectorImplFactory.first( delegate ) );
122     }
123
124     /** NamespaceSelector which searches for a look in default namespace
125      * "Looks/Types/".
126      *
127      * <P>
128      * To create this namespace selector from an XML layer type:
129      * <pre>
130      * &lt;file name="NameOfYourNamaspaceSelector.instance" &gt;
131      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.LookSelector" /&gt;
132      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Selectors.defaultTypes" /&gt;
133      * &lt;/file&gt;
134      * </pre>
135      * @see #namespaceTypes(String)
136      * @return default namespace selector searching for looks in "Looks/Types"
137      * on system filesystem.
138      */

139     public static synchronized final LookSelector defaultTypes() {
140         if (DEFAULT_SELECTOR == null) {
141             DEFAULT_SELECTOR = new LookSelector( SelectorImplFactory.namespaceTypes( DEFAULT_CONTEXT ) );
142         }
143         return DEFAULT_SELECTOR;
144     }
145
146     /** Searches for a look in namespace given name of context to search. Name
147      * of the look should correspond with a type (class, superclass, interface)
148      * of the represented object.
149      *
150      * <P>
151      * To create this namespace selector from an XML layer type:
152      * <pre>
153      * &lt;file name="NameOfYourNamaspaceSelector.instance" &gt;
154      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.LookSelector" /&gt;
155      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Selectors.namespaceTypes" /&gt;
156      * &lt;!--Optionally! The context prefix for the namespace. The default prefix is "Looks/Types/".--&gt;
157      * &lt;attr name="context" stringvalue="Looks/Selectors/MyTypes/" /&gt;
158      * &lt;/file&gt;
159      * </pre>
160      *
161      * @param contextPrefix the Context to be searched
162      * @return LookSelector searching the given Context by type of the
163      * represented object.
164      */

165     public static final LookSelector namespaceTypes( String JavaDoc contextPrefix ) {
166         return new LookSelector( SelectorImplFactory.namespaceTypes( contextPrefix ) );
167     }
168
169     // * &lt;!--Optionally! The additionally features are append at last. The default value is false.--$gt;
170
// * &lt;attr name="asLast" boolvalue="true" /&gt;
171
// * @param asLast If true the decoratingLook will be the second look in the
172
// * resulting composite looks. If true the decorating look will
173
// * occupy the first place.
174
// * @param excludable If set to true the decoration will stop on next
175
// * change of LookSelector in the nodes hierarchy.
176
// The excludable parameter determines
177
// * whether decorating should stop on next change of selector in the node
178
// * hierarchy. I.e. if the excludable parameter is set to <CODE>true</CODE>
179
// * then the decoration will stop as soon as some node in the nodes hierarchy
180
// * will heave some other LookSelector set. If parameter is set to <CODE>false</CODE>
181
// * then the decoration will be effective throughout the complete node hierarchy.
182

183     /** Decorates looks found by given selector, with a decorating look.
184      * Works exactly as the decorator. Resulting looks are composite
185      * looks made from the look found by the selector and the decorating look.
186      *
187      * <P>
188      * To create this decorator selector from an XML layer type:
189      * <pre>
190      * &lt;file name="NameOfYourDecoratorSelector.instance" &gt;
191      * &lt;attr name="instanceClass" stringvalue="org.netbeans.spi.looks.LookSelector" /&gt;
192      * &lt;attr name="instanceCreate" methodvalue="org.netbeans.spi.looks.Selectors.decorator" /&gt;
193      * &lt;!--The path to declaration of look selector searches for the looks.--&gt;
194      * &lt;attr name="lookSelector" stringvalue="Looks/Selectors/MyTypes/" /&gt;
195      * &lt;!--The path to declaration of decorating look.--&gt;
196      * &lt;attr name="decoratingLook" stringvalue="Looks/MyLayer/MyLooks/FooLook" /&gt;
197      * &lt;!--Optionally! The additionally features are append at last. The default value is false.--&gt;
198      * &lt;/file&gt;
199      * </pre>
200      *
201      * @param lookSelector Selector used for searching for Looks for given represented
202      * object
203      * @param decoratingLook Look which will decorate all the subnodes.
204      * @return LookSelector which adds the decorating look to every look provided
205      * in the parameter selector.
206      *
207      */

208     public static final LookSelector decorator( LookSelector lookSelector, Look decoratingLook ) {
209         return new LookSelector( SelectorImplFactory.decorator( lookSelector, decoratingLook, true, true ) );
210     }
211
212     /*
213     public static final LookSelector decorator( LookSelector selector, Look decoratingLook, boolean asLast, boolean excludable ) {
214         return new org.netbeans.modules.looks.DecoratorSelector( selector, decoratingLook, asLast, excludable );
215     }
216     */

217
218     /** Creates composite selector which merges all selectors given as
219      * parameter and removed duplicities.
220      * @param selectors Array of selctors to be merged.
221      * @return Selector which merges the selectors
222      */

223     public static final LookSelector composite( LookSelector[] selectors ) {
224         return new LookSelector( SelectorImplFactory.composite( selectors, true ) );
225     }
226
227     // Usefull look selectors may be published later ---------------------------
228

229
230     static synchronized final LookSelector selector( org.netbeans.modules.looks.NamespaceLookProvider provider, String JavaDoc prefix ) {
231         return new LookSelector( SelectorImplFactory.namespaceProvider( provider, prefix ) );
232     }
233
234
235     
236     // Private methods ---------------------------------------------------------
237

238     private static LookSelector createDecoratorSelector(FileObject fo) throws IOException JavaDoc {
239         Look decorator = Looks.readLookAttribute (fo, DECORATING_LOOK);
240         LookSelector selector = readLookSelectorAttribute (fo, LOOK_SELECTOR);
241
242         // proccess parameters
243
// 1. read asLast, default value is false
244
// 2. read excludable, default value is excludable
245
/*
246         Boolean helpBool = null;
247
248         helpBool = readBooleanAttribute (fo, AS_LAST);
249         boolean asLast = helpBool != null && helpBool.booleanValue () ? true : false;
250
251         helpBool = readBooleanAttribute (fo, EXCLUDABLE);
252         boolean excludable = helpBool != null && helpBool.booleanValue () ? true : false;
253
254         return new org.netbeans.modules.looks.DecoratorSelector (selector, decorator, asLast, excludable );
255         */

256         return new LookSelector( SelectorImplFactory.decorator( selector, decorator, true, true ) );
257     }
258
259     private static LookSelector readLookSelectorAttribute (FileObject fo, String JavaDoc attribute) throws IOException JavaDoc {
260         return (LookSelector)Looks.readLookOrSelectorAttribute (fo, attribute, false);
261     }
262 }
263
Popular Tags