001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.awt.datatransfer.Transferable;
045: import org.netbeans.core.ui.LookupNode;
046: import org.openide.actions.PropertiesAction;
047: import org.openide.actions.ToolsAction;
048: import org.openide.loaders.DataFolder;
049: import org.openide.nodes.AbstractNode;
050: import org.openide.nodes.Children;
051: import org.openide.nodes.FilterNode;
052: import org.openide.nodes.Node;
053: import org.openide.util.HelpCtx;
054: import org.openide.util.Mutex;
055: import org.openide.util.NbBundle;
056: import org.openide.util.actions.SystemAction;
057: import org.openide.util.datatransfer.PasteType;
058:
059: /** This object represents environment settings in the Corona system.
060: * This class is final only for performance purposes.
061: * Can be unfinaled if desired.
062: *
063: * @author Petr Hamernik, Dafe Simonek
064: */
065: final class EnvironmentNode extends AbstractNode {
066: /** generated Serialized Version UID */
067: static final long serialVersionUID = 4782447107972624693L;
068: /** name of section to filter */
069: private String filter;
070: /** icon base for icons of this node */
071: private static final String EN_ICON_BASE = "org/netbeans/core/resources/"; // NOI18N
072: /** map between type of node and the parent node for this type */
073: private static java.util.HashMap<String, Node> types = new java.util.HashMap<String, Node>(
074: 11);
075: /** A lock for the find method. */
076: private static final Object lock = new Object();
077:
078: /** Type to add an entry to the root nodes. */
079: public static final String TYPE_ROOTS = "roots"; // NOI18N
080: /** Type to add an entry to the Environment (in the Explorer). */
081: public static final String TYPE_ENVIRONMENT = "environment"; // NOI18N
082: /** Type to add an entry to the Session settings. */
083: public static final String TYPE_SESSION = "session"; // NOI18N
084:
085: /** Constructor */
086: private EnvironmentNode(String filter, Children children) {
087: super (children);
088: this .filter = filter;
089: decorateNode(filter, this );
090: }
091:
092: /** Finds the node for given name.
093: */
094: public static Node find(final String name) {
095: // XXX this is probably obsolete? consider deleting
096: Node retValue = Children.MUTEX
097: .readAccess(new Mutex.Action<Node>() {
098: public Node run() {
099: synchronized (lock) {
100: Node n = types.get(name);
101: if (n == null) {
102: DataFolder folder = null;
103: if (TYPE_ENVIRONMENT.equals(name)) {
104: folder = NbPlaces.getDefault()
105: .findSessionFolder(
106: "UI/Runtime"); // NOI18N
107: } else if (TYPE_ROOTS.equals(name)) {
108: folder = NbPlaces.getDefault()
109: .findSessionFolder(
110: "UI/Roots");
111: } else {
112: assert TYPE_SESSION.equals(name) : name;
113: folder = NbPlaces.getDefault()
114: .findSessionFolder(
115: "UI/Services"); // NOI18N
116: }
117:
118: n = new PersistentLookupNode(name,
119: folder);
120: if (TYPE_ENVIRONMENT.equals(name)) {
121: //#118628 - don't allow drag and drop into empty
122: //area of Services window
123: n = new NoDragAndDropNode(n);
124: }
125: types.put(name, n);
126: }
127: return n;
128: }
129: }
130: });
131: if (retValue != null) {
132: return retValue;
133: }
134: throw new IllegalStateException();
135: }
136:
137: private static void decorateNode(String name, AbstractNode node) {
138: String resourceName = "CTL_" + name + "_name"; // NOI18N
139: String iconBase = EN_ICON_BASE + name.toLowerCase() + ".gif";
140:
141: node.setDisplayName(NbBundle.getMessage(EnvironmentNode.class,
142: resourceName));
143: node.setIconBaseWithExtension(iconBase);
144: }
145:
146: public HelpCtx getHelpCtx() {
147: return new HelpCtx(EnvironmentNode.class);
148: }
149:
150: /** Getter for set of actions that should be present in the
151: * popup menu of this node. This set is used in construction of
152: * menu returned from getContextMenu and specially when a menu for
153: * more nodes is constructed.
154: *
155: * @return array of system actions that should be in popup menu
156: */
157: public SystemAction[] createActions() {
158: return new SystemAction[] {
159: SystemAction.get(ToolsAction.class),
160: SystemAction.get(PropertiesAction.class) };
161: }
162:
163: /** For deserialization */
164: public Node.Handle getHandle() {
165: return new EnvironmentHandle(filter);
166: }
167:
168: /** Adds serialization support to LookupNode */
169: private static final class PersistentLookupNode extends LookupNode
170: implements java.beans.PropertyChangeListener {
171:
172: private String filter;
173:
174: public PersistentLookupNode(String filter, DataFolder folder) {
175: super (folder);
176: this .filter = filter;
177:
178: if (TYPE_ROOTS.equals(filter)) {
179: folder
180: .addPropertyChangeListener(org.openide.util.WeakListeners
181: .propertyChange(this , folder));
182: }
183: }
184:
185: public Node.Handle getHandle() {
186: return new EnvironmentHandle(filter);
187: }
188:
189: /** Listens on changes on root nodes. */
190: public void propertyChange(java.beans.PropertyChangeEvent evt) {
191: if (DataFolder.PROP_CHILDREN.equals(evt.getPropertyName())) {
192: NbPlaces.getDefault().fireChange();
193: }
194: }
195: } // end of PersistentLookupNode
196:
197: static final class EnvironmentHandle implements Node.Handle {
198: static final long serialVersionUID = -850350968366553370L;
199:
200: /** field */
201: private String filter;
202:
203: /** constructor */
204: public EnvironmentHandle(String filter) {
205: this .filter = filter;
206: }
207:
208: public Node getNode() {
209: String f = filter;
210: if (f == null) {
211: // use the original node
212: f = TYPE_ENVIRONMENT;
213: }
214:
215: return find(f);
216: }
217: }
218:
219: private static class NoDragAndDropNode extends FilterNode {
220: public NoDragAndDropNode(Node orig) {
221: super (orig);
222: }
223:
224: @Override
225: public PasteType getDropType(Transferable t, int action,
226: int index) {
227: return null;
228: }
229: }
230: }
|