]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/packageview/FileTransferDropAdapter.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / packageview / FileTransferDropAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.swt.dnd.DND;
14 import org.eclipse.swt.dnd.DropTargetEvent;
15 import org.eclipse.swt.dnd.FileTransfer;
16 import org.eclipse.swt.dnd.Transfer;
17 import org.eclipse.swt.dnd.TransferData;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IResource;
23
24 import org.eclipse.jface.util.TransferDropTargetListener;
25 import org.eclipse.jface.viewers.StructuredViewer;
26
27 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
28
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.IPackageFragment;
32 import org.eclipse.jdt.core.IPackageFragmentRoot;
33 import org.eclipse.jdt.core.JavaModelException;
34
35 import org.eclipse.jdt.internal.corext.util.Resources;
36
37 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
39
40 /**
41  * Adapter to handle file drop from other applications.
42  */
43 public class FileTransferDropAdapter extends JdtViewerDropAdapter implements TransferDropTargetListener {
44
45         public FileTransferDropAdapter(StructuredViewer viewer) {
46                 super(viewer);
47
48                 setScrollEnabled(true);
49                 setExpandEnabled(true);
50                 setFeedbackEnabled(false);
51         }
52
53         //---- TransferDropTargetListener interface ---------------------------------------
54
55         /**
56          * {@inheritDoc}
57          */
58         public Transfer getTransfer() {
59                 return FileTransfer.getInstance();
60         }
61
62         /**
63          * {@inheritDoc}
64          */
65         public boolean isEnabled(DropTargetEvent event) {
66                 Object target= event.item != null ? event.item.getData() : null;
67                 if (target == null)
68                         return false;
69                 return target instanceof IJavaElement || target instanceof IResource;
70         }
71
72         //---- Actual DND -----------------------------------------------------------------
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         public boolean validateDrop(Object target, int operation, TransferData transferType) {
79                 return determineOperation(target, operation, transferType, DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_COPY) != DND.DROP_NONE;
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         protected int determineOperation(Object target, int operation, TransferData transferType, int operations) {
87
88                 boolean isPackageFragment= target instanceof IPackageFragment;
89                 boolean isJavaProject= target instanceof IJavaProject;
90                 boolean isPackageFragmentRoot= target instanceof IPackageFragmentRoot;
91                 boolean isContainer= target instanceof IContainer;
92
93                 if (!(isPackageFragment || isJavaProject || isPackageFragmentRoot || isContainer))
94                         return DND.DROP_NONE;
95
96                 if (isContainer) {
97                         IContainer container= (IContainer)target;
98                         if (container.isAccessible() && !Resources.isReadOnly(container))
99                                 return DND.DROP_COPY;
100                 } else {
101                         IJavaElement element= (IJavaElement)target;
102                         if (!element.isReadOnly())
103                                 return DND.DROP_COPY;
104                 }
105
106                 return DND.DROP_NONE;
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public boolean performDrop(final Object data) {
114                 try {
115                         final int currentOperation= getCurrentOperation();
116
117                         if (data == null || !(data instanceof String[]) || currentOperation != DND.DROP_COPY)
118                                 return false;
119
120                         final IContainer target= getActualTarget(getCurrentTarget());
121                         if (target == null)
122                                 return false;
123
124                         // Run the import operation asynchronously.
125                         // Otherwise the drag source (e.g., Windows Explorer) will be blocked
126                         // while the operation executes. Fixes bug 35796.
127                         Display.getCurrent().asyncExec(new Runnable() {
128                                 public void run() {
129                                         getShell().forceActive();
130                                         new CopyFilesAndFoldersOperation(getShell()).copyOrLinkFiles((String[])data, target, currentOperation);
131                                 }
132                         });
133
134                         return true;
135                 } catch (JavaModelException e) {
136                         String title= PackagesMessages.DropAdapter_errorTitle;
137                         String message= PackagesMessages.DropAdapter_errorMessage;
138                         ExceptionHandler.handle(e, getShell(), title, message);
139                         return false;
140                 }
141         }
142
143         private IContainer getActualTarget(Object dropTarget) throws JavaModelException{
144                 if (dropTarget instanceof IContainer)
145                         return (IContainer)dropTarget;
146                 else if (dropTarget instanceof IJavaElement)
147                         return getActualTarget(((IJavaElement)dropTarget).getCorrespondingResource());
148                 return null;
149         }
150
151         private Shell getShell() {
152                 return getViewer().getControl().getShell();
153         }
154 }