KickJava   Java API By Example, From Geeks To Geeks.

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


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
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.ui.internal.ide.undo.UndoMessages;
22
23 /**
24  * An AbstractCopyOrMoveResourcesOperation represents an undoable operation for
25  * moving or copying one or more resources in the workspace. Clients may call
26  * the public API from a background thread.
27  *
28  * This class is not intended to be subclassed by clients.
29  *
30  * @since 3.3
31  *
32  */

33 abstract class AbstractCopyOrMoveResourcesOperation extends
34         AbstractResourcesOperation {
35
36     // Used when there are different destination names for each resource
37
protected IPath[] destinationPaths = null;
38
39     // Used when all resources are going to the same container (no name changes)
40
protected IPath destination = null;
41
42     /**
43      * Create an AbstractCopyOrMoveResourcesOperation that moves or copies all
44      * of the specified resources to the specified paths. The destination paths
45      * must include the names of the resources at their new location.
46      *
47      * @param resources
48      * the resources to be moved or copied. May not contain null
49      * resources, or resources that are descendants of already
50      * included resources.
51      * @param destinationPaths
52      * the destination paths for the resources, including the name to
53      * be assigned to the resource at its new location. May not contain
54      * null paths, and must be the same length as the resources array.
55      * @param label
56      * the label of the operation
57      *
58      */

59     AbstractCopyOrMoveResourcesOperation(IResource[] resources,
60             IPath[] destinationPaths, String JavaDoc label) {
61         super(resources, label);
62         // Check for null arguments
63
if (this.resources == null || destinationPaths == null)
64             throw new IllegalArgumentException JavaDoc("The resource and destination paths may not be null"); //$NON-NLS-1$
65
// Special case to flag descendants. Note this would fail on the next check
66
// anyway, so we are first giving a more specific explanation.
67
// See bug #176764
68
if (this.resources.length != resources.length)
69             throw new IllegalArgumentException JavaDoc("The resource list contained descendants that cannot be moved to separate destination paths"); //$NON-NLS-1$
70
// Check for destination paths corresponding for each resource
71
if (this.resources.length != destinationPaths.length) {
72             throw new IllegalArgumentException JavaDoc("The resource and destination paths must be the same length"); //$NON-NLS-1$
73
}
74         for (int i=0; i<this.resources.length; i++) {
75             if (this.resources[i] == null) {
76                 throw new IllegalArgumentException JavaDoc("The resources array may not contain null resources"); //$NON-NLS-1$
77
}
78             if (destinationPaths[i] == null) {
79                 throw new IllegalArgumentException JavaDoc("The destination paths array may not contain null paths"); //$NON-NLS-1$
80
}
81         }
82         this.destinationPaths = destinationPaths;
83     }
84
85     /**
86      * Create an AbstractCopyOrMoveResourcesOperation that moves or copies all
87      * of the specified resources to the same target location, using their
88      * existing names.
89      *
90      * @param resources
91      * the resources to be moved or copied
92      * @param destinationPath
93      * the destination path for the resources, not including the name
94      * of the new resource.
95      * @param label
96      * the label of the operation
97      */

98     AbstractCopyOrMoveResourcesOperation(IResource[] resources,
99             IPath destinationPath, String JavaDoc label) {
100         super(resources, label);
101         destination = destinationPath;
102     }
103
104     /**
105      * Create an AbstractCopyOrMoveResourcesOperation whose destination is not
106      * yet specified.
107      *
108      * @param resources
109      * the resources to be modified
110      * @param label
111      * the label of the operation
112      */

113     AbstractCopyOrMoveResourcesOperation(IResource[] resources, String JavaDoc label) {
114         super(resources, label);
115     }
116
117
118     /**
119      * Compute the status for moving or copying the resources. A status severity
120      * of <code>OK</code> indicates that the copy or move is likely to be
121      * successful. A status severity of <code>ERROR</code> indicates that the
122      * operation is no longer valid. Other status severities are open to
123      * interpretation by the caller.
124      *
125      * Note this method may be called on initial moving or copying of a
126      * resource, or when a move or copy is undone or redone. Therefore, this
127      * method should check conditions that can change over the life of the
128      * operation, such as whether the file to moved or copied exists, and
129      * whether the target location is still valid. One-time static checks should
130      * typically be done by the caller so that the user is not continually
131      * prompted or warned about conditions that were acceptable at the time of
132      * original execution and do not change over time.
133      *
134      * @return the status indicating the projected outcome of moving or copying
135      * the resources.
136      */

137     protected IStatus computeMoveOrCopyStatus() {
138         // Check for error conditions first so that we do not prompt the user
139
// on warnings that eventually will not matter anyway.
140
if (resources == null) {
141             markInvalid();
142             return getErrorStatus(UndoMessages.AbstractResourcesOperation_NotEnoughInfo);
143         }
144         for (int i = 0; i < resources.length; i++) {
145             IResource resource = resources[i];
146             // Does the resource still exist?
147
if (!resource.exists()) {
148                 markInvalid();
149                 return getErrorStatus(UndoMessages.AbstractCopyOrMoveResourcesOperation_ResourceDoesNotExist);
150             }
151
152             // Are we really trying to move it to a different name?
153
if (!isDestinationPathValid(resource, i)) {
154                 markInvalid();
155                 return getErrorStatus(UndoMessages.AbstractCopyOrMoveResourcesOperation_SameNameOrLocation);
156             }
157             // Is the proposed name valid?
158
IStatus status = getWorkspace().validateName(
159                     getProposedName(resource, i), resource.getType());
160             if (status.getSeverity() == IStatus.ERROR) {
161                 markInvalid();
162             }
163             if (!status.isOK()) {
164                 return status;
165             }
166         }
167         return Status.OK_STATUS;
168     }
169
170     /**
171      * Return the destination path that should be used to move or copy the
172      * specified resource. This path is relative to the workspace.
173      *
174      * @param resource
175      * the resource being moved or copied
176      * @param index
177      * the integer index of the resource in the resource array
178      * @return the path specifying the destination for the resource
179      */

180     protected IPath getDestinationPath(IResource resource, int index) {
181         if (destinationPaths != null) {
182             return destinationPaths[index];
183         }
184         return destination.append(resource.getName());
185
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#appendDescriptiveText(java.lang.StringBuffer)
192      */

193     protected void appendDescriptiveText(StringBuffer JavaDoc text) {
194         super.appendDescriptiveText(text);
195         text.append(" destination: "); //$NON-NLS-1$
196
text.append(destination);
197         text.append(", destinationPaths: "); //$NON-NLS-1$
198
text.append(destinationPaths);
199         text.append('\'');
200     }
201
202     /**
203      * Return a boolean indicating whether the proposed destination path for a
204      * resource is valid.
205      *
206      * @param resource
207      * the resource whose path is to be checked
208      * @param index
209      * the integer index of the resource in the resource array
210      * @return a boolean indicating whether the destination path is valid
211      */

212     protected boolean isDestinationPathValid(IResource resource, int index) {
213         return !resource.getFullPath().equals(
214                 getDestinationPath(resource, index));
215     }
216
217     /**
218      * Return a string indicating the proposed name for the resource
219      *
220      * @param resource
221      * the resource whose path is to be checked
222      * @param index
223      * the integer index of the resource in the resource array
224      * @return the string name of the resource
225      */

226     protected String JavaDoc getProposedName(IResource resource, int index) {
227         return getDestinationPath(resource, index).lastSegment();
228     }
229
230     /*
231      * (non-Javadoc)
232      *
233      * Map execution to move status.
234      *
235      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeExecutionStatus(org.eclipse.core.runtime.IProgressMonitor)
236      */

237     public IStatus computeExecutionStatus(IProgressMonitor monitor) {
238         IStatus status = super.computeExecutionStatus(monitor);
239         if (status.isOK()) {
240             status = computeMoveOrCopyStatus();
241         }
242         return status;
243     }
244
245     /*
246      * (non-Javadoc)
247      *
248      * Map redo to move status.
249      *
250      * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#computeRedoableStatus(org.eclipse.core.runtime.IProgressMonitor)
251      */

252     public IStatus computeRedoableStatus(IProgressMonitor monitor) {
253         IStatus status = super.computeRedoableStatus(monitor);
254         if (status.isOK()) {
255             status = computeMoveOrCopyStatus();
256         }
257         return status;
258     }
259 }
260
Popular Tags