| 1 7 package javax.swing.text; 8 9 import java.lang.reflect.Method ; 10 11 import java.security.AccessController ; 12 import java.security.PrivilegedAction ; 13 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Hashtable ; 17 import java.util.Enumeration ; 18 import java.util.Vector ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Map.Entry; 22 import java.util.Set ; 23 24 import java.io.*; 25 26 import java.awt.*; 27 import java.awt.event.*; 28 import java.awt.datatransfer.*; 29 import java.awt.im.InputContext ; 30 import java.awt.im.InputMethodRequests ; 31 import java.awt.font.TextHitInfo ; 32 import java.awt.font.TextAttribute ; 33 34 import java.text.*; 35 import java.text.AttributedCharacterIterator.Attribute; 36 37 import javax.swing.*; 38 import javax.swing.event.*; 39 import javax.swing.plaf.*; 40 41 import javax.accessibility.*; 42 43 import sun.awt.AppContext; 44 45 263 public abstract class JTextComponent extends JComponent implements Scrollable, Accessible 264 { 265 272 public JTextComponent() { 273 super(); 274 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.INPUT_METHOD_EVENT_MASK); 276 caretEvent = new MutableCaretEvent(this); 277 addMouseListener(caretEvent); 278 addFocusListener(caretEvent); 279 setEditable(true); 280 setDragEnabled(false); 281 setLayout(null); updateUI(); 283 } 284 285 290 public TextUI getUI() { return (TextUI)ui; } 291 292 297 public void setUI(TextUI ui) { 298 super.setUI(ui); 299 } 300 301 307 public void updateUI() { 308 setUI((TextUI)UIManager.getUI(this)); 309 invalidate(); 310 } 311 312 319 public void addCaretListener(CaretListener listener) { 320 listenerList.add(CaretListener.class, listener); 321 } 322 323 329 public void removeCaretListener(CaretListener listener) { 330 listenerList.remove(CaretListener.class, listener); 331 } 332 333 346 public CaretListener[] getCaretListeners() { 347 return (CaretListener[])listenerList.getListeners(CaretListener.class); 348 } 349 350 360 protected void fireCaretUpdate(CaretEvent e) { 361 Object [] listeners = listenerList.getListenerList(); 363 for (int i = listeners.length-2; i>=0; i-=2) { 366 if (listeners[i]==CaretListener.class) { 367 ((CaretListener)listeners[i+1]).caretUpdate(e); 368 } 369 } 370 } 371 372 385 public void setDocument(Document doc) { 386 Document old = model; 387 388 392 try { 393 if (old instanceof AbstractDocument ) { 394 ((AbstractDocument )old).readLock(); 395 } 396 if (accessibleContext != null) { 397 model.removeDocumentListener( 398 ((AccessibleJTextComponent)accessibleContext)); 399 } 400 if (inputMethodRequestsHandler != null) { 401 model.removeDocumentListener((DocumentListener)inputMethodRequestsHandler); 402 } 403 model = doc; 404 405 Boolean runDir = getComponentOrientation().isLeftToRight() 408 ? TextAttribute.RUN_DIRECTION_LTR 409 : TextAttribute.RUN_DIRECTION_RTL; 410 doc.putProperty( TextAttribute.RUN_DIRECTION, runDir ); 411 412 firePropertyChange("document", old, doc); 413 } finally { 414 if (old instanceof AbstractDocument ) { 415 ((AbstractDocument )old).readUnlock(); 416 } 417 } 418 419 revalidate(); 420 repaint(); 421 if (accessibleContext != null) { 422 model.addDocumentListener( 423 ((AccessibleJTextComponent)accessibleContext)); 424 } 425 if (inputMethodRequestsHandler != null) { 426 model.addDocumentListener((DocumentListener)inputMethodRequestsHandler); 427 } 428 } 429 430 439 public Document getDocument() { 440 return model; 441 } 442 443 public void setComponentOrientation( ComponentOrientation o ) { 445 Document doc = getDocument(); 448 if( doc != null ) { 449 Boolean runDir = o.isLeftToRight() 450 ? TextAttribute.RUN_DIRECTION_LTR 451 : TextAttribute.RUN_DIRECTION_RTL; 452 doc.putProperty( TextAttribute.RUN_DIRECTION, runDir ); 453 } 454 super.setComponentOrientation( o ); 455 } 456 457 466 public Action [] getActions() { 467 return getUI().getEditorKit(this).getActions(); 468 } 469 470 485 public void setMargin(Insets m) { 486 Insets old = margin; 487 margin = m; 488 firePropertyChange("margin", old, m); 489 invalidate(); 490 } 491 492 498 public Insets getMargin() { 499 return margin; 500 } 501 502 509 public void setNavigationFilter(NavigationFilter filter) { 510 navigationFilter = filter; 511 } 512 513 522 public NavigationFilter getNavigationFilter() { 523 return navigationFilter; 524 } 525 526 532 public Caret getCaret() { 533 return caret; 534 } 535 536 549 public void setCaret(Caret c) { 550 if (caret != null) { 551 caret.removeChangeListener(caretEvent); 552 caret.deinstall(this); 553 } 554 Caret old = caret; 555 caret = c; 556 if (caret != null) { 557 caret.install(this); 558 caret.addChangeListener(caretEvent); 559 } 560 firePropertyChange("caret", old, caret); 561 } 562 563 568 public Highlighter getHighlighter() { 569 return highlighter; 570 } 571 572 587 public void setHighlighter(Highlighter h) { 588 if (highlighter != null) { 589 highlighter.deinstall(this); 590 } 591 Highlighter old = highlighter; 592 highlighter = h; 593 if (highlighter != null) { 594 highlighter.install(this); 595 } 596 firePropertyChange("highlighter", old, h); 597 } 598 599 612 public void setKeymap(Keymap map) { 613 Keymap old = keymap; 614 keymap = map; 615 firePropertyChange("keymap", old, keymap); 616 updateInputMap(old, map); 617 } 618 619 659 public void setDragEnabled(boolean b) { 660 if (b && GraphicsEnvironment.isHeadless()) { 661 throw new HeadlessException(); 662 } 663 dragEnabled = b; 664 } 665 666 673 public boolean getDragEnabled() { 674 return dragEnabled; 675 } 676 677 678 684 void updateInputMap(Keymap oldKm, Keymap newKm) { 685 InputMap km = getInputMap(JComponent.WHEN_FOCUSED); 687 InputMap last = km; 688 while (km != null && !(km instanceof KeymapWrapper)) { 689 last = km; 690 km = km.getParent(); 691 } 692 if (km != null) { 693 if (newKm == null) { 696 if (last != km) { 697 last.setParent(km.getParent()); 698 } 699 else { 700 last.setParent(null); 701 } 702 } 703 else { 704 InputMap newKM = new KeymapWrapper(newKm); 705 last.setParent(newKM); 706 if (last != km) { 707 newKM.setParent(km.getParent()); 708 } 709 } 710 } 711 else if (newKm != null) { 712 km = getInputMap(JComponent.WHEN_FOCUSED); 713 if (km != null) { 714 InputMap newKM = new KeymapWrapper(newKm); 717 newKM.setParent(km.getParent()); 718 km.setParent(newKM); 719 } 720 } 721 722 ActionMap am = getActionMap(); 724 ActionMap lastAM = am; 725 while (am != null && !(am instanceof KeymapActionMap)) { 726 lastAM = am; 727 am = am.getParent(); 728 } 729 if (am != null) { 730 if (newKm == null) { 733 if (lastAM != am) { 734 lastAM.setParent(am.getParent()); 735 } 736 else { 737 lastAM.setParent(null); 738 } 739 } 740 else { 741 ActionMap newAM = new KeymapActionMap(newKm); 742 lastAM.setParent(newAM); 743 if (lastAM != am) { 744 newAM.setParent(am.getParent()); 745 } 746 } 747 } 748 else if (newKm != null) { 749 am = getActionMap(); 750 if (am != null) { 751 ActionMap newAM = new KeymapActionMap(newKm); 754 newAM.setParent(am.getParent()); 755 am.setParent(newAM); 756 } 757 } 758 } 759 760 766 public Keymap getKeymap() { 767 return keymap; 768 } 769 770 785 public static Keymap addKeymap(String nm, Keymap parent) { 786 Keymap map = new DefaultKeymap(nm, parent); 787 if (nm != null) { 788 getKeymapTable().put(nm, map); 790 } 791 return map; 792 } 793 794 801 public static Keymap removeKeymap(String nm) { 802 return getKeymapTable().remove(nm); 803 } 804 805 812 public static Keymap getKeymap(String nm) { 813 return getKeymapTable().get(nm); 814 } 815 816 private static HashMap <String ,Keymap > getKeymapTable() { 817 AppContext appContext = AppContext.getAppContext(); 818 HashMap <String ,Keymap > keymapTable = 819 (HashMap <String ,Keymap >)appContext.get(KEYMAP_TABLE); 820 if (keymapTable == null) { 821 keymapTable = new HashMap <String ,Keymap >(17); 822 appContext.put(KEYMAP_TABLE, keymapTable); 823 Keymap binding = addKeymap(DEFAULT_KEYMAP, null); 825 binding.setDefaultAction(new 826 DefaultEditorKit.DefaultKeyTypedAction ()); 827 } 828 return keymapTable; 829 } 830 831 843 public static class KeyBinding { 844 845 848 public KeyStroke key; 849 850 853 public String actionName; 854 855 861 public KeyBinding(KeyStroke key, String actionName) { 862 this.key = key; 863 this.actionName = actionName; 864 } 865 } 866 867 902 public static void loadKeymap(Keymap map, KeyBinding[] bindings, Action [] actions) { 903 Hashtable h = new Hashtable (); 904 for (int i = 0; i < actions.length; i++) { 905 Action a = actions[i]; 906 String value = (String )a.getValue(Action.NAME); 907 h.put((value!=null ? value:""), a); 908 } 909 for (int i = 0; i < bindings.length; i++) { 910 Action a = (Action ) h.get(bindings[i].actionName); 911 if (a != null) { 912 map.addActionForKeyStroke(bindings[i].key, a); 913 } 914 } 915 } 916 917 924 private static Boolean isProcessInputMethodEventOverridden(Class klass) { 925 if (klass == JTextComponent .class) { 926 return Boolean.FALSE; 927 } 928 Boolean retValue = (Boolean )overrideMap.get(klass.getName()); 929 930 if (retValue != null) { 931 return retValue; 932 } 933 Boolean sOverriden = isProcessInputMethodEventOverridden( 934 klass.getSuperclass()); 935 936 if (sOverriden.booleanValue()) { 937 overrideMap.put(klass.getName(), sOverriden); 940 return sOverriden; 941 } 942 try { 945 Class [] classes = new Class [1]; 946 classes[0] = InputMethodEvent.class; 947 948 Method m = klass.getDeclaredMethod("processInputMethodEvent", 949 classes); 950 retValue = Boolean.TRUE; 951 } catch (NoSuchMethodException nsme) { 952 retValue = Boolean.FALSE; 953 } 954 overrideMap.put(klass.getName(), retValue); 955 return retValue; 956 } 957 958 964 public Color getCaretColor() { 965 return caretColor; 966 } 967 968 981 public void setCaretColor(Color c) { 982 Color old = caretColor; 983 caretColor = c; 984 firePropertyChange("caretColor", old, caretColor); 985 } 986 987 993 public Color getSelectionColor() { 994 return selectionColor; 995 } 996 997 |