KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > nodes > SourceOptions


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.openide.src.nodes;
21
22 import java.util.ResourceBundle JavaDoc;
23
24 import org.openide.src.*;
25 import org.openide.options.SystemOption;
26 import org.openide.util.HelpCtx;
27 import org.openide.util.NbBundle;
28
29 /*
30 * TODO:
31 * <UL>
32 * <LI> weak listeners for listening on format changes - all element nodes should react on it.
33 * </UL>
34 */

35 /** Display options for the hierarchy of source elements.
36 * These options determine the display name format
37 * of each kind of element.
38 * <p>Also included are read-only properties for the "long formats",
39 * which are in practice used for {@link ElementNode#getHintElementFormat}.
40 * <p>Changes to settings will fire property change events.
41 *
42 * @author Petr Hamernik
43 */

44 public final class SourceOptions extends SystemOption {
45     private static final int lastCompatibleVersionTag = 1;
46     private static final int currentVersionTag = 1;
47     
48     /** Resource bundle. */
49     private static ResourceBundle JavaDoc bundle;
50
51     /** Kinds of the format. */
52     private static final byte T_INITIALIZER = 0;
53     private static final byte T_FIELD = 1;
54     private static final byte T_CONSTRUCTOR = 2;
55     private static final byte T_METHOD = 3;
56     private static final byte T_CLASS = 4;
57     private static final byte T_INTERFACE = 5;
58
59     /** Names of all properties. */
60     static final String JavaDoc[] PROP_NAMES = {
61         "initializerElementFormat", "fieldElementFormat", // NOI18N
62
"constructorElementFormat", "methodElementFormat", // NOI18N
63
"classElementFormat", "interfaceElementFormat" // NOI18N
64
};
65     
66     static Element[] TEST_ELEMENTS;
67
68     /** default values for the formats - short form. */
69     private static final ElementFormat[] DEFAULT_FORMATS_SHORT = new ElementFormat[6];
70
71     /** default values for the formats - long form. */
72     private static final ElementFormat[] DEFAULT_FORMATS_LONG = new ElementFormat[6];
73
74     /**
75      * Current format for individual element types, or null if the format is
76      * not yet specified by the user.
77      */

78     private ElementFormat[] formats = new ElementFormat[6];
79     
80     /**
81      * Version tag to use;
82      */

83     private int version;
84     
85     private static synchronized void loadBundle() {
86         if (bundle != null)
87             return;
88         bundle = NbBundle.getBundle(SourceOptions.class);
89     }
90     
91     private static void loadDefaultFormats() {
92         if (DEFAULT_FORMATS_SHORT[0] != null)
93             return;
94         synchronized (SourceOptions.class) {
95             if (DEFAULT_FORMATS_SHORT[0] != null)
96                 return;
97             loadBundle();
98             for (int i = 0; i < 6; i++) {
99                 DEFAULT_FORMATS_SHORT[i] = new ElementFormat(bundle.getString("SHORT_"+PROP_NAMES[i]));
100                 DEFAULT_FORMATS_LONG[i] = new ElementFormat(bundle.getString("LONG_"+PROP_NAMES[i]));
101             }
102         }
103     }
104     
105     /**
106      * Resets all element formats to their default values.
107      */

108     private void clearElementFormats() {
109         formats = new ElementFormat[6];
110     }
111
112     /** Property name of the initializer display format. */
113     public static final String JavaDoc PROP_INITIALIZER_FORMAT = PROP_NAMES[T_INITIALIZER];
114
115     /** Property name of the field display format. */
116     public static final String JavaDoc PROP_FIELD_FORMAT = PROP_NAMES[T_FIELD];
117
118     /** Property name of the constructor display format. */
119     public static final String JavaDoc PROP_CONSTRUCTOR_FORMAT = PROP_NAMES[T_CONSTRUCTOR];
120
121     /** Property name of the method display format. */
122     public static final String JavaDoc PROP_METHOD_FORMAT = PROP_NAMES[T_METHOD];
123
124     /** Property name of the class display format. */
125     public static final String JavaDoc PROP_CLASS_FORMAT = PROP_NAMES[T_CLASS];
126
127     /** Property name of the interface display format. */
128     public static final String JavaDoc PROP_INTERFACE_FORMAT = PROP_NAMES[T_INTERFACE];
129
130     /** Property name of the 'categories usage' property. */
131     public static final String JavaDoc PROP_CATEGORIES_USAGE = "categoriesUsage"; // NOI18N
132

133     /** CategoriesUsage property current value */
134     private static boolean categories = true;
135
136     static final long serialVersionUID =-2120623049071035434L;
137
138     /** @return display name
139     */

140     public String JavaDoc displayName () {
141         loadBundle();
142         return bundle.getString("MSG_sourceOptions");
143     }
144
145     public HelpCtx getHelpCtx () {
146         return new HelpCtx (SourceOptions.class);
147     }
148
149     // ============= public methods ===================
150

151     /** Set the initializer format.
152     * @param format the new format
153     */

154     public void setInitializerElementFormat(ElementFormat format) {
155         setElementFormat(T_INITIALIZER, format);
156     }
157
158     /** Get the initializer format.
159     * @return the current format
160     */

161     public ElementFormat getInitializerElementFormat() {
162         return getElementFormat(T_INITIALIZER);
163     }
164
165     /** Set the field format.
166     * @param format the new format
167     */

168     public void setFieldElementFormat(ElementFormat format) {
169         setElementFormat(T_FIELD, format);
170     }
171     
172     private ElementFormat getElementFormat(int type) {
173         synchronized (this) {
174             if (formats[type] != null)
175                 return formats[type];
176             // if writing the option to the disk, return a default == null value.
177
if (isWriteExternal())
178                 return null;
179         }
180         loadDefaultFormats();
181         return DEFAULT_FORMATS_SHORT[type];
182     }
183
184     /** Get the field format.
185     * @return the current format
186     */

187     public ElementFormat getFieldElementFormat() {
188         return getElementFormat(T_FIELD);
189     }
190
191     /** Set the constructor format.
192     * @param format the new format
193     */

194     public void setConstructorElementFormat(ElementFormat format) {
195         setElementFormat(T_CONSTRUCTOR, format);
196     }
197
198     /** Get the constructor format.
199     * @return the current format
200     */

201     public ElementFormat getConstructorElementFormat() {
202         return getElementFormat(T_CONSTRUCTOR);
203     }
204
205     /** Set the method format.
206     * @param format the new format
207     */

208     public void setMethodElementFormat(ElementFormat format) {
209         setElementFormat(T_METHOD, format);
210     }
211
212     /** Get the method format.
213     * @return the current format
214     */

215     public ElementFormat getMethodElementFormat() {
216         return getElementFormat(T_METHOD);
217     }
218
219     /** Set the class format.
220     * @param format the new format
221     */

222     public void setClassElementFormat(ElementFormat format) {
223         setElementFormat(T_CLASS, format);
224     }
225
226     /** Get the class format.
227     * @return the current format
228     */

229     public ElementFormat getClassElementFormat() {
230         return getElementFormat(T_CLASS);
231     }
232
233     /** Set the interface format.
234     * @param format the new format
235     */

236     public void setInterfaceElementFormat(ElementFormat format) {
237         setElementFormat(T_INTERFACE, format);
238     }
239
240     /** Get the interface format.
241     * @return the current format
242     */

243     public ElementFormat getInterfaceElementFormat() {
244         return getElementFormat(T_INTERFACE);
245     }
246
247     // ============= getters for long form of formats =================
248

249     /** Get the initializer format for longer hints.
250     * @return the current format
251     */

252     public ElementFormat getInitializerElementLongFormat() {
253         loadDefaultFormats();
254         return DEFAULT_FORMATS_LONG[T_INITIALIZER];
255     }
256
257     /** Get the field format for longer hints.
258     * @return the current format
259     */

260     public ElementFormat getFieldElementLongFormat() {
261         loadDefaultFormats();
262         return DEFAULT_FORMATS_LONG[T_FIELD];
263     }
264
265     /** Get the constructor format for longer hints.
266     * @return the current format
267     */

268     public ElementFormat getConstructorElementLongFormat() {
269         loadDefaultFormats();
270         return DEFAULT_FORMATS_LONG[T_CONSTRUCTOR];
271     }
272
273     /** Get the method format for longer hints.
274     * @return the current format
275     */

276     public ElementFormat getMethodElementLongFormat() {
277         loadDefaultFormats();
278         return DEFAULT_FORMATS_LONG[T_METHOD];
279     }
280
281     /** Get the class format for longer hints.
282     * @return the current format
283     */

284     public ElementFormat getClassElementLongFormat() {
285         loadDefaultFormats();
286         return DEFAULT_FORMATS_LONG[T_CLASS];
287     }
288
289     /** Get the interface format for longer hints.
290     * @return the current format
291     */

292     public ElementFormat getInterfaceElementLongFormat() {
293         loadDefaultFormats();
294         return DEFAULT_FORMATS_LONG[T_INTERFACE];
295     }
296
297     // ============= categories of elements usage ===================
298

299     /** Set the property whether categories under class elements should be used or not.
300     * @param cat if <CODE>true</CODE> the elements under class elements are divided into
301     * categories: fields, constructors, methods. Otherwise (<CODE>false</CODE>) all elements
302     * are placed directly under class element.
303     */

304     public void setCategoriesUsage(boolean cat) {
305         categories = cat;
306     }
307
308     /** Test whether categiries under class elements are used or not.
309     * @return <CODE>true</CODE> if the elements under class elements are divided into
310     * categories: fields, constructors, methods. Otherwise <CODE>false</CODE> (all elements
311     * are placed directly under class element).
312     */

313     public boolean getCategoriesUsage() {
314         return categories;
315     }
316
317     // ============= private methods ===================
318

319     private synchronized static Element getTestElement(int index) {
320         if (TEST_ELEMENTS == null) {
321             Element[] els = new Element[6];
322             
323             try {
324                 els[T_INITIALIZER] = new InitializerElement();
325                 FieldElement f = new FieldElement();
326                 Identifier id = Identifier.create("foo"); // NOI18N
327
f.setName(id); // NOI18N
328
f.setType(Type.INT);
329                 els[T_FIELD] = f;
330
331                 MethodElement m = new MethodElement();
332                 m.setName(id);
333                 m.setReturn(Type.VOID);
334                 els[T_METHOD] = m;
335                 els[T_CONSTRUCTOR] = new ConstructorElement();
336
337                 ClassElement c = new ClassElement();
338                 c.setName(id);
339                 els[T_CLASS] = els[T_INTERFACE] = c;
340                 TEST_ELEMENTS = els;
341             } catch (SourceException ex) {
342                 // cannot happen.
343
}
344         }
345         return TEST_ELEMENTS[index];
346     }
347         
348     /** Sets the format for the given index.
349     * @param index One of the constants T_XXX
350     * @param format the new format for the specific type.
351     */

352     private void setElementFormat(byte index, ElementFormat format) {
353         ElementFormat old = formats[index];
354         if (format != null) {
355             // check whether the format is valid for the element type:
356
Element el = getTestElement(index);
357             try {
358                 format.format(el);
359             } catch (IllegalArgumentException JavaDoc iae) {
360                 throw (IllegalArgumentException JavaDoc)
361                     org.openide.ErrorManager.getDefault().annotate(
362                         iae, org.openide.ErrorManager.USER, null,
363                         bundle.getString("MSG_IllegalElementFormat"), // NOI18N
364
null, null);
365             }
366         }
367         formats[index] = format;
368         firePropertyChange (PROP_NAMES[index], old, formats[index]);
369     }
370     
371     public void writeExternal(java.io.ObjectOutput JavaDoc out) throws java.io.IOException JavaDoc {
372         super.writeExternal(out);
373         out.writeInt(version);
374     }
375     
376     public void readExternal (java.io.ObjectInput JavaDoc in)
377     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
378         super.readExternal(in);
379         if (in.available() > 0) {
380         version = in.readInt();
381         }
382         if (version < lastCompatibleVersionTag) {
383             clearElementFormats();
384             version = currentVersionTag;
385         }
386     }
387 }
388
Popular Tags