KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > AnnotationTypesNode


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.modules.editor.options;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import org.openide.util.HelpCtx;
25 import org.openide.util.actions.SystemAction;
26 import org.openide.actions.PropertiesAction;
27 import org.openide.nodes.Children;
28 import org.netbeans.modules.editor.options.AnnotationTypesFolder;
29 import org.netbeans.editor.AnnotationType;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.BeanNode;
32 import org.openide.nodes.Node;
33 import java.util.Iterator JavaDoc;
34 import java.beans.IntrospectionException JavaDoc;
35 import org.openide.util.NbBundle;
36 import org.openide.nodes.PropertySupport;
37 import org.openide.nodes.Sheet;
38 import org.netbeans.editor.AnnotationTypes;
39 import java.lang.Boolean JavaDoc;
40 import java.beans.PropertyChangeListener JavaDoc;
41 import java.beans.PropertyChangeEvent JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.List JavaDoc;
45 import org.openide.ErrorManager;
46 import org.openide.util.Utilities;
47
48 /** Node representing the Annotation Types in Options window.
49  *
50  * @author David Konecny
51  * @since 07/2001
52  */

53 public class AnnotationTypesNode extends AbstractNode {
54
55     private static final String JavaDoc HELP_ID = "editing.configuring.annotations"; // !!! NOI18N
56
private static final String JavaDoc ICON_BASE = "org/netbeans/modules/editor/resources/annotationtypes"; // NOI18N
57

58     
59     /** Creates new AnnotationTypesNode */
60     public AnnotationTypesNode() {
61         super(new AnnotationTypesSubnodes ());
62         setName("annotationtypes"); // NOI18N
63
setDisplayName(getBundleString("ATN_AnnotationTypesNode_Name")); // NOI18N
64
setShortDescription (getBundleString("ATN_AnnotationTypesNode_Description")); // NOI18N
65
setIconBase (ICON_BASE);
66     }
67     
68     private String JavaDoc getBundleString(String JavaDoc s) {
69         return NbBundle.getMessage(AnnotationTypesNode.class, s);
70     }
71
72     public HelpCtx getHelpCtx () {
73         return new HelpCtx (HELP_ID);
74     }
75     
76     protected SystemAction[] createActions () {
77         return new SystemAction[] {
78                    SystemAction.get (PropertiesAction.class),
79                };
80     }
81
82     /** Create properties sheet */
83     protected Sheet createSheet() {
84         Sheet sheet = super.createSheet();
85         
86     Sheet.Set ps = sheet.get (Sheet.PROPERTIES);
87     if (ps == null) {
88         ps = Sheet.createPropertiesSet ();
89     }
90         
91         ps.put(createProperty(AnnotationTypes.PROP_BACKGROUND_DRAWING, boolean.class)); //NOI18N
92
ps.put(createProperty(AnnotationTypes.PROP_BACKGROUND_GLYPH_ALPHA, int.class)); //NOI18N
93
ps.put(createProperty(AnnotationTypes.PROP_COMBINE_GLYPHS, boolean.class)); //NOI18N
94
ps.put(createProperty(AnnotationTypes.PROP_GLYPHS_OVER_LINE_NUMBERS, boolean.class)); //NOI18N
95
ps.put(createProperty(AnnotationTypes.PROP_SHOW_GLYPH_GUTTER, boolean.class)); //NOI18N
96
sheet.put(ps);
97         
98         return sheet;
99     }
100     
101     /** Create PropertySupport for given property name and class */
102     private PropertySupport createProperty(final String JavaDoc name, final Class JavaDoc clazz) {
103         return new PropertySupport.ReadWrite(name, clazz,
104             getBundleString("PROP_" + name), //NOI18N
105
getBundleString("HINT_" + name)) { //NOI18N
106
public Object JavaDoc getValue() {
107                 return getProperty(name);
108             }
109             public void setValue(Object JavaDoc value) {
110                 setProperty(name, value);
111             }
112             public boolean supportsDefaultValue() {
113                 return false;
114             }
115         };
116     }
117
118     /** General setter */
119     private void setProperty(String JavaDoc property, Object JavaDoc value) {
120         if (property.equals(AnnotationTypes.PROP_BACKGROUND_DRAWING))
121             AnnotationTypes.getTypes().setBackgroundDrawing((Boolean JavaDoc)value);
122         if (property.equals(AnnotationTypes.PROP_BACKGROUND_GLYPH_ALPHA))
123             AnnotationTypes.getTypes().setBackgroundGlyphAlpha(((Integer JavaDoc)value).intValue());
124         if (property.equals(AnnotationTypes.PROP_COMBINE_GLYPHS))
125             AnnotationTypes.getTypes().setCombineGlyphs((Boolean JavaDoc)value);
126         if (property.equals(AnnotationTypes.PROP_GLYPHS_OVER_LINE_NUMBERS))
127             AnnotationTypes.getTypes().setGlyphsOverLineNumbers((Boolean JavaDoc)value);
128         if (property.equals(AnnotationTypes.PROP_SHOW_GLYPH_GUTTER))
129             AnnotationTypes.getTypes().setShowGlyphGutter((Boolean JavaDoc)value);
130     }
131
132     /** General getter*/
133     private Object JavaDoc getProperty(String JavaDoc property) {
134         if (property.equals(AnnotationTypes.PROP_BACKGROUND_DRAWING))
135             return AnnotationTypes.getTypes().isBackgroundDrawing();
136         if (property.equals(AnnotationTypes.PROP_BACKGROUND_GLYPH_ALPHA))
137             return AnnotationTypes.getTypes().getBackgroundGlyphAlpha();
138         if (property.equals(AnnotationTypes.PROP_COMBINE_GLYPHS))
139             return AnnotationTypes.getTypes().isCombineGlyphs();
140         if (property.equals(AnnotationTypes.PROP_GLYPHS_OVER_LINE_NUMBERS))
141             return AnnotationTypes.getTypes().isGlyphsOverLineNumbers();
142         if (property.equals(AnnotationTypes.PROP_SHOW_GLYPH_GUTTER))
143             return AnnotationTypes.getTypes().isShowGlyphGutter();
144         
145         return null;
146     }
147     
148     /** Class representing subnodes of AnnotationType node.*/
149     private static class AnnotationTypesSubnodes extends Children.Array {
150
151         /** Listener on add/remove of annotation type. */
152         private PropertyChangeListener JavaDoc listener;
153         
154         public AnnotationTypesSubnodes() {
155             super();
156             AnnotationTypes.getTypes().addPropertyChangeListener( listener = new PropertyChangeListener JavaDoc() {
157                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
158                     if (evt.getPropertyName() == AnnotationTypes.PROP_ANNOTATION_TYPES) {
159                         AnnotationTypesSubnodes.this.nodes = initCollection();
160                         refresh();
161                     }
162                 }
163             });
164         }
165         
166         /** Initialize the collection with results of parsing of "Editors/AnnotationTypes" directory */
167         protected java.util.Collection JavaDoc initCollection() {
168             
169             AnnotationTypesFolder folder = AnnotationTypesFolder.getAnnotationTypesFolder();
170
171             Iterator JavaDoc types = AnnotationTypes.getTypes().getAnnotationTypeNames();
172
173             java.util.List JavaDoc list = new java.util.LinkedList JavaDoc();
174
175             for( ; types.hasNext(); ) {
176                 String JavaDoc name = (String JavaDoc)types.next();
177                 AnnotationType type = AnnotationTypes.getTypes().getType(name);
178                 if (type == null || !type.isVisible())
179                     continue;
180                 try {
181                     list.add(new AnnotationTypesSubnode(type));
182                 } catch (IntrospectionException JavaDoc e) {
183                     ErrorManager.getDefault().notify(e);
184                     continue;
185                 }
186             }
187
188             return list;
189         }
190         
191         // Cf. #7925, though not quite the same.
192
private static final class AnnotationTypesSubnode extends BeanNode {
193             private final URL JavaDoc iconURL;
194
195             public AnnotationTypesSubnode(AnnotationType type) throws IntrospectionException JavaDoc {
196                 super(new AnnotationTypeOptions(type));
197                 setName(type.getDescription());
198                 iconURL = type.getGlyph();
199             }
200             
201             public Image JavaDoc getIcon(int type) {
202                 // Utilities.loadImage does not handle URLs.
203
// Toolkit.getImage would work, but U.lI does nicer caching.
204
if (iconURL.getProtocol().equals("nbresloc")) { // NOI18N
205
return Utilities.loadImage(iconURL.getPath().substring(1));
206                 } else {
207                     return Toolkit.getDefaultToolkit().getImage(iconURL);
208                 }
209             }
210             
211             public boolean canDestroy() {
212                 return false;
213             }
214             public HelpCtx getHelpCtx() {
215                 return new HelpCtx(HELP_ID);
216             }
217             
218         }
219     }
220
221 }
222
Popular Tags