KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > annotate > RannotateCommand


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 package org.netbeans.lib.cvsclient.command.annotate;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.netbeans.lib.cvsclient.*;
25 import org.netbeans.lib.cvsclient.command.*;
26 import org.netbeans.lib.cvsclient.connection.*;
27 import org.netbeans.lib.cvsclient.event.*;
28 import org.netbeans.lib.cvsclient.request.*;
29
30 /**
31  * The rannotate command is similar to anootate, but doens't operate on currently checked
32  * out sources.
33
34  *
35  * @author MIlos Kleint
36  */

37 public class RannotateCommand extends BasicCommand {
38
39
40     /**
41      * The modules to checkout. These names are unexpanded and will be passed
42      * to a module-expansion request.
43      */

44     private final List modules = new LinkedList();
45
46     /**
47      * The expanded modules.
48      */

49     private final List expandedModules = new LinkedList();
50     
51     /**
52      * Use head revision if a revision meeting criteria set by switches -r/-D
53      * (tag/date) is not found.
54      */

55     private boolean useHeadIfNotFound;
56
57     /**
58      * equals the -D switch of command line cvs.
59      */

60     private String JavaDoc annotateByDate;
61
62     /**
63      * Equals the -r switch of command-line cvs.
64      */

65     private String JavaDoc annotateByRevision;
66
67     /**
68      * Holds value of property headerAndDescOnly.
69      */

70     private boolean headerAndDescOnly;
71
72     public RannotateCommand() {
73         resetCVSCommand();
74     }
75
76     /**
77      * Set the modules to export.
78      * @param theModules the names of the modules to export
79      */

80     public void setModule(String JavaDoc module) {
81         modules.add(module);
82     }
83
84     /**
85      * clears the list of modules for export.
86      */

87
88     public void clearModules() {
89         this.modules.clear();
90     }
91
92     /**
93      * Set the modules to export.
94      * @param theModules the names of the modules to export
95      */

96     public void setModules(String JavaDoc[] modules) {
97         clearModules();
98         if (modules == null) {
99             return;
100         }
101         for (int i = 0; i < modules.length; i++) {
102             String JavaDoc module = modules[i];
103             this.modules.add(module);
104         }
105     }
106
107     public String JavaDoc[] getModules() {
108         String JavaDoc[] mods = new String JavaDoc[modules.size()];
109         mods = (String JavaDoc[])modules.toArray(mods);
110         return mods;
111     }
112
113     private void processExistingModules(String JavaDoc localPath) {
114         if (expandedModules.size() == 0) {
115             return;
116         }
117
118         String JavaDoc[] directories = new String JavaDoc[expandedModules.size()];
119         directories = (String JavaDoc[])expandedModules.toArray(directories);
120         setModules(directories);
121     }
122     
123
124     /**
125      * Execute this command.
126      * @param client the client services object that provides any necessary
127      * services to this command, including the ability to actually process
128      * all the requests
129      */

130     public void execute(ClientServices client, EventManager em)
131             throws CommandException, AuthenticationException {
132
133         client.ensureConnection();
134
135         requests = new LinkedList();
136         if (client.isFirstCommand()) {
137             requests.add(new RootRequest(client.getRepository()));
138         }
139         for (Iterator it = modules.iterator(); it.hasNext();) {
140             String JavaDoc module = (String JavaDoc)it.next();
141             requests.add(new ArgumentRequest(module));
142         }
143         expandedModules.clear();
144         requests.add(new DirectoryRequest(".", client.getRepository())); //NOI18N
145
requests.add(new ExpandModulesRequest());
146         try {
147             client.processRequests(requests);
148         }
149         catch (CommandException ex) {
150             throw ex;
151         }
152         catch (Exception JavaDoc ex) {
153             throw new CommandException(ex, ex.getLocalizedMessage());
154         }
155         requests.clear();
156         postExpansionExecute(client, em);
157     }
158
159     /**
160      * This is called when the server has responded to an expand-modules
161      * request.
162      */

163     public void moduleExpanded(ModuleExpansionEvent e) {
164         expandedModules.add(e.getModule());
165     }
166
167     /**
168      * Execute this command
169      * @param client the client services object that provides any necessary
170      * services to this command, including the ability to actually process
171      * all the requests
172      */

173     private void postExpansionExecute(ClientServices client, EventManager em)
174             throws CommandException, AuthenticationException {
175
176 // processExistingModules(client.getLocalPath());
177
super.execute(client, em);
178
179         //
180
// moved modules code to the end of the other arguments --GAR
181
//
182
if (!isRecursive())
183         {
184             requests.add(1, new ArgumentRequest("-l")); //NOI18N
185
}
186         if (useHeadIfNotFound) {
187             requests.add(1, new ArgumentRequest("-f")); //NOI18N
188
}
189         if (annotateByDate != null && annotateByDate.length() > 0) {
190             requests.add(1, new ArgumentRequest("-D")); //NOI18N
191
requests.add(2, new ArgumentRequest(getAnnotateByDate()));
192         }
193         if (annotateByRevision != null && annotateByRevision.length() > 0) {
194             requests.add(1, new ArgumentRequest("-r")); //NOI18N
195
requests.add(2, new ArgumentRequest(getAnnotateByRevision()));
196         }
197
198
199         for (Iterator it = modules.iterator(); it.hasNext();) {
200             String JavaDoc module = (String JavaDoc)it.next();
201             requests.add(new ArgumentRequest(module));
202         }
203
204         requests.add(new DirectoryRequest(".", client.getRepository())); //NOI18N
205
requests.add(CommandRequest.RANNOTATE);
206         try {
207             client.processRequests(requests);
208             requests.clear();
209
210         }
211         catch (CommandException ex) {
212             throw ex;
213         }
214         catch (Exception JavaDoc ex) {
215             throw new CommandException(ex, ex.getLocalizedMessage());
216         }
217     }
218
219
220     public String JavaDoc getCVSCommand() {
221         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("rannotate "); //NOI18N
222
toReturn.append(getCVSArguments());
223         if (modules != null && modules.size() > 0) {
224             for (Iterator it = modules.iterator(); it.hasNext();) {
225                 String JavaDoc module = (String JavaDoc)it.next();
226                 toReturn.append(module);
227                 toReturn.append(' ');
228             }
229         }
230         else {
231             String JavaDoc localizedMsg = CommandException.getLocalMessage("ExportCommand.moduleEmpty.text"); //NOI18N
232
toReturn.append(" "); //NOI18N
233
toReturn.append(localizedMsg);
234         }
235         return toReturn.toString();
236     }
237
238     public String JavaDoc getCVSArguments() {
239         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc(""); //NOI18N
240
if (!isRecursive()) {
241             toReturn.append("-l "); //NOI18N
242
}
243         if (getAnnotateByRevision() != null) {
244             toReturn.append("-r "); //NOI18N
245
toReturn.append(getAnnotateByRevision());
246             toReturn.append(" "); //NOI18N
247
}
248         if (getAnnotateByDate() != null) {
249             toReturn.append("-D "); //NOI18N
250
toReturn.append(getAnnotateByDate());
251             toReturn.append(" "); //NOI18N
252
}
253         if (isUseHeadIfNotFound()) {
254             toReturn.append("-f "); //NOI18N
255
}
256         return toReturn.toString();
257     }
258
259     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
260         if (opt == 'R') {
261             setRecursive(true);
262         }
263         else if (opt == 'l') {
264             setRecursive(false);
265         }
266         else if (opt == 'r') {
267             setAnnotateByRevision(optArg);
268         }
269         else if (opt == 'D') {
270             setAnnotateByDate(optArg);
271         }
272         else if (opt == 'f') {
273             setUseHeadIfNotFound(true);
274         }
275         else {
276             return false;
277         }
278         return true;
279     }
280
281     public void resetCVSCommand() {
282         setRecursive(true);
283         setAnnotateByDate(null);
284         setAnnotateByRevision(null);
285         setUseHeadIfNotFound(false);
286     }
287
288     /**
289      * String returned by this method defines which options are available for this particular command
290      */

291     public String JavaDoc getOptString() {
292         return "Rlr:D:f"; //NOI18N
293
}
294
295
296     /**
297      * Create a builder for this command.
298      * @param eventMan the event manager used to receive events.
299      */

300     public Builder createBuilder(EventManager eventMan) {
301         return new AnnotateBuilder(eventMan, this);
302
303     }
304     
305     /** called when server responses with "ok" or "error", (when the command finishes)
306      */

307     public void commandTerminated(TerminationEvent e) {
308         if (builder != null) {
309             builder.outputDone();
310         }
311     }
312
313     /**
314      * Getter for property useHeadIfNotFound.
315      * @return Value of property useHeadIfNotFound.
316      */

317     public boolean isUseHeadIfNotFound() {
318         return useHeadIfNotFound;
319     }
320
321     /**
322      * Setter for property useHeadIfNotFound.
323      * @param useHeadIfNotFound New value of property useHeadIfNotFound.
324      */

325     public void setUseHeadIfNotFound(boolean useHeadIfNotFound) {
326         this.useHeadIfNotFound = useHeadIfNotFound;
327     }
328
329     /**
330      * Getter for property annotateByDate.
331      * @return Value of property annotateByDate.
332      */

333     public String JavaDoc getAnnotateByDate() {
334         return annotateByDate;
335     }
336
337     /**
338      * Setter for property annotateByDate.
339      * @param annotateByDate New value of property annotateByDate.
340      */

341     public void setAnnotateByDate(String JavaDoc annotateByDate) {
342         this.annotateByDate = annotateByDate;
343     }
344
345     /**
346      * Getter for property annotateByRevision.
347      * @return Value of property annotateByRevision.
348      */

349     public String JavaDoc getAnnotateByRevision() {
350         return annotateByRevision;
351     }
352
353     /**
354      * Setter for property annotateByRevision.
355      * @param annotateByRevision New value of property annotateByRevision.
356      */

357     public void setAnnotateByRevision(String JavaDoc annotateByRevision) {
358         this.annotateByRevision = annotateByRevision;
359     }
360     
361 }
362
Popular Tags