KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.ui.packageview;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.swt.dnd.DND;
19 import org.eclipse.swt.dnd.DropTargetEvent;
20 import org.eclipse.swt.dnd.Transfer;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.util.TransferDropTargetListener;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredViewer;
27
28 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
29
30 import org.eclipse.jdt.core.IJavaElement;
31 import org.eclipse.jdt.core.JavaModelException;
32
33 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor;
34 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
35 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
36 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
37 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
38 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
39
40 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDropAdapter;
41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
42 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyStarter;
43 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveStarter;
44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45
46 public class SelectionTransferDropAdapter extends JdtViewerDropAdapter implements TransferDropTargetListener {
47
48     private List JavaDoc fElements;
49     private JavaMoveProcessor fMoveProcessor;
50     private int fCanMoveElements;
51     private JavaCopyProcessor fCopyProcessor;
52     private int fCanCopyElements;
53     private ISelection fSelection;
54
55     private static final long DROP_TIME_DIFF_TRESHOLD= 150;
56
57     public SelectionTransferDropAdapter(StructuredViewer viewer) {
58         super(viewer, DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND);
59     }
60
61     //---- TransferDropTargetListener interface ---------------------------------------
62

63     public Transfer getTransfer() {
64         return LocalSelectionTransfer.getInstance();
65     }
66     
67     public boolean isEnabled(DropTargetEvent event) {
68         Object JavaDoc target= event.item != null ? event.item.getData() : null;
69         if (target == null)
70             return false;
71         return target instanceof IJavaElement || target instanceof IResource;
72     }
73
74     //---- Actual DND -----------------------------------------------------------------
75

76     public void dragEnter(DropTargetEvent event) {
77         clear();
78         super.dragEnter(event);
79     }
80     
81     public void dragLeave(DropTargetEvent event) {
82         clear();
83         super.dragLeave(event);
84     }
85     
86     private void clear() {
87         fElements= null;
88         fSelection= null;
89         fMoveProcessor= null;
90         fCanMoveElements= 0;
91         fCopyProcessor= null;
92         fCanCopyElements= 0;
93     }
94     
95     public void validateDrop(Object JavaDoc target, DropTargetEvent event, int operation) {
96         event.detail= DND.DROP_NONE;
97         
98         if (tooFast(event))
99             return;
100         
101         initializeSelection();
102                 
103         try {
104             switch(operation) {
105                 case DND.DROP_DEFAULT: event.detail= handleValidateDefault(target, event); break;
106                 case DND.DROP_COPY: event.detail= handleValidateCopy(target, event); break;
107                 case DND.DROP_MOVE: event.detail= handleValidateMove(target, event); break;
108             }
109         } catch (JavaModelException e){
110             ExceptionHandler.handle(e, PackagesMessages.SelectionTransferDropAdapter_error_title, PackagesMessages.SelectionTransferDropAdapter_error_message);
111             event.detail= DND.DROP_NONE;
112         }
113     }
114
115     protected void initializeSelection(){
116         if (fElements != null)
117             return;
118         ISelection s= LocalSelectionTransfer.getInstance().getSelection();
119         if (!(s instanceof IStructuredSelection))
120             return;
121         fSelection= s;
122         fElements= ((IStructuredSelection)s).toList();
123     }
124     
125     protected ISelection getSelection(){
126         return fSelection;
127     }
128     
129     private boolean tooFast(DropTargetEvent event) {
130         return Math.abs(LocalSelectionTransfer.getInstance().getSelectionSetTime() - (event.time & 0xFFFFFFFFL)) < DROP_TIME_DIFF_TRESHOLD;
131     }
132
133     public void drop(Object JavaDoc target, DropTargetEvent event) {
134         try{
135             switch(event.detail) {
136                 case DND.DROP_MOVE: handleDropMove(target, event); break;
137                 case DND.DROP_COPY: handleDropCopy(target, event); break;
138             }
139         } catch (JavaModelException e){
140             ExceptionHandler.handle(e, PackagesMessages.SelectionTransferDropAdapter_error_title, PackagesMessages.SelectionTransferDropAdapter_error_message);
141         } catch(InvocationTargetException JavaDoc e) {
142             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
143         } catch (InterruptedException JavaDoc e) {
144             //ok
145
} finally {
146             // The drag source listener must not perform any operation
147
// since this drop adapter did the remove of the source even
148
// if we moved something.
149
event.detail= DND.DROP_NONE;
150         }
151     }
152     
153     private int handleValidateDefault(Object JavaDoc target, DropTargetEvent event) throws JavaModelException{
154         if (target == null)
155             return DND.DROP_NONE;
156         
157         if ((event.operations & DND.DROP_MOVE) != 0) {
158             return handleValidateMove(target, event);
159         }
160         if ((event.operations & DND.DROP_COPY) != 0) {
161             return handleValidateCopy(target, event);
162         }
163         return DND.DROP_NONE;
164     }
165     
166     private int handleValidateMove(Object JavaDoc target, DropTargetEvent event) throws JavaModelException{
167         if (target == null)
168             return DND.DROP_NONE;
169         
170         if (fMoveProcessor == null) {
171             IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(ReorgUtils.getResources(fElements), ReorgUtils.getJavaElements(fElements));
172             if (policy.canEnable())
173                 fMoveProcessor= new JavaMoveProcessor(policy);
174         }
175
176         if (!canMoveElements())
177             return DND.DROP_NONE;
178
179         if (target instanceof IResource && fMoveProcessor != null && fMoveProcessor.setDestination((IResource)target).isOK())
180             return DND.DROP_MOVE;
181         else if (target instanceof IJavaElement && fMoveProcessor != null && fMoveProcessor.setDestination((IJavaElement)target).isOK())
182             return DND.DROP_MOVE;
183         else
184             return DND.DROP_NONE;
185     }
186     
187     private boolean canMoveElements() {
188         if (fCanMoveElements == 0) {
189             fCanMoveElements= 2;
190             if (fMoveProcessor == null)
191                 fCanMoveElements= 1;
192         }
193         return fCanMoveElements == 2;
194     }
195
196     private void handleDropMove(final Object JavaDoc target, DropTargetEvent event) throws JavaModelException, InvocationTargetException JavaDoc, InterruptedException JavaDoc{
197         IJavaElement[] javaElements= ReorgUtils.getJavaElements(fElements);
198         IResource[] resources= ReorgUtils.getResources(fElements);
199         ReorgMoveStarter starter= null;
200         if (target instanceof IResource)
201             starter= ReorgMoveStarter.create(javaElements, resources, (IResource)target);
202         else if (target instanceof IJavaElement)
203             starter= ReorgMoveStarter.create(javaElements, resources, (IJavaElement)target);
204         if (starter != null)
205             starter.run(getShell());
206     }
207
208     private int handleValidateCopy(Object JavaDoc target, DropTargetEvent event) throws JavaModelException{
209
210         if (fCopyProcessor == null) {
211             final ICopyPolicy policy= ReorgPolicyFactory.createCopyPolicy(ReorgUtils.getResources(fElements), ReorgUtils.getJavaElements(fElements));
212             fCopyProcessor= policy.canEnable() ? new JavaCopyProcessor(policy) : null;
213         }
214
215         if (!canCopyElements())
216             return DND.DROP_NONE;
217
218         if (target instanceof IResource && fCopyProcessor != null && fCopyProcessor.setDestination((IResource)target).isOK())
219             return DND.DROP_COPY;
220         else if (target instanceof IJavaElement && fCopyProcessor != null && fCopyProcessor.setDestination((IJavaElement)target).isOK())
221             return DND.DROP_COPY;
222         else
223             return DND.DROP_NONE;
224     }
225             
226     private boolean canCopyElements() {
227         if (fCanCopyElements == 0) {
228             fCanCopyElements= 2;
229             if (fCopyProcessor == null)
230                 fCanCopyElements= 1;
231         }
232         return fCanCopyElements == 2;
233     }
234     
235     private void handleDropCopy(final Object JavaDoc target, DropTargetEvent event) throws JavaModelException, InvocationTargetException JavaDoc, InterruptedException JavaDoc{
236         IJavaElement[] javaElements= ReorgUtils.getJavaElements(fElements);
237         IResource[] resources= ReorgUtils.getResources(fElements);
238         ReorgCopyStarter starter= null;
239         if (target instanceof IResource)
240             starter= ReorgCopyStarter.create(javaElements, resources, (IResource)target);
241         else if (target instanceof IJavaElement)
242             starter= ReorgCopyStarter.create(javaElements, resources, (IJavaElement)target);
243         if (starter != null)
244             starter.run(getShell());
245     }
246
247     private Shell getShell() {
248         return getViewer().getControl().getShell();
249     }
250 }
251
Popular Tags