KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > CommonDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.navigator;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.jface.util.LocalSelectionTransfer;
15 import org.eclipse.jface.viewers.StructuredViewer;
16 import org.eclipse.swt.dnd.DND;
17 import org.eclipse.swt.dnd.DropTargetEvent;
18 import org.eclipse.swt.dnd.FileTransfer;
19 import org.eclipse.swt.dnd.Transfer;
20 import org.eclipse.swt.dnd.TransferData;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Item;
24 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
25 import org.eclipse.ui.internal.navigator.dnd.NavigatorDnDService;
26 import org.eclipse.ui.internal.navigator.dnd.NavigatorPluginDropAction;
27 import org.eclipse.ui.part.PluginDropAdapter;
28 import org.eclipse.ui.part.PluginTransfer;
29
30 /**
31  * Provides an implementation of {@link PluginDropAdapter} which uses the
32  * extensions provided by the associated {@link INavigatorContentService}.
33  *
34  * <p>
35  * Clients should not need to create an instance of this class unless they are
36  * creating their own custom viewer. Otherwise, {@link CommonViewer} configures
37  * its drop adapter automatically.
38  * </p>
39  *
40  *
41  * @see INavigatorDnDService
42  * @see CommonDragAdapter
43  * @see CommonDragAdapterAssistant
44  * @see CommonDropAdapterAssistant
45  * @see CommonViewer
46  * @since 3.2
47  */

48 public final class CommonDropAdapter extends PluginDropAdapter {
49
50     private static final Transfer[] SUPPORTED_DROP_TRANSFERS = new Transfer[] {
51             LocalSelectionTransfer.getTransfer(), FileTransfer.getInstance(),
52             PluginTransfer.getInstance() };
53
54     private static final boolean DEBUG = false;
55
56     private final INavigatorContentService contentService;
57
58     private final NavigatorDnDService dndService;
59
60     /**
61      * Create a DropAdapter that handles a drop based on the given content
62      * service and selection provider.
63      *
64      * @param aContentService
65      * The content service this Drop Adapter is associated with
66      * @param aStructuredViewer
67      * The viewer this DropAdapter is associated with.
68      */

69     public CommonDropAdapter(INavigatorContentService aContentService,
70             StructuredViewer aStructuredViewer) {
71         super(aStructuredViewer);
72         contentService = aContentService;
73         dndService = (NavigatorDnDService) contentService.getDnDService();
74     }
75
76     /**
77      *
78      * @return An array of Transfers allowed by the CommonDropAdapter. Includes
79      * {@link LocalSelectionTransfer#getTransfer()},
80      * {@link FileTransfer#getInstance()},
81      * {@link PluginTransfer#getInstance()}.
82      * @see LocalSelectionTransfer
83      * @see FileTransfer
84      * @see PluginTransfer
85      */

86     public Transfer[] getSupportedDropTransfers() {
87         return SUPPORTED_DROP_TRANSFERS;
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.eclipse.jface.viewers.ViewerDropAdapter#dragEnter(org.eclipse.swt.dnd.DropTargetEvent)
94      */

95     public void dragEnter(DropTargetEvent event) {
96         super.dragEnter(event);
97
98         for (int i = 0; i < event.dataTypes.length; i++) {
99             if (LocalSelectionTransfer.getTransfer().isSupportedType(
100                     event.dataTypes[i])) {
101                 event.currentDataType = event.dataTypes[i];
102                 return;
103             }
104         }
105
106         for (int i = 0; i < event.dataTypes.length; i++) {
107             if (FileTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
108                 event.currentDataType = event.dataTypes[i];
109                 event.detail = DND.DROP_COPY;
110                 return;
111             }
112         }
113
114         for (int i = 0; i < event.dataTypes.length; i++) {
115             if (PluginTransfer.getInstance()
116                     .isSupportedType(event.dataTypes[i])) {
117                 event.currentDataType = event.dataTypes[i];
118                 return;
119             }
120         }
121
122         event.detail = DND.DROP_NONE;
123
124     }
125
126     /*
127      * (non-Javadoc)
128      *
129      * @see org.eclipse.swt.dnd.DropTargetAdapter#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
130      */

131     public void dragLeave(DropTargetEvent event) {
132         super.dragLeave(event);
133         if (LocalSelectionTransfer.getTransfer().isSupportedType(
134                 event.currentDataType)) {
135             event.data = NavigatorPluginDropAction
136                     .createTransferData(contentService);
137         }
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.ui.part.PluginDropAdapter#drop(org.eclipse.swt.dnd.DropTargetEvent)
144      */

145     public void drop(DropTargetEvent event) {
146         if (PluginTransfer.getInstance().isSupportedType(event.currentDataType)) {
147             super.drop(event);
148         } else {
149
150             Object JavaDoc target = getCurrentTarget() != null ?
151                             getCurrentTarget() : getViewer().getInput();
152                             
153             CommonDropAdapterAssistant[] assistants = dndService
154                 .findCommonDropAdapterAssistants(target, getCurrentTransfer());
155
156             IStatus valid = null;
157             for (int i = 0; i < assistants.length; i++) {
158                 try {
159  
160                     valid = assistants[i].validateDrop(getCurrentTarget(),
161                             getCurrentOperation(), getCurrentTransfer());
162                     if (valid != null && valid.isOK()) {
163                         assistants[i].handleDrop(this, event,
164                                 getCurrentTarget());
165                         return;
166                     }
167                 } catch (Throwable JavaDoc t) {
168                     NavigatorPlugin.logError(0, t.getMessage(), t);
169                 }
170             }
171         }
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object,
178      * int, org.eclipse.swt.dnd.TransferData)
179      */

180     public boolean validateDrop(Object JavaDoc aDropTarget, int theDropOperation,
181             TransferData theTransferData) {
182
183         if (DEBUG) {
184             System.out.println("CommonDropAdapter.validateDrop (begin)"); //$NON-NLS-1$
185
}
186
187         boolean result = false;
188
189         IStatus valid = null;
190
191         if (super.validateDrop(aDropTarget, theDropOperation, theTransferData)) {
192             result = true;
193         } else {
194
195             Object JavaDoc target = aDropTarget != null ? aDropTarget : getViewer().getInput();
196             CommonDropAdapterAssistant[] assistants = dndService
197                     .findCommonDropAdapterAssistants(target,
198                             theTransferData);
199             if (DEBUG) {
200                 System.out
201                         .println("CommonDropAdapter.validateDrop found " + assistants.length + " drop assistants"); //$NON-NLS-1$//$NON-NLS-2$
202
for(int i=0; i<assistants.length; i++)
203                     System.out.println("CommonDropAdapter.validateDrop :" + assistants[i].getClass().getName()); //$NON-NLS-1$
204

205             }
206             for (int i = 0; i < assistants.length; i++) {
207                 try {
208                     valid = assistants[i].validateDrop(target,
209                             theDropOperation, theTransferData);
210                 } catch (Throwable JavaDoc t) {
211                     NavigatorPlugin.logError(0, t.getMessage(), t);
212                 }
213                 if (valid != null && valid.isOK()) {
214                     result = true;
215                     if (DEBUG) {
216                         System.out
217                                 .println("CommonDropAdapter.validateDrop found \""+assistants[i].getClass().getName()+"\" would handle drop."); //$NON-NLS-1$ //$NON-NLS-2$
218
}
219                     break;
220                 }
221             }
222         }
223
224         if (DEBUG) {
225             System.out
226                     .println("CommonDropAdapter.validateDrop (returning " + (valid != null ? valid.getSeverity() + ": " + valid.getMessage() : "" + result) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
227
}
228
229
230         setScrollExpandEnabled(result);
231         return result;
232
233     }
234
235     /*
236      * The visibility of the following methods is raised for downstream clients
237      * (assistants).
238      */

239
240     /*
241      * (non-Javadoc)
242      *
243      * @see org.eclipse.jface.viewers.ViewerDropAdapter#getBounds(org.eclipse.swt.widgets.Item)
244      */

245     public Rectangle getBounds(Item item) {
246         return super.getBounds(item);
247     }
248
249     /*
250      * (non-Javadoc)
251      *
252      * @see org.eclipse.jface.viewers.ViewerDropAdapter#getCurrentLocation()
253      */

254     public int getCurrentLocation() {
255         return super.getCurrentLocation();
256     }
257
258     /*
259      * (non-Javadoc)
260      *
261      * @see org.eclipse.jface.viewers.ViewerDropAdapter#getCurrentOperation()
262      */

263     public int getCurrentOperation() {
264         return super.getCurrentOperation();
265     }
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see org.eclipse.jface.viewers.ViewerDropAdapter#getCurrentTarget()
271      */

272     public Object JavaDoc getCurrentTarget() {
273         return super.getCurrentTarget();
274     }
275
276     /*
277      * (non-Javadoc)
278      *
279      * @see org.eclipse.ui.part.PluginDropAdapter#getCurrentTransfer()
280      */

281     public TransferData getCurrentTransfer() {
282         return super.getCurrentTransfer();
283     }
284     
285     /**
286      * Returns the position of the given event's coordinates relative to its target.
287      * The position is determined to be before, after, or on the item, based on
288      * some threshold value.
289      *
290      * @param event the event
291      * @return one of the <code>LOCATION_* </code>constants defined in this class
292      */

293     protected int determineLocation(DropTargetEvent event) {
294         if (!(event.item instanceof Item)) {
295             return LOCATION_NONE;
296         }
297 // Item item = (Item) event.item;
298
Point coordinates = new Point(event.x, event.y);
299         coordinates = getViewer().getControl().toControl(coordinates);
300 // if (item != null) {
301
// Rectangle bounds = getBounds(item);
302
// if (bounds == null) {
303
// return LOCATION_NONE;
304
// }
305
// if ((coordinates.y - bounds.y) < 5) {
306
// return LOCATION_BEFORE;
307
// }
308
// if ((bounds.y + bounds.height - coordinates.y) < 5) {
309
// return LOCATION_AFTER;
310
// }
311
// }
312
return LOCATION_ON;
313     }
314
315 }
316
Popular Tags