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  
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 float floor = 1.0f;
33      
34      protected float ceiling = 5.0f;
35      
36      protected float delta = .2f;
37      
38  	/**
39  	 * create an instance with default zoom in/out values
40  	 */
41  	public LensMagnificationGraphMousePlugin() {
42  	    this(MouseEvent.CTRL_MASK);
43  	}
44      
45      /**
46       * create an instance with passed modifiers
47       * @param modifiers
48       */
49      public LensMagnificationGraphMousePlugin(float floor, float ceiling, float delta) {
50          this(MouseEvent.CTRL_MASK, floor, ceiling, delta);
51      }
52      
53      public LensMagnificationGraphMousePlugin(int modifiers) {
54          this(modifiers, 1.0f, 4.0f, .2f);
55      }
56      public LensMagnificationGraphMousePlugin(int modifiers, float floor, float ceiling, float delta) {
57          super(modifiers);
58          this.floor = floor;
59          this.ceiling = ceiling;
60          this.delta = delta;
61      }
62      /**
63       * override to check equality with a mask
64       */
65      public boolean checkModifiers(MouseEvent e) {
66          return (e.getModifiers() & modifiers) != 0;
67      }
68  
69      private void changeMagnification(MutableTransformer transformer, float delta) {
70          if(transformer instanceof LensTransformer) {
71              LensTransformer ht = (LensTransformer)transformer;
72              float magnification = ht.getMagnification() + delta;
73              magnification = Math.max(floor, magnification);
74              magnification = Math.min(magnification, ceiling);
75              ht.setMagnification(magnification);
76          }
77      }
78  	/**
79  	 * zoom the display in or out, depending on the direction of the
80  	 * mouse wheel motion.
81  	 */
82      public void mouseWheelMoved(MouseWheelEvent e) {
83          boolean accepted = checkModifiers(e);
84          float delta = this.delta;
85          if(accepted == true) {
86              VisualizationViewer vv = (VisualizationViewer)e.getSource();
87              MutableTransformer modelTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
88              MutableTransformer viewTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
89              int amount = e.getWheelRotation();
90              if(amount < 0) {
91                  delta = -delta;
92              }
93              changeMagnification(modelTransformer, delta);
94              changeMagnification(viewTransformer, delta);
95              vv.repaint();
96              e.consume();
97          }
98      }
99  }