source: trunk/greenstone3-extensions/vishnu/src/vishnu/testvis/treemap/visual/TreemapPanel.java@ 8189

Last change on this file since 8189 was 8189, checked in by kjdon, 20 years ago

first version of Imperial College's Visualiser code

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 KB
Line 
1/**
2* name TreemapPanel.java
3* author Peter Au
4* modified Daniel Heesch (29.04.02)
5*/
6
7package vishnu.testvis.treemap.visual;
8
9import vishnu.testvis.object.DataManager;
10import vishnu.testvis.treemap.rectangle.ClusterRect;
11import vishnu.testvis.visual.*;
12import vishnu.testvis.object.*;
13import java.util.Stack;
14import java.awt.*;
15import java.awt.event.*;
16import javax.swing.*;
17import java.util.*;
18
19public class TreemapPanel extends Canvas
20{
21
22 private int _width, _height;
23 private JButton _popUpButton;
24 private NavigatorPanel _navigator;
25 private String _newTerms;
26 Vishnu _displayFrame;
27 Image _offScreenImage=null;
28 boolean _init_done=false, _drillDownFlag=false, _newSearchFlag=false, _popUpFlag=false;
29 Stack _visualObjs;
30
31 JPopupMenu[] popups;
32 PopupMenu popup;
33
34 public TreemapPanel(int w, int h, JButton bb, NavigatorPanel n, Vishnu displayFrame)
35 {
36 _displayFrame = displayFrame;
37 _width=w; _height=h;
38 _popUpButton = bb;
39 _navigator = n;
40 _displayFrame = displayFrame;
41 setSize(_width,_height);
42 _visualObjs = new Stack();
43 addMouseListener(new MouseHandler());
44 _popUpButton.setActionCommand("popUp");
45 _popUpButton.setEnabled(false);
46 _popUpButton.setToolTipText("Go back to the previous level");
47 _popUpButton.addActionListener(new ButtonHandler());
48
49 popup = new PopupMenu();
50
51 add(popup);
52
53 }
54
55 class MouseHandler extends MouseAdapter{}
56
57 class ButtonHandler implements ActionListener
58 {
59 public void actionPerformed(ActionEvent event)
60 {
61 String action = event.getActionCommand();
62 String st;
63 if (action.equals("popUp")){
64 processPopUp();
65 }
66 }
67 }
68
69 public void update(Graphics oldg)
70 {
71 Graphics2D g = (Graphics2D) oldg;
72 Visualiser currentVObj;
73
74 if (_newSearchFlag)
75 {
76 _visualObjs.push(new Visualiser(_displayFrame.dataManager, 0F,0F, _width, _height));
77 currentVObj = ((Visualiser) _visualObjs.peek());
78 currentVObj.createTreemapView(createImage(_width, _height));
79
80 //-- draw the treemap view on the offscreen-buffer
81 currentVObj.drawTreemapView(_offScreenImage, this);
82
83 _newSearchFlag = false;
84 }
85 else if (_drillDownFlag)
86 {
87 _displayFrame.refine = true;
88 currentVObj = (Visualiser) _visualObjs.peek();
89
90 //-- create a list of image buffer for the animated pictures
91 Image iBuffer[]=new Image[Visualiser.__FRAME];
92
93 for(int i=0;i<iBuffer.length;i++)
94 iBuffer[i]=createImage(_width,_height);
95
96 //-- prepare the zooming pictures of the current visaul object
97 currentVObj.prepare_zooming(iBuffer, this);
98
99 int s = _visualObjs.size();
100 float x = Visualiser.__borderwidth*s,
101 y = Visualiser.__borderheight*s;
102
103 DataManager dm = new DataManager(_displayFrame);
104 dm.query(_newTerms);
105 _visualObjs.push(new Visualiser(dm,x, y, _width-x*2, _height-y*2));
106
107 /**
108 * create a treemap view for the new data
109 * before doing so, we have to copy the last zooming picture to the treemap
110 * this is because the zooming picture contains the borders
111 */
112
113 Image tmpImg = createImage(_width, _height);
114 Graphics2D tmpGph = (Graphics2D) tmpImg.getGraphics();
115 tmpGph.drawImage(currentVObj.getLastZoomingImage(),0,0,this);
116
117 //-- now create the treemap view
118 ((Visualiser) _visualObjs.peek()).createTreemapView(tmpImg);
119
120 //-- draw the treemap view (top element of the stack) on the offscreen buffer
121 ((Visualiser) _visualObjs.peek()).drawTreemapView(_offScreenImage, this);
122
123 //-- when everyting is done, we then show the animated pictures
124 //-- we use the 2nd element of the stack, not the top one
125 currentVObj.show_zoomIn(g,this);
126
127 _drillDownFlag = false;
128 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
129 }
130 else if (_popUpFlag)
131 {
132
133 currentVObj = (Visualiser) _visualObjs.peek();
134 //-- redraw the treemap view on the offscreen buffer, as the previous
135 //-- level is gone
136 currentVObj.drawTreemapView(_offScreenImage, this);
137 //-- show the animated pictures
138 currentVObj.show_zoomOut(g,this);
139 _popUpFlag = false;
140 }
141 paint(g);
142 }
143
144
145 public void clearDisplay()
146 {
147 _navigator.clear();
148 }
149
150
151 public void processPopUp()
152 {
153 if (_visualObjs.size() > 1)
154 {
155 _displayFrame.log.log("UNREFINE_QUERY");
156 //-- get rid of the top obj in the stack
157 _visualObjs.pop();
158 //-- reset the necessary instance variable back to the default values
159 ((Visualiser) _visualObjs.peek()).setDefault();
160 //-- clear the table and display
161 clearDisplay();
162 if (_visualObjs.size()==1) _popUpButton.setEnabled(false);
163 _popUpFlag = true;
164 repaint();
165 }
166 }
167
168 public void newSearch()
169 {
170 clearDisplay();
171 _popUpButton.setEnabled(false);
172 _visualObjs = new Stack();
173 _newSearchFlag = true;
174 repaint();
175 }
176
177
178 private void processDrillDown(int x, int y){
179 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
180 ClusterRect r = ((Visualiser) _visualObjs.peek()).drillDown(x,y);
181 //-- chose which cluster to drill down
182 if (r!=null){
183 //-- do sth on 'r'??? send it back to the server and do the clustering again
184 _popUpButton.setEnabled(true);
185 clearDisplay();
186 _drillDownFlag = true;
187 _newTerms = r.getClusterObj().getLabel();
188 _navigator.clear(); //-- clear the table and the display
189 repaint();
190
191 }
192 else
193 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
194 }
195
196
197
198 private void processInspect(int x, int y){
199 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
200 ClusterRect r = ((Visualiser) _visualObjs.peek()).inspect(x,y);
201 if (r!=null){
202 //-- draw a corner on the selected cluster
203 ((Visualiser) _visualObjs.peek()).drawCorner(r, _offScreenImage,
204 this, x, y, _width, _height);
205 //-- setup the data model in the table
206 _navigator.setData(((Visualiser) _visualObjs.peek()).getDataManager().getWords(),
207 r.getClusterObj());
208
209 // and show all documents!
210 _navigator.showDocuments();
211
212 repaint();
213 }
214 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
215 }
216
217
218 protected void processMouseEvent(MouseEvent e)
219 {
220 if (e.getID()==MouseEvent.MOUSE_CLICKED)
221 {
222
223 if (!_visualObjs.empty())
224 {
225 Font weak = new Font("Arial",Font.PLAIN,14);
226 Font bold = new Font("Arial",Font.BOLD,14);
227
228 final ClusterRect r = ((Visualiser) _visualObjs.peek()).inspect(e.getX(),e.getY());
229 ClusterObj cObj = r.getClusterObj();
230 int numDocs = cObj.getDocsSize();
231
232 boolean img_hasChanged;
233
234 popup.removeAll();
235 MenuItem menuItem = new MenuItem(numDocs + " documents");
236 menuItem.setFont(bold);
237
238 menuItem.addActionListener(new ActionListener()
239 {
240 public void actionPerformed(ActionEvent actionEvent)
241 {
242
243 if (r!=null)
244 {
245
246 //-- draw a corner on the selected cluster
247 // ((Visualiser) _visualObjs.peek()).drawCorner(r, _offScreenImage,
248 // this, x, y, _width, _height);
249 //-- setup the data model in the table
250
251 String str = r.getClusterObj().getLabel();
252 _displayFrame.log.log("SELECTED_CLUSTER",str);
253 _navigator.setData(((Visualiser) _visualObjs.peek()).getDataManager().getWords(),
254 r.getClusterObj());
255
256 // and show all documents!
257 _navigator.showDocuments();
258
259 repaint();
260 }
261
262 }
263 });
264
265 popup.add(menuItem);
266 popup.addSeparator();
267
268 menuItem = new MenuItem("Refined Query");
269 menuItem.setFont(bold);
270 menuItem.addActionListener(new ActionListener()
271 {
272 public void actionPerformed(ActionEvent actionEvent)
273 {
274 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
275
276 // all this does is return the rectangle, on which drill down
277 // is to be performed
278 //ClusterRect r = ((Visualiser) _visualObjs.peek()).drillDown(x,y);
279 //-- chose which cluster to drill down
280 if (r!=null)
281 {
282 //-- do sth on 'r'??? send it back to the server and do the clustering again
283 _popUpButton.setEnabled(true);
284 clearDisplay();
285 _drillDownFlag = true;
286 _newTerms = r.getClusterObj().getLabel();
287 _navigator.clear(); //-- clear the table and the display
288 repaint();
289 }
290 else
291 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
292 }
293 });
294
295 popup.add(menuItem);
296 popup.addSeparator();
297 String label = cObj.getLabel();
298
299 StringTokenizer st = new StringTokenizer(label);
300
301 while( st.hasMoreTokens() )
302 {
303 menuItem = new MenuItem(st.nextToken());
304 menuItem.setFont(bold);
305 menuItem.setEnabled(false);
306 popup.add(menuItem);
307 }
308
309 popup.show(this,e.getX(),e.getY());
310
311 /*img_hasChanged = ((Visualiser) _visualObjs.peek()).drawTipBox(
312 _offScreenImage, this, e.getX(),e.getY(), _width,_height);
313 System.out.println("Changed?: " + img_hasChanged);
314 if (img_hasChanged)*/ repaint();
315 }
316
317 } // if mouseclick
318 }
319
320
321 //------------------------------------------------------------------
322
323 public void paint(Graphics oldg)
324 {
325 //System.out.println("Tree map paint called");
326 Graphics2D tmpGph, g = (Graphics2D) oldg;
327 Image tmpImg;
328 /**
329 * create off-scrren images for double-buffering
330 * There is a known deficiency bug (Java itself) with createImage in java applications.
331 * Workaround: to create necessary image buffers inside paint!
332 */
333 if (!_init_done){
334 _offScreenImage = createImage(_width, _height);
335 _init_done = true;
336 }
337
338 /*if(this.isVisible())*/ g.drawImage(_offScreenImage,0,0,this);
339
340 } //-- paint
341
342
343
344} //-- TreemapPanel
345
346
347 /*
348 addMouseMotionListener( new MouseMotionAdapter()
349 {
350 public void mouseMoved(MouseEvent e)
351 {
352 if (!_visualObjs.empty())
353 {
354 final ClusterRect r = ((Visualiser) _visualObjs.peek()).inspect(e.getX(),e.getY());
355 ClusterObj cObj = r.getClusterObj();
356 int numDocs = cObj.getDocsSize();
357 System.out.print("Mouse moved to ");
358 System.out.println(e.getX() + " " + e.getY());
359
360 boolean img_hasChanged;
361
362 popup.removeAll();
363 MenuItem menuItem = new MenuItem(numDocs + " documents");
364 menuItem.addActionListener(new ActionListener()
365 {
366 public void actionPerformed(ActionEvent actionEvent)
367 {
368 System.out.println("Show documents");
369
370 if (r!=null)
371 {
372 //-- draw a corner on the selected cluster
373 //((Visualiser) _visualObjs.peek()).drawCorner(r, _offScreenImage,
374 // this, x, y, _width, _height);
375 //-- setup the data model in the table
376 _navigator.setData(((Visualiser) _visualObjs.peek()).getDataManager().getWords(),
377 r.getClusterObj());
378
379 // and show all documents!
380 _navigator.showDocuments();
381
382 repaint();
383 }
384
385 }
386 });
387
388 popup.add(menuItem);
389
390 menuItem = new MenuItem("Drill down");
391 menuItem.addActionListener(new ActionListener()
392 {
393 public void actionPerformed(ActionEvent actionEvent)
394 {
395 System.out.println("Drill down clicked");
396 }
397 });
398
399 popup.add(menuItem);
400
401 popup.show(this,e.getX(),e.getY());
402
403 img_hasChanged = ((Visualiser) _visualObjs.peek()).drawTipBox(
404 _offScreenImage, this, e.getX(),e.getY(), _width,_height);
405 System.out.println("Changed?: " + img_hasChanged);
406 if (img_hasChanged) repaint();
407 }
408 }
409 });*/
410
411 /* protected void processMouseEvent(MouseEvent e)
412 {
413
414 if (!_visualObjs.empty())
415 {
416 //-- if the mouse points outside the obj, then copy the original treemap view
417 //-- without the tipbox to the screen
418 if (e.getID()==MouseEvent.MOUSE_EXITED){
419 //-- get the most update view of the treemap
420 ((Visualiser) _visualObjs.peek()).drawTreemapView(_offScreenImage, this);
421 repaint();
422 }
423 else if (e.getID()==MouseEvent.MOUSE_CLICKED){
424
425 //-- if the user pressed the first mouse button
426 if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK)
427 {
428 System.out.println("Mouse clicked");
429 processInspect(e.getX(),e.getY());
430
431 }
432 //-- if the user pressed the third mouse button
433 else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
434 processDrillDown(e.getX(),e.getY());
435
436 }
437 }
438
439 }*/
440
441 //------------------------------------------------------------------
442/* protected void processMouseMotionEvent(MouseEvent e)
443 {
444 System.out.println("Outer Mouse in motion");
445 if (!_visualObjs.empty())
446 {
447 Font weak = new Font("Arial",Font.PLAIN,14);
448 Font bold = new Font("Arial",Font.BOLD,14);
449
450 final ClusterRect r = ((Visualiser) _visualObjs.peek()).inspect(e.getX(),e.getY());
451 ClusterObj cObj = r.getClusterObj();
452 int numDocs = cObj.getDocsSize();
453
454 System.out.println("Inner Mouse in motion");
455 System.out.println(e.getX() + " " + e.getY());
456 //System.out.println("Mouse in motion");
457 boolean img_hasChanged;
458
459 popup.removeAll();
460 MenuItem menuItem = new MenuItem(numDocs + " documents");
461 menuItem.setFont(bold);
462
463 menuItem.addActionListener(new ActionListener()
464 {
465 public void actionPerformed(ActionEvent actionEvent)
466 {
467 System.out.println("Show documents");
468
469 if (r!=null)
470 {
471 //-- draw a corner on the selected cluster
472 // ((Visualiser) _visualObjs.peek()).drawCorner(r, _offScreenImage,
473 // this, x, y, _width, _height);
474 //-- setup the data model in the table
475 _navigator.setData(((Visualiser) _visualObjs.peek()).getDataManager().getWords(),
476 r.getClusterObj());
477
478 // and show all documents!
479 _navigator.showDocuments();
480
481 repaint();
482 }
483
484 }
485 });
486
487 popup.add(menuItem);
488 popup.addSeparator();
489
490 menuItem = new MenuItem("Refined Query");
491 menuItem.setFont(bold);
492 menuItem.addActionListener(new ActionListener()
493 {
494 public void actionPerformed(ActionEvent actionEvent)
495 {
496 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
497
498 // all this does is return the rectangle, on which drill down
499 // is to be performed
500 //ClusterRect r = ((Visualiser) _visualObjs.peek()).drillDown(x,y);
501 //-- chose which cluster to drill down
502 if (r!=null)
503 {
504 //-- do sth on 'r'??? send it back to the server and do the clustering again
505 _popUpButton.setEnabled(true);
506 clearDisplay();
507 _drillDownFlag = true;
508 _newTerms = r.getClusterObj().getLabel();
509 _navigator.clear(); //-- clear the table and the display
510 repaint();
511 }
512 else
513 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
514 }
515 });
516
517 popup.add(menuItem);
518 popup.addSeparator();
519 String label = cObj.getLabel();
520
521 StringTokenizer st = new StringTokenizer(label);
522
523 while( st.hasMoreTokens() )
524 {
525 menuItem = new MenuItem(st.nextToken());
526 menuItem.setFont(bold);
527 menuItem.setEnabled(false);
528 popup.add(menuItem);
529 }
530
531 popup.show(this,e.getX(),e.getY());
532
533 //img_hasChanged = ((Visualiser) _visualObjs.peek()).drawTipBox(
534 // _offScreenImage, this, e.getX(),e.getY(), _width,_height);
535 //System.out.println("Changed?: " + img_hasChanged);
536 //if (img_hasChanged)
537 repaint();
538 }
539 }*/
Note: See TracBrowser for help on using the repository browser.