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.modules.project.ui.groups;
043:
044: import java.io.IOException;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.util.Enumeration;
048: import java.util.Set;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import java.util.prefs.Preferences;
052: import org.netbeans.api.progress.ProgressHandle;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectManager;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileStateInvalidException;
057: import org.openide.filesystems.URLMapper;
058: import org.openide.util.Exceptions;
059:
060: /**
061: * All projects which can be found beneath some directory, with an optional main project.
062: * @author Jesse Glick
063: */
064: public class DirectoryGroup extends Group {
065:
066: private static final Logger LOG = Logger
067: .getLogger(DirectoryGroup.class.getName());
068:
069: static final String KIND = "directory"; // NOI18N
070:
071: /**
072: * Create a new group derived from a directory.
073: */
074: public static DirectoryGroup create(String name, FileObject dir)
075: throws FileStateInvalidException {
076: String path = dir.getURL().toExternalForm();
077: String id = sanitizeNameAndUniquifyForId(name);
078: LOG.log(Level.FINE, "Creating: {0}", id);
079: Preferences p = NODE.node(id);
080: p.put(KEY_NAME, name);
081: p.put(KEY_KIND, KIND);
082: p.put(KEY_PATH, path);
083: return new DirectoryGroup(id);
084: }
085:
086: DirectoryGroup(String id) {
087: super (id);
088: }
089:
090: protected void findProjects(Set<Project> projects,
091: ProgressHandle h, int start, int end) {
092: String dir = prefs().get(KEY_PATH, null);
093: FileObject fo = null;
094: try {
095: fo = URLMapper.findFileObject(new URL(dir));
096: } catch (MalformedURLException x) {
097: LOG.log(Level.WARNING, null, x);
098: }
099: if (fo != null && fo.isFolder()) {
100: try {
101: Project p = ProjectManager.getDefault().findProject(fo);
102: if (p != null) {
103: projects.add(p);
104: if (h != null) {
105: h.progress(progressMessage(p), Math.min(
106: ++start, end));
107: }
108: }
109: } catch (IOException x) {
110: Exceptions.printStackTrace(x);
111: }
112: Enumeration<? extends FileObject> e = fo.getFolders(true);
113: while (e.hasMoreElements()) {
114: try {
115: Project p = ProjectManager.getDefault()
116: .findProject(e.nextElement());
117: if (p != null) {
118: projects.add(p);
119: if (h != null) {
120: h.progress(progressMessage(p), Math.min(
121: ++start, end));
122: }
123: }
124: } catch (IOException x) {
125: Exceptions.printStackTrace(x);
126: }
127: }
128: }
129: }
130:
131: public FileObject getDirectory() {
132: String p = prefs().get(KEY_PATH, null);
133: if (p != null && p.length() > 0) {
134: try {
135: return URLMapper.findFileObject(new URL(p));
136: } catch (MalformedURLException x) {
137: LOG.log(Level.WARNING, null, x);
138: }
139: }
140: return null;
141: }
142:
143: public GroupEditPanel createPropertiesPanel() {
144: return new DirectoryGroupEditPanel(this);
145: }
146:
147: }
|