KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > groups > DirectoryGroup


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.modules.project.ui.groups;
21
22 import java.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.prefs.Preferences JavaDoc;
30 import org.netbeans.api.progress.ProgressHandle;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileStateInvalidException;
35 import org.openide.filesystems.URLMapper;
36 import org.openide.util.Exceptions;
37
38 /**
39  * All projects which can be found beneath some directory, with an optional main project.
40  * @author Jesse Glick
41  */

42 public class DirectoryGroup extends Group {
43
44     private static final Logger JavaDoc LOG = Logger.getLogger(DirectoryGroup.class.getName());
45
46     static final String JavaDoc KIND = "directory"; // NOI18N
47

48     /**
49      * Create a new group derived from a directory.
50      */

51     public static DirectoryGroup create(String JavaDoc name, FileObject dir) throws FileStateInvalidException {
52         String JavaDoc path = dir.getURL().toExternalForm();
53         String JavaDoc id = sanitizeNameAndUniquifyForId(name);
54         LOG.log(Level.FINE, "Creating: {0}", id);
55         Preferences JavaDoc p = NODE.node(id);
56         p.put(KEY_NAME, name);
57         p.put(KEY_KIND, KIND);
58         p.put(KEY_PATH, path);
59         return new DirectoryGroup(id);
60     }
61
62     DirectoryGroup(String JavaDoc id) {
63         super(id);
64     }
65
66     protected void findProjects(Set JavaDoc<Project> projects, ProgressHandle h, int start, int end) {
67         String JavaDoc dir = prefs().get(KEY_PATH, null);
68         FileObject fo = null;
69         try {
70             fo = URLMapper.findFileObject(new URL JavaDoc(dir));
71         } catch (MalformedURLException JavaDoc x) {
72             LOG.log(Level.WARNING, null, x);
73         }
74         if (fo != null && fo.isFolder()) {
75             try {
76                 Project p = ProjectManager.getDefault().findProject(fo);
77                 if (p != null) {
78                     projects.add(p);
79                     if (h != null) {
80                         h.progress(progressMessage(p), Math.min(++start, end));
81                     }
82                 }
83             } catch (IOException JavaDoc x) {
84                 Exceptions.printStackTrace(x);
85             }
86             Enumeration JavaDoc<? extends FileObject> e = fo.getFolders(true);
87             while (e.hasMoreElements()) {
88                 try {
89                     Project p = ProjectManager.getDefault().findProject(e.nextElement());
90                     if (p != null) {
91                         projects.add(p);
92                         if (h != null) {
93                             h.progress(progressMessage(p), Math.min(++start, end));
94                         }
95                     }
96                 } catch (IOException JavaDoc x) {
97                     Exceptions.printStackTrace(x);
98                 }
99             }
100         }
101     }
102
103     public FileObject getDirectory() {
104         String JavaDoc p = prefs().get(KEY_PATH, null);
105         if (p != null && p.length() > 0) {
106             try {
107                 return URLMapper.findFileObject(new URL JavaDoc(p));
108             } catch (MalformedURLException JavaDoc x) {
109                 LOG.log(Level.WARNING, null, x);
110             }
111         }
112         return null;
113     }
114
115     public GroupEditPanel createPropertiesPanel() {
116         return new DirectoryGroupEditPanel(this);
117     }
118
119 }
120
Popular Tags