KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.OutputStream JavaDoc;
23 import org.openide.ErrorManager;
24 import org.openide.loaders.FolderInstance;
25 import org.openide.cookies.InstanceCookie;
26 import java.lang.ClassNotFoundException JavaDoc;
27 import org.openide.loaders.DataFolder;
28 import org.openide.loaders.DataObject;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.lang.String JavaDoc;
33 import org.openide.filesystems.FileObject;
34 import org.netbeans.editor.AnnotationType;
35 import java.util.Iterator JavaDoc;
36 import org.netbeans.editor.AnnotationTypes;
37 import org.w3c.dom.*;
38 import org.openide.xml.XMLUtil;
39 import org.openide.filesystems.FileLock;
40 import java.lang.Exception JavaDoc;
41 import org.openide.filesystems.FileChangeAdapter;
42 import org.openide.filesystems.FileEvent;
43 import org.openide.filesystems.Repository;
44
45 /** Representation of the "Editors/AnnotationTypes" folder. All
46  * instances created through the createInstance() method are
47  * stored in Map and passed to AnnotationType.setTypes(). This
48  * class should only be responsible for processing of the folder,
49  * listening of the changes in folder etc. Clients should use
50  * AnnotationType.getType and other methods in AnnotationType
51  * for access to AnnotationTypes.
52  *
53  * @author David Konecny
54  * @since 07/2001
55  */

56 public class AnnotationTypesFolder extends FolderInstance{
57     
58     /** folder for annotation type XML files */
59     private static final String JavaDoc FOLDER = "Editors/AnnotationTypes"; // NOI18N
60

61     /** instance of this class */
62     private static AnnotationTypesFolder folder;
63
64     /** map of annotationtype_name <-> AnnotationType_instance*/
65     private Map JavaDoc annotationTypes;
66
67     /** FileObject which represent the folder with annotation types*/
68     private FileObject fo;
69     
70     /** Creates new AnnotationTypesFolder */
71     private AnnotationTypesFolder(FileObject fo, DataFolder fld) {
72         super(fld);
73         recreate();
74         instanceFinished();
75         
76         this.fo = fo;
77         
78         // add listener on changes in annotation types folder
79
fo.addFileChangeListener(new FileChangeAdapter() {
80             public void fileDeleted(FileEvent fe) {
81                 AnnotationType type;
82                 for (Iterator JavaDoc it = AnnotationTypes.getTypes().getAnnotationTypeNames(); it.hasNext(); ) {
83                     type = AnnotationTypes.getTypes().getType((String JavaDoc)it.next());
84                     if ( type != null && ((FileObject)type.getProp(AnnotationType.PROP_FILE)).equals(fe.getFile()) ) {
85                         AnnotationTypes.getTypes().removeType(type.getName());
86                         break;
87                     }
88                 }
89             }
90         });
91         
92     }
93
94     /** Gets AnnotationTypesFolder singleton instance. */
95     public static synchronized AnnotationTypesFolder getAnnotationTypesFolder(){
96         if (folder != null) {
97             return folder;
98         }
99         
100         FileObject f = Repository.getDefault().getDefaultFileSystem().findResource(FOLDER);
101         if (f == null) {
102             return null;
103         }
104         
105         try {
106             DataObject d = DataObject.find(f);
107             DataFolder df = (DataFolder)d.getCookie(DataFolder.class);
108             if (df != null)
109                 folder = new AnnotationTypesFolder(f, df);
110         } catch (org.openide.loaders.DataObjectNotFoundException ex) {
111             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
112             return null;
113         }
114         return folder;
115     }
116
117     /** Called for each XML file found in FOLDER directory */
118     protected Object JavaDoc createInstance(InstanceCookie[] cookies) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
119         annotationTypes = new HashMap JavaDoc(cookies.length * 4 / 3 + 1);
120         
121         for (int i = 0; i < cookies.length; i++) {
122             Object JavaDoc o = cookies[i].instanceCreate();
123             if (o instanceof AnnotationType) {
124                 AnnotationType type = (AnnotationType)o;
125                 annotationTypes.put(type.getName(), type);
126             }
127         }
128         
129         // set all these types to AnnotationType static member
130
AnnotationTypes.getTypes().setTypes(annotationTypes);
131         
132         return null;
133     }
134
135     /** Save changed AnnotationType */
136     public void saveAnnotationType(AnnotationType type) {
137
138         FileObject fo = (FileObject)type.getProp(AnnotationType.PROP_FILE);
139
140         Document doc = XMLUtil.createDocument(AnnotationTypeProcessor.TAG_TYPE, null, AnnotationTypeProcessor.DTD_PUBLIC_ID, AnnotationTypeProcessor.DTD_SYSTEM_ID);
141         Element typeElem = doc.getDocumentElement();
142
143         typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_NAME, type.getName());
144         if (type.getProp(AnnotationType.PROP_LOCALIZING_BUNDLE) != null)
145             typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_LOCALIZING_BUNDLE, (String JavaDoc)type.getProp(AnnotationType.PROP_LOCALIZING_BUNDLE));
146         if (type.getProp(AnnotationType.PROP_DESCRIPTION_KEY) != null)
147             typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_DESCRIPTION_KEY, (String JavaDoc)type.getProp(AnnotationType.PROP_DESCRIPTION_KEY));
148         typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_VISIBLE, type.isVisible() ? "true" : "false"); // NOI18N
149
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_USE_HIHGLIGHT_COLOR, type.isUseHighlightColor() ? "true" : "false"); // NOI18N
150
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_USE_WAVE_UNDERLINE_COLOR, type.isUseWaveUnderlineColor() ? "true" : "false"); // NOI18N
151
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_INHERIT_FOREGROUND_COLOR, type.isInheritForegroundColor() ? "true" : "false"); // NOI18N
152
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_USE_CUSTOM_SIDEBAR_COLOR, type.isUseCustomSidebarColor() ? "true" : "false"); // NOI18N
153
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_TYPE, type.isWholeLine() ? "line" : "linepart"); // NOI18N
154
if (type.getProp(AnnotationType.PROP_GLYPH_URL) != null)
155             typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_GLYPH, type.getGlyph().toExternalForm());
156         if (type.getProp(AnnotationType.PROP_HIGHLIGHT_COLOR) != null)// && type.isUseHighlightColor())
157
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_HIGHLIGHT, "0x"+Integer.toHexString(type.getHighlight().getRGB() & 0x00FFFFFF)); // NOI18N
158

159         if (type.getProp(AnnotationType.PROP_WAVEUNDERLINE_COLOR) != null)// && type.isUseWaveUnderlineColor())
160
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_WAVEUNDERLINE, "0x"+Integer.toHexString(type.getWaveUnderlineColor().getRGB() & 0x00FFFFFF)); // NOI18N
161

162         if (type.getProp(AnnotationType.PROP_FOREGROUND_COLOR) != null)// && !type.isInheritForegroundColor())
163
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_FOREGROUND, "0x"+Integer.toHexString(type.getForegroundColor().getRGB() & 0x00FFFFFF)); // NOI18N
164
if (type.getProp(AnnotationType.PROP_CUSTOM_SIDEBAR_COLOR) != null)// && !type.isUseCustomSidebarColor())
165
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_CUSTOM_SIDEBAR_COLOR, "0x"+Integer.toHexString(type.getCustomSidebarColor().getRGB() & 0x00FFFFFF)); // NOI18N
166
if (type.getProp(AnnotationType.PROP_ACTIONS_FOLDER) != null)
167             typeElem.setAttribute(AnnotationTypeProcessor.ATTR_TYPE_ACTIONS, (String JavaDoc)type.getProp(AnnotationType.PROP_ACTIONS_FOLDER));
168         
169         if (type.getCombinations() != null) {
170             
171             Element combsElem = doc.createElement(AnnotationTypeProcessor.TAG_COMBINATION);
172             combsElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINATION_TIPTEXT_KEY, (String JavaDoc)type.getProp(AnnotationType.PROP_COMBINATION_TOOLTIP_TEXT_KEY));
173             if (type.getProp(AnnotationType.PROP_COMBINATION_ORDER) != null)
174                 combsElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINATION_ORDER, ""+type.getCombinationOrder());
175             if (type.getProp(AnnotationType.PROP_COMBINATION_MINIMUM_OPTIONALS) != null)
176                 combsElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINATION_MIN_OPTIONALS, ""+type.getMinimumOptionals());
177
178             typeElem.appendChild(combsElem);
179
180             AnnotationType.CombinationMember[] combs = type.getCombinations();
181             for (int i=0; i < combs.length; i++) {
182                 Element combElem = doc.createElement(AnnotationTypeProcessor.TAG_COMBINE);
183                 combElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINE_ANNOTATIONTYPE, combs[i].getName());
184                 combElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINE_ABSORBALL, combs[i].isAbsorbAll() ? "true" : "false"); // NOI18N
185
combElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINE_OPTIONAL, combs[i].isOptional() ? "true" : "false"); // NOI18N
186
if (combs[i].getMinimumCount() > 0)
187                     combElem.setAttribute(AnnotationTypeProcessor.ATTR_COMBINE_MIN, ""+combs[i].getMinimumCount());
188                 combsElem.appendChild(combElem);
189             }
190         }
191         
192         //extended properties:
193
typeElem.setAttribute(AnnotationTypeProcessor.ATTR_SEVERITY, type.getSeverity().getName());
194         typeElem.setAttribute(AnnotationTypeProcessor.ATTR_BROWSEABLE, Boolean.toString(type.isBrowseable()));
195         typeElem.setAttribute(AnnotationTypeProcessor.ATTR_PRIORITY, Integer.toString(type.getPriority()));
196         
197         doc.getDocumentElement().normalize();
198         
199         try{
200             FileLock lock = fo.lock();
201             OutputStream JavaDoc os = null;
202             try {
203                 os = fo.getOutputStream(lock);
204                 XMLUtil.write(doc, os, "UTF-8"); // NOI18N
205
} catch (Exception JavaDoc ex){
206                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
207             } finally {
208                 if (os != null) {
209                     try {
210                         os.close();
211                     } catch (IOException JavaDoc e) {
212                         ErrorManager.getDefault().notify(e);
213                     }
214                 }
215                 lock.releaseLock();
216             }
217         }catch (IOException JavaDoc ex){
218             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
219         }
220         
221     }
222     
223 }
224
Popular Tags