KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.*;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.team.core.RepositoryProvider;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.internal.ccvs.core.*;
20 import org.eclipse.team.internal.ccvs.core.client.Command;
21 import org.eclipse.team.internal.ccvs.core.client.Session;
22 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException;
23 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
24 import org.eclipse.team.internal.ccvs.core.resources.RemoteFolderTree;
25 import org.eclipse.team.internal.ccvs.ui.*;
26 import org.eclipse.team.internal.ccvs.ui.Policy;
27
28 /**
29  * Create a folder and any missing parents in the repository
30  */

31 public class ShareProjectOperation extends CVSOperation {
32
33     private ICVSRepositoryLocation location;
34     private IProject project;
35     private String JavaDoc moduleName;
36     private Shell shell;
37
38     public ShareProjectOperation(Shell shell, ICVSRepositoryLocation location, IProject project, String JavaDoc moduleName) {
39         super(null);
40         this.shell = shell;
41         this.moduleName = moduleName;
42         this.project = project;
43         this.location = location;
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
48      */

49     protected void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
50         try {
51             monitor.beginTask(getTaskName(), 100);
52             // Create the remote module
53
final ICVSRemoteFolder remote = createRemoteFolder(Policy.subMonitorFor(monitor, 50));
54             // Map the project to the module in a workspace runnable
55
final TeamException[] exception = new TeamException[] {null};
56             ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
57                 public void run(IProgressMonitor monitor) throws CoreException {
58                     try {
59                         mapProjectToRemoteFolder(remote, monitor);
60                     } catch (TeamException e) {
61                         exception[0] = e;
62                     }
63                 }
64             }, ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(project), 0, Policy.subMonitorFor(monitor, 100));
65             if (exception[0] != null)
66                 throw exception[0];
67         } catch (CoreException e) {
68             throw CVSException.wrapException(e);
69         } finally {
70             monitor.done();
71         }
72     }
73
74     /**
75      * Create the remote folder to which the project is being mapped
76      * (as well as any ancestors) and return it. If the remote folder does not
77      * exist remotely, this method will create it.
78      * @param monitor a progress monitor
79      * @return the existing remote folder to which the project is being mapped
80      * @throws CVSException
81      */

82     protected ICVSRemoteFolder createRemoteFolder(IProgressMonitor monitor) throws CVSException {
83         String JavaDoc projectName = project.getName();
84         if (moduleName == null)
85             moduleName = projectName;
86
87         RemoteFolderTree root = new RemoteFolderTree(null, location, Path.EMPTY.toString(), null);
88         Path path = new Path(null, moduleName);
89         
90         try {
91             monitor.beginTask(getTaskName(), 100 * path.segmentCount());
92             return ensureTreeExists(root, path, monitor);
93         } catch (TeamException e) {
94             throw CVSException.wrapException(e);
95         } finally {
96             monitor.done();
97         }
98     }
99     
100     /**
101      * Map the project to the remote folder by associating the CVS
102      * Repository Provider with the project and, at the very least,
103      * assigning the folder sync info for the remote folder as the
104      * folder sync info for the project.
105      * @param remote the remote folder to which the projetc is being mapped
106      * @param monitor a progress monitor
107      * @throws CVSException
108      */

109     protected void mapProjectToRemoteFolder(final ICVSRemoteFolder remote, IProgressMonitor monitor) throws TeamException {
110         monitor.beginTask(null, IProgressMonitor.UNKNOWN);
111         purgeAnyCVSFolders(Policy.subMonitorFor(monitor, IProgressMonitor.UNKNOWN));
112         // Link the project to the newly created module
113
monitor.subTask(NLS.bind(CVSUIMessages.ShareProjectOperation_3, new String JavaDoc[] { project.getName(), remote.getRepositoryRelativePath() }));
114         ICVSFolder folder = (ICVSFolder)CVSWorkspaceRoot.getCVSResourceFor(project);
115         folder.setFolderSyncInfo(remote.getFolderSyncInfo());
116         //Register it with Team. If it already is, no harm done.
117
RepositoryProvider.map(project, CVSProviderPlugin.getTypeId());
118         monitor.done();
119     }
120
121     /*
122      * Create handles for all the children in the moduleName path
123      */

124     private RemoteFolderTree createChild(RemoteFolderTree parent, String JavaDoc name, IProgressMonitor monitor) throws CVSException, TeamException {
125         RemoteFolderTree child = new RemoteFolderTree(parent, name, location, new Path(null, parent.getRepositoryRelativePath()).append(name).toString(), null);
126         parent.setChildren(new ICVSRemoteResource[] { child });
127         if (child.exists(Policy.subMonitorFor(monitor, 50))) {
128             // The child exists so get the handle that was received from the server
129
return (RemoteFolderTree)parent.getFolder(name);
130         } else {
131             // Create the folder remotely
132
createFolder(child, Policy.subMonitorFor(monitor, 50));
133             return child;
134         }
135     }
136
137     /*
138      * Ensure that all the folders in the tree exist
139      */

140     private ICVSRemoteFolder ensureTreeExists(RemoteFolderTree folder, IPath path, IProgressMonitor monitor) throws TeamException {
141         if (path.isEmpty()) return folder;
142         String JavaDoc name = path.segment(0);
143         RemoteFolderTree child = createChild(folder, name, monitor);
144         return ensureTreeExists(child, path.removeFirstSegments(1), monitor);
145     }
146     
147     private void createFolder(RemoteFolderTree folder, IProgressMonitor monitor) throws TeamException {
148         Session s = new Session(location, folder.getParent());
149         s.open(monitor, true /* open for modification */);
150         try {
151             IStatus status = Command.ADD.execute(s,
152                     Command.NO_GLOBAL_OPTIONS,
153                     Command.NO_LOCAL_OPTIONS,
154                     new String JavaDoc[] { folder.getName() },
155                     null,
156                     monitor);
157             // If we get a warning, the operation most likely failed so check that the status is OK
158
if (status.getCode() == CVSStatus.SERVER_ERROR || ! status.isOK()) {
159                 throw new CVSServerException(status);
160             }
161         } finally {
162             s.close();
163         }
164     }
165
166     /* (non-Javadoc)
167      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
168      */

169     protected String JavaDoc getTaskName() {
170         return NLS.bind(CVSUIMessages.ShareProjectOperation_0, new String JavaDoc[] { project.getName(), moduleName });
171     }
172
173     /**
174      * @return Returns the project.
175      */

176     public IProject getProject() {
177         return project;
178     }
179     
180     /* (non-Javadoc)
181      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getShell()
182      */

183     public Shell getShell() {
184         return shell;
185     }
186     
187     /**
188      * Method findCommonRootInSubfolders.
189      * @param monitor
190      * @return String
191      */

192     private void purgeAnyCVSFolders(final IProgressMonitor monitor) {
193         try {
194             monitor.beginTask(null, IProgressMonitor.UNKNOWN);
195             ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor(project);
196             folder.accept(new ICVSResourceVisitor() {
197                 public void visitFile(ICVSFile file) throws CVSException {
198                     // nothing to do for files
199
}
200                 public void visitFolder(ICVSFolder folder) throws CVSException {
201                     monitor.subTask(NLS.bind(CVSUIMessages.ShareProjectOperation_2, new String JavaDoc[] { folder.getIResource().getFullPath().toString() } ));
202                     if (folder.isCVSFolder()) {
203                         // for now, just unmanage
204
folder.unmanage(null);
205                     }
206                 }
207             }, true /* recurse */);
208         } catch (CVSException e) {
209             // log the exception and return null
210
CVSUIPlugin.log(e);
211         } finally {
212             monitor.done();
213         }
214     }
215 }
216
Popular Tags