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 | |
package org.jfree.chart.plot; |
27 | |
|
28 | |
import java.util.Iterator; |
29 | |
import java.util.LinkedList; |
30 | |
|
31 | |
import javax.swing.event.ChangeEvent; |
32 | |
import javax.swing.event.ChangeListener; |
33 | |
|
34 | |
import org.jfree.data.hc.HCTreeNode; |
35 | |
import org.jfree.data.hc.DataRange; |
36 | |
import org.jfree.data.hc.DataRangeMismatchException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class HCTreeNodeInfo { |
46 | |
|
47 | |
private boolean open; |
48 | |
private HCTreeNode node; |
49 | |
private HCTreeNodeInfo parent; |
50 | |
private HCTreeNodeInfo leftChild; |
51 | |
private HCTreeNodeInfo rightChild; |
52 | |
private DataRange visibleDataRange; |
53 | |
private StandardHCClusteringInfo clusteringInfo; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public HCTreeNodeInfo( |
68 | |
StandardHCClusteringInfo clusteringInfo, |
69 | |
HCTreeNode node |
70 | 197 | ) { |
71 | |
|
72 | 197 | if (node == null) throw new NullPointerException( |
73 | |
"A HCTreeNodeInfo object must have a non-null HCTreeNode." |
74 | |
); |
75 | 196 | if (clusteringInfo == null) throw new NullPointerException( |
76 | |
"A HCTreeNodeInfo object must have a non-null clustering info." |
77 | |
); |
78 | |
|
79 | 195 | this.open = false; |
80 | 195 | this.node = node; |
81 | 195 | this.parent = null; |
82 | 195 | this.clusteringInfo = clusteringInfo; |
83 | 195 | this.visibleDataRange = new DataRange(0,0); |
84 | |
|
85 | 195 | if (node.getLeftChild() == null) this.leftChild = null; |
86 | |
else { |
87 | |
|
88 | 72 | this.leftChild = new HCTreeNodeInfo( |
89 | |
clusteringInfo, |
90 | |
node.getLeftChild() |
91 | |
); |
92 | 72 | this.leftChild.setParent(this); |
93 | 72 | this.open = true; |
94 | |
|
95 | |
} |
96 | 195 | if (node.getRightChild() == null) this.rightChild = null; |
97 | |
else { |
98 | |
|
99 | 72 | this.rightChild = new HCTreeNodeInfo( |
100 | |
clusteringInfo, |
101 | |
node.getRightChild() |
102 | |
); |
103 | 72 | this.rightChild.setParent(this); |
104 | 72 | this.open = true; |
105 | |
|
106 | |
} |
107 | |
|
108 | 195 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setNodeOpen(boolean open) { |
116 | |
|
117 | 5 | if ((this.leftChild != null) && (this.rightChild != null)) { |
118 | |
|
119 | |
|
120 | |
|
121 | 5 | this.open = open; |
122 | 5 | this.updateVisibleDataRange(); |
123 | 5 | this.clusteringInfo.notifyChangeListeners(new ChangeEvent(this)); |
124 | |
|
125 | |
} |
126 | |
|
127 | 5 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private void setSubTreeOpenRecursively(boolean open) { |
136 | |
|
137 | 20 | if ((this.leftChild != null) && (this.rightChild != null)) { |
138 | |
|
139 | 8 | this.leftChild.setSubTreeOpenRecursively(open); |
140 | 8 | this.rightChild.setSubTreeOpenRecursively(open); |
141 | 8 | this.open = open; |
142 | |
|
143 | |
} |
144 | |
|
145 | 20 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void setSubTreeOpen(boolean open) { |
153 | |
|
154 | 4 | if ((this.leftChild != null) && (this.rightChild != null)) { |
155 | |
|
156 | |
|
157 | |
|
158 | 4 | this.setSubTreeOpenRecursively(open); |
159 | 4 | this.updateVisibleDataRange(); |
160 | 4 | this.clusteringInfo.notifyChangeListeners( |
161 | |
new ChangeEvent(this)); |
162 | |
|
163 | |
} |
164 | |
|
165 | 4 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public boolean isNodeOpen() { |
174 | |
|
175 | 227 | return this.open; |
176 | |
|
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public HCTreeNode getNode() { |
185 | |
|
186 | 472 | return this.node; |
187 | |
|
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public HCTreeNodeInfo getLeftChild() { |
196 | |
|
197 | 321 | return this.leftChild; |
198 | |
|
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
public HCTreeNodeInfo getRightChild() { |
207 | |
|
208 | 284 | return this.rightChild; |
209 | |
|
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public HCTreeNodeInfo getParent() { |
218 | |
|
219 | 3 | return this.parent; |
220 | |
|
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public void setParent(HCTreeNodeInfo node) { |
235 | |
|
236 | |
|
237 | 148 | if ( |
238 | |
(parent != null) && |
239 | |
((parent.getLeftChild() != this) && |
240 | |
(parent.getRightChild() != this)) |
241 | 2 | ) throw new IllegalArgumentException( |
242 | |
"A HCTreeNodeInfo object can only be set as a parent of another " + |
243 | |
"object, if that node is already its child."); |
244 | 146 | if (this.parent != null) throw new IllegalArgumentException( |
245 | |
"You cannot set a parent for a HCTreeNodeInfo node that already " + |
246 | |
"has one."); |
247 | 145 | this.parent = node; |
248 | |
|
249 | 145 | } |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
public String toString() { |
257 | |
|
258 | 112 | return this.clusteringInfo.getName(this.getNode().getDataRange()); |
259 | |
|
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
public StandardHCClusteringInfo getClusteringInfo() { |
268 | |
|
269 | 3 | return this.clusteringInfo; |
270 | |
|
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
public DataRange getVisibleDataRange() { |
279 | |
|
280 | 627 | return this.visibleDataRange; |
281 | |
|
282 | |
} |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
public HCTreeNodeInfo getNodeByNode(HCTreeNode node) { |
295 | |
|
296 | 7 | DataRange dr = node.getDataRange(); |
297 | |
|
298 | 6 | if (dr.equals(this.getNode().getDataRange())) return this; |
299 | 3 | if ((this.getLeftChild() != null) && |
300 | |
(this.getLeftChild().getNode().getDataRange().contains(dr))) |
301 | 1 | return this.getLeftChild().getNodeByNode(node); |
302 | 2 | if ((this.getRightChild() != null) && |
303 | |
(this.getRightChild().getNode().getDataRange().contains(dr))) |
304 | 1 | return this.getRightChild().getNodeByNode(node); |
305 | |
|
306 | 1 | throw new IndexOutOfBoundsException(); |
307 | |
|
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public HCTreeNodeInfo getNodeByIndex(int index) { |
321 | |
|
322 | 7 | if ( |
323 | |
(this.leftChild != null) && |
324 | |
(this.leftChild.getNode().getDataRange().contains(index))) { |
325 | |
|
326 | 1 | return this.leftChild.getNodeByIndex(index); |
327 | |
|
328 | |
} |
329 | 6 | if ( |
330 | |
(this.rightChild != null) && |
331 | |
(this.rightChild.getNode().getDataRange().contains(index))) { |
332 | |
|
333 | 1 | return this.rightChild.getNodeByIndex(index); |
334 | |
|
335 | |
} |
336 | 5 | if (this.getNode().getDataRange().equals(new DataRange(index,index))) { |
337 | |
|
338 | 3 | return this; |
339 | |
|
340 | |
} |
341 | 2 | throw new IndexOutOfBoundsException( |
342 | |
"There is no node " + index + "under this node."); |
343 | |
|
344 | |
} |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
public HCTreeNodeInfo getNodeByVisibleIndex(int index) { |
357 | |
|
358 | 352 | if ( |
359 | |
(this.leftChild != null) && |
360 | |
(this.leftChild.getVisibleDataRange().contains(index)) && |
361 | |
(this.open)) { |
362 | |
|
363 | 91 | return this.leftChild.getNodeByVisibleIndex(index); |
364 | |
|
365 | |
} |
366 | 261 | if ( |
367 | |
(this.rightChild != null) && |
368 | |
(this.rightChild.getVisibleDataRange().contains(index)) && |
369 | |
(this.open)) { |
370 | |
|
371 | 126 | return this.rightChild.getNodeByVisibleIndex(index); |
372 | |
|
373 | |
} |
374 | 135 | if (this.visibleDataRange.equals(new DataRange(index,index))) { |
375 | |
|
376 | 125 | return this; |
377 | |
|
378 | |
} |
379 | 10 | throw new IndexOutOfBoundsException( |
380 | |
"There is no visible node " + index + "under this node."); |
381 | |
|
382 | |
} |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
private int updateVisibleDataRange(int index) { |
396 | |
|
397 | 202 | if ( |
398 | |
(this.leftChild == null) || |
399 | |
(this.rightChild == null) || |
400 | |
(!this.open) |
401 | |
) { |
402 | |
|
403 | 124 | this.visibleDataRange = new DataRange(index,index); |
404 | 124 | return index+1; |
405 | |
|
406 | |
} else { |
407 | |
|
408 | 78 | index = this.getLeftChild().updateVisibleDataRange(index); |
409 | 78 | index = this.getRightChild().updateVisibleDataRange(index); |
410 | 78 | this.visibleDataRange = (DataRange) |
411 | |
(this.getLeftChild().getVisibleDataRange().clone()); |
412 | |
try { |
413 | 78 | this.visibleDataRange.add( |
414 | |
this.getRightChild().getVisibleDataRange() |
415 | |
); |
416 | 0 | } catch (DataRangeMismatchException e) { |
417 | |
|
418 | 78 | } |
419 | 78 | return index; |
420 | |
|
421 | |
} |
422 | |
|
423 | |
} |
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
public void updateVisibleDataRange() { |
431 | |
|
432 | 46 | this.clusteringInfo.getRootNode().updateVisibleDataRange(0); |
433 | |
|
434 | 46 | } |
435 | |
|
436 | |
} |
437 | |
|