/
SetupTransferSupport.java
497 lines (431 loc) · 14.4 KB
/
SetupTransferSupport.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright (c) 2016, 2021 Ed Merks (Berlin, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Eike Stepper - initial API and implementation
* Hannes Wellmann - Bug 574644: Support direct drag&drop of Auto-Launch-Installer Button
*/
package org.eclipse.oomph.setup.ui;
import org.eclipse.oomph.base.util.ArchiveResourceFactoryImpl;
import org.eclipse.oomph.internal.ui.OomphDropAdapter;
import org.eclipse.oomph.internal.ui.OomphEditingDomain;
import org.eclipse.oomph.internal.ui.OomphTransferDelegate;
import org.eclipse.oomph.setup.internal.core.SetupContext;
import org.eclipse.oomph.setup.internal.core.util.SetupCoreUtil;
import org.eclipse.oomph.setup.p2.util.MarketPlaceListing;
import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.command.IdentityCommand;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.EMFEditPlugin;
import org.eclipse.emf.edit.command.AbstractOverrideableCommand;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.command.CopyCommand;
import org.eclipse.emf.edit.command.DragAndDropCommand;
import org.eclipse.emf.edit.command.DragAndDropFeedback;
import org.eclipse.emf.edit.command.PasteFromClipboardCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.emf.edit.provider.resource.ResourceSetItemProvider;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Control;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Ed Merks
*/
public class SetupTransferSupport
{
public static final List<? extends OomphTransferDelegate> USER_RESOLVING_DELEGATES;
static
{
List<? extends OomphTransferDelegate> delegates = OomphTransferDelegate.merge(OomphTransferDelegate.DELEGATES,
new OomphTransferDelegate.FileTransferDelegate()
{
@Override
protected void gather(EditingDomain domain, URI uri)
{
super.gather(domain, SetupContext.isUserScheme(uri.scheme()) ? SetupContext.resolve(uri) : uri);
}
}, //
new OomphTransferDelegate.URLTransferDelegate()
{
@Override
protected void gather(EditingDomain domain, URI uri)
{
super.gather(domain, SetupContext.isUserScheme(uri.scheme()) ? SetupContext.resolve(uri) : uri);
}
});
USER_RESOLVING_DELEGATES = Collections.unmodifiableList(delegates);
}
public static final List<? extends Transfer> USER_RESOLVING_TRANSFERS = OomphTransferDelegate.asTransfers(USER_RESOLVING_DELEGATES);
public static Transfer[] userResolvingTransfers()
{
return USER_RESOLVING_TRANSFERS.toArray(new Transfer[USER_RESOLVING_TRANSFERS.size()]);
}
private final List<DropListener> dropListeners = new UniqueEList<SetupTransferSupport.DropListener>();
private final List<DropTarget> dropTargets = new ArrayList<DropTarget>();
private final OomphEditingDomain editingDomain;
private final OomphDropAdapter dropListener;
private List<Resource> resources = null;
public SetupTransferSupport()
{
ComposedAdapterFactory composedAdapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
composedAdapterFactory.insertAdapterFactory(new ResourceItemProviderAdapterFactory()
{
@Override
public Adapter createResourceSetAdapter()
{
return new ResourceSetItemProvider(this)
{
@Override
protected Command factorAddCommand(EditingDomain domain, CommandParameter commandParameter)
{
return createDragAndDropCommand(domain, commandParameter.getOwner(), 0.0F, 0, 0, commandParameter.getCollection());
}
@Override
protected Command createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation,
final Collection<?> collection)
{
Command result = new AddToResourceCommand(domain, collection);
if (!result.canExecute())
{
result = new LoadResourceCommand(domain, collection);
}
return result;
}
};
}
});
final BasicCommandStack commandStack = new BasicCommandStack()
{
@Override
protected void handleError(Exception exception)
{
// Ignore
}
};
editingDomain = new OomphEditingDomain(composedAdapterFactory, commandStack, null, USER_RESOLVING_DELEGATES)
{
@Override
public Command createCommand(Class<? extends Command> commandClass, CommandParameter commandParameter)
{
if (commandClass == CopyCommand.class)
{
return new IdentityCommand(commandParameter.getOwner());
}
return super.createCommand(commandClass, commandParameter);
}
};
final ResourceSet resourceSet = editingDomain.getResourceSet();
SetupCoreUtil.configureResourceSet(resourceSet);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("zip", new ArchiveResourceFactoryImpl()); //$NON-NLS-1$
commandStack.addCommandStackListener(new CommandStackListener()
{
public void commandStackChanged(EventObject event)
{
Command mostRecentCommand = commandStack.getMostRecentCommand();
if (mostRecentCommand != null)
{
Set<Resource> droppedResources = new LinkedHashSet<Resource>();
Collection<?> affectedObjects = mostRecentCommand.getAffectedObjects();
for (Object object : affectedObjects)
{
if (object instanceof Resource)
{
Resource resource = (Resource)object;
droppedResources.add(resource);
}
}
resourceSet.getResources().clear();
if (resources == null)
{
resourcesDropped(droppedResources);
}
else
{
resources.addAll(droppedResources);
}
}
}
});
dropListener = new OomphDropAdapter(editingDomain, null, USER_RESOLVING_DELEGATES);
}
public void addControl(Control control)
{
int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
DropTarget dropTarget = new DropTarget(control, dndOperations);
dropTarget.setTransfer(userResolvingTransfers());
dropTarget.addDropListener(dropListener);
dropTargets.add(dropTarget);
}
public void excludeControl(Control control)
{
int dndOperations = DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK;
DropTarget dropTarget = new DropTarget(control, dndOperations);
dropTarget.setTransfer(userResolvingTransfers());
dropTarget.addDropListener(new DropTargetAdapter()
{
@Override
public void dragEnter(DropTargetEvent event)
{
event.detail = DND.DROP_NONE;
}
});
dropTargets.add(dropTarget);
}
public void removeControls()
{
for (DropTarget dropTarget : dropTargets)
{
dropTarget.dispose();
}
dropTargets.clear();
}
public void urisDropped(Collection<? extends URI> uris)
{
try
{
Command command = editingDomain.createCommand(DragAndDropCommand.class,
new CommandParameter(editingDomain.getResourceSet(), new DragAndDropCommand.Detail(0.5f, 0, 0), null, uris));
editingDomain.getCommandStack().execute(command);
}
catch (RuntimeException exception)
{
}
}
public Collection<? extends Resource> getResources()
{
try
{
resources = new ArrayList<Resource>();
Command command = editingDomain.createCommand(PasteFromClipboardCommand.class, new CommandParameter(editingDomain.getResourceSet()));
editingDomain.getCommandStack().execute(command);
return resources;
}
catch (RuntimeException exception)
{
return resources;
}
finally
{
resources = null;
}
}
public void addDropListener(DropListener dropListener)
{
dropListeners.add(dropListener);
}
public void removeDropListener(DropListener dropListener)
{
dropListeners.remove(dropListener);
}
public void resourcesDropped(Collection<? extends Resource> resources)
{
for (DropListener dropListener : dropListeners)
{
dropListener.resourcesDropped(resources);
}
}
public interface DropListener
{
public void resourcesDropped(Collection<? extends Resource> resources);
}
}
abstract class ResourceCommand extends AbstractOverrideableCommand implements AbstractCommand.NonDirtying, DragAndDropFeedback
{
protected Collection<?> collection;
protected ResourceCommand(EditingDomain domain, Collection<?> collection)
{
super(domain);
this.collection = collection == null ? Collections.emptyList() : collection;
}
protected List<Resource> resources;
@Override
protected abstract boolean prepare();
@Override
public abstract void doExecute();
@Override
public void doUndo()
{
domain.getResourceSet().getResources().removeAll(resources);
resources = null;
}
@Override
public void doRedo()
{
doExecute();
}
@Override
public Collection<?> doGetAffectedObjects()
{
return resources == null ? Collections.singleton(domain.getResourceSet()) : resources;
}
@Override
public String doGetDescription()
{
return EMFEditPlugin.INSTANCE.getString("_UI_LoadResources_description"); //$NON-NLS-1$
}
@Override
public String doGetLabel()
{
return EMFEditPlugin.INSTANCE.getString("_UI_LoadResources_label"); //$NON-NLS-1$
}
public boolean validate(Object owner, float location, int operations, int operation, Collection<?> collection)
{
return false;
}
public int getFeedback()
{
return 0;
}
public int getOperation()
{
return DROP_COPY;
}
}
class AddToResourceCommand extends ResourceCommand
{
protected AddToResourceCommand(EditingDomain domain, Collection<?> collection)
{
super(domain, collection);
}
@Override
protected boolean prepare()
{
for (Object object : collection)
{
if (object instanceof EObject)
{
EObject eObject = (EObject)object;
if (eObject.eContainer() == null)
{
continue;
}
}
return false;
}
return true;
}
@Override
public void doExecute()
{
resources = new ArrayList<Resource>();
ResourceSet resourceSet = domain.getResourceSet();
for (Object object : collection)
{
EObject eObject = (EObject)object;
Resource originalResource = eObject.eResource();
URI uri = originalResource == null ? URI.createURI("dummy:/*.xmi") : originalResource.getURI(); //$NON-NLS-1$
Resource resource = resourceSet.getResource(uri, false);
if (resource == null)
{
resource = resourceSet.createResource(uri);
}
EcoreUtil.Copier copier = new EcoreUtil.Copier(false, true);
EObject copy = copier.copy(eObject);
copier.copyReferences();
resource.getContents().add(copy);
resources.add(resource);
}
}
}
class LoadResourceCommand extends ResourceCommand
{
private static final Pattern AUTO_LAUNCHER_WEBSITE = Pattern.compile("^https?://www\\.eclipse\\.org/setups/installer/\\?"); //$NON-NLS-1$
private static final Pattern SETUP_URL_PARAMETER = Pattern.compile("url=([^&]+)"); //$NON-NLS-1$
private static URI extractSetupURLIfAutoLauncherURL(URI uri)
{
if (AUTO_LAUNCHER_WEBSITE.matcher(uri.toString()).find() && uri.hasQuery())
{
Matcher matcher = SETUP_URL_PARAMETER.matcher(uri.query());
if (matcher.find())
{
String setupURL = matcher.group(1);
return URI.createURI(setupURL);
}
}
return uri;
}
private static Collection<?> extractSetupURLsIfAutoLauncherURLs(Collection<?> collection)
{
if (collection == null)
{
return collection;
}
Collection<Object> copy = new ArrayList<Object>(collection.size());
for (Object object : collection)
{
copy.add(object instanceof URI ? extractSetupURLIfAutoLauncherURL((URI)object) : object);
}
return copy;
}
protected LoadResourceCommand(EditingDomain domain, Collection<?> collection)
{
super(domain, extractSetupURLsIfAutoLauncherURLs(collection));
}
@Override
protected boolean prepare()
{
for (Object object : collection)
{
if (!(object instanceof URI))
{
return false;
}
URI uri = (URI)object;
String fileExtension = uri.fileExtension();
if (!"setup".equals(fileExtension) && !"zip".equals(fileExtension) //$NON-NLS-1$ //$NON-NLS-2$
&& MarketPlaceListing.getMarketPlaceListing(uri, domain.getResourceSet().getURIConverter()) == null)
{
return false;
}
}
return true;
}
@Override
public void doExecute()
{
resources = new ArrayList<Resource>();
ResourceSet resourceSet = domain.getResourceSet();
for (Object object : collection)
{
URI uri = (URI)object;
MarketPlaceListing marketPlaceListing = MarketPlaceListing.getMarketPlaceListing(uri, resourceSet.getURIConverter());
if (marketPlaceListing != null)
{
uri = marketPlaceListing.getListing();
}
Resource resource = resourceSet.getResource(uri, marketPlaceListing != null);
if (resource == null)
{
resource = resourceSet.createResource(uri);
}
resources.add(resource);
}
}
}