~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to projects/projectui/src/org/netbeans/modules/project/ui/groups/AdHocGroup.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.project.ui.groups;
 
43
 
 
44
import java.util.Arrays;
 
45
import java.util.Collection;
 
46
import java.util.HashSet;
 
47
import java.util.Set;
 
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;
 
56
 
 
57
/**
 
58
 * Arbitrary collection of projects, with an optional main project.
 
59
 * @author Jesse Glick
 
60
 */
 
61
public class AdHocGroup extends Group {
 
62
 
 
63
    private static final Logger LOG = Logger.getLogger(AdHocGroup.class.getName());
 
64
 
 
65
    /** Preferences key for whether to automatically synchronize projects. */
 
66
    private static final String KEY_AUTO_SYNCH = "autoSynch"; // NOI18N
 
67
 
 
68
    static final String KIND = "adHoc"; // NOI18N
 
69
 
 
70
    /**
 
71
     * Create a new ad-hoc group of projects.
 
72
     */
 
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);
 
81
    }
 
82
 
 
83
    AdHocGroup(String id) {
 
84
        super(id);
 
85
    }
 
86
 
 
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);
 
93
                if (p != null) {
 
94
                    if (h != null) {
 
95
                        h.progress(progressMessage(p), start += ((end - start) / items.length));
 
96
                    }
 
97
                    projects.add(p);
 
98
                }
 
99
            }
 
100
        }
 
101
    }
 
102
 
 
103
    /**
 
104
     * Change the projects in the group.
 
105
     */
 
106
    public void setProjects(Set<Project> projects) {
 
107
        Set<String> projectPaths = new TreeSet<String>();
 
108
        for (Project prj : projects) {
 
109
            try {
 
110
                projectPaths.add(prj.getProjectDirectory().getURL().toExternalForm());
 
111
            } catch (FileStateInvalidException x) {
 
112
                LOG.log(Level.WARNING, null, x);
 
113
            }
 
114
        }
 
115
        prefs().put(KEY_PATH, joinPaths(projectPaths));
 
116
        LOG.log(Level.FINE, "updating projects for {0} to {1}", new Object[] {id, projects});
 
117
    }
 
118
 
 
119
    private static String joinPaths(Collection<String> paths) {
 
120
        StringBuilder b = new StringBuilder();
 
121
        for (String p : paths) {
 
122
            if (b.length() > 0) {
 
123
                b.append(' ');
 
124
            }
 
125
            b.append(p);
 
126
        }
 
127
        return b.toString();
 
128
    }
 
129
 
 
130
    /**
 
131
     * If true, group will automatically update its contents when open.
 
132
     */
 
133
    public boolean isAutoSynch() {
 
134
        return prefs().getBoolean(KEY_AUTO_SYNCH, false);
 
135
    }
 
136
 
 
137
    /**
 
138
     * @see #isAutoSynch
 
139
     */
 
140
    public void setAutoSynch(boolean b) {
 
141
        prefs().putBoolean(KEY_AUTO_SYNCH, b);
 
142
    }
 
143
 
 
144
    /**
 
145
     * Update a group's definition with the current list of open projects (and main project).
 
146
     */
 
147
    public void synch() {
 
148
        OpenProjects op = OpenProjects.getDefault();
 
149
        setProjects(new HashSet<Project>(Arrays.asList(op.getOpenProjects())));
 
150
        setMainProject(op.getMainProject());
 
151
    }
 
152
 
 
153
    public GroupEditPanel createPropertiesPanel() {
 
154
        return new AdHocGroupEditPanel(this);
 
155
    }
 
156
 
 
157
    @Override
 
158
    protected void closed() {
 
159
        if (isAutoSynch()) {
 
160
            setProjects(new HashSet<Project>(Arrays.asList(OpenProjects.getDefault().getOpenProjects())));
 
161
        }
 
162
        // *After* setting projects - so that main project status correctly updated for new group.
 
163
        super.closed();
 
164
    }
 
165
 
 
166
    @Override
 
167
    public boolean isPristine() {
 
168
        if (isAutoSynch()) {
 
169
            return true;
 
170
        } else {
 
171
            return super.isPristine();
 
172
        }
 
173
    }
 
174
 
 
175
    @Override
 
176
    protected String toString(boolean scrubPersonalInfo) {
 
177
        return super.toString(scrubPersonalInfo) + (isAutoSynch() ? "" : "[!autoSynch]");
 
178
    }
 
179
 
 
180
}