1 | package vishnu.testvis.dendro;
|
---|
2 |
|
---|
3 | import java.awt.image.*;
|
---|
4 | import javax.swing.*;
|
---|
5 | import java.awt.*;
|
---|
6 | import java.awt.event.*;
|
---|
7 | import java.util.Vector;
|
---|
8 | import java.util.Enumeration;
|
---|
9 | import vishnu.testvis.visual.*;
|
---|
10 | import vishnu.testvis.object.*;
|
---|
11 | import vishnu.testvis.sammon.*;
|
---|
12 | import vishnu.datablock.*;
|
---|
13 |
|
---|
14 |
|
---|
15 | public class TreePanel extends JPanel
|
---|
16 | {
|
---|
17 |
|
---|
18 | DendroPanel parent;
|
---|
19 | Vishnu currentMain;
|
---|
20 | DataManager dataManager;
|
---|
21 | DataBlock data;
|
---|
22 | Image offScreen=null;
|
---|
23 | Dimension offScreenSize;
|
---|
24 | Graphics offGraphics;
|
---|
25 | Graphics2D offGraphics2D;
|
---|
26 | Vector clusterVect = null;
|
---|
27 | Cluster root = null;
|
---|
28 | Cluster selectedRoot = null;
|
---|
29 | ClusterObj selectedCluster = null;
|
---|
30 | ClusterObj oldCluster = null;
|
---|
31 |
|
---|
32 | int selectedClusterID = -1;
|
---|
33 | int oldClusterID = -1;
|
---|
34 | JPopupMenu popup;
|
---|
35 | boolean constructed = false;
|
---|
36 | int selectedDepth = -1;
|
---|
37 | int initBranchLength = 85;
|
---|
38 |
|
---|
39 | boolean firstTime = true;
|
---|
40 | Graphics2D big;
|
---|
41 | BufferedImage bi;
|
---|
42 | Rectangle area;
|
---|
43 | Dimension d = null;
|
---|
44 |
|
---|
45 | public TreePanel(DendroPanel parent, Vishnu frame)
|
---|
46 | {
|
---|
47 | currentMain = frame;
|
---|
48 | this.parent = parent;
|
---|
49 | dataManager = frame.dataManager;
|
---|
50 |
|
---|
51 | popup = new JPopupMenu();
|
---|
52 | popup.setVisible(false);
|
---|
53 | add(popup);
|
---|
54 |
|
---|
55 | this.addComponentListener( new ComponentAdapter()
|
---|
56 | {
|
---|
57 | public void componentResized(ComponentEvent evt)
|
---|
58 | {
|
---|
59 | repaint();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | );
|
---|
63 |
|
---|
64 | this.addMouseMotionListener( new MouseMotionAdapter()
|
---|
65 | {
|
---|
66 | public void mouseMoved(MouseEvent mouseEvent)
|
---|
67 | {
|
---|
68 | int x = mouseEvent.getX();
|
---|
69 | int y = mouseEvent.getY();
|
---|
70 |
|
---|
71 | if( clusterVect != null )
|
---|
72 | {
|
---|
73 |
|
---|
74 | oldClusterID = selectedClusterID;
|
---|
75 | oldCluster = selectedCluster;
|
---|
76 | selectedClusterID = -1;
|
---|
77 | selectedCluster = null;
|
---|
78 |
|
---|
79 | boolean notFound = true;
|
---|
80 | int i = 0;
|
---|
81 |
|
---|
82 | while( notFound && (i < clusterVect.size()) )
|
---|
83 | {
|
---|
84 | ClusterObj co = (ClusterObj)clusterVect.elementAt(i);
|
---|
85 |
|
---|
86 | if( co.inCircle(x,y) )
|
---|
87 | {
|
---|
88 | selectedCluster = co;
|
---|
89 | selectedClusterID = i;
|
---|
90 |
|
---|
91 | if( selectedClusterID != oldClusterID )
|
---|
92 | {
|
---|
93 | repaint();
|
---|
94 | }
|
---|
95 | notFound = false;
|
---|
96 | }
|
---|
97 | i++;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if( (oldClusterID != selectedClusterID) && ( oldClusterID != -1 ) )
|
---|
101 | {
|
---|
102 | popup.setVisible(false);
|
---|
103 | repaint();
|
---|
104 | //update();
|
---|
105 | }
|
---|
106 |
|
---|
107 | if( selectedClusterID != -1 )
|
---|
108 | {
|
---|
109 | if( selectedClusterID != oldClusterID )
|
---|
110 | {
|
---|
111 | popup.removeAll();
|
---|
112 | int size = selectedCluster.cluster._items;
|
---|
113 | String str = size + ((size > 1) ? " documents" : " document");
|
---|
114 | JMenuItem menuItem = new JMenuItem(str);
|
---|
115 | menuItem.addActionListener(new ActionListener()
|
---|
116 | {
|
---|
117 | public void actionPerformed(ActionEvent actionEvent)
|
---|
118 | {
|
---|
119 | getDocObjs();
|
---|
120 | String[] drillWords = selectedCluster.getFreqWords();
|
---|
121 | String keyWordStr = new String(drillWords[0]+" "+drillWords[1]+" "+drillWords[2]);
|
---|
122 |
|
---|
123 | updateHLinkPanel();
|
---|
124 | updateKeyWordTable();
|
---|
125 | }
|
---|
126 |
|
---|
127 | });
|
---|
128 |
|
---|
129 | popup.add(menuItem);
|
---|
130 |
|
---|
131 | popup.add(new JPopupMenu.Separator());
|
---|
132 |
|
---|
133 | int mode0 = -1;
|
---|
134 |
|
---|
135 | // if the currentRoot is selected, dont show "drilldown"
|
---|
136 | if( selectedClusterID == 0 )
|
---|
137 | {
|
---|
138 | // if we are at the top, dont show "undrill"
|
---|
139 | if( ((Object)selectedCluster.cluster).equals((Object)root) )
|
---|
140 | {
|
---|
141 | str = "Top of hierarchy";
|
---|
142 | mode0 = 2;
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | str = "Sequential undrill";
|
---|
147 | mode0 = 1;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | // if somewhere else in the tree
|
---|
151 | else
|
---|
152 | {
|
---|
153 | if( selectedCluster.cluster._items > 1 )
|
---|
154 | {
|
---|
155 | str = "Drill down";
|
---|
156 | mode0 = 0;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | str = "Outermost twigs";
|
---|
161 | mode0 = 3;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | final int mode = mode0;
|
---|
166 |
|
---|
167 | menuItem = new JMenuItem(str);
|
---|
168 | if( (mode != 0 ) && (mode != 1) )
|
---|
169 | menuItem.setEnabled(false);
|
---|
170 |
|
---|
171 | menuItem.addActionListener(new ActionListener()
|
---|
172 | {
|
---|
173 | public void actionPerformed(ActionEvent evt)
|
---|
174 | {
|
---|
175 | if( mode == 0 )
|
---|
176 | {
|
---|
177 | selectedRoot = selectedCluster.cluster;
|
---|
178 | String[] drillWords = selectedCluster.getFreqWords();
|
---|
179 | String keyWordStr = new String(drillWords[0]+" "+drillWords[1]+" "+drillWords[2]);
|
---|
180 |
|
---|
181 | // to establish new clusterobj vector
|
---|
182 | constructed = false;
|
---|
183 | repaint();
|
---|
184 | //update();
|
---|
185 | }
|
---|
186 | if( mode == 1 )
|
---|
187 | {
|
---|
188 | selectedRoot = selectedRoot._parent;
|
---|
189 | constructed = false;
|
---|
190 | repaint();
|
---|
191 | //update();
|
---|
192 | }
|
---|
193 | }
|
---|
194 | });
|
---|
195 | popup.add(menuItem);
|
---|
196 |
|
---|
197 | if( mode == 1 )
|
---|
198 | {
|
---|
199 | menuItem = new JMenuItem("Total undrill");
|
---|
200 | menuItem.addActionListener(new ActionListener()
|
---|
201 | {
|
---|
202 | public void actionPerformed(ActionEvent evt)
|
---|
203 | {
|
---|
204 | selectedRoot = root;
|
---|
205 | constructed = false;
|
---|
206 | //update();
|
---|
207 | repaint();
|
---|
208 | }
|
---|
209 | });
|
---|
210 | popup.add(menuItem);
|
---|
211 | }
|
---|
212 |
|
---|
213 | popup.add(new JPopupMenu.Separator());
|
---|
214 | int[] wordFreq = selectedCluster.cluster.getWordFreq();
|
---|
215 | String[] words = getMostFrequentWords(wordFreq);
|
---|
216 |
|
---|
217 | selectedCluster.setFreqWords(words);
|
---|
218 |
|
---|
219 | menuItem = new JMenuItem(words[0]);
|
---|
220 | menuItem.setEnabled(false);
|
---|
221 | popup.add(menuItem);
|
---|
222 |
|
---|
223 | menuItem = new JMenuItem(words[1]);
|
---|
224 | menuItem.setEnabled(false);
|
---|
225 | popup.add(menuItem);
|
---|
226 |
|
---|
227 | menuItem = new JMenuItem(words[2]);
|
---|
228 | menuItem.setEnabled(false);
|
---|
229 | popup.add(menuItem);
|
---|
230 |
|
---|
231 | popup.validate();
|
---|
232 | popup.setVisible(true);
|
---|
233 | }
|
---|
234 |
|
---|
235 | popup.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
|
---|
236 | mouseEvent.consume();
|
---|
237 | }
|
---|
238 | } // of clustervect != null
|
---|
239 | } // of mouse moved
|
---|
240 | });
|
---|
241 |
|
---|
242 | setVisible(true);
|
---|
243 | }
|
---|
244 |
|
---|
245 | public void clear()
|
---|
246 | {
|
---|
247 | constructed = false;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | public String[] getMostFrequentWords(int[] wordFreq)
|
---|
252 | {
|
---|
253 |
|
---|
254 |
|
---|
255 | int max1Freq = 0;
|
---|
256 | int max1Index = 0;
|
---|
257 | int max2Freq = 0;
|
---|
258 | int max2Index = 0;
|
---|
259 | int max3Freq = 0;
|
---|
260 | int max3Index = 0;
|
---|
261 |
|
---|
262 | for( int i = 0; i < wordFreq.length; i++ )
|
---|
263 | {
|
---|
264 | if( wordFreq[i] > max3Freq )
|
---|
265 | {
|
---|
266 | if( wordFreq[i] > max2Freq )
|
---|
267 | {
|
---|
268 | if( wordFreq[i] > max1Freq )
|
---|
269 | {
|
---|
270 | max3Freq = max2Freq;
|
---|
271 | max3Index = max2Index;
|
---|
272 | max2Freq = max1Freq;
|
---|
273 | max2Index = max1Index;
|
---|
274 | max1Freq = wordFreq[i];
|
---|
275 | max1Index = i;
|
---|
276 | }
|
---|
277 | else
|
---|
278 | {
|
---|
279 | max3Freq = max2Freq;
|
---|
280 | max3Index = max2Index;
|
---|
281 | max2Freq = wordFreq[i];
|
---|
282 | max2Index = i;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | else
|
---|
286 | {
|
---|
287 | max3Freq = wordFreq[i];
|
---|
288 | max3Index = i;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | }
|
---|
293 |
|
---|
294 | data = dataManager.getDataBlock();
|
---|
295 | String[] words = new String[3];
|
---|
296 | words[0] = data.words[max1Index];
|
---|
297 | words[1] = data.words[max2Index];
|
---|
298 | words[2] = data.words[max3Index];
|
---|
299 | return words;
|
---|
300 | }
|
---|
301 |
|
---|
302 | public void getDocObjs()
|
---|
303 | {
|
---|
304 |
|
---|
305 | Vector docVect = selectedCluster.cluster.getDocVect();
|
---|
306 | DocObj[] docs = new DocObj[docVect.size()];
|
---|
307 |
|
---|
308 | for( int i = 0; i < docVect.size(); i++ )
|
---|
309 | {
|
---|
310 | int index = ((Integer)docVect.elementAt(i)).intValue();
|
---|
311 | docs[i] = dataManager.getDocObjAt(index);
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | if( !selectedCluster.getOrderStatus() )
|
---|
316 | {
|
---|
317 | QSortForDocObj qSort = new QSortForDocObj();
|
---|
318 | try
|
---|
319 | {
|
---|
320 | qSort.sort(docs);
|
---|
321 | }
|
---|
322 | catch (Exception e)
|
---|
323 | {}
|
---|
324 |
|
---|
325 | selectedCluster.setDocObjs(docs);
|
---|
326 | selectedCluster.setOrderStatus(true);
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | }
|
---|
331 |
|
---|
332 | public void updateHLinkPanel()
|
---|
333 | {
|
---|
334 |
|
---|
335 | String htmlDescription = selectedCluster.formatDocs();
|
---|
336 | parent.hLinkPanel.setText(htmlDescription);
|
---|
337 | }
|
---|
338 |
|
---|
339 | public void updateKeyWordTable()
|
---|
340 | {
|
---|
341 | parent.keywordPanel.update(selectedCluster);
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | public void setConstructed(boolean b)
|
---|
346 | {
|
---|
347 | constructed = b;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | public void getData()
|
---|
352 | {
|
---|
353 | dataManager = currentMain.dataManager;
|
---|
354 | data = dataManager.getDataBlock();
|
---|
355 | SparseMatrix m = data.matrix;
|
---|
356 |
|
---|
357 | Vector idVect = new Vector();
|
---|
358 | Vector sammCluster = dataManager.getSammonClusters();
|
---|
359 |
|
---|
360 | for( int i = 0; i < sammCluster.size(); i++ )
|
---|
361 | {
|
---|
362 | SammCluster cluster = (SammCluster)sammCluster.elementAt(i);
|
---|
363 | idVect.addAll(cluster.getIDs());
|
---|
364 | }
|
---|
365 |
|
---|
366 | for( int c = 0; c < idVect.size(); c++ )
|
---|
367 | {
|
---|
368 | Integer i = (Integer)idVect.elementAt(c);
|
---|
369 | }
|
---|
370 |
|
---|
371 | for (int c = 0; c< idVect.size();c++)
|
---|
372 | {
|
---|
373 | // the document id of the cth document
|
---|
374 | Integer i = (Integer)idVect.elementAt(c);
|
---|
375 | }
|
---|
376 |
|
---|
377 | //int [][] full = m.getExpanded();
|
---|
378 | float [][] full = m.getExpanded();
|
---|
379 | // the number of new clusters generated is either 20
|
---|
380 | // or if there are not many docs half the #docs
|
---|
381 |
|
---|
382 | int limit = m.getRowCount();
|
---|
383 |
|
---|
384 | int clusterNum = 10;
|
---|
385 |
|
---|
386 | Clustering clustering = new Clustering(dataManager,Clustering.COMPLETE_LINKAGE,
|
---|
387 | full,m.getRowCount(),
|
---|
388 | m.getColumnCount(),1,
|
---|
389 | clusterNum,limit);
|
---|
390 |
|
---|
391 | root = clustering.getRoot();
|
---|
392 | selectedRoot = root;
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | public void update(Graphics g)
|
---|
397 | {
|
---|
398 | Graphics2D g2 = (Graphics2D)g;
|
---|
399 |
|
---|
400 | d = getSize();
|
---|
401 | int w = d.width;
|
---|
402 | int h = d.height;
|
---|
403 |
|
---|
404 | area = new Rectangle(d);
|
---|
405 | bi = (BufferedImage)createImage(w, h);
|
---|
406 | offGraphics2D = bi.createGraphics();
|
---|
407 | area.setLocation(w/2-50, h/2-25);
|
---|
408 | BasicStroke myStroke = new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
|
---|
409 |
|
---|
410 | offGraphics2D.setColor(Color.white);
|
---|
411 | offGraphics2D.fillRect(0,0,d.width,d.height);
|
---|
412 |
|
---|
413 | if( !constructed )
|
---|
414 | {
|
---|
415 | clusterVect = new Vector();
|
---|
416 | selectedCluster = null;
|
---|
417 | selectedClusterID = -1;
|
---|
418 | }
|
---|
419 |
|
---|
420 | int initRad = 0;
|
---|
421 | initBranchLength = (int)(Math.min(d.width,d.height)*0.12);
|
---|
422 |
|
---|
423 | Cluster cc = selectedRoot;
|
---|
424 | Point center = new Point(d.width/2,d.height/2);
|
---|
425 | int radius = (int)(Math.log(cc._items))* 3;
|
---|
426 | if( radius < 2 ) radius = 2;
|
---|
427 |
|
---|
428 | // all the leaf cluster ids underneath selected node
|
---|
429 | // to be painted bright red
|
---|
430 | Vector allSubNodeIDs = new Vector();
|
---|
431 |
|
---|
432 | if( !constructed ){
|
---|
433 | ClusterObj rootObj = new ClusterObj(cc,center,radius,dataManager);
|
---|
434 | clusterVect.addElement(rootObj);
|
---|
435 | }
|
---|
436 | else{
|
---|
437 | ClusterObj c = (ClusterObj)clusterVect.elementAt(0);
|
---|
438 | c.setPosition(center);
|
---|
439 | clusterVect.setElementAt(c,0);
|
---|
440 | c = (ClusterObj)clusterVect.elementAt(0);
|
---|
441 | }
|
---|
442 |
|
---|
443 | int depth = 1;
|
---|
444 | TheInt ii = new TheInt(1);
|
---|
445 |
|
---|
446 | boolean selected = false;
|
---|
447 |
|
---|
448 | // used to paint the tree different colour under selected node
|
---|
449 | selectedDepth = -1;
|
---|
450 |
|
---|
451 | if( cc._items > 1 ){
|
---|
452 | double alpha = (double)Math.PI/Math.pow(2,depth);
|
---|
453 | double theta = 0;
|
---|
454 |
|
---|
455 | if( (selectedClusterID == 0) )
|
---|
456 | {
|
---|
457 | selected = true;
|
---|
458 | selectedDepth = 0;
|
---|
459 | offGraphics2D.setColor(Color.blue);
|
---|
460 | }
|
---|
461 |
|
---|
462 | paintPoke(offGraphics2D,cc._child_1,center.x,center.y,center,depth,theta,-alpha,initRad, selected, allSubNodeIDs, ii);
|
---|
463 | paintPoke(offGraphics2D,cc._child_2,center.x,center.y,center,depth,theta,alpha,initRad, selected, allSubNodeIDs, ii);
|
---|
464 | }
|
---|
465 |
|
---|
466 | for( int i = 0; i < clusterVect.size(); i++ )
|
---|
467 | {
|
---|
468 | ClusterObj co = (ClusterObj)clusterVect.elementAt(i);
|
---|
469 |
|
---|
470 | // if leaf draw reddish
|
---|
471 | if( co.cluster._items == 1 )
|
---|
472 | {
|
---|
473 | int r = co.getRad();
|
---|
474 | offGraphics2D.setColor(new Color((float)1, (float)0.7, (float)0.7));
|
---|
475 | offGraphics2D.fillArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
476 | offGraphics2D.setColor(Color.black);
|
---|
477 | offGraphics2D.drawArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
478 | }
|
---|
479 | else // draw blueish
|
---|
480 | {
|
---|
481 | offGraphics2D.setColor(Color.white);
|
---|
482 | int r = co.getRad();
|
---|
483 | offGraphics2D.fillArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
484 | offGraphics2D.setColor(new Color((float)0.7, (float)0.7, (float) 1));
|
---|
485 | offGraphics2D.drawArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
486 | }
|
---|
487 |
|
---|
488 | }
|
---|
489 |
|
---|
490 | for( int i = 0; i < allSubNodeIDs.size(); i++ )
|
---|
491 | {
|
---|
492 | int value = ((Integer)allSubNodeIDs.elementAt(i)).intValue();
|
---|
493 | ClusterObj co = (ClusterObj)clusterVect.elementAt(value);
|
---|
494 | // if leaf
|
---|
495 | if( co.cluster._items == 1 )
|
---|
496 | {
|
---|
497 | int r = co.getRad();
|
---|
498 | offGraphics2D.setColor(Color.red);
|
---|
499 | offGraphics2D.fillArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
500 | offGraphics2D.setColor(Color.black);
|
---|
501 | offGraphics2D.drawArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
502 | }
|
---|
503 | else
|
---|
504 | {
|
---|
505 | offGraphics2D.setColor(Color.white);
|
---|
506 | int r = co.getRad();
|
---|
507 | offGraphics2D.fillArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
508 | offGraphics2D.setColor(Color.blue);
|
---|
509 | offGraphics2D.drawArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | }
|
---|
514 |
|
---|
515 | if( (selectedCluster != null) && (selectedCluster.cluster._items > 1) )
|
---|
516 | {
|
---|
517 | offGraphics2D.setColor(Color.blue);
|
---|
518 | offGraphics2D.fillArc(selectedCluster.getX()-selectedCluster.getRad(),selectedCluster.getY()-selectedCluster.getRad(),2*selectedCluster.getRad(),2*selectedCluster.getRad(),0,360);
|
---|
519 | }
|
---|
520 |
|
---|
521 | g2.drawImage(bi, 0, 0, this);
|
---|
522 | constructed = true;
|
---|
523 | }
|
---|
524 |
|
---|
525 |
|
---|
526 | public void update()
|
---|
527 | {
|
---|
528 |
|
---|
529 | if( !constructed )
|
---|
530 | {
|
---|
531 | clusterVect = new Vector();
|
---|
532 | selectedCluster = null;
|
---|
533 | selectedClusterID = -1;
|
---|
534 | }
|
---|
535 |
|
---|
536 | Dimension d = getSize();
|
---|
537 | offScreenSize = d;
|
---|
538 | if( d.width <= 0 ) return;
|
---|
539 |
|
---|
540 | offScreen = createImage(d.width,d.height);
|
---|
541 | offGraphics = offScreen.getGraphics();
|
---|
542 |
|
---|
543 | Graphics2D offGraphics2D = (Graphics2D)offGraphics;
|
---|
544 |
|
---|
545 | offGraphics2D.setColor(Color.white);
|
---|
546 | offGraphics2D.fillRect(0,0,d.width,d.height);
|
---|
547 | offGraphics2D.setColor(new Color((float)0.7, (float)0.7, (float)1));
|
---|
548 | BasicStroke myStroke = new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
|
---|
549 | offGraphics2D.setStroke(myStroke);
|
---|
550 |
|
---|
551 | int initRad = 0;
|
---|
552 |
|
---|
553 | Cluster cc = selectedRoot;
|
---|
554 | Point center = new Point(d.width/2,d.height/2);
|
---|
555 | int radius = (int)(Math.log(cc._items))* 3;
|
---|
556 | if( radius < 2 ) radius = 2;
|
---|
557 |
|
---|
558 | // all the leaf cluster ids underneath selected node
|
---|
559 | // to be painted bright red
|
---|
560 | Vector allSubNodeIDs = new Vector();
|
---|
561 |
|
---|
562 | if( !constructed )
|
---|
563 | {
|
---|
564 | ClusterObj rootObj = new ClusterObj(cc,center,radius,dataManager);
|
---|
565 | clusterVect.addElement(rootObj);
|
---|
566 | }
|
---|
567 | else
|
---|
568 | {
|
---|
569 | ClusterObj c = (ClusterObj)clusterVect.elementAt(0);
|
---|
570 | c.setPosition(center);
|
---|
571 | clusterVect.setElementAt(c,0);
|
---|
572 | c = (ClusterObj)clusterVect.elementAt(0);
|
---|
573 | }
|
---|
574 |
|
---|
575 | int depth = 1;
|
---|
576 | TheInt ii = new TheInt(1);
|
---|
577 |
|
---|
578 | boolean selected = false;
|
---|
579 |
|
---|
580 | // used to paint the tree different colour under selected node
|
---|
581 | selectedDepth = -1;
|
---|
582 |
|
---|
583 | if( cc._items > 1 ){
|
---|
584 | double alpha = (double)Math.PI/Math.pow(2,depth);
|
---|
585 | double theta = 0;
|
---|
586 |
|
---|
587 | if( (selectedClusterID == 0) ){
|
---|
588 | selected = true;
|
---|
589 | selectedDepth = 0;
|
---|
590 | offGraphics2D.setColor(Color.blue);
|
---|
591 | }
|
---|
592 |
|
---|
593 | paintPoke(offGraphics2D,cc._child_1,center.x,center.y,center,depth,theta,-alpha,initRad, selected, allSubNodeIDs, ii);
|
---|
594 | paintPoke(offGraphics2D,cc._child_2,center.x,center.y,center,depth,theta,alpha,initRad, selected, allSubNodeIDs, ii);
|
---|
595 | }
|
---|
596 |
|
---|
597 | for( int i = 0; i < clusterVect.size(); i++ )
|
---|
598 | {
|
---|
599 |
|
---|
600 | ClusterObj co = (ClusterObj)clusterVect.elementAt(i);
|
---|
601 |
|
---|
602 | // if leaf draw reddish
|
---|
603 | if( co.cluster._items == 1 )
|
---|
604 | {
|
---|
605 | int r = co.getRad();
|
---|
606 | offGraphics2D.setColor(new Color((float)1, (float)0.7, (float)0.7));
|
---|
607 | offGraphics2D.fillArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
608 | offGraphics2D.setColor(Color.black);
|
---|
609 | offGraphics2D.drawArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
610 | }
|
---|
611 | else // draw blueish
|
---|
612 | {
|
---|
613 | offGraphics2D.setColor(Color.white);
|
---|
614 | int r = co.getRad();
|
---|
615 | offGraphics2D.fillArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
616 | offGraphics2D.setColor(new Color((float)0.7, (float)0.7, (float) 1));
|
---|
617 | offGraphics2D.drawArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
618 | }
|
---|
619 |
|
---|
620 | }
|
---|
621 |
|
---|
622 | for( int i = 0; i < allSubNodeIDs.size(); i++ )
|
---|
623 | {
|
---|
624 | int value = ((Integer)allSubNodeIDs.elementAt(i)).intValue();
|
---|
625 | ClusterObj co = (ClusterObj)clusterVect.elementAt(value);
|
---|
626 | // if leaf
|
---|
627 | if( co.cluster._items == 1 )
|
---|
628 | {
|
---|
629 | int r = co.getRad();
|
---|
630 | offGraphics2D.setColor(Color.red);
|
---|
631 | offGraphics2D.fillArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
632 | offGraphics2D.setColor(Color.black);
|
---|
633 | offGraphics2D.drawArc(co.getX()-5,co.getY()-5,2*r,2*r,0,360);
|
---|
634 | }
|
---|
635 | else
|
---|
636 | {
|
---|
637 | offGraphics2D.setColor(Color.white);
|
---|
638 | int r = co.getRad();
|
---|
639 | offGraphics2D.fillArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
640 | offGraphics2D.setColor(Color.blue);
|
---|
641 | offGraphics2D.drawArc(co.getX()-r,co.getY()-r,2*r,2*r,0,360);
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | }
|
---|
646 |
|
---|
647 | if( (selectedCluster != null) && (selectedCluster.cluster._items > 1) )
|
---|
648 | {
|
---|
649 | offGraphics2D.setColor(Color.blue);
|
---|
650 | offGraphics2D.fillArc(selectedCluster.getX()-selectedCluster.getRad(),selectedCluster.getY()-selectedCluster.getRad(),2*selectedCluster.getRad(),2*selectedCluster.getRad(),0,360);
|
---|
651 | }
|
---|
652 |
|
---|
653 | repaint();
|
---|
654 | constructed = true;
|
---|
655 | }
|
---|
656 |
|
---|
657 | // theta is the angle in the polar representation
|
---|
658 | public void paintPoke(Graphics2D g, Cluster child, int x, int y, Point center, int d, double theta, double alpha,int r, boolean selected, Vector allSubNodeIDs, TheInt ii)
|
---|
659 | {
|
---|
660 | // translate parent cluster position to center
|
---|
661 | int x1 = x - center.x;
|
---|
662 | int y1 = y - center.y;
|
---|
663 |
|
---|
664 | // rotate cluster about origin by alpha to obtain intermediate tree point
|
---|
665 | int x_inter = (int)(x1 * Math.cos(alpha) + y1 * Math.sin(alpha)) + center.x;
|
---|
666 | int y_inter = (int)(x1 * - Math.sin(alpha) + y1 * Math.cos(alpha)) + center.y;
|
---|
667 |
|
---|
668 | // rotate cluster about origin by theta
|
---|
669 | int x2 = (int)(x1 * Math.cos(-theta) + y1 * Math.sin(-theta));
|
---|
670 | int y2 = (int)(x1 * - Math.sin(-theta) + y1 * Math.cos(-theta));
|
---|
671 |
|
---|
672 | // extend radius
|
---|
673 | int dRad = (initBranchLength-d*10);
|
---|
674 | if( dRad <= 0 ) dRad = 10;
|
---|
675 | int x3 = x2 + dRad;
|
---|
676 | int y3 = y2;
|
---|
677 |
|
---|
678 | // rotate back by (theta +/- alpha)
|
---|
679 | int x4 = (int)(x3 * Math.cos(theta+alpha) + y3 * Math.sin(theta+alpha));
|
---|
680 | int y4 = (int)(x3 * -Math.sin(theta+alpha) + y3 * Math.sin(theta+alpha));
|
---|
681 |
|
---|
682 | // finally, add the center back again
|
---|
683 | int x_new = x4 + center.x;
|
---|
684 | int y_new = y4 + center.y;
|
---|
685 |
|
---|
686 | int startAngle = (int)(180*theta/Math.PI);
|
---|
687 | int rangeAngle = (int)(180*alpha/Math.PI);
|
---|
688 |
|
---|
689 | boolean hitSelectedNode = false;
|
---|
690 | if( selectedClusterID != -1 )
|
---|
691 | {
|
---|
692 | Object obj1 = (Object)child;
|
---|
693 | ClusterObj selObj = (ClusterObj)clusterVect.elementAt(selectedClusterID);
|
---|
694 | Object obj2 = (Object)selObj.cluster;
|
---|
695 | hitSelectedNode = obj1.equals(obj2);
|
---|
696 | }
|
---|
697 |
|
---|
698 | int currentClusterID = -1;
|
---|
699 |
|
---|
700 | if( selectedClusterID != -1 )
|
---|
701 | {
|
---|
702 | int w = 0;
|
---|
703 | boolean found = false;
|
---|
704 | while( w < clusterVect.size() && !found )
|
---|
705 | {
|
---|
706 | Object obj1 = (Object)child;
|
---|
707 | ClusterObj obj = (ClusterObj)clusterVect.elementAt(w);
|
---|
708 | Object obj2 = (Object)obj.cluster;
|
---|
709 | if( obj1.equals(obj2) )
|
---|
710 | {
|
---|
711 | currentClusterID = w;
|
---|
712 | found = true;
|
---|
713 | }
|
---|
714 | w++;
|
---|
715 | }
|
---|
716 | }
|
---|
717 |
|
---|
718 | if( (d <= selectedDepth) && (!hitSelectedNode) )
|
---|
719 | {
|
---|
720 | g.setColor(new Color( (float) 0.7, (float) 0.7, (float) 1));
|
---|
721 | selected = false;
|
---|
722 |
|
---|
723 | }
|
---|
724 |
|
---|
725 | g.drawArc(center.x-r,center.y-r,2*r,2*r,startAngle,rangeAngle);
|
---|
726 | g.drawLine(x_inter,y_inter, x_new, y_new);
|
---|
727 |
|
---|
728 | if( hitSelectedNode )
|
---|
729 | {
|
---|
730 | selectedDepth = d;
|
---|
731 | selected = true;
|
---|
732 | g.setColor(Color.blue);
|
---|
733 | Vector docVector = child.getDocVect();
|
---|
734 | }
|
---|
735 |
|
---|
736 | g.setColor(selected ? Color.blue : new Color((float) 0.7, (float) 0.7, (float) 1) );
|
---|
737 |
|
---|
738 | int radius = (int)(Math.log(child._items))* 3;
|
---|
739 | if( radius < 2 ) radius = 2;
|
---|
740 |
|
---|
741 | if( child._items == 1 )
|
---|
742 | {
|
---|
743 | Vector docVector = child.getDocVect();
|
---|
744 | radius = Math.max(5,(int)Math.sqrt(docVector.size())*2);
|
---|
745 | }
|
---|
746 |
|
---|
747 | d++;
|
---|
748 | r = (int)(Math.sqrt((x_new-center.x)*(x_new-center.x) + (y_new-center.y)*(y_new-center.y)));
|
---|
749 |
|
---|
750 | Point P = new Point(x_new,y_new);
|
---|
751 | if( !constructed )
|
---|
752 | {
|
---|
753 | ClusterObj cObj = new ClusterObj(child,P,radius,dataManager);
|
---|
754 | clusterVect.addElement(cObj);
|
---|
755 | }
|
---|
756 | else
|
---|
757 | {
|
---|
758 | ClusterObj c = (ClusterObj)clusterVect.elementAt(ii.val);
|
---|
759 | c.setPosition(P);
|
---|
760 | clusterVect.setElementAt(c,ii.val);
|
---|
761 | }
|
---|
762 | ii.val++;
|
---|
763 |
|
---|
764 | double new_alpha = (double)alpha/2;
|
---|
765 |
|
---|
766 | if( selected )
|
---|
767 | allSubNodeIDs.addElement(new Integer(currentClusterID) );
|
---|
768 |
|
---|
769 | if( child._items > 1 && d < 6 ){
|
---|
770 | paintPoke(g,child._child_1,x_new,y_new,center,d,theta+alpha,new_alpha,r,selected,allSubNodeIDs,ii);
|
---|
771 | paintPoke(g,child._child_2,x_new,y_new,center,d,theta+alpha,-new_alpha,r,selected, allSubNodeIDs,ii);
|
---|
772 | }
|
---|
773 |
|
---|
774 | // we have reached the outermost twigs
|
---|
775 | else{
|
---|
776 | int[] wordFreq = child.getWordFreq();
|
---|
777 | String[] words = getMostFrequentWords(wordFreq);
|
---|
778 |
|
---|
779 | Font f = new Font("Courier",Font.PLAIN,16);
|
---|
780 | FontMetrics fm = g.getFontMetrics(f);
|
---|
781 | int ascent = fm.getAscent();
|
---|
782 | int height = fm.getHeight();
|
---|
783 | int wordLength = fm.stringWidth(words[0]);
|
---|
784 | g.setFont(f);
|
---|
785 |
|
---|
786 | if( x_new > center.x ){
|
---|
787 | g.drawString(words[0],x_new + Math.max(radius,5) + 10,y_new + Math.max(radius,5));
|
---|
788 | }
|
---|
789 | else{
|
---|
790 | g.drawString(words[0],x_new - Math.max(radius,5) - 10 - wordLength,y_new + Math.max(radius,5));
|
---|
791 | }
|
---|
792 | }
|
---|
793 | }
|
---|
794 |
|
---|
795 |
|
---|
796 | public void paintTree(Cluster cc, Graphics g, int x, int y, int dist)
|
---|
797 | {
|
---|
798 | int dy = 10;
|
---|
799 | int rad = 2;
|
---|
800 | int dmt = 4;
|
---|
801 | g.setColor(Color.white);
|
---|
802 | g.fillArc(x-rad,y-rad,dmt,dmt,0,360);
|
---|
803 | g.setColor(Color.blue);
|
---|
804 | g.drawOval(x-rad,y-rad,dmt,dmt);
|
---|
805 | String desc = String.valueOf(cc._items);
|
---|
806 | g.drawString(desc,x-(int)rad/2 - (desc.length()-2)*3,y + (int)rad/2 - 2);
|
---|
807 |
|
---|
808 | if( (cc._items > 1) )
|
---|
809 | {
|
---|
810 | g.drawLine(x,y+rad,x-dist,y-rad+dy);
|
---|
811 | g.drawLine(x,y+rad,x+dist,y-rad+dy);
|
---|
812 | if( cc._child_1 != null ) paintTree(cc._child_1, g, x - dist, y + dy, (int)dist/2);
|
---|
813 | if( cc._child_2 != null ) paintTree(cc._child_2, g, x + dist, y + dy, (int)dist/2);
|
---|
814 | }
|
---|
815 | }
|
---|
816 |
|
---|
817 | public void paint(Graphics g)
|
---|
818 | {
|
---|
819 | update(g);
|
---|
820 | }
|
---|
821 |
|
---|
822 | }
|
---|