KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.netbeans.editor.BaseKit;
31 import org.netbeans.editor.Settings;
32 import org.openide.NotifyDescriptor;
33 import org.openide.cookies.InstanceCookie;
34 import org.openide.filesystems.FileChangeAdapter;
35 import org.openide.filesystems.FileChangeListener;
36 import org.openide.filesystems.FileEvent;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.Repository;
39 import org.openide.loaders.DataFolder;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42 import org.openide.options.SystemOption;
43 import org.openide.util.NbBundle;
44
45 /** Editor Settings main node folder.
46  * In this folder are stored global options such as global keybindings.
47  * Mime options are lazily initialized after loading appropriate kit
48  * (NbEditorKit.java) or after request of Option window to show
49  * the properties.
50  * Initialization starts with loading user's setting from
51  * XML files and then initializer is added to Settings and reseted.
52  *
53  * @author Martin Roskanin
54  * @since 08/2001
55  */

56 public class AllOptionsFolder{
57     
58     /** folder for Editor Settings main node */
59     public static final String JavaDoc FOLDER = "Editors"; // NOI18N
60
public static final String JavaDoc OPTION_FILE_NAME = "Settings.settings"; // NOI18N
61

62     /** instance of this class */
63     private static AllOptionsFolder settingsFolder;
64     
65     private static boolean baseInitialized = false;
66     
67     private static DataFolder folder;
68     private static MIMEOptionFolder mimeFolder;
69     
70     // List of already initialized options
71
private static Map JavaDoc installedOptions = new Hashtable JavaDoc();
72     
73     /** Listens to changes on the Modules folder */
74     private static FileChangeListener moduleRegListener;
75
76     
77     /** Creates new AllOptionsFolder */
78     private AllOptionsFolder(DataFolder fld) {
79         folder = fld;
80     }
81     
82     /** Gets the singleton of global options MIME folder */
83     public MIMEOptionFolder getMIMEFolder(){
84         synchronized (Settings.class){
85             if (mimeFolder!=null) return mimeFolder;
86
87             FileObject f = Repository.getDefault().getDefaultFileSystem().
88             findResource(FOLDER+"/text/"+BaseOptions.BASE); //NOI18N
89

90             // MIME folder doesn't exist, let's create it
91
if (f==null){
92                 FileObject fo = Repository.getDefault().getDefaultFileSystem().
93                 findResource(AllOptionsFolder.FOLDER);
94                 String JavaDoc fName = "text/"+BaseOptions.BASE; //NOI18N
95

96                 if (fo != null){
97                     try{
98                         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(fName,"/"); //NOI18N
99
while (stok.hasMoreElements()) {
100                             String JavaDoc newFolder = stok.nextToken();
101                             if (fo.getFileObject(newFolder) == null)
102                                 fo = fo.createFolder(newFolder);
103                             else
104                                 fo = fo.getFileObject(newFolder);
105                         }
106                     }catch(IOException JavaDoc ioe){
107                         ioe.printStackTrace();
108                     }
109
110                     f = Repository.getDefault().getDefaultFileSystem().
111                     findResource(AllOptionsFolder.FOLDER+"/text/"+BaseOptions.BASE); //NOI18N
112
}
113             }
114
115             if (f != null) {
116                 try {
117                     DataObject d = DataObject.find(f);
118                     DataFolder df = (DataFolder)d.getCookie(DataFolder.class);
119                     if (df != null) {
120                         mimeFolder = new MIMEOptionFolder(df, getBase());
121                         return mimeFolder;
122                     }
123                 } catch (org.openide.loaders.DataObjectNotFoundException ex) {
124                     ex.printStackTrace();
125                 }
126             }
127
128             return null;
129         }
130     }
131     
132     /** Returns list of installed Options. Values = options classes */
133     public List JavaDoc getInstalledOptions(){
134         
135         // first XMLized options
136

137         List JavaDoc retList = new ArrayList JavaDoc();
138         String JavaDoc[] MIMES = new String JavaDoc[] {"text", "application"}; //#25246 application/xml-dtd // NOI18N
139
for (int in = 0; in<MIMES.length; in++) {
140             FileObject mainFolderFO = Repository.getDefault().getDefaultFileSystem().
141             findResource(AllOptionsFolder.FOLDER+"/" + MIMES[in]); //NOI18N
142
if (mainFolderFO != null){
143                 DataFolder mainFolder = DataFolder.findFolder(mainFolderFO);
144                 if (mainFolder != null){
145                     DataObject subFolders[] = mainFolder.getChildren();
146                     for (int i=0; i<subFolders.length; i++){
147                         if (!(subFolders[i] instanceof DataFolder)) continue;
148                         DataFolder subFolder = (DataFolder) subFolders[i];
149                         FileObject optionInstance = Repository.getDefault().getDefaultFileSystem().
150                             findResource(subFolder.getPrimaryFile().getPath()+"/"+AllOptionsFolder.OPTION_FILE_NAME); // NOI18N
151
if (optionInstance == null) continue;
152                         try{
153                             DataObject optionDO = DataObject.find(optionInstance);
154                             if (optionDO == null) continue;
155                             InstanceCookie ic = (InstanceCookie)optionDO.getCookie(InstanceCookie.class);
156                             if (ic == null) continue;
157                             BaseOptions bo = AllOptionsFolder.getDefault().getBO(ic);
158                             if (bo == null) continue;
159                             retList.add(bo.getClass());
160                         }catch(DataObjectNotFoundException donf){
161                             donf.printStackTrace();
162                         }
163                     }
164                 }
165             }
166         }
167         
168         // Now old SystemOptions options
169
AllOptions allOptions
170         = (AllOptions)AllOptions.findObject(AllOptions.class, true);
171         
172         if (allOptions == null) return retList;
173         
174         SystemOption[] sos = allOptions.getOptions();
175         if (sos == null) return retList;
176         
177         for (int i=0; i<sos.length; i++){
178             
179             if (!(sos[i] instanceof BaseOptions)) continue;
180             
181             BaseOptions bo = (BaseOptions) sos[i];
182             if (retList.contains(bo.getClass())) retList.remove(bo.getClass());
183             if (BaseKit.getKit(bo.getKitClass()).getContentType() != null){
184                 retList.add(bo.getClass());
185                 processInitializers(bo, false);
186             }else{
187                 final String JavaDoc kitClazz = bo.getKitClass().toString();
188                 SwingUtilities.invokeLater(
189                 new Runnable JavaDoc() {
190                     public void run() {
191                         NotifyDescriptor msg = new NotifyDescriptor.Message(
192                         
193                         NbBundle.getMessage( AllOptionsFolder.class, "ERR_NoContentTypeDefined", kitClazz),
194                         NotifyDescriptor.WARNING_MESSAGE
195                         );
196                         
197                         org.openide.DialogDisplayer.getDefault().notify(msg);
198                     }
199                 }
200                 );
201             }
202         }
203         
204         return retList;
205     }
206     
207     public static void unregisterModuleRegListener(){
208         FileObject moduleRegistry = Repository.getDefault().getDefaultFileSystem().findResource("Modules"); //NOI18N
209

210         if (moduleRegistry !=null){ //NOI18N
211
if (moduleRegListener!=null)
212                 moduleRegistry.removeFileChangeListener(moduleRegListener);
213         }
214     }
215     
216     /** Creates the only instance of AllOptionsFolder. */
217     public static AllOptionsFolder getDefault(){
218         synchronized (Settings.class) {
219             // try to find the itutor XML settings
220
if (settingsFolder!=null) return settingsFolder;
221             org.openide.filesystems.FileObject f = Repository.getDefault().getDefaultFileSystem().
222             findResource(FOLDER);
223             if (f==null) return null;
224
225             DataFolder df = DataFolder.findFolder(f);
226             if (df == null) {
227             } else {
228                 if (settingsFolder == null){
229                     settingsFolder = new AllOptionsFolder(df);
230
231                     // attach listeners for module registry for listening on addition or removal of modules in IDE
232
if(moduleRegListener == null) {
233                         moduleRegListener = new FileChangeAdapter() {
234                             public void fileChanged(FileEvent fe){
235                                 updateOptions();
236                             }
237                         };
238
239                         FileObject moduleRegistry = Repository.getDefault().getDefaultFileSystem().findResource("Modules"); //NOI18N
240

241                         if (moduleRegistry !=null){ //NOI18N
242
moduleRegistry.addFileChangeListener(moduleRegListener);
243                         }
244                     }
245
246                     return settingsFolder;
247                 }
248             }
249             return null;
250         }
251     }
252     
253     /** Getter for KeyBingings */
254     public List JavaDoc getKeyBindingList() {
255         return getBase().getKeyBindingList();
256     }
257     
258     /** Setter for KeyBindings */
259     public void setKeyBindingList(List JavaDoc list) {
260         getBase().setKeyBindingList(list);
261     }
262     
263     public boolean isToolbarVisible() {
264         return getBase().isToolbarVisible();
265     }
266     
267     public void setToolbarVisible(boolean toolbarVisible) {
268         getBase().setToolbarVisible(toolbarVisible);
269     }
270
271     public boolean getLineNumberVisible(){
272         return getBase().getLineNumberVisible();
273     }
274     
275     public void setLineNumberVisible(boolean lineVisible) {
276         getBase().setLineNumberVisible(lineVisible);
277     }
278     
279     public boolean isTextAntialiasing() {
280         return getBase().isTextAntialiasing();
281     }
282     
283     public void setTextAntialiasing(boolean textAntialiasing) {
284         getBase().setTextAntialiasing(textAntialiasing);
285     }
286
287     /** Loads default global keyBindings List and initializes it.
288      * It is used mainly by other options for initializing global keyBindings */

289     protected void loadDefaultKeyBindings(){
290         getBase().getKeyBindingList();
291     }
292     
293     /** Returns kitClass of uninstalled option */
294     private static Class JavaDoc uninstallOption(){
295         List JavaDoc updatedInstalledOptions = AllOptionsFolder.getDefault().getInstalledOptions();
296         synchronized (Settings.class){
297             Iterator JavaDoc i = installedOptions.keySet().iterator();
298             while (i.hasNext()){
299                 Object JavaDoc obj = i.next();
300                 if(obj instanceof Class JavaDoc){
301                     if (!updatedInstalledOptions.contains(obj)){
302                         installedOptions.remove(obj);
303                         return (Class JavaDoc)obj;
304                     }
305                 }
306             }
307             return null;
308         }
309     }
310     
311     private static void updateOptions(){
312         uninstallOption();
313         List JavaDoc installedOpts = new ArrayList JavaDoc(installedOptions.values());
314         Iterator JavaDoc i = installedOpts.iterator();
315         while (i.hasNext()){
316             Object JavaDoc obj = i.next();
317             if (obj instanceof BaseOptions){
318                 BaseOptions bo = (BaseOptions)obj;
319                 if (bo != null){
320                     bo.initPopupMenuItems();
321                 }
322             }
323         }
324     }
325     
326     /** Returns true if BaseOptions has been initialized */
327     public boolean baseInitialized(){
328         return baseInitialized;
329     }
330     
331     /** Gets the singleton of BaseOptions and register it in Settings initializer,
332      * if it wasn't been done before. */

333     private BaseOptions getBase(){
334         
335         BaseOptions ret = (BaseOptions)BaseOptions.findObject(BaseOptions.class, true);
336         
337         synchronized (Settings.class){
338             if (baseInitialized == false){
339                 // Add the initializer for the base options. It will not be removed
340
Settings.addInitializer(ret.getSettingsInitializer(),
341                 Settings.OPTION_LEVEL);
342                 baseInitialized = true;
343                 Settings.reset();
344             }
345         }
346         
347         return ret;
348     }
349     
350     /** Gets the instance of BaseOptions from InstanceCookie */
351     protected BaseOptions getBO(InstanceCookie ic){
352         initInstance(ic);
353         BaseOptions ret = null;
354         try{
355             synchronized (Settings.class){
356                 ret = (installedOptions.get(ic.instanceClass()) instanceof BaseOptions) ? (BaseOptions) installedOptions.get(ic.instanceClass())
357                 : null;
358             }
359         }catch(ClassNotFoundException JavaDoc cnfe){
360             cnfe.printStackTrace();
361             
362         }catch(IOException JavaDoc ioex){
363             ioex.printStackTrace();
364         }
365         return ret;
366     }
367     
368     /** Create the instance of appropriate BaseOption subclass */
369     private void initInstance(InstanceCookie ic){
370         try{
371             Object JavaDoc optionObj;
372             synchronized (Settings.class){
373                 if (installedOptions.containsKey(ic.instanceClass())) {
374                     return;
375                 }
376                 optionObj = ic.instanceCreate();
377                 if (!(optionObj instanceof BaseOptions)) return;
378                 installedOptions.put(ic.instanceClass(), (BaseOptions)optionObj);
379             }
380             processInitializers((BaseOptions)optionObj, false);
381         }catch(ClassNotFoundException JavaDoc cnfe){
382             cnfe.printStackTrace();
383         }catch(IOException JavaDoc ioex){
384             ioex.printStackTrace();
385         }
386     }
387     
388     /**
389      * Lazily inits MIME Option class. Calls <code>loadMIMEOption(kitClass, true)</code>.
390      *
391      * @param kitClass The editor kit class you want to load options for.
392      *
393      * @deprecated See {@link loadMimeOption(Class, boolean)} for details.
394      */

395     public void loadMIMEOption(Class JavaDoc kitClass){
396         loadMIMEOption(kitClass, true);
397     }
398     
399     /**
400      * Lazily inits MIME Option class. If processOldTypeOption is true initializers
401      * for this option will be processed.
402      *
403      * @param kitClass The editor kit class you want to load options for.
404      * @param processOldTypeOptions Internal magic, if you really want to call
405      * this method, you should probably set this to <code>true</code>.
406      *
407      * @deprecated There is no reason you should call this method. It should have
408      * never been made public. Use <code>MimeLookup.getLookup(MimePath.parse(your-mime-type)).lookup(BaseOptions.class)</code>
409      * for accessing <code>BaseOptions</code> for your mime type.
410      */

411     public void loadMIMEOption(Class JavaDoc kitClass, boolean processOldTypeOption){
412         String JavaDoc contentType = BaseKit.getKit(kitClass).getContentType();
413         if (contentType == null) return;
414         FileObject optionFO = Repository.getDefault().getDefaultFileSystem().
415         findResource(FOLDER+"/"+contentType+"/"+OPTION_FILE_NAME); //NOI18N
416
if (optionFO == null) {
417             // old type of BaseOptions.
418
// Options weren't transfered to XML form for this kitClass yet.
419
// We have to find them via BaseOptions.getOptions and process initializers.
420
if (processOldTypeOption){
421                 BaseOptions oldBO = BaseOptions.getOptions(kitClass);
422                 if (oldBO != null){
423                     boolean process = false;
424                     synchronized (Settings.class){
425                         if (!installedOptions.containsKey(kitClass)){
426                             installedOptions.put(kitClass, oldBO);
427                             process = true;
428                         }
429                     }
430                     if (process){
431                         processInitializers(oldBO, false);
432                     }
433                 }
434             }
435             return;
436         }
437
438         try{
439             DataObject optionDO = DataObject.find(optionFO);
440             if (optionDO == null) return;
441
442             InstanceCookie ic = (InstanceCookie)optionDO.getCookie(InstanceCookie.class);
443             if (ic == null) return;
444
445             initInstance(ic);
446
447         }catch(DataObjectNotFoundException donf){
448             donf.printStackTrace();
449         }
450     }
451     
452     /** Updates MIME option initializer. Loads user's settings stored in XML
453      * files and updates Setting's initializers via reset method */

454     private void processInitializers(BaseOptions bo, boolean remove) {
455         //synchronized (BaseKit.class){
456
synchronized (Settings.class){
457                 Settings.Initializer si = bo.getSettingsInitializer();
458                 // Remove the old one
459
Settings.removeInitializer(si.getName());
460                 if (!remove) { // add the new one
461
Settings.addInitializer(si, Settings.OPTION_LEVEL);
462                 }
463
464                 // load all settings of this mime type from XML files
465
bo.loadXMLSettings();
466
467                 //initialize popup menu
468
bo.initPopupMenuItems();
469
470                 /* Reset the settings so that the new initializers take effect
471                  * or the old are removed. */

472                 Settings.reset();
473             }
474         //}
475
}
476     
477 }
478
Popular Tags