KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > CheckoutToRemoteFolderOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.operations;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.team.internal.ccvs.core.*;
21 import org.eclipse.team.internal.ccvs.core.client.*;
22 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
23 import org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener;
24 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
25 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderSandbox;
26 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
27 import org.eclipse.team.internal.ccvs.ui.Policy;
28 import org.eclipse.ui.IWorkbenchPart;
29
30 /**
31  * Checkout a remote folder into a sandbox that is contained within remote folder handles and
32  * the file contents cache.
33  */

34 public class CheckoutToRemoteFolderOperation extends CheckoutOperation {
35
36     RemoteFolderSandbox sandbox;
37     
38     /**
39      * This class overrides the "Created" handler in order to configure the remote file
40      * to recieve and cache the contents
41      */

42     public class CreatedResponseHandler extends UpdatedHandler {
43         public CreatedResponseHandler() {
44             super(UpdatedHandler.HANDLE_CREATED);
45         }
46         /* (non-Javadoc)
47          * @see org.eclipse.team.internal.ccvs.core.client.UpdatedHandler#receiveTargetFile(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.ICVSFile, java.lang.String, java.util.Date, boolean, boolean, org.eclipse.core.runtime.IProgressMonitor)
48          */

49         protected void receiveTargetFile(
50             Session session,
51             ICVSFile mFile,
52             String JavaDoc entryLine,
53             Date JavaDoc modTime,
54             boolean binary,
55             boolean readOnly,
56             boolean executable,
57             IProgressMonitor monitor)
58             throws CVSException {
59             
60             if (mFile instanceof RemoteFile) {
61                 try {
62                     ((RemoteFile)mFile).aboutToReceiveContents(entryLine.getBytes());
63                     super.receiveTargetFile(
64                         session,
65                         mFile,
66                         entryLine,
67                         modTime,
68                         binary,
69                         readOnly,
70                         executable,
71                         monitor);
72                 } finally {
73                     ((RemoteFile)mFile).doneReceivingContents();
74                 }
75             } else {
76                 super.receiveTargetFile(
77                         session,
78                         mFile,
79                         entryLine,
80                         modTime,
81                         binary,
82                         readOnly,
83                         executable,
84                         monitor);
85             }
86         }
87     }
88     
89     public class SandboxCheckout extends Checkout {
90         
91             /* (non-Javadoc)
92          * @see org.eclipse.team.internal.ccvs.core.client.Command#commandFinished(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.client.Command.GlobalOption[], org.eclipse.team.internal.ccvs.core.client.Command.LocalOption[], org.eclipse.team.internal.ccvs.core.ICVSResource[], org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IStatus)
93          */

94         protected IStatus commandFinished(
95             Session session,
96             GlobalOption[] globalOptions,
97             LocalOption[] localOptions,
98             ICVSResource[] resources,
99             IProgressMonitor monitor,
100             IStatus status)
101             throws CVSException {
102             
103             // Don't do anything (i.e. don't prune)
104
return status;
105         }
106
107         /* (non-Javadoc)
108          * @see org.eclipse.team.internal.ccvs.core.client.Command#doExecute(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.client.Command.GlobalOption[], org.eclipse.team.internal.ccvs.core.client.Command.LocalOption[], java.lang.String[], org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener, org.eclipse.core.runtime.IProgressMonitor)
109          */

110         protected IStatus doExecute(
111             Session session,
112             GlobalOption[] globalOptions,
113             LocalOption[] localOptions,
114             String JavaDoc[] arguments,
115             ICommandOutputListener listener,
116             IProgressMonitor monitor)
117             throws CVSException {
118             
119             ResponseHandler newCreated = new CreatedResponseHandler();
120             ResponseHandler oldCreated = session.getResponseHandler(newCreated.getResponseID());
121             session.registerResponseHandler(newCreated);
122             try {
123                 return super.doExecute(
124                         session,
125                         globalOptions,
126                         localOptions,
127                         arguments,
128                         listener,
129                         monitor);
130             } finally {
131                 session.registerResponseHandler(oldCreated);
132             }
133         }
134
135 }
136     public static ICVSRemoteFolder checkoutRemoteFolder(IWorkbenchPart part, ICVSRemoteFolder folder, IProgressMonitor monitor) throws CVSException, InvocationTargetException JavaDoc, InterruptedException JavaDoc {
137         CheckoutToRemoteFolderOperation op = new CheckoutToRemoteFolderOperation(part, folder);
138         op.run(monitor);
139         return op.getResultingFolder();
140     }
141     public CheckoutToRemoteFolderOperation(IWorkbenchPart part, ICVSRemoteFolder remoteFolder) {
142         super(part, new ICVSRemoteFolder[] { remoteFolder });
143     }
144
145     /* (non-Javadoc)
146      * @see org.eclipse.team.internal.ccvs.ui.operations.CheckoutOperation#checkout(org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder, org.eclipse.core.runtime.IProgressMonitor)
147      */

148     protected IStatus checkout(
149         ICVSRemoteFolder folder,
150         IProgressMonitor monitor)
151         throws CVSException {
152         
153         IPath sandboxPath = new Path(null, folder.getRepositoryRelativePath()).removeLastSegments(1);
154         String JavaDoc pathString;
155         if (sandboxPath.isEmpty()) {
156             pathString = ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME;
157         } else {
158             pathString = sandboxPath.toString();
159         }
160         sandbox = new RemoteFolderSandbox(null, folder.getRepository(), pathString, folder.getTag());
161         return checkout(folder, sandbox, monitor);
162     }
163
164     /* (non-Javadoc)
165      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
166      */

167     protected String JavaDoc getTaskName() {
168         return NLS.bind(CVSUIMessages.CheckoutToRemoteFolderOperation_0, new String JavaDoc[] { getRemoteFolders()[0].getName() });
169     }
170     
171     protected IStatus checkout(final ICVSRemoteFolder resource, final ICVSFolder sandbox, IProgressMonitor pm) throws CVSException {
172         // Get the location and the workspace root
173
ICVSRepositoryLocation repository = resource.getRepository();
174         // Open a connection session to the repository
175
final Session session = new Session(repository, sandbox);
176         pm.beginTask(null, 100);
177         Policy.checkCanceled(pm);
178         session.open(Policy.subMonitorFor(pm, 5), false /* read-only */);
179         try {
180             // Build the local options
181
List JavaDoc localOptions = new ArrayList JavaDoc();
182             // Add the options related to the CVSTag
183
CVSTag tag = resource.getTag();
184             if (tag == null) {
185                 // A null tag in a remote resource indicates HEAD
186
tag = CVSTag.DEFAULT;
187             }
188             localOptions.add(Update.makeTagOption(tag));
189             localOptions.add(Checkout.makeDirectoryNameOption(resource.getName()));
190             
191             // Perform the checkout
192
IStatus status = new SandboxCheckout().execute(session,
193                     Command.NO_GLOBAL_OPTIONS,
194                     (LocalOption[])localOptions.toArray(new LocalOption[localOptions.size()]),
195                     new String JavaDoc[]{resource.getRepositoryRelativePath()},
196                     null,
197                     Policy.subMonitorFor(pm, 90));
198             if (status.getCode() == CVSStatus.SERVER_ERROR) {
199                 // Any created projects will exist but will not be mapped to CVS
200
return status;
201             }
202             return OK;
203         } catch (CVSException e) {
204             // An exception occurred either during the module-expansion or checkout
205
// Since we were able to make a connection, return the status so the
206
// checkout of any other modules can proceed
207
return e.getStatus();
208         } finally {
209             session.close();
210             pm.done();
211         }
212     }
213     
214     public ICVSRemoteFolder getResultingFolder() throws CVSException {
215         return (ICVSRemoteFolder)sandbox.getFolder(getRemoteFolders()[0].getName());
216     }
217 }
218
Popular Tags