KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > resources > ResourceDropAdapterAssistant


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.navigator.resources;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.MultiStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.util.LocalSelectionTransfer;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.swt.dnd.DND;
29 import org.eclipse.swt.dnd.DropTargetEvent;
30 import org.eclipse.swt.dnd.FileTransfer;
31 import org.eclipse.swt.dnd.TransferData;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
36 import org.eclipse.ui.actions.MoveFilesAndFoldersOperation;
37 import org.eclipse.ui.actions.ReadOnlyStateChecker;
38 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages;
39 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorPlugin;
40 import org.eclipse.ui.navigator.CommonDropAdapter;
41 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
42 import org.eclipse.ui.part.ResourceTransfer;
43
44 /**
45  *
46  * Clients may reference this class in the <b>dropAssistant</b> element of a
47  * <b>org.eclipse.ui.navigator.navigatorContent</b> extension point.
48  *
49  * <p>
50  * Clients may not extend or instantiate this class for any purpose.
51  * Clients may have no direct dependencies on the contract of this class.
52  * </p>
53  *
54  * @since 3.2
55  *
56  */

57 public class ResourceDropAdapterAssistant extends CommonDropAdapterAssistant {
58
59     private static final boolean DEBUG = false;
60     private static final IResource[] NO_RESOURCES = new IResource[0];
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#isSupportedType(org.eclipse.swt.dnd.TransferData)
66      */

67     public boolean isSupportedType(TransferData aTransferType) {
68         return super.isSupportedType(aTransferType)
69                 || FileTransfer.getInstance().isSupportedType(aTransferType);
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object,
76      * int, org.eclipse.swt.dnd.TransferData)
77      */

78     public IStatus validateDrop(Object JavaDoc target, int aDropOperation,
79             TransferData transferType) {
80
81         if (!(target instanceof IResource)) {
82             return WorkbenchNavigatorPlugin
83                     .createStatus(
84                             IStatus.INFO,
85                             0,
86                             WorkbenchNavigatorMessages.DropAdapter_targetMustBeResource,
87                             null);
88         }
89         IResource resource = (IResource) target;
90         if (!resource.isAccessible()) {
91             return WorkbenchNavigatorPlugin
92                     .createErrorStatus(
93                             0,
94                             WorkbenchNavigatorMessages.DropAdapter_canNotDropIntoClosedProject,
95                             null);
96         }
97         IContainer destination = getActualTarget(resource);
98         if (destination.getType() == IResource.ROOT) {
99             return WorkbenchNavigatorPlugin
100                     .createErrorStatus(
101                             0,
102                             WorkbenchNavigatorMessages.DropAdapter_resourcesCanNotBeSiblings,
103                             null);
104         }
105         String JavaDoc message = null;
106         // drag within Eclipse?
107
if (LocalSelectionTransfer.getTransfer().isSupportedType(transferType)) {
108             IResource[] selectedResources = getSelectedResources();
109
110             if (selectedResources.length == 0) {
111                 message = WorkbenchNavigatorMessages.DropAdapter_dropOperationErrorOther;
112             } else {
113                 CopyFilesAndFoldersOperation operation;
114                 if (aDropOperation == DND.DROP_COPY) {
115                     if (DEBUG) {
116                         System.out
117                                 .println("ResourceDropAdapterAssistant.validateDrop validating COPY."); //$NON-NLS-1$
118
}
119
120                     operation = new CopyFilesAndFoldersOperation(getShell());
121                 } else {
122                     if (DEBUG) {
123                         System.out
124                                 .println("ResourceDropAdapterAssistant.validateDrop validating MOVE."); //$NON-NLS-1$
125
}
126                     operation = new MoveFilesAndFoldersOperation(getShell());
127                 }
128                 message = operation.validateDestination(destination,
129                         selectedResources);
130             }
131         } // file import?
132
else if (FileTransfer.getInstance().isSupportedType(transferType)) {
133             String JavaDoc[] sourceNames = (String JavaDoc[]) FileTransfer.getInstance()
134                     .nativeToJava(transferType);
135             if (sourceNames == null) {
136                 // source names will be null on Linux. Use empty names to do
137
// destination validation.
138
// Fixes bug 29778
139
sourceNames = new String JavaDoc[0];
140             }
141             CopyFilesAndFoldersOperation copyOperation = new CopyFilesAndFoldersOperation(
142                     getShell());
143             message = copyOperation.validateImportDestination(destination,
144                     sourceNames);
145         }
146         if (message != null) {
147             return WorkbenchNavigatorPlugin.createErrorStatus(0, message, null);
148         }
149         return Status.OK_STATUS;
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(CommonDropAdapter,
156      * DropTargetEvent, Object)
157      */

158     public IStatus handleDrop(CommonDropAdapter aDropAdapter,
159             DropTargetEvent aDropTargetEvent, Object JavaDoc aTarget) {
160
161         if (DEBUG) {
162             System.out
163                     .println("ResourceDropAdapterAssistant.handleDrop (begin)"); //$NON-NLS-1$
164
}
165
166         // alwaysOverwrite = false;
167
if (aDropAdapter.getCurrentTarget() == null
168                 || aDropTargetEvent.data == null) {
169             return Status.CANCEL_STATUS;
170         }
171         IStatus status = null;
172         IResource[] resources = null;
173         TransferData currentTransfer = aDropAdapter.getCurrentTransfer();
174         if (LocalSelectionTransfer.getTransfer().isSupportedType(
175                 currentTransfer)) {
176             resources = getSelectedResources();
177         } else if (ResourceTransfer.getInstance().isSupportedType(
178                 currentTransfer)) {
179             resources = (IResource[]) aDropTargetEvent.data;
180         }
181
182         if (FileTransfer.getInstance().isSupportedType(currentTransfer)) {
183             status = performFileDrop(aDropAdapter, aDropTargetEvent.data);
184         } else if (resources != null && resources.length > 0) {
185             if (aDropAdapter.getCurrentOperation() == DND.DROP_COPY) {
186                 if (DEBUG) {
187                     System.out
188                             .println("ResourceDropAdapterAssistant.handleDrop executing COPY."); //$NON-NLS-1$
189
}
190                 status = performResourceCopy(aDropAdapter, getShell(),
191                         resources);
192             } else {
193                 if (DEBUG) {
194                     System.out
195                             .println("ResourceDropAdapterAssistant.handleDrop executing MOVE."); //$NON-NLS-1$
196
}
197
198                 status = performResourceMove(aDropAdapter, resources);
199             }
200         }
201         openError(status);
202         IContainer target = getActualTarget((IResource) aDropAdapter
203                 .getCurrentTarget());
204         if (target != null && target.isAccessible()) {
205             try {
206                 target.refreshLocal(IResource.DEPTH_ONE, null);
207             } catch (CoreException e) {
208             }
209         }
210         return status;
211     }
212
213     /*
214      * (non-Javadoc)
215      *
216      * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validatePluginTransferDrop(org.eclipse.jface.viewers.IStructuredSelection,
217      * java.lang.Object)
218      */

219     public IStatus validatePluginTransferDrop(
220             IStructuredSelection aDragSelection, Object JavaDoc aDropTarget) {
221         if (!(aDropTarget instanceof IResource)) {
222             return WorkbenchNavigatorPlugin
223                     .createStatus(
224                             IStatus.INFO,
225                             0,
226                             WorkbenchNavigatorMessages.DropAdapter_targetMustBeResource,
227                             null);
228         }
229         IResource resource = (IResource) aDropTarget;
230         if (!resource.isAccessible()) {
231             return WorkbenchNavigatorPlugin
232                     .createErrorStatus(
233                             0,
234                             WorkbenchNavigatorMessages.DropAdapter_canNotDropIntoClosedProject,
235                             null);
236         }
237         IContainer destination = getActualTarget(resource);
238         if (destination.getType() == IResource.ROOT) {
239             return WorkbenchNavigatorPlugin
240                     .createErrorStatus(
241                             0,
242                             WorkbenchNavigatorMessages.DropAdapter_resourcesCanNotBeSiblings,
243                             null);
244         }
245
246         IResource[] selectedResources = getSelectedResources(aDragSelection);
247
248         String JavaDoc message = null;
249         if (selectedResources.length == 0) {
250             message = WorkbenchNavigatorMessages.DropAdapter_dropOperationErrorOther;
251         } else {
252             MoveFilesAndFoldersOperation operation;
253
254             operation = new MoveFilesAndFoldersOperation(getShell());
255             message = operation.validateDestination(destination,
256                     selectedResources);
257         }
258         if (message != null) {
259             return WorkbenchNavigatorPlugin.createErrorStatus(0, message, null);
260         }
261         return Status.OK_STATUS;
262     }
263     
264     /* (non-Javadoc)
265      * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handlePluginTransferDrop(org.eclipse.jface.viewers.IStructuredSelection, java.lang.Object)
266      */

267     public IStatus handlePluginTransferDrop(IStructuredSelection aDragSelection, Object JavaDoc aDropTarget) {
268
269         IContainer target = getActualTarget((IResource) aDropTarget);
270         IResource[] resources = getSelectedResources(aDragSelection);
271         
272         MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation(
273                  getShell());
274         operation.copyResources(resources, target);
275
276         if (target != null && target.isAccessible()) {
277             try {
278                 target.refreshLocal(IResource.DEPTH_ONE, null);
279             } catch (CoreException e) {
280             }
281         }
282         return Status.OK_STATUS;
283     }
284
285     /**
286      * Returns the actual target of the drop, given the resource under the
287      * mouse. If the mouse target is a file, then the drop actually occurs in
288      * its parent. If the drop location is before or after the mouse target and
289      * feedback is enabled, the target is also the parent.
290      */

291     private IContainer getActualTarget(IResource mouseTarget) {
292
293         /* if cursor is on a file, return the parent */
294         if (mouseTarget.getType() == IResource.FILE) {
295             return mouseTarget.getParent();
296         }
297         /* otherwise the mouseTarget is the real target */
298         return (IContainer) mouseTarget;
299     }
300
301     /**
302      * Returns the resource selection from the LocalSelectionTransfer.
303      *
304      * @return the resource selection from the LocalSelectionTransfer
305      */

306     private IResource[] getSelectedResources() {
307
308         ISelection selection = LocalSelectionTransfer.getTransfer()
309                 .getSelection();
310         if (selection instanceof IStructuredSelection) {
311             return getSelectedResources((IStructuredSelection)selection);
312         }
313         return NO_RESOURCES;
314     }
315
316     /**
317      * Returns the resource selection from the LocalSelectionTransfer.
318      *
319      * @return the resource selection from the LocalSelectionTransfer
320      */

321     private IResource[] getSelectedResources(IStructuredSelection selection) {
322         ArrayList JavaDoc selectedResources = new ArrayList JavaDoc();
323
324         for (Iterator JavaDoc i = selection.iterator(); i.hasNext();) {
325             Object JavaDoc o = i.next();
326             if (o instanceof IResource) {
327                 selectedResources.add(o);
328             } else if (o instanceof IAdaptable) {
329                 IAdaptable a = (IAdaptable) o;
330                 IResource r = (IResource) a.getAdapter(IResource.class);
331                 if (r != null) {
332                     selectedResources.add(r);
333                 }
334             }
335         }
336         return (IResource[]) selectedResources
337                 .toArray(new IResource[selectedResources.size()]);
338     }
339
340     /**
341      * Performs a resource copy
342      */

343     private IStatus performResourceCopy(CommonDropAdapter dropAdapter,
344             Shell shell, IResource[] sources) {
345         MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1,
346                 WorkbenchNavigatorMessages.DropAdapter_problemsMoving, null);
347         mergeStatus(problems, validateTarget(dropAdapter.getCurrentTarget(),
348                 dropAdapter.getCurrentTransfer(), dropAdapter
349                         .getCurrentOperation()));
350
351         IContainer target = getActualTarget((IResource) dropAdapter
352                 .getCurrentTarget());
353         CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(
354                 shell);
355         operation.copyResources(sources, target);
356
357         return problems;
358     }
359
360     /**
361      * Performs a resource move
362      */

363     private IStatus performResourceMove(CommonDropAdapter dropAdapter,
364             IResource[] sources) {
365         MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 1,
366                 WorkbenchNavigatorMessages.DropAdapter_problemsMoving, null);
367         mergeStatus(problems, validateTarget(dropAdapter.getCurrentTarget(),
368                 dropAdapter.getCurrentTransfer(), dropAdapter
369                         .getCurrentOperation()));
370
371         IContainer target = getActualTarget((IResource) dropAdapter
372                 .getCurrentTarget());
373         ReadOnlyStateChecker checker = new ReadOnlyStateChecker(getShell(),
374                 WorkbenchNavigatorMessages.MoveResourceAction_title,
375                 WorkbenchNavigatorMessages.MoveResourceAction_checkMoveMessage);
376         sources = checker.checkReadOnlyResources(sources);
377         MoveFilesAndFoldersOperation operation = new MoveFilesAndFoldersOperation(
378                 getShell());
379         operation.copyResources(sources, target);
380
381         return problems;
382     }
383
384     /**
385      * Performs a drop using the FileTransfer transfer type.
386      */

387     private IStatus performFileDrop(CommonDropAdapter anAdapter, Object JavaDoc data) {
388         MultiStatus problems = new MultiStatus(PlatformUI.PLUGIN_ID, 0,
389                 WorkbenchNavigatorMessages.DropAdapter_problemImporting, null);
390         mergeStatus(problems,
391                 validateTarget(anAdapter.getCurrentTarget(), anAdapter
392                         .getCurrentTransfer(), anAdapter.getCurrentOperation()));
393
394         final IContainer target = getActualTarget((IResource) anAdapter
395                 .getCurrentTarget());
396         final String JavaDoc[] names = (String JavaDoc[]) data;
397         // Run the import operation asynchronously.
398
// Otherwise the drag source (e.g., Windows Explorer) will be blocked
399
// while the operation executes. Fixes bug 16478.
400
Display.getCurrent().asyncExec(new Runnable JavaDoc() {
401             public void run() {
402                 getShell().forceActive();
403                 CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(
404                         getShell());
405                 operation.copyFiles(names, target);
406             }
407         });
408         return problems;
409     }
410
411     /**
412      * Ensures that the drop target meets certain criteria
413      */

414     private IStatus validateTarget(Object JavaDoc target, TransferData transferType,
415             int dropOperation) {
416         if (!(target instanceof IResource)) {
417             return WorkbenchNavigatorPlugin
418                     .createInfoStatus(WorkbenchNavigatorMessages.DropAdapter_targetMustBeResource);
419         }
420         IResource resource = (IResource) target;
421         if (!resource.isAccessible()) {
422             return WorkbenchNavigatorPlugin
423                     .createErrorStatus(WorkbenchNavigatorMessages.DropAdapter_canNotDropIntoClosedProject);
424         }
425         IContainer destination = getActualTarget(resource);
426         if (destination.getType() == IResource.ROOT) {
427             return WorkbenchNavigatorPlugin
428                     .createErrorStatus(WorkbenchNavigatorMessages.DropAdapter_resourcesCanNotBeSiblings);
429         }
430         String JavaDoc message = null;
431         // drag within Eclipse?
432
if (LocalSelectionTransfer.getTransfer().isSupportedType(transferType)) {
433             IResource[] selectedResources = getSelectedResources();
434
435             if (selectedResources.length == 0) {
436                 message = WorkbenchNavigatorMessages.DropAdapter_dropOperationErrorOther;
437             } else {
438                 CopyFilesAndFoldersOperation operation;
439                 if (dropOperation == DND.DROP_COPY) {
440                     operation = new CopyFilesAndFoldersOperation(getShell());
441                 } else {
442                     operation = new MoveFilesAndFoldersOperation(getShell());
443                 }
444                 message = operation.validateDestination(destination,
445                         selectedResources);
446             }
447         } // file import?
448
else if (FileTransfer.getInstance().isSupportedType(transferType)) {
449             String JavaDoc[] sourceNames = (String JavaDoc[]) FileTransfer.getInstance()
450                     .nativeToJava(transferType);
451             if (sourceNames == null) {
452                 // source names will be null on Linux. Use empty names to do
453
// destination validation.
454
// Fixes bug 29778
455
sourceNames = new String JavaDoc[0];
456             }
457             CopyFilesAndFoldersOperation copyOperation = new CopyFilesAndFoldersOperation(
458                     getShell());
459             message = copyOperation.validateImportDestination(destination,
460                     sourceNames);
461         }
462         if (message != null) {
463             return WorkbenchNavigatorPlugin.createErrorStatus(message);
464         }
465         return Status.OK_STATUS;
466     }
467
468     /**
469      * Adds the given status to the list of problems. Discards OK statuses. If
470      * the status is a multi-status, only its children are added.
471      */

472     private void mergeStatus(MultiStatus status, IStatus toMerge) {
473         if (!toMerge.isOK()) {
474             status.merge(toMerge);
475         }
476     }
477
478     /**
479      * Opens an error dialog if necessary. Takes care of complex rules necessary
480      * for making the error dialog look nice.
481      */

482     private void openError(IStatus status) {
483         if (status == null) {
484             return;
485         }
486
487         String JavaDoc genericTitle = WorkbenchNavigatorMessages.DropAdapter_title;
488         int codes = IStatus.ERROR | IStatus.WARNING;
489
490         // simple case: one error, not a multistatus
491
if (!status.isMultiStatus()) {
492             ErrorDialog
493                     .openError(getShell(), genericTitle, null, status, codes);
494             return;
495         }
496
497         // one error, single child of multistatus
498
IStatus[] children = status.getChildren();
499         if (children.length == 1) {
500             ErrorDialog.openError(getShell(), status.getMessage(), null,
501                     children[0], codes);
502             return;
503         }
504         // several problems
505
ErrorDialog.openError(getShell(), genericTitle, null, status, codes);
506     }
507
508 }
509
Popular Tags