1 13 14 package swingwt.awt; 15 16 17 import java.lang.reflect.InvocationTargetException ; 18 import java.lang.reflect.Method ; 19 import java.text.AttributedCharacterIterator ; 20 import java.text.AttributedString ; 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.custom.CCombo; 26 import org.eclipse.swt.custom.ExtendedModifyEvent; 27 import org.eclipse.swt.custom.StyledText; 28 import org.eclipse.swt.events.ModifyEvent; 29 import org.eclipse.swt.events.ModifyListener; 30 import org.eclipse.swt.widgets.Combo; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Text; 33 34 import swingwt.awt.event.ActionEvent; 35 import swingwt.awt.event.ActionListener; 36 import swingwt.awt.event.ComponentEvent; 37 import swingwt.awt.event.ComponentListener; 38 import swingwt.awt.event.FocusEvent; 39 import swingwt.awt.event.FocusListener; 40 import swingwt.awt.event.InputMethodEvent; 41 import swingwt.awt.event.InputMethodListener; 42 import swingwt.awt.event.KeyEvent; 43 import swingwt.awt.event.KeyListener; 44 import swingwt.awt.event.MouseEvent; 45 import swingwt.awt.event.MouseListener; 46 import swingwt.awt.event.MouseMotionListener; 47 import swingwt.awt.font.TextHitInfo; 48 import swingwtx.accessibility.AccessibleContext; 49 import swingwtx.swing.JEditorPane; 50 import swingwtx.swing.JScrollPane; 51 import swingwtx.swing.JTable; 52 import swingwtx.swing.JTextArea; 53 import swingwtx.swing.JToolBar; 54 import swingwtx.swing.JTree; 55 import swingwtx.swing.SwingUtilities; 56 import swingwtx.swing.SwingWTUtils; 57 58 64 public class Component implements ImageObserver { 65 66 67 public Control peer = null; 68 70 protected org.eclipse.swt.widgets.Composite ppeer = null; 71 72 73 protected String componentName = this.getClass().getName(); 74 75 76 protected static GraphicsConfiguration graphicsConfig = new GraphicsConfiguration(); 77 78 79 protected Container parent = null; 80 81 82 public Object layoutModifier = null; 83 84 85 protected boolean pVisible = true; 86 87 protected boolean pEnabled = true; 88 89 protected String pToolTipText = ""; 90 91 protected swingwt.awt.Color pBackground = null; 92 93 protected swingwt.awt.Color pForeground = null; 94 95 protected Dimension pSize = null; 96 97 protected Dimension pPrefSize = null; 98 99 protected Dimension pMinSize = null; 100 101 protected Dimension pMaxSize = null; 102 103 protected swingwt.awt.Point pLocation = null; 104 105 protected swingwt.awt.Font pFont = null; 106 107 protected Object pLayoutData = null; 108 109 protected swingwt.awt.Cursor pCursor = swingwt.awt.Cursor.getPredefinedCursor(swingwt.awt.Cursor.DEFAULT_CURSOR); 110 111 protected boolean pDoubleBuffered = false; 112 113 114 protected Vector actionListeners = new Vector (); 115 116 protected Vector mouseListeners = new Vector (); 117 118 protected Vector mouseMotionListeners = new Vector (); 119 120 protected Vector keyListeners = new Vector (); 121 122 protected Vector focusListeners = new Vector (); 123 124 protected Vector componentListeners = new Vector (); 125 126 protected Vector inputMethodListeners = new Vector (); 127 128 protected boolean consumeAvailableWidth = false; 129 130 protected boolean consumeAvailableHeight = false; 131 132 protected String actionCommand = ""; 133 134 protected boolean isUserSetFont = false; 135 136 protected boolean focusable = true; 137 138 private Object retval = null; 139 private int iretval = 0; 140 141 protected org.eclipse.swt.events.MouseListener swingWTDefaultActionEventAdapter = null; 142 143 protected Component.SWTEventAdapter swingWTEventAdapter = null; 144 145 protected AccessibleContext accessibleContext = null; 146 147 148 public static final float TOP_ALIGNMENT = 0.0f; 149 public static final float CENTER_ALIGNMENT = 0.5f; 150 public static final float BOTTOM_ALIGNMENT = 1.0f; 151 public static final float LEFT_ALIGNMENT = 0.0f; 152 public static final float RIGHT_ALIGNMENT = 1.0f; 153 154 155 public void paint(Graphics g) { 156 } 157 158 163 protected void paintComponent(Graphics g) { 164 paint(g); 165 } 166 167 169 174 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 175 ppeer = new org.eclipse.swt.widgets.Canvas(parent.getComposite(), 0); 176 peer = ppeer; 177 this.parent = parent; 178 } 179 180 181 public Control getPeer() { return peer; } 182 183 184 public void show() { setVisible(true); } 185 186 public void hide() { setVisible(false); } 187 188 189 public void setVisible(final boolean b) { 190 pVisible = b; 191 SwingUtilities.invokeSync(new Runnable () { 192 public void run() { 193 if (SwingWTUtils.isSWTControlAvailable(peer)) 194 peer.setVisible(b); 195 } 196 }); 197 } 198 199 public boolean isVisible() { return pVisible; } 200 201 public boolean isShowing() { return isVisible(); } 202 203 204 public void setEnabled(final boolean b) { 205 pEnabled = b; 206 SwingUtilities.invokeAsync(new Runnable () { 207 public void run() { 208 if (SwingWTUtils.isSWTControlAvailable(peer)) 209 peer.setEnabled(b); 210 } 211 }); 212 } 213 214 public boolean isEnabled() { return pEnabled; } 215 216 public void requestFocus() { SwingUtilities.invokeSync(new Runnable () { public void run() { if (SwingWTUtils.isSWTControlAvailable(peer)) peer.setFocus();}}); } 217 218 public void grabFocus() { SwingUtilities.invokeSync(new Runnable () { public void run() {if (SwingWTUtils.isSWTControlAvailable(peer)) peer.forceFocus();}}); } 219 220 public void repaint(int x, int y, int width, int height) { 221 repaint(0, x, y, width, height); 222 } 223 224 public void repaint(long tm, int x, int y, int width, int height) { 225 repaint(); 227 } 228 229 230 public void repaint() { 231 SwingUtilities.invokeSync(new Runnable () { 232 public void run() { 233 if (SwingWTUtils.isSWTControlAvailable(peer)) { 234 if (ppeer != null) { 238 Graphics g = getGraphics(); 239 paintComponent(g); 240 g.dispose(); 241 } 242 else { 243 peer.redraw(); 245 } 246 } 247 } 248 }); 249 } 250 251 252 public swingwt.awt.Color getBackground() { 253 if (!SwingWTUtils.isSWTControlAvailable(peer)) 254 return pBackground; 255 else { 256 SwingUtilities.invokeSync(new Runnable () { 257 public void run() { 258 retval = new swingwt.awt.Color(peer.getBackground()); 259 } 260 }); 261 return (Color) retval; 262 } 263 } 264 265 public void setBackground(final swingwt.awt.Color c) { 266 if (!SwingWTUtils.isSWTControlAvailable(peer)) 267 pBackground = c; 268 else 269 SwingUtilities.invokeSync(new Runnable () { 270 public void run() { 271 peer.setBackground(c.getSWTColor()); 272 } 273 }); 274 } 275 276 public swingwt.awt.Color getForeground() { 277 if (!SwingWTUtils.isSWTControlAvailable(peer)) 278 return pForeground; 279 else { 280 SwingUtilities.invokeSync(new Runnable () { 281 public void run() { 282 retval = new swingwt.awt.Color(peer.getForeground()); 283 } 284 }); 285 return (Color) retval; 286 } 287 } 288 289 public void setForeground(final swingwt.awt.Color c) { 290 if (!SwingWTUtils.isSWTControlAvailable(peer)) 291 pForeground = c; 292 else 293 SwingUtilities.invokeSync(new Runnable () { 294 public void run() { 295 peer.setForeground(c.getSWTColor()); 296 } 297 }); 298 } 299 300 301 public int getWidth() { 302 return getSize().width; 303 } 304 305 306 public int getHeight() { 307 return getSize().height; 308 } 309 310 311 public void setWidth(final int width) { 312 if (!SwingWTUtils.isSWTControlAvailable(peer)) return; 313 SwingUtilities.invokeSync(new Runnable () { 314 public void run() { 315 org.eclipse.swt.graphics.Point p = peer.getSize(); 316 p.x = width; peer.setSize(p); 317 } 318 }); 319 } 320 321 322 public void setHeight(final int height) { 323 if (!SwingWTUtils.isSWTControlAvailable(peer)) return; 324 SwingUtilities.invokeSync(new Runnable () { 325 public void run() { 326 org.eclipse.swt.graphics.Point p = peer.getSize(); 327 p.y = height; peer.setSize(p); 328 } 329 }); 330 } 331 332 334 public Graphics getGraphics() { 335 if (!SwingWTUtils.isSWTControlAvailable(peer)) 336 return null; 337 else { 338 SwingUtilities.invokeSync(new Runnable () { 339 public void run() { 340 graphics = new SWTGraphics2DRenderer(new org.eclipse.swt.graphics.GC(peer), true); 341 } 342 }); 343 return (Graphics) graphics; 344 } 345 } 346 private Object graphics = null; 347 348 352 public Dimension getPreferredSize() { 353 354 if (pPrefSize != null) 359 return pPrefSize; 360 361 else if (SwingWTUtils.isSWTControlAvailable(peer)) { 362 if (this instanceof JToolBar || this instanceof swingwtx.custom.JCoolBar) { 364 org.eclipse.swt.graphics.Point p = peer.computeSize(-1, -1); 365 return new Dimension(p.x, p.y); 366 } 367 368 else if (this instanceof Container) { 370 371 if (((Container) this).getComposite() == null) { 374 org.eclipse.swt.graphics.Point p = peer.computeSize(-1, -1); 375 return new Dimension(p.x, p.y); 376 } 377 378 if (((Container) this).getLayout() == null) 380 return new Dimension(0, 0); 381 382 return ((Container) this).getLayout().preferredLayoutSize((Container) this); 383 } 384 else 385 { 386 org.eclipse.swt.graphics.Point p = peer.computeSize(-1, -1); 387 return new Dimension(p.x, p.y); 388 } 389 } 390 else { 391 return calculatePreferredSize(); 394 } 395 } 396 397 public void setPreferredSize(Dimension d) { pPrefSize = d; } 398 406 protected Dimension calculatePreferredSize() { 407 Dimension size = new Dimension(150, 50); 408 setSize(size); 409 return size; 410 } 411 412 413 public Dimension getMaximumSize() { if (pMaxSize == null) pMaxSize = new Dimension(1024, 1024); return pMaxSize; } 414 415 public void setMaximumSize(Dimension d) { pMaxSize = d; } 416 417 public Dimension getMinimumSize() { if (pMinSize == null) pMinSize = new Dimension(1, 1); return pMinSize; } 418 419 public void setMinimumSize(Dimension d) { pMinSize = d; } 420 public boolean isMinimumSizeSet() { return pMinSize!=null; } 421 422 423 public void setSize(final int width, final int height) { 424 setSize(new Dimension(width, height)); 425 } 426 427 public void setSize(final Dimension d) { 428 429 pSize = d; 430 if (!SwingWTUtils.isSWTControlAvailable(peer)) return; 431 432 SwingUtilities.invokeSync(new Runnable () { 433 public void run() { 434 peer.setBounds(peer.getLocation().x, peer.getLocation().y, d.width, d.height); 435 } 436 }); 437 } 438 439 451 public Dimension getSize() { 452 if (parent instanceof JScrollPane && 453 (!(this instanceof JTable)) && 454 (!(this instanceof JTextArea)) && 455 (!(this instanceof JTree)) && 456 (!(this instanceof JEditorPane)) 457 ) { 458 Dimension returnDim = getPreferredSize(); 459 Dimension peers = getPeerSize(); 460 if (returnDim.width < peers.width) returnDim.width = peers.width; 461 if (returnDim.height < peers.height) returnDim.height = peers.height; 462 return returnDim; 463 } 464 else 465 return getPeerSize(); 466 } 467 468 469 public Dimension getPeerSize() { 470 final Dimension d = new Dimension(); 471 if (!SwingWTUtils.isSWTControlAvailable(peer)) return d; 472 SwingUtilities.invokeSync(new Runnable () { 473 public void run() { 474 d.height = peer.getSize().y; 475 d.width = peer.getSize().x; 476 } 477 }); 478 return d; 479 } 480 481 482 public void setFocusTraversalKeysEnabled(boolean b) {} 483 484 485 public Point getLocation() { 486 final Point p = new Point(0, 0); 487 if (!SwingWTUtils.isSWTControlAvailable(peer)) return p; 488 SwingUtilities.invokeSync(new Runnable () { 489 public void run() { 490 p.x = peer.getLocation().x; 491 p.y = peer.getLocation().y; 492 } 493 }); 494 return p; 495 } 496 497 500 public Point getLocationOnScreen() { 501 final Point p = new Point(0, 0); 502 if (!SwingWTUtils.isSWTControlAvailable(peer)) return p; 503 SwingUtilities.invokeSync(new Runnable () { 504 public void run() { 505 p.x = peer.getLocation().x + peer.getShell().getLocation().x; 506 p.y = peer.getLocation().y + peer.getShell().getLocation().y; 507 } 508 }); 509 return p; 510 } 511 512 513 public Image createImage(int width, int height) { 514 swingwt.awt.image.BufferedImage i = new swingwt.awt.image.BufferedImage(width, height, swingwt.awt.image.BufferedImage.TYPE_INT_RGB); 515 return i; 516 } 517 518 519 public swingwt.awt.image.VolatileImage createVolatileImage(int width, int height) { 520 swingwt.awt.image.VolatileImage i = new swingwt.awt.image.VolatileImage(width, height, swingwt.awt.image.BufferedImage.TYPE_INT_RGB); 521 return i; 522 } 523 524 525 public swingwt.awt.FontMetrics getFontMetrics(swingwt.awt.Font f) { 526 return Toolkit.getDefaultToolkit().getFontMetrics(f); 527 } 528 529 530 531 public swingwt.awt.Font getFont() { 532 if (!SwingWTUtils.isSWTControlAvailable(peer)) 533 return pFont; 534 else { 535 SwingUtilities.invokeSync(new Runnable () { 536 public void run() { 537 retval = new swingwt.awt.Font(peer.getFont()); 538 } 539 }); 540 return (swingwt.awt.Font) retval; 541 } 542 } 543 544 public void setFont(final swingwt.awt.Font f) { 545 if (f!=null) { 546 isUserSetFont = true; 547 if (!SwingWTUtils.isSWTControlAvailable(peer)) 548 pFont = f; 549 else 550 SwingUtilities.invokeSync(new Runnable () { 551 public void run() { 552 peer.setFont(f.getSWTFont()); 553 } 554 }); 555 } else { 556 isUserSetFont = false; 557 } 558 } 559 560 565 public boolean hasSetFont() { 566 return isUserSetFont; 567 } 568 569 570 public String getToolTipText() { return pToolTipText; } 571 572 public void setToolTipText(final String text) { pToolTipText = text; SwingUtilities.invokeSync(new Runnable () { public void run() {if (SwingWTUtils.isSWTControlAvailable(peer)) peer.setToolTipText(text);}}); } 573 574 public void setBounds(final int x, final int y, final int width, final int height) { 575 576 pSize = new Dimension(width, height); 577 pLocation = new Point(x, y); 578 579 if (!SwingWTUtils.isSWTControlAvailable(peer)) return; 580 581 SwingUtilities.invokeSync(new Runnable () { 582 public void run() { 583 peer.setBounds(x, y, width, height); 584 } 585 }); 586 } 587 588 public void setBounds(Rectangle r) { 589 setBounds(r.x, r.y, r.width, r.height); 590 } 591 592 public Rectangle getBounds() { 593 if (!SwingWTUtils.isSWTControlAvailable(peer)) return new Rectangle(0, 0, 0, 0); 594 SwingUtilities.invokeSync(new Runnable () { 595 public void run() { 596 retval = new Rectangle(peer.getLocation().x, peer.getLocation().y, peer.getSize().x, peer.getSize().y); 597 } 598 }); 599 return (Rectangle) retval; 600 } 601 602 public float getAlignmentX() { return 0; } 603 604 public float getAlignmentY() { return 0; } 605 606 public void setAlignmentX(float val) {} 607 608 public void setAlignmentY(float val) {} 609 610 611 public void transferFocus() { 612 SwingUtilities.invokeSync(new Runnable () { 613 public void run() { 614 peer.traverse(SWT.TRAVERSE_TAB_NEXT); 615 } 616 }); 617 } 618 619 620 public void transferFocusBackward() { 621 SwingUtilities.invokeSync(new Runnable () { 622 public void run() { 623 peer.traverse(SWT.TRAVERSE_TAB_PREVIOUS); 624 } 625 }); 626 } 627 628 629 public void transferFocusUpCycle() { 630 } 631 632 633 public boolean contains(int x, int y) { return false; } 634 635 637 public void invalidate() { repaint(); } 638 639 640 public void validate() {} 641 642 643 public String getActionCommand() { return actionCommand; } 644 645 public void setActionCommand(String command) { actionCommand = command; } 646 647 public void setLocation(final Point p) { 648 pLocation = p; 649 SwingUtilities.invokeSync(new Runnable () { 650 public void run() { 651 if (SwingWTUtils.isSWTControlAvailable(peer)) 652 peer.setBounds(p.x, p.y, peer.getSize().x, peer.getSize().y); 653 } 654 }); 655 } 656 657 public void setLocation(int x, int y) { 658 setLocation(new Point(x, y)); 659 } 660 661 public int getX() { 662 SwingUtilities.invokeSync(new Runnable () { 663 public void run() { 664 if (SwingWTUtils.isSWTControlAvailable(peer)) 665 iretval = peer.getLocation().x; 666 else 667 iretval = 1; 668 } 669 }); 670 return iretval; 671 } 672 673 public int getY() { 674 SwingUtilities.invokeSync(new Runnable () { 675 public void run() { 676 if (SwingWTUtils.isSWTControlAvailable(peer)) 677 iretval = peer.getLocation().y; 678 else 679 iretval = 1; 680 } 681 }); 682 return iretval; 683 } 684 685 public Container getParent() { return parent; } 686 687 688 public String getName() { return componentName; } 689 690 public void setName(String newName) { componentName = newName; } 691 692 public void setCursor(final swingwt.awt.Cursor c) { 693 pCursor = c; 694 SwingUtilities.invokeSync(new Runnable () { 695 public void run() { 696 try { 697 if (SwingWTUtils.isSWTControlAvailable(peer)) 698 peer.setCursor(c.getSWTCursor()); 699 } 700 catch (Exception e) { 701 e.printStackTrace(); 702 } 703 } 704 }); 705 } 706 707 public boolean isFocusable() { return focusable; } 708 709 public void setFocusable(boolean b) { focusable = b; } 710 711 public swingwt.awt.Cursor getCursor() { return pCursor; } 712 713 public Toolkit getToolkit() { return Toolkit.getDefaultToolkit(); } 714 715 public GraphicsConfiguration getGraphicsConfiguration() { return graphicsConfig; } 716 717 public ComponentOrientation getComponentOrientation() { return ComponentOrientation.LEFT_TO_RIGHT; } 718 public void setComponentOrientation(ComponentOrientation o) { } 719 720 721 public void dispose() { 722 componentOnlyDispose(); 723 } 724 725 729 public void componentOnlyDispose() { 730 731 if (SwingWTUtils.isSWTControlAvailable(peer)) { 732 SwingUtilities.invokeSync(new Runnable () { 733 public void run() { 734 peer.dispose(); 735 } 736 }); 737 } 738 739 peer = null; 740 ppeer = null; 741 742 mouseListeners.removeAllElements(); 743 mouseMotionListeners.removeAllElements(); 744 actionListeners.removeAllElements(); 745 focusListeners.removeAllElements(); 746 keyListeners.removeAllElements(); 747 componentListeners.removeAllElements(); 748 inputMethodListeners.removeAllElements(); 749 } 750 751 752 public void addActionListener(ActionListener l) { 753 actionListeners.add(l); 754 } 755 756 757 public void removeActionListener(ActionListener l) { 758 actionListeners.remove(l); 759 } 760 761 762 public void addComponentListener(ComponentListener l) { 763 componentListeners.add(l); 764 } 765 766 767 public void removeComponentListener(ComponentListener l) { 768 componentListeners.remove(l); 769 } 770 771 772 public void addMouseListener(MouseListener l) { 773 mouseListeners.add(l); 774 } 775 776 777 public void removeMouseListener(MouseListener l) { 778 mouseListeners.remove(l); 779 } 780 781 782 public void addMouseMotionListener(MouseMotionListener l) { 783 mouseMotionListeners.add(l); 784 } 785 786 787 public void removeMouseMotionListener(MouseMotionListener l) { 788 mouseMotionListeners.remove(l); 789 } 790 791 792 public void addKeyListener(KeyListener l) { 793 keyListeners.add(l); 794 } 795 796 797 public void removeKeyListener(KeyListener l) { 798 keyListeners.remove(l); 799 } 800 801 802 public void addFocusListener(FocusListener l) { 803 focusListeners.add(l); 804 } 805 806 807 public void removeFocusListener(FocusListener l) { 808 focusListeners.remove(l); 809 } 810 811 812 public void addInputMethodListener(InputMethodListener l) { 813 inputMethodListeners.add(l); 814 } 815 816 817 public void removeInputMethodListener(InputMethodListener l) { 818 inputMethodListeners.remove(l); 819 } 820 821 824 public void processActionEvent(int id) { 825 Iterator i = actionListeners.iterator(); 826 ActionEvent ae = new ActionEvent(this, id, this.getActionCommand()); 827 while (i.hasNext()) { 828 ActionListener al = (ActionListener) i.next(); 829 al.actionPerformed(ae); 830 } 831 } 832 833 public void processComponentEvent(ComponentEvent e) { 834 for (int i = 0; i < componentListeners.size(); i++) { 835 if (e.getID() == ComponentEvent.COMPONENT_HIDDEN) 836 ((ComponentListener) componentListeners.get(i)).componentHidden(e); 837 if (e.getID() == ComponentEvent.COMPONENT_MOVED) 838 ((ComponentListener) componentListeners.get(i)).componentMoved(e); 839 if (e.getID() == ComponentEvent.COMPONENT_RESIZED) 840 ((ComponentListener) componentListeners.get(i)).componentResized(e); 841 if (e.getID() == ComponentEvent.COMPONENT_SHOWN) 842 ((ComponentListener) componentListeners.get(i)).componentShown(e); 843 } 844 } 845 846 849 public void processMouseEvent(MouseEvent e) { 850 Iterator i = mouseListeners.iterator(); 851 while (i.hasNext()) { 852 MouseListener ml = (MouseListener) i.next(); 853 if (e.eventID == MouseEvent.CLICKED) ml.mouseClicked(e); 854 else if (e.eventID == MouseEvent.ENTERED) ml.mouseEntered(e); 855 else if (e.eventID == MouseEvent.EXITED) ml.mouseExited(e); 856 else if (e.eventID == MouseEvent.PRESSED) ml.mousePressed(e); 857 else if (e.eventID == MouseEvent.RELEASED) ml.mouseReleased(e); 858 } 859 } 860 861 864 public void processMouseMotionEvent(MouseEvent e) { 865 for (int i = 0; i < mouseMotionListeners.size(); i++) { 866 MouseMotionListener ml = (MouseMotionListener) mouseMotionListeners.get(i); 867 if (e.getButton() != 0) { 868 ml.mouseDragged(e); 869 } 870 else 871 ml.mouseMoved(e); 872 } 873 } 874 875 878 public void processKeyEvent(KeyEvent e) { 879 Iterator i = keyListeners.iterator(); 880 while (i.hasNext()) { 881 KeyListener ml = (KeyListener) i.next(); 882 if (e.eventID == KeyEvent.TYPED) ml.keyTyped(e); 883 if (e.eventID == KeyEvent.RELEASED) ml.keyReleased(e); 884 if (e.eventID == KeyEvent.PRESSED) ml.keyPressed(e); 885 } 886 } 887 888 891 public void processFocusEvent(FocusEvent e) { 892 Iterator i = focusListeners.iterator(); 893 while (i.hasNext()) { 894 FocusListener fl = (FocusListener) i.next(); 895 if (e.getID() == FocusEvent.FOCUS_GAINED) fl.focusGained(e); 896 if (e.getID() == FocusEvent.FOCUS_LOST) fl.focusLost(e); 897 } 898 } 899 900 903 public void processInputMethodEvent(InputMethodEvent e) { 904 Iterator i = inputMethodListeners.iterator(); 905 while (i.hasNext()) { 906 InputMethodListener iml = (InputMethodListener) i.next(); 907 if (e.getID() == InputMethodEvent.CARET_POSITION_CHANGED) iml.caretPositionChanged(e); 908 if (e.getID() == InputMethodEvent.INPUT_METHOD_TEXT_CHANGED) iml.inputMethodTextChanged(e); 909 } 910 } 911 912 917 public void setCachedProperties() { 918 if (peer == null) return; 919 peer.setToolTipText(pToolTipText); 920 if (pBackground != null) peer.setBackground(pBackground.getSWTColor()); 921 if (pForeground != null) peer.setForeground(pForeground.getSWTColor()); 922 if (pFont != null) peer.setFont(pFont.getSWTFont()); 923 if (pSize != null && pLocation != null) { 924 peer.setBounds(pLocation.x, pLocation.y, pSize.width, pSize.height); 925 } 926 else if (pSize != null) 927 peer.setBounds(peer.getLocation().x, peer.getLocation().y, pSize.width, pSize.height); 928 else if (pLocation != null) 929 peer.setBounds(pLocation.x, pLocation.y, peer.getSize().x, peer.getSize().y); 930 if (pCursor != null && !pCursor.equals(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR))) peer.setCursor(pCursor.getSWTCursor()); 931 if (pLayoutData != null) peer.setLayoutData(pLayoutData); 932 } 933 934 940 public void registerEvents() { 941 942 if (!SwingWTUtils.isSWTControlAvailable(peer)) return; 943 944 if (swingWTEventAdapter == null) 945 swingWTEventAdapter = new Component.SWTEventAdapter(this); 946 947 registerActionEvents(); 948 registerComponentEvents(); 949 registerMouseEvents(); 950 registerKeyEvents(); 951 registerFocusEvents(); 952 registerTraversalEvents(); 953 registerPaintEvents(); 954 registerModifyEvents(); 955 registerExtendedModifyEvents(); 956 } 957 958 963 private class SWTEventAdapter implements org.eclipse.swt.events.PaintListener, 964 org.eclipse.swt.events.TraverseListener, 965 org.eclipse.swt.events.FocusListener, 966 org.eclipse.swt.events.MouseListener, 967 org.eclipse.swt.events.MouseTrackListener, 968 org.eclipse.swt.events.MouseMoveListener, 969 org.eclipse.swt.events.KeyListener, 970 org.eclipse.swt.events.ControlListener, 971 org.eclipse.swt.custom.ExtendedModifyListener, 972 org.eclipse.swt.events.ModifyListener { 973 974 975 private Component parent = null; 976 977 public SWTEventAdapter(Component parent) { 978 this.parent = parent; 979 } 980 public void paintControl(org.eclipse.swt.events.PaintEvent e) { 981 982 if (pDoubleBuffered) { 985 org.eclipse.swt.graphics.Image img = 986 new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), e.width, e.height); 987 org.eclipse.swt.graphics.GC gc = 988 new org.eclipse.swt.graphics.GC(img); 989 paintComponent(new SWTGraphics2DRenderer(gc)); 991 e.gc.drawImage(img, 0, 0); 993 gc.dispose(); img.dispose(); 995 } 996 else 997 paintComponent(new SWTGraphics2DRenderer(e.gc)); 998 999 } 1000 public void keyTraversed(org.eclipse.swt.events.TraverseEvent e) { 1001 processTraverseEvent(e); 1002 } 1003 public void focusGained(org.eclipse.swt.events.FocusEvent e) { 1004 FocusEvent ev = new FocusEvent(parent, FocusEvent.FOCUS_GAINED); 1005 processFocusEvent(ev); 1006 } 1007 public void focusLost(org.eclipse.swt.events.FocusEvent e) { 1008 FocusEvent ev = new FocusEvent(parent, FocusEvent.FOCUS_LOST); 1009 processFocusEvent(ev); 1010 } 1011 public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 1012 MouseEvent ev = new MouseEvent(parent); 1013 ev.clickCount = 1; 1014 ev.eventID = MouseEvent.PRESSED; 1015 ev.setX(e.x); 1016 ev.setY(e.y); 1017 ev.setButton(ev.translateSWTButton(e.button)); 1018 processMouseEvent(ev); 1019 ev.eventID = MouseEvent.CLICKED; 1020 processMouseEvent(ev); 1021 } 1022 public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) { 1023 MouseEvent ev = new MouseEvent(parent); 1024 ev.clickCount = 2; 1025 ev.eventID = MouseEvent.CLICKED; 1026 ev.setX(e.x); 1027 ev.setY(e.y); 1028 ev.setButton(ev.translateSWTButton(e.button)); 1029 processMouseEvent(ev); 1030 } 1031 public void mouseUp(org.eclipse.swt.events.MouseEvent e) { 1032 MouseEvent ev = new MouseEvent(parent); 1033 ev.clickCount = 1; 1034 ev.eventID = MouseEvent.RELEASED; 1035 ev.setX(e.x); 1036 ev.setY(e.y); 1037 ev.setButton(ev.translateSWTButton(e.button)); 1038 processMouseEvent(ev); 1039 processMouseMotionEvent(ev); } 1041 public void mouseEnter(org.eclipse.swt.events.MouseEvent e) { 1042 MouseEvent ev = new MouseEvent(parent); 1043 ev.clickCount = 0; 1044 ev.eventID = MouseEvent.ENTERED; 1045 ev.setX(e.x); 1046 ev.setY(e.y); 1047 ev.setButton(ev.translateSWTButton(e.button)); 1048 processMouseEvent(ev); 1049 } 1050 public void mouseExit(org.eclipse.swt.events.MouseEvent e) { 1051 MouseEvent ev = new MouseEvent(parent); 1052 ev.clickCount = 0; 1053 ev.eventID = MouseEvent.EXITED; 1054 ev.setX(e.x); 1055 ev.setY(e.y); 1056 ev.setButton(ev.translateSWTButton(e.button)); 1057 processMouseEvent(ev); 1058 } 1059 public void mouseHover(org.eclipse.swt.events.MouseEvent e) { 1060 MouseEvent ev = new MouseEvent(parent); 1061 ev.eventID = MouseEvent.ENTERED; 1062 ev.setX(e.x); 1063 ev.setY(e.y); 1064 ev.setButton(ev.translateSWTButton(e.button)); 1065 processMouseMotionEvent(ev); 1066 } 1067 public void mouseMove(org.eclipse.swt.events.MouseEvent e) { 1068 MouseEvent ev = new MouseEvent(parent); 1069 ev.eventID = MouseEvent.ENTERED; 1070 ev.setX(e.x); 1071 ev.setY(e.y); 1072 ev.setButton(ev.translateSWTButton(e.button)); 1073 processMouseMotionEvent(ev); 1074 } 1075 public void keyPressed(org.eclipse.swt.events.KeyEvent e) { 1076 KeyEvent ev = new KeyEvent(parent); 1077 ev.eventID = KeyEvent.PRESSED; 1078 ev.setKeyChar(e.character); 1079 ev.setKeyCode(ev.translateSWTKey(e.keyCode)); 1080 ev.setModifiers(ev.translateSWTModifiers(e.stateMask)); 1081 processKeyEvent(ev); 1082 1083 if (e.doit) e.doit = !ev.isConsumed(); 1085 } 1086 public void keyReleased(org.eclipse.swt.events.KeyEvent e) { 1087 KeyEvent ev = new KeyEvent(parent); 1088 ev.eventID = KeyEvent.RELEASED; 1089 ev.setKeyChar(e.character); 1090 ev.setKeyCode(ev.translateSWTKey(e.keyCode)); 1091 ev.setModifiers(ev.translateSWTModifiers(e.stateMask)); 1092 processKeyEvent(ev); 1093 1094 if (e.doit) e.doit = !ev.isConsumed(); 1096 } 1097 1098 public void controlMoved(org.eclipse.swt.events.ControlEvent controlEvent) { 1099 processComponentEvent(new ComponentEvent(parent, ComponentEvent.COMPONENT_MOVED)); 1100 } 1101 1102 public void controlResized(org.eclipse.swt.events.ControlEvent controlEvent) { 1103 processComponentEvent(new ComponentEvent(parent, ComponentEvent.COMPONENT_RESIZED)); 1104 } 1105 1106 public void modifyText(ExtendedModifyEvent modifyEvent) { 1107 1108 int id = InputMethodEvent.INPUT_METHOD_TEXT_CHANGED; 1110 1111 if (id > 0) { 1112 1113 String fullText = ""; 1114 if ( peer instanceof StyledText ) fullText = ((StyledText)peer).getText(); 1115 if ( peer instanceof Text ) fullText = ((Text)peer).getText(); 1116 if ( peer instanceof CCombo ) fullText = ((CCombo)peer).getText(); 1117 if ( peer instanceof Combo ) fullText = ((Combo)peer).getText(); 1118 1119 int caretPosition = modifyEvent.start + modifyEvent.length; 1120 1121 AttributedString attributedString = new AttributedString (fullText); 1122 AttributedCharacterIterator text = attributedString. 1123 getIterator(null, modifyEvent.start, caretPosition); 1124 TextHitInfo textHitInfo = TextHitInfo.trailing(caretPosition); 1125 InputMethodEvent inputMethodEvent = new InputMethodEvent( parent, id, text, modifyEvent.length, 1126 textHitInfo, textHitInfo ); 1127 processInputMethodEvent(inputMethodEvent); 1128 1129 } 1130 else { 1131 throw new IllegalComponentStateException("Unknown ExtendedModifyState error"); 1132 } 1133 } 1134 1135 public void modifyText(ModifyEvent modifyEvent) { 1136 } 1138 1139 1140 } 1141 1142 1143 protected void registerPaintEvents() { 1144 peer.removePaintListener(swingWTEventAdapter); 1145 peer.addPaintListener(swingWTEventAdapter); 1146 } 1147 1148 protected void registerTraversalEvents() { 1149 peer.removeTraverseListener(swingWTEventAdapter); 1150 peer.addTraverseListener(swingWTEventAdapter); 1151 } 1152 1153 protected void registerFocusEvents() { 1154 peer.removeFocusListener(swingWTEventAdapter); 1155 peer.addFocusListener(swingWTEventAdapter); 1156 } 1157 1158 protected void registerComponentEvents() { 1159 peer.removeControlListener(swingWTEventAdapter); 1160 peer.addControlListener(swingWTEventAdapter); 1161 } 1162 1163 protected void registerMouseEvents() { 1164 peer.removeMouseListener(swingWTEventAdapter); 1165 peer.removeMouseTrackListener(swingWTEventAdapter); 1166 peer.addMouseListener(swingWTEventAdapter); 1167 peer.addMouseTrackListener(swingWTEventAdapter); 1168 } 1169 1170 protected void registerKeyEvents() { 1171 peer.removeKeyListener(swingWTEventAdapter); 1172 peer.addKeyListener(swingWTEventAdapter); 1173 } 1174 1175 protected void registerActionEvents() { 1176 if (swingWTDefaultActionEventAdapter == null) 1177 swingWTDefaultActionEventAdapter = new org.eclipse.swt.events.MouseListener() { 1178 public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 1179 processActionEvent(0); 1180 } 1181 public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) { 1182 processActionEvent(0); 1183 } 1184 public void mouseUp(org.eclipse.swt.events.MouseEvent e) { 1185 } 1186 }; 1187 peer.removeMouseListener(swingWTDefaultActionEventAdapter); 1188 peer.addMouseListener(swingWTDefaultActionEventAdapter); 1189 } 1190 1191 1192 1204 protected void registerModifyEvents() { 1205 if ( peer instanceof StyledText || peer instanceof Text || 1206 peer instanceof CCombo || peer instanceof Combo ) 1207 { 1208 boolean success = false; 1209 try 1210 { 1211 Method addMethod = peer.getClass().getMethod("addModifyListener", new Class [] {ModifyListener.class}); 1212 Method removeMethod = peer.getClass().getMethod("removeModifyListener", new Class [] {ModifyListener.class}); 1213 removeMethod.invoke(peer, new Object [] {swingWTEventAdapter}); 1214 addMethod.invoke(peer, new Object [] {swingWTEventAdapter}); 1215 1216 success = true; 1217 } 1218 catch (NoSuchMethodException e) {} 1219 catch (SecurityException e) {} 1220 catch (IllegalAccessException e) {} 1221 catch (InvocationTargetException e) {} 1222 1223 if (!success) 1224 throw new IllegalComponentStateException("SWT peer missing required methods! (?)"); 1225 } 1226 } 1227 1228 1231 protected void registerExtendedModifyEvents() { 1232 if ( peer instanceof StyledText ) 1233 { 1234 boolean success = false; 1235 try 1236 { 1237 Method addMethod = peer.getClass().getMethod("addExtendedModifyListener", 1238 new Class [] {ModifyListener.class}); 1239 Method removeMethod = peer.getClass().getMethod("removeExtendedModifyListener", 1240 new Class [] {ModifyListener.class}); 1241 removeMethod.invoke(peer, new Object [] {swingWTEventAdapter}); 1242 addMethod.invoke(peer, new Object [] {swingWTEventAdapter}); 1243 1244 success = true; 1245 } 1246 catch (NoSuchMethodException e) {} 1247 catch (SecurityException e) {} 1248 catch (IllegalAccessException e) {} 1249 catch (InvocationTargetException e) {} 1250 1251 if (!success) 1252 throw new IllegalComponentStateException("SWT peer missing required methods! (?)"); 1253 } 1254 } 1255 1256 1261 protected void processTraverseEvent(org.eclipse.swt.events.TraverseEvent e) { 1262 1263 if (e.keyCode == SWT.CR) { 1266 processActionEvent(0); 1267 } 1268 1269 if (swingwtx.swing.FocusManager.getCurrentManager() == null) { return; } 1272 1273 if (((e.keyCode == SWT.TAB) && ((e.stateMask & SWT.SHIFT)) != 0) || 1275 (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { 1276 e.doit = false; 1277 swingwtx.swing.FocusManager.getCurrentManager().focusPreviousComponent(this); 1278 } 1279 1280 if (((e.keyCode == SWT.TAB) && ((e.stateMask & SWT.SHIFT)) == 0) || 1282 (e.detail == SWT.TRAVERSE_TAB_NEXT)) { 1283 e.doit = false; 1284 swingwtx.swing.FocusManager.getCurrentManager().focusNextComponent(this); 1285 } 1286 1287 } 1288 1289 1294 protected void setComponentRemoved() { 1295 } 1296 1297 1298 public boolean imageUpdate( 1299 Image img, 1300 int infoflags, 1301 int x, 1302 int y, 1303 int width, 1304 int height) { 1305 return false; 1307 } 1308 1309 public boolean hasFocus() { 1310 final boolean[] ret = new boolean[1]; 1311 SwingUtilities.invokeSync( 1312 new Runnable () { 1313 public void run() { 1314 if (SwingWTUtils.isSWTControlAvailable(peer)) 1315 ret[0] = peer.isFocusControl(); 1316 else 1317 ret[0] = false; 1318 } 1319 }); 1320 return ret[0]; 1321 } 1322 1323 1327 protected Object LOCK = new Object (); 1328 1332 public Object getTreeLock() { return LOCK; } 1333 1334 1335 public AccessibleContext getAccessibleContext() { return accessibleContext; } 1336 1337} 1338 1339 1567 | Popular Tags |