KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > SuggestionTypesFolder


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.tasklist.suggestions;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.openide.cookies.InstanceCookie;
26 import org.openide.filesystems.FileObject;
27 import org.openide.loaders.DataFolder;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30 import org.openide.loaders.FolderInstance;
31 import org.openide.filesystems.Repository;
32
33 /** Representation of the "Suggestions/Types" folder. All
34  * instances created through the createInstance() method are
35  * stored in Map and passed to SuggestionType.setTypes(). This
36  * class should only be responsible for processing of the folder,
37  * listening of the changes in folder etc. Clients should use
38  * SuggestionType.getType and other methods in SuggestionType
39  * for access to SuggestionTypes.
40  * <p>
41  * Based on AnnotationTypesFolder in the editor package.
42  * <p>
43  *
44  * @author Tor Norbye, David Konecny
45  */

46 final public class SuggestionTypesFolder extends FolderInstance {
47     
48     /** folder for suggestion type XML files */
49     private static final String JavaDoc FOLDER = "Suggestions/Types";
50     
51     /** instance of this class */
52     private static SuggestionTypesFolder folder;
53
54     /** map of suggestiontype_name <-> SuggestionType_instance*/
55     private Map JavaDoc suggestionTypes;
56
57     /** FileObject which represent the folder with suggestion types*/
58     private FileObject fo;
59     
60     /** Creates new SuggestionTypesFolder */
61     private SuggestionTypesFolder(FileObject fo, DataFolder fld) {
62         super(fld);
63         recreate();
64         instanceFinished();
65         
66         this.fo = fo;
67     }
68
69     /** Gets SuggestionTypesFolder singleton instance. */
70     public static synchronized SuggestionTypesFolder getSuggestionTypesFolder(){
71         if (folder != null) {
72             return folder;
73         }
74         
75         FileObject f = Repository.getDefault().getDefaultFileSystem().findResource(FOLDER);
76         if (f == null) {
77             return null;
78         }
79         
80         try {
81             DataObject d = DataObject.find(f);
82             DataFolder df = (DataFolder)d.getCookie(DataFolder.class);
83             if (df != null)
84                 folder = new SuggestionTypesFolder(f, df);
85         } catch (DataObjectNotFoundException ex) {
86             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
87             return null;
88         }
89         return folder;
90     }
91
92     /** Called for each XML file found in FOLDER directory */
93     protected Object JavaDoc createInstance(InstanceCookie[] cookies) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
94         suggestionTypes = new HashMap JavaDoc(cookies.length * 4 / 3);
95         
96         for (int i = 0; i < cookies.length; i++) {
97             Object JavaDoc o = cookies[i].instanceCreate();
98             if (o instanceof SuggestionType) {
99                 SuggestionType type = (SuggestionType)o;
100                 type.setPosition(i);
101                 suggestionTypes.put(type.getName(), type);
102             }
103         }
104         
105         // set all these types to SuggestionType static member
106
SuggestionTypes.getDefault().setTypes(suggestionTypes);
107         
108         return null;
109     }
110 }
111
Popular Tags