1
|
package eu.etaxonomy.taxeditor.navigation.navigator;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.Comparator;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.LinkedHashSet;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
10
|
|
11
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
12
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
13
|
import org.eclipse.swt.dnd.TransferData;
|
14
|
import org.eclipse.ui.internal.navigator.dnd.CommonDropAdapterDescriptor;
|
15
|
import org.eclipse.ui.internal.navigator.dnd.CommonDropDescriptorManager;
|
16
|
import org.eclipse.ui.internal.navigator.extensions.CommonDragAssistantDescriptor;
|
17
|
import org.eclipse.ui.internal.navigator.extensions.NavigatorViewerDescriptor;
|
18
|
import org.eclipse.ui.navigator.CommonDragAdapterAssistant;
|
19
|
import org.eclipse.ui.navigator.CommonDropAdapter;
|
20
|
import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
|
21
|
import org.eclipse.ui.navigator.INavigatorContentService;
|
22
|
import org.eclipse.ui.navigator.INavigatorDnDService;
|
23
|
|
24
|
import eu.etaxonomy.taxeditor.navigation.navigator.dnd.TaxonNavigatorDragAdapterAssistant;
|
25
|
|
26
|
public class TaxonNavigatorDnDService implements INavigatorDnDService {
|
27
|
private static final CommonDropAdapterAssistant[] NO_ASSISTANTS = new CommonDropAdapterAssistant[0];
|
28
|
|
29
|
private INavigatorContentService contentService;
|
30
|
|
31
|
private TaxonNavigatorDragAdapterAssistant[] dragAssistants;
|
32
|
|
33
|
private TreeNodeDropAdapter dropAdapter;
|
34
|
|
35
|
private final Map dropAssistants = new HashMap();
|
36
|
|
37
|
public TaxonNavigatorDnDService(INavigatorContentService contentService){
|
38
|
this.contentService = contentService;
|
39
|
}
|
40
|
|
41
|
|
42
|
@Override
|
43
|
public CommonDragAdapterAssistant[] getCommonDragAssistants() {
|
44
|
|
45
|
return dragAssistants;
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public void bindDragAssistant(CommonDragAdapterAssistant anAssistant) {
|
50
|
if(dragAssistants == null)
|
51
|
initializeDragAssistants();
|
52
|
TaxonNavigatorDragAdapterAssistant[] newDragAssistants = new TaxonNavigatorDragAdapterAssistant[dragAssistants.length + 1];
|
53
|
System.arraycopy(dragAssistants, 0, newDragAssistants, 0, dragAssistants.length);
|
54
|
newDragAssistants[dragAssistants.length] = (TaxonNavigatorDragAdapterAssistant) anAssistant;
|
55
|
dragAssistants = newDragAssistants;
|
56
|
|
57
|
}
|
58
|
|
59
|
private void initializeDragAssistants() {
|
60
|
int i = 0;
|
61
|
Set dragDescriptors = ((NavigatorViewerDescriptor) contentService
|
62
|
.getViewerDescriptor()).getDragAssistants();
|
63
|
dragAssistants = new TaxonNavigatorDragAdapterAssistant[dragDescriptors
|
64
|
.size()];
|
65
|
for (Iterator iter = dragDescriptors.iterator(); iter.hasNext();) {
|
66
|
CommonDragAssistantDescriptor descriptor = (CommonDragAssistantDescriptor) iter
|
67
|
.next();
|
68
|
dragAssistants[i++] = (TaxonNavigatorDragAdapterAssistant) descriptor.createDragAssistant();
|
69
|
}
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public CommonDropAdapterAssistant[] findCommonDropAdapterAssistants(
|
74
|
Object aDropTarget, TransferData aTransferType) {
|
75
|
CommonDropAdapterDescriptor[] descriptors = CommonDropDescriptorManager
|
76
|
.getInstance().findCommonDropAdapterAssistants(aDropTarget,
|
77
|
contentService);
|
78
|
|
79
|
if (descriptors.length == 0) {
|
80
|
return NO_ASSISTANTS;
|
81
|
}
|
82
|
|
83
|
if (LocalSelectionTransfer.getTransfer().isSupportedType(aTransferType)
|
84
|
&& LocalSelectionTransfer.getTransfer().getSelection() instanceof IStructuredSelection) {
|
85
|
return getAssistantsBySelection(descriptors, (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection());
|
86
|
}
|
87
|
return getAssistantsByTransferData(descriptors, aTransferType);
|
88
|
}
|
89
|
|
90
|
private CommonDropAdapterAssistant[] getAssistantsByTransferData(
|
91
|
CommonDropAdapterDescriptor[] descriptors,
|
92
|
TransferData aTransferType) {
|
93
|
|
94
|
Set assistants = new LinkedHashSet();
|
95
|
for (int i = 0; i < descriptors.length; i++) {
|
96
|
CommonDropAdapterAssistant asst = getAssistant(descriptors[i]);
|
97
|
if (asst.isSupportedType(aTransferType)) {
|
98
|
assistants.add(asst);
|
99
|
}
|
100
|
}
|
101
|
return sortAssistants((CommonDropAdapterAssistant[]) assistants
|
102
|
.toArray(new CommonDropAdapterAssistant[assistants.size()]));
|
103
|
|
104
|
}
|
105
|
|
106
|
private CommonDropAdapterAssistant[] getAssistantsBySelection(
|
107
|
CommonDropAdapterDescriptor[] descriptors, IStructuredSelection aSelection) {
|
108
|
|
109
|
Set assistants = new LinkedHashSet();
|
110
|
|
111
|
for (int i = 0; i < descriptors.length; i++) {
|
112
|
if(descriptors[i].areDragElementsSupported(aSelection)) {
|
113
|
assistants.add(getAssistant(descriptors[i]));
|
114
|
}
|
115
|
}
|
116
|
|
117
|
return sortAssistants((CommonDropAdapterAssistant[]) assistants
|
118
|
.toArray(new CommonDropAdapterAssistant[assistants.size()]));
|
119
|
}
|
120
|
|
121
|
private CommonDropAdapterAssistant getAssistant(
|
122
|
CommonDropAdapterDescriptor descriptor) {
|
123
|
TreeNodeDropAdapterAssistant asst = (TreeNodeDropAdapterAssistant) dropAssistants
|
124
|
.get(descriptor);
|
125
|
if (asst != null) {
|
126
|
return asst;
|
127
|
}
|
128
|
synchronized (dropAssistants) {
|
129
|
asst = (TreeNodeDropAdapterAssistant) dropAssistants.get(descriptor);
|
130
|
if (asst == null) {
|
131
|
dropAssistants.put(descriptor, (asst = (TreeNodeDropAdapterAssistant) descriptor
|
132
|
.createDropAssistant()));
|
133
|
asst.init(contentService);
|
134
|
//asst.setCommonDropAdapter(dropAdapter);
|
135
|
}
|
136
|
}
|
137
|
return asst;
|
138
|
}
|
139
|
|
140
|
|
141
|
private CommonDropAdapterAssistant[] sortAssistants(CommonDropAdapterAssistant[] array) {
|
142
|
Arrays.sort(array, new Comparator() {
|
143
|
public int compare(Object arg0, Object arg1) {
|
144
|
CommonDropAdapterAssistant a = (CommonDropAdapterAssistant) arg0;
|
145
|
CommonDropAdapterAssistant b = (CommonDropAdapterAssistant) arg1;
|
146
|
// This is to ensure that the navigator resources drop assistant will
|
147
|
// always be first on the list of drop assistant, if a conflict ever
|
148
|
// occurs.
|
149
|
String id = "org.eclipse.ui.navigator.resources."; //$NON-NLS-1$
|
150
|
if (a.getClass().getName().startsWith(id))
|
151
|
return -1;
|
152
|
if (b.getClass().getName().startsWith(id))
|
153
|
return 1;
|
154
|
return a.getClass().getName().compareTo(b.getClass().getName());
|
155
|
}
|
156
|
});
|
157
|
return array;
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public CommonDropAdapterAssistant[] findCommonDropAdapterAssistants(
|
162
|
Object aDropTarget, IStructuredSelection theDragSelection) {
|
163
|
// TODO Auto-generated method stub
|
164
|
return null;
|
165
|
}
|
166
|
|
167
|
public void setDropAdaptor(TreeNodeDropAdapter da) {
|
168
|
dropAdapter = da;
|
169
|
}
|
170
|
}
|