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.Dimension;
14  import java.awt.event.MouseEvent;
15  import java.awt.geom.Point2D;
16  
17  import edu.uci.ics.jung.visualization.Layer;
18  import edu.uci.ics.jung.visualization.VisualizationViewer;
19  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
20  
21  /**
22   * Overrides ShearingGraphMousePlugin so that mouse events in the
23   * satellite view cause shearing of the main view
24   * 
25   * @see ShearingGraphMousePlugin
26   * @author Tom Nelson 
27   *
28   */
29  public class SatelliteShearingGraphMousePlugin extends ShearingGraphMousePlugin {
30  
31      public SatelliteShearingGraphMousePlugin() {
32          super();
33      }
34  
35      public SatelliteShearingGraphMousePlugin(int modifiers) {
36          super(modifiers);
37      }
38      
39      /**
40       * overridden to shear the main view
41       */
42      public void mouseDragged(MouseEvent e) {
43          if(down == null) return;
44          VisualizationViewer vv = (VisualizationViewer)e.getSource();
45          boolean accepted = checkModifiers(e);
46          if(accepted) {
47              if(vv instanceof SatelliteVisualizationViewer) {
48                  VisualizationViewer vvMaster = 
49                      ((SatelliteVisualizationViewer)vv).getMaster();
50                  
51                  MutableTransformer modelTransformerMaster = 
52                  	vvMaster.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
53  
54                  vv.setCursor(cursor);
55                  Point2D q = down;
56                  Point2D p = e.getPoint();
57                  float dx = (float) (p.getX()-q.getX());
58                  float dy = (float) (p.getY()-q.getY());
59  
60                  Dimension d = vv.getSize();
61                  float shx = 2.f*dx/d.height;
62                  float shy = 2.f*dy/d.width;
63                  // I want to compute shear based on the view coordinates of the
64                  // lens center in the satellite view.
65                  // translate the master view center to layout coords, then translate
66                  // that point to the satellite view's view coordinate system....
67                  Point2D center = vv.getRenderContext().getMultiLayerTransformer().transform(vvMaster.getRenderContext().getMultiLayerTransformer().inverseTransform(vvMaster.getCenter()));
68                  if(p.getX() < center.getX()) {
69                      shy = -shy;
70                  }
71                  if(p.getY() < center.getY()) {
72                      shx = -shx;
73                  }
74                  modelTransformerMaster.shear(-shx, -shy, vvMaster.getCenter());
75  
76                  down.x = e.getX();
77                  down.y = e.getY();
78              }
79              e.consume();
80          }
81      }
82  }