1
2 package hep.wired.interaction;
3
4 import java.awt.*;
5 import java.awt.event.*;
6
7 import org.freehep.application.Application;
8 import org.freehep.xml.io.XMLIOManager;
9
10 import hep.wired.edit.Scale;
11 import hep.wired.edit.Translate;
12 import hep.wired.services.GraphicsPanel;
13 import hep.wired.services.InteractionHandler;
14 import hep.wired.services.RecordPlot;
15
16 /***
17 * Default InteractionHandler which handles mousewheel events to scroll, cursor keys to translate.
18 *
19 * @author Mark Donszelmann
20 * @version $Id: DefaultInteractionHandler.java 1858 2005-06-08 00:11:23Z duns $
21 */
22 public class DefaultInteractionHandler extends AbstractInteractionHandler {
23
24 private static DefaultInteractionHandler instance;
25
26 protected static final double fixedScale = Math.sqrt(2);
27 protected static final double multiplyScale = 3;
28 protected static final double fixedTranslate = 1000;
29 protected static final double multiplyTranslate = 10;
30
31 private double lastScale;
32
33 /***
34 * Creates a DefaultInteractionHandler.
35 */
36 protected DefaultInteractionHandler(String name) {
37 super(name);
38 lastScale = 1.0;
39 }
40
41 public static DefaultInteractionHandler getInstance() {
42 if (instance == null) {
43 instance = new DefaultInteractionHandler("Default");
44 }
45 return instance;
46 }
47
48 public String getDescription() {
49 return "Move mousewheel to zoom in/out; Use cursor keys to translate.";
50 }
51
52 public boolean isSupportedBy(GraphicsPanel panel) {
53 return true;
54 }
55
56 public void changeCursor(RecordPlot plot, InputEvent event) {
57 plot.setCursor(Cursor.getDefaultCursor());
58 }
59
60 public void mouseEntered(RecordPlot plot, MouseEvent event) {
61 plot.requestFocusInWindow();
62 changeCursor(plot, event);
63 }
64
65 protected double getLastScale() {
66 return lastScale;
67 }
68
69 public void mouseWheelMoved(RecordPlot plot, MouseWheelEvent event) {
70 if (!plot.supports(new Scale())) {
71 lastScale = 1;
72 return;
73 }
74
75 double scale = fixedScale - 1.0;
76 scale *= event.isShiftDown() ? multiplyScale : 1;
77 scale *= event.isControlDown() ? 1/multiplyScale : 1;
78 scale += 1.0;
79 double clicks = event.getWheelRotation();
80 scale *= clicks;
81 scale = (scale < 0) ? 1 / -scale : scale;
82 lastScale = scale;
83 plot.postEdit(new Scale(scale, scale, scale));
84 }
85
86 public boolean otherKeyPressed(RecordPlot plot, KeyEvent event) {
87 switch (event.getKeyCode()) {
88 case KeyEvent.VK_UP:
89 case KeyEvent.VK_KP_UP:
90 translate(plot, event, 0, fixedTranslate);
91 return true;
92
93 case KeyEvent.VK_DOWN:
94 case KeyEvent.VK_KP_DOWN:
95 translate(plot, event, 0, -fixedTranslate);
96 return true;
97
98 case KeyEvent.VK_LEFT:
99 case KeyEvent.VK_KP_LEFT:
100 translate(plot, event, -fixedTranslate, 0);
101 return true;
102
103 case KeyEvent.VK_RIGHT:
104 case KeyEvent.VK_KP_RIGHT:
105 translate(plot, event, fixedTranslate, 0);
106 return true;
107
108 case KeyEvent.VK_PLUS:
109 case KeyEvent.VK_ADD:
110 if (plot.supports(new Scale())) {
111 plot.postEdit(new Scale(fixedScale, fixedScale, fixedScale));
112 }
113 return true;
114
115 case KeyEvent.VK_MINUS:
116 case KeyEvent.VK_SUBTRACT:
117 if (plot.supports(new Scale())) {
118 plot.postEdit(new Scale(1/fixedScale, 1/fixedScale, 1/fixedScale));
119 }
120 return true;
121
122 default:
123 return false;
124 }
125 }
126
127 private void translate(RecordPlot plot, InputEvent event, double dx, double dy) {
128 if (!plot.supports(new Translate())) return;
129
130 if (event.isControlDown()) {
131 dx /= multiplyTranslate;
132 dy /= multiplyTranslate;
133 }
134
135 if (event.isShiftDown()) {
136 dx *= multiplyTranslate;
137 dy *= multiplyTranslate;
138 }
139
140 plot.postEdit(new Translate(dx, dy, 0));
141 }
142 }