001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.ide.undo;
011:
012: import java.net.URI;
013:
014: import org.eclipse.core.filesystem.URIUtil;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IProjectDescription;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
019: import org.eclipse.core.runtime.Assert;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IAdaptable;
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.eclipse.core.runtime.IStatus;
024: import org.eclipse.core.runtime.Platform;
025: import org.eclipse.core.runtime.Status;
026: import org.eclipse.ui.internal.ide.undo.UndoMessages;
027:
028: /**
029: * A MoveProjectOperation represents an undoable operation for moving a
030: * project's content to a different location. Clients may call the public API
031: * from a background thread.
032: *
033: * This class is intended to be instantiated and used by clients. It is not
034: * intended to be subclassed by clients.
035: *
036: * @since 3.3
037: *
038: */
039: public class MoveProjectOperation extends
040: AbstractCopyOrMoveResourcesOperation {
041:
042: private URI projectLocation;
043:
044: /**
045: * Create a MoveProjectOperation that moves the specified project contents
046: * to a new location.
047: *
048: * @param project
049: * the project to be moved
050: * @param location
051: * the location for the project
052: * @param label
053: * the label of the operation
054: */
055: public MoveProjectOperation(IProject project, URI location,
056: String label) {
057: super (new IResource[] { project }, label);
058: Assert.isLegal(project != null);
059: if (URIUtil.toPath(location).equals(Platform.getLocation())) {
060: projectLocation = null;
061: } else {
062: projectLocation = location;
063: }
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#updateResourceChangeDescriptionFactory(org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory,
070: * int)
071: */
072: protected boolean updateResourceChangeDescriptionFactory(
073: IResourceChangeDescriptionFactory factory, int operation) {
074: // A change of project location only is not of interest to
075: // model providers, so treat it as if nothing is happening.
076: return false;
077: }
078:
079: /*
080: * Get the project that this operation is moving.
081: */
082: private IProject getProject() {
083: return (IProject) resources[0];
084: }
085:
086: /*
087: * (non-Javadoc)
088: * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#isDestinationPathValid(org.eclipse.core.resources.IResource, int)
089: */
090: protected boolean isDestinationPathValid(IResource resource,
091: int index) {
092: // path has already been validated in #computeMoveOrCopyStatus()
093: return true;
094: }
095:
096: /*
097: * (non-Javadoc)
098: * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#getProposedName(org.eclipse.core.resources.IResource, int)
099: */
100: protected String getProposedName(IResource resource, int index) {
101: return getProject().getName();
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * Checks that the specified project location is valid in addition to
108: * superclass checks.
109: *
110: * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#computeMoveOrCopyStatus()
111: */
112: protected IStatus computeMoveOrCopyStatus() {
113: IStatus status = Status.OK_STATUS;
114: if (projectLocation != null) {
115: status = getWorkspace().validateProjectLocationURI(
116: getProject(), projectLocation);
117: }
118: if (status.isOK()) {
119: return super .computeMoveOrCopyStatus();
120: }
121: return status;
122: }
123:
124: /*
125: * (non-Javadoc)
126: *
127: * Map execute to moving the project
128: *
129: * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor,
130: * org.eclipse.core.runtime.IAdaptable)
131: */
132: protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo)
133: throws CoreException {
134: projectLocation = moveProject(getProject(), projectLocation,
135: monitor);
136: // nothing was overwritten
137: setResourceDescriptions(new ResourceDescription[0]);
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * Map undo to moving the project.
144: *
145: * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doUndo(org.eclipse.core.runtime.IProgressMonitor,
146: * org.eclipse.core.runtime.IAdaptable)
147: */
148: protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo)
149: throws CoreException {
150: doExecute(monitor, uiInfo);
151: }
152:
153: /*
154: * Move the project to its new location, returning its previous location.
155: */
156: URI moveProject(IProject project, URI locationURI,
157: IProgressMonitor monitor) throws CoreException {
158: monitor
159: .setTaskName(UndoMessages.AbstractCopyOrMoveResourcesOperation_moveProjectProgress);
160:
161: IProjectDescription description = project.getDescription();
162: // Record the original path so this can be undone
163: URI newDestinationURI = description.getLocationURI();
164: // Set the new location into the project's description
165: description.setLocationURI(locationURI);
166:
167: project.move(description, IResource.FORCE | IResource.SHALLOW,
168: monitor);
169:
170: // Now adjust the projectLocation so this can be undone/redone.
171: return newDestinationURI;
172: }
173:
174: /*
175: * (non-Javadoc)
176: *
177: * Map undo to move status.
178: *
179: * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
180: */
181: public IStatus computeUndoableStatus(IProgressMonitor monitor) {
182: IStatus status = super.computeUndoableStatus(monitor);
183: if (status.isOK()) {
184: status = computeMoveOrCopyStatus();
185: }
186: return status;
187: }
188: }
|