KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > FileTransferDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.packageview;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IResource;
15
16 import org.eclipse.swt.dnd.DND;
17 import org.eclipse.swt.dnd.DropTargetEvent;
18 import org.eclipse.swt.dnd.FileTransfer;
19 import org.eclipse.swt.dnd.Transfer;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.util.TransferDropTargetListener;
24 import org.eclipse.jface.viewers.AbstractTreeViewer;
25
26 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
27
28 import org.eclipse.jdt.core.IJavaElement;
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IPackageFragment;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.JavaModelException;
33
34 import org.eclipse.jdt.internal.corext.util.Resources;
35
36 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
37 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
38
39 /**
40  * Adapter to handle file drop from other applications.
41  */

42 class FileTransferDropAdapter extends JdtViewerDropAdapter implements TransferDropTargetListener {
43     
44     FileTransferDropAdapter(AbstractTreeViewer viewer) {
45         super(viewer, DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND);
46     }
47
48     //---- TransferDropTargetListener interface ---------------------------------------
49

50     public Transfer getTransfer() {
51         return FileTransfer.getInstance();
52     }
53     
54     public boolean isEnabled(DropTargetEvent event) {
55         Object JavaDoc target= event.item != null ? event.item.getData() : null;
56         if (target == null)
57             return false;
58         return target instanceof IJavaElement || target instanceof IResource;
59     }
60
61     //---- Actual DND -----------------------------------------------------------------
62

63     public void validateDrop(Object JavaDoc target, DropTargetEvent event, int operation) {
64         event.detail= DND.DROP_NONE;
65         
66         boolean isPackageFragment= target instanceof IPackageFragment;
67         boolean isJavaProject= target instanceof IJavaProject;
68         boolean isPackageFragmentRoot= target instanceof IPackageFragmentRoot;
69         boolean isContainer= target instanceof IContainer;
70         
71         if (!(isPackageFragment || isJavaProject || isPackageFragmentRoot || isContainer))
72             return;
73             
74         if (isContainer) {
75             IContainer container= (IContainer)target;
76             if (container.isAccessible() && !Resources.isReadOnly(container))
77                 event.detail= DND.DROP_COPY;
78         } else {
79             IJavaElement element= (IJavaElement)target;
80             if (!element.isReadOnly())
81                 event.detail= DND.DROP_COPY;
82         }
83             
84         return;
85     }
86
87     public void drop(Object JavaDoc dropTarget, final DropTargetEvent event) {
88         try {
89             int operation= event.detail;
90
91             event.detail= DND.DROP_NONE;
92             final Object JavaDoc data= event.data;
93             if (data == null || !(data instanceof String JavaDoc[]) || operation != DND.DROP_COPY)
94                 return;
95
96             final IContainer target= getActualTarget(dropTarget);
97             if (target == null)
98                 return;
99
100             // Run the import operation asynchronously.
101
// Otherwise the drag source (e.g., Windows Explorer) will be blocked
102
// while the operation executes. Fixes bug 35796.
103
Display.getCurrent().asyncExec(new Runnable JavaDoc() {
104                 public void run() {
105                     getShell().forceActive();
106                     new CopyFilesAndFoldersOperation(getShell()).copyFiles((String JavaDoc[]) data, target);
107                     // Import always performs a copy.
108
event.detail= DND.DROP_COPY;
109                 }
110             });
111         } catch (JavaModelException e) {
112             String JavaDoc title= PackagesMessages.DropAdapter_errorTitle;
113             String JavaDoc message= PackagesMessages.DropAdapter_errorMessage;
114             ExceptionHandler.handle(e, getShell(), title, message);
115         }
116     }
117     
118     private IContainer getActualTarget(Object JavaDoc dropTarget) throws JavaModelException{
119         if (dropTarget instanceof IContainer)
120             return (IContainer)dropTarget;
121         else if (dropTarget instanceof IJavaElement)
122             return getActualTarget(((IJavaElement)dropTarget).getCorrespondingResource());
123         return null;
124     }
125     
126     private Shell getShell() {
127         return getViewer().getControl().getShell();
128     }
129 }
130
Popular Tags