KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > Checkout


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.client;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.team.internal.ccvs.core.*;
19 import org.eclipse.team.internal.ccvs.core.client.listeners.*;
20 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException;
21 import org.eclipse.team.internal.ccvs.core.resources.RemoteModule;
22
23 public class Checkout extends Command {
24     /*** Local options: specific to checkout ***/
25     public static final LocalOption DO_NOT_SHORTEN = new LocalOption("-N"); //$NON-NLS-1$
26
public static final LocalOption FETCH_MODULE_ALIASES = new LocalOption("-c"); //$NON-NLS-1$
27
public static LocalOption makeDirectoryNameOption(String JavaDoc moduleName) {
28         return new LocalOption("-d", moduleName); //$NON-NLS-1$
29
}
30
31     /*** Default command output listener ***/
32     private static final ICommandOutputListener DEFAULT_OUTPUT_LISTENER = new UpdateListener(null);
33     
34     /** Command options found in the CVSROOT/modules file */
35     public static LocalOption ALIAS = new LocalOption("-a"); //$NON-NLS-1$
36
public static LocalOption makeStatusOption(String JavaDoc status) {
37         return new LocalOption("-s", status); //$NON-NLS-1$
38
}
39     
40     protected Checkout() { }
41     protected String JavaDoc getRequestId() {
42         return "co"; //$NON-NLS-1$
43
}
44     
45     protected ICommandOutputListener getDefaultCommandOutputListener() {
46         return DEFAULT_OUTPUT_LISTENER;
47     }
48     
49     protected ICVSResource[] computeWorkResources(Session session, LocalOption[] localOptions,
50         String JavaDoc[] arguments) throws CVSException {
51             
52         // We shouldn't have any arguments if we're fetching the module definitions
53
if (arguments.length < 1 && ! FETCH_MODULE_ALIASES.isElementOf(localOptions)) throw new IllegalArgumentException JavaDoc();
54         
55         // We can determine the local directories using either the -d option or the module expansions
56
Option dOption = findOption(localOptions, "-d"); //$NON-NLS-1$
57
if (dOption != null) {
58             // Should we append the expansions to the -d argument?
59
return new ICVSResource[] {session.getLocalRoot().getFolder(dOption.argument)};
60         }
61         String JavaDoc[] modules = session.getModuleExpansions();
62         ICVSResource[] resources = new ICVSResource[modules.length];
63         for (int i = 0; i < resources.length; i++) {
64             resources[i] = session.getLocalRoot().getFolder(modules[i]);
65         }
66         return resources;
67     }
68     
69     /**
70      * Start the Checkout command:
71      * Send the module that is going to be checked-out to the server
72      * by reading the name of the resource given
73      * (This has to change to we give it the name of the modul and the
74      * Checkout creates everything for us)
75      */

76     protected ICVSResource[] sendLocalResourceState(Session session, GlobalOption[] globalOptions,
77         LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor)
78         throws CVSException {
79         
80         // We need a folder to put the project(s) we checkout into
81
Assert.isTrue(session.getLocalRoot().isFolder());
82         
83         // Send the information about the local workspace resources to the server
84
List JavaDoc resourcesToSend = new ArrayList JavaDoc(resources.length);
85         for (int i = 0; i < resources.length; i++) {
86             ICVSResource resource = resources[i];
87             if (resource.exists() && resource.isFolder() && ((ICVSFolder)resource).isCVSFolder()) {
88                 resourcesToSend.add(resource);
89             }
90         }
91         if ( ! resourcesToSend.isEmpty()) {
92             resources = (ICVSResource[]) resourcesToSend.toArray(new ICVSResource[resourcesToSend.size()]);
93             new FileStructureVisitor(session, localOptions, true, true).visit(session, resources, monitor);
94         } else {
95             monitor.beginTask(null, 100);
96             monitor.done();
97         }
98         return resources;
99     }
100
101     protected void sendLocalWorkingDirectory(Session session) throws CVSException {
102         session.sendConstructedRootDirectory();
103     }
104
105     /**
106      * On sucessful finish, prune empty directories if
107      * the -P option was specified (or is implied by -D or -r)
108      */

109     protected IStatus commandFinished(Session session, GlobalOption[] globalOptions,
110         LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor,
111         IStatus status) throws CVSException {
112         // If we didn't succeed, don't do any post processing
113
if (status.getCode() == CVSStatus.SERVER_ERROR) {
114             return status;
115         }
116     
117         // If we are retrieving the modules file, ignore other options
118
if (FETCH_MODULE_ALIASES.isElementOf(localOptions)) return status;
119
120         // If we are pruning (-P) or getting a sticky copy (-D or -r), then prune empty directories
121
if (PRUNE_EMPTY_DIRECTORIES.isElementOf(localOptions) ||
122             (findOption(localOptions, "-D") != null) || //$NON-NLS-1$
123
(findOption(localOptions, "-r") != null)) { //$NON-NLS-1$
124

125             // Prune empty directories
126
new PruneFolderVisitor().visit(session, resources);
127         }
128         
129         return status;
130     }
131     
132     /**
133      * Override execute to perform a expand-modules before the checkout
134      */

135     protected IStatus doExecute(Session session, GlobalOption[] globalOptions,
136         LocalOption[] localOptions, String JavaDoc[] arguments, ICommandOutputListener listener,
137         IProgressMonitor monitor) throws CVSException {
138         monitor.beginTask(null, 100);
139
140         if ( ! FETCH_MODULE_ALIASES.isElementOf(localOptions)) {
141             // Execute the expand-modules command.
142
// This will put the expansions in the session for later retrieval
143
IStatus status = Request.EXPAND_MODULES.execute(session, arguments, Policy.subMonitorFor(monitor, 10));
144             if (status.getCode() == CVSStatus.SERVER_ERROR)
145                 return status;
146             
147             // If -d is not included in the local options, then send -N (do not shorten directory paths)
148
// to the server (as is done by other cvs clients)
149
if (findOption(localOptions, "-d") == null) { //$NON-NLS-1$
150
if ( ! DO_NOT_SHORTEN.isElementOf(localOptions)) {
151                     LocalOption[] newLocalOptions = new LocalOption[localOptions.length + 1];
152                     newLocalOptions[0] = DO_NOT_SHORTEN;
153                     System.arraycopy(localOptions, 0, newLocalOptions, 1, localOptions.length);
154                     localOptions = newLocalOptions;
155                 }
156             }
157         }
158         
159         return super.doExecute(session, globalOptions, localOptions, arguments, listener, Policy.subMonitorFor(monitor, 90));
160     }
161     
162     /**
163      * Perform a checkout to get the module expansions defined in the CVSROOT/modules file
164      */

165     public RemoteModule[] getRemoteModules(Session session, CVSTag tag, IProgressMonitor monitor)
166         throws CVSException {
167         
168         ModuleDefinitionsListener moduleDefinitionListener = new ModuleDefinitionsListener();
169         
170         IStatus status = super.execute(session, NO_GLOBAL_OPTIONS, new LocalOption[] {FETCH_MODULE_ALIASES}, NO_ARGUMENTS,
171             moduleDefinitionListener, monitor);
172             
173         if (status.getCode() == CVSStatus.SERVER_ERROR) {
174             throw new CVSServerException(status);
175         }
176         
177         return RemoteModule.createRemoteModules(moduleDefinitionListener.getModuleExpansions(), session.getCVSRepositoryLocation(), tag);
178     }
179     
180     protected String JavaDoc getDisplayText() {
181         return "checkout"; //$NON-NLS-1$
182
}
183 }
184
Popular Tags