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.event.MouseEvent;
15  import java.awt.event.MouseWheelEvent;
16  import java.awt.event.MouseWheelListener;
17  
18  import edu.uci.ics.jung.visualization.Layer;
19  import edu.uci.ics.jung.visualization.VisualizationViewer;
20  import edu.uci.ics.jung.visualization.transform.LensTransformer;
21  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
22  
23  /** 
24   * HyperbolicMagnificationGraphMousePlugin changes the magnification
25   * within the Hyperbolic projection of the HyperbolicTransformer.
26   * 
27   * @author Tom Nelson
28   */
29  public class LensMagnificationGraphMousePlugin extends AbstractGraphMousePlugin
30      implements MouseWheelListener {
31  
32      protected final float floor;
33      protected final float ceiling;
34      protected final float delta;
35      
36  	/**
37  	 * Creates an instance with modifier of CTRL_MASK, and default min/max/delta zoom values
38  	 * of 1/4/0.2.
39  	 */
40  	public LensMagnificationGraphMousePlugin() {
41  	    this(MouseEvent.CTRL_MASK);
42  	}
43      
44  	/**
45  	 * Creates an instance with modifier of CTRL_MASK, and the specified zoom parameters.
46  	 * @param floor the minimum zoom value
47  	 * @param ceiling the maximum zoom value
48  	 * @param delta the change in zoom value caused by each mouse event
49  	 */
50      public LensMagnificationGraphMousePlugin(float floor, float ceiling, float delta) {
51          this(MouseEvent.CTRL_MASK, floor, ceiling, delta);
52      }
53      
54      /**
55       * Creates an instance with the specified modifiers and the default min/max/delta zoom values
56       * of 1/4/0.2.
57       * @param modifiers the mouse event modifiers to specify
58       */
59      public LensMagnificationGraphMousePlugin(int modifiers) {
60          this(modifiers, 1.0f, 4.0f, .2f);
61      }
62      
63  	/**
64  	 * Creates an instance with the specified mouse event modifiers and zoom parameters.
65       * @param modifiers the mouse event modifiers to specify
66  	 * @param floor the minimum zoom value
67  	 * @param ceiling the maximum zoom value
68  	 * @param delta the change in zoom value caused by each mouse event
69  	 */
70      public LensMagnificationGraphMousePlugin(int modifiers, float floor, float ceiling, float delta) {
71          super(modifiers);
72          this.floor = floor;
73          this.ceiling = ceiling;
74          this.delta = delta;
75      }
76  
77      /**
78       * override to check equality with a mask
79       */
80      public boolean checkModifiers(MouseEvent e) {
81          return (e.getModifiers() & modifiers) != 0;
82      }
83  
84      private void changeMagnification(MutableTransformer transformer, float delta) {
85          if(transformer instanceof LensTransformer) {
86              LensTransformer ht = (LensTransformer)transformer;
87              float magnification = ht.getMagnification() + delta;
88              magnification = Math.max(floor, magnification);
89              magnification = Math.min(magnification, ceiling);
90              ht.setMagnification(magnification);
91          }
92      }
93  	/**
94  	 * zoom the display in or out, depending on the direction of the
95  	 * mouse wheel motion.
96  	 */
97      public void mouseWheelMoved(MouseWheelEvent e) {
98          boolean accepted = checkModifiers(e);
99          float delta = this.delta;
100         if(accepted == true) {
101             VisualizationViewer<?, ?> vv = (VisualizationViewer<?, ?>)e.getSource();
102             MutableTransformer modelTransformer = vv.getRenderContext().getMultiLayerTransformer()
103             	.getTransformer(Layer.LAYOUT);
104             MutableTransformer viewTransformer = vv.getRenderContext().getMultiLayerTransformer()
105             	.getTransformer(Layer.VIEW);
106             int amount = e.getWheelRotation();
107             if(amount < 0) {
108                 delta = -delta;
109             }
110             changeMagnification(modelTransformer, delta);
111             changeMagnification(viewTransformer, delta);
112             vv.repaint();
113             e.consume();
114         }
115     }
116 }