KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > project > UpdateWithDependenciesAction


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.versioning.system.cvss.ui.actions.project;
21
22 import org.openide.util.actions.SystemAction;
23 import org.openide.util.HelpCtx;
24 import org.openide.util.NbBundle;
25 import org.openide.util.RequestProcessor;
26 import org.openide.nodes.Node;
27 import org.openide.windows.WindowManager;
28 import org.netbeans.modules.versioning.system.cvss.util.Utils;
29 import org.netbeans.modules.versioning.system.cvss.util.Context;
30 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.UpdateExecutor;
31 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
32 import org.netbeans.modules.versioning.system.cvss.ExecutorGroup;
33 import org.netbeans.modules.versioning.spi.VCSContext;
34 import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
35 import org.netbeans.lib.cvsclient.command.GlobalOptions;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.spi.project.SubprojectProvider;
38
39 import java.io.File JavaDoc;
40 import java.util.*;
41 import java.awt.event.ActionEvent JavaDoc;
42
43 /**
44  * Updates given project and all sources of
45  * dependee projects that are under CVS version
46  * control.
47  *
48  * @author Petr Kuzel
49  */

50 public final class UpdateWithDependenciesAction extends SystemAction {
51
52     public UpdateWithDependenciesAction() {
53         setIcon(null);
54         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
55
}
56
57     public void actionPerformed(ActionEvent JavaDoc ev) {
58         setEnabled(false);
59         final Node nodes[] = WindowManager.getDefault().getRegistry().getActivatedNodes();
60         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
61             public void run() {
62                 async(nodes);
63             }
64         });
65
66     }
67
68     private void async(Node[] nodes) {
69
70         ExecutorGroup group = new ExecutorGroup(NbBundle.getMessage(UpdateWithDependenciesAction.class, "BK2001"));
71         try {
72             group.progress(NbBundle.getMessage(UpdateWithDependenciesAction.class, "BK2002"));
73
74             Set projects = new HashSet();
75             Set<Context> contexts = new LinkedHashSet<Context>();
76
77             for (int i = 0; i < nodes.length; i++) {
78                 Node node = nodes[i];
79                 Project project = (Project) node.getLookup().lookup(Project.class);
80                 addUpdateContexts(contexts, project, projects);
81             }
82
83             if (contexts.size() > 0) {
84                 Context context = new Context(new HashSet(), new HashSet(), new HashSet());
85                 for (Context cx : contexts) {
86                     context = context.union(cx);
87                 }
88                 UpdateCommand updateCommand = new UpdateCommand();
89                 updateCommand.setBuildDirectories(true);
90                 updateCommand.setPruneDirectories(true);
91                 updateCommand.setFiles(context.getFiles());
92
93                 GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
94                 gtx.setExclusions((File JavaDoc[]) context.getExclusions().toArray(new File JavaDoc[0]));
95                 group.addExecutors(UpdateExecutor.splitCommand(updateCommand, CvsVersioningSystem.getInstance(), gtx, org.netbeans.modules.versioning.util.Utils.getContextDisplayName(VCSContext.forNodes(nodes))));
96
97                 group.execute();
98             }
99         } finally {
100             setEnabled(true);
101         }
102     }
103
104     public boolean isEnabled() {
105         Node [] nodes = WindowManager.getDefault().getRegistry().getActivatedNodes();
106         for (int i = 0; i < nodes.length; i++) {
107             Node node = nodes[i];
108             if (Utils.isVersionedProject(node) == false) {
109                 return false;
110             }
111         }
112         return nodes.length > 0;
113     }
114
115     protected boolean asynchronous() {
116         return false;
117     }
118
119     public String JavaDoc getName() {
120         return NbBundle.getMessage(UpdateWithDependenciesAction.class, "CTL_MenuItem_UpdateWithDependencies");
121     }
122
123     public HelpCtx getHelpCtx() {
124         return null;
125     }
126
127
128     private static void addUpdateContexts(Collection contexts, Project project, Set updatedProjects) {
129         if (updatedProjects.contains(project)) {
130             return;
131         }
132         updatedProjects.add(project);
133
134         Context ctx = createProjectContext(project);
135         if (ctx.getFiles().length > 0) {
136             contexts.add(ctx);
137         }
138
139         SubprojectProvider deps = (SubprojectProvider) project.getLookup().lookup(SubprojectProvider.class);
140         Iterator it = deps.getSubprojects().iterator();
141         while (it.hasNext()) {
142             Project subProject = (Project) it.next();
143             addUpdateContexts(contexts, subProject, updatedProjects); // RESURSION
144
}
145     }
146
147     private static Context createProjectContext(Project project) {
148         Set files = new HashSet();
149         Set roots = new HashSet();
150         Set excludes = new HashSet();
151
152         // remove nonversioned files
153
Utils.addProjectFiles(files, roots, excludes, project);
154         Iterator it = files.iterator();
155         while (it.hasNext()) {
156             File JavaDoc file = (File JavaDoc) it.next();
157             File JavaDoc probe = null;
158             if (file.isDirectory()) {
159                 probe = new File JavaDoc(file, "CVS/Repository"); // NOI18N
160
}
161             if (file.isFile()) {
162                 probe = new File JavaDoc(file.getParentFile(), "CVS/Repository"); // NOI18N
163
}
164             if (probe == null || probe.isFile() == false) {
165                 it.remove();
166             }
167         }
168         return new Context(files, roots, excludes);
169     }
170
171 }
172
Popular Tags