KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > tag > TagCommand


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 the CVS Client Library.
16  * The Initial Developer of the Original Software is Thomas Singer.
17  * Portions created by Robert Greig are Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Thomas Singer.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.command.tag;
23
24 import java.io.*;
25
26 import org.netbeans.lib.cvsclient.*;
27 import org.netbeans.lib.cvsclient.command.*;
28 import org.netbeans.lib.cvsclient.connection.*;
29 import org.netbeans.lib.cvsclient.event.*;
30 import org.netbeans.lib.cvsclient.request.*;
31
32 /**
33  * The tag command adds or deleted a tag to the specified files/directories.
34  *
35  * @author Thomas Singer
36  */

37 public class TagCommand extends BasicCommand {
38     /**
39      * The event manager to use.
40      */

41     private EventManager eventManager;
42
43     private boolean checkThatUnmodified;
44
45     private boolean deleteTag;
46
47     private boolean makeBranchTag;
48
49     private boolean overrideExistingTag;
50     
51     private boolean matchHeadIfRevisionNotFound;
52
53     private String JavaDoc tag;
54
55     private String JavaDoc tagByDate;
56
57     private String JavaDoc tagByRevision;
58
59     /**
60      * Construct a new tag command.
61      */

62     public TagCommand() {
63     }
64
65     /**
66      * Creates the TagBuilder.
67      * @param eventManager the event manager used to received cvs events
68      */

69     public Builder createBuilder(EventManager eventManager) {
70         return new TagBuilder(eventManager, getLocalDirectory());
71     }
72
73     /**
74      * Returns true if checking for unmodified files is enabled.
75      * @deprecated
76      */

77     public boolean doesCheckThatUnmodified() {
78         return checkThatUnmodified;
79     }
80
81     /**
82      * Returns true if checking for unmodified files is enabled.
83      */

84     public boolean isCheckThatUnmodified() {
85         return checkThatUnmodified;
86     }
87
88     /**
89      * Enabled the check for unmodified files.
90      */

91     public void setCheckThatUnmodified(boolean checkThatUnmodified) {
92         this.checkThatUnmodified = checkThatUnmodified;
93     }
94
95     /**
96      * Returnes true if the tag should be deleted (otherwise added).
97      * @deprecated
98      */

99     public boolean doesDeleteTag() {
100         return deleteTag;
101     }
102
103     /**
104      * Returnes true if the tag should be deleted (otherwise added).
105      */

106     public boolean isDeleteTag() {
107         return deleteTag;
108     }
109
110     /**
111      * Sets whether the tag should be deleted (true) or added (false).
112      */

113     public void setDeleteTag(boolean deleteTag) {
114         this.deleteTag = deleteTag;
115     }
116
117     /**
118      * Returns true if the tag should be a branch tag.
119      * @deprecated
120      */

121     public boolean doesMakeBranchTag() {
122         return makeBranchTag;
123     }
124
125     /**
126      * Returns true if the tag should be a branch tag.
127      */

128     public boolean isMakeBranchTag() {
129         return makeBranchTag;
130     }
131
132     /**
133      * Sets whether the tag should be a branch tag.
134      */

135     public void setMakeBranchTag(boolean makeBranchTag) {
136         this.makeBranchTag = makeBranchTag;
137     }
138
139     /**
140      * Returns true to indicate that existing tag will be overridden.
141      * @deprecated
142      */

143     public boolean doesOverrideExistingTag() {
144         return overrideExistingTag;
145     }
146
147     /**
148      * Returns true to indicate that existing tag will be overridden.
149      */

150     public boolean isOverrideExistingTag() {
151         return overrideExistingTag;
152     }
153
154     /**
155      * Sets whether existing tags should be overridden.
156      */

157     public void setOverrideExistingTag(boolean overrideExistingTag) {
158         this.overrideExistingTag = overrideExistingTag;
159     }
160     
161     public boolean isMatchHeadIfRevisionNotFound() {
162         return matchHeadIfRevisionNotFound;
163     }
164     
165     public void setMatchHeadIfRevisionNotFound(boolean matchHeadIfRevisionNotFound) {
166         this.matchHeadIfRevisionNotFound = matchHeadIfRevisionNotFound;
167     }
168
169     /**
170      * Returns the tag that should be added or deleted.
171      */

172     public String JavaDoc getTag() {
173         return tag;
174     }
175
176     /**
177      * Sets the tag that should be added or deleted.
178      */

179     public void setTag(String JavaDoc tag) {
180         this.tag = tag;
181     }
182
183     /**
184      * Returns the latest date of a revision to be tagged.
185      * @return date value. the latest Revision not later ten date is tagged.
186      */

187     public String JavaDoc getTagByDate() {
188         return tagByDate;
189     }
190
191     /**
192      * Sets the latest date of a revision to be tagged.
193      * @param tagDate New value of property tagDate.
194      */

195     public void setTagByDate(String JavaDoc tagDate) {
196         tagByDate = tagDate;
197     }
198
199     /**
200      * Sets the latest date of a revision to be tagged. Can be both a number and a tag.
201      * @return Value of property tagRevision.
202      */

203     public String JavaDoc getTagByRevision() {
204         return tagByRevision;
205     }
206
207     /**
208      * Sets the latest date of a revision to be tagged. Can be both a number and a tag.
209      * @param tagRevision New value of property tagRevision.
210      */

211     public void setTagByRevision(String JavaDoc tagRevision) {
212         tagByRevision = tagRevision;
213     }
214
215     /**
216      * Execute the command.
217      *
218      * @param client the client services object that provides any necessary
219      * services to this command, including the ability to actually
220      * process all the requests.
221      */

222     public void execute(ClientServices client, EventManager eventManager)
223             throws CommandException, AuthenticationException {
224         client.ensureConnection();
225
226         this.eventManager = eventManager;
227
228         super.execute(client, eventManager);
229
230         try {
231             requests.add(1, new ArgumentRequest(getTag()));
232
233             if (checkThatUnmodified) {
234                 requests.add(1, new ArgumentRequest("-c")); //NOI18N
235
}
236
237             if (overrideExistingTag) {
238                 requests.add(1, new ArgumentRequest("-F")); //NOI18N
239
}
240             
241             if (matchHeadIfRevisionNotFound) {
242                 requests.add(1, new ArgumentRequest("-f")); // NOI18N
243
}
244
245             if (makeBranchTag) {
246                 requests.add(1, new ArgumentRequest("-b")); //NOI18N
247
}
248
249             if (deleteTag) {
250                 requests.add(1, new ArgumentRequest("-d")); //NOI18N
251
}
252             if (tagByDate != null && tagByDate.length() > 0) {
253                 requests.add(1, new ArgumentRequest("-D")); //NOI18N
254
requests.add(2, new ArgumentRequest(getTagByDate()));
255             }
256             if (tagByRevision != null && tagByRevision.length() > 0) {
257                 requests.add(1, new ArgumentRequest("-r")); //NOI18N
258
requests.add(2, new ArgumentRequest(getTagByRevision()));
259             }
260
261             addRequestForWorkingDirectory(client);
262             addArgumentRequests();
263             addRequest(CommandRequest.TAG);
264
265             client.processRequests(requests);
266         }
267         catch (CommandException ex) {
268             throw ex;
269         }
270         catch (EOFException ex) {
271             throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); //NOI18N
272
}
273         catch (Exception JavaDoc ex) {
274             throw new CommandException(ex, ex.getLocalizedMessage());
275         }
276         finally {
277             requests.clear();
278         }
279     }
280
281     /**
282      * Called when server responses with "ok" or "error", (when the command
283      * finishes).
284      */

285     public void commandTerminated(TerminationEvent e) {
286         if (builder != null) {
287             builder.outputDone();
288         }
289     }
290
291     /**
292      * This method returns how the tag command would looklike when typed on the
293      * command line.
294      */

295     public String JavaDoc getCVSCommand() {
296         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("tag "); //NOI18N
297
toReturn.append(getCVSArguments());
298         if (getTag() != null) {
299             toReturn.append(getTag());
300             toReturn.append(" "); //NOI18N
301
}
302         File[] files = getFiles();
303         if (files != null) {
304             for (int index = 0; index < files.length; index++) {
305                 toReturn.append(files[index].getName());
306                 toReturn.append(' ');
307             }
308         }
309         return toReturn.toString();
310     }
311
312     /**
313      * Takes the arguments and sets the command.
314      * To be mainly used for automatic settings (like parsing the .cvsrc file)
315      * @return true if the option (switch) was recognized and set
316      */

317     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
318         if (opt == 'R') {
319             setRecursive(true);
320         }
321         else if (opt == 'l') {
322             setRecursive(false);
323         }
324         else if (opt == 'c') {
325             setCheckThatUnmodified(true);
326         }
327         else if (opt == 'd') {
328             setDeleteTag(true);
329         }
330         else if (opt == 'F') {
331             setOverrideExistingTag(true);
332         }
333         else if (opt == 'f') {
334             setMatchHeadIfRevisionNotFound(true);
335         }
336         else if (opt == 'b') {
337             setMakeBranchTag(true);
338         }
339         else if (opt == 'D') {
340             setTagByDate(optArg.trim());
341         }
342         else if (opt == 'r') {
343             setTagByRevision(optArg.trim());
344         }
345         else {
346             return false;
347         }
348         return true;
349     }
350
351     /**
352      * String returned by this method defines which options are available for
353      * this command.
354      */

355     public String JavaDoc getOptString() {
356         return "RlcFfbdD:r:"; //NOI18N
357
}
358
359     /**
360      * Resets all switches in the command.
361      * After calling this method, the command should have no switches defined
362      * and should behave defaultly.
363      */

364     public void resetCVSCommand() {
365         setRecursive(true);
366         setCheckThatUnmodified(false);
367         setDeleteTag(false);
368         setMakeBranchTag(false);
369         setOverrideExistingTag(false);
370         setMatchHeadIfRevisionNotFound(false);
371     }
372
373     /**
374      * Returns the arguments of the command in the command-line style.
375      * Similar to getCVSCommand() however without the files and command's name
376      */

377     public String JavaDoc getCVSArguments() {
378         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc();
379         if (!isRecursive()) {
380             toReturn.append("-l "); //NOI18N
381
}
382         if (isCheckThatUnmodified()) {
383             toReturn.append("-c "); //NOI18N
384
}
385         if (isOverrideExistingTag()) {
386             toReturn.append("-F "); //NOI18N
387
}
388         if (isMatchHeadIfRevisionNotFound()) {
389             toReturn.append("-f ");
390         }
391         if (isMakeBranchTag()) {
392             toReturn.append("-b "); //NOI18N
393
}
394         if (isDeleteTag()) {
395             toReturn.append("-d "); //NOI18N
396
}
397         if (getTagByRevision() != null && getTagByRevision().length() > 0) {
398             toReturn.append("-r "); //NOI18N
399
toReturn.append(getTagByRevision());
400             toReturn.append(" "); //NOI18N
401
}
402         if (getTagByDate() != null && getTagByDate().length() > 0) {
403             toReturn.append("-D "); //NOI18N
404
toReturn.append(getTagByDate());
405             toReturn.append(" "); //NOI18N
406
}
407         return toReturn.toString();
408     }
409 }
410
Popular Tags