Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
3 | * of California | |
4 | * All rights reserved. | |
5 | * | |
6 | * This software is open-source under the BSD license; see either | |
7 | * "license.txt" or | |
8 | * http://jung.sourceforge.net/license.txt for a description. | |
9 | */ | |
10 | package edu.uci.ics.jung.visualization; | |
11 | ||
12 | import java.awt.Dimension; | |
13 | import java.awt.Graphics; | |
14 | import java.awt.Graphics2D; | |
15 | import java.awt.RenderingHints; | |
16 | import java.awt.event.ComponentAdapter; | |
17 | import java.awt.event.ComponentEvent; | |
18 | import java.awt.event.ItemEvent; | |
19 | import java.awt.event.ItemListener; | |
20 | import java.awt.event.MouseAdapter; | |
21 | import java.awt.event.MouseEvent; | |
22 | import java.awt.event.MouseListener; | |
23 | import java.awt.event.MouseMotionListener; | |
24 | import java.awt.event.MouseWheelEvent; | |
25 | import java.awt.event.MouseWheelListener; | |
26 | import java.awt.geom.AffineTransform; | |
27 | import java.awt.geom.Point2D; | |
28 | import java.awt.image.BufferedImage; | |
29 | import java.util.ArrayList; | |
30 | import java.util.ConcurrentModificationException; | |
31 | import java.util.HashMap; | |
32 | import java.util.Iterator; | |
33 | import java.util.List; | |
34 | import java.util.Map; | |
35 | ||
36 | import javax.swing.JPanel; | |
37 | import javax.swing.ToolTipManager; | |
38 | import javax.swing.event.ChangeEvent; | |
39 | import javax.swing.event.ChangeListener; | |
40 | import javax.swing.event.EventListenerList; | |
41 | ||
42 | import org.apache.commons.collections.Predicate; | |
43 | ||
44 | import edu.uci.ics.jung.graph.Edge; | |
45 | import edu.uci.ics.jung.graph.Vertex; | |
46 | import edu.uci.ics.jung.graph.decorators.ToolTipFunction; | |
47 | import edu.uci.ics.jung.graph.decorators.ToolTipFunctionAdapter; | |
48 | import edu.uci.ics.jung.utils.ChangeEventSupport; | |
49 | import edu.uci.ics.jung.utils.DefaultChangeEventSupport; | |
50 | import edu.uci.ics.jung.utils.Pair; | |
51 | import edu.uci.ics.jung.visualization.transform.LayoutTransformer; | |
52 | import edu.uci.ics.jung.visualization.transform.MutableAffineTransformer; | |
53 | import edu.uci.ics.jung.visualization.transform.MutableTransformer; | |
54 | import edu.uci.ics.jung.visualization.transform.Transformer; | |
55 | import edu.uci.ics.jung.visualization.transform.ViewTransformer; | |
56 | ||
57 | /** | |
58 | * A class that maintains many of the details necessary for creating | |
59 | * visualizations of graphs. | |
60 | * | |
61 | * @author Joshua O'Madadhain | |
62 | * @author Tom Nelson | |
63 | * @author Danyel Fisher | |
64 | */ | |
65 | public class VisualizationViewer extends JPanel | |
66 | implements Transformer, LayoutTransformer, ViewTransformer, | |
67 | HasGraphLayout, ChangeListener, ChangeEventSupport{ | |
68 | ||
69 | 0 | protected ChangeEventSupport changeSupport = |
70 | new DefaultChangeEventSupport(this); | |
71 | ||
72 | /** | |
73 | * holds the state of this View | |
74 | */ | |
75 | protected VisualizationModel model; | |
76 | ||
77 | /** | |
78 | * handles the actual drawing of graph elements | |
79 | */ | |
80 | protected Renderer renderer; | |
81 | ||
82 | /** should be set to user-defined class to provide | |
83 | * tooltips on the graph elements | |
84 | */ | |
85 | protected ToolTipFunction toolTipFunction; | |
86 | ||
87 | /** | |
88 | * rendering hints used in drawing. Anti-aliasing is on | |
89 | * by default | |
90 | */ | |
91 | 0 | protected Map renderingHints = new HashMap(); |
92 | ||
93 | /** | |
94 | * pluggable support for picking graph elements by | |
95 | * finding them based on their coordinates. Typically | |
96 | * used in mouse events. | |
97 | */ | |
98 | protected PickSupport pickSupport; | |
99 | ||
100 | /** | |
101 | * holds the state of which elements of the graph are | |
102 | * currently 'picked' | |
103 | */ | |
104 | protected PickedState pickedState; | |
105 | ||
106 | /** | |
107 | * a listener used to cause pick events to result in | |
108 | * repaints, even if they come from another view | |
109 | */ | |
110 | protected ItemListener pickEventListener; | |
111 | ||
112 | /** | |
113 | * an offscreen image to render the graph | |
114 | * Used if doubleBuffered is set to true | |
115 | */ | |
116 | protected BufferedImage offscreen; | |
117 | ||
118 | /** | |
119 | * graphics context for the offscreen image | |
120 | * Used if doubleBuffered is set to true | |
121 | */ | |
122 | protected Graphics2D offscreenG2d; | |
123 | ||
124 | /** | |
125 | * user-settable choice to use the offscreen image | |
126 | * or not. 'false' by default | |
127 | */ | |
128 | protected boolean doubleBuffered; | |
129 | ||
130 | /** | |
131 | * Provides support for mutating the AffineTransform that | |
132 | * is supplied to the rendering Graphics2D | |
133 | */ | |
134 | 0 | protected MutableTransformer viewTransformer = |
135 | new MutableAffineTransformer(new AffineTransform()); | |
136 | ||
137 | 0 | protected MutableTransformer layoutTransformer = |
138 | new MutableAffineTransformer(new AffineTransform()); | |
139 | ||
140 | /** | |
141 | * a collection of user-implementable functions to render under | |
142 | * the topology (before the graph is rendered) | |
143 | */ | |
144 | 0 | protected List preRenderers = new ArrayList(); |
145 | ||
146 | /** | |
147 | * a collection of user-implementable functions to render over the | |
148 | * topology (after the graph is rendered) | |
149 | */ | |
150 | 0 | protected List postRenderers = new ArrayList(); |
151 | ||
152 | /** | |
153 | * provides MouseListener, MouseMotionListener, and MouseWheelListener | |
154 | * events to the graph | |
155 | */ | |
156 | protected GraphMouse graphMouse; | |
157 | ||
158 | /** | |
159 | * if true, then when the View is resized, the current Layout | |
160 | * is resized to the same size. | |
161 | */ | |
162 | // protected boolean lockLayoutToViewSize; | |
163 | ||
164 | 0 | protected Map locationMap = new HashMap(); |
165 | ||
166 | /** | |
167 | * Create an instance with passed parameters. | |
168 | * | |
169 | * @param layout The Layout to apply, with its associated Graph | |
170 | * @param renderer The Renderer to draw it with | |
171 | */ | |
172 | public VisualizationViewer(Layout layout, Renderer renderer) { | |
173 | 0 | this(new DefaultVisualizationModel(layout), renderer); |
174 | 0 | } |
175 | ||
176 | /** | |
177 | * Create an instance with passed parameters. | |
178 | * | |
179 | * @param layout The Layout to apply, with its associated Graph | |
180 | * @param renderer The Renderer to draw it with | |
181 | * @param preferredSize the preferred size of this View | |
182 | */ | |
183 | public VisualizationViewer(Layout layout, Renderer renderer, Dimension preferredSize) { | |
184 | 0 | this(new DefaultVisualizationModel(layout, preferredSize), renderer, preferredSize); |
185 | 0 | } |
186 | ||
187 | /** | |
188 | * Create an instance with passed parameters. | |
189 | * | |
190 | * @param model | |
191 | * @param renderer | |
192 | */ | |
193 | public VisualizationViewer(VisualizationModel model, Renderer renderer) { | |
194 | 0 | this(model, renderer, new Dimension(600,600)); |
195 | 0 | } |
196 | /** | |
197 | * Create an instance with passed parameters. | |
198 | * | |
199 | * @param model | |
200 | * @param renderer | |
201 | * @param preferredSize initial preferred size of the view | |
202 | */ | |
203 | public VisualizationViewer(VisualizationModel model, Renderer renderer, | |
204 | 0 | Dimension preferredSize) { |
205 | 0 | this.model = model; |
206 | 0 | model.addChangeListener(this); |
207 | 0 | setDoubleBuffered(false); |
208 | 0 | this.addComponentListener(new VisualizationListener(this)); |
209 | ||
210 | 0 | setPickSupport(new ClassicPickSupport()); |
211 | 0 | setPickedState(new MultiPickedState()); |
212 | 0 | setRenderer(renderer); |
213 | 0 | renderer.setPickedKey(pickedState); |
214 | ||
215 | 0 | setPreferredSize(preferredSize); |
216 | 0 | renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
217 | 0 | initMouseClicker(); |
218 | 0 | scaleToLayout(model.getGraphLayout().getCurrentSize()); |
219 | 0 | this.layoutTransformer.addChangeListener(this); |
220 | 0 | this.viewTransformer.addChangeListener(this); |
221 | 0 | } |
222 | ||
223 | /** | |
224 | * set whether this class uses its offscreen image or not. If | |
225 | * true, then doubleBuffering in the superclass is set to 'false' | |
226 | */ | |
227 | public void setDoubleBuffered(boolean doubleBuffered) { | |
228 | 0 | this.doubleBuffered = doubleBuffered; |
229 | 0 | } |
230 | ||
231 | /** | |
232 | * whether this class uses double buffering. The superclass | |
233 | * will be the opposite state. | |
234 | */ | |
235 | public boolean isDoubleBuffered() { | |
236 | 0 | return doubleBuffered; |
237 | } | |
238 | ||
239 | /** | |
240 | * Ensure that, if doubleBuffering is enabled, the offscreen | |
241 | * image buffer exists and is the correct size. | |
242 | * @param d | |
243 | */ | |
244 | protected void checkOffscreenImage(Dimension d) { | |
245 | 0 | if(doubleBuffered) { |
246 | 0 | if(offscreen == null || offscreen.getWidth() != d.width || offscreen.getHeight() != d.height) { |
247 | 0 | offscreen = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); |
248 | 0 | offscreenG2d = offscreen.createGraphics(); |
249 | } | |
250 | } | |
251 | 0 | } |
252 | ||
253 | /** | |
254 | * @return Returns the model. | |
255 | */ | |
256 | public VisualizationModel getModel() { | |
257 | 0 | return model; |
258 | } | |
259 | /** | |
260 | * @param model The model to set. | |
261 | */ | |
262 | public void setModel(VisualizationModel model) { | |
263 | 0 | this.model = model; |
264 | 0 | } |
265 | /** | |
266 | * In response to changes from the model, repaint the | |
267 | * view, then fire an event to any listeners. | |
268 | * Examples of listeners are the GraphZoomScrollPane and | |
269 | * the BirdsEyeVisualizationViewer | |
270 | */ | |
271 | public void stateChanged(ChangeEvent e) { | |
272 | 0 | repaint(); |
273 | 0 | fireStateChanged(); |
274 | 0 | } |
275 | ||
276 | /** | |
277 | * Creates a default mouseClicker behavior: a default. | |
278 | * @see GraphMouseImpl | |
279 | * @deprecated replaced by setGraphMouse() | |
280 | */ | |
281 | protected void initMouseClicker() { | |
282 | // GraphMouseImpl will give original behavior | |
283 | 0 | setGraphMouse(new GraphMouseImpl() ); |
284 | 0 | } |
285 | ||
286 | /** | |
287 | * convenience pass-thru to model | |
288 | * @param scb | |
289 | */ | |
290 | public void setTextCallback(StatusCallback scb) { | |
291 | 0 | model.setTextCallback(scb); |
292 | 0 | } |
293 | ||
294 | /** | |
295 | * a setter for the GraphMouse. This will remove any | |
296 | * previous GraphMouse (including the one that | |
297 | * is added in the initMouseClicker method. | |
298 | * @param graphMouse new value | |
299 | */ | |
300 | public void setGraphMouse(GraphMouse graphMouse) { | |
301 | 0 | this.graphMouse = graphMouse; |
302 | 0 | MouseListener[] ml = getMouseListeners(); |
303 | 0 | for(int i=0; i<ml.length; i++) { |
304 | 0 | if(ml[i] instanceof GraphMouse) { |
305 | 0 | removeMouseListener(ml[i]); |
306 | } | |
307 | } | |
308 | 0 | MouseMotionListener[] mml = getMouseMotionListeners(); |
309 | 0 | for(int i=0; i<mml.length; i++) { |
310 | 0 | if(mml[i] instanceof GraphMouse) { |
311 | 0 | removeMouseMotionListener(mml[i]); |
312 | } | |
313 | } | |
314 | 0 | MouseWheelListener[] mwl = getMouseWheelListeners(); |
315 | 0 | for(int i=0; i<mwl.length; i++) { |
316 | 0 | if(mwl[i] instanceof GraphMouse) { |
317 | 0 | removeMouseWheelListener(mwl[i]); |
318 | } | |
319 | } | |
320 | 0 | addMouseListener(graphMouse); |
321 | 0 | addMouseMotionListener(graphMouse); |
322 | 0 | addMouseWheelListener(graphMouse); |
323 | 0 | } |
324 | ||
325 | /** | |
326 | * @return the current <code>GraphMouse</code> | |
327 | */ | |
328 | public GraphMouse getGraphMouse() { | |
329 | 0 | return graphMouse; |
330 | } | |
331 | ||
332 | /** | |
333 | * Sets the showing Renderer to be the input Renderer. Also | |
334 | * tells the Renderer to refer to this visualizationviewer | |
335 | * as a PickedKey. (Because Renderers maintain a small | |
336 | * amount of state, such as the PickedKey, it is important | |
337 | * to create a separate instance for each VV instance.) | |
338 | */ | |
339 | public void setRenderer(Renderer r) { | |
340 | 0 | this.renderer = r; |
341 | 0 | if(renderer instanceof PluggableRenderer) { |
342 | 0 | PluggableRenderer pr = (PluggableRenderer)renderer; |
343 | 0 | pr.setScreenDevice(this); |
344 | 0 | pr.setViewTransformer(getViewTransformer()); |
345 | ||
346 | 0 | if(pickSupport instanceof ShapePickSupport) { |
347 | 0 | ((ShapePickSupport)pickSupport).setHasShapes((HasShapeFunctions)renderer); |
348 | } | |
349 | } | |
350 | ||
351 | 0 | r.setPickedKey(pickedState); |
352 | 0 | repaint(); |
353 | 0 | } |
354 | ||
355 | /** | |
356 | * Returns the renderer used by this instance. | |
357 | */ | |
358 | public Renderer getRenderer() { | |
359 | 0 | return renderer; |
360 | } | |
361 | ||
362 | /** | |
363 | * Removes the current graph layout, and adds a new one. | |
364 | * @param layout the new layout to set | |
365 | */ | |
366 | public void setGraphLayout(Layout layout) { | |
367 | 0 | setGraphLayout(layout, true); |
368 | 0 | } |
369 | /** | |
370 | * Removes the current graph layout, and adds a new one, | |
371 | * optionally re-scaling the view to show the entire layout | |
372 | * @param layout the new layout to set | |
373 | * @param scaleToLayout whether to scale the view to show the whole layout | |
374 | */ | |
375 | public void setGraphLayout(Layout layout, boolean scaleToLayout) { | |
376 | ||
377 | 0 | Dimension viewSize = getSize(); |
378 | 0 | if(viewSize.width <= 0 || viewSize.height <= 0) { |
379 | 0 | viewSize = getPreferredSize(); |
380 | } | |
381 | 0 | model.setGraphLayout(layout, viewSize); |
382 | 0 | if(scaleToLayout) scaleToLayout(layout.getCurrentSize()); |
383 | 0 | } |
384 | ||
385 | protected void scaleToLayout(Dimension layoutSize) { | |
386 | 0 | Dimension viewSize = getSize(); |
387 | 0 | if(viewSize.width == 0 || viewSize.height == 0) { |
388 | 0 | viewSize = getPreferredSize(); |
389 | } | |
390 | 0 | float scalex = (float)viewSize.width/layoutSize.width; |
391 | 0 | float scaley = (float)viewSize.height/layoutSize.height; |
392 | 0 | float scale = 1; |
393 | 0 | if(scalex - 1 < scaley - 1) { |
394 | 0 | scale = scalex; |
395 | } else { | |
396 | 0 | scale = scaley; |
397 | } | |
398 | // set scale to show the entire graph layout | |
399 | 0 | viewTransformer.setScale(scale, scale, new Point2D.Float()); |
400 | 0 | } |
401 | ||
402 | /** | |
403 | * Returns the current graph layout. | |
404 | * Passes thru to the model | |
405 | */ | |
406 | public Layout getGraphLayout() { | |
407 | 0 | return model.getGraphLayout(); |
408 | } | |
409 | ||
410 | /** | |
411 | * This is the interface for adding a mouse listener. The GEL | |
412 | * will be called back with mouse clicks on vertices. | |
413 | * @param gel | |
414 | */ | |
415 | public void addGraphMouseListener( GraphMouseListener gel ) { | |
416 | 0 | addMouseListener( new MouseListenerTranslator( gel, this )); |
417 | 0 | } |
418 | ||
419 | /** | |
420 | * Pre-relaxes and starts a visRunner thread | |
421 | * Passes thru to the model | |
422 | */ | |
423 | public synchronized void init() { | |
424 | 0 | model.init(); |
425 | 0 | } |
426 | ||
427 | /** | |
428 | * Restarts layout, then calls init(); | |
429 | * passes thru to the model | |
430 | */ | |
431 | public synchronized void restart() { | |
432 | 0 | model.restart(); |
433 | 0 | } |
434 | ||
435 | /** | |
436 | * | |
437 | * @see javax.swing.JComponent#setVisible(boolean) | |
438 | */ | |
439 | public void setVisible(boolean aFlag) { | |
440 | 0 | super.setVisible(aFlag); |
441 | 0 | model.getGraphLayout().resize(this.getSize()); |
442 | 0 | } |
443 | ||
444 | /** | |
445 | * convenience pass-thru to the model | |
446 | */ | |
447 | public void prerelax() { | |
448 | 0 | model.prerelax(); |
449 | 0 | } |
450 | ||
451 | /** | |
452 | * convenience pass-thru to the model | |
453 | */ | |
454 | protected synchronized void start() { | |
455 | 0 | model.start(); |
456 | 0 | } |
457 | ||
458 | /** | |
459 | * convenience pass-thru to the model | |
460 | * | |
461 | */ | |
462 | public synchronized void suspend() { | |
463 | 0 | model.suspend(); |
464 | 0 | } |
465 | ||
466 | /** | |
467 | * convenience pass-thru to the model | |
468 | * | |
469 | */ | |
470 | public synchronized void unsuspend() { | |
471 | 0 | model.unsuspend(); |
472 | 0 | } |
473 | ||
474 | /** | |
475 | * @deprecated Use <code>getPickedState.isPicked(e)</code>. | |
476 | */ | |
477 | public boolean isPicked(Vertex v) { | |
478 | 0 | return pickedState.isPicked(v); |
479 | } | |
480 | ||
481 | /** | |
482 | * @deprecated Use <code>getPickedState.isPicked(e)</code>. | |
483 | */ | |
484 | public boolean isPicked(Edge e) { | |
485 | 0 | return pickedState.isPicked(e); |
486 | } | |
487 | ||
488 | /** | |
489 | * @deprecated Use <code>getPickedState.pick(picked, b)</code>. | |
490 | */ | |
491 | protected void pick(Vertex picked, boolean b) | |
492 | { | |
493 | 0 | pickedState.pick(picked, b); |
494 | 0 | } |
495 | ||
496 | 0 | long[] relaxTimes = new long[5]; |
497 | 0 | long[] paintTimes = new long[5]; |
498 | 0 | int relaxIndex = 0; |
499 | 0 | int paintIndex = 0; |
500 | double paintfps, relaxfps; | |
501 | ||
502 | /** | |
503 | * Returns a flag that says whether the visRunner thread is running. If | |
504 | * it is not, then you may need to restart the thread. | |
505 | */ | |
506 | public boolean isVisRunnerRunning() { | |
507 | 0 | return model.isVisRunnerRunning(); |
508 | } | |
509 | ||
510 | /** | |
511 | * setter for the scale | |
512 | * fires a PropertyChangeEvent with the AffineTransforms representing | |
513 | * the previous and new values for scale and offset | |
514 | * @deprecated access via getViewTransformer method | |
515 | * @param scalex | |
516 | * @param scaley | |
517 | */ | |
518 | public void scale(double scalex, double scaley) { | |
519 | 0 | scale(scalex, scaley, null); |
520 | 0 | } |
521 | /** | |
522 | * have the model scale the graph with the passed parameters. | |
523 | * If 'from' is null, use the center of this View as the | |
524 | * center to scale from | |
525 | * @deprecated access via getViewTransformer method | |
526 | * @param scalex | |
527 | * @param scaley | |
528 | * @param from | |
529 | */ | |
530 | public void scale(double scalex, double scaley, Point2D from) { | |
531 | 0 | if(from == null) { |
532 | 0 | from = getCenter(); |
533 | } | |
534 | 0 | viewTransformer.scale(scalex, scaley, from); |
535 | 0 | } |
536 | ||
537 | /** | |
538 | * have the model replace the transform scale values with the | |
539 | * passed parameters | |
540 | * @deprecated access via getViewTransformer method | |
541 | * @param scalex | |
542 | * @param scaley | |
543 | */ | |
544 | public void setScale(double scalex, double scaley) { | |
545 | 0 | setScale(scalex, scaley, null); |
546 | 0 | } |
547 | ||
548 | /** | |
549 | * Have the model replace the transform scale values with the | |
550 | * passed parameters. If 'from' is null, use this View's center | |
551 | * as the center to scale from. | |
552 | * @deprecated access via getViewTransformer method | |
553 | * @param scalex | |
554 | * @param scaley | |
555 | */ | |
556 | public void setScale(double scalex, double scaley, Point2D from) { | |
557 | 0 | viewTransformer.setScale(scalex, scaley, from); |
558 | 0 | } |
559 | ||
560 | /** | |
561 | * getter for scalex | |
562 | * @deprecated access via getViewTransformer method | |
563 | * @return scalex | |
564 | */ | |
565 | public double getScaleX() { | |
566 | 0 | return viewTransformer.getScaleX(); |
567 | } | |
568 | ||
569 | /** | |
570 | * getter for scaley | |
571 | * @deprecated access via getViewTransformer method | |
572 | */ | |
573 | public double getScaleY() { | |
574 | 0 | return viewTransformer.getScaleY(); |
575 | } | |
576 | ||
577 | /** | |
578 | * getter for offsetx | |
579 | * @deprecated use getTranslateX | |
580 | */ | |
581 | public double getOffsetX() { | |
582 | 0 | return getTranslateX(); |
583 | } | |
584 | /** | |
585 | * gets the translateX from the model | |
586 | * @deprecated access via getViewTransformer method | |
587 | * @return the translateX | |
588 | */ | |
589 | public double getTranslateX() { | |
590 | 0 | return viewTransformer.getTranslateX(); |
591 | } | |
592 | ||
593 | /** | |
594 | * getter for offsety | |
595 | * @deprecated use getTranslateY() | |
596 | */ | |
597 | public double getOffsetY() { | |
598 | 0 | return getTranslateY(); |
599 | } | |
600 | ||
601 | /** | |
602 | * gets the translateY from the model | |
603 | * @deprecated access via getViewTransformer method | |
604 | * @return the translateY | |
605 | */ | |
606 | public double getTranslateY() { | |
607 | 0 | return viewTransformer.getTranslateY(); |
608 | } | |
609 | ||
610 | /** | |
611 | * set the offset values that will be used in the | |
612 | * translation component of the graph rendering transform. | |
613 | * Changes the transform to the identity transform, then | |
614 | * sets the translation conponents to the passed values | |
615 | * Fires a PropertyChangeEvent with the AffineTransforms representing | |
616 | * the previous and new values for the transform | |
617 | * @deprecated use setTranslate(double, offset, double offset) | |
618 | * @param offsetx | |
619 | * @param offsety | |
620 | */ | |
621 | public void setOffset(double offsetx, double offsety) { | |
622 | 0 | setTranslate(offsetx, offsety); |
623 | 0 | } |
624 | ||
625 | /** | |
626 | * sets the translate x,y in the model | |
627 | * previous translate values are lost | |
628 | * @deprecated access via getViewTransformer method | |
629 | * @param tx | |
630 | * @param ty | |
631 | */ | |
632 | public void setTranslate(double tx, double ty) { | |
633 | 0 | viewTransformer.setTranslate(tx, ty); |
634 | 0 | } |
635 | ||
636 | /** | |
637 | * Translates the model's current transform by tX and ty. | |
638 | * @deprecated access via getViewTransformer method | |
639 | */ | |
640 | public void translate(double tx, double ty) { | |
641 | 0 | viewTransformer.translate(tx, ty); |
642 | 0 | } |
643 | ||
644 | /** | |
645 | * Transform the mouse point with the inverse transform | |
646 | * of the VisualizationViewer. This maps from screen coordinates | |
647 | * to graph coordinates. | |
648 | * @param p the point to transform (typically, a mouse point) | |
649 | * @return a transformed Point2D | |
650 | */ | |
651 | public Point2D inverseTransform(Point2D p) { | |
652 | 0 | return layoutTransformer.inverseTransform(inverseViewTransform(p)); |
653 | } | |
654 | ||
655 | public Point2D inverseViewTransform(Point2D p) { | |
656 | 0 | return viewTransformer.inverseTransform(p); |
657 | } | |
658 | ||
659 | public Point2D inverseLayoutTransform(Point2D p) { | |
660 | 0 | return layoutTransformer.inverseTransform(p); |
661 | } | |
662 | ||
663 | /** | |
664 | * Transform the mouse point with the current transform | |
665 | * of the VisualizationViewer. This maps from graph coordinates | |
666 | * to screen coordinates. | |
667 | * @param p the point to transform | |
668 | * @return a transformed Point2D | |
669 | */ | |
670 | public Point2D transform(Point2D p) { | |
671 | // transform with vv transform | |
672 | 0 | return viewTransformer.transform(layoutTransform(p)); |
673 | } | |
674 | ||
675 | public Point2D viewTransform(Point2D p) { | |
676 | 0 | return viewTransformer.transform(p); |
677 | } | |
678 | ||
679 | public Point2D layoutTransform(Point2D p) { | |
680 | 0 | return layoutTransformer.transform(p); |
681 | } | |
682 | ||
683 | /** | |
684 | * @param transformer The transformer to set. | |
685 | */ | |
686 | public void setViewTransformer(MutableTransformer transformer) { | |
687 | 0 | this.viewTransformer.removeChangeListener(this); |
688 | 0 | this.viewTransformer = transformer; |
689 | 0 | this.viewTransformer.addChangeListener(this); |
690 | 0 | if(renderer instanceof PluggableRenderer) { |
691 | 0 | ((PluggableRenderer)renderer).setViewTransformer(transformer); |
692 | } | |
693 | 0 | } |
694 | ||
695 | public void setLayoutTransformer(MutableTransformer transformer) { | |
696 | 0 | this.layoutTransformer.removeChangeListener(this); |
697 | 0 | this.layoutTransformer = transformer; |
698 | 0 | this.layoutTransformer.addChangeListener(this); |
699 | 0 | } |
700 | ||
701 | public MutableTransformer getViewTransformer() { | |
702 | 0 | return viewTransformer; |
703 | } | |
704 | ||
705 | public MutableTransformer getLayoutTransformer() { | |
706 | 0 | return layoutTransformer; |
707 | } | |
708 | ||
709 | /** | |
710 | * @return Returns the renderingHints. | |
711 | */ | |
712 | public Map getRenderingHints() { | |
713 | 0 | return renderingHints; |
714 | } | |
715 | /** | |
716 | * @param renderingHints The renderingHints to set. | |
717 | */ | |
718 | public void setRenderingHints(Map renderingHints) { | |
719 | 0 | this.renderingHints = renderingHints; |
720 | 0 | } |
721 | ||
722 | protected void paintComponent(Graphics g) { | |
723 | 0 | super.paintComponent(g); |
724 | ||
725 | 0 | checkOffscreenImage(getSize()); |
726 | 0 | model.start(); |
727 | ||
728 | 0 | Graphics2D g2d = (Graphics2D)g; |
729 | 0 | if(doubleBuffered) { |
730 | 0 | renderGraph(offscreenG2d); |
731 | 0 | g2d.drawImage(offscreen, null, 0, 0); |
732 | } else { | |
733 | 0 | renderGraph(g2d); |
734 | } | |
735 | 0 | } |
736 | ||
737 | protected void renderGraph(Graphics2D g2d) { | |
738 | ||
739 | 0 | Layout layout = model.getGraphLayout(); |
740 | ||
741 | 0 | g2d.setRenderingHints(renderingHints); |
742 | ||
743 | 0 | long start = System.currentTimeMillis(); |
744 | ||
745 | // the size of the VisualizationViewer | |
746 | 0 | Dimension d = getSize(); |
747 | ||
748 | // clear the offscreen image | |
749 | 0 | g2d.setColor(getBackground()); |
750 | 0 | g2d.fillRect(0,0,d.width,d.height); |
751 | ||
752 | 0 | AffineTransform oldXform = g2d.getTransform(); |
753 | 0 | AffineTransform newXform = new AffineTransform(oldXform); |
754 | 0 | newXform.concatenate(viewTransformer.getTransform()); |
755 | ||
756 | 0 | g2d.setTransform(newXform); |
757 | ||
758 | // if there are preRenderers set, paint them | |
759 | 0 | for(Iterator iterator=preRenderers.iterator(); iterator.hasNext(); ) { |
760 | 0 | Paintable paintable = (Paintable)iterator.next(); |
761 | 0 | if(paintable.useTransform()) { |
762 | 0 | paintable.paint(g2d); |
763 | } else { | |
764 | 0 | g2d.setTransform(oldXform); |
765 | 0 | paintable.paint(g2d); |
766 | 0 | g2d.setTransform(newXform); |
767 | } | |
768 | } | |
769 | ||
770 | 0 | locationMap.clear(); |
771 | ||
772 | // paint all the edges | |
773 | try { | |
774 | 0 | for (Iterator iter = layout.getGraph().getEdges().iterator(); |
775 | 0 | iter.hasNext(); |
776 | ) { | |
777 | 0 | Edge e = (Edge) iter.next(); |
778 | 0 | Vertex v1 = (Vertex) e.getEndpoints().getFirst(); |
779 | 0 | Vertex v2 = (Vertex) e.getEndpoints().getSecond(); |
780 | ||
781 | 0 | Point2D p = (Point2D) locationMap.get(v1); |
782 | 0 | if(p == null) { |
783 | ||
784 | 0 | p = layout.getLocation(v1); |
785 | 0 | p = layoutTransformer.transform(p); |
786 | 0 | locationMap.put(v1, p); |
787 | } | |
788 | 0 | Point2D q = (Point2D) locationMap.get(v2); |
789 | 0 | if(q == null) { |
790 | 0 | q = layout.getLocation(v2); |
791 | 0 | q = layoutTransformer.transform(q); |
792 | 0 | locationMap.put(v2, q); |
793 | } | |
794 | ||
795 | 0 | if(p != null && q != null) { |
796 | 0 | renderer.paintEdge( |
797 | g2d, | |
798 | e, | |
799 | (int) p.getX(), | |
800 | (int) p.getY(), | |
801 | (int) q.getX(), | |
802 | (int) q.getY()); | |
803 | } | |
804 | } | |
805 | 0 | } catch(ConcurrentModificationException cme) { |
806 | 0 | repaint(); |
807 | 0 | } |
808 | ||
809 | // paint all the vertices | |
810 | try { | |
811 | 0 | for (Iterator iter = layout.getGraph().getVertices().iterator(); |
812 | 0 | iter.hasNext(); |
813 | ) { | |
814 | ||
815 | 0 | Vertex v = (Vertex) iter.next(); |
816 | 0 | Point2D p = (Point2D) locationMap.get(v); |
817 | 0 | if(p == null) { |
818 | 0 | p = layout.getLocation(v); |
819 | 0 | p = layoutTransformer.transform(p); |
820 | 0 | locationMap.put(v, p); |
821 | } | |
822 | 0 | if(p != null) { |
823 | 0 | renderer.paintVertex( |
824 | g2d, | |
825 | v, | |
826 | (int) p.getX(), | |
827 | (int) p.getY()); | |
828 | } | |
829 | } | |
830 | 0 | } catch(ConcurrentModificationException cme) { |
831 | 0 | repaint(); |
832 | 0 | } |
833 | ||
834 | 0 | long delta = System.currentTimeMillis() - start; |
835 | 0 | paintTimes[paintIndex++] = delta; |
836 | 0 | paintIndex = paintIndex % paintTimes.length; |
837 | 0 | paintfps = average(paintTimes); |
838 | ||
839 | // if there are postRenderers set, do it | |
840 | 0 | for(Iterator iterator=postRenderers.iterator(); iterator.hasNext(); ) { |
841 | 0 | Paintable paintable = (Paintable)iterator.next(); |
842 | 0 | if(paintable.useTransform()) { |
843 | 0 | paintable.paint(g2d); |
844 | } else { | |
845 | 0 | g2d.setTransform(oldXform); |
846 | 0 | paintable.paint(g2d); |
847 | 0 | g2d.setTransform(newXform); |
848 | } | |
849 | } | |
850 | 0 | g2d.setTransform(oldXform); |
851 | 0 | } |
852 | ||
853 | /** | |
854 | * Returns the double average of a number of long values. | |
855 | * @param paintTimes an array of longs | |
856 | * @return the average of the doubles | |
857 | */ | |
858 | protected double average(long[] paintTimes) { | |
859 | 0 | double l = 0; |
860 | 0 | for (int i = 0; i < paintTimes.length; i++) { |
861 | 0 | l += paintTimes[i]; |
862 | } | |
863 | 0 | return l / paintTimes.length; |
864 | } | |
865 | ||
866 | /** | |
867 | * VisualizationListener reacts to changes in the size of the | |
868 | * VisualizationViewer. When the size changes, it ensures | |
869 | * that the offscreen image is sized properly. | |
870 | * If the layout is locked to this view size, then the layout | |
871 | * is also resized to be the same as the view size. | |
872 | * | |
873 | * | |
874 | */ | |
875 | protected class VisualizationListener extends ComponentAdapter { | |
876 | protected VisualizationViewer vv; | |
877 | public VisualizationListener(VisualizationViewer vv) { | |
878 | this.vv = vv; | |
879 | } | |
880 | ||
881 | /** | |
882 | * create a new offscreen image for the graph | |
883 | * whenever the window is resied | |
884 | */ | |
885 | public void componentResized(ComponentEvent e) { | |
886 | Dimension d = vv.getSize(); | |
887 | if(d.width <= 0 || d.height <= 0) return; | |
888 | checkOffscreenImage(d); | |
889 | // if(getLockLayoutToViewSize()) { | |
890 | // model.resizeLayout(vv.getSize()); | |
891 | // } | |
892 | repaint(); | |
893 | } | |
894 | } | |
895 | ||
896 | /** | |
897 | * convenience pass-thru to model | |
898 | */ | |
899 | public synchronized void stop() { | |
900 | 0 | model.stop(); |
901 | 0 | } |
902 | ||
903 | /** | |
904 | * sets the tooltip listener to the user's defined implementation | |
905 | * of ToolTipListener | |
906 | * @param listener the listener to ser | |
907 | */ | |
908 | public void setToolTipListener(ToolTipListener listener) { | |
909 | 0 | if(listener instanceof ToolTipFunction) { |
910 | 0 | setToolTipFunction((ToolTipFunction)listener); |
911 | } else { | |
912 | 0 | setToolTipFunction(new ToolTipListenerWrapper(listener)); |
913 | } | |
914 | 0 | } |
915 | ||
916 | public void setToolTipFunction(ToolTipFunction toolTipFunction) { | |
917 | 0 | this.toolTipFunction = toolTipFunction; |
918 | 0 | ToolTipManager.sharedInstance().registerComponent(this); |
919 | 0 | } |
920 | /** | |
921 | * called by the superclass to display tooltips | |
922 | */ | |
923 | public String getToolTipText(MouseEvent event) { | |
924 | 0 | if(toolTipFunction != null) { |
925 | 0 | if(toolTipFunction instanceof ToolTipListenerWrapper) { |
926 | 0 | return toolTipFunction.getToolTipText(event); |
927 | } | |
928 | 0 | Point2D p = inverseViewTransform(event.getPoint()); |
929 | 0 | Vertex vertex = pickSupport.getVertex(p.getX(), p.getY()); |
930 | 0 | if(vertex != null && willRender(vertex)) { |
931 | 0 | return toolTipFunction.getToolTipText(vertex); |
932 | } | |
933 | 0 | Edge edge = pickSupport.getEdge(p.getX(), p.getY()); |
934 | 0 | if(edge != null && willRender(edge)) { |
935 | 0 | return toolTipFunction.getToolTipText(edge); |
936 | } | |
937 | 0 | return toolTipFunction.getToolTipText(event); |
938 | } | |
939 | 0 | return super.getToolTipText(event); |
940 | } | |
941 | ||
942 | private boolean willRender(Vertex v) { | |
943 | 0 | if(renderer instanceof PluggableRenderer) { |
944 | 0 | Predicate vip = ((PluggableRenderer)renderer).getVertexIncludePredicate(); |
945 | 0 | return vip == null || vip.evaluate(v) == true; |
946 | } | |
947 | 0 | return true; |
948 | } | |
949 | ||
950 | private boolean willRender(Edge e) { | |
951 | 0 | if(renderer instanceof PluggableRenderer) { |
952 | 0 | Predicate eip = ((PluggableRenderer)renderer).getEdgeIncludePredicate(); |
953 | 0 | Pair endpoints = e.getEndpoints(); |
954 | 0 | boolean edgeAnswer = eip == null || eip.evaluate(e); |
955 | 0 | boolean endpointAnswer = willRender((Vertex)endpoints.getFirst()) && |
956 | willRender((Vertex)endpoints.getSecond()); | |
957 | 0 | return edgeAnswer && endpointAnswer; |
958 | } | |
959 | 0 | return true; |
960 | } | |
961 | ||
962 | /** | |
963 | * The interface for the tool tip listener. Implement this | |
964 | * interface to add custom tool tip to the graph elements. | |
965 | * See sample code for examples | |
966 | */ | |
967 | public interface ToolTipListener { | |
968 | String getToolTipText(MouseEvent event); | |
969 | } | |
970 | ||
971 | /** | |
972 | * used internally to wrap any legacy ToolTipListener | |
973 | * implementations so they can be used as a ToolTipFunction | |
974 | * @author Tom Nelson - RABA Technologies | |
975 | * | |
976 | * | |
977 | */ | |
978 | protected static class ToolTipListenerWrapper extends ToolTipFunctionAdapter { | |
979 | ToolTipListener listener; | |
980 | public ToolTipListenerWrapper(ToolTipListener listener) { | |
981 | this.listener = listener; | |
982 | } | |
983 | public String getToolTipText(MouseEvent e) { | |
984 | return listener.getToolTipText(e); | |
985 | } | |
986 | } | |
987 | ||
988 | /** | |
989 | * an interface for the preRender and postRender | |
990 | */ | |
991 | public interface Paintable { | |
992 | public void paint(Graphics g); | |
993 | public boolean useTransform(); | |
994 | } | |
995 | ||
996 | /** | |
997 | * a convenience type to represent a class that | |
998 | * processes all types of mouse events for the graph | |
999 | */ | |
1000 | public interface GraphMouse extends MouseListener, MouseMotionListener, MouseWheelListener {} | |
1001 | ||
1002 | /** | |
1003 | * this is the original GraphMouse class, renamed to use GraphMouse as the interface name, | |
1004 | * and updated to correctly apply the vv transform to the point point | |
1005 | * | |
1006 | */ | |
1007 | protected final class GraphMouseImpl extends MouseAdapter implements GraphMouse { | |
1008 | protected Vertex picked; | |
1009 | ||
1010 | public void mousePressed(MouseEvent e) { | |
1011 | ||
1012 | Point2D p = inverseViewTransform(e.getPoint()); | |
1013 | ||
1014 | Vertex v = pickSupport.getVertex(p.getX(), p.getY()); | |
1015 | if (v == null) { | |
1016 | return; | |
1017 | } | |
1018 | picked = v; | |
1019 | pick(picked, true); | |
1020 | model.getGraphLayout().forceMove(picked, p.getX(), p.getY()); | |
1021 | repaint(); | |
1022 | } | |
1023 | public void mouseReleased(MouseEvent e) { | |
1024 | if (picked == null) | |
1025 | return; | |
1026 | pick(picked, false); | |
1027 | picked = null; | |
1028 | repaint(); | |
1029 | } | |
1030 | public void mouseDragged(MouseEvent e) { | |
1031 | if (picked == null) | |
1032 | return; | |
1033 | Point2D p = inverseViewTransform(e.getPoint()); | |
1034 | ||
1035 | model.getGraphLayout().forceMove(picked, p.getX(), p.getY()); | |
1036 | repaint(); | |
1037 | } | |
1038 | ||
1039 | public void mouseMoved(MouseEvent e) { | |
1040 | return; | |
1041 | } | |
1042 | /** | |
1043 | * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent) | |
1044 | */ | |
1045 | public void mouseWheelMoved(MouseWheelEvent e) { | |
1046 | return; | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | /** | |
1051 | * @param paintable The paintable to add. | |
1052 | */ | |
1053 | public void addPreRenderPaintable(Paintable paintable) { | |
1054 | 0 | if(preRenderers == null) { |
1055 | 0 | preRenderers = new ArrayList(); |
1056 | } | |
1057 | 0 | preRenderers.add(paintable); |
1058 | 0 | } |
1059 | ||
1060 | /** | |
1061 | * @param paintable The paintable to remove. | |
1062 | */ | |
1063 | public void removePreRenderPaintable(Paintable paintable) { | |
1064 | 0 | if(preRenderers != null) { |
1065 | 0 | preRenderers.remove(paintable); |
1066 | } | |
1067 | 0 | } |
1068 | ||
1069 | /** | |
1070 | * @param paintable The paintable to add. | |
1071 | */ | |
1072 | public void addPostRenderPaintable(Paintable paintable) { | |
1073 | 0 | if(postRenderers == null) { |
1074 | 0 | postRenderers = new ArrayList(); |
1075 | } | |
1076 | 0 | postRenderers.add(paintable); |
1077 | 0 | } |
1078 | ||
1079 | /** | |
1080 | * @param paintable The paintable to remove. | |
1081 | */ | |
1082 | public void removePostRenderPaintable(Paintable paintable) { | |
1083 | 0 | if(postRenderers != null) { |
1084 | 0 | postRenderers.remove(paintable); |
1085 | } | |
1086 | 0 | } |
1087 | ||
1088 | /** | |
1089 | * Adds a <code>ChangeListener</code>. | |
1090 | * @param l the listener to be added | |
1091 | */ | |
1092 | public void addChangeListener(ChangeListener l) { | |
1093 | 0 | changeSupport.addChangeListener(l); |
1094 | 0 | } |
1095 | ||
1096 | /** | |
1097 | * Removes a ChangeListener. | |
1098 | * @param l the listener to be removed | |
1099 | */ | |
1100 | public void removeChangeListener(ChangeListener l) { | |
1101 | 0 | changeSupport.removeChangeListener(l); |
1102 | 0 | } |
1103 | ||
1104 | /** | |
1105 | * Returns an array of all the <code>ChangeListener</code>s added | |
1106 | * with addChangeListener(). | |
1107 | * | |
1108 | * @return all of the <code>ChangeListener</code>s added or an empty | |
1109 | * array if no listeners have been added | |
1110 | */ | |
1111 | public ChangeListener[] getChangeListeners() { | |
1112 | 0 | return changeSupport.getChangeListeners(); |
1113 | } | |
1114 | ||
1115 | /** | |
1116 | * Notifies all listeners that have registered interest for | |
1117 | * notification on this event type. The event instance | |
1118 | * is lazily created. | |
1119 | * @see EventListenerList | |
1120 | */ | |
1121 | public void fireStateChanged() { | |
1122 | 0 | changeSupport.fireStateChanged(); |
1123 | 0 | } |
1124 | ||
1125 | /** | |
1126 | * @return Returns the pickedState. | |
1127 | */ | |
1128 | public PickedState getPickedState() { | |
1129 | 0 | return pickedState; |
1130 | } | |
1131 | /** | |
1132 | * @param pickedState The pickedState to set. | |
1133 | */ | |
1134 | public void setPickedState(PickedState pickedState) { | |
1135 | 0 | if(pickEventListener != null && this.pickedState != null) { |
1136 | 0 | this.pickedState.removeItemListener(pickEventListener); |
1137 | } | |
1138 | 0 | this.pickedState = pickedState; |
1139 | 0 | if(renderer != null) { |
1140 | 0 | renderer.setPickedKey(pickedState); |
1141 | } | |
1142 | 0 | if(pickEventListener == null) { |
1143 | 0 | pickEventListener = new ItemListener() { |
1144 | ||
1145 | public void itemStateChanged(ItemEvent e) { | |
1146 | repaint(); | |
1147 | } | |
1148 | }; | |
1149 | } | |
1150 | 0 | pickedState.addItemListener(pickEventListener); |
1151 | 0 | } |
1152 | ||
1153 | /** | |
1154 | * @return Returns the pickSupport. | |
1155 | */ | |
1156 | public PickSupport getPickSupport() { | |
1157 | 0 | return pickSupport; |
1158 | } | |
1159 | /** | |
1160 | * @param pickSupport The pickSupport to set. | |
1161 | */ | |
1162 | public void setPickSupport(PickSupport pickSupport) { | |
1163 | 0 | this.pickSupport = pickSupport; |
1164 | 0 | this.pickSupport.setHasGraphLayout(this); |
1165 | 0 | if(pickSupport instanceof ShapePickSupport && renderer instanceof HasShapeFunctions) { |
1166 | 0 | ((ShapePickSupport)pickSupport).setHasShapes((HasShapeFunctions)renderer); |
1167 | 0 | ((ShapePickSupport)pickSupport).setLayoutTransformer(this); |
1168 | } | |
1169 | 0 | } |
1170 | ||
1171 | public Point2D getCenter() { | |
1172 | 0 | Dimension d = getSize(); |
1173 | 0 | return new Point2D.Float(d.width/2, d.height/2); |
1174 | } | |
1175 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |