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.io.IOException;
045: import javax.swing.event.ChangeListener;
046: import org.openide.filesystems.FileObject;
047: import org.openide.filesystems.FileSystem;
048: import org.openide.filesystems.FileUtil;
049: import org.openide.filesystems.Repository;
050: import org.openide.loaders.DataFolder;
051: import org.openide.nodes.Node;
052: import org.openide.util.ChangeSupport;
053:
054: /** Important places in the system.
055: *
056: * @author Jaroslav Tulach
057: */
058: public final class NbPlaces extends Object {
059: private final ChangeSupport cs = new ChangeSupport(this );
060:
061: /** No instance outside this class.
062: */
063: private NbPlaces() {
064: }
065:
066: private static NbPlaces DEFAULT;
067:
068: /** Getter for default instance.
069: */
070: public static synchronized NbPlaces getDefault() {
071: if (DEFAULT == null) {
072: DEFAULT = new NbPlaces();
073: }
074: return DEFAULT;
075: }
076:
077: public void addChangeListener(ChangeListener l) {
078: cs.addChangeListener(l);
079: }
080:
081: public void removeChangeListener(ChangeListener l) {
082: cs.removeChangeListener(l);
083: }
084:
085: void fireChange() {
086: cs.fireChange();
087: }
088:
089: /** Environment node. Place for all transient information about
090: * the IDE.
091: */
092: public Node environment() {
093: return EnvironmentNode.find(EnvironmentNode.TYPE_ENVIRONMENT);
094: }
095:
096: /** Session node */
097: public Node session() {
098: return EnvironmentNode.find(EnvironmentNode.TYPE_SESSION);
099: }
100:
101: /** Root nodes.
102: */
103: public Node[] roots() {
104: return EnvironmentNode.find(EnvironmentNode.TYPE_ROOTS)
105: .getChildren().getNodes();
106: }
107:
108: /** Default folder for toolbars.
109: */
110: public DataFolder toolbars() {
111: return findSessionFolder("Toolbars"); // NOI18N
112: }
113:
114: /** Default folder for menus.
115: */
116: public DataFolder menus() {
117: return findSessionFolder("Menu"); // NOI18N
118: }
119:
120: /** Default folder for actions pool.
121: */
122: public DataFolder actions() {
123: return findSessionFolder("Actions"); // NOI18N
124: }
125:
126: /**
127: * Returns a DataFolder subfolder of the session folder. In the DataFolder
128: * folders go first (sorted by name) followed by the rest of objects sorted
129: * by name.
130: */
131: public static DataFolder findSessionFolder(String name) {
132: try {
133: FileSystem fs = Repository.getDefault()
134: .getDefaultFileSystem();
135: FileObject fo = fs.findResource(name);
136: if (fo == null) {
137: // resource not found, try to create new folder
138: fo = FileUtil.createFolder(fs.getRoot(), name);
139: }
140: return DataFolder.findFolder(fo);
141: } catch (IOException ex) {
142: throw (IllegalStateException) new IllegalStateException(
143: "Folder not found and cannot be created: " + name)
144: .initCause(ex); // NOI18N
145: }
146: }
147:
148: }
|