All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
17 
18 import org.piccolo2d.PCamera;
19 import org.piccolo2d.PNode;
20 import org.piccolo2d.event.PBasicInputEventHandler;
21 import org.piccolo2d.event.PInputEvent;
22 import org.piccolo2d.nodes.PPath;
23 
24 import java.awt.Color;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.KeyEvent;
28 import java.awt.event.KeyListener;
29 import java.awt.event.WindowEvent;
30 import java.awt.event.WindowListener;
31 
32 import javax.swing.Timer;
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  selection = null;
177  }
178  }
179 
184  @Override
185  public void mouseWheelRotated(PInputEvent e) {
186  PCamera lc = svWindow.canvas.getCamera();
187  double sf = SVWindow.SCALING_FACTOR;
188 
189  if (e.getWheelRotation() < 0) {
190  sf = 1 / sf;
191  }
192  lc.scaleViewAboutPoint(lc.getScale() / sf, e.getPosition().getX(), e
193  .getPosition().getY());
194  }
195 
201  @Override
202  public void mouseMoved(PInputEvent e) {
203  processEvent(new SVEvent(SVEventType.SVET_MOTION, svWindow, (int) e
204  .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null));
205  }
206 
210  @Override
211  public void mouseEntered(PInputEvent e) {
212  timer.restart();
213  }
214 
218  @Override
219  public void mouseExited(PInputEvent e) {
220  timer.stop();
221  }
222 
227  public void actionPerformed(ActionEvent e) {
228  processEvent(new SVEvent(SVEventType.SVET_HOVER, svWindow, lastXMove,
229  lastYMove, 0, 0, null));
230  }
231 
242  public void keyPressed(KeyEvent e) {
243  char keyCh = e.getKeyChar();
244  if (keyCh == '\r' || keyCh == '\n' || keyCh == '\0' || keyCh == '?') {
245  processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove,
246  lastYMove, 0, 0, keyStr));
247  // Send newline characters as '!' as '!' can never be a keypressed
248  // and the client eats all newline characters.
249  keyStr = "!";
250  } else {
251  processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove,
252  lastYMove, 0, 0, String.valueOf(keyCh)));
253  keyStr += keyCh;
254  }
255  }
256 
262  public void windowClosing(WindowEvent e) {
263  processEvent(new SVEvent(SVEventType.SVET_DESTROY, svWindow, lastXMove,
264  lastYMove, 0, 0, null));
265  e.getWindow().dispose();
267  if (SVWindow.nrWindows == 0) {
268  processEvent(new SVEvent(SVEventType.SVET_EXIT, svWindow, lastXMove,
269  lastYMove, 0, 0, null));
270  }
271  }
272 
274  public void keyReleased(KeyEvent e) {
275  }
276 
277  public void keyTyped(KeyEvent e) {
278  }
279 
280  public void windowActivated(WindowEvent e) {
281  }
282 
283  public void windowClosed(WindowEvent e) {
284  }
285 
286  public void windowDeactivated(WindowEvent e) {
287  }
288 
289  public void windowDeiconified(WindowEvent e) {
290  }
291 
292  public void windowIconified(WindowEvent e) {
293  }
294 
295  public void windowOpened(WindowEvent e) {
296  }
297 }
static void addMessage(SVEvent e)
Definition: ScrollView.java:60
static final double SCALING_FACTOR
Definition: SVWindow.java:63
void show(Component Invoker, int x, int y)