/
ServiceTreeModel.java
229 lines (197 loc) · 6.44 KB
/
ServiceTreeModel.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
/**
*
*/
package net.sf.taverna.t2.workbench.ui.servicepanel.servicetree;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.tree.DefaultTreeModel;
import net.sf.taverna.t2.servicedescriptions.ServiceDescription;
import net.sf.taverna.t2.workbench.ui.servicepanel.ServiceFilter;
import org.apache.log4j.Logger;
/**
* @author alson
*
*/
public class ServiceTreeModel extends DefaultTreeModel {
public static final String AVAILABLE_SERVICES = "Available services";
public static final String NO_MATCHING_SERVICES = "No matching services";
public static final String MATCHING_SERVICES = "Matching services";
private static final ServiceTreeNode ROOT = new ServiceTreeNode(
AVAILABLE_SERVICES);
public static final String MOBY_OBJECTS = "MOBY Objects";
@SuppressWarnings("unchecked")
private static Comparator<Object> comparator = new ServicePathElementComparator();
private static Logger logger = Logger.getLogger(ServiceTreeModel.class);
private final Map<ServiceDescription<?>, ServiceTreeNode> serviceDescriptionToNode = new HashMap<ServiceDescription<?>, ServiceTreeNode>();
public ServiceTreeModel() {
super(ROOT);
}
/**
*
*/
private static final long serialVersionUID = 8669321454833989964L;
@SuppressWarnings("rawtypes")
public synchronized void addServiceDescriptions(
final Collection<? extends ServiceDescription> addedDescriptions) {
for (final ServiceDescription sd : addedDescriptions) {
addServiceDescription(sd);
}
}
@SuppressWarnings("rawtypes")
private void addServiceDescription(final ServiceDescription sd) {
final List path = sd.getPath();
ServiceTreeNode currentParent = ROOT;
for (final Object segment : path) {
currentParent = findOrCreateChild(currentParent, segment);
}
findOrCreateChild(currentParent, sd);
}
private ServiceTreeNode findOrCreateChild(
final ServiceTreeNode currentParent, final Object segment) {
final int childCount = currentParent.getChildCount();
for (int i = 0; i < childCount; i++) {
final ServiceTreeNode child = (ServiceTreeNode) currentParent
.getChildAt(i);
final int comparison = comparator.compare(child.getUserObject(),
segment);
if (comparison < 0) {
continue;
}
if (comparison == 0) {
return child;
}
if (comparison > 0) {
final ServiceTreeNode newChild = createAndInsertNewChild(
currentParent, segment, i);
return newChild;
}
}
final ServiceTreeNode newChild = createAndInsertNewChild(currentParent,
segment, currentParent.getChildCount());
return newChild;
}
/**
* @param currentParent
* @param segment
* @param i
* @return
*/
@SuppressWarnings("rawtypes")
private ServiceTreeNode createAndInsertNewChild(
final ServiceTreeNode currentParent, final Object segment,
final int i) {
final ServiceTreeNode newChild = new ServiceTreeNode(segment);
if (segment instanceof ServiceDescription) {
serviceDescriptionToNode
.put((ServiceDescription) segment, newChild);
}
this.insertNodeInto(newChild, currentParent, i);
logger.info("New child inserted into " + currentParent.toString()
+ " at " + i);
return newChild;
}
@SuppressWarnings("rawtypes")
public void removeServiceDescriptions(
Collection<? extends ServiceDescription> removedDescriptions) {
for (final ServiceDescription sd : removedDescriptions) {
removeServiceDescription(sd);
}
}
private void removeServiceDescription(final ServiceDescription<?> sd) {
ServiceTreeNode node = serviceDescriptionToNode.get(sd);
if (node == null) {
return;
}
ServiceTreeNode parent = (ServiceTreeNode) node.getParent();
this.removeNodeFromParent(node);
while ((parent != null) && (parent.getChildCount() == 0)) {
node = parent;
parent = (ServiceTreeNode) node.getParent();
this.removeNodeFromParent(node);
}
}
@SuppressWarnings("rawtypes")
protected static class ServicePathElementComparator implements Comparator {
@Override
public int compare(final Object o1, final Object o2) {
final String o1ToCompare = (o1 instanceof ServiceDescription ? ((ServiceDescription) o1)
.getName() : o1.toString());
final String o2ToCompare = (o2 instanceof ServiceDescription ? ((ServiceDescription) o2)
.getName() : o2.toString());
if (o1ToCompare.equalsIgnoreCase(o2ToCompare)) {
return 0;
}
if (o1ToCompare.equals(ServiceDescription.SERVICE_TEMPLATES)) {
return -1;
}
if (o2ToCompare.equals(ServiceDescription.SERVICE_TEMPLATES)) {
return 1;
}
if (o1ToCompare.equals(ServiceDescription.LOCAL_SERVICES)) {
return -1;
}
if (o2ToCompare.equals(ServiceDescription.LOCAL_SERVICES)) {
return 1;
}
if (o1ToCompare.equals(MOBY_OBJECTS)) {
return -1;
}
if (o2ToCompare.equals(MOBY_OBJECTS)) {
return 1;
}
return o1ToCompare.compareToIgnoreCase(o2ToCompare);
}
}
public ServiceTreeModel cloneWithFilter(final ServiceFilter filter) {
if (filter == null) {
return this;
}
if (filter != null) {
final String filterString = filter.getFilterString();
if ((filterString == null) || filterString.isEmpty()) {
return this;
}
}
final ServiceTreeModel clonedModel = new ServiceTreeModel();
clonedModel.setRoot(cloneAndFilterNode(
(ServiceTreeNode) this.getRoot(), filter, false));
if (clonedModel.getRoot() == null) {
clonedModel.setRoot(new ServiceTreeNode(NO_MATCHING_SERVICES));
} else {
((ServiceTreeNode) clonedModel.getRoot())
.setUserObject(MATCHING_SERVICES);
}
return clonedModel;
}
private ServiceTreeNode cloneAndFilterNode(final ServiceTreeNode node,
final ServiceFilter filter, final boolean forcePass) {
final ServiceTreeNode clonedNode = (ServiceTreeNode) node.clone();
if (forcePass || filter.pass(node)) {
final int count = node.getChildCount();
for (int i = 0; i < count; i++) {
final ServiceTreeNode child = (ServiceTreeNode) node
.getChildAt(i);
clonedNode.add(cloneAndFilterNode(child, filter, true));
}
return clonedNode;
} else {
final int count = node.getChildCount();
for (int i = 0; i < count; i++) {
final ServiceTreeNode child = (ServiceTreeNode) node
.getChildAt(i);
final ServiceTreeNode possibleClonedChild = cloneAndFilterNode(
child, filter, false);
if (possibleClonedChild != null) {
clonedNode.add(possibleClonedChild);
}
}
if (clonedNode.getChildCount() > 0) {
return clonedNode;
}
return null;
}
}
}