KickJava   Java API By Example, From Geeks To Geeks.

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


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.lib.cvsclient.command.annotate;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.netbeans.lib.cvsclient.*;
26 import org.netbeans.lib.cvsclient.command.*;
27 import org.netbeans.lib.cvsclient.connection.*;
28 import org.netbeans.lib.cvsclient.event.*;
29 import org.netbeans.lib.cvsclient.request.*;
30
31 /**
32  * The annotate command shows all lines of the file and annotates each line with cvs-related info.
33  * @author Milos Kleint
34  */

35 public class AnnotateCommand extends BasicCommand {
36     /**
37      * The event manager to use
38      */

39     protected EventManager eventManager;
40
41     /**
42      * Use head revision if a revision meeting criteria set by switches -r/-D
43      * (tag/date) is not found.
44      */

45     private boolean useHeadIfNotFound;
46
47     /**
48      * equals the -D switch of command line cvs.
49      */

50     private String JavaDoc annotateByDate;
51
52     /**
53      * Equals the -r switch of command-line cvs.
54      */

55     private String JavaDoc annotateByRevision;
56
57     /**
58      * Construct a new diff command
59      */

60     public AnnotateCommand() {
61     }
62
63     /**
64      * Create a builder for this command.
65      * @param eventMan the event manager used to receive events.
66      */

67     public Builder createBuilder(EventManager eventMan) {
68         return new AnnotateBuilder(eventMan, this);
69     }
70
71     /**
72      * Execute a command
73      * @param client the client services object that provides any necessary
74      * services to this command, including the ability to actually process
75      * all the requests.
76      */

77     public void execute(ClientServices client, EventManager em)
78             throws CommandException, AuthenticationException {
79         eventManager = em;
80
81         client.ensureConnection();
82
83         super.execute(client, em);
84
85         excludeBinaryFiles(requests);
86
87         try {
88             if (useHeadIfNotFound) {
89                 requests.add(1, new ArgumentRequest("-f")); //NOI18N
90
}
91             if (annotateByDate != null && annotateByDate.length() > 0) {
92                 requests.add(1, new ArgumentRequest("-D")); //NOI18N
93
requests.add(2, new ArgumentRequest(getAnnotateByDate()));
94             }
95             if (annotateByRevision != null && annotateByRevision.length() > 0) {
96                 requests.add(1, new ArgumentRequest("-r")); //NOI18N
97
requests.add(2, new ArgumentRequest(getAnnotateByRevision()));
98             }
99             addRequestForWorkingDirectory(client);
100             addArgumentRequests();
101             addRequest(CommandRequest.ANNOTATE);
102             client.processRequests(requests);
103         }
104         catch (CommandException ex) {
105             throw ex;
106         }
107         catch (Exception JavaDoc ex) {
108             throw new CommandException(ex, ex.getLocalizedMessage());
109         }
110         finally {
111             requests.clear();
112         }
113     }
114
115     private void excludeBinaryFiles(java.util.List JavaDoc requests) {
116         Iterator it = requests.iterator();
117         while (it.hasNext()) {
118             Object JavaDoc obj = it.next();
119             if (obj instanceof EntryRequest) {
120                 EntryRequest req = (EntryRequest)obj;
121                 if (req.getEntry().isBinary()) {
122                     it.remove();
123                     if (it.hasNext()) {
124                         // removes also the follwoing modified/unchanged request
125
it.next();
126                         it.remove();
127                     }
128                 }
129             }
130         }
131     }
132
133     /** called when server responses with "ok" or "error", (when the command finishes)
134      */

135     public void commandTerminated(TerminationEvent e) {
136         if (builder != null) {
137             builder.outputDone();
138         }
139     }
140
141     /**
142      * Getter for property useHeadIfNotFound.
143      * @return Value of property useHeadIfNotFound.
144      */

145     public boolean isUseHeadIfNotFound() {
146         return useHeadIfNotFound;
147     }
148
149     /**
150      * Setter for property useHeadIfNotFound.
151      * @param useHeadIfNotFound New value of property useHeadIfNotFound.
152      */

153     public void setUseHeadIfNotFound(boolean useHeadIfNotFound) {
154         this.useHeadIfNotFound = useHeadIfNotFound;
155     }
156
157     /**
158      * Getter for property annotateByDate.
159      * @return Value of property annotateByDate.
160      */

161     public String JavaDoc getAnnotateByDate() {
162         return annotateByDate;
163     }
164
165     /**
166      * Setter for property annotateByDate.
167      * @param annotateByDate New value of property annotateByDate.
168      */

169     public void setAnnotateByDate(String JavaDoc annotateByDate) {
170         this.annotateByDate = annotateByDate;
171     }
172
173     /**
174      * Getter for property annotateByRevision.
175      * @return Value of property annotateByRevision.
176      */

177     public String JavaDoc getAnnotateByRevision() {
178         return annotateByRevision;
179     }
180
181     /**
182      * Setter for property annotateByRevision.
183      * @param annotateByRevision New value of property annotateByRevision.
184      */

185     public void setAnnotateByRevision(String JavaDoc annotateByRevision) {
186         this.annotateByRevision = annotateByRevision;
187     }
188
189     /**
190      * This method returns how the command would looklike when typed on the command line.
191      * Each command is responsible for constructing this information.
192      * @returns <command's name> [<parameters>] files/dirs. Example: checkout -p CvsCommand.java
193      */

194     public String JavaDoc getCVSCommand() {
195         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("annotate "); //NOI18N
196
toReturn.append(getCVSArguments());
197         File[] files = getFiles();
198         if (files != null) {
199             for (int index = 0; index < files.length; index++) {
200                 toReturn.append(files[index].getName() + " "); //NOI18N
201
}
202         }
203         return toReturn.toString();
204     }
205
206     /** takes the arguments and sets the command. To be mainly
207      * used for automatic settings (like parsing the .cvsrc file)
208      * @return true if the option (switch) was recognized and set
209      */

210     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
211         if (opt == 'R') {
212             setRecursive(true);
213         }
214         else if (opt == 'l') {
215             setRecursive(false);
216         }
217         else if (opt == 'r') {
218             setAnnotateByRevision(optArg);
219         }
220         else if (opt == 'D') {
221             setAnnotateByDate(optArg);
222         }
223         else if (opt == 'f') {
224             setUseHeadIfNotFound(true);
225         }
226         else {
227             return false;
228         }
229         return true;
230     }
231
232     /**
233      * String returned by this method defines which options are available for this particular command
234      */

235     public String JavaDoc getOptString() {
236         return "Rlr:D:f"; //NOI18N
237
}
238
239     /**
240      * resets all switches in the command. After calling this method,
241      * the command should have no switches defined and should behave defaultly.
242      */

243     public void resetCVSCommand() {
244         setRecursive(true);
245         setAnnotateByDate(null);
246         setAnnotateByRevision(null);
247         setUseHeadIfNotFound(false);
248     }
249
250     /**
251      * Returns the arguments of the command in the command-line style.
252      * Similar to getCVSCommand() however without the files and command's name
253      */

254     public String JavaDoc getCVSArguments() {
255         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc(""); //NOI18N
256
if (!isRecursive()) {
257             toReturn.append("-l "); //NOI18N
258
}
259         if (getAnnotateByRevision() != null) {
260             toReturn.append("-r "); //NOI18N
261
toReturn.append(getAnnotateByRevision());
262             toReturn.append(" "); //NOI18N
263
}
264         if (getAnnotateByDate() != null) {
265             toReturn.append("-D "); //NOI18N
266
toReturn.append(getAnnotateByDate());
267             toReturn.append(" "); //NOI18N
268
}
269         if (isUseHeadIfNotFound()) {
270             toReturn.append("-f "); //NOI18N
271
}
272         return toReturn.toString();
273     }
274
275 }
276
Popular Tags