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.Cursor;
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 TranslatingGraphMousePlugin so that mouse events in
23   * the satellite view cause translating of the main view
24   * 
25   * @see TranslatingGraphMousePlugin
26   * @author Tom Nelson 
27   *
28   */
29  public class SatelliteTranslatingGraphMousePlugin extends
30          TranslatingGraphMousePlugin {
31  
32      public SatelliteTranslatingGraphMousePlugin() {
33          super();
34      }
35  
36      public SatelliteTranslatingGraphMousePlugin(int modifiers) {
37          super(modifiers);
38      }
39      
40      /**
41       * chack the modifiers. If accepted, translate the main view according
42       * to the dragging of the mouse pointer in the satellite view
43       * @param e the event
44       */
45      public void mouseDragged(MouseEvent e) {
46          VisualizationViewer vv = (VisualizationViewer)e.getSource();
47          boolean accepted = checkModifiers(e);
48          if(accepted) {
49              if(vv instanceof SatelliteVisualizationViewer) {
50                  VisualizationViewer vvMaster = 
51                      ((SatelliteVisualizationViewer)vv).getMaster();
52                  
53                  MutableTransformer modelTransformerMaster = 
54                  	vvMaster.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
55                  vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
56                  try {
57                      Point2D q = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(down);
58                      Point2D p = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(e.getPoint());
59                      float dx = (float) (p.getX()-q.getX());
60                      float dy = (float) (p.getY()-q.getY());
61                      
62                      modelTransformerMaster.translate(-dx, -dy);
63                      down.x = e.getX();
64                      down.y = e.getY();
65                  } catch(RuntimeException ex) {
66                      System.err.println("down = "+down+", e = "+e);
67                      throw ex;
68                  }
69              }
70              e.consume();
71          }
72      }
73  
74  
75  }