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.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   * TranslatingGraphMousePlugin uses a MouseButtonOne press and
26   * drag gesture to translate the graph display in the x and y
27   * direction. The default MouseButtonOne modifier can be overridden
28   * to cause a different mouse gesture to translate the display.
29   * 
30   * 
31   * @author Tom Nelson
32   */
33  public class TranslatingGraphMousePlugin extends AbstractGraphMousePlugin
34      implements MouseListener, MouseMotionListener {
35  
36  	/**
37  	 */
38  	public TranslatingGraphMousePlugin() {
39  	    this(MouseEvent.BUTTON1_MASK);
40  	}
41  
42  	/**
43  	 * create an instance with passed modifer value
44  	 * @param modifiers the mouse event modifier to activate this function
45  	 */
46  	public TranslatingGraphMousePlugin(int modifiers) {
47  	    super(modifiers);
48          this.cursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
49  	}
50  
51  	/**
52       * Check the event modifiers. Set the 'down' point for later
53       * use. If this event satisfies the modifiers, change the cursor
54       * to the system 'move cursor'
55  	 * @param e the event
56  	 */
57  	public void mousePressed(MouseEvent e) {
58  	    VisualizationViewer vv = (VisualizationViewer)e.getSource();
59  	    boolean accepted = checkModifiers(e);
60  	    down = e.getPoint();
61  	    if(accepted) {
62  	        vv.setCursor(cursor);
63  	    }
64  	}
65      
66  	/**
67  	 * unset the 'down' point and change the cursoe back to the system
68       * default cursor
69  	 */
70      public void mouseReleased(MouseEvent e) {
71          VisualizationViewer vv = (VisualizationViewer)e.getSource();
72          down = null;
73          vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
74      }
75      
76      /**
77       * chack the modifiers. If accepted, translate the graph according
78       * to the dragging of the mouse pointer
79       * @param e the event
80  	 */
81      public void mouseDragged(MouseEvent e) {
82          VisualizationViewer vv = (VisualizationViewer)e.getSource();
83          boolean accepted = checkModifiers(e);
84          if(accepted) {
85              MutableTransformer modelTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
86              vv.setCursor(cursor);
87              try {
88                  Point2D q = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(down);
89                  Point2D p = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(e.getPoint());
90                  float dx = (float) (p.getX()-q.getX());
91                  float dy = (float) (p.getY()-q.getY());
92                  
93                  modelTransformer.translate(dx, dy);
94                  down.x = e.getX();
95                  down.y = e.getY();
96              } catch(RuntimeException ex) {
97                  System.err.println("down = "+down+", e = "+e);
98                  throw ex;
99              }
100         
101             e.consume();
102             vv.repaint();
103         }
104     }
105 
106     public void mouseClicked(MouseEvent e) {
107         // TODO Auto-generated method stub
108         
109     }
110 
111     public void mouseEntered(MouseEvent e) {
112         // TODO Auto-generated method stub
113         
114     }
115 
116     public void mouseExited(MouseEvent e) {
117         // TODO Auto-generated method stub
118         
119     }
120 
121     public void mouseMoved(MouseEvent e) {
122         // TODO Auto-generated method stub
123         
124     }
125 }