1
2
3
4
5
6
7
8
9
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
25
26
27
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
40
41 public LensMagnificationGraphMousePlugin() {
42 this(MouseEvent.CTRL_MASK);
43 }
44
45
46
47
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
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
80
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 }