tesseract
3.03
|
00001 // Copyright 2007 Google Inc. All Rights Reserved. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); You may not 00004 // use this file except in compliance with the License. You may obtain a copy of 00005 // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 00006 // applicable law or agreed to in writing, software distributed under the 00007 // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 00008 // OF ANY KIND, either express or implied. See the License for the specific 00009 // language governing permissions and limitations under the License. 00010 00011 package com.google.scrollview.events; 00012 00013 import com.google.scrollview.ScrollView; 00014 import com.google.scrollview.events.SVEvent; 00015 import com.google.scrollview.events.SVEventType; 00016 import com.google.scrollview.ui.SVWindow; 00017 00018 import org.piccolo2d.PCamera; 00019 import org.piccolo2d.PNode; 00020 import org.piccolo2d.event.PBasicInputEventHandler; 00021 import org.piccolo2d.event.PInputEvent; 00022 import org.piccolo2d.nodes.PPath; 00023 00024 import java.awt.Color; 00025 import java.awt.event.ActionEvent; 00026 import java.awt.event.ActionListener; 00027 import java.awt.event.KeyEvent; 00028 import java.awt.event.KeyListener; 00029 import java.awt.event.WindowEvent; 00030 import java.awt.event.WindowListener; 00031 00032 import javax.swing.Timer; 00033 00042 public class SVEventHandler extends PBasicInputEventHandler implements 00043 ActionListener, KeyListener, WindowListener { 00044 00046 public Timer timer; 00047 00049 private SVWindow svWindow; 00050 00052 private int lastX = 0; 00053 private int lastY = 0; 00054 00059 private int lastXMove = 0; 00060 private int lastYMove = 0; 00061 00063 private int startX = 0; 00064 private int startY = 0; 00065 private float rubberBandTransparency = 0.5f; 00066 private PNode selection = null; 00067 00072 private String keyStr = "!"; 00073 00075 public SVEventHandler(SVWindow wdw) { 00076 timer = new Timer(1000, this); 00077 svWindow = wdw; 00078 } 00079 00084 private void processEvent(SVEvent e) { 00085 lastXMove = e.x; 00086 lastYMove = e.y; 00087 ScrollView.addMessage(e); 00088 timer.restart(); 00089 } 00090 00092 private void showPopup(PInputEvent e) { 00093 double x = e.getCanvasPosition().getX(); 00094 double y = e.getCanvasPosition().getY(); 00095 00096 if (svWindow.svPuMenu != null) { 00097 svWindow.svPuMenu.show(svWindow, (int) x, (int) y); 00098 } 00099 } 00100 00101 00103 @Override 00104 public void mouseClicked(PInputEvent e) { 00105 if (e.isPopupTrigger()) { 00106 showPopup(e); 00107 } else { 00108 processEvent(new SVEvent(SVEventType.SVET_CLICK, svWindow, (int) e 00109 .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null)); 00110 } 00111 } 00112 00119 @Override 00120 public void mousePressed(PInputEvent e) { 00121 if (e.isPopupTrigger()) { 00122 showPopup(e); 00123 } else { 00124 lastX = (int) e.getPosition().getX(); 00125 lastY = (int) e.getPosition().getY(); 00126 timer.restart(); 00127 } 00128 } 00129 00131 @Override 00132 public void mouseDragged(PInputEvent e) { 00133 processEvent(new SVEvent(SVEventType.SVET_MOUSE, svWindow, (int) e 00134 .getPosition().getX(), (int) e.getPosition().getY(), (int) e 00135 .getPosition().getX() 00136 - lastX, (int) e.getPosition().getY() - lastY, null)); 00137 00138 // Paint a selection rectangle. 00139 if (selection == null) { 00140 startX = (int) e.getPosition().getX(); 00141 startY = (int) e.getPosition().getY(); 00142 selection = PPath.createRectangle(startX, startY, 1, 1); 00143 selection.setTransparency(rubberBandTransparency); 00144 svWindow.canvas.getLayer().addChild(selection); 00145 } else { 00146 int right = Math.max(startX, (int) e.getPosition().getX()); 00147 int left = Math.min(startX, (int) e.getPosition().getX()); 00148 int bottom = Math.max(startY, (int) e.getPosition().getY()); 00149 int top = Math.min(startY, (int) e.getPosition().getY()); 00150 svWindow.canvas.getLayer().removeChild(selection); 00151 selection = PPath.createRectangle(left, top, right - left, bottom - top); 00152 selection.setPaint(Color.YELLOW); 00153 selection.setTransparency(rubberBandTransparency); 00154 svWindow.canvas.getLayer().addChild(selection); 00155 } 00156 } 00157 00164 @Override 00165 public void mouseReleased(PInputEvent e) { 00166 if (e.isPopupTrigger()) { 00167 showPopup(e); 00168 } else { 00169 processEvent(new SVEvent(SVEventType.SVET_SELECTION, svWindow, (int) e 00170 .getPosition().getX(), (int) e.getPosition().getY(), (int) e 00171 .getPosition().getX() 00172 - lastX, (int) e.getPosition().getY() - lastY, null)); 00173 } 00174 if (selection != null) { 00175 svWindow.canvas.getLayer().removeChild(selection); 00176 selection = null; 00177 } 00178 } 00179 00184 @Override 00185 public void mouseWheelRotated(PInputEvent e) { 00186 PCamera lc = svWindow.canvas.getCamera(); 00187 double sf = SVWindow.SCALING_FACTOR; 00188 00189 if (e.getWheelRotation() < 0) { 00190 sf = 1 / sf; 00191 } 00192 lc.scaleViewAboutPoint(lc.getScale() / sf, e.getPosition().getX(), e 00193 .getPosition().getY()); 00194 } 00195 00201 @Override 00202 public void mouseMoved(PInputEvent e) { 00203 processEvent(new SVEvent(SVEventType.SVET_MOTION, svWindow, (int) e 00204 .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null)); 00205 } 00206 00210 @Override 00211 public void mouseEntered(PInputEvent e) { 00212 timer.restart(); 00213 } 00214 00218 @Override 00219 public void mouseExited(PInputEvent e) { 00220 timer.stop(); 00221 } 00222 00227 public void actionPerformed(ActionEvent e) { 00228 processEvent(new SVEvent(SVEventType.SVET_HOVER, svWindow, lastXMove, 00229 lastYMove, 0, 0, null)); 00230 } 00231 00242 public void keyPressed(KeyEvent e) { 00243 char keyCh = e.getKeyChar(); 00244 if (keyCh == '\r' || keyCh == '\n' || keyCh == '\0' || keyCh == '?') { 00245 processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove, 00246 lastYMove, 0, 0, keyStr)); 00247 // Send newline characters as '!' as '!' can never be a keypressed 00248 // and the client eats all newline characters. 00249 keyStr = "!"; 00250 } else { 00251 processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove, 00252 lastYMove, 0, 0, String.valueOf(keyCh))); 00253 keyStr += keyCh; 00254 } 00255 } 00256 00262 public void windowClosing(WindowEvent e) { 00263 processEvent(new SVEvent(SVEventType.SVET_DESTROY, svWindow, lastXMove, 00264 lastYMove, 0, 0, null)); 00265 e.getWindow().dispose(); 00266 SVWindow.nrWindows--; 00267 if (SVWindow.nrWindows == 0) { 00268 processEvent(new SVEvent(SVEventType.SVET_EXIT, svWindow, lastXMove, 00269 lastYMove, 0, 0, null)); 00270 } 00271 } 00272 00274 public void keyReleased(KeyEvent e) { 00275 } 00276 00277 public void keyTyped(KeyEvent e) { 00278 } 00279 00280 public void windowActivated(WindowEvent e) { 00281 } 00282 00283 public void windowClosed(WindowEvent e) { 00284 } 00285 00286 public void windowDeactivated(WindowEvent e) { 00287 } 00288 00289 public void windowDeiconified(WindowEvent e) { 00290 } 00291 00292 public void windowIconified(WindowEvent e) { 00293 } 00294 00295 public void windowOpened(WindowEvent e) { 00296 } 00297 }