1 18 package org.apache.batik.gvt.event; 19 20 import java.awt.Point ; 21 import java.awt.event.InputEvent ; 22 import java.awt.event.KeyEvent ; 23 import java.awt.event.KeyListener ; 24 import java.awt.event.MouseEvent ; 25 import java.awt.event.MouseListener ; 26 import java.awt.event.MouseMotionListener ; 27 import java.awt.geom.AffineTransform ; 28 import java.awt.geom.Point2D ; 29 import java.lang.reflect.Array ; 30 import java.util.EventListener ; 31 import java.util.EventObject ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 35 import javax.swing.event.EventListenerList ; 36 37 import org.apache.batik.gvt.GraphicsNode; 38 import org.apache.batik.gvt.TextNode; 39 40 53 public class AWTEventDispatcher implements EventDispatcher, 54 MouseListener , 55 MouseMotionListener , 56 KeyListener { 57 58 61 protected GraphicsNode root; 62 63 67 protected AffineTransform baseTransform; 68 69 72 protected EventListenerList glisteners; 73 74 77 protected GraphicsNode lastHit; 78 79 83 protected GraphicsNode dummyNode = new TextNode(); 84 85 88 protected GraphicsNode currentKeyEventTarget; 89 90 94 protected List eventQueue = new LinkedList (); 95 protected boolean eventDispatchEnabled = true; 96 protected int eventQueueMaxSize = MAX_QUEUE_SIZE; 97 100 final static int MAX_QUEUE_SIZE = 10; 101 102 private int nodeIncrementEventID = KeyEvent.KEY_PRESSED; 103 private int nodeIncrementEventCode = KeyEvent.VK_TAB; 104 private int nodeIncrementEventModifiers = 0; 105 private int nodeDecrementEventID = KeyEvent.KEY_PRESSED; 106 private int nodeDecrementEventCode = KeyEvent.VK_TAB; 107 private int nodeDecrementEventModifiers = InputEvent.SHIFT_MASK; 108 109 112 public AWTEventDispatcher() { 113 } 114 115 120 public void setRootNode(GraphicsNode root) { 121 if (this.root != root) 122 eventQueue.clear(); this.root = root; 124 } 125 126 130 public GraphicsNode getRootNode() { 131 return root; 132 } 133 134 139 public void setBaseTransform(AffineTransform t) { 140 if ((baseTransform != t) && 141 ((baseTransform == null) || (!baseTransform.equals(t)))) 142 eventQueue.clear(); 145 baseTransform = t; 146 } 147 148 152 public AffineTransform getBaseTransform() { 153 return new AffineTransform (baseTransform); 154 } 155 156 160 165 public void mousePressed(MouseEvent evt) { 166 dispatchEvent(evt); 167 } 168 169 174 public void mouseReleased(MouseEvent evt) { 175 dispatchEvent(evt); 176 } 177 178 183 public void mouseEntered(MouseEvent evt) { 184 dispatchEvent(evt); 185 } 186 187 192 public void mouseExited(MouseEvent evt) { 193 dispatchEvent(evt); 194 } 195 196 201 public void mouseClicked(MouseEvent evt) { 202 dispatchEvent(evt); 203 } 204 205 210 public void mouseMoved(MouseEvent evt) { 211 dispatchEvent(evt); 212 } 213 214 219 public void mouseDragged(MouseEvent evt) { 220 dispatchEvent(evt); 221 } 222 223 228 public void keyPressed(KeyEvent evt) { 229 dispatchEvent(evt); 230 } 231 232 237 public void keyReleased(KeyEvent evt) { 238 dispatchEvent(evt); 239 } 240 241 246 public void keyTyped(KeyEvent evt) { 247 dispatchEvent(evt); 248 } 249 250 254 259 public void addGraphicsNodeMouseListener(GraphicsNodeMouseListener l) { 260 if (glisteners == null) { 261 glisteners = new EventListenerList (); 262 } 263 glisteners.add(GraphicsNodeMouseListener.class, l); 264 } 265 266 271 public void removeGraphicsNodeMouseListener(GraphicsNodeMouseListener l) { 272 if (glisteners != null) { 273 glisteners.remove(GraphicsNodeMouseListener.class, l); 274 } 275 } 276 277 282 public void addGraphicsNodeKeyListener(GraphicsNodeKeyListener l) { 283 if (glisteners == null) { 284 glisteners = new EventListenerList (); 285 } 286 glisteners.add(GraphicsNodeKeyListener.class, l); 287 } 288 289 294 public void removeGraphicsNodeKeyListener(GraphicsNodeKeyListener l) { 295 if (glisteners != null) { 296 glisteners.remove(GraphicsNodeKeyListener.class, l); 297 } 298 } 299 300 305 public EventListener [] getListeners(Class listenerType) { 306 Object array = 307 Array.newInstance(listenerType, 308 glisteners.getListenerCount(listenerType)); 309 Object [] pairElements = glisteners.getListenerList(); 310 for (int i = 0, j = 0;i < pairElements.length-1; i+=2) { 311 if (pairElements[i].equals(listenerType)) { 312 Array.set(array, j, pairElements[i+1]); 313 ++j; 314 } 315 } 316 return (EventListener []) array; 317 } 318 319 323 public void setEventDispatchEnabled(boolean b) { 324 eventDispatchEnabled = b; 325 if (eventDispatchEnabled) { 326 while (eventQueue.size() > 0) { 328 EventObject evt = (EventObject )eventQueue.remove(0); 329 dispatchEvent(evt); 330 } 331 } 332 } 333 public void setEventQueueMaxSize(int n) { 334 eventQueueMaxSize = n; 335 if (n == 0) eventQueue.clear(); 336 while(eventQueue.size() > eventQueueMaxSize) 337 eventQueue.remove(0); 338 } 339 340 344 public void dispatchEvent(EventObject evt) { 345 if (root == null) return; 347 if (!eventDispatchEnabled) { 348 if (eventQueueMaxSize > 0) { 349 eventQueue.add(evt); 350 while (eventQueue.size() > eventQueueMaxSize) 351 eventQueue.remove(0); 354 } 355 return; 356 } 357 if (evt instanceof MouseEvent ) { 358 dispatchMouseEvent((MouseEvent ) evt); 359 } else if (evt instanceof KeyEvent ) { 360 InputEvent e = (InputEvent )evt; 361 if (isNodeIncrementEvent(e)) { 362 incrementKeyTarget(); 363 } else if (isNodeDecrementEvent(e)) { 364 decrementKeyTarget(); 365 } else { 366 dispatchKeyEvent((KeyEvent ) evt); 367 } 368 } 369 } 370 371 375 protected void dispatchKeyEvent(KeyEvent evt) { 376 currentKeyEventTarget = lastHit; 377 if (currentKeyEventTarget != null) { 378 processKeyEvent 379 (new GraphicsNodeKeyEvent(currentKeyEventTarget, 380 evt.getID(), 381 evt.getWhen(), 382 evt.getModifiers(), 383 evt.getKeyCode(), 384 evt.getKeyChar())); 385 } 386 } 387 388 392 protected void dispatchMouseEvent(MouseEvent evt) { 393 GraphicsNodeMouseEvent gvtevt; 394 Point2D p = new Point2D.Float (evt.getX(), evt.getY()); 395 Point2D gnp = p; 396 if (baseTransform != null) { 397 gnp = baseTransform.transform(p, null); 398 } 399 400 GraphicsNode node = root.nodeHitAt(gnp); 401 402 Point screenPos; 405 if (!evt.getComponent().isShowing()) { 406 screenPos = new Point (0,0); 407 } else { 408 screenPos = evt.getComponent().getLocationOnScreen(); 409 screenPos.x += evt.getX(); 410 screenPos.y += evt.getY(); 411 } 412 413 if (lastHit != node) { 414 if (lastHit != null) { 416 gvtevt = new GraphicsNodeMouseEvent(lastHit, 417 MouseEvent. 418 MOUSE_EXITED, 419 evt.getWhen(), 420 evt.getModifiers(), 421 (float)gnp.getX(), 422 (float)gnp.getY(), 423 (int)Math.floor(p.getX()), 424 (int)Math.floor(p.getY()), 425 screenPos.x, 426 screenPos.y, 427 evt.getClickCount(), 428 node); 429 processMouseEvent(gvtevt); 430 } 432 if (node != null) { 434 gvtevt = new GraphicsNodeMouseEvent(node, 435 MouseEvent. 436 MOUSE_ENTERED, 437 evt.getWhen(), 438 evt. 439 getModifiers(), 440 (float)gnp.getX(), 441 (float)gnp.getY(), 442 (int)Math.floor(p.getX()), 443 (int)Math.floor(p.getY()), 444 screenPos.x, 445 screenPos.y, 446 evt.getClickCount(), 447 lastHit); 448 processMouseEvent(gvtevt); 449 } 451 } 452 if (node != null) { 454 gvtevt = new GraphicsNodeMouseEvent(node, 455 evt.getID(), 456 evt.getWhen(), 457 evt.getModifiers(), 458 (float)gnp.getX(), 459 (float)gnp.getY(), 460 (int)Math.floor(p.getX()), 461 (int)Math.floor(p.getY()), 462 screenPos.x, 463 screenPos.y, 464 evt.getClickCount(), 465 null); 466 467 processMouseEvent(gvtevt); 469 470 } else { 471 gvtevt = new GraphicsNodeMouseEvent(root, 473 evt.getID(), 474 evt.getWhen(), 475 evt.getModifiers(), 476 (float)gnp.getX(), 477 (float)gnp.getY(), 478 (int)Math.floor(p.getX()), 479 (int)Math.floor(p.getY()), 480 screenPos.x, 481 screenPos.y, 482 evt.getClickCount(), 483 null); 484 485 processMouseEvent(gvtevt); 486 } 487 lastHit = node; 488 } 489 490 495 protected void processMouseEvent(GraphicsNodeMouseEvent evt) { 496 if (glisteners != null) { 497 GraphicsNodeMouseListener[] listeners = 498 (GraphicsNodeMouseListener[]) 499 getListeners(GraphicsNodeMouseListener.class); 500 switch (evt.getID()) { 501 case GraphicsNodeMouseEvent.MOUSE_MOVED: 502 for (int i = 0; i < listeners.length; i++) { 503 listeners[i].mouseMoved(evt); 504 } 505 break; 506 case GraphicsNodeMouseEvent.MOUSE_DRAGGED: 507 for (int i = 0; i < listeners.length; i++) { 508 listeners[i].mouseDragged(evt); 509 } 510 break; 511 case GraphicsNodeMouseEvent.MOUSE_ENTERED: 512 for (int i = 0; i < listeners.length; i++) { 513 listeners[i].mouseEntered(evt); 514 } 515 break; 516 case GraphicsNodeMouseEvent.MOUSE_EXITED: 517 for (int i = 0; i < listeners.length; i++) { 518 listeners[i].mouseExited(evt); 519 } 520 break; 521 case GraphicsNodeMouseEvent.MOUSE_CLICKED: 522 for (int i = 0; i < listeners.length; i++) { 523 listeners[i].mouseClicked(evt); 524 } 525 break; 526 case GraphicsNodeMouseEvent.MOUSE_PRESSED: 527 for (int i = 0; i < listeners.length; i++) { 528 listeners[i].mousePressed(evt); 529 } 530 break; 531 case GraphicsNodeMouseEvent.MOUSE_RELEASED: 532 for (int i = 0; i < listeners.length; i++) { 533 listeners[i].mouseReleased(evt); 534 } 535 break; 536 default: 537 throw new Error ("Unknown Mouse Event type: "+evt.getID()); 538 } 539 } 540 } 541 542 548 public void processKeyEvent(GraphicsNodeKeyEvent evt) { 549 if ((glisteners != null)) { 550 GraphicsNodeKeyListener[] listeners = 551 (GraphicsNodeKeyListener[]) 552 getListeners(GraphicsNodeKeyListener.class); 553 554 switch (evt.getID()) { 555 case GraphicsNodeKeyEvent.KEY_PRESSED: 556 for (int i=0; i<listeners.length; ++i) { 557 listeners[i].keyPressed(evt); 558 } 559 break; 560 case GraphicsNodeKeyEvent.KEY_RELEASED: 561 for (int i=0; i<listeners.length; ++i) { 562 listeners[i].keyReleased(evt); 563 } 564 break; 565 case GraphicsNodeKeyEvent.KEY_TYPED: 566 for (int i=0; i<listeners.length; ++i) { 567 listeners[i].keyTyped(evt); 568 } 569 break; 570 default: 571 throw new Error ("Unknown Key Event type: "+evt.getID()); 572 } 573 } 574 evt.consume(); 575 } 576 577 578 private void incrementKeyTarget() { 579 throw new Error ("Increment not implemented."); 581 } 582 583 private void decrementKeyTarget() { 584 throw new Error ("Decrement not implemented."); 586 } 587 588 592 public void setNodeIncrementEvent(InputEvent e) { 593 nodeIncrementEventID = e.getID(); 594 if (e instanceof KeyEvent ) { 595 nodeIncrementEventCode = ((KeyEvent ) e).getKeyCode(); 596 } 597 nodeIncrementEventModifiers = e.getModifiers(); 598 } 599 600 606 public void setNodeDecrementEvent(InputEvent e) { 607 nodeDecrementEventID = e.getID(); 608 if (e instanceof KeyEvent ) { 609 nodeDecrementEventCode = ((KeyEvent ) e).getKeyCode(); 610 } 611 nodeDecrementEventModifiers = e.getModifiers(); 612 } 613 614 619 protected boolean isNodeIncrementEvent(InputEvent e) { 620 return ((e.getID() == nodeIncrementEventID) && 622 ((e instanceof KeyEvent ) ? 623 (((KeyEvent ) e).getKeyCode() == nodeIncrementEventCode) : true) && 624 ((e.getModifiers() & nodeIncrementEventModifiers) != 0)); 625 } 626 627 631 protected boolean isNodeDecrementEvent(InputEvent e) { 632 return ((e.getID() == nodeDecrementEventID) && 634 ((e instanceof KeyEvent ) ? 635 ( ((KeyEvent ) e).getKeyCode() == nodeDecrementEventCode) : true) && 636 ((e.getModifiers() & nodeDecrementEventModifiers) != 0 )); 637 638 } 639 } 640 | Popular Tags |