KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > MoveFilesAndFoldersOperation


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.ui.actions;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspace;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.OperationCanceledException;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.ide.undo.AbstractWorkspaceOperation;
26 import org.eclipse.ui.ide.undo.MoveResourcesOperation;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
28
29 /**
30  * Moves files and folders.
31  * <p>
32  * This class may be instantiated; it is not intended to be subclassed.
33  * </p>
34  *
35  * @since 2.1
36  */

37 public class MoveFilesAndFoldersOperation extends CopyFilesAndFoldersOperation {
38
39     /**
40      * Creates a new operation initialized with a shell.
41      *
42      * @param shell
43      * parent shell for error dialogs
44      */

45     public MoveFilesAndFoldersOperation(Shell shell) {
46         super(shell);
47     }
48
49     /**
50      * Returns whether this operation is able to perform on-the-fly
51      * auto-renaming of resources with name collisions.
52      *
53      * @return <code>true</code> if auto-rename is supported, and
54      * <code>false</code> otherwise
55      */

56     protected boolean canPerformAutoRename() {
57         return false;
58     }
59
60     /**
61      * Moves the resources to the given destination. This method is called
62      * recursively to merge folders during folder move.
63      *
64      * @param resources
65      * the resources to move
66      * @param destination
67      * destination to which resources will be moved
68      * @param subMonitor
69      * a progress monitor for showing progress and for cancelation
70      *
71      * @deprecated As of 3.3, the work is performed in the undoable operation
72      * created in
73      * {@link #getUndoableCopyOrMoveOperation(IResource[], IPath)}
74      */

75     protected void copy(IResource[] resources, IPath destination,
76             IProgressMonitor subMonitor) throws CoreException {
77         for (int i = 0; i < resources.length; i++) {
78             IResource source = resources[i];
79             IPath destinationPath = destination.append(source.getName());
80             IWorkspace workspace = source.getWorkspace();
81             IWorkspaceRoot workspaceRoot = workspace.getRoot();
82             IResource existing = workspaceRoot.findMember(destinationPath);
83             if (source.getType() == IResource.FOLDER && existing != null) {
84                 // the resource is a folder and it exists in the destination,
85
// move the children of the folder.
86
if (homogenousResources(source, existing)) {
87                     IResource[] children = ((IContainer) source).members();
88                     copy(children, destinationPath, subMonitor);
89                     delete(source, subMonitor);
90                 } else {
91                     // delete the destination folder, moving a linked folder
92
// over an unlinked one or vice versa. Fixes bug 28772.
93
delete(existing, new SubProgressMonitor(subMonitor, 0));
94                     source.move(destinationPath, IResource.SHALLOW
95                             | IResource.KEEP_HISTORY, new SubProgressMonitor(
96                             subMonitor, 0));
97                 }
98             } else {
99                 // if we're merging folders, we could be overwriting an existing
100
// file
101
if (existing != null) {
102                     if (homogenousResources(source, existing)) {
103                         moveExisting(source, existing, subMonitor);
104                     } else {
105                         // Moving a linked resource over unlinked or vice versa.
106
// Can't use setContents here. Fixes bug 28772.
107
delete(existing, new SubProgressMonitor(subMonitor, 0));
108                         source.move(destinationPath, IResource.SHALLOW
109                                 | IResource.KEEP_HISTORY,
110                                 new SubProgressMonitor(subMonitor, 0));
111                     }
112                 } else {
113                     source.move(destinationPath, IResource.SHALLOW
114                             | IResource.KEEP_HISTORY, new SubProgressMonitor(
115                             subMonitor, 0));
116                 }
117                 subMonitor.worked(1);
118                 if (subMonitor.isCanceled()) {
119                     throw new OperationCanceledException();
120                 }
121             }
122         }
123     }
124
125     /**
126      * Returns the message for querying deep copy/move of a linked resource.
127      *
128      * @param source
129      * resource the query is made for
130      * @return the deep query message
131      */

132     protected String JavaDoc getDeepCheckQuestion(IResource source) {
133         return NLS
134                 .bind(
135                         IDEWorkbenchMessages.CopyFilesAndFoldersOperation_deepMoveQuestion,
136                         source.getFullPath().makeRelative());
137     }
138
139     /**
140      * Returns the task title for this operation's progress dialog.
141      *
142      * @return the task title
143      */

144     protected String JavaDoc getOperationTitle() {
145         return IDEWorkbenchMessages.MoveFilesAndFoldersOperation_operationTitle;
146     }
147
148     /**
149      * Returns the message for this operation's problems dialog.
150      *
151      * @return the problems message
152      */

153     protected String JavaDoc getProblemsMessage() {
154         return IDEWorkbenchMessages.MoveFilesAndFoldersOperation_problemMessage;
155     }
156
157     /**
158      * Returns the title for this operation's problems dialog.
159      *
160      * @return the problems dialog title
161      */

162     protected String JavaDoc getProblemsTitle() {
163         return IDEWorkbenchMessages.MoveFilesAndFoldersOperation_moveFailedTitle;
164     }
165
166     /**
167      * Returns whether the source file in a destination collision will be
168      * validateEdited together with the collision itself. Returns true.
169      *
170      * @return boolean <code>true</code>, the source file in a destination
171      * collision should be validateEdited.
172      */

173     protected boolean getValidateConflictSource() {
174         return true;
175     }
176
177     /**
178      * Sets the content of the existing file to the source file content. Deletes
179      * the source file.
180      *
181      * @param source
182      * source file to move
183      * @param existing
184      * existing file to set the source content in
185      * @param subMonitor
186      * a progress monitor for showing progress and for cancelation
187      * @throws CoreException
188      * setContents failed
189      * @deprecated As of 3.3, this method is not called.
190      */

191     private void moveExisting(IResource source, IResource existing,
192             IProgressMonitor subMonitor) throws CoreException {
193         IFile existingFile = getFile(existing);
194
195         if (existingFile != null) {
196             IFile sourceFile = getFile(source);
197
198             if (sourceFile != null) {
199                 existingFile.setContents(sourceFile.getContents(),
200                         IResource.KEEP_HISTORY, new SubProgressMonitor(
201                                 subMonitor, 0));
202                 delete(sourceFile, subMonitor);
203             }
204         }
205     }
206
207     /*
208      * (non-Javadoc) Overrides method in CopyFilesAndFoldersOperation
209      *
210      * Note this method is for internal use only. It is not API.
211      *
212      */

213     public String JavaDoc validateDestination(IContainer destination,
214             IResource[] sourceResources) {
215         IPath destinationLocation = destination.getLocation();
216
217         for (int i = 0; i < sourceResources.length; i++) {
218             IResource sourceResource = sourceResources[i];
219
220             // is the source being copied onto itself?
221
if (sourceResource.getParent().equals(destination)) {
222                 return NLS
223                         .bind(
224                                 IDEWorkbenchMessages.MoveFilesAndFoldersOperation_sameSourceAndDest,
225                                 sourceResource.getName());
226             }
227             // test if linked source is copied onto itself. Fixes bug 29913.
228
if (destinationLocation != null) {
229                 IPath sourceLocation = sourceResource.getLocation();
230                 IPath destinationResource = destinationLocation
231                         .append(sourceResource.getName());
232                 if (sourceLocation != null
233                         && sourceLocation.isPrefixOf(destinationResource)) {
234                     return NLS
235                             .bind(
236                                     IDEWorkbenchMessages.MoveFilesAndFoldersOperation_sameSourceAndDest,
237                                     sourceResource.getName());
238                 }
239             }
240         }
241         return super.validateDestination(destination, sourceResources);
242     }
243
244     /*
245      * (non-Javadoc)
246      *
247      * @see org.eclipse.ui.actions.CopyFilesAndFoldersOperation#isMove()
248      */

249     protected boolean isMove() {
250         return true;
251     }
252
253     /**
254      * Returns an AbstractWorkspaceOperation suitable for performing the move or
255      * copy operation that will move or copy the given resources to the given
256      * destination path.
257      *
258      * @param resources
259      * the resources to be moved or copied
260      * @param destinationPath
261      * the destination path to which the resources should be moved
262      * @return the operation that should be used to perform the move or copy
263      * @since 3.3
264      */

265     protected AbstractWorkspaceOperation getUndoableCopyOrMoveOperation(
266             IResource[] resources, IPath destinationPath) {
267         return new MoveResourcesOperation(resources, destinationPath,
268                 IDEWorkbenchMessages.CopyFilesAndFoldersOperation_moveTitle);
269
270     }
271 }
272
Popular Tags