KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.MultiStatus;
26 import org.eclipse.core.runtime.SubProgressMonitor;
27
28 import org.eclipse.core.resources.IResource;
29
30 import org.eclipse.swt.dnd.DND;
31 import org.eclipse.swt.dnd.DragSourceAdapter;
32 import org.eclipse.swt.dnd.DragSourceEvent;
33 import org.eclipse.swt.dnd.FileTransfer;
34 import org.eclipse.swt.dnd.Transfer;
35 import org.eclipse.swt.widgets.Shell;
36
37 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
38 import org.eclipse.jface.operation.IRunnableWithProgress;
39 import org.eclipse.jface.util.TransferDragSourceListener;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.viewers.ISelectionProvider;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43
44 import org.eclipse.ui.actions.WorkspaceModifyOperation;
45
46 import org.eclipse.jdt.core.IJavaElement;
47 import org.eclipse.jdt.core.IPackageFragmentRoot;
48
49 import org.eclipse.jdt.internal.corext.util.Resources;
50
51 import org.eclipse.jdt.internal.ui.JavaPlugin;
52 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
53
54 /**
55  * Drag support class to allow dragging of files and folder from
56  * the packages view to another application.
57  */

58 class FileTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
59     
60     private ISelectionProvider fProvider;
61     
62     FileTransferDragAdapter(ISelectionProvider provider) {
63         fProvider= provider;
64         Assert.isNotNull(fProvider);
65     }
66
67     public Transfer getTransfer() {
68         return FileTransfer.getInstance();
69     }
70     
71     public void dragStart(DragSourceEvent event) {
72         event.doit= isDragable(fProvider.getSelection());
73     }
74     
75     private boolean isDragable(ISelection s) {
76         if (!(s instanceof IStructuredSelection))
77             return false;
78         IStructuredSelection selection= (IStructuredSelection)s;
79         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
80             Object JavaDoc element= iter.next();
81             if (element instanceof IJavaElement) {
82                 IJavaElement jElement= (IJavaElement)element;
83                 int type= jElement.getElementType();
84                 // valid elements are: roots, units and types. Don't allow dragging
85
// projects outside of eclipse
86
if (type != IJavaElement.PACKAGE_FRAGMENT_ROOT &&
87                     type != IJavaElement.COMPILATION_UNIT && type != IJavaElement.TYPE)
88                     return false;
89                 IPackageFragmentRoot root= (IPackageFragmentRoot)jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
90                 if (root != null && root.isArchive())
91                     return false;
92             }
93         }
94         List JavaDoc resources= convertIntoResources(selection);
95         return resources.size() == selection.size();
96     }
97     
98     public void dragSetData(DragSourceEvent event){
99         List JavaDoc elements= getResources();
100         if (elements == null || elements.size() == 0) {
101             event.data= null;
102             return;
103         }
104         
105         event.data= getResourceLocations(elements);
106     }
107
108     private static String JavaDoc[] getResourceLocations(List JavaDoc resources) {
109         return Resources.getLocationOSStrings((IResource[]) resources.toArray(new IResource[resources.size()]));
110     }
111     
112     public void dragFinished(DragSourceEvent event) {
113         if (!event.doit)
114             return;
115         
116         if (event.detail == DND.DROP_MOVE) {
117             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=30543
118
// handleDropMove(event);
119
} else if (event.detail == DND.DROP_NONE || event.detail == DND.DROP_TARGET_MOVE) {
120             handleRefresh(event);
121         }
122     }
123     
124     /* package */ void handleDropMove(DragSourceEvent event) {
125         final List JavaDoc elements= getResources();
126         if (elements == null || elements.size() == 0)
127             return;
128         
129         WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
130             public void execute(IProgressMonitor monitor) throws CoreException {
131                 try {
132                     monitor.beginTask(PackagesMessages.DragAdapter_deleting, elements.size());
133                     MultiStatus status= createMultiStatus();
134                     Iterator JavaDoc iter= elements.iterator();
135                     while(iter.hasNext()) {
136                         IResource resource= (IResource)iter.next();
137                         try {
138                             monitor.subTask(resource.getFullPath().toOSString());
139                             resource.delete(true, null);
140                             
141                         } catch (CoreException e) {
142                             status.add(e.getStatus());
143                         } finally {
144                             monitor.worked(1);
145                         }
146                     }
147                     if (!status.isOK()) {
148                         throw new CoreException(status);
149                     }
150                 } finally {
151                     monitor.done();
152                 }
153             }
154         };
155         runOperation(op, true, false);
156     }
157     
158     private void handleRefresh(DragSourceEvent event) {
159         final Set JavaDoc roots= collectRoots(getResources());
160         
161         WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
162             public void execute(IProgressMonitor monitor) throws CoreException {
163                 try {
164                     monitor.beginTask(PackagesMessages.DragAdapter_refreshing, roots.size());
165                     MultiStatus status= createMultiStatus();
166                     Iterator JavaDoc iter= roots.iterator();
167                     while (iter.hasNext()) {
168                         IResource r= (IResource)iter.next();
169                         try {
170                             r.refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(monitor, 1));
171                         } catch (CoreException e) {
172                             status.add(e.getStatus());
173                         }
174                     }
175                     if (!status.isOK()) {
176                         throw new CoreException(status);
177                     }
178                 } finally {
179                     monitor.done();
180                 }
181             }
182         };
183         
184         runOperation(op, true, false);
185     }
186
187     protected Set JavaDoc collectRoots(final List JavaDoc elements) {
188         final Set JavaDoc roots= new HashSet JavaDoc(10);
189         
190         Iterator JavaDoc iter= elements.iterator();
191         while (iter.hasNext()) {
192             IResource resource= (IResource)iter.next();
193             IResource parent= resource.getParent();
194             if (parent == null) {
195                 roots.add(resource);
196             } else {
197                 roots.add(parent);
198             }
199         }
200         return roots;
201     }
202     
203     private List JavaDoc getResources() {
204         ISelection s= fProvider.getSelection();
205         if (!(s instanceof IStructuredSelection))
206             return null;
207         
208         return convertIntoResources((IStructuredSelection)s);
209     }
210
211     private List JavaDoc convertIntoResources(IStructuredSelection selection) {
212         List JavaDoc result= new ArrayList JavaDoc(selection.size());
213         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
214             Object JavaDoc o= iter.next();
215             IResource r= null;
216             if (o instanceof IResource) {
217                 r= (IResource)o;
218             } else if (o instanceof IAdaptable) {
219                 r= (IResource)((IAdaptable)o).getAdapter(IResource.class);
220             }
221             // Only add resource for which we have a location
222
// in the local file system.
223
if (r != null && r.getLocation() != null) {
224                 result.add(r);
225             }
226         }
227         return result;
228     }
229     
230     private MultiStatus createMultiStatus() {
231         return new MultiStatus(JavaPlugin.getPluginId(),
232             IStatus.OK, PackagesMessages.DragAdapter_problem, null);
233     }
234     
235     private void runOperation(IRunnableWithProgress op, boolean fork, boolean cancelable) {
236         try {
237             Shell parent= JavaPlugin.getActiveWorkbenchShell();
238             new ProgressMonitorDialog(parent).run(fork, cancelable, op);
239         } catch (InvocationTargetException JavaDoc e) {
240             String JavaDoc message= PackagesMessages.DragAdapter_problem;
241             String JavaDoc title= PackagesMessages.DragAdapter_problemTitle;
242             ExceptionHandler.handle(e, title, message);
243         } catch (InterruptedException JavaDoc e) {
244             // Do nothing. Operation has been canceled by user.
245
}
246     }
247 }
248
Popular Tags