View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors 
3    *
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9    * Created on Mar 8, 2005
10   *
11   */
12  package edu.uci.ics.jung.visualization.control;
13  
14  import java.awt.Cursor;
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.visualization.Layer;
21  import edu.uci.ics.jung.visualization.VisualizationViewer;
22  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
23  
24  /** 
25   * ViewTranslatingGraphMousePlugin uses a MouseButtonOne press and
26   * drag gesture to translate the graph display in the x and y
27   * direction by changing the AffineTransform applied to the Graphics2D.
28   * The default MouseButtonOne modifier can be overridden
29   * to cause a different mouse gesture to translate the display.
30   * 
31   * 
32   * @author Tom Nelson
33   */
34  public class ViewTranslatingGraphMousePlugin extends AbstractGraphMousePlugin
35      implements MouseListener, MouseMotionListener {
36  
37  	/**
38  	 */
39  	public ViewTranslatingGraphMousePlugin() {
40  	    this(MouseEvent.BUTTON1_MASK);
41  	}
42  
43  	/**
44  	 * create an instance with passed modifer value
45  	 * @param modifiers the mouse event modifier to activate this function
46  	 */
47  	public ViewTranslatingGraphMousePlugin(int modifiers) {
48  	    super(modifiers);
49          this.cursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
50  	}
51  
52  	/**
53       * Check the event modifiers. Set the 'down' point for later
54       * use. If this event satisfies the modifiers, change the cursor
55       * to the system 'move cursor'
56  	 * @param e the event
57  	 */
58  	public void mousePressed(MouseEvent e) {
59  	    VisualizationViewer<?,?> vv = (VisualizationViewer<?,?>)e.getSource();
60  	    boolean accepted = checkModifiers(e);
61  	    down = e.getPoint();
62  	    if(accepted) {
63  	        vv.setCursor(cursor);
64  	    }
65  	}
66      
67  	/**
68  	 * unset the 'down' point and change the cursoe back to the system
69       * default cursor
70  	 */
71      public void mouseReleased(MouseEvent e) {
72          VisualizationViewer<?,?> vv = (VisualizationViewer<?,?>)e.getSource();
73          down = null;
74          vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
75      }
76      
77      /**
78       * chack the modifiers. If accepted, translate the graph according
79       * to the dragging of the mouse pointer
80       * @param e the event
81  	 */
82      public void mouseDragged(MouseEvent e) {
83          VisualizationViewer<?,?> vv = (VisualizationViewer<?,?>)e.getSource();
84          boolean accepted = checkModifiers(e);
85          if(accepted) {
86              MutableTransformer viewTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
87              vv.setCursor(cursor);
88              try {
89                  Point2D q = viewTransformer.inverseTransform(down);
90                  Point2D p = viewTransformer.inverseTransform(e.getPoint());
91                  float dx = (float) (p.getX()-q.getX());
92                  float dy = (float) (p.getY()-q.getY());
93                  
94                  viewTransformer.translate(dx, dy);
95                  down.x = e.getX();
96                  down.y = e.getY();
97              } catch(RuntimeException ex) {
98                  System.err.println("down = "+down+", e = "+e);
99                  throw ex;
100             }
101         
102             e.consume();
103         }
104     }
105 
106     public void mouseClicked(MouseEvent e) {
107     }
108 
109     public void mouseEntered(MouseEvent e) {
110     }
111 
112     public void mouseExited(MouseEvent e) {
113     }
114 
115     public void mouseMoved(MouseEvent e) {
116     }
117 }