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.event.MouseEvent;
15  import java.awt.event.MouseWheelEvent;
16  import java.awt.event.MouseWheelListener;
17  import java.awt.geom.Point2D;
18  
19  import edu.uci.ics.jung.visualization.VisualizationViewer;
20  
21  /** 
22   * ScalingGraphMouse applies a scaling transformation to the graph layout.
23   * The Vertices get closer or farther apart, but do not themselves change
24   * size. ScalingGraphMouse uses MouseWheelEvents to apply the scaling.
25   * 
26   * @author Tom Nelson
27   */
28  public class ScalingGraphMousePlugin extends AbstractGraphMousePlugin
29      implements MouseWheelListener {
30  
31      /**
32       * the amount to zoom in by
33       */
34  	protected float in = 1.1f;
35  	/**
36  	 * the amount to zoom out by
37  	 */
38  	protected float out = 1/1.1f;
39  	
40  	/**
41  	 * whether to center the zoom at the current mouse position
42  	 */
43  	protected boolean zoomAtMouse = true;
44      
45      /**
46       * controls scaling operations
47       */
48      protected ScalingControl scaler;
49  	
50      public ScalingGraphMousePlugin(ScalingControl scaler, int modifiers) {
51          this(scaler, modifiers, 1.1f, 1/1.1f);
52      }
53      
54      public ScalingGraphMousePlugin(ScalingControl scaler, int modifiers, float in, float out) {
55          super(modifiers);
56          this.scaler = scaler;
57          this.in = in;
58          this.out = out;
59      }
60     /**
61       * @param zoomAtMouse The zoomAtMouse to set.
62       */
63      public void setZoomAtMouse(boolean zoomAtMouse) {
64          this.zoomAtMouse = zoomAtMouse;
65      }
66      
67      public boolean checkModifiers(MouseEvent e) {
68          return e.getModifiers() == modifiers || (e.getModifiers() & modifiers) != 0;
69      }
70  
71      /**
72  	 * zoom the display in or out, depending on the direction of the
73  	 * mouse wheel motion.
74  	 */
75  	public void mouseWheelMoved(MouseWheelEvent e) {
76          boolean accepted = checkModifiers(e);
77          if(accepted == true) {
78              VisualizationViewer vv = (VisualizationViewer)e.getSource();
79              Point2D mouse = e.getPoint();
80              Point2D center = vv.getCenter();
81              int amount = e.getWheelRotation();
82              if(zoomAtMouse) {
83                  if(amount > 0) {
84                      scaler.scale(vv, in, mouse);
85                  } else if(amount < 0) {
86                      scaler.scale(vv, out, mouse);
87                  }
88              } else {
89                  if(amount > 0) {
90                      scaler.scale(vv, in, center);
91                  } else if(amount < 0) {
92                      scaler.scale(vv, out, center);
93                  }
94              }
95              e.consume();
96              vv.repaint();
97          }
98  	}
99      /**
100      * @return Returns the zoom in value.
101      */
102     public float getIn() {
103         return in;
104     }
105     /**
106      * @param in The zoom in value to set.
107      */
108     public void setIn(float in) {
109         this.in = in;
110     }
111     /**
112      * @return Returns the zoom out value.
113      */
114     public float getOut() {
115         return out;
116     }
117     /**
118      * @param out The zoom out value to set.
119      */
120     public void setOut(float out) {
121         this.out = out;
122     }
123 
124     public ScalingControl getScaler() {
125         return scaler;
126     }
127 
128     public void setScaler(ScalingControl scaler) {
129         this.scaler = scaler;
130     }
131 }