blob: 03cb59b34bf956f09f9110522597a7e2cf902f89 (
plain) (
blame)
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
|
/*****************************************************************************
* Copyright (c) 2011 Atos Origin Integration - CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tristan Faure (Atos Origin Integration) tristan.faure@atosorigin.com - Initial API and implementation
* Camille Letavernier (CEA LIST) camille.letavernier@cea.fr
*****************************************************************************/
package org.eclipse.papyrus.infra.onefile.internal.ui.model.adapters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.onefile.model.IPapyrusFile;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TransferData;
import org.eclipse.ui.navigator.CommonDropAdapter;
import org.eclipse.ui.navigator.resources.ResourceDropAdapterAssistant;
/**
* Assistant to manage drag and drop of {@link IPapyrusFile} {@link IPapyrusFile} are not adapted to {@link IResource} to prevent
* misunderstanding so during transfer the selection is changed
*
* @author tristan.faure@atosorigin.com
*
*/
public class PapyrusCommonDropAdapterAssistant extends ResourceDropAdapterAssistant {
@Override
public IStatus validateDrop(Object target, int aDropOperation, TransferData transferType) {
manageSelection();
return super.validateDrop(target, aDropOperation, transferType);
}
@Override
public IStatus handleDrop(CommonDropAdapter aDropAdapter, DropTargetEvent aDropTargetEvent, Object aTarget) {
manageSelection();
return super.handleDrop(aDropAdapter, aDropTargetEvent, aTarget);
}
@Override
protected void doInit() {
super.doInit();
manageSelection();
}
private void manageSelection() {
List<Object> elements = new ArrayList<Object>();
ISelection selec = LocalSelectionTransfer.getTransfer().getSelection();
boolean selectionChanged = false;
if (selec instanceof IStructuredSelection) {
IStructuredSelection struc = (IStructuredSelection) selec;
for (Iterator<Object> i = struc.iterator(); i.hasNext();) {
Object o = i.next();
if (o instanceof IPapyrusFile) {
IPapyrusFile papy = (IPapyrusFile) o;
// TODO if a drop assistant is implemented use previous implementation :
// elements.add(papy.getMainFile());
elements.addAll(Arrays.asList(papy.getAssociatedResources()));
selectionChanged = true;
} else {
elements.add(o); // Do not prevent other drops.
}
}
}
if (!selectionChanged) {
return;
}
LocalSelectionTransfer.getTransfer().setSelection(new StructuredSelection(elements));
}
}
|