View Javadoc

1   /*
2    * Copyright (c) 2005, 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    * Created on Mar 8, 2005
10   *
11   */
12  package edu.uci.ics.jung.visualization.control;
13  
14  import java.awt.event.InputEvent;
15  import java.awt.event.MouseEvent;
16  import java.awt.event.MouseListener;
17  import java.awt.event.MouseMotionListener;
18  import java.awt.geom.Point2D;
19  
20  import edu.uci.ics.jung.algorithms.layout.Layout;
21  import edu.uci.ics.jung.visualization.Layer;
22  import edu.uci.ics.jung.visualization.VisualizationViewer;
23  
24  /** 
25   * A version of the AnimatedPickingGraphMousePlugin that is for
26   * the SatelliteVisualizationViewer. The difference it that when
27   * you pick a Vertex in the Satellite View, the 'master view' is
28   * translated to move that Vertex to the center.
29   * @see AnimatedPickingGraphMousePlugin
30   * @author Tom Nelson
31   */
32  public class SatelliteAnimatedPickingGraphMousePlugin<V,E> extends AnimatedPickingGraphMousePlugin<V,E>
33      implements MouseListener, MouseMotionListener {
34  
35      /**
36  	 * create an instance 
37  	 * 
38  	 */
39  	public SatelliteAnimatedPickingGraphMousePlugin() {
40  	    this(InputEvent.BUTTON1_MASK  | InputEvent.CTRL_MASK);
41  	}
42  
43      public SatelliteAnimatedPickingGraphMousePlugin(int selectionModifiers) {
44          super(selectionModifiers);
45      }
46  
47      /**
48       * override subclass method to translate the master view instead
49       * of this satellite view
50       * 
51       */
52      @SuppressWarnings("unchecked")
53      public void mouseReleased(MouseEvent e) {
54      		if (e.getModifiers() == modifiers) {
55  			final VisualizationViewer<V,E> vv = (VisualizationViewer) e.getSource();
56  			if (vv instanceof SatelliteVisualizationViewer) {
57  				final VisualizationViewer<V,E> vvMaster = 
58  					((SatelliteVisualizationViewer) vv).getMaster();
59  
60  				if (vertex != null) {
61  					Layout<V,E> layout = vvMaster.getGraphLayout();
62  					Point2D q = layout.transform(vertex);
63  					Point2D lvc = 
64  						vvMaster.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.LAYOUT, vvMaster.getCenter());
65  					final double dx = (lvc.getX() - q.getX()) / 10;
66  					final double dy = (lvc.getY() - q.getY()) / 10;
67  
68  					Runnable animator = new Runnable() {
69  
70  						public void run() {
71  							for (int i = 0; i < 10; i++) {
72  								vvMaster.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).translate(dx,
73  										dy);
74  								try {
75  									Thread.sleep(100);
76  								} catch (InterruptedException ex) {
77  								}
78  							}
79  						}
80  					};
81  					Thread thread = new Thread(animator);
82  					thread.start();
83  				}
84  			}
85  		}
86  	}
87  }