KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dnd > ResourceTransferDragAdapter


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.dnd;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22
23 import org.eclipse.core.resources.IResource;
24
25 import org.eclipse.swt.dnd.DND;
26 import org.eclipse.swt.dnd.DragSourceAdapter;
27 import org.eclipse.swt.dnd.DragSourceEvent;
28 import org.eclipse.swt.dnd.Transfer;
29 import org.eclipse.swt.widgets.Shell;
30
31 import org.eclipse.jface.dialogs.ErrorDialog;
32 import org.eclipse.jface.util.TransferDragSourceListener;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.ISelectionProvider;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36
37 import org.eclipse.ui.part.ResourceTransfer;
38
39 import org.eclipse.jdt.core.IJavaElement;
40
41 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
42 import org.eclipse.jdt.internal.ui.JavaPlugin;
43 import org.eclipse.jdt.internal.ui.JavaUIMessages;
44 import org.eclipse.jdt.internal.ui.util.SWTUtil;
45
46 /**
47  * A drag adapter that transfers the current selection as </code>
48  * IResource</code>. Only those elements in the selection are part
49  * of the transfer which can be converted into an <code>IResource
50  * </code>.
51  */

52 public class ResourceTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
53
54     private ISelectionProvider fProvider;
55
56     private static final List JavaDoc EMPTY_LIST= new ArrayList JavaDoc(0);
57
58     /**
59      * Creates a new ResourceTransferDragAdapter for the given selection
60      * provider.
61      *
62      * @param provider the selection provider to access the viewer's selection
63      */

64     public ResourceTransferDragAdapter(ISelectionProvider provider) {
65         fProvider= provider;
66         Assert.isNotNull(fProvider);
67     }
68     
69     public Transfer getTransfer() {
70         return ResourceTransfer.getInstance();
71     }
72     
73     public void dragStart(DragSourceEvent event) {
74         event.doit= convertSelection().size() > 0;
75     }
76     
77     public void dragSetData(DragSourceEvent event) {
78         List JavaDoc resources= convertSelection();
79         event.data= resources.toArray(new IResource[resources.size()]);
80     }
81     
82     public void dragFinished(DragSourceEvent event) {
83         if (!event.doit)
84             return;
85
86         if (event.detail == DND.DROP_MOVE) {
87             handleFinishedDropMove(event);
88         }
89     }
90     
91     private List JavaDoc convertSelection() {
92         ISelection s= fProvider.getSelection();
93         if (!(s instanceof IStructuredSelection))
94             return EMPTY_LIST;
95         IStructuredSelection selection= (IStructuredSelection)s;
96         List JavaDoc result= new ArrayList JavaDoc(selection.size());
97         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
98             Object JavaDoc element= iter.next();
99             IResource resource= null;
100             if (element instanceof IJavaElement) {
101                 // don't use IAdaptable as for members only the top level type adapts
102
resource= ((IJavaElement) element).getResource();
103             } else if (element instanceof IAdaptable) {
104                 resource= (IResource) ((IAdaptable) element).getAdapter(IResource.class);
105             }
106             if (resource != null)
107                 result.add(resource);
108         }
109         return result;
110     }
111     
112     private void handleFinishedDropMove(DragSourceEvent event) {
113         MultiStatus status= new MultiStatus(
114             JavaPlugin.getPluginId(),
115             IJavaStatusConstants.INTERNAL_ERROR,
116             JavaUIMessages.ResourceTransferDragAdapter_cannot_delete_resource,
117             null);
118         List JavaDoc resources= convertSelection();
119         for (Iterator JavaDoc iter= resources.iterator(); iter.hasNext();) {
120             IResource resource= (IResource) iter.next();
121             try {
122                 resource.delete(true, null);
123             } catch (CoreException e) {
124                 status.add(e.getStatus());
125             }
126         }
127         if (status.getChildren().length > 0) {
128             Shell parent= SWTUtil.getShell(event.widget);
129             ErrorDialog error= new ErrorDialog(parent,
130                 JavaUIMessages.ResourceTransferDragAdapter_moving_resource,
131                 JavaUIMessages.ResourceTransferDragAdapter_cannot_delete_files,
132                 status, IStatus.ERROR);
133             error.open();
134         }
135     }
136 }
137
138
Popular Tags