KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > navigator > JavaDropAdapterAssistant


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 package org.eclipse.jdt.internal.ui.navigator;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.swt.dnd.DND;
23 import org.eclipse.swt.dnd.DropTargetEvent;
24 import org.eclipse.swt.dnd.FileTransfer;
25 import org.eclipse.swt.dnd.TransferData;
26
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29
30 import org.eclipse.ui.actions.CopyFilesAndFoldersOperation;
31 import org.eclipse.ui.navigator.CommonDropAdapter;
32 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
33
34 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
35
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.JavaModelException;
38
39 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor;
40 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
41 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
42 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
43 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
44 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
45
46 import org.eclipse.jdt.internal.ui.packageview.PackagesMessages;
47 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
48 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyStarter;
49 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveStarter;
50 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
51
52 public class JavaDropAdapterAssistant extends CommonDropAdapterAssistant {
53
54     private List JavaDoc fElements;
55     private JavaMoveProcessor fMoveProcessor;
56     private int fCanMoveElements;
57     private JavaCopyProcessor fCopyProcessor;
58     private int fCanCopyElements;
59
60     public IStatus handleDrop(CommonDropAdapter dropAdapter, DropTargetEvent dropTargetEvent, Object JavaDoc target) {
61         if (LocalSelectionTransfer.getInstance().isSupportedType(dropAdapter.getCurrentTransfer())) {
62             try {
63
64                 switch (dropAdapter.getCurrentOperation()) {
65                     case DND.DROP_MOVE :
66                         handleDropMove(target);
67                         break;
68                     case DND.DROP_COPY :
69                         handleDropCopy(target);
70                         break;
71                 }
72             } catch (JavaModelException e) {
73                 ExceptionHandler.handle(e, PackagesMessages.SelectionTransferDropAdapter_error_title, PackagesMessages.SelectionTransferDropAdapter_error_message);
74             } catch (InvocationTargetException JavaDoc e) {
75                 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
76             } catch (InterruptedException JavaDoc e) {
77                 //ok
78
} finally {
79                 // The drag source listener must not perform any operation
80
// since this drop adapter did the remove of the source even
81
// if we moved something.
82
//event.detail= DND.DROP_NONE;
83
}
84             clear();
85             return Status.OK_STATUS;
86         } else if (FileTransfer.getInstance().isSupportedType(dropAdapter.getCurrentTransfer())) {
87             try {
88
89                 final Object JavaDoc data = FileTransfer.getInstance().nativeToJava(dropAdapter.getCurrentTransfer());
90                 if (!(data instanceof String JavaDoc[]))
91                     return Status.CANCEL_STATUS;
92
93                 final IContainer targetContainer = getActualTarget(target);
94                 if (targetContainer == null)
95                     return Status.CANCEL_STATUS;
96
97
98                 getShell().forceActive();
99                 new CopyFilesAndFoldersOperation(getShell()).copyFiles((String JavaDoc[]) data, targetContainer);
100             } catch (JavaModelException e) {
101                 String JavaDoc title = PackagesMessages.DropAdapter_errorTitle;
102                 String JavaDoc message = PackagesMessages.DropAdapter_errorMessage;
103                 ExceptionHandler.handle(e, getShell(), title, message);
104             }
105             return Status.OK_STATUS;
106         }
107         return Status.CANCEL_STATUS;
108     }
109
110     public IStatus validateDrop(Object JavaDoc target, int operation, TransferData transferType) {
111         IStatus result = Status.OK_STATUS;
112         if (LocalSelectionTransfer.getInstance().isSupportedType(transferType)) {
113             initializeSelection();
114             try {
115                 switch (operation) {
116                     case DND.DROP_DEFAULT :
117                         if (handleValidateDefault(target) != DND.DROP_NONE) {
118                             result = Status.OK_STATUS;
119                         } else {
120                             result = Status.CANCEL_STATUS;
121                         }
122                         break;
123                     case DND.DROP_COPY :
124                         if (handleValidateCopy(target) != DND.DROP_NONE) {
125                             result = Status.OK_STATUS;
126                         } else {
127                             result = Status.CANCEL_STATUS;
128                         }
129                         break;
130                     case DND.DROP_MOVE :
131                         if (handleValidateMove(target) != DND.DROP_NONE) {
132                             result = Status.OK_STATUS;
133                         } else {
134                             result = Status.CANCEL_STATUS;
135                         }
136                         break;
137                 }
138             } catch (JavaModelException e) {
139                 ExceptionHandler.handle(e, PackagesMessages.SelectionTransferDropAdapter_error_title, PackagesMessages.SelectionTransferDropAdapter_error_message);
140                 //event.detail= DND.DROP_NONE;
141
result = Status.CANCEL_STATUS;
142             }
143         }
144         return result;
145     }
146     
147     public boolean isSupportedType(TransferData transferType) {
148         return super.isSupportedType(transferType) || FileTransfer.getInstance().isSupportedType(transferType);
149     }
150     
151     private IContainer getActualTarget(Object JavaDoc dropTarget) throws JavaModelException {
152         if (dropTarget instanceof IContainer)
153             return (IContainer) dropTarget;
154         else if (dropTarget instanceof IJavaElement)
155             return getActualTarget(((IJavaElement) dropTarget).getCorrespondingResource());
156         return null;
157     }
158
159     protected void initializeSelection() {
160         if (fElements != null)
161             return;
162         ISelection s = LocalSelectionTransfer.getInstance().getSelection();
163         if (!(s instanceof IStructuredSelection))
164             return;
165         fElements = ((IStructuredSelection) s).toList();
166     }
167
168     private void handleDropMove(final Object JavaDoc target) throws JavaModelException, InvocationTargetException JavaDoc, InterruptedException JavaDoc {
169         IJavaElement[] javaElements = ReorgUtils.getJavaElements(fElements);
170         IResource[] resources = ReorgUtils.getResources(fElements);
171         ReorgMoveStarter starter = null;
172         if (target instanceof IResource)
173             starter = ReorgMoveStarter.create(javaElements, resources, (IResource) target);
174         else if (target instanceof IJavaElement)
175             starter = ReorgMoveStarter.create(javaElements, resources, (IJavaElement) target);
176         if (starter != null)
177             starter.run(getShell());
178     }
179  
180     private void handleDropCopy(final Object JavaDoc target) throws JavaModelException, InvocationTargetException JavaDoc, InterruptedException JavaDoc {
181         IJavaElement[] javaElements = ReorgUtils.getJavaElements(fElements);
182         IResource[] resources = ReorgUtils.getResources(fElements);
183         ReorgCopyStarter starter = null;
184         if (target instanceof IResource)
185             starter = ReorgCopyStarter.create(javaElements, resources, (IResource) target);
186         else if (target instanceof IJavaElement)
187             starter = ReorgCopyStarter.create(javaElements, resources, (IJavaElement) target);
188         if (starter != null)
189             starter.run(getShell());
190     }
191  
192     private int handleValidateCopy(Object JavaDoc target) throws JavaModelException {
193
194         final ICopyPolicy policy= ReorgPolicyFactory.createCopyPolicy(ReorgUtils.getResources(fElements), ReorgUtils.getJavaElements(fElements));
195         fCopyProcessor= policy.canEnable() ? new JavaCopyProcessor(policy) : null;
196         
197         if (!canCopyElements())
198             return DND.DROP_NONE;
199
200         if (target instanceof IResource && fCopyProcessor != null && fCopyProcessor.setDestination((IResource)target).isOK())
201             return DND.DROP_COPY;
202         else if (target instanceof IJavaElement && fCopyProcessor != null && fCopyProcessor.setDestination((IJavaElement)target).isOK())
203             return DND.DROP_COPY;
204         else
205             return DND.DROP_NONE;
206     }
207  
208     private int handleValidateDefault(Object JavaDoc target) throws JavaModelException {
209         if (target == null)
210             return DND.DROP_NONE;
211
212         return handleValidateMove(target);
213     }
214
215     private int handleValidateMove(Object JavaDoc target) throws JavaModelException {
216         if (target == null)
217             return DND.DROP_NONE;
218         
219         IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(ReorgUtils.getResources(fElements), ReorgUtils.getJavaElements(fElements));
220         fMoveProcessor= (policy.canEnable()) ? new JavaMoveProcessor(policy) : null;
221
222         if (!canMoveElements())
223             return DND.DROP_NONE;
224
225         if (target instanceof IResource && fMoveProcessor != null && fMoveProcessor.setDestination((IResource)target).isOK())
226             return DND.DROP_MOVE;
227         else if (target instanceof IJavaElement && fMoveProcessor != null && fMoveProcessor.setDestination((IJavaElement)target).isOK())
228             return DND.DROP_MOVE;
229         else
230             return DND.DROP_NONE;
231     }
232
233
234     private boolean canMoveElements() {
235         if (fCanMoveElements == 0) {
236             fCanMoveElements = 2;
237             if (fMoveProcessor == null)
238                 fCanMoveElements = 1;
239         }
240         return fCanMoveElements == 2;
241     }
242
243
244     private boolean canCopyElements() {
245         if (fCanCopyElements == 0) {
246             fCanCopyElements = 2;
247             if (fCopyProcessor == null)
248                 fCanCopyElements = 1;
249         }
250         return fCanCopyElements == 2;
251     }
252
253     private void clear() {
254         fElements = null;
255         fMoveProcessor = null;
256         fCanMoveElements = 0;
257         fCopyProcessor = null;
258         fCanCopyElements = 0;
259     }
260 }
261
Popular Tags