KickJava   Java API By Example, From Geeks To Geeks.

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


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.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.ui.internal.ide.undo.UndoMessages;
29
30 /**
31  * A MoveProjectOperation represents an undoable operation for moving a
32  * project's content to a different location. Clients may call the public API
33  * from a background thread.
34  *
35  * This class is intended to be instantiated and used by clients. It is not
36  * intended to be subclassed by clients.
37  *
38  * @since 3.3
39  *
40  */

41 public class MoveProjectOperation extends AbstractCopyOrMoveResourcesOperation {
42
43     private URI JavaDoc projectLocation;
44
45     /**
46      * Create a MoveProjectOperation that moves the specified project contents
47      * to a new location.
48      *
49      * @param project
50      * the project to be moved
51      * @param location
52      * the location for the project
53      * @param label
54      * the label of the operation
55      */

56     public MoveProjectOperation(IProject project, URI JavaDoc location, String JavaDoc label) {
57         super(new IResource[] { project }, label);
58         Assert.isLegal(project != null);
59         if (URIUtil.toPath(location).equals(Platform.getLocation())) {
60             projectLocation = null;
61         } else {
62             projectLocation = location;
63         }
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#updateResourceChangeDescriptionFactory(org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory,
70      * int)
71      */

72     protected boolean updateResourceChangeDescriptionFactory(
73             IResourceChangeDescriptionFactory factory, int operation) {
74         // A change of project location only is not of interest to
75
// model providers, so treat it as if nothing is happening.
76
return false;
77     }
78
79     /*
80      * Get the project that this operation is moving.
81      */

82     private IProject getProject() {
83         return (IProject) resources[0];
84     }
85     
86     /*
87      * (non-Javadoc)
88      * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#isDestinationPathValid(org.eclipse.core.resources.IResource, int)
89      */

90     protected boolean isDestinationPathValid(IResource resource, int index) {
91         // path has already been validated in #computeMoveOrCopyStatus()
92
return true;
93     }
94
95     /*
96      * (non-Javadoc)
97      * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#getProposedName(org.eclipse.core.resources.IResource, int)
98      */

99     protected String JavaDoc getProposedName(IResource resource, int index) {
100         return getProject().getName();
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * Checks that the specified project location is valid in addition to
107      * superclass checks.
108      *
109      * @see org.eclipse.ui.ide.undo.AbstractCopyOrMoveResourcesOperation#computeMoveOrCopyStatus()
110      */

111     protected IStatus computeMoveOrCopyStatus() {
112         IStatus status = Status.OK_STATUS;
113         if (projectLocation != null) {
114             status = getWorkspace().validateProjectLocationURI(getProject(),
115                     projectLocation);
116         }
117         if (status.isOK()) {
118             return super.computeMoveOrCopyStatus();
119         }
120         return status;
121     }
122
123     /*
124      * (non-Javadoc)
125      *
126      * Map execute to moving the project
127      *
128      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor,
129      * org.eclipse.core.runtime.IAdaptable)
130      */

131     protected void doExecute(IProgressMonitor monitor, IAdaptable uiInfo)
132             throws CoreException {
133         projectLocation = moveProject(getProject(), projectLocation, monitor);
134         // nothing was overwritten
135
setResourceDescriptions(new ResourceDescription[0]);
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * Map undo to moving the project.
142      *
143      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doUndo(org.eclipse.core.runtime.IProgressMonitor,
144      * org.eclipse.core.runtime.IAdaptable)
145      */

146     protected void doUndo(IProgressMonitor monitor, IAdaptable uiInfo)
147             throws CoreException {
148         doExecute(monitor, uiInfo);
149     }
150     
151     /*
152      * Move the project to its new location, returning its previous location.
153      */

154     URI JavaDoc moveProject(IProject project, URI JavaDoc locationURI, IProgressMonitor monitor)
155             throws CoreException {
156         monitor
157                 .setTaskName(UndoMessages.AbstractCopyOrMoveResourcesOperation_moveProjectProgress);
158     
159         IProjectDescription description = project.getDescription();
160         // Record the original path so this can be undone
161
URI JavaDoc newDestinationURI = description.getLocationURI();
162         // Set the new location into the project's description
163
description.setLocationURI(locationURI);
164     
165         project.move(description, IResource.FORCE | IResource.SHALLOW, monitor);
166     
167         // Now adjust the projectLocation so this can be undone/redone.
168
return newDestinationURI;
169     }
170     
171     /*
172      * (non-Javadoc)
173      *
174      * Map undo to move status.
175      *
176      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeUndoableStatus(org.eclipse.core.runtime.IProgressMonitor)
177      */

178     public IStatus computeUndoableStatus(IProgressMonitor monitor) {
179         IStatus status = super.computeUndoableStatus(monitor);
180         if (status.isOK()) {
181             status = computeMoveOrCopyStatus();
182         }
183         return status;
184     }
185 }
186
Popular Tags