KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.core.ui.LookupNode;
23 import org.openide.actions.PropertiesAction;
24 import org.openide.actions.ToolsAction;
25 import org.openide.loaders.DataFolder;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.util.HelpCtx;
30 import org.openide.util.Mutex;
31 import org.openide.util.NbBundle;
32 import org.openide.util.actions.SystemAction;
33
34 /** This object represents environment settings in the Corona system.
35 * This class is final only for performance purposes.
36 * Can be unfinaled if desired.
37 *
38 * @author Petr Hamernik, Dafe Simonek
39 */

40 final class EnvironmentNode extends AbstractNode {
41     /** generated Serialized Version UID */
42     static final long serialVersionUID = 4782447107972624693L;
43     /** name of section to filter */
44     private String JavaDoc filter;
45     /** icon base for icons of this node */
46     private static final String JavaDoc EN_ICON_BASE = "org/netbeans/core/resources/"; // NOI18N
47
/** map between type of node and the parent node for this type */
48     private static java.util.HashMap JavaDoc<String JavaDoc, Node> types = new java.util.HashMap JavaDoc<String JavaDoc, Node> (11);
49     /** A lock for the find method. */
50     private static final Object JavaDoc lock = new Object JavaDoc();
51
52     /** Type to add an entry to the root nodes. */
53     public static final String JavaDoc TYPE_ROOTS = "roots"; // NOI18N
54
/** Type to add an entry to the Environment (in the Explorer). */
55     public static final String JavaDoc TYPE_ENVIRONMENT = "environment"; // NOI18N
56
/** Type to add an entry to the Session settings. */
57     public static final String JavaDoc TYPE_SESSION = "session"; // NOI18N
58

59     /** Constructor */
60     private EnvironmentNode (String JavaDoc filter, Children children) {
61         super (children);
62         this.filter = filter;
63         decorateNode(filter, this);
64     }
65     
66     
67     /** Finds the node for given name.
68      */

69     public static Node find (final String JavaDoc name) {
70         // XXX this is probably obsolete? consider deleting
71
Node retValue =
72             Children.MUTEX.readAccess(new Mutex.Action<Node>() {
73                 public Node run() {
74                     synchronized (lock) {
75                         Node n = types.get (name);
76                         if (n == null) {
77                             DataFolder folder = null;
78                             if (TYPE_ENVIRONMENT.equals(name)) {
79                                 folder = NbPlaces.getDefault().findSessionFolder("UI/Runtime"); // NOI18N
80
} else if (TYPE_ROOTS.equals(name)) {
81                                 folder = NbPlaces.getDefault().findSessionFolder("UI/Roots");
82                             } else {
83                                 assert TYPE_SESSION.equals(name) : name;
84                                 folder = NbPlaces.getDefault().findSessionFolder("UI/Services"); // NOI18N
85
}
86
87                             n = new PersistentLookupNode(name, folder);
88                             types.put (name, n);
89                         }
90                         return n;
91                     }
92                 }
93             });
94         if (retValue != null) {
95             return retValue;
96         }
97         throw new IllegalStateException JavaDoc();
98     }
99     
100     private static void decorateNode (String JavaDoc name, AbstractNode node) {
101         String JavaDoc resourceName = "CTL_" + name + "_name"; // NOI18N
102
String JavaDoc iconBase = EN_ICON_BASE + name.toLowerCase () + ".gif";
103         
104         node.setDisplayName(NbBundle.getMessage (EnvironmentNode.class, resourceName));
105         node.setIconBaseWithExtension(iconBase);
106     }
107         
108
109     public HelpCtx getHelpCtx () {
110         return new HelpCtx (EnvironmentNode.class);
111     }
112
113     /** Getter for set of actions that should be present in the
114     * popup menu of this node. This set is used in construction of
115     * menu returned from getContextMenu and specially when a menu for
116     * more nodes is constructed.
117     *
118     * @return array of system actions that should be in popup menu
119     */

120     public SystemAction[] createActions () {
121         return new SystemAction[] {
122                    SystemAction.get(ToolsAction.class),
123                    SystemAction.get(PropertiesAction.class)
124                };
125     }
126
127     /** For deserialization */
128     public Node.Handle getHandle () {
129         return new EnvironmentHandle (filter);
130     }
131
132     /** Adds serialization support to LookupNode */
133     private static final class PersistentLookupNode extends LookupNode
134     implements java.beans.PropertyChangeListener JavaDoc {
135         
136         private String JavaDoc filter;
137         
138         public PersistentLookupNode (String JavaDoc filter, DataFolder folder) {
139             super(folder);
140             this.filter = filter;
141             
142             if (TYPE_ROOTS.equals(filter)) {
143                 folder.addPropertyChangeListener(
144                     org.openide.util.WeakListeners.propertyChange(this, folder));
145             }
146         }
147         
148         public Node.Handle getHandle () {
149             return new EnvironmentHandle (filter);
150         }
151
152         /** Listens on changes on root nodes. */
153         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
154             if(DataFolder.PROP_CHILDREN.equals(evt.getPropertyName())) {
155                 NbPlaces.getDefault().fireChange();
156             }
157         }
158     } // end of PersistentLookupNode
159

160     static final class EnvironmentHandle implements Node.Handle {
161         static final long serialVersionUID =-850350968366553370L;
162         
163         /** field */
164         private String JavaDoc filter;
165         
166         /** constructor */
167         public EnvironmentHandle (String JavaDoc filter) {
168             this.filter = filter;
169         }
170         public Node getNode () {
171             String JavaDoc f = filter;
172             if (f == null) {
173                 // use the original node
174
f = TYPE_ENVIRONMENT;
175             }
176             
177             return find (f);
178         }
179     }
180 }
181
Popular Tags