KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > add > AddExecutor


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.add;
21
22 import org.netbeans.modules.versioning.system.cvss.*;
23 import org.netbeans.lib.cvsclient.command.GlobalOptions;
24 import org.netbeans.lib.cvsclient.command.add.AddCommand;
25 import org.netbeans.lib.cvsclient.command.add.AddInformation;
26 import org.openide.ErrorManager;
27 import org.openide.util.NbBundle;
28
29 import java.util.*;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34  * Executes a given 'add' command and refreshes file statuses.
35  *
36  * @author Maros Sandor
37  */

38 public class AddExecutor extends ExecutorSupport {
39
40     /**
41      * Splits the original command into more commands if the original
42      * command would execute on incompatible files.
43      * See {@link #prepareBasicCommand(org.netbeans.lib.cvsclient.command.BasicCommand)}
44      * for more information.
45      *
46      * @param cmd command to execute
47      * @param cvs CVS engine to use
48      * @param options global option for the command
49      * @return array of executors that will execute the command (or array of splitted commands)
50      */

51     public static AddExecutor[] splitCommand(AddCommand cmd, CvsVersioningSystem cvs, GlobalOptions options) {
52
53         List fileSets = new ArrayList();
54         
55         File JavaDoc [] files = getNewDirectories(cmd.getFiles());
56         if (files.length > 0) {
57             try {
58                 File JavaDoc [][] sets = splitFiles(files);
59                 for (int i = 0; i < sets.length; i++) {
60                     File JavaDoc[] set = sets[i];
61                     Arrays.sort(set, byLengthComparator);
62                     fileSets.add(set);
63                 }
64             } catch (IOException JavaDoc e) {
65                 ErrorManager.getDefault().notify(e);
66                 return null;
67             }
68         }
69         
70         try {
71             File JavaDoc [][] sets = splitFiles(cmd.getFiles());
72             fileSets.addAll(Arrays.asList(sets));
73         } catch (IOException JavaDoc e) {
74             ErrorManager.getDefault().notify(e);
75             return null;
76         }
77         
78         AddCommand [] commands = new AddCommand[fileSets.size()];
79         for (int i = 0; i < commands.length; i++) {
80             commands[i] = (AddCommand) cmd.clone();
81             commands[i].setFiles((File JavaDoc[]) fileSets.get(i));
82         }
83         
84         AddExecutor [] executors = new AddExecutor[commands.length];
85         for (int i = 0; i < commands.length; i++) {
86             AddCommand command = commands[i];
87             int len = command.getFiles().length;
88             String JavaDoc param = len == 1 ?
89                     command.getFiles()[0].getName() :
90                     NbBundle.getMessage(AddExecutor.class, "MSG_AddExecutor_CmdDisplayXfiles", Integer.toString(len));
91             command.setDisplayName(NbBundle.getMessage(AddExecutor.class, "MSG_AddExecutor_CmdDisplayName", param));
92             executors[i] = new AddExecutor(cvs, command, options);
93         }
94         return executors;
95     }
96
97     private static File JavaDoc[] getNewDirectories(File JavaDoc[] files) {
98         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
99         Set newDirs = new HashSet();
100         for (int i = 0; i < files.length; i++) {
101             File JavaDoc parent = files[i].getParentFile();
102             for (;;) {
103                 if (cache.getStatus(parent).getStatus() == FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY) {
104                     newDirs.add(parent);
105                 } else {
106                     break;
107                 }
108                 parent = parent.getParentFile();
109                 if (parent == null) break;
110             }
111         }
112         List dirs = new ArrayList(newDirs);
113         return (File JavaDoc []) dirs.toArray(new File JavaDoc[dirs.size()]);
114     }
115
116     private static final Comparator byLengthComparator = new Comparator() {
117         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
118             File JavaDoc a = (File JavaDoc) o1;
119             File JavaDoc b = (File JavaDoc) o2;
120             return a.getAbsolutePath().length() - b.getAbsolutePath().length();
121         }
122     };
123     
124     private AddExecutor(CvsVersioningSystem cvs, AddCommand cmd, GlobalOptions options) {
125         super(cvs, cmd, options);
126     }
127
128     protected void commandFinished(ClientRuntime.Result result) {
129         Set parents = new HashSet();
130         // TODO: refresh ALL files that were given as arguments + their parent directories
131
// TODO: refresh ONLY if those files are already cached
132
for (Iterator i = toRefresh.iterator(); i.hasNext();) {
133             AddInformation addInformation = (AddInformation) i.next();
134             File JavaDoc file = addInformation.getFile();
135             cache.refreshCached(file, addInformation.getType().charAt(0));
136             parents.add(file.getParentFile());
137         }
138         toRefresh.clear();
139         
140         for (Iterator i = parents.iterator(); i.hasNext();) {
141             File JavaDoc dir = (File JavaDoc) i.next();
142             cache.refreshCached(dir, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
143         }
144     }
145 }
146
Popular Tags