2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.project.ui.groups;
44
import java.util.Arrays;
45
import java.util.Collection;
46
import java.util.HashSet;
48
import java.util.TreeSet;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
51
import java.util.prefs.Preferences;
52
import org.netbeans.api.progress.ProgressHandle;
53
import org.netbeans.api.project.Project;
54
import org.netbeans.api.project.ui.OpenProjects;
55
import org.openide.filesystems.FileStateInvalidException;
58
* Arbitrary collection of projects, with an optional main project.
61
public class AdHocGroup extends Group {
63
private static final Logger LOG = Logger.getLogger(AdHocGroup.class.getName());
65
/** Preferences key for whether to automatically synchronize projects. */
66
private static final String KEY_AUTO_SYNCH = "autoSynch"; // NOI18N
68
static final String KIND = "adHoc"; // NOI18N
71
* Create a new ad-hoc group of projects.
73
public static AdHocGroup create(String name, boolean autoSynch) {
74
String sanitizedId = sanitizeNameAndUniquifyForId(name);
75
LOG.log(Level.FINE, "Creating: {0}", sanitizedId);
76
Preferences p = NODE.node(sanitizedId);
77
p.put(KEY_NAME, name);
78
p.put(KEY_KIND, KIND);
79
p.putBoolean(KEY_AUTO_SYNCH, autoSynch);
80
return new AdHocGroup(sanitizedId);
83
AdHocGroup(String id) {
87
protected void findProjects(Set<Project> projects, ProgressHandle h, int start, int end) {
88
String paths = prefs().get(KEY_PATH, "");
89
if (paths.length() > 0) { // "".split(...) -> [""]
90
String[] items = paths.split(" ");
91
for (String path : items) {
92
Project p = projectForPath(path);
95
h.progress(progressMessage(p), start += ((end - start) / items.length));
104
* Change the projects in the group.
106
public void setProjects(Set<Project> projects) {
107
Set<String> projectPaths = new TreeSet<String>();
108
for (Project prj : projects) {
110
projectPaths.add(prj.getProjectDirectory().getURL().toExternalForm());
111
} catch (FileStateInvalidException x) {
112
LOG.log(Level.WARNING, null, x);
115
prefs().put(KEY_PATH, joinPaths(projectPaths));
116
LOG.log(Level.FINE, "updating projects for {0} to {1}", new Object[] {id, projects});
119
private static String joinPaths(Collection<String> paths) {
120
StringBuilder b = new StringBuilder();
121
for (String p : paths) {
122
if (b.length() > 0) {
131
* If true, group will automatically update its contents when open.
133
public boolean isAutoSynch() {
134
return prefs().getBoolean(KEY_AUTO_SYNCH, false);
140
public void setAutoSynch(boolean b) {
141
prefs().putBoolean(KEY_AUTO_SYNCH, b);
145
* Update a group's definition with the current list of open projects (and main project).
147
public void synch() {
148
OpenProjects op = OpenProjects.getDefault();
149
setProjects(new HashSet<Project>(Arrays.asList(op.getOpenProjects())));
150
setMainProject(op.getMainProject());
153
public GroupEditPanel createPropertiesPanel() {
154
return new AdHocGroupEditPanel(this);
158
protected void closed() {
160
setProjects(new HashSet<Project>(Arrays.asList(OpenProjects.getDefault().getOpenProjects())));
162
// *After* setting projects - so that main project status correctly updated for new group.
167
public boolean isPristine() {
171
return super.isPristine();
176
protected String toString(boolean scrubPersonalInfo) {
177
return super.toString(scrubPersonalInfo) + (isAutoSynch() ? "" : "[!autoSynch]");