KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > AnnotationTypes


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
21 package org.netbeans.editor;
22
23 import java.net.URL JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import org.netbeans.editor.Settings;
29 import org.netbeans.editor.WeakPropertyChangeSupport;
30 import javax.swing.SwingUtilities JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33
34 /** Registry of all annotation types. This is singleton and it also keeps
35  * the settings whether the annotation types which are not active are
36  * drawn on the background, whther the combining of annotations is
37  * turned on or off etc. These settings are shared by all views.
38  *
39  * @author David Konecny
40  * @since 08/2001
41  */

42
43 public class AnnotationTypes {
44
45     /** Whether the pasive glyphs are drawn on the background under the text (boolean). */
46     public static final String JavaDoc PROP_BACKGROUND_DRAWING = "backgroundDrawing"; // NOI18N
47

48     /** The alpha of the pasive glyphs drawn on the background under the text (int 0..100%). */
49     public static final String JavaDoc PROP_BACKGROUND_GLYPH_ALPHA = "backgroundGlyphAlpha"; // NOI18N
50

51     /** Whether the glyphs should be combined according to combination annotation types (boolean). */
52     public static final String JavaDoc PROP_COMBINE_GLYPHS = "combineGlyphs"; // NOI18N
53

54     /** Whether the glyph icon should be drawn over the line numbers (boolean). */
55     public static final String JavaDoc PROP_GLYPHS_OVER_LINE_NUMBERS = "glyphsOverLineNumbers"; // NOI18N
56

57     /** Whether the glyph gutter should be shown after opening editor or not (boolean).
58      * If this value is set to False, the gutter will automatically show after first annotation
59      * has been added.*/

60     public static final String JavaDoc PROP_SHOW_GLYPH_GUTTER = "showGlyphGutter"; // NOI18N
61

62     /** Property which is fired when list of annotation types is changing. */
63     public static final String JavaDoc PROP_ANNOTATION_TYPES = "annotationTypes"; // NOI18N
64

65     /** Storage of all properties. */
66     private Map JavaDoc properties;
67
68     /** Support for property change listeners*/
69     private WeakPropertyChangeSupport support;
70     
71     /** Static map containing all annotation types: annotation_name <-> annotation_type */
72     private Map JavaDoc allTypes = null;
73     
74     /** Flag whether the annotation types were loaded or not */
75     private boolean loadedTypes = false;
76
77     /** Flag whether the properties of this class were loaded or not */
78     private boolean loadedSettings = false;
79     
80     /** Flag whether the loading of properties is in progress */
81     private boolean loadingInProgress = false;
82     
83     /** Loader for loading annotation types from a storage*/
84     private Loader loader = null;
85     
86     /** URL to the default glyph icon */
87     private static URL JavaDoc defaultGlyphIcon = null;
88
89     /** Single instance of this class */
90     private static AnnotationTypes annoTypes = null;
91     
92     private AnnotationTypes() {
93         properties = new HashMap JavaDoc(5*4/3);
94         support = new WeakPropertyChangeSupport();
95     }
96
97     /** Returns instance of AnnotationTypes singleton. */
98     public static AnnotationTypes getTypes() {
99         if (annoTypes == null) {
100             annoTypes = new AnnotationTypes();
101         }
102         return annoTypes;
103     }
104     
105     /** Gets Image which represents the default glyph icon. It is
106      * used in case the annotation type does not have its own icon. */

107     public static URL JavaDoc getDefaultGlyphURL() {
108         if (defaultGlyphIcon == null) {
109             try {
110                 // TODO: in standalone this will not work
111
defaultGlyphIcon = new URL JavaDoc("nbresloc:/org/netbeans/editor/resources/defaultglyph.gif"); // NOI18N
112
} catch (MalformedURLException JavaDoc ex) {
113                 Utilities.annotateLoggable(ex);
114             }
115         }
116         
117         return defaultGlyphIcon;
118     }
119
120     /** Getter for BackgroundDrawing property
121      * @return whether the background drawing should be used or not */

122     public Boolean JavaDoc isBackgroundDrawing() {
123         loadSettings();
124         
125         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_BACKGROUND_DRAWING);
126         if (b == null)
127             return Boolean.FALSE;
128         return b;
129     }
130
131     /** Setter for the BackgroundDrawing property
132      * @param drawing use background drawing or not */

133     public void setBackgroundDrawing(Boolean JavaDoc drawing) {
134         if (!isBackgroundDrawing().equals(drawing)) {
135             putProp(PROP_BACKGROUND_DRAWING, drawing);
136             firePropertyChange(PROP_BACKGROUND_DRAWING, null, null);
137             // force repaint of all documents
138
Settings.touchValue(null, null);
139             saveSetting(PROP_BACKGROUND_DRAWING, drawing);
140         }
141     }
142     
143     /** Getter for CombineGlyphs property
144      * @return whether the combination annotation types are used or not */

145     public Boolean JavaDoc isCombineGlyphs() {
146         loadSettings();
147         
148         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_COMBINE_GLYPHS);
149         if (b == null)
150             return Boolean.TRUE;
151         return b;
152     }
153
154     /** Setter for the CombineGlyphs property
155      * @param combine combine annotation types */

156     public void setCombineGlyphs(Boolean JavaDoc combine) {
157         if (!isCombineGlyphs().equals(combine)) {
158             putProp(PROP_COMBINE_GLYPHS, combine);
159             firePropertyChange(PROP_COMBINE_GLYPHS, null, null);
160             // force repaint of all documents
161
Settings.touchValue(null, null);
162             saveSetting(PROP_COMBINE_GLYPHS, combine);
163         }
164     }
165     
166     /** Getter for BackgroundGlyphAlpha property
167      * @return percentage of alpha (0..100) */

168     public Integer JavaDoc getBackgroundGlyphAlpha() {
169         loadSettings();
170         
171         if (getProp(PROP_BACKGROUND_GLYPH_ALPHA) == null)
172             return new Integer JavaDoc(40);
173         return (Integer JavaDoc)getProp(PROP_BACKGROUND_GLYPH_ALPHA);
174     }
175
176     /** Setter for the BackgroundGlyphAlpha property
177      * @param alpha alpha value in percentage (0..100) */

178     public void setBackgroundGlyphAlpha(int alpha) {
179         if (alpha < 0 || alpha > 100) {
180             return;
181         }
182         Integer JavaDoc i = new Integer JavaDoc(alpha);
183         putProp(PROP_BACKGROUND_GLYPH_ALPHA, i);
184         firePropertyChange(PROP_BACKGROUND_GLYPH_ALPHA, null, null);
185         // force repaint of all documents
186
Settings.touchValue(null, null);
187         saveSetting(PROP_BACKGROUND_GLYPH_ALPHA, i);
188     }
189
190     /** Getter for GlyphsOverLineNumbers property
191      * @return whether the glyph should be drawn over the line numbers */

192     public Boolean JavaDoc isGlyphsOverLineNumbers() {
193         loadSettings();
194         
195         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_GLYPHS_OVER_LINE_NUMBERS);
196         if (b == null)
197             return Boolean.TRUE;
198         return b;
199     }
200
201     /** Setter for the GlyphsOverLineNumbers property
202      * @param over draw the glyphd over the line numbers */

203     public void setGlyphsOverLineNumbers(Boolean JavaDoc over) {
204         if (!isGlyphsOverLineNumbers().equals(over)) {
205             putProp(PROP_GLYPHS_OVER_LINE_NUMBERS, over);
206             firePropertyChange(PROP_GLYPHS_OVER_LINE_NUMBERS, null, null);
207             saveSetting(PROP_GLYPHS_OVER_LINE_NUMBERS, over);
208         }
209     }
210     
211     /** Getter for ShowGlyphGutter property
212      * @return whether the glyph should be shown after opening editor or not */

213     public Boolean JavaDoc isShowGlyphGutter() {
214         loadSettings();
215         
216         Boolean JavaDoc b = (Boolean JavaDoc)getProp(PROP_SHOW_GLYPH_GUTTER);
217         if (b == null)
218             return Boolean.TRUE;
219         return b;
220     }
221
222     /** Setter for the ShowGlyphGutter property
223      * @param gutter show gutter */

224     public void setShowGlyphGutter(Boolean JavaDoc gutter) {
225         if (!isShowGlyphGutter().equals(gutter)) {
226             putProp(PROP_SHOW_GLYPH_GUTTER, gutter);
227             firePropertyChange(PROP_SHOW_GLYPH_GUTTER, null, null);
228             saveSetting(PROP_SHOW_GLYPH_GUTTER, gutter);
229         }
230     }
231     
232     /** Gets property for appropriate string value */
233     private Object JavaDoc getProp(String JavaDoc prop){
234         return properties.get(prop);
235     }
236     
237     /** Puts property to Map */
238     private void putProp(Object JavaDoc key, Object JavaDoc value){
239         if (value == null) {
240             properties.remove(key);
241             return;
242         }
243         properties.put(key,value);
244     }
245     
246     
247     /** Add listeners on changes of annotation type properties
248      * @param l change listener*/

249     final public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
250         support.addPropertyChangeListener (l);
251     }
252     
253     /** Remove listeners on changes of annotation type properties
254      * @param l change listener*/

255     final public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
256         support.removePropertyChangeListener (l);
257     }
258
259     /** Fire property change to registered listeners. */
260     final protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
261         support.firePropertyChange(this, propertyName, oldValue, newValue);
262     }
263
264     /** Initialize the map of all annotation types
265      * @param map map containing all annotation types */

266     public final void setTypes(Map JavaDoc map) {
267         if (allTypes != null) {
268             allTypes = map;
269             // loading of annotation types is done in non-AWT thread
270
// while listeners on this change usually needs to update UI
271
// that's reason why the change is fired in AWT thread
272
SwingUtilities.invokeLater(new FirePropertyChange());
273         } else {
274             allTypes = map;
275         }
276     }
277
278     public final void removeType(String JavaDoc name) {
279         allTypes.remove(name);
280         SwingUtilities.invokeLater(new FirePropertyChange());
281     }
282     
283     /** Returns AnnotationType instance for the given name of the type
284      * @param name annotation type name
285      * @return instance describing annotation type */

286     public final AnnotationType getType(String JavaDoc name) {
287         loadTypes();
288         
289         AnnotationType ret = null;
290         if (allTypes != null){
291             ret = (AnnotationType)allTypes.get(name);
292         }
293         
294         if (ret == null){
295             Utilities.annotateLoggable(new NullPointerException JavaDoc("null AnnotationType for:"+name)); //NOI18N
296
}
297         
298         return ret;
299     }
300
301     /** Iterator of all annotation type names in the system */
302     public Iterator JavaDoc getAnnotationTypeNames() {
303         loadTypes();
304         Set JavaDoc temp = new HashSet JavaDoc();
305         if (allTypes != null)
306             temp.addAll(allTypes.keySet());
307         return temp.iterator();
308     }
309
310     /** Gets count of all annotation type names */
311     public int getAnnotationTypeNamesCount() {
312         loadTypes();
313         
314         return allTypes.keySet().size();
315     }
316     
317     /** Gets count of all visible annotation type names */
318     public int getVisibleAnnotationTypeNamesCount() {
319         loadTypes();
320         
321         Iterator JavaDoc i = getAnnotationTypeNames();
322         int count = 0;
323         for (; i.hasNext(); ) {
324             AnnotationType type = getType((String JavaDoc)i.next());
325             if (type == null)
326                 continue;
327             if (type.isVisible())
328                 count++;
329         }
330         return count;
331     }
332     
333     /** Register loader for loading of annotation types */
334     public void registerLoader(Loader l) {
335         loader = l;
336         loadedTypes = false;
337         loadedSettings = false;
338     }
339
340     /** Check if the types were loaded and load them if not */
341     private void loadTypes() {
342         if (loadedTypes || loader == null)
343             return;
344
345         loader.loadTypes();
346         
347         loadedTypes = true;
348     }
349     
350     /** Save changes in one annotation type */
351     public void saveType(AnnotationType type) {
352         if (!loadedTypes || loader == null)
353             return;
354         
355         loader.saveType(type);
356     }
357
358     /** Check if the settings were loaded and load them if not */
359     private void loadSettings() {
360         if (loadedSettings || loader == null || loadingInProgress)
361             return;
362
363         loadingInProgress = true;
364         loader.loadSettings();
365         loadingInProgress = false;
366         
367         loadedSettings = true;
368     }
369
370     /** Save change of property */
371     public void saveSetting(String JavaDoc settingName, Object JavaDoc value) {
372         if (!loadedSettings || loader == null)
373             return;
374         
375         loader.saveSetting(settingName, value);
376     }
377     
378     /** Loader of annotation types. The loader must be registered and is called
379      * when the annotation types data are queried. */

380     public interface Loader {
381
382         /** Load all annotation types data. */
383         public void loadTypes();
384
385         /** Load properties of this class. */
386         public void loadSettings();
387
388         /** Save one annotation type. */
389         public void saveType(AnnotationType type);
390
391         /** Save changed property of this class. */
392         public void saveSetting(String JavaDoc settingName, Object JavaDoc value);
393     }
394
395     /** This class is defined instead of two anonymous Runnables which are needed. */
396     private class FirePropertyChange implements Runnable JavaDoc {
397         FirePropertyChange() {
398         }
399         public void run() {
400             firePropertyChange(PROP_ANNOTATION_TYPES, null, null);
401         }
402     }
403     
404 }
405
Popular Tags