-
Notifications
You must be signed in to change notification settings - Fork 82
/
FileTransferDragAdapter.java
258 lines (225 loc) · 7.83 KB
/
FileTransferDragAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*******************************************************************************
* Copyright (c) 2000, 2016 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.packageview;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.TransferDragSourceListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Resources;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
import org.eclipse.jdt.internal.ui.util.Progress;
/**
* Drag support class to allow dragging of files and folder from
* the packages view to another application.
*/
public class FileTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
private ISelectionProvider fProvider;
public FileTransferDragAdapter(ISelectionProvider provider) {
fProvider= provider;
Assert.isNotNull(fProvider);
}
@Override
public Transfer getTransfer() {
return FileTransfer.getInstance();
}
@Override
public void dragStart(DragSourceEvent event) {
event.doit= isDragable(fProvider.getSelection());
}
private boolean isDragable(ISelection s) {
if (!(s instanceof IStructuredSelection))
return false;
IStructuredSelection selection= (IStructuredSelection)s;
for (Object element : selection) {
if (element instanceof IJavaElement) {
IJavaElement jElement= (IJavaElement)element;
int type= jElement.getElementType();
// valid elements are: roots, units and types. Don't allow dragging
// projects outside of eclipse
if (type != IJavaElement.PACKAGE_FRAGMENT_ROOT &&
type != IJavaElement.COMPILATION_UNIT && type != IJavaElement.TYPE)
return false;
IPackageFragmentRoot root= (IPackageFragmentRoot)jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (root != null && (root.isArchive() || root.isExternal()))
return false;
} else if (element instanceof IProject) {
return false;
}
}
List<IResource> resources= convertIntoResources(selection);
return resources.size() == selection.size();
}
@Override
public void dragSetData(DragSourceEvent event){
List<IResource> elements= getResources();
if (elements == null || elements.isEmpty()) {
event.data= null;
return;
}
event.data= getResourceLocations(elements);
}
private static String[] getResourceLocations(List<IResource> resources) {
return Resources.getLocationOSStrings(resources.toArray(new IResource[resources.size()]));
}
@Override
public void dragFinished(DragSourceEvent event) {
if (!event.doit)
return;
if (event.detail == DND.DROP_MOVE) {
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=30543
// handleDropMove(event);
} else if (event.detail == DND.DROP_TARGET_MOVE) {
handleRefresh();
}
}
/* package */ void handleDropMove() {
final List<IResource> elements= getResources();
if (elements == null || elements.isEmpty())
return;
WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
@Override
public void execute(IProgressMonitor monitor) throws CoreException {
try {
monitor.beginTask(PackagesMessages.DragAdapter_deleting, elements.size());
MultiStatus status= createMultiStatus();
Iterator<IResource> iter= elements.iterator();
while(iter.hasNext()) {
IResource resource= iter.next();
try {
monitor.subTask(BasicElementLabels.getPathLabel(resource.getFullPath(), true));
resource.delete(true, null);
} catch (CoreException e) {
status.add(e.getStatus());
} finally {
monitor.worked(1);
}
}
if (!status.isOK()) {
throw new CoreException(status);
}
} finally {
monitor.done();
}
}
};
runOperation(op, true, false);
}
private void handleRefresh() {
final Set<IResource> roots= collectRoots(getResources());
WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
@Override
public void execute(IProgressMonitor monitor) throws CoreException {
try {
monitor.beginTask(PackagesMessages.DragAdapter_refreshing, roots.size());
MultiStatus status= createMultiStatus();
Iterator<IResource> iter= roots.iterator();
while (iter.hasNext()) {
IResource r= iter.next();
try {
r.refreshLocal(IResource.DEPTH_ONE, Progress.subMonitor(monitor, 1));
} catch (CoreException e) {
status.add(e.getStatus());
}
}
if (!status.isOK()) {
throw new CoreException(status);
}
} finally {
monitor.done();
}
}
};
runOperation(op, true, false);
}
protected Set<IResource> collectRoots(final List<IResource> elements) {
final Set<IResource> roots= new HashSet<>(10);
Iterator<IResource> iter= elements.iterator();
while (iter.hasNext()) {
IResource resource= iter.next();
IResource parent= resource.getParent();
if (parent == null) {
roots.add(resource);
} else {
roots.add(parent);
}
}
return roots;
}
private List<IResource> getResources() {
ISelection s= fProvider.getSelection();
if (!(s instanceof IStructuredSelection))
return null;
return convertIntoResources((IStructuredSelection)s);
}
private List<IResource> convertIntoResources(IStructuredSelection selection) {
List<IResource> result= new ArrayList<>(selection.size());
for (Object o : selection) {
IResource r= null;
if (o instanceof IResource) {
r= (IResource)o;
} else if (o instanceof IAdaptable) {
r= ((IAdaptable)o).getAdapter(IResource.class);
}
// Only add resource for which we have a location
// in the local file system.
if (r != null && r.getLocation() != null) {
result.add(r);
}
}
return result;
}
private MultiStatus createMultiStatus() {
return new MultiStatus(JavaPlugin.getPluginId(),
IStatus.OK, PackagesMessages.DragAdapter_problem, null);
}
private void runOperation(IRunnableWithProgress op, boolean fork, boolean cancelable) {
try {
Shell parent= JavaPlugin.getActiveWorkbenchShell();
new ProgressMonitorDialog(parent).run(fork, cancelable, op);
} catch (InvocationTargetException e) {
String message= PackagesMessages.DragAdapter_problem;
String title= PackagesMessages.DragAdapter_problemTitle;
ExceptionHandler.handle(e, title, message);
} catch (InterruptedException e) {
// Do nothing. Operation has been canceled by user.
}
}
}