KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > undo > CopyProjectOperation


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.ui.ide.undo;
13
14 import java.net.URI JavaDoc;
15
16 import org.eclipse.core.filesystem.URIUtil;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectDescription;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Platform;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.SubProgressMonitor;
31 import org.eclipse.ui.internal.ide.undo.ProjectDescription;
32 import org.eclipse.ui.internal.ide.undo.UndoMessages;
33
34 /**
35  * A CopyProjectOperation represents an undoable operation for copying a
36  * project, also specifying the location of its contents. Clients may call the
37  * public API from a background thread.
38  *
39  * This class is intended to be instantiated and used by clients. It is not
40  * intended to be subclassed by clients.
41  *
42  * @since 3.3
43  *
44  */

45 public class CopyProjectOperation extends AbstractCopyOrMoveResourcesOperation {
46
47     private URI JavaDoc projectLocation;
48
49     private IProject originalProject;
50
51     private ProjectDescription originalProjectDescription;
52
53     /**
54      * Create a CopyProjectOperation that copies the specified project and sets
55      * its location to the specified location.
56      *
57      * @param project
58      * the project to be copied
59      * @param name
60      * the name of the copy
61      * @param location
62      * the location for the project's content, or <code>null</code>
63      * if the default project location should be used.
64      * @param label
65      * the label of the operation
66      */

67     public CopyProjectOperation(IProject project, String JavaDoc name, URI JavaDoc location,
68             String JavaDoc label) {
69         super(new IResource[] { project }, new Path(name), label);
70         Assert.isLegal(project != null);
71         originalProject = project;
72         if (location != null
73                 && URIUtil.toPath(location).equals(Platform.getLocation())) {
74             projectLocation = null;
75         } else {
76             projectLocation = location;
77         }
78     }
79
80     /*
81      * Make a project handle for the proposed target project, or null if one
82      * cannot be made.
83      */

84     private IProject getProposedProjectHandle() {
85         if (destination.segmentCount() == 1) {
86             return getWorkspace().getRoot().getProject(
87                     destination.lastSegment());
88         }
89         return null;
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * Checks that the specified project location is valid in addition to
96      * superclass checks.
97      *
98      * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#computeMoveOrCopyStatus()
99      */

100     protected IStatus computeMoveOrCopyStatus() {
101         IStatus status = Status.OK_STATUS;
102         IProject project = getProposedProjectHandle();
103         if (project == null) {
104             return getErrorStatus(UndoMessages.AbstractResourcesOperation_NotEnoughInfo);
105         }
106         if (projectLocation != null) {
107             status = getWorkspace().validateProjectLocationURI(project,
108                     projectLocation);
109         }
110         if (status.isOK()) {
111             return super.computeMoveOrCopyStatus();
112         }
113         return status;
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * Map execute to copying the project
120      *
121      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor,
122      * org.eclipse.core.runtime.IAdaptable)
123      */

124     protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo)
125             throws CoreException {
126         IProject newProject = copyProject(originalProject, destination,
127                 projectLocation, monitor);
128         setTargetResources(new IResource[] { newProject });
129         setResourceDescriptions(new ResourceDescription[0]);
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * Map undo to deleting the project we just copied.
136      *
137      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor,
138      * org.eclipse.core.runtime.IAdaptable)
139      */

140     protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo)
141             throws CoreException {
142         // Delete the project that was copied
143
WorkspaceUndoUtil.delete(resources, new SubProgressMonitor(monitor, 1),
144                 uiInfo, true);
145         // Set the target resource to the original
146
setTargetResources(new IResource[] { originalProject });
147         setResourceDescriptions(new ResourceDescription[0]);
148     }
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#isDestinationPathValid(org.eclipse.core.resources.IResource,
154      * int)
155      */

156     protected boolean isDestinationPathValid(IResource resource, int index) {
157         // path has already been validated in #computeMoveOrCopyStatus()
158
return true;
159     }
160
161     /*
162      * (non-Javadoc)
163      *
164      * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#getProposedName(org.eclipse.core.resources.IResource,
165      * int)
166      */

167     protected String JavaDoc getProposedName(IResource resource, int index) {
168         return destination.lastSegment();
169     }
170
171     /*
172      * (non-Javadoc)
173      *
174      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#updateResourceChangeDescriptionFactory(org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory,
175      * int)
176      */

177     protected boolean updateResourceChangeDescriptionFactory(
178             IResourceChangeDescriptionFactory factory, int operation) {
179         boolean update = false;
180         if (operation == UNDO) {
181             for (int i = 0; i < resources.length; i++) {
182                 update = true;
183                 IResource resource = resources[i];
184                 factory.delete(resource);
185             }
186         } else {
187             factory.copy(originalProject,
188                     getDestinationPath(originalProject, 0));
189         }
190         return update;
191     }
192
193     /*
194      * (non-Javadoc)
195      *
196      * This implementation computes the ability to delete the original copy and
197      * restore any overwritten resources.
198      *
199      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
200      */

201     public IStatus computeUndoableStatus(IProgressMonitor monitor) {
202         IStatus status = super.computeUndoableStatus(monitor);
203         if (!status.isOK()) {
204             return status;
205         }
206         // If the original project content no longer exist, we do not want to
207
// attempt to undo the copy which involves deleting the copies. They may
208
// be all we have left.
209
if (originalProject == null
210                 || !originalProjectDescription.verifyExistence(true)) {
211             markInvalid();
212             return getErrorStatus(UndoMessages.CopyResourcesOperation_NotAllowedDueToDataLoss);
213         }
214         // undoing a copy means deleting the copy that was made
215
if (status.isOK()) {
216             status = computeDeleteStatus();
217         }
218         return status;
219     }
220
221     /*
222      * Copy the specified project, returning the handle of the copy.
223      */

224     IProject copyProject(IProject project, IPath destinationPath,
225             URI JavaDoc locationURI, IProgressMonitor monitor) throws CoreException {
226         monitor
227                 .setTaskName(UndoMessages.AbstractCopyOrMoveResourcesOperation_copyProjectProgress);
228
229         boolean open = project.isOpen();
230         if (!open) {
231             // Must open project in order to get the original project
232
// description for performing the undo.
233
project.open(null);
234         }
235         originalProjectDescription = new ProjectDescription(project);
236         IProjectDescription description = project.getDescription();
237
238         // Set the new name and location into the project's description
239
description.setName(destinationPath.lastSegment());
240         description.setLocationURI(locationURI);
241
242         project.copy(description, IResource.FORCE | IResource.SHALLOW, monitor);
243
244         // Close the original project if it was closed when we started.
245
if (!open) {
246             project.close(null);
247         }
248         // Now return the handle of the new project
249
return (IProject) getWorkspace().getRoot().findMember(destinationPath);
250     }
251 }
252
Popular Tags