KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > NavigatorDragAdapter


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.ui.views.navigator;
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.resources.IFile;
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.swt.dnd.DND;
26 import org.eclipse.swt.dnd.DragSource;
27 import org.eclipse.swt.dnd.DragSourceAdapter;
28 import org.eclipse.swt.dnd.DragSourceEvent;
29 import org.eclipse.swt.dnd.DragSourceListener;
30 import org.eclipse.swt.dnd.FileTransfer;
31 import org.eclipse.swt.dnd.TransferData;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.actions.ReadOnlyStateChecker;
35 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
36 import org.eclipse.ui.part.ResourceTransfer;
37
38 /**
39  * Implements drag behaviour when items are dragged out of the
40  * resource navigator.
41  *
42  * @since 2.0
43  */

44 public class NavigatorDragAdapter extends DragSourceAdapter {
45     private static final String JavaDoc CHECK_MOVE_TITLE = ResourceNavigatorMessages.DragAdapter_title;
46
47     private static final String JavaDoc CHECK_DELETE_MESSAGE = ResourceNavigatorMessages.DragAdapter_checkDeleteMessage;
48
49     ISelectionProvider selectionProvider;
50
51     private TransferData lastDataType;
52
53     /**
54      * Constructs a new drag adapter.
55      * @param provider The selection provider
56      */

57     public NavigatorDragAdapter(ISelectionProvider provider) {
58         selectionProvider = provider;
59     }
60
61     /**
62      * This implementation of {@link DragSourceListener#dragFinished(DragSourceEvent)}
63      * responds to a drag that has moved resources outside the Navigator by deleting
64      * the corresponding source resource.
65      */

66     public void dragFinished(DragSourceEvent event) {
67         LocalSelectionTransfer.getInstance().setSelection(null);
68
69         if (event.doit == false) {
70             return;
71         }
72
73         final int typeMask = IResource.FOLDER | IResource.FILE;
74         if (event.detail == DND.DROP_MOVE) {
75             //never delete resources when dragging outside Eclipse.
76
//workaround for bug 30543.
77
if (lastDataType != null
78                     && FileTransfer.getInstance().isSupportedType(lastDataType)) {
79                 return;
80             }
81
82             IResource[] resources = getSelectedResources(typeMask);
83             DragSource dragSource = (DragSource) event.widget;
84             Control control = dragSource.getControl();
85             Shell shell = control.getShell();
86             ReadOnlyStateChecker checker;
87
88             if (resources == null || resources.length == 0) {
89                 return;
90             }
91
92             checker = new ReadOnlyStateChecker(shell, CHECK_MOVE_TITLE,
93                     CHECK_DELETE_MESSAGE);
94             resources = checker.checkReadOnlyResources(resources);
95             //delete the old elements
96
for (int i = 0; i < resources.length; i++) {
97                 try {
98                     resources[i].delete(IResource.KEEP_HISTORY
99                             | IResource.FORCE, null);
100                 } catch (CoreException e) {
101                     e.printStackTrace();
102                 }
103             }
104         } else if (event.detail == DND.DROP_TARGET_MOVE) {
105             IResource[] resources = getSelectedResources(typeMask);
106
107             // file moved for us by OS, no need to delete the resources, just
108
// update the view
109
if (resources == null) {
110                 return;
111             }
112             for (int i = 0; i < resources.length; i++) {
113                 try {
114                     resources[i].refreshLocal(IResource.DEPTH_INFINITE, null);
115                 } catch (CoreException e) {
116                     e.printStackTrace();
117                 }
118             }
119         }
120     }
121
122     /**
123      * This implementation of {@link DragSourceListener#dragSetData(DragSourceEvent)}
124      * sets the drag event data based on the current selection in the Navigator.
125      */

126     public void dragSetData(DragSourceEvent event) {
127         final int typeMask = IResource.FILE | IResource.FOLDER;
128         IResource[] resources = getSelectedResources(typeMask);
129
130         if (resources == null || resources.length == 0) {
131             return;
132         }
133
134         lastDataType = event.dataType;
135         //use local selection transfer if possible
136
if (LocalSelectionTransfer.getInstance()
137                 .isSupportedType(event.dataType)) {
138             event.data = LocalSelectionTransfer.getInstance().getSelection();
139             return;
140         }
141         //use resource transfer if possible
142
if (ResourceTransfer.getInstance().isSupportedType(event.dataType)) {
143             event.data = resources;
144             return;
145         }
146         //resort to a file transfer
147
if (!FileTransfer.getInstance().isSupportedType(event.dataType)) {
148             return;
149         }
150
151         // Get the path of each file and set as the drag data
152
final int length = resources.length;
153         int actualLength = 0;
154         String JavaDoc[] fileNames = new String JavaDoc[length];
155         for (int i = 0; i < length; i++) {
156             IPath location = resources[i].getLocation();
157             // location may be null. See bug 29491.
158
if (location != null) {
159                 fileNames[actualLength++] = location.toOSString();
160             }
161         }
162         if (actualLength == 0) {
163             return;
164         }
165         // was one or more of the locations null?
166
if (actualLength < length) {
167             String JavaDoc[] tempFileNames = fileNames;
168             fileNames = new String JavaDoc[actualLength];
169             for (int i = 0; i < actualLength; i++) {
170                 fileNames[i] = tempFileNames[i];
171             }
172         }
173         event.data = fileNames;
174     }
175
176     /**
177      * This implementation of {@link DragSourceListener#dragStart(DragSourceEvent)}
178      * allows the drag to start if the current Navigator selection contains resources
179      * that can be dragged.
180      */

181     public void dragStart(DragSourceEvent event) {
182         lastDataType = null;
183         // Workaround for 1GEUS9V
184
DragSource dragSource = (DragSource) event.widget;
185         Control control = dragSource.getControl();
186         if (control != control.getDisplay().getFocusControl()) {
187             event.doit = false;
188             return;
189         }
190
191         IStructuredSelection selection = (IStructuredSelection) selectionProvider
192                 .getSelection();
193         for (Iterator JavaDoc i = selection.iterator(); i.hasNext();) {
194             Object JavaDoc next = i.next();
195             if (!(next instanceof IFile || next instanceof IFolder)) {
196                 event.doit = false;
197                 return;
198             }
199         }
200         if (selection.isEmpty()) {
201             event.doit = false;
202             return;
203         }
204         LocalSelectionTransfer.getInstance().setSelection(selection);
205         event.doit = true;
206     }
207
208     private IResource[] getSelectedResources(int resourceTypes) {
209         List JavaDoc resources = new ArrayList JavaDoc();
210         IResource[] result = new IResource[0];
211
212         ISelection selection = selectionProvider.getSelection();
213         if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) {
214             return null;
215         }
216         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
217
218         // loop through list and look for matching items
219
Iterator JavaDoc itr = structuredSelection.iterator();
220         while (itr.hasNext()) {
221             Object JavaDoc obj = itr.next();
222             if (obj instanceof IResource) {
223                 IResource res = (IResource) obj;
224                 if ((res.getType() & resourceTypes) == res.getType()) {
225                     resources.add(res);
226                 }
227             }
228         }
229         result = new IResource[resources.size()];
230         resources.toArray(result);
231         return result;
232     }
233 }
234
Popular Tags