KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > NavigatorDropAdapter


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.views.navigator;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.StructuredViewer;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.dnd.DND;
30 import org.eclipse.swt.dnd.DropTargetEvent;
31 import org.eclipse.swt.dnd.FileTransfer;
32 import org.eclipse.swt.dnd.TransferData;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
37 import org.eclipse.ui.actions.MoveFilesAndFoldersOperation;
38 import org.eclipse.ui.actions.ReadOnlyStateChecker;
39 import org.eclipse.ui.dialogs.IOverwriteQuery;
40 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
41 import org.eclipse.ui.part.PluginDropAdapter;
42 import org.eclipse.ui.part.ResourceTransfer;
43
44 /**
45  * Implements drop behaviour for drag and drop operations
46  * that land on the resource navigator.
47  *
48  * @since 2.0
49  */

50 public class NavigatorDropAdapter extends PluginDropAdapter implements
51         IOverwriteQuery {
52
53     /**
54      * A flag indicating that overwrites should always occur.
55      */

56     private boolean alwaysOverwrite = false;
57
58     /**
59      * The last valid operation.
60      */

61     private int lastValidOperation = DND.DROP_NONE;
62
63     /**
64      * Constructs a new drop adapter.
65      *
66      * @param viewer the navigator's viewer
67      */

68     public NavigatorDropAdapter(StructuredViewer viewer) {
69         super(viewer);
70     }
71
72     /*
73      * @see org.eclipse.swt.dnd.DropTargetListener#dragEnter(org.eclipse.swt.dnd.DropTargetEvent)
74      */

75     public void dragEnter(DropTargetEvent event) {
76         if (FileTransfer.getInstance().isSupportedType(event.currentDataType)
77                 && event.detail == DND.DROP_DEFAULT) {
78             // default to copy when dragging from outside Eclipse. Fixes bug 16308.
79
event.detail = DND.DROP_COPY;
80         }
81         super.dragEnter(event);
82     }
83
84     /**
85      * Returns an error status with the given info.
86      */

87     private IStatus error(String JavaDoc message) {
88         return error(message, null);
89     }
90
91     /**
92      * Returns an error status with the given info.
93      */

94     private IStatus error(String JavaDoc message, Throwable JavaDoc exception) {
95         return new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 0, message,
96                 exception);
97     }
98
99     /**
100      * Returns the actual target of the drop, given the resource
101      * under the mouse. If the mouse target is a file, then the drop actually
102      * occurs in its parent. If the drop location is before or after the
103      * mouse target and feedback is enabled, the target is also the parent.
104      */

105     private IContainer getActualTarget(IResource mouseTarget) {
106         /* if cursor is before or after mouseTarget, set target to parent */
107         if (getFeedbackEnabled()) {
108             if (getCurrentLocation() == LOCATION_BEFORE
109                     || getCurrentLocation() == LOCATION_AFTER) {
110                 return mouseTarget.getParent();
111             }
112         }
113         /* if cursor is on a file, return the parent */
114         if (mouseTarget.getType() == IResource.FILE) {
115             return mouseTarget.getParent();
116         }
117         /* otherwise the mouseTarget is the real target */
118         return (IContainer) mouseTarget;
119     }
120
121     /**
122      * Returns the display
123      */

124     private Display getDisplay() {
125         return getViewer().getControl().getDisplay();
126     }
127
128     /**
129      * Returns the resource selection from the LocalSelectionTransfer.
130      *
131      * @return the resource selection from the LocalSelectionTransfer
132      */

133     private IResource[] getSelectedResources() {
134         ArrayList JavaDoc selectedResources = new ArrayList JavaDoc();
135
136         ISelection selection = LocalSelectionTransfer.getInstance()
137                 .getSelection();
138         if (selection instanceof IStructuredSelection) {
139             IStructuredSelection ssel = (IStructuredSelection) selection;
140             for (Iterator JavaDoc i = ssel.iterator(); i.hasNext();) {
141                 Object JavaDoc o = i.next();
142                 if (o instanceof IResource) {
143                     selectedResources.add(o);
144                 }
145                 else if (o instanceof IAdaptable) {
146                     IAdaptable a = (IAdaptable) o;
147                     IResource r = (IResource) a.getAdapter(IResource.class);
148                     if (r != null) {
149                         selectedResources.add(r);
150                     }
151                 }
152             }
153         }
154         return (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]);
155     }
156
157     /**
158      * Returns the shell
159      */

160     private Shell getShell() {
161         return getViewer().getControl().getShell();
162     }
163
164     /**
165      * Returns an error status with the given info.
166      */

167     private IStatus info(String JavaDoc message) {
168         return new Status(IStatus.INFO, PlatformUI.PLUGIN_ID, 0, message, null);
169     }
170
171     /**
172      * Adds the given status to the list of problems. Discards
173      * OK statuses. If the status is a multi-status, only its children
174      * are added.
175      */

176     private void mergeStatus(MultiStatus status, IStatus toMerge) {
177         if (!toMerge.isOK()) {
178             status.merge(toMerge);
179         }
180     }
181
182     /**
183      * Returns an status indicating success.
184      */

185     private IStatus ok() {
186         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0,
187                 ResourceNavigatorMessages.DropAdapter_ok, null);
188     }
189
190     /**
191      * Opens an error dialog if necessary. Takes care of
192      * complex rules necessary for making the error dialog look nice.
193      */

194     private void openError(IStatus status) {
195         if (status == null) {
196             return;
197         }
198
199         String JavaDoc genericTitle = ResourceNavigatorMessages.DropAdapter_title;
200         int codes = IStatus.ERROR | IStatus.WARNING;
201
202         //simple case: one error, not a multistatus
203
if (!status.isMultiStatus()) {
204             ErrorDialog
205                     .openError(getShell(), genericTitle, null, status, codes);
206             return;
207         }
208
209         //one error, single child of multistatus
210
IStatus[] children = status.getChildren();
211         if (children.length == 1) {
212             ErrorDialog.openError(getShell(), status.getMessage(), null,
213                     children[0], codes);
214             return;
215         }
216         //several problems
217
ErrorDialog.openError(getShell(), genericTitle, null, status, codes);
218     }
219
220     /**
221      * Perform the drop.
222      * @see org.eclipse.swt.dnd.DropTargetListener#drop(org.eclipse.swt.dnd.DropTargetEvent)
223      */

224     public boolean performDrop(final Object JavaDoc data) {
225         alwaysOverwrite = false;
226         if (getCurrentTarget() == null || data == null) {
227             return false;
228         }
229         boolean result = false;
230         IStatus status = null;
231         IResource[] resources = null;
232         TransferData currentTransfer = getCurrentTransfer();
233         if (LocalSelectionTransfer.getInstance().isSupportedType(
234                 currentTransfer)) {
235             resources = getSelectedResources();
236         } else if (ResourceTransfer.getInstance().isSupportedType(
237                 currentTransfer)) {
238             resources = (IResource[]) data;
239         } else if (FileTransfer.getInstance().isSupportedType(currentTransfer)) {
240             status = performFileDrop(data);
241             result = status.isOK();
242         } else {
243             result = NavigatorDropAdapter.super.performDrop(data);
244         }
245         if (resources != null && resources.length > 0) {
246             if (getCurrentOperation() == DND.DROP_COPY) {
247                 status = performResourceCopy(getShell(), resources);
248             } else {
249                 status = performResourceMove(resources);
250             }
251         }
252         openError(status);
253         return result;
254     }
255
256     /**
257      * Performs a drop using the FileTransfer transfer type.
258      */

259     private IStatus performFileDrop(Object JavaDoc data) {
260         MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 0,
261                 ResourceNavigatorMessages.DropAdapter_problemImporting, null);
262         mergeStatus(problems, validateTarget(getCurrentTarget(),
263                 getCurrentTransfer()));
264
265         final IContainer target = getActualTarget((IResource) getCurrentTarget());
266         final String JavaDoc[] names = (String JavaDoc[]) data;
267         // Run the import operation asynchronously.
268
// Otherwise the drag source (e.g., Windows Explorer) will be blocked
269
// while the operation executes. Fixes bug 16478.
270
Display.getCurrent().asyncExec(new Runnable JavaDoc() {
271             public void run() {
272                 getShell().forceActive();
273                 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(
274                         getShell());
275                 operation.copyFiles(names, target);
276             }
277         });
278         return problems;
279     }
280
281     /**
282      * Performs a resource copy
283      */

284     private IStatus performResourceCopy(Shell shell, IResource[] sources) {
285         MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1,
286                 ResourceNavigatorMessages.DropAdapter_problemsMoving, null);
287         mergeStatus(problems, validateTarget(getCurrentTarget(),
288                 getCurrentTransfer()));
289
290         IContainer target = getActualTarget((IResource) getCurrentTarget());
291         CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(
292                 shell);
293         operation.copyResources(sources, target);
294
295         return problems;
296     }
297
298     /**
299      * Performs a resource move
300      */

301     private IStatus performResourceMove(IResource[] sources) {
302         MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1,
303                 ResourceNavigatorMessages.DropAdapter_problemsMoving, null);
304         mergeStatus(problems, validateTarget(getCurrentTarget(),
305                 getCurrentTransfer()));
306
307         IContainer target = getActualTarget((IResource) getCurrentTarget());
308         ReadOnlyStateChecker checker = new ReadOnlyStateChecker(
309                 getShell(),
310                 ResourceNavigatorMessages.MoveResourceAction_title,
311                 ResourceNavigatorMessages.MoveResourceAction_checkMoveMessage);
312         sources = checker.checkReadOnlyResources(sources);
313         MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation(
314                 getShell());
315         operation.copyResources(sources, target);
316
317         return problems;
318     }
319
320     /*
321      * @see org.eclipse.ui.dialogs.IOverwriteQuery#queryOverwrite(java.lang.String)
322      */

323     public String JavaDoc queryOverwrite(String JavaDoc pathString) {
324         if (alwaysOverwrite) {
325             return ALL;
326         }
327
328         final String JavaDoc returnCode[] = { CANCEL };
329         final String JavaDoc msg = NLS.bind(ResourceNavigatorMessages.DropAdapter_overwriteQuery, pathString);
330         final String JavaDoc[] options = { IDialogConstants.YES_LABEL,
331                 IDialogConstants.YES_TO_ALL_LABEL, IDialogConstants.NO_LABEL,
332                 IDialogConstants.CANCEL_LABEL };
333         getDisplay().syncExec(new Runnable JavaDoc() {
334             public void run() {
335                 MessageDialog dialog = new MessageDialog(
336                         getShell(),
337                         ResourceNavigatorMessages.DropAdapter_question, null, msg, MessageDialog.QUESTION, options, 0);
338                 dialog.open();
339                 int returnVal = dialog.getReturnCode();
340                 String JavaDoc[] returnCodes = { YES, ALL, NO, CANCEL };
341                 returnCode[0] = returnVal < 0 ? CANCEL : returnCodes[returnVal];
342             }
343         });
344         if (returnCode[0] == ALL) {
345             alwaysOverwrite = true;
346         }
347         return returnCode[0];
348     }
349
350     /**
351      * This method is used to notify the action that some aspect of
352      * the drop operation has changed.
353      */

354     public boolean validateDrop(Object JavaDoc target, int dragOperation,
355             TransferData transferType) {
356
357         if (dragOperation != DND.DROP_NONE) {
358             lastValidOperation = dragOperation;
359         }
360         if (FileTransfer.getInstance().isSupportedType(transferType)
361                 && lastValidOperation != DND.DROP_COPY) {
362             // only allow copying when dragging from outside Eclipse
363
return false;
364         }
365         if (super.validateDrop(target, dragOperation, transferType)) {
366             return true;
367         }
368         return validateTarget(target, transferType).isOK();
369     }
370
371     /**
372      * Ensures that the drop target meets certain criteria
373      */

374     private IStatus validateTarget(Object JavaDoc target, TransferData transferType) {
375         if (!(target instanceof IResource)) {
376             return info(ResourceNavigatorMessages.DropAdapter_targetMustBeResource);
377         }
378         IResource resource = (IResource) target;
379         if (!resource.isAccessible()) {
380             return error(ResourceNavigatorMessages.DropAdapter_canNotDropIntoClosedProject);
381         }
382         IContainer destination = getActualTarget(resource);
383         if (destination.getType() == IResource.ROOT) {
384             return error(ResourceNavigatorMessages.DropAdapter_resourcesCanNotBeSiblings);
385         }
386         String JavaDoc message = null;
387         // drag within Eclipse?
388
if (LocalSelectionTransfer.getInstance().isSupportedType(transferType)) {
389             IResource[] selectedResources = getSelectedResources();
390
391             if (selectedResources.length == 0) {
392                 message = ResourceNavigatorMessages.DropAdapter_dropOperationErrorOther;
393             } else {
394                 CopyFilesAndFoldersOperation operation;
395                 if (lastValidOperation == DND.DROP_COPY) {
396                     operation = new CopyFilesAndFoldersOperation(getShell());
397                 } else {
398                     operation = new MoveFilesAndFoldersOperation(getShell());
399                 }
400                 message = operation.validateDestination(destination,
401                         selectedResources);
402             }
403         } // file import?
404
else if (FileTransfer.getInstance().isSupportedType(transferType)) {
405             String JavaDoc[] sourceNames = (String JavaDoc[]) FileTransfer.getInstance()
406                     .nativeToJava(transferType);
407             if (sourceNames == null) {
408                 // source names will be null on Linux. Use empty names to do destination validation.
409
// Fixes bug 29778
410
sourceNames = new String JavaDoc[0];
411             }
412             CopyFilesAndFoldersOperation copyOperation = new CopyFilesAndFoldersOperation(
413                     getShell());
414             message = copyOperation.validateImportDestination(destination,
415                     sourceNames);
416         }
417         if (message != null) {
418             return error(message);
419         }
420         return ok();
421     }
422 }
423
Popular Tags