1 /* 2 * $RCSfile: EventCatcher.java,v $ 3 * 4 * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Sun designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Sun in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 24 * CA 95054 USA or visit www.sun.com if you need additional information or 25 * have any questions. 26 * 27 * $Revision: 1.8 $ 28 * $Date: 2008/02/28 20:17:21 $ 29 * $State: Exp $ 30 */ 31 32 package javax.media.j3d; 33 34 import javax.vecmath.*; 35 import java.awt.*; 36 import java.awt.event.*; 37 38 39 /** 40 * The EventCatcher class is used to track events on a Canvas3D using the 41 * 1.1 event model. Most events are sent to the canvas for processing. 42 */ 43 class EventCatcher extends Object implements ComponentListener, FocusListener, 44 KeyListener, MouseListener, MouseMotionListener, MouseWheelListener, WindowListener { 45 46 // The canvas associated with this event catcher 47 private Canvas3D canvas; 48 private static final boolean DEBUG = false; 49 private boolean stopped = false; 50 51 /** 52 * flags for event listeners 53 */ 54 private boolean focusEvents = false; 55 private boolean keyEvents = false; 56 private boolean mouseEvents = false; 57 private boolean mouseMotionEvents = false; 58 private boolean mouseWheelEvents = false; 59 private boolean mouseListenerAdded = false; 60 EventCatcher(Canvas3D c)61 EventCatcher(Canvas3D c) { 62 canvas = c; 63 64 if (VirtualUniverse.mc.isD3D()) { 65 enableKeyEvents(); 66 } 67 } 68 enableFocusEvents()69 void enableFocusEvents() { 70 if (!focusEvents) { 71 canvas.addFocusListener(this); 72 focusEvents = true; 73 } 74 } 75 76 disableFocusEvents()77 void disableFocusEvents() { 78 if (focusEvents) { 79 canvas.removeFocusListener(this); 80 focusEvents = false; 81 } 82 } 83 enableKeyEvents()84 void enableKeyEvents() { 85 if (!keyEvents) { 86 canvas.addKeyListener(this); 87 keyEvents = true; 88 // listen for mouseEntered events for keyboard focusing 89 if (!mouseListenerAdded) { 90 canvas.addMouseListener(this); 91 mouseListenerAdded = true; 92 } 93 } 94 } 95 disableKeyEvents()96 void disableKeyEvents() { 97 if (keyEvents) { 98 canvas.removeKeyListener(this); 99 keyEvents = false; 100 // listen for mouseEntered events for keyboard focusing 101 if (!mouseEvents) { 102 if (mouseListenerAdded) { 103 canvas.removeMouseListener(this); 104 mouseListenerAdded = false; 105 } 106 } 107 } 108 } 109 110 111 enableMouseEvents()112 void enableMouseEvents() { 113 if (!mouseEvents) { 114 mouseEvents = true; 115 if (!mouseListenerAdded) { 116 canvas.addMouseListener(this); 117 mouseListenerAdded = true; 118 } 119 } 120 } 121 disableMouseEvents()122 void disableMouseEvents() { 123 if (mouseEvents) { 124 mouseEvents = false; 125 if (!keyEvents) { 126 if (mouseListenerAdded) { 127 canvas.removeMouseListener(this); 128 mouseListenerAdded = false; 129 } 130 } 131 } 132 } 133 enableMouseMotionEvents()134 void enableMouseMotionEvents() { 135 if (!mouseMotionEvents) { 136 canvas.addMouseMotionListener(this); 137 mouseMotionEvents = true; 138 } 139 } 140 141 disableMouseMotionEvents()142 void disableMouseMotionEvents() { 143 if (mouseMotionEvents) { 144 canvas.removeMouseMotionListener(this); 145 mouseMotionEvents = false; 146 } 147 } 148 enableMouseWheelEvents()149 void enableMouseWheelEvents() { 150 if (!mouseWheelEvents) { 151 canvas.addMouseWheelListener(this); 152 mouseWheelEvents = true; 153 } 154 } 155 156 disableMouseWheelEvents()157 void disableMouseWheelEvents() { 158 if (mouseWheelEvents) { 159 canvas.removeMouseWheelListener(this); 160 mouseWheelEvents = false; 161 } 162 } 163 164 componentResized(ComponentEvent e)165 public void componentResized(ComponentEvent e) { 166 if (e.getSource() == canvas) { 167 if (DEBUG) { 168 System.err.println(e); 169 } 170 canvas.sendEventToBehaviorScheduler(e); 171 if (VirtualUniverse.mc.isD3D()) { 172 canvas.notifyD3DPeer(Canvas3D.RESIZE); 173 } 174 canvas.evaluateVisiblilty(); 175 canvas.redraw(); 176 } 177 } 178 componentMoved(ComponentEvent e)179 public void componentMoved(ComponentEvent e) { 180 if (e.getSource() == canvas) { 181 if (DEBUG) { 182 System.err.println(e); 183 } 184 canvas.sendEventToBehaviorScheduler(e); 185 186 // Issue 458 - the following is not needed for a move 187 // if (VirtualUniverse.mc.isD3D()) { 188 // canvas.notifyD3DPeer(Canvas3D.RESIZE); 189 // } 190 // canvas.evaluateVisiblilty(true); 191 } 192 } 193 componentHidden(ComponentEvent e)194 public void componentHidden(ComponentEvent e) { 195 if (DEBUG) { 196 System.err.println(e); 197 } 198 if (e.getSource() == canvas) { 199 canvas.sendEventToBehaviorScheduler(e); 200 } 201 canvas.evaluateVisiblilty(); 202 } 203 componentShown(ComponentEvent e)204 public void componentShown(ComponentEvent e) { 205 if (DEBUG) { 206 System.err.println(e); 207 } 208 if (e.getSource() == canvas) { 209 canvas.sendEventToBehaviorScheduler(e); 210 } 211 canvas.evaluateVisiblilty(); 212 } 213 focusGained(FocusEvent e)214 public void focusGained(FocusEvent e) { 215 canvas.sendEventToBehaviorScheduler(e); 216 if (DEBUG) { 217 System.err.println(e); 218 } 219 } 220 focusLost(FocusEvent e)221 public void focusLost(FocusEvent e) { 222 canvas.sendEventToBehaviorScheduler(e); 223 if (DEBUG) { 224 System.err.println(e); 225 } 226 } 227 keyTyped(KeyEvent e)228 public void keyTyped(KeyEvent e) { 229 canvas.sendEventToBehaviorScheduler(e); 230 if (DEBUG) { 231 System.err.println(e); 232 } 233 } 234 keyPressed(KeyEvent e)235 public void keyPressed(KeyEvent e) { 236 canvas.sendEventToBehaviorScheduler(e); 237 238 if (VirtualUniverse.mc.isD3D() && 239 e.isAltDown() && 240 (e.getKeyCode() == KeyEvent.VK_ENTER)) { 241 canvas.notifyD3DPeer(Canvas3D.TOGGLEFULLSCREEN); 242 } 243 244 if (DEBUG) { 245 System.err.println(e); 246 } 247 } 248 keyReleased(KeyEvent e)249 public void keyReleased(KeyEvent e) { 250 canvas.sendEventToBehaviorScheduler(e); 251 if (stopped) { 252 stopped = false; 253 } else { 254 stopped = true; 255 } 256 if (DEBUG) { 257 System.err.println(e); 258 } 259 } 260 mouseClicked(MouseEvent e)261 public void mouseClicked(MouseEvent e) { 262 // if (keyEvents && 263 // (VirtualUniverse.mc.getRenderingAPI() != 264 // MasterControl.RENDER_OPENGL_SOLARIS)) { 265 // // bug 4362074 266 // canvas.requestFocus(); 267 // } 268 269 if (mouseEvents) { 270 canvas.sendEventToBehaviorScheduler(e); 271 } 272 if (DEBUG) { 273 System.err.println(e); 274 } 275 } 276 mouseEntered(MouseEvent e)277 public void mouseEntered(MouseEvent e) { 278 // if (keyEvents && 279 // (VirtualUniverse.mc.getRenderingAPI() == 280 // MasterControl.RENDER_OPENGL_SOLARIS)) { 281 // // bug 4362074 282 // canvas.requestFocus(); 283 // } 284 if (mouseEvents) { 285 canvas.sendEventToBehaviorScheduler(e); 286 } 287 if (DEBUG) { 288 System.err.println(e); 289 } 290 } 291 mouseExited(MouseEvent e)292 public void mouseExited(MouseEvent e) { 293 if (mouseEvents) 294 canvas.sendEventToBehaviorScheduler(e); 295 if (DEBUG) { 296 System.err.println(e); 297 } 298 } 299 mousePressed(MouseEvent e)300 public void mousePressed(MouseEvent e) { 301 if (mouseEvents) 302 canvas.sendEventToBehaviorScheduler(e); 303 if (DEBUG) { 304 System.err.println(e); 305 } 306 } 307 mouseReleased(MouseEvent e)308 public void mouseReleased(MouseEvent e) { 309 if (mouseEvents) 310 canvas.sendEventToBehaviorScheduler(e); 311 if (DEBUG) { 312 System.err.println(e); 313 } 314 } 315 mouseDragged(MouseEvent e)316 public void mouseDragged(MouseEvent e) { 317 // Note : We don't have to test for mouseMotionEvent here because 318 // this routine will never be called unless mouseMotionEvent is enabled. 319 canvas.sendEventToBehaviorScheduler(e); 320 if (DEBUG) { 321 System.err.println(e); 322 } 323 } 324 mouseMoved(MouseEvent e)325 public void mouseMoved(MouseEvent e) { 326 // Note : We don't have to test for mouseMotionEvent here because 327 // this routine will never be called unless mouseMotionEvent is enabled. 328 canvas.sendEventToBehaviorScheduler(e); 329 if (DEBUG) { 330 System.err.println(e); 331 } 332 } 333 mouseWheelMoved(MouseWheelEvent e)334 public void mouseWheelMoved(MouseWheelEvent e) { 335 // Note : We don't have to test for mouseWheelEvent here because 336 // this routine will never be called unless mouseWheelEvent is enabled. 337 canvas.sendEventToBehaviorScheduler(e); 338 if (DEBUG) { 339 System.err.println(e); 340 } 341 } 342 343 /* 344 * WindowListener methods 345 */ windowClosed(WindowEvent e)346 public void windowClosed(WindowEvent e) { 347 if (DEBUG) { 348 System.err.println(e); 349 } 350 canvas.sendEventToBehaviorScheduler(e); 351 // Issue 458 - Don't set canvas visible to false 352 } 353 windowClosing(WindowEvent e)354 public void windowClosing(WindowEvent e) { 355 if (DEBUG) { 356 System.err.println(e); 357 } 358 canvas.sendEventToBehaviorScheduler(e); 359 // Issue 458 - Don't set canvas.visible to false 360 } 361 windowActivated(WindowEvent e)362 public void windowActivated(WindowEvent e) { 363 if (DEBUG) { 364 System.err.println(e); 365 } 366 canvas.sendEventToBehaviorScheduler(e); 367 } 368 windowDeactivated(WindowEvent e)369 public void windowDeactivated(WindowEvent e) { 370 if (DEBUG) { 371 System.err.println(e); 372 } 373 canvas.sendEventToBehaviorScheduler(e); 374 } 375 windowDeiconified(WindowEvent e)376 public void windowDeiconified(WindowEvent e) { 377 if (DEBUG) { 378 System.err.println(e); 379 } 380 canvas.sendEventToBehaviorScheduler(e); 381 if (canvas.view != null) { 382 canvas.view.sendEventToSoundScheduler(e); 383 } 384 canvas.evaluateVisiblilty(); 385 } 386 windowIconified(WindowEvent e)387 public void windowIconified(WindowEvent e) { 388 if (DEBUG) { 389 System.err.println(e); 390 } 391 canvas.sendEventToBehaviorScheduler(e); 392 if (canvas.view != null) { 393 canvas.view.sendEventToSoundScheduler(e); 394 } 395 canvas.evaluateVisiblilty(); 396 } 397 windowOpened(WindowEvent e)398 public void windowOpened(WindowEvent e) { 399 if (DEBUG) { 400 System.err.println(e); 401 } 402 canvas.sendEventToBehaviorScheduler(e); 403 canvas.evaluateVisiblilty(); 404 } 405 reset()406 void reset() { 407 focusEvents = false; 408 keyEvents = false; 409 mouseEvents = false; 410 mouseMotionEvents = false; 411 mouseWheelEvents = false; 412 mouseListenerAdded = false; 413 stopped = false; 414 } 415 416 } 417 418