KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.TreeSet 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.ui.OpenProjects;
33 import org.openide.filesystems.FileStateInvalidException;
34
35 /**
36  * Arbitrary collection of projects, with an optional main project.
37  * @author Jesse Glick
38  */

39 public class AdHocGroup extends Group {
40
41     private static final Logger JavaDoc LOG = Logger.getLogger(AdHocGroup.class.getName());
42
43     /** Preferences key for whether to automatically synchronize projects. */
44     private static final String JavaDoc KEY_AUTO_SYNCH = "autoSynch"; // NOI18N
45

46     static final String JavaDoc KIND = "adHoc"; // NOI18N
47

48     /**
49      * Create a new ad-hoc group of projects.
50      */

51     public static AdHocGroup create(String JavaDoc name, boolean autoSynch) {
52         String JavaDoc id = sanitizeNameAndUniquifyForId(name);
53         LOG.log(Level.FINE, "Creating: {0}", id);
54         Preferences JavaDoc p = NODE.node(id);
55         p.put(KEY_NAME, name);
56         p.put(KEY_KIND, KIND);
57         p.putBoolean(KEY_AUTO_SYNCH, autoSynch);
58         return new AdHocGroup(id);
59     }
60
61     AdHocGroup(String JavaDoc id) {
62         super(id);
63     }
64
65     protected void findProjects(Set JavaDoc<Project> projects, ProgressHandle h, int start, int end) {
66         String JavaDoc paths = prefs().get(KEY_PATH, "");
67         if (paths.length() > 0) { // "".split(...) -> [""]
68
String JavaDoc[] items = paths.split(" ");
69             for (String JavaDoc path : items) {
70                 Project p = projectForPath(path);
71                 if (p != null) {
72                     if (h != null) {
73                         h.progress(progressMessage(p), start += ((end - start) / items.length));
74                     }
75                     projects.add(p);
76                 }
77             }
78         }
79     }
80
81     /**
82      * Change the projects in the group.
83      */

84     public void setProjects(Set JavaDoc<Project> projects) {
85         Set JavaDoc<String JavaDoc> projectPaths = new TreeSet JavaDoc<String JavaDoc>();
86         for (Project prj : projects) {
87             try {
88                 projectPaths.add(prj.getProjectDirectory().getURL().toExternalForm());
89             } catch (FileStateInvalidException x) {
90                 LOG.log(Level.WARNING, null, x);
91             }
92         }
93         prefs().put(KEY_PATH, joinPaths(projectPaths));
94         LOG.log(Level.FINE, "updating projects for {0} to {1}", new Object JavaDoc[] {id, projects});
95     }
96
97     private static String JavaDoc joinPaths(Collection JavaDoc<String JavaDoc> paths) {
98         StringBuilder JavaDoc b = new StringBuilder JavaDoc();
99         for (String JavaDoc p : paths) {
100             if (b.length() > 0) {
101                 b.append(' ');
102             }
103             b.append(p);
104         }
105         return b.toString();
106     }
107
108     /**
109      * If true, group will automatically update its contents when open.
110      */

111     public boolean isAutoSynch() {
112         return prefs().getBoolean(KEY_AUTO_SYNCH, false);
113     }
114
115     /**
116      * @see #isAutoSynch
117      */

118     public void setAutoSynch(boolean b) {
119         prefs().putBoolean(KEY_AUTO_SYNCH, b);
120     }
121
122     /**
123      * Update a group's definition with the current list of open projects (and main project).
124      */

125     public void synch() {
126         OpenProjects op = OpenProjects.getDefault();
127         setProjects(new HashSet JavaDoc<Project>(Arrays.asList(op.getOpenProjects())));
128         setMainProject(op.getMainProject());
129     }
130
131     public GroupEditPanel createPropertiesPanel() {
132         return Group.isAdvancedMode() ? new AdHocGroupEditPanel(this) : new AdHocGroupEditPanelBasic(this);
133     }
134
135     @Override JavaDoc
136     protected void closed() {
137         if (isAutoSynch()) {
138             setProjects(new HashSet JavaDoc<Project>(Arrays.asList(OpenProjects.getDefault().getOpenProjects())));
139         }
140         // *After* setting projects - so that main project status correctly updated for new group.
141
super.closed();
142     }
143
144     @Override JavaDoc
145     public boolean isPristine() {
146         if (isAutoSynch()) {
147             return true;
148         } else {
149             return super.isPristine();
150         }
151     }
152
153     @Override JavaDoc
154     protected String JavaDoc toString(boolean scrubPersonalInfo) {
155         return super.toString(scrubPersonalInfo) + (isAutoSynch() ? "" : "[!autoSynch]");
156     }
157
158 }
159
Popular Tags