KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > Command


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 Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.command;
23
24 import java.io.*;
25
26 import org.netbeans.lib.cvsclient.*;
27 import org.netbeans.lib.cvsclient.connection.*;
28 import org.netbeans.lib.cvsclient.event.*;
29
30 /**
31  * All commands must extend this class. A command is essentially a
32  * collection of requests that make up what is logically known as a CVS
33  * command (from a user's perspective). Commands correspond to operations the
34  * user can perform with CVS, for example checkout a module or perform a
35  * diff on two file versions.<br>
36  * Commands are automatically added as CVS event listeners. They can act
37  * on particular events and perhaps fire new events.
38  *
39  * @author Robert Greig
40  */

41 public abstract class Command
42         implements CVSListener, Cloneable JavaDoc {
43     /**
44      * The local directory from which the command is being run.
45      * This gives us the ability to construct a full pathname for the file
46      * which we are processing. The information from the responses alone is
47      * not enough.
48      */

49     protected String JavaDoc localDirectory;
50     
51     /**
52      * The global options.
53      */

54     private GlobalOptions globalOptions;
55     
56     private boolean failed = false;
57
58     private String JavaDoc displayName;
59
60     /**
61      * Execute this command.
62      * @param client the client services object that provides any necessary
63      * services to this command, including the ability to actually
64      * process all the requests
65      * @param e the event manager. The command can use this to fire events
66      * if necessary - for example, while parsing status responses.
67      * @return Whether the execution was successfull
68      */

69     public void execute(ClientServices client, EventManager eventManager)
70             throws CommandException, CommandAbortedException, AuthenticationException {
71         setLocalDirectory(client.getLocalPath());
72         this.globalOptions = client.getGlobalOptions();
73     }
74
75     /**
76      * This method returns how the command would looklike when typed on the
77      * command line.
78      *
79      * Each command is responsible for constructing this information.
80      *
81      * @returns <command's name> [<parameters>] files/dirs. Example: checkout -p CvsCommand.java
82      */

83     public abstract String JavaDoc getCVSCommand();
84
85     /**
86      * Returns the arguments of the command in the command-line style.
87      * Similar to getCVSCommand() however without the files and command's name.
88      */

89     public abstract String JavaDoc getCVSArguments();
90
91     /**
92      * Takes the arguments and sets the command.
93      * To be mainly used for automatic settings (like parsing the .cvsrc file).
94      *
95      * @return true if the option (switch) was recognized and set
96      */

97     public abstract boolean setCVSCommand(char opt, String JavaDoc optArg);
98
99     /**
100      * Resets all switches in the command to the default behaviour.
101      * After calling this method, the command should behave defaultly.
102      */

103     public abstract void resetCVSCommand();
104
105     /**
106      * Returns a String that defines which options are available for this
107      * particular command.
108      */

109     public abstract String JavaDoc getOptString();
110
111     /**
112      * This method just calls the Object.clone() and makes it public.
113      */

114     public Object JavaDoc clone() {
115         try {
116             return super.clone();
117         }
118         catch (CloneNotSupportedException JavaDoc ex) {
119             return null;
120         }
121     }
122     
123     public boolean hasFailed() {
124         return failed;
125     }
126
127     /**
128      * Called when the server wants to send a message to be displayed to
129      * the user. The message is only for information purposes and clients
130      * can choose to ignore these messages if they wish.
131      * @param e the event
132      */

133     public void messageSent(MessageEvent e) {
134         if (e.isError() && (e.getSource() instanceof org.netbeans.lib.cvsclient.response.ErrorResponse)
135                             || e.getSource() == this) {
136             // We need to ignore ErrorMessageResponse
137
failed = true;
138         }
139     }
140     
141     public void messageSent(BinaryMessageEvent e) {
142         
143     }
144
145     /**
146      * Called when a file has been added.
147      * @param e the event
148      */

149     public void fileAdded(FileAddedEvent e) {
150     }
151
152     /**
153      * Called when a file is going to be removed.
154      * @param e the event
155      */

156     public void fileToRemove(FileToRemoveEvent e) {
157     }
158
159     /**
160      * Called when a file is removed.
161      * @param e the event
162      */

163     public void fileRemoved(FileRemovedEvent e) {
164     }
165
166     /**
167      * Called when a file has been updated.
168      * @param e the event
169      */

170     public void fileUpdated(FileUpdatedEvent e) {
171     }
172
173     /**
174      * Called when file status information has been received.
175      */

176     public void fileInfoGenerated(FileInfoEvent e) {
177     }
178
179     /**
180      * Called when server responses with "ok" or "error", (when the command finishes).
181      */

182     public void commandTerminated(TerminationEvent e) {
183     }
184
185     /**
186      * This is called when the servers has responded to an expand-modules
187      * request.
188      */

189     public void moduleExpanded(ModuleExpansionEvent e) {
190     }
191
192     /**
193      * Returns the local path the command is associated with.
194      */

195     public final String JavaDoc getLocalDirectory() {
196         return localDirectory;
197     }
198
199     /**
200      * Returns the local path the command is associated with.
201      * @deprecated Please use the getLocalDirectory() method instead.
202      */

203     public final String JavaDoc getLocalPath() {
204         return localDirectory;
205     }
206     
207     /**
208      * Get the global options.
209      */

210     public final GlobalOptions getGlobalOptions() {
211         return globalOptions;
212     }
213     
214     /**
215      * Returns the relative path of the specified file (relative to the set
216      * local path).
217      * Backward slashes will be replaced by forward slashes.
218      */

219     public final String JavaDoc getRelativeToLocalPathInUnixStyle(File file) {
220         String JavaDoc filePath = file.getAbsolutePath();
221         int startIndex = localDirectory.length() + 1;
222         if (startIndex >= filePath.length()) {
223             return "."; //NOI18N
224
}
225         String JavaDoc relativePath = filePath.substring(startIndex);
226         return relativePath.replace('\\', '/');
227     }
228
229     /**
230      * Sets the local directory for the command.
231      */

232     protected final void setLocalDirectory(String JavaDoc localDirectory) {
233         this.localDirectory = localDirectory;
234     }
235
236     /**
237      * Returns the trimmed version of the specified String s.
238      * The returned String is null if the specified String is null or contains
239      * only white spaces.
240      */

241     protected static final String JavaDoc getTrimmedString(String JavaDoc s) {
242         if (s == null) {
243             return null;
244         }
245
246         s = s.trim();
247         if (s.length() == 0) {
248             return null;
249         }
250
251         return s;
252     }
253
254     /**
255      * Defines prefered display name or <code>null</code>.
256      * Localized string should highlight command purpose (use verb in gerund).
257      * E.g. <code>UpdateCommand</code> used to refresh statuses should
258      * be named "Refreshing Status" rather than "cvs -N update",
259      * "Updating" or "Status Refresh".
260      */

261     public void setDisplayName(String JavaDoc name) {
262         this.displayName = name;
263     }
264
265     /**
266      * Returns localized name describing command purpose
267      * or <code>null</code>.
268      *
269      * @see #getCVSCommand()
270      */

271     public String JavaDoc getDisplayName() {
272         return displayName;
273     }
274 }
275
Popular Tags