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.util.Arrays;
045: import java.util.Collection;
046: import java.util.HashSet;
047: import java.util.Set;
048: import java.util.TreeSet;
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.ui.OpenProjects;
055: import org.openide.filesystems.FileStateInvalidException;
056:
057: /**
058: * Arbitrary collection of projects, with an optional main project.
059: * @author Jesse Glick
060: */
061: public class AdHocGroup extends Group {
062:
063: private static final Logger LOG = Logger.getLogger(AdHocGroup.class
064: .getName());
065:
066: /** Preferences key for whether to automatically synchronize projects. */
067: private static final String KEY_AUTO_SYNCH = "autoSynch"; // NOI18N
068:
069: static final String KIND = "adHoc"; // NOI18N
070:
071: /**
072: * Create a new ad-hoc group of projects.
073: */
074: public static AdHocGroup create(String name, boolean autoSynch) {
075: String sanitizedId = sanitizeNameAndUniquifyForId(name);
076: LOG.log(Level.FINE, "Creating: {0}", sanitizedId);
077: Preferences p = NODE.node(sanitizedId);
078: p.put(KEY_NAME, name);
079: p.put(KEY_KIND, KIND);
080: p.putBoolean(KEY_AUTO_SYNCH, autoSynch);
081: return new AdHocGroup(sanitizedId);
082: }
083:
084: AdHocGroup(String id) {
085: super (id);
086: }
087:
088: protected void findProjects(Set<Project> projects,
089: ProgressHandle h, int start, int end) {
090: String paths = prefs().get(KEY_PATH, "");
091: if (paths.length() > 0) { // "".split(...) -> [""]
092: String[] items = paths.split(" ");
093: for (String path : items) {
094: Project p = projectForPath(path);
095: if (p != null) {
096: if (h != null) {
097: h
098: .progress(
099: progressMessage(p),
100: start += ((end - start) / items.length));
101: }
102: projects.add(p);
103: }
104: }
105: }
106: }
107:
108: /**
109: * Change the projects in the group.
110: */
111: public void setProjects(Set<Project> projects) {
112: Set<String> projectPaths = new TreeSet<String>();
113: for (Project prj : projects) {
114: try {
115: projectPaths.add(prj.getProjectDirectory().getURL()
116: .toExternalForm());
117: } catch (FileStateInvalidException x) {
118: LOG.log(Level.WARNING, null, x);
119: }
120: }
121: prefs().put(KEY_PATH, joinPaths(projectPaths));
122: LOG.log(Level.FINE, "updating projects for {0} to {1}",
123: new Object[] { id, projects });
124: }
125:
126: private static String joinPaths(Collection<String> paths) {
127: StringBuilder b = new StringBuilder();
128: for (String p : paths) {
129: if (b.length() > 0) {
130: b.append(' ');
131: }
132: b.append(p);
133: }
134: return b.toString();
135: }
136:
137: /**
138: * If true, group will automatically update its contents when open.
139: */
140: public boolean isAutoSynch() {
141: return prefs().getBoolean(KEY_AUTO_SYNCH, false);
142: }
143:
144: /**
145: * @see #isAutoSynch
146: */
147: public void setAutoSynch(boolean b) {
148: prefs().putBoolean(KEY_AUTO_SYNCH, b);
149: }
150:
151: /**
152: * Update a group's definition with the current list of open projects (and main project).
153: */
154: public void synch() {
155: OpenProjects op = OpenProjects.getDefault();
156: setProjects(new HashSet<Project>(Arrays.asList(op
157: .getOpenProjects())));
158: setMainProject(op.getMainProject());
159: }
160:
161: public GroupEditPanel createPropertiesPanel() {
162: return new AdHocGroupEditPanel(this );
163: }
164:
165: @Override
166: protected void closed() {
167: if (isAutoSynch()) {
168: setProjects(new HashSet<Project>(Arrays.asList(OpenProjects
169: .getDefault().getOpenProjects())));
170: }
171: // *After* setting projects - so that main project status correctly updated for new group.
172: super .closed();
173: }
174:
175: @Override
176: public boolean isPristine() {
177: if (isAutoSynch()) {
178: return true;
179: } else {
180: return super .isPristine();
181: }
182: }
183:
184: @Override
185: protected String toString(boolean scrubPersonalInfo) {
186: return super .toString(scrubPersonalInfo)
187: + (isAutoSynch() ? "" : "[!autoSynch]");
188: }
189:
190: }
|