KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28 import org.netbeans.api.progress.ProgressHandle;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectUtils;
31 import org.netbeans.spi.project.SubprojectProvider;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileStateInvalidException;
34 import org.openide.filesystems.URLMapper;
35
36 /**
37  * A main project and all (recursive) subprojects, with an optional main project.
38  * @author Jesse Glick
39  */

40 public class SubprojectsGroup extends Group {
41
42     private static final Logger JavaDoc LOG = Logger.getLogger(SubprojectsGroup.class.getName());
43
44     static final String JavaDoc KIND = "subprojects"; // NOI18N
45

46     /**
47      * Create a new group based on a superproject.
48      * The main project is always initialized to be the superproject itself,
49      * but this could be changed later.
50      * The display name is by default that of the superproject.
51      */

52     public static SubprojectsGroup create(Project project) throws FileStateInvalidException {
53         String JavaDoc path = project.getProjectDirectory().getURL().toExternalForm();
54         String JavaDoc id = sanitizeNameAndUniquifyForId(ProjectUtils.getInformation(project).getName());
55         LOG.log(Level.FINE, "Creating: {0}", id);
56         Preferences JavaDoc p = NODE.node(id);
57         p.put(KEY_KIND, KIND);
58         p.put(KEY_PATH, path);
59         p.put(KEY_MAIN, path);
60         return new SubprojectsGroup(id);
61     }
62
63     SubprojectsGroup(String JavaDoc id) {
64         super(id);
65     }
66
67     @Override JavaDoc
68     protected String JavaDoc getNameOrNull() {
69         String JavaDoc n = super.getNameOrNull();
70         if (n == null) {
71             Project p = projectForPath(prefs().get(KEY_PATH, null));
72             if (p != null) {
73                 return ProjectUtils.getInformation(p).getDisplayName();
74             }
75         }
76         return n;
77     }
78
79     protected void findProjects(Set JavaDoc<Project> projects, ProgressHandle h, int start, int end) {
80         Project p = projectForPath(prefs().get(KEY_PATH, null));
81         if (p != null) {
82             visitSubprojects(p, projects, h, new int[] {start, end});
83         }
84     }
85
86     private static void visitSubprojects(Project p, Set JavaDoc<Project> projects, ProgressHandle h, int[] startEnd) {
87         if (projects.add(p)) {
88             if (h != null) {
89                 h.progress(progressMessage(p), Math.min(++startEnd[0], startEnd[1]));
90             }
91             SubprojectProvider spp = p.getLookup().lookup(SubprojectProvider.class);
92             if (spp != null) {
93                 for (Project p2 : spp.getSubprojects()) {
94                     visitSubprojects(p2, projects, h, startEnd);
95                 }
96             }
97         }
98     }
99
100     public FileObject getMasterProjectDirectory() {
101         String JavaDoc p = prefs().get(KEY_PATH, null);
102         if (p != null && p.length() > 0) {
103             try {
104                 return URLMapper.findFileObject(new URL JavaDoc(p));
105             } catch (MalformedURLException JavaDoc x) {
106                 LOG.log(Level.WARNING, null, x);
107             }
108         }
109         return null;
110     }
111
112     public GroupEditPanel createPropertiesPanel() {
113         return new SubprojectsGroupEditPanel(this);
114     }
115
116 }
117
Popular Tags