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
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceGridGUI;
import java.util.Enumeration;
import java.awt.Component;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.JPopupMenu;
//
// This class behaves like a leaf; derived class that represent non-leaf nodes must
// override various methods.
//
public class TreeNodeBase implements javax.swing.tree.TreeNode, TreeCellRenderer
{
public Coordinator getCoordinator()
{
return _parent.getCoordinator();
}
public Enumeration children()
{
return new Enumeration()
{
public boolean hasMoreElements()
{
return false;
}
public Object nextElement()
{
throw new java.util.NoSuchElementException();
}
};
}
public boolean getAllowsChildren()
{
return false;
}
public javax.swing.tree.TreeNode getChildAt(int childIndex)
{
return null;
}
public int getChildCount()
{
return 0;
}
public int getIndex(javax.swing.tree.TreeNode node)
{
return -1;
}
public javax.swing.tree.TreeNode getParent()
{
return _parent;
}
public boolean isLeaf()
{
return true;
}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
return null;
}
public String toString()
{
return _id;
}
public JPopupMenu getPopupMenu()
{
return null;
}
public String getId()
{
return _id;
}
public TreePath getPath()
{
if(_parent == null)
{
return new TreePath(this);
}
else
{
return _parent.getPath().pathByAddingChild(this);
}
}
public java.util.LinkedList getFullId()
{
java.util.LinkedList result = _parent == null ?
new java.util.LinkedList() :
_parent.getFullId();
result.add(_id);
return result;
}
public TreeNodeBase findChild(String id)
{
Enumeration p = children();
while(p.hasMoreElements())
{
TreeNodeBase child = (TreeNodeBase)p.nextElement();
if(child.getId().equals(id))
{
return child;
}
}
return null;
}
protected String makeNewChildId(String base)
{
String id = base;
int i = 0;
while(findChild(id) != null)
{
id = base + "-" + (++i);
}
return id;
}
protected TreeNodeBase(TreeNodeBase parent, String id)
{
_parent = parent;
_id = id;
}
//
// Helper functions
//
protected boolean insertSortedChild(TreeNodeBase newChild, java.util.List children,
DefaultTreeModel treeModel)
{
String id = newChild.getId();
int i;
for(i = 0; i < children.size(); ++i)
{
String otherId = ((TreeNodeBase)children.get(i)).getId();
int cmp = id.compareTo(otherId);
if(cmp == 0)
{
return false;
}
else if(cmp < 0)
{
break;
}
}
children.add(i, newChild);
if(treeModel != null)
{
treeModel.nodesWereInserted(this, new int[]{getIndex(newChild)});
}
return true;
}
protected String insertSortedChildren(java.util.List newChildren,
java.util.List intoChildren,
DefaultTreeModel treeModel)
{
TreeNodeBase[] children = (TreeNodeBase[])newChildren.toArray(new TreeNodeBase[0]);
java.util.Arrays.sort(children, _childComparator);
int[] indices = new int[children.length];
int offset = -1;
int i = 0;
boolean checkInsert = true;
for(int j = 0; j < children.length; ++j)
{
String id = children[j].getId();
if(checkInsert)
{
while(i < intoChildren.size())
{
TreeNodeBase existingChild = (TreeNodeBase)intoChildren.get(i);
int cmp = id.compareTo(existingChild.getId());
if(cmp == 0)
{
return id;
}
if(cmp < 0)
{
break; // while
}
i++;
}
if(i < intoChildren.size())
{
// Insert here, and increment i (since children is sorted)
intoChildren.add(i, children[j]);
if(offset == -1)
{
offset = getIndex((TreeNodeBase)intoChildren.get(0));
}
indices[j] = offset + i;
i++; continue; // for
}
else
{
checkInsert = false;
}
}
//
// Append
//
intoChildren.add(children[j]);
if(offset == -1)
{
offset = getIndex((TreeNodeBase)intoChildren.get(0));
}
indices[j] = offset + i;
i++;
}
if(treeModel != null)
{
treeModel.nodesWereInserted(this, indices);
}
return null;
}
protected void removeSortedChildren(String[] childIds,
java.util.List fromChildren,
DefaultTreeModel treeModel)
{
if(childIds.length == 0)
{
return; // nothing to do
}
assert fromChildren.size() > 0;
String[] ids = (String[])childIds.clone();
java.util.Arrays.sort(ids);
Object[] childrenToRemove = new Object[ids.length];
int[] indices = new int[ids.length];
int i = getIndex((TreeNodeBase)fromChildren.get(0));
int j = 0;
java.util.Iterator p = fromChildren.iterator();
while(p.hasNext() && j < ids.length)
{
TreeNodeBase child = (TreeNodeBase)p.next();
if(ids[j].equals(child.getId()))
{
childrenToRemove[j] = child;
indices[j] = i;
p.remove();
++j;
}
++i;
}
//
// Should be all removed
//
assert(j == ids.length);
if(treeModel != null)
{
treeModel.nodesWereRemoved(this, indices, childrenToRemove);
}
}
protected void childrenChanged(java.util.List children, DefaultTreeModel treeModel)
{
java.util.Iterator p = children.iterator();
while(p.hasNext())
{
TreeNodeBase child = (TreeNodeBase)p.next();
treeModel.nodeStructureChanged(child);
}
}
public int[] resize(int[] array, int size)
{
int[] result = new int[size];
System.arraycopy(array, 0, result, 0, size);
return result;
}
protected TreeNodeBase find(String id, java.util.List inList)
{
java.util.Iterator p = inList.iterator();
while(p.hasNext())
{
TreeNodeBase node = (TreeNodeBase)p.next();
if(node.getId().equals(id))
{
return node;
}
}
return null;
}
protected TreeNodeBase _parent;
protected String _id;
protected java.util.Comparator _childComparator = new java.util.Comparator()
{
public int compare(Object o1, Object o2)
{
TreeNodeBase child1 = (TreeNodeBase)o1;
TreeNodeBase child2 = (TreeNodeBase)o2;
return child1.getId().compareTo(child2.getId());
}
};
}
|