1 7 package java.awt; 8 9 import java.awt.peer.TextComponentPeer; 10 import java.awt.event.*; 11 import java.util.EventListener ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 import sun.awt.InputMethodSupport; 16 import java.text.BreakIterator ; 17 import javax.swing.text.AttributeSet ; 18 import javax.accessibility.*; 19 import java.awt.im.InputMethodRequests ; 20 21 22 43 public class TextComponent extends Component implements Accessible { 44 45 53 String text; 54 55 64 boolean editable = true; 65 66 75 int selectionStart; 76 77 86 int selectionEnd; 87 88 boolean backgroundSetByClientCode = false; 92 93 97 transient private boolean canAccessClipboard; 98 99 transient protected TextListener textListener; 100 101 104 private static final long serialVersionUID = -2214773872412987419L; 105 106 119 TextComponent(String text) throws HeadlessException { 120 GraphicsEnvironment.checkHeadless(); 121 this.text = (text != null) ? text : ""; 122 setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 123 checkSystemClipboardAccess(); 124 } 125 126 private void enableInputMethodsIfNecessary() { 127 if (checkForEnableIM) { 128 checkForEnableIM = false; 129 try { 130 Toolkit toolkit = Toolkit.getDefaultToolkit(); 131 boolean shouldEnable = false; 132 if (toolkit instanceof InputMethodSupport) { 133 shouldEnable = ((InputMethodSupport)toolkit) 134 .enableInputMethodsForTextComponent(); 135 } 136 enableInputMethods(shouldEnable); 137 } catch (Exception e) { 138 } 140 } 141 } 142 143 public void enableInputMethods(boolean enable) { 144 checkForEnableIM = false; 145 super.enableInputMethods(enable); 146 } 147 148 boolean areInputMethodsEnabled() { 149 if (checkForEnableIM) { 152 enableInputMethodsIfNecessary(); 153 } 154 155 return (eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0; 158 } 159 160 public InputMethodRequests getInputMethodRequests() { 161 TextComponentPeer peer = (TextComponentPeer)this.peer; 162 if (peer != null) return peer.getInputMethodRequests(); 163 else return null; 164 } 165 166 167 168 175 public void addNotify() { 176 super.addNotify(); 177 enableInputMethodsIfNecessary(); 178 } 179 180 186 public void removeNotify() { 187 synchronized (getTreeLock()) { 188 TextComponentPeer peer = (TextComponentPeer)this.peer; 189 if (peer != null) { 190 text = peer.getText(); 191 selectionStart = peer.getSelectionStart(); 192 selectionEnd = peer.getSelectionEnd(); 193 } 194 super.removeNotify(); 195 } 196 } 197 198 206 public synchronized void setText(String t) { 207 text = (t != null) ? t : ""; 208 TextComponentPeer peer = (TextComponentPeer)this.peer; 209 if (peer != null) { 210 peer.setText(text); 211 } 212 } 213 214 221 public synchronized String getText() { 222 TextComponentPeer peer = (TextComponentPeer)this.peer; 223 if (peer != null) { 224 text = peer.getText(); 225 } 226 return text; 227 } 228 229 235 public synchronized String getSelectedText() { 236 return getText().substring(getSelectionStart(), getSelectionEnd()); 237 } 238 239 246 public boolean isEditable() { 247 return editable; 248 } 249 250 266 public synchronized void setEditable(boolean b) { 267 if (editable == b) { 268 return; 269 } 270 271 editable = b; 272 TextComponentPeer peer = (TextComponentPeer)this.peer; 273 if (peer != null) { 274 peer.setEditable(b); 275 } 276 } 277 278 291 public Color getBackground() { 292 if (!editable && !backgroundSetByClientCode) { 293 return SystemColor.control; 294 } 295 296 return super.getBackground(); 297 } 298 299 308 public void setBackground(Color c) { 309 backgroundSetByClientCode = true; 310 super.setBackground(c); 311 } 312 313 320 public synchronized int getSelectionStart() { 321 TextComponentPeer peer = (TextComponentPeer)this.peer; 322 if (peer != null) { 323 selectionStart = peer.getSelectionStart(); 324 } 325 return selectionStart; 326 } 327 328 343 public synchronized void setSelectionStart(int selectionStart) { 344 347 select(selectionStart, getSelectionEnd()); 348 } 349 350 357 public synchronized int getSelectionEnd() { 358 TextComponentPeer peer = (TextComponentPeer)this.peer; 359 if (peer != null) { 360 selectionEnd = peer.getSelectionEnd(); 361 } 362 return selectionEnd; 363 } 364 365 379 public synchronized void setSelectionEnd(int selectionEnd) { 380 383 select(getSelectionStart(), selectionEnd); 384 } 385 386 418 public synchronized void select(int selectionStart, int selectionEnd) { 419 String text = getText(); 420 if (selectionStart < 0) { 421 selectionStart = 0; 422 } 423 if (selectionStart > text.length()) { 424 selectionStart = text.length(); 425 } 426 if (selectionEnd > text.length()) { 427 selectionEnd = text.length(); 428 } 429 if (selectionEnd < selectionStart) { 430 selectionEnd = selectionStart; 431 } 432 433 this.selectionStart = selectionStart; 434 this.selectionEnd = selectionEnd; 435 436 TextComponentPeer peer = (TextComponentPeer)this.peer; 437 if (peer != null) { 438 peer.select(selectionStart, selectionEnd); 439 } 440 } 441 442 446 public synchronized void selectAll() { 447 String text = getText(); 448 this.selectionStart = 0; 449 this.selectionEnd = getText().length(); 450 451 TextComponentPeer peer = (TextComponentPeer)this.peer; 452 if (peer != null) { 453 peer.select(selectionStart, selectionEnd); 454 } 455 } 456 457 473 public synchronized void setCaretPosition(int position) { 474 if (position < 0) { 475 throw new IllegalArgumentException ("position less than zero."); 476 } 477 478 int maxposition = getText().length(); 479 if (position > maxposition) { 480 position = maxposition; 481 } 482 483 TextComponentPeer peer = (TextComponentPeer)this.peer; 484 if (peer != null) { 485 peer.setCaretPosition(position); 486 } else { 487 select(position, position); 488 } 489 } 490 491 502 public synchronized int getCaretPosition() { 503 TextComponentPeer peer = (TextComponentPeer)this.peer; 504 int position = 0; 505 506 if (peer != null) { 507 position = peer.getCaretPosition(); 508 } else { 509 position = selectionStart; 510 } 511 return position; 512 } 513 514 525 public synchronized void addTextListener(TextListener l) { 526 if (l == null) { 527 return; 528 } 529 textListener = AWTEventMulticaster.add(textListener, l); 530 newEventsOnly = true; 531 } 532 533 545 public synchronized void removeTextListener(TextListener l) { 546 if (l == null) { 547 return; 548 } 549 textListener = AWTEventMulticaster.remove(textListener, l); 550 } 551 552 565 public synchronized TextListener[] getTextListeners() { 566 return (TextListener[])(getListeners(TextListener.class)); 567 } 568 569 602 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 603 EventListener l = null; 604 if (listenerType == TextListener.class) { 605 l = textListener; 606 } else { 607 return super.getListeners(listenerType); 608 } 609 return AWTEventMulticaster.getListeners(l, listenerType); 610 } 611 612 boolean eventEnabled(AWTEvent e) { 614 if (e.id == TextEvent.TEXT_VALUE_CHANGED) { 615 if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 || 616 textListener != null) { 617 return true; 618 } 619 return false; 620 } 621 return super.eventEnabled(e); 622 } 623 624 634 protected void processEvent(AWTEvent e) { 635 if (e instanceof TextEvent) { 636 processTextEvent((TextEvent)e); 637 return; 638 } 639 super.processEvent(e); 640 } 641 642 661 protected void processTextEvent(TextEvent e) { 662 TextListener listener = textListener; 663 if (listener != null) { 664 int id = e.getID(); 665 switch (id) { 666 case TextEvent.TEXT_VALUE_CHANGED: 667 listener.textValueChanged(e); 668 break; 669 } 670 } 671 } 672 673 683 protected String paramString() { 684 String str = super.paramString() + ",text=" + getText(); 685 if (editable) { 686 str += ",editable"; 687 } 688 return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd(); 689 } 690 691 694 private void checkSystemClipboardAccess() { 695 canAccessClipboard = true; 696 SecurityManager sm = System.getSecurityManager(); 697 if (sm != null) { 698 try { 699 sm.checkSystemClipboardAccess(); 700 } 701 catch (SecurityException e) { 702 canAccessClipboard = false; 703 } 704 } 705 } 706 707 710 715 private int textComponentSerializedDataVersion = 1; 716 717 732 private void writeObject(java.io.ObjectOutputStream s) 733 throws IOException 734 { 735 TextComponentPeer peer = (TextComponentPeer)this.peer; 739 if (peer != null) { 740 text = peer.getText(); 741 selectionStart = peer.getSelectionStart(); 742 selectionEnd = peer.getSelectionEnd(); 743 } 744 745 s.defaultWriteObject(); 746 747 AWTEventMulticaster.save(s, textListenerK, textListener); 748 s.writeObject(null); 749 } 750 751 764 private void readObject(ObjectInputStream s) 765 throws ClassNotFoundException , IOException , HeadlessException 766 { 767 GraphicsEnvironment.checkHeadless(); 768 s.defaultReadObject(); 769 770 this.text = (text != null) ? text : ""; 773 select(selectionStart, selectionEnd); 774 775 Object keyOrNull; 776 while(null != (keyOrNull = s.readObject())) { 777 String key = ((String )keyOrNull).intern(); 778 779 if (textListenerK == key) { 780 addTextListener((TextListener)(s.readObject())); 781 } else { 782 s.readObject(); 784 } 785 } 786 enableInputMethodsIfNecessary(); 787 checkSystemClipboardAccess(); 788 } 789 790 791 795 796 799 int getIndexAtPoint(Point p) { 800 return -1; 801 808 } 809 810 811 814 Rectangle getCharacterBounds(int i) { 815 return null; 816 823 } 824 825 826 835 public AccessibleContext getAccessibleContext() { 836 if (accessibleContext == null) { 837 accessibleContext = new AccessibleAWTTextComponent(); 838 } 839 return accessibleContext; 840 } 841 842 848 protected class AccessibleAWTTextComponent extends AccessibleAWTComponent 849 implements AccessibleText, TextListener 850 { 851 854 private static final long serialVersionUID = 3631432373506317811L; 855 856 860 public AccessibleAWTTextComponent() { 861 TextComponent.this.addTextListener(this); 862 } 863 864 867 public void textValueChanged(TextEvent textEvent) { 868 Integer cpos = new Integer (TextComponent.this.getCaretPosition()); 869 firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, cpos); 870 } 871 872 885 public AccessibleStateSet getAccessibleStateSet() { 886 AccessibleStateSet states = super.getAccessibleStateSet(); 887 if (TextComponent.this.isEditable()) { 888 states.add(AccessibleState.EDITABLE); 889 } 890 return states; 891 } 892 893 894 901 public AccessibleRole getAccessibleRole() { 902 return AccessibleRole.TEXT; 903 } 904 905 913 public AccessibleText getAccessibleText() { 914 return this; 915 } 916 917 918 920 924 925 933 public int getIndexAtPoint(Point p) { 934 return TextComponent.this.getIndexAtPoint(p); 935 } 936 937 946 public Rectangle getCharacterBounds(int i) { 947 return TextComponent.this.getCharacterBounds(i); 948 } 949 950 955 public int getCharCount() { 956 return TextComponent.this.getText().length(); 957 } 958 959 968 public int getCaretPosition() { 969 return TextComponent.this.getCaretPosition(); 970 } 971 972 978 public AttributeSet getCharacterAttribute(int i) { 979 return null; } 981 982 991 public int getSelectionStart() { 992 return TextComponent.this.getSelectionStart(); 993 } 994 995 1004 public int getSelectionEnd() { 1005 return TextComponent.this.getSelectionEnd(); 1006 } 1007 1008 1013 public String getSelectedText() { 1014 String selText = TextComponent.this.getSelectedText(); 1015 if (selText == null || selText.equals("")) { 1017 return null; 1018 } 1019 return selText; 1020 } 1021 1022 1031 public String getAtIndex(int part, int index) { 1032 if (index < 0 || index >= TextComponent.this.getText().length()) { 1033 return null; 1034 } 1035 switch (part) { 1036 case AccessibleText.CHARACTER: 1037 return TextComponent.this.getText().substring(index, index+1); 1038 case AccessibleText.WORD: { 1039 String s = TextComponent.this.getText(); 1040 BreakIterator words = BreakIterator.getWordInstance(); 1041 words.setText(s); 1042 int end = words.following(index); 1043 return s.substring(words.previous(), end); 1044 } 1045 case AccessibleText.SENTENCE: { 1046 String s = TextComponent.this.getText(); 1047 BreakIterator sentence = BreakIterator.getSentenceInstance(); 1048 sentence.setText(s); 1049 int end = sentence.following(index); 1050 return s.substring(sentence.previous(), end); 1051 } 1052 default: 1053 return null; 1054 } 1055 } 1056 1057 private static final boolean NEXT = true; 1058 private static final boolean PREVIOUS = false; 1059 1060 1064 private int findWordLimit(int index, BreakIterator words, boolean direction, 1065 String s) { 1066 int last = (direction == NEXT) ? words.following(index) 1072 : words.preceding(index); 1073 int current = (direction == NEXT) ? words.next() 1074 : words.previous(); 1075 while (current != BreakIterator.DONE) { 1076 for (int p = Math.min(last, current); p < Math.max(last, current); p++) { 1077 if (Character.isLetter(s.charAt(p))) { 1078 return last; 1079 } 1080 } 1081 last = current; 1082 current = (direction == NEXT) ? words.next() 1083 : words.previous(); 1084 } 1085 return BreakIterator.DONE; 1086 } 1087 1088 1097 public String getAfterIndex(int part, int index) { 1098 if (index < 0 || index >= TextComponent.this.getText().length()) { 1099 return null; 1100 } 1101 switch (part) { 1102 case AccessibleText.CHARACTER: 1103 if (index+1 >= TextComponent.this.getText().length()) { 1104 return null; 1105 } 1106 return TextComponent.this.getText().substring(index+1, index+2); 1107 case AccessibleText.WORD: { 1108 String s = TextComponent.this.getText(); 1109 BreakIterator words = BreakIterator.getWordInstance(); 1110 words.setText(s); 1111 int start = findWordLimit(index, words, NEXT, s); 1112 if (start == BreakIterator.DONE || start >= s.length()) { 1113 return null; 1114 } 1115 int end = words.following(start); 1116 if (end == BreakIterator.DONE || end >= s.length()) { 1117 return null; 1118 } 1119 return s.substring(start, end); 1120 } 1121 case AccessibleText.SENTENCE: { 1122 String s = TextComponent.this.getText(); 1123 BreakIterator sentence = BreakIterator.getSentenceInstance(); 1124 sentence.setText(s); 1125 int start = sentence.following(index); 1126 if (start == BreakIterator.DONE || start >= s.length()) { 1127 return null; 1128 } 1129 int end = sentence.following(start); 1130 if (end == BreakIterator.DONE || end >= s.length()) { 1131 return null; 1132 } 1133 return s.substring(start, end); 1134 } 1135 default: 1136 return null; 1137 } 1138 } 1139 1140 1141 1150 public String getBeforeIndex(int part, int index) { 1151 if (index < 0 || index > TextComponent.this.getText().length()-1) { 1152 return null; 1153 } 1154 switch (part) { 1155 case AccessibleText.CHARACTER: 1156 if (index == 0) { 1157 return null; 1158 } 1159 return TextComponent.this.getText().substring(index-1, index); 1160 case AccessibleText.WORD: { 1161 String s = TextComponent.this.getText(); 1162 BreakIterator words = BreakIterator.getWordInstance(); 1163 words.setText(s); 1164 int end = findWordLimit(index, words, PREVIOUS, s); 1165 if (end == BreakIterator.DONE) { 1166 return null; 1167 } 1168 int start = words.preceding(end); 1169 if (start == BreakIterator.DONE) { 1170 return null; 1171 } 1172 return s.substring(start, end); 1173 } 1174 case AccessibleText.SENTENCE: { 1175 String s = TextComponent.this.getText(); 1176 BreakIterator sentence = BreakIterator.getSentenceInstance(); 1177 sentence.setText(s); 1178 int end = sentence.following(index); 1179 end = sentence.previous(); 1180 int start = sentence.previous(); 1181 if (start == BreakIterator.DONE) { 1182 return null; 1183 } 1184 return s.substring(start, end); 1185 } 1186 default: 1187 return null; 1188 } 1189 } 1190 } 1192 private boolean checkForEnableIM = true; 1193} 1194 | Popular Tags |