KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > resources > ResourceDragAdapterAssistant


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.navigator.resources;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedHashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.dnd.DragSourceEvent;
24 import org.eclipse.swt.dnd.FileTransfer;
25 import org.eclipse.swt.dnd.Transfer;
26 import org.eclipse.ui.navigator.CommonDragAdapterAssistant;
27 import org.eclipse.ui.navigator.INavigatorDnDService;
28 import org.eclipse.ui.part.ResourceTransfer;
29 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
30
31 /**
32  * Clients may reference this class in the <b>dragAssistant</b> element of a
33  * <b>org.eclipse.ui.navigator.viewer</b> extension point.
34  *
35  * <p>
36  * Clients may not extend or instantiate this class for any purpose other than
37  * {@link INavigatorDnDService#bindDragAssistant(CommonDragAdapterAssistant)}.
38  * Clients may have no direct dependencies on the contract of this class.
39  * </p>
40  *
41  * @since 3.2
42  *
43  */

44 public final class ResourceDragAdapterAssistant extends
45         CommonDragAdapterAssistant {
46
47     private static final boolean DEBUG = false;
48
49     private static final Transfer[] SUPPORTED_TRANSFERS = new Transfer[] {
50             ResourceTransfer.getInstance(),
51             LocalSelectionTransfer.getInstance(), FileTransfer.getInstance() };
52
53     private static final Class JavaDoc IRESOURCE_TYPE = IResource.class;
54
55     /*
56      * (non-Javadoc)
57      *
58      * @see org.eclipse.ui.navigator.CommonDragAdapterAssistant#getSupportedTransferTypes()
59      */

60     public Transfer[] getSupportedTransferTypes() {
61         return SUPPORTED_TRANSFERS;
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.eclipse.ui.navigator.CommonDragAdapterAssistant#setDragData(org.eclipse.swt.dnd.DragSourceEvent,
68      * org.eclipse.jface.viewers.IStructuredSelection)
69      */

70     public boolean setDragData(DragSourceEvent anEvent,
71             IStructuredSelection aSelection) {
72
73         if (LocalSelectionTransfer.getInstance().isSupportedType(
74                 anEvent.dataType)) {
75             anEvent.data = aSelection;
76             if (DEBUG) {
77                 System.out
78                         .println("ResourceDragAdapterAssistant.dragSetData set LocalSelectionTransfer"); //$NON-NLS-1$
79
}
80             return true;
81         } else if (ResourceTransfer.getInstance().isSupportedType(
82                 anEvent.dataType)) {
83             anEvent.data = getSelectedResources(aSelection);
84             if (DEBUG) {
85                 System.out
86                         .println("ResourceDragAdapterAssistant.dragSetData set ResourceTransfer"); //$NON-NLS-1$
87
}
88             return true;
89         } else if (FileTransfer.getInstance().isSupportedType(anEvent.dataType)) {
90
91             IResource[] resources = getSelectedResources(aSelection);
92
93             // Get the path of each file and set as the drag data
94
final int length = resources.length;
95             int actualLength = 0;
96             String JavaDoc[] fileNames = new String JavaDoc[length];
97             for (int i = 0; i < length; i++) {
98                 IPath location = resources[i].getLocation();
99                 // location may be null. See bug 29491.
100
if (location != null) {
101                     fileNames[actualLength++] = location.toOSString();
102                 }
103             }
104             if (actualLength > 0) {
105                 // was one or more of the locations null?
106
if (actualLength < length) {
107                     String JavaDoc[] tempFileNames = fileNames;
108                     fileNames = new String JavaDoc[actualLength];
109                     for (int i = 0; i < actualLength; i++)
110                         fileNames[i] = tempFileNames[i];
111                 }
112                 anEvent.data = fileNames;
113     
114                 if (DEBUG)
115                     System.out
116                             .println("ResourceDragAdapterAssistant.dragSetData set FileTransfer"); //$NON-NLS-1$
117
return true;
118             }
119         }
120         return false;
121
122     }
123
124     private IResource[] getSelectedResources(IStructuredSelection aSelection) {
125         Set JavaDoc resources = new LinkedHashSet JavaDoc();
126         IResource resource = null;
127         for (Iterator JavaDoc iter = aSelection.iterator(); iter.hasNext();) {
128             Object JavaDoc selected = iter.next();
129             resource = adaptToResource(selected);
130             if (resource != null) {
131                 resources.add(resource);
132         }
133         }
134         return (IResource[]) resources.toArray(new IResource[resources.size()]);
135     }
136
137     private IResource adaptToResource(Object JavaDoc selected) {
138         IResource resource;
139         if (selected instanceof IResource) {
140             resource = (IResource) selected;
141         } else if (selected instanceof IAdaptable) {
142             resource = (IResource) ((IAdaptable) selected)
143                     .getAdapter(IRESOURCE_TYPE);
144         } else {
145             resource = (IResource) Platform.getAdapterManager().getAdapter(
146                     selected, IRESOURCE_TYPE);
147         }
148         return resource;
149     }
150
151 }
152
Popular Tags