Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVEventHandler.java
Go to the documentation of this file.
1 // Copyright 2007 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); You may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 // applicable law or agreed to in writing, software distributed under the
7 // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8 // OF ANY KIND, either express or implied. See the License for the specific
9 // language governing permissions and limitations under the License.
10 
11 package com.google.scrollview.events;
12 
13 import com.google.scrollview.ScrollView;
14 import com.google.scrollview.ui.SVWindow;
15 import com.google.scrollview.events.SVEvent;
16 import com.google.scrollview.events.SVEventType;
17 
18 import java.awt.Color;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.awt.event.KeyEvent;
22 import java.awt.event.KeyListener;
23 import java.awt.event.WindowEvent;
24 import java.awt.event.WindowListener;
25 
26 import javax.swing.Timer;
27 
28 import edu.umd.cs.piccolo.PCamera;
29 import edu.umd.cs.piccolo.PNode;
30 import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
31 import edu.umd.cs.piccolo.event.PInputEvent;
32 import edu.umd.cs.piccolo.nodes.PPath;
33 
42 public class SVEventHandler extends PBasicInputEventHandler implements
43  ActionListener, KeyListener, WindowListener {
44 
46  public Timer timer;
47 
49  private SVWindow svWindow;
50 
52  private int lastX = 0;
53  private int lastY = 0;
54 
59  private int lastXMove = 0;
60  private int lastYMove = 0;
61 
63  private int startX = 0;
64  private int startY = 0;
65  private float rubberBandTransparency = 0.5f;
66  private PNode selection = null;
67 
72  private String keyStr = "!";
73 
75  public SVEventHandler(SVWindow wdw) {
76  timer = new Timer(1000, this);
77  svWindow = wdw;
78  }
79 
84  private void processEvent(SVEvent e) {
85  lastXMove = e.x;
86  lastYMove = e.y;
88  timer.restart();
89  }
90 
92  private void showPopup(PInputEvent e) {
93  double x = e.getCanvasPosition().getX();
94  double y = e.getCanvasPosition().getY();
95 
96  if (svWindow.svPuMenu != null) {
97  svWindow.svPuMenu.show(svWindow, (int) x, (int) y);
98  }
99  }
100 
101 
103  @Override
104  public void mouseClicked(PInputEvent e) {
105  if (e.isPopupTrigger()) {
106  showPopup(e);
107  } else {
108  processEvent(new SVEvent(SVEventType.SVET_CLICK, svWindow, (int) e
109  .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null));
110  }
111  }
112 
119  @Override
120  public void mousePressed(PInputEvent e) {
121  if (e.isPopupTrigger()) {
122  showPopup(e);
123  } else {
124  lastX = (int) e.getPosition().getX();
125  lastY = (int) e.getPosition().getY();
126  timer.restart();
127  }
128  }
129 
131  @Override
132  public void mouseDragged(PInputEvent e) {
133  processEvent(new SVEvent(SVEventType.SVET_MOUSE, svWindow, (int) e
134  .getPosition().getX(), (int) e.getPosition().getY(), (int) e
135  .getPosition().getX()
136  - lastX, (int) e.getPosition().getY() - lastY, null));
137 
138  // Paint a selection rectangle.
139  if (selection == null) {
140  startX = (int) e.getPosition().getX();
141  startY = (int) e.getPosition().getY();
142  selection = PPath.createRectangle(startX, startY, 1, 1);
143  selection.setTransparency(rubberBandTransparency);
144  svWindow.canvas.getLayer().addChild(selection);
145  } else {
146  int right = Math.max(startX, (int) e.getPosition().getX());
147  int left = Math.min(startX, (int) e.getPosition().getX());
148  int bottom = Math.max(startY, (int) e.getPosition().getY());
149  int top = Math.min(startY, (int) e.getPosition().getY());
150  svWindow.canvas.getLayer().removeChild(selection);
151  selection = PPath.createRectangle(left, top, right - left, bottom - top);
152  selection.setPaint(Color.YELLOW);
153  selection.setTransparency(rubberBandTransparency);
154  svWindow.canvas.getLayer().addChild(selection);
155  }
156  }
157 
164  @Override
165  public void mouseReleased(PInputEvent e) {
166  if (e.isPopupTrigger()) {
167  showPopup(e);
168  } else {
169  processEvent(new SVEvent(SVEventType.SVET_SELECTION, svWindow, (int) e
170  .getPosition().getX(), (int) e.getPosition().getY(), (int) e
171  .getPosition().getX()
172  - lastX, (int) e.getPosition().getY() - lastY, null));
173  }
174  if (selection != null) {
175  svWindow.canvas.getLayer().removeChild(selection);
176  }
177  }
178 
183  @Override
184  public void mouseWheelRotated(PInputEvent e) {
185  PCamera lc = svWindow.canvas.getCamera();
186  double sf = SVWindow.SCALING_FACTOR;
187 
188  if (e.getWheelRotation() < 0) {
189  sf = 1 / sf;
190  }
191  lc.scaleViewAboutPoint(lc.getScale() / sf, e.getPosition().getX(), e
192  .getPosition().getY());
193  }
194 
200  @Override
201  public void mouseMoved(PInputEvent e) {
202  processEvent(new SVEvent(SVEventType.SVET_MOTION, svWindow, (int) e
203  .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null));
204  }
205 
209  @Override
210  public void mouseEntered(PInputEvent e) {
211  timer.restart();
212  }
213 
217  @Override
218  public void mouseExited(PInputEvent e) {
219  timer.stop();
220  }
221 
226  public void actionPerformed(ActionEvent e) {
227  processEvent(new SVEvent(SVEventType.SVET_HOVER, svWindow, lastXMove,
228  lastYMove, 0, 0, null));
229  }
230 
241  public void keyPressed(KeyEvent e) {
242  char keyCh = e.getKeyChar();
243  if (keyCh == '\r' || keyCh == '\n' || keyCh == '\0' || keyCh == '?') {
244  processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove,
245  lastYMove, 0, 0, keyStr));
246  // Send newline characters as '!' as '!' can never be a keypressed
247  // and the client eats all newline characters.
248  keyStr = "!";
249  } else {
250  processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove,
251  lastYMove, 0, 0, String.valueOf(keyCh)));
252  keyStr += keyCh;
253  }
254  }
255 
261  public void windowClosing(WindowEvent e) {
262  processEvent(new SVEvent(SVEventType.SVET_DESTROY, svWindow, lastXMove,
263  lastYMove, 0, 0, null));
264  e.getWindow().dispose();
266  if (SVWindow.nrWindows == 0) {
267  processEvent(new SVEvent(SVEventType.SVET_EXIT, svWindow, lastXMove,
268  lastYMove, 0, 0, null));
269  }
270  }
271 
273  public void keyReleased(KeyEvent e) {
274  }
275 
276  public void keyTyped(KeyEvent e) {
277  }
278 
279  public void windowActivated(WindowEvent e) {
280  }
281 
282  public void windowClosed(WindowEvent e) {
283  }
284 
285  public void windowDeactivated(WindowEvent e) {
286  }
287 
288  public void windowDeiconified(WindowEvent e) {
289  }
290 
291  public void windowIconified(WindowEvent e) {
292  }
293 
294  public void windowOpened(WindowEvent e) {
295  }
296 }