View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    *
8    * Created on Aug 15, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.control;
12  
13  import java.awt.event.MouseEvent;
14  import java.awt.geom.Point2D;
15  
16  import edu.uci.ics.jung.visualization.Layer;
17  import edu.uci.ics.jung.visualization.VisualizationViewer;
18  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
19  
20  /**
21   * Mouse events in the SatelliteView that match the modifiers
22   * will cause the Main view to rotate
23   * @see RotatingGraphMousePlugin
24   * @author Tom Nelson 
25   *
26   */
27  public class SatelliteRotatingGraphMousePlugin extends RotatingGraphMousePlugin {
28  
29      public SatelliteRotatingGraphMousePlugin() {
30          super();
31      }
32  
33      public SatelliteRotatingGraphMousePlugin(int modifiers) {
34          super(modifiers);
35      }
36      /**
37       * check the modifiers. If accepted, use the mouse drag motion
38       * to rotate the graph in the master view
39       */
40      public void mouseDragged(MouseEvent e) {
41          if(down == null) return;
42          VisualizationViewer vv = (VisualizationViewer)e.getSource();
43          boolean accepted = checkModifiers(e);
44          if(accepted) {
45              if(vv instanceof SatelliteVisualizationViewer) {
46                  VisualizationViewer vvMaster = 
47                      ((SatelliteVisualizationViewer)vv).getMaster();
48                  
49                  MutableTransformer modelTransformerMaster = 
50                  	vvMaster.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
51  
52                  // rotate
53                  vv.setCursor(cursor);
54                  // I want to compute rotation based on the view coordinates of the
55                  // lens center in the satellite view.
56                  // translate the master view center to layout coords, then translate
57                  // that point to the satellite view's view coordinate system....
58                  Point2D center = vv.getRenderContext().getMultiLayerTransformer().transform(vvMaster.getRenderContext().getMultiLayerTransformer().inverseTransform(vvMaster.getCenter()));
59                  Point2D q = down;
60                  Point2D p = e.getPoint();
61                  Point2D v1 = new Point2D.Double(center.getX()-p.getX(), center.getY()-p.getY());
62                  Point2D v2 = new Point2D.Double(center.getX()-q.getX(), center.getY()-q.getY());
63                  double theta = angleBetween(v1, v2);
64                  modelTransformerMaster.rotate(-theta, 
65                          vvMaster.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW, vvMaster.getCenter()));
66                  down.x = e.getX();
67                  down.y = e.getY();
68              } 
69              e.consume();
70          }
71      }
72  
73  }