KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NbPlaces


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.core;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.Repository;
32 import org.openide.loaders.DataFolder;
33 import org.openide.nodes.Node;
34
35 /** Important places in the system.
36 *
37 * @author Jaroslav Tulach
38 */

39 public final class NbPlaces extends Object JavaDoc {
40     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
41     
42     /** No instance outside this class.
43     */

44     private NbPlaces() {
45     }
46     
47     private static NbPlaces DEFAULT;
48     
49     /** Getter for default instance.
50      */

51     public static synchronized NbPlaces getDefault() {
52         if (DEFAULT == null) {
53             DEFAULT = new NbPlaces();
54         }
55         return DEFAULT;
56     }
57     
58     public void addChangeListener(ChangeListener JavaDoc l) {
59         synchronized (listeners) {
60             listeners.add(l);
61         }
62     }
63     
64     public void removeChangeListener(ChangeListener JavaDoc l) {
65         synchronized (listeners) {
66             listeners.remove(l);
67         }
68     }
69     
70     void fireChange() {
71         ChangeListener JavaDoc[] l;
72         synchronized (listeners) {
73             l = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
74         }
75         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
76         for (int i = 0; i < l.length; i++) {
77             l[i].stateChanged(ev);
78         }
79     }
80
81     /** Environment node. Place for all transient information about
82     * the IDE.
83     */

84     public Node environment () {
85         return EnvironmentNode.find(EnvironmentNode.TYPE_ENVIRONMENT);
86     }
87
88
89     /** Session node */
90     public Node session () {
91         return EnvironmentNode.find(EnvironmentNode.TYPE_SESSION);
92     }
93
94     /** Root nodes.
95     */

96     public Node[] roots () {
97         return EnvironmentNode.find(EnvironmentNode.TYPE_ROOTS).getChildren ().getNodes ();
98     }
99
100     /** Default folder for toolbars.
101     */

102     public DataFolder toolbars () {
103         return findSessionFolder ("Toolbars"); // NOI18N
104
}
105
106     /** Default folder for menus.
107     */

108     public DataFolder menus () {
109         return findSessionFolder ("Menu"); // NOI18N
110
}
111
112     /** Default folder for actions pool.
113     */

114     public DataFolder actions () {
115         return findSessionFolder ("Actions"); // NOI18N
116
}
117
118      /**
119      * Returns a DataFolder subfolder of the session folder. In the DataFolder
120      * folders go first (sorted by name) followed by the rest of objects sorted
121      * by name.
122      */

123      public static DataFolder findSessionFolder (String JavaDoc name) {
124         try {
125             FileSystem fs = Repository.getDefault().getDefaultFileSystem ();
126             FileObject fo = fs.findResource(name);
127             if (fo == null) {
128                 // resource not found, try to create new folder
129
fo = FileUtil.createFolder(fs.getRoot(), name);
130             }
131             return DataFolder.findFolder(fo);
132         } catch (IOException JavaDoc ex) {
133             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Folder not found and cannot be created: " + name).initCause(ex); // NOI18N
134
}
135     }
136
137 }
138
Popular Tags