KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > watch > WatchCommand


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.watch;
20
21 import java.io.*;
22
23 import org.netbeans.lib.cvsclient.*;
24 import org.netbeans.lib.cvsclient.command.*;
25 import org.netbeans.lib.cvsclient.connection.*;
26 import org.netbeans.lib.cvsclient.event.*;
27 import org.netbeans.lib.cvsclient.request.*;
28 import org.netbeans.lib.cvsclient.util.*;
29
30 /**
31  * @author Thomas Singer
32  */

33 public class WatchCommand extends BasicCommand {
34     private WatchMode watchMode;
35
36     private Watch watch;
37
38     /**
39      * Construct a new WatchCommand.
40      */

41     public WatchCommand() {
42         resetCVSCommand();
43     }
44
45     /**
46      * Executes this command.
47      *
48      * @param client the client services object that provides any necessary
49      * services to this command, including the ability to actually
50      * process all the requests
51      * @param eventManager the EventManager used for sending events
52      *
53      * @throws IllegalStateException if the commands options aren't set correctly
54      * @throws AuthenticationException if the connection could not be established
55      * @throws CommandException if some other thing gone wrong
56      */

57     public void execute(ClientServices client, EventManager eventManager)
58             throws CommandException, AuthenticationException {
59         checkState();
60
61         client.ensureConnection();
62
63         try {
64             super.execute(client, eventManager);
65
66             if (getWatchMode().isWatchOptionAllowed()) {
67                 String JavaDoc[] arguments = getWatchNotNull().getArguments();
68                 for (int i = 0; i < arguments.length; i++) {
69                     addRequest(new ArgumentRequest("-a")); // NOI18N
70
addRequest(new ArgumentRequest(arguments[i]));
71                 }
72             }
73
74             addRequestForWorkingDirectory(client);
75             addArgumentRequests();
76             addRequest(getWatchMode().getCommand());
77
78             client.processRequests(requests);
79         }
80         catch (CommandException ex) {
81             throw ex;
82         }
83         catch (Exception JavaDoc ex) {
84             throw new CommandException(ex, ex.getLocalizedMessage());
85         }
86         finally {
87             requests.clear();
88         }
89     }
90
91     /**
92      * If a builder was set-up, it's outputDone() method is called.
93      * This method is called, when the server responses with "ok" or "error"
94      * (== when the command finishes).
95      */

96     public void commandTerminated(TerminationEvent e) {
97         if (builder != null) {
98             builder.outputDone();
99         }
100     }
101
102     /**
103      * Uses the specified argument to set the appropriate properties.
104      * To be mainly used for automatic settings (like parsing the .cvsrc file)
105      *
106      * @return whether the option (switch) was recognized and set
107      */

108     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
109         if (opt == 'R') {
110             setRecursive(true);
111         }
112         else if (opt == 'l') {
113             setRecursive(false);
114         }
115         else {
116             return false;
117         }
118         return true;
119     }
120
121     /**
122      * String returned by this method defines which options are available for
123      * this command.
124      */

125     public String JavaDoc getOptString() {
126         return "Rl"; //NOI18N
127
}
128
129     /**
130      * Resets all switches in this command.
131      * After calling this method, the command behaves like newly created.
132      */

133     public void resetCVSCommand() {
134         setRecursive(true);
135         setWatch(null);
136     }
137
138     /**
139      * Returns how this command would look like when typed on the command line.
140      */

141     public String JavaDoc getCVSCommand() {
142         StringBuffer JavaDoc cvsCommand = new StringBuffer JavaDoc("watch "); //NOI18N
143
cvsCommand.append(getCVSArguments());
144         appendFileArguments(cvsCommand);
145         return cvsCommand.toString();
146     }
147
148     /**
149      * Returns the arguments of the command in the command-line style.
150      * Similar to getCVSCommand() however without the files and command's name
151      */

152     public String JavaDoc getCVSArguments() {
153         checkState();
154
155         StringBuffer JavaDoc cvsArguments = new StringBuffer JavaDoc();
156         cvsArguments.append(getWatchMode().toString());
157         cvsArguments.append(' ');
158
159         if (!isRecursive()) {
160             cvsArguments.append("-l "); //NOI18N
161
}
162
163         if (getWatchMode().isWatchOptionAllowed()) {
164             cvsArguments.append("-a ");
165             cvsArguments.append(getWatchNotNull().toString());
166         }
167         return cvsArguments.toString();
168     }
169
170     /**
171      * Returns the WatchMode.
172      */

173     public WatchMode getWatchMode() {
174         return watchMode;
175     }
176
177     /**
178      * Sets the WatchMode.
179      */

180     public void setWatchMode(WatchMode watchMode) {
181         this.watchMode = watchMode;
182     }
183
184     /**
185      * Returns the watch.
186      */

187     public Watch getWatch() {
188         return watch;
189     }
190
191     private Watch getWatchNotNull() {
192         if (watch == null) {
193             return Watch.ALL;
194         }
195         return watch;
196     }
197
198     /**
199      * Sets the watch.
200      * If the WatchMode ADD or REMOVE is used, null is the same as Watch.ALL.
201      * If the WatchMode ON or OFF is used, this option isn't used at all.
202      */

203     public void setWatch(Watch watch) {
204         this.watch = watch;
205     }
206
207     private void checkState() {
208         if (getWatchMode() == null) {
209             throw new IllegalStateException JavaDoc("Watch mode expected!");
210         }
211     }
212 }
213
Popular Tags