KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > hints > ProvidersList


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 package org.netbeans.spi.editor.hints;
20
21 import java.beans.XMLDecoder JavaDoc;
22 import java.beans.XMLEncoder JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import org.netbeans.modules.editor.hints.options.ProvidersListAccessor;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileLock;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.Repository;
38 import org.openide.loaders.DataFolder;
39 import org.openide.loaders.FolderLookup;
40 import org.openide.util.Lookup.Template;
41
42 /**
43  *
44  * @author Jan Lahoda
45  */

46 public final class ProvidersList {
47     
48     static {
49         ProvidersListAccessor.INSTANCE = new ProvidersListAccessor() {
50             public Collection JavaDoc<String JavaDoc> getInstalledProvidersList() {
51                 return ProvidersList.getInstalledProvidersList();
52             }
53             public void setProviderEnabled(String JavaDoc providerKey, boolean enabled) {
54                 ProvidersList.setProviderEnabled(providerKey, enabled);
55             }
56             public void setSeverity(String JavaDoc providerKey, String JavaDoc errorKey, Severity severity) {
57                 ProvidersList.setSeverity(providerKey, errorKey, severity);
58             }
59             public ProviderDescription getProviderDescription(String JavaDoc providerKey) {
60                 return ProvidersList.getProviderDescription(providerKey);
61             }
62             public List JavaDoc<ProviderDescription> getDescriptions() {
63                 return ProvidersList.getDescriptions();
64             }
65         };
66     }
67     
68     private static final String JavaDoc PROVIDERS_LIST_FOLDER = "EditorHints/"; // NOI18N
69
private static final String JavaDoc CONFIG_FILE_NAME = "editorhintsconfig.xml"; //NOI18N
70

71     private static List JavaDoc<ProviderDescription> descriptions = null;
72     private static Map JavaDoc<String JavaDoc, Boolean JavaDoc> key2Enabled = null;
73     private static Map JavaDoc<String JavaDoc, ProviderDescription> key2Description = null;
74     private static Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>> key2Error2Severity = null;
75     
76     private static boolean initialized = false;
77     
78     /** Creates a new instance of ProvidersList */
79     private ProvidersList() {
80     }
81     
82     static synchronized Collection JavaDoc<String JavaDoc> getInstalledProvidersList() {
83         if (!initialized) {
84             init();
85             initialized = true;
86         }
87         
88         return key2Description.keySet();
89     }
90     
91     private static void init() {
92         key2Description = new HashMap JavaDoc<String JavaDoc, ProviderDescription>();
93         descriptions = new ArrayList JavaDoc<ProviderDescription>();
94         FileObject listFolder = Repository.getDefault().getDefaultFileSystem().findResource(PROVIDERS_LIST_FOLDER);
95         FolderLookup flookup = new FolderLookup(DataFolder.findContainer(listFolder));
96         Collection JavaDoc<? extends ProviderDescription> looked = flookup.getLookup().lookupAll(ProviderDescription.class);
97         
98         for (ProviderDescription desc : looked) {
99             key2Description.put(desc.getKey(), desc);
100             descriptions.add(desc);
101         }
102
103     FileObject configFile = Repository.getDefault().getDefaultFileSystem().findResource(CONFIG_FILE_NAME);
104     
105     if (configFile != null) {
106         InputStream JavaDoc ins = null;
107         
108         try {
109         ins = configFile.getInputStream();
110         
111         XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc(ins);
112         
113         key2Error2Severity = (Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>>) decoder.readObject();
114         key2Enabled = (Map JavaDoc<String JavaDoc, Boolean JavaDoc>) decoder.readObject();
115         
116         decoder.close();
117         } catch (IOException JavaDoc e) {
118         ErrorManager.getDefault().notify(e);
119         } finally {
120         if (ins != null) {
121             try {
122             ins.close();
123             } catch (IOException JavaDoc e) {
124             ErrorManager.getDefault().notify(e);
125             }
126         }
127         }
128     } else {
129         key2Error2Severity = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Severity>>();
130         key2Enabled = new HashMap JavaDoc<String JavaDoc, Boolean JavaDoc>();
131     }
132     }
133     
134     private static void save() {
135     FileLock lock = null;
136     OutputStream JavaDoc out = null;
137     
138     try {
139         FileObject configFile = Repository.getDefault().getDefaultFileSystem().findResource(CONFIG_FILE_NAME);
140         
141         if (configFile == null) {
142         FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
143         
144         configFile = FileUtil.createData(root, CONFIG_FILE_NAME);
145         }
146         
147         lock = configFile.lock();
148         out = configFile.getOutputStream(lock);
149         
150         XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc(out);
151         
152         encoder.writeObject(key2Error2Severity);
153         encoder.writeObject(key2Enabled);
154         
155         encoder.close();
156     } catch (IOException JavaDoc e) {
157         ErrorManager.getDefault().notify(e);
158     } finally {
159         if (out != null) {
160         try {
161             out.close();
162         } catch (IOException JavaDoc e) {
163             ErrorManager.getDefault().notify(e);
164         }
165         }
166         
167         if (lock != null) {
168         lock.releaseLock();
169         }
170     }
171     }
172     
173     /**Test if the given provider should be enabled or disabled.
174      *
175      * @param providerKey the provider ID to test
176      * @return true if the given provider should be enabled
177      */

178     public static boolean isProviderEnabled(String JavaDoc providerKey) {
179         if (!initialized) {
180             init();
181             initialized = true;
182         }
183         
184         if (!key2Description.containsKey(providerKey)) {
185             throw new IllegalArgumentException JavaDoc("Unknown provider key: " + providerKey); // NOI18N
186
}
187         
188         Boolean JavaDoc enabled = (Boolean JavaDoc) key2Enabled.get(providerKey);
189         
190         if (enabled == null) {
191             ProviderDescription desc = getProviderDescription(providerKey);
192             
193             return desc.getDefaultState();
194         }
195         
196         return enabled.booleanValue();
197     }
198     
199     /**Return severity of the given error of the given provider.
200      *
201      * @param providerKey the provider ID to test
202      * @param errorKey the error ID to test
203      * @return desired severity of the given error
204      */

205     public static synchronized Severity getErrorSeverity(String JavaDoc providerKey, String JavaDoc errorKey) {
206         if (!initialized) {
207             init();
208             initialized = true;
209         }
210         
211         if (!key2Description.containsKey(providerKey)) {
212             throw new IllegalArgumentException JavaDoc("Unknown provider key: " + providerKey); // NOI18N
213
}
214         
215         Map JavaDoc<String JavaDoc, Severity> error2Severity = key2Error2Severity.get(providerKey);
216         
217         if (error2Severity == null) {
218             key2Error2Severity.put(providerKey, error2Severity = new HashMap JavaDoc<String JavaDoc, Severity>());
219         }
220         
221         Severity severity = error2Severity.get(errorKey);
222         
223         if (severity == null) {
224             ProviderDescription desc = getProviderDescription(providerKey);
225             
226             if (desc.getSupportedErrorKeys().contains(errorKey)) {
227                 return desc.getErrorDefaultSeverity(errorKey);
228             } else {
229                 throw new IllegalArgumentException JavaDoc("Unknown error key: " + errorKey + " for provider: " + providerKey); // NOI18N
230
}
231         }
232         
233         return severity;
234     }
235     
236     /**Probably not for general use, TBD.
237      */

238     static synchronized void setProviderEnabled(String JavaDoc providerKey, boolean enabled) {
239         if (!initialized) {
240             init();
241             initialized = true;
242         }
243         
244         if (!key2Description.containsKey(providerKey)) {
245             throw new IllegalArgumentException JavaDoc("Unknown provider key: " + providerKey); // NOI18N
246
}
247         
248         key2Enabled.put(providerKey, Boolean.valueOf(enabled));
249     
250     save();
251     }
252
253     /**Probably not for general use, TBD.
254      */

255     static synchronized void setSeverity(String JavaDoc providerKey, String JavaDoc errorKey, Severity severity) {
256         if (!initialized) {
257             init();
258             initialized = true;
259         }
260         
261         if (!getProviderDescription(providerKey).getSupportedErrorKeys().contains(errorKey)) {
262             throw new IllegalArgumentException JavaDoc("Unknown error key: " + errorKey + " for provider: " + providerKey); // NOI18N
263
}
264         
265         Map JavaDoc<String JavaDoc, Severity> error2Severity = key2Error2Severity.get(providerKey);
266         
267         if (error2Severity == null) {
268             key2Error2Severity.put(providerKey, error2Severity = new HashMap JavaDoc<String JavaDoc, Severity>());
269         }
270         
271         error2Severity.put(errorKey, severity);
272     
273     save();
274     }
275     
276     /**Probably not for general use, TBD.
277      */

278     static synchronized ProviderDescription getProviderDescription(String JavaDoc providerKey) {
279         if (!initialized) {
280             init();
281             initialized = true;
282         }
283         
284         if (!key2Description.containsKey(providerKey)) {
285             throw new IllegalArgumentException JavaDoc("Unknown provider key: " + providerKey); // NOI18N
286
}
287         
288         return (ProviderDescription) key2Description.get(providerKey);
289     }
290     
291     /**Probably not for general use, TBD.
292      */

293     static synchronized List JavaDoc<ProviderDescription> getDescriptions() {
294         if (!initialized) {
295             init();
296             initialized = true;
297         }
298         
299         return descriptions;
300     }
301     
302 // /**HACK, need to store it somewhere and this is a very simple (though incorrect) place, make bette.
303
// */
304
// public static final int EAGER_ON_PROJECT = 0;
305
// public static final int EAGER_ON_DEMAND = 1;
306
// public static final int EAGER_LAZY = 2;
307
//
308
// public static synchronized void setEagerness(int type) {
309
// if (!initialized) {
310
// init();
311
// initialized = true;
312
// }
313
//
314
// key2Enabled.put("eager-hack", new Integer(type));
315
//
316
// save();
317
// }
318
//
319
// public static synchronized int getEagerness() {
320
// if (!initialized) {
321
// init();
322
// initialized = true;
323
// }
324
//
325
// Integer v = (Integer) key2Enabled.get("eager-hack");
326
//
327
// if (v == null)
328
// return EAGER_LAZY;
329
//
330
// return v.intValue();
331
// }
332
}
333
Popular Tags