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.versioning.system.cvss.ui.actions.project;
043:
044: import org.openide.util.actions.SystemAction;
045: import org.openide.util.HelpCtx;
046: import org.openide.util.NbBundle;
047: import org.openide.util.RequestProcessor;
048: import org.openide.nodes.Node;
049: import org.openide.windows.WindowManager;
050: import org.netbeans.modules.versioning.system.cvss.util.Utils;
051: import org.netbeans.modules.versioning.system.cvss.util.Context;
052: import org.netbeans.modules.versioning.system.cvss.ui.actions.update.UpdateExecutor;
053: import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
054: import org.netbeans.modules.versioning.system.cvss.ExecutorGroup;
055: import org.netbeans.modules.versioning.spi.VCSContext;
056: import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
057: import org.netbeans.lib.cvsclient.command.GlobalOptions;
058: import org.netbeans.api.project.Project;
059: import org.netbeans.spi.project.SubprojectProvider;
060:
061: import java.io.File;
062: import java.util.*;
063: import java.awt.event.ActionEvent;
064:
065: /**
066: * Updates given project and all sources of
067: * dependee projects that are under CVS version
068: * control.
069: *
070: * @author Petr Kuzel
071: */
072: public final class UpdateWithDependenciesAction extends SystemAction {
073:
074: public UpdateWithDependenciesAction() {
075: setIcon(null);
076: putValue("noIconInMenu", Boolean.TRUE); // NOI18N
077: }
078:
079: public void actionPerformed(ActionEvent ev) {
080: setEnabled(false);
081: final Node nodes[] = WindowManager.getDefault().getRegistry()
082: .getActivatedNodes();
083: RequestProcessor.getDefault().post(new Runnable() {
084: public void run() {
085: async(nodes);
086: }
087: });
088:
089: }
090:
091: private void async(Node[] nodes) {
092:
093: ExecutorGroup group = new ExecutorGroup(NbBundle.getMessage(
094: UpdateWithDependenciesAction.class, "BK2001"));
095: try {
096: group.progress(NbBundle.getMessage(
097: UpdateWithDependenciesAction.class, "BK2002"));
098:
099: Set projects = new HashSet();
100: Set<Context> contexts = new LinkedHashSet<Context>();
101:
102: for (int i = 0; i < nodes.length; i++) {
103: Node node = nodes[i];
104: Project project = (Project) node.getLookup().lookup(
105: Project.class);
106: addUpdateContexts(contexts, project, projects);
107: }
108:
109: if (contexts.size() > 0) {
110: Context context = new Context(new HashSet(),
111: new HashSet(), new HashSet());
112: for (Context cx : contexts) {
113: context = context.union(cx);
114: }
115: UpdateCommand updateCommand = new UpdateCommand();
116: updateCommand.setBuildDirectories(true);
117: updateCommand.setPruneDirectories(true);
118: updateCommand.setFiles(context.getFiles());
119:
120: GlobalOptions gtx = CvsVersioningSystem
121: .createGlobalOptions();
122: gtx.setExclusions((File[]) context.getExclusions()
123: .toArray(new File[0]));
124: group.addExecutors(UpdateExecutor.splitCommand(
125: updateCommand, CvsVersioningSystem
126: .getInstance(), gtx, null));
127:
128: group.execute();
129: }
130: } finally {
131: setEnabled(true);
132: }
133: }
134:
135: public boolean isEnabled() {
136: Node[] nodes = WindowManager.getDefault().getRegistry()
137: .getActivatedNodes();
138: for (int i = 0; i < nodes.length; i++) {
139: Node node = nodes[i];
140: if (Utils.isVersionedProject(node) == false) {
141: return false;
142: }
143: }
144: return nodes.length > 0;
145: }
146:
147: protected boolean asynchronous() {
148: return false;
149: }
150:
151: public String getName() {
152: return NbBundle.getMessage(UpdateWithDependenciesAction.class,
153: "CTL_MenuItem_UpdateWithDependencies");
154: }
155:
156: public HelpCtx getHelpCtx() {
157: return null;
158: }
159:
160: private static void addUpdateContexts(Collection contexts,
161: Project project, Set updatedProjects) {
162: if (updatedProjects.contains(project)) {
163: return;
164: }
165: updatedProjects.add(project);
166:
167: Context ctx = createProjectContext(project);
168: if (ctx.getFiles().length > 0) {
169: contexts.add(ctx);
170: }
171:
172: SubprojectProvider deps = (SubprojectProvider) project
173: .getLookup().lookup(SubprojectProvider.class);
174: Iterator it = deps.getSubprojects().iterator();
175: while (it.hasNext()) {
176: Project subProject = (Project) it.next();
177: addUpdateContexts(contexts, subProject, updatedProjects); // RESURSION
178: }
179: }
180:
181: private static Context createProjectContext(Project project) {
182: Set files = new HashSet();
183: Set roots = new HashSet();
184: Set excludes = new HashSet();
185:
186: // remove nonversioned files
187: Utils.addProjectFiles(files, roots, excludes, project);
188: Iterator it = files.iterator();
189: while (it.hasNext()) {
190: File file = (File) it.next();
191: File probe = null;
192: if (file.isDirectory()) {
193: probe = new File(file, "CVS/Repository"); // NOI18N
194: }
195: if (file.isFile()) {
196: probe = new File(file.getParentFile(), "CVS/Repository"); // NOI18N
197: }
198: if (probe == null || probe.isFile() == false) {
199: it.remove();
200: }
201: }
202: return new Context(files, roots, excludes);
203: }
204:
205: }
|