开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from Stefan/Java8Source
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (2)
master
develop
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (2)
master
develop
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (2)
master
develop
Java8Source
/
src
/
java
/
awt
/
TextComponent.java
Java8Source
/
src
/
java
/
awt
/
TextComponent.java
TextComponent.java 42.12 KB
一键复制 编辑 原始数据 按行查看 历史
Stefan 提交于 2020年08月11日 23:17 +08:00 . 初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt;
import java.awt.peer.TextComponentPeer;
import java.awt.event.*;
import java.util.EventListener;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import sun.awt.InputMethodSupport;
import java.text.BreakIterator;
import javax.swing.text.AttributeSet;
import javax.accessibility.*;
import java.awt.im.InputMethodRequests;
import sun.security.util.SecurityConstants;
/**
* The <code>TextComponent</code> class is the superclass of
* any component that allows the editing of some text.
* <p>
* A text component embodies a string of text. The
* <code>TextComponent</code> class defines a set of methods
* that determine whether or not this text is editable. If the
* component is editable, it defines another set of methods
* that supports a text insertion caret.
* <p>
* In addition, the class defines methods that are used
* to maintain a current <em>selection</em> from the text.
* The text selection, a substring of the component's text,
* is the target of editing operations. It is also referred
* to as the <em>selected text</em>.
*
* @author Sami Shaio
* @author Arthur van Hoff
* @since JDK1.0
*/
public class TextComponent extends Component implements Accessible {
/**
* The value of the text.
* A <code>null</code> value is the same as "".
*
* @serial
* @see #setText(String)
* @see #getText()
*/
String text;
/**
* A boolean indicating whether or not this
* <code>TextComponent</code> is editable.
* It will be <code>true</code> if the text component
* is editable and <code>false</code> if not.
*
* @serial
* @see #isEditable()
*/
boolean editable = true;
/**
* The selection refers to the selected text, and the
* <code>selectionStart</code> is the start position
* of the selected text.
*
* @serial
* @see #getSelectionStart()
* @see #setSelectionStart(int)
*/
int selectionStart;
/**
* The selection refers to the selected text, and the
* <code>selectionEnd</code>
* is the end position of the selected text.
*
* @serial
* @see #getSelectionEnd()
* @see #setSelectionEnd(int)
*/
int selectionEnd;
// A flag used to tell whether the background has been set by
// developer code (as opposed to AWT code). Used to determine
// the background color of non-editable TextComponents.
boolean backgroundSetByClientCode = false;
transient protected TextListener textListener;
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = -2214773872412987419L;
/**
* Constructs a new text component initialized with the
* specified text. Sets the value of the cursor to
* <code>Cursor.TEXT_CURSOR</code>.
* @param text the text to be displayed; if
* <code>text</code> is <code>null</code>, the empty
* string <code>""</code> will be displayed
* @exception HeadlessException if
* <code>GraphicsEnvironment.isHeadless</code>
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
* @see java.awt.Cursor
*/
TextComponent(String text) throws HeadlessException {
GraphicsEnvironment.checkHeadless();
this.text = (text != null) ? text : "";
setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
private void enableInputMethodsIfNecessary() {
if (checkForEnableIM) {
checkForEnableIM = false;
try {
Toolkit toolkit = Toolkit.getDefaultToolkit();
boolean shouldEnable = false;
if (toolkit instanceof InputMethodSupport) {
shouldEnable = ((InputMethodSupport)toolkit)
.enableInputMethodsForTextComponent();
}
enableInputMethods(shouldEnable);
} catch (Exception e) {
// if something bad happens, just don't enable input methods
}
}
}
/**
* Enables or disables input method support for this text component. If input
* method support is enabled and the text component also processes key events,
* incoming events are offered to the current input method and will only be
* processed by the component or dispatched to its listeners if the input method
* does not consume them. Whether and how input method support for this text
* component is enabled or disabled by default is implementation dependent.
*
* @param enable true to enable, false to disable
* @see #processKeyEvent
* @since 1.2
*/
public void enableInputMethods(boolean enable) {
checkForEnableIM = false;
super.enableInputMethods(enable);
}
boolean areInputMethodsEnabled() {
// moved from the constructor above to here and addNotify below,
// this call will initialize the toolkit if not already initialized.
if (checkForEnableIM) {
enableInputMethodsIfNecessary();
}
// TextComponent handles key events without touching the eventMask or
// having a key listener, so just check whether the flag is set
return (eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0;
}
public InputMethodRequests getInputMethodRequests() {
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) return peer.getInputMethodRequests();
else return null;
}
/**
* Makes this Component displayable by connecting it to a
* native screen resource.
* This method is called internally by the toolkit and should
* not be called directly by programs.
* @see java.awt.TextComponent#removeNotify
*/
public void addNotify() {
super.addNotify();
enableInputMethodsIfNecessary();
}
/**
* Removes the <code>TextComponent</code>'s peer.
* The peer allows us to modify the appearance of the
* <code>TextComponent</code> without changing its
* functionality.
*/
public void removeNotify() {
synchronized (getTreeLock()) {
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
text = peer.getText();
selectionStart = peer.getSelectionStart();
selectionEnd = peer.getSelectionEnd();
}
super.removeNotify();
}
}
/**
* Sets the text that is presented by this
* text component to be the specified text.
* @param t the new text;
* if this parameter is <code>null</code> then
* the text is set to the empty string ""
* @see java.awt.TextComponent#getText
*/
public synchronized void setText(String t) {
boolean skipTextEvent = (text == null || text.isEmpty())
&& (t == null || t.isEmpty());
text = (t != null) ? t : "";
TextComponentPeer peer = (TextComponentPeer)this.peer;
// Please note that we do not want to post an event
// if TextArea.setText() or TextField.setText() replaces an empty text
// by an empty text, that is, if component's text remains unchanged.
if (peer != null && !skipTextEvent) {
peer.setText(text);
}
}
/**
* Returns the text that is presented by this text component.
* By default, this is an empty string.
*
* @return the value of this <code>TextComponent</code>
* @see java.awt.TextComponent#setText
*/
public synchronized String getText() {
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
text = peer.getText();
}
return text;
}
/**
* Returns the selected text from the text that is
* presented by this text component.
* @return the selected text of this text component
* @see java.awt.TextComponent#select
*/
public synchronized String getSelectedText() {
return getText().substring(getSelectionStart(), getSelectionEnd());
}
/**
* Indicates whether or not this text component is editable.
* @return <code>true</code> if this text component is
* editable; <code>false</code> otherwise.
* @see java.awt.TextComponent#setEditable
* @since JDK1.0
*/
public boolean isEditable() {
return editable;
}
/**
* Sets the flag that determines whether or not this
* text component is editable.
* <p>
* If the flag is set to <code>true</code>, this text component
* becomes user editable. If the flag is set to <code>false</code>,
* the user cannot change the text of this text component.
* By default, non-editable text components have a background color
* of SystemColor.control. This default can be overridden by
* calling setBackground.
*
* @param b a flag indicating whether this text component
* is user editable.
* @see java.awt.TextComponent#isEditable
* @since JDK1.0
*/
public synchronized void setEditable(boolean b) {
if (editable == b) {
return;
}
editable = b;
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.setEditable(b);
}
}
/**
* Gets the background color of this text component.
*
* By default, non-editable text components have a background color
* of SystemColor.control. This default can be overridden by
* calling setBackground.
*
* @return This text component's background color.
* If this text component does not have a background color,
* the background color of its parent is returned.
* @see #setBackground(Color)
* @since JDK1.0
*/
public Color getBackground() {
if (!editable && !backgroundSetByClientCode) {
return SystemColor.control;
}
return super.getBackground();
}
/**
* Sets the background color of this text component.
*
* @param c The color to become this text component's color.
* If this parameter is null then this text component
* will inherit the background color of its parent.
* @see #getBackground()
* @since JDK1.0
*/
public void setBackground(Color c) {
backgroundSetByClientCode = true;
super.setBackground(c);
}
/**
* Gets the start position of the selected text in
* this text component.
* @return the start position of the selected text
* @see java.awt.TextComponent#setSelectionStart
* @see java.awt.TextComponent#getSelectionEnd
*/
public synchronized int getSelectionStart() {
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
selectionStart = peer.getSelectionStart();
}
return selectionStart;
}
/**
* Sets the selection start for this text component to
* the specified position. The new start point is constrained
* to be at or before the current selection end. It also
* cannot be set to less than zero, the beginning of the
* component's text.
* If the caller supplies a value for <code>selectionStart</code>
* that is out of bounds, the method enforces these constraints
* silently, and without failure.
* @param selectionStart the start position of the
* selected text
* @see java.awt.TextComponent#getSelectionStart
* @see java.awt.TextComponent#setSelectionEnd
* @since JDK1.1
*/
public synchronized void setSelectionStart(int selectionStart) {
/* Route through select method to enforce consistent policy
* between selectionStart and selectionEnd.
*/
select(selectionStart, getSelectionEnd());
}
/**
* Gets the end position of the selected text in
* this text component.
* @return the end position of the selected text
* @see java.awt.TextComponent#setSelectionEnd
* @see java.awt.TextComponent#getSelectionStart
*/
public synchronized int getSelectionEnd() {
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
selectionEnd = peer.getSelectionEnd();
}
return selectionEnd;
}
/**
* Sets the selection end for this text component to
* the specified position. The new end point is constrained
* to be at or after the current selection start. It also
* cannot be set beyond the end of the component's text.
* If the caller supplies a value for <code>selectionEnd</code>
* that is out of bounds, the method enforces these constraints
* silently, and without failure.
* @param selectionEnd the end position of the
* selected text
* @see java.awt.TextComponent#getSelectionEnd
* @see java.awt.TextComponent#setSelectionStart
* @since JDK1.1
*/
public synchronized void setSelectionEnd(int selectionEnd) {
/* Route through select method to enforce consistent policy
* between selectionStart and selectionEnd.
*/
select(getSelectionStart(), selectionEnd);
}
/**
* Selects the text between the specified start and end positions.
* <p>
* This method sets the start and end positions of the
* selected text, enforcing the restriction that the start position
* must be greater than or equal to zero. The end position must be
* greater than or equal to the start position, and less than or
* equal to the length of the text component's text. The
* character positions are indexed starting with zero.
* The length of the selection is
* <code>endPosition</code> - <code>startPosition</code>, so the
* character at <code>endPosition</code> is not selected.
* If the start and end positions of the selected text are equal,
* all text is deselected.
* <p>
* If the caller supplies values that are inconsistent or out of
* bounds, the method enforces these constraints silently, and
* without failure. Specifically, if the start position or end
* position is greater than the length of the text, it is reset to
* equal the text length. If the start position is less than zero,
* it is reset to zero, and if the end position is less than the
* start position, it is reset to the start position.
*
* @param selectionStart the zero-based index of the first
character (<code>char</code> value) to be selected
* @param selectionEnd the zero-based end position of the
text to be selected; the character (<code>char</code> value) at
<code>selectionEnd</code> is not selected
* @see java.awt.TextComponent#setSelectionStart
* @see java.awt.TextComponent#setSelectionEnd
* @see java.awt.TextComponent#selectAll
*/
public synchronized void select(int selectionStart, int selectionEnd) {
String text = getText();
if (selectionStart < 0) {
selectionStart = 0;
}
if (selectionStart > text.length()) {
selectionStart = text.length();
}
if (selectionEnd > text.length()) {
selectionEnd = text.length();
}
if (selectionEnd < selectionStart) {
selectionEnd = selectionStart;
}
this.selectionStart = selectionStart;
this.selectionEnd = selectionEnd;
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.select(selectionStart, selectionEnd);
}
}
/**
* Selects all the text in this text component.
* @see java.awt.TextComponent#select
*/
public synchronized void selectAll() {
this.selectionStart = 0;
this.selectionEnd = getText().length();
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.select(selectionStart, selectionEnd);
}
}
/**
* Sets the position of the text insertion caret.
* The caret position is constrained to be between 0
* and the last character of the text, inclusive.
* If the passed-in value is greater than this range,
* the value is set to the last character (or 0 if
* the <code>TextComponent</code> contains no text)
* and no error is returned. If the passed-in value is
* less than 0, an <code>IllegalArgumentException</code>
* is thrown.
*
* @param position the position of the text insertion caret
* @exception IllegalArgumentException if <code>position</code>
* is less than zero
* @since JDK1.1
*/
public synchronized void setCaretPosition(int position) {
if (position < 0) {
throw new IllegalArgumentException("position less than zero.");
}
int maxposition = getText().length();
if (position > maxposition) {
position = maxposition;
}
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
peer.setCaretPosition(position);
} else {
select(position, position);
}
}
/**
* Returns the position of the text insertion caret.
* The caret position is constrained to be between 0
* and the last character of the text, inclusive.
* If the text or caret have not been set, the default
* caret position is 0.
*
* @return the position of the text insertion caret
* @see #setCaretPosition(int)
* @since JDK1.1
*/
public synchronized int getCaretPosition() {
TextComponentPeer peer = (TextComponentPeer)this.peer;
int position = 0;
if (peer != null) {
position = peer.getCaretPosition();
} else {
position = selectionStart;
}
int maxposition = getText().length();
if (position > maxposition) {
position = maxposition;
}
return position;
}
/**
* Adds the specified text event listener to receive text events
* from this text component.
* If <code>l</code> is <code>null</code>, no exception is
* thrown and no action is performed.
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
* >AWT Threading Issues</a> for details on AWT's threading model.
*
* @param l the text event listener
* @see #removeTextListener
* @see #getTextListeners
* @see java.awt.event.TextListener
*/
public synchronized void addTextListener(TextListener l) {
if (l == null) {
return;
}
textListener = AWTEventMulticaster.add(textListener, l);
newEventsOnly = true;
}
/**
* Removes the specified text event listener so that it no longer
* receives text events from this text component
* If <code>l</code> is <code>null</code>, no exception is
* thrown and no action is performed.
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
* >AWT Threading Issues</a> for details on AWT's threading model.
*
* @param l the text listener
* @see #addTextListener
* @see #getTextListeners
* @see java.awt.event.TextListener
* @since JDK1.1
*/
public synchronized void removeTextListener(TextListener l) {
if (l == null) {
return;
}
textListener = AWTEventMulticaster.remove(textListener, l);
}
/**
* Returns an array of all the text listeners
* registered on this text component.
*
* @return all of this text component's <code>TextListener</code>s
* or an empty array if no text
* listeners are currently registered
*
*
* @see #addTextListener
* @see #removeTextListener
* @since 1.4
*/
public synchronized TextListener[] getTextListeners() {
return getListeners(TextListener.class);
}
/**
* Returns an array of all the objects currently registered
* as <code><em>Foo</em>Listener</code>s
* upon this <code>TextComponent</code>.
* <code><em>Foo</em>Listener</code>s are registered using the
* <code>add<em>Foo</em>Listener</code> method.
*
* <p>
* You can specify the <code>listenerType</code> argument
* with a class literal, such as
* <code><em>Foo</em>Listener.class</code>.
* For example, you can query a
* <code>TextComponent</code> <code>t</code>
* for its text listeners with the following code:
*
* <pre>TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));</pre>
*
* If no such listeners exist, this method returns an empty array.
*
* @param listenerType the type of listeners requested; this parameter
* should specify an interface that descends from
* <code>java.util.EventListener</code>
* @return an array of all objects registered as
* <code><em>Foo</em>Listener</code>s on this text component,
* or an empty array if no such
* listeners have been added
* @exception ClassCastException if <code>listenerType</code>
* doesn't specify a class or interface that implements
* <code>java.util.EventListener</code>
*
* @see #getTextListeners
* @since 1.3
*/
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
EventListener l = null;
if (listenerType == TextListener.class) {
l = textListener;
} else {
return super.getListeners(listenerType);
}
return AWTEventMulticaster.getListeners(l, listenerType);
}
// REMIND: remove when filtering is done at lower level
boolean eventEnabled(AWTEvent e) {
if (e.id == TextEvent.TEXT_VALUE_CHANGED) {
if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 ||
textListener != null) {
return true;
}
return false;
}
return super.eventEnabled(e);
}
/**
* Processes events on this text component. If the event is a
* <code>TextEvent</code>, it invokes the <code>processTextEvent</code>
* method else it invokes its superclass's <code>processEvent</code>.
* <p>Note that if the event parameter is <code>null</code>
* the behavior is unspecified and may result in an
* exception.
*
* @param e the event
*/
protected void processEvent(AWTEvent e) {
if (e instanceof TextEvent) {
processTextEvent((TextEvent)e);
return;
}
super.processEvent(e);
}
/**
* Processes text events occurring on this text component by
* dispatching them to any registered <code>TextListener</code> objects.
* <p>
* NOTE: This method will not be called unless text events
* are enabled for this component. This happens when one of the
* following occurs:
* <ul>
* <li>A <code>TextListener</code> object is registered
* via <code>addTextListener</code>
* <li>Text events are enabled via <code>enableEvents</code>
* </ul>
* <p>Note that if the event parameter is <code>null</code>
* the behavior is unspecified and may result in an
* exception.
*
* @param e the text event
* @see Component#enableEvents
*/
protected void processTextEvent(TextEvent e) {
TextListener listener = textListener;
if (listener != null) {
int id = e.getID();
switch (id) {
case TextEvent.TEXT_VALUE_CHANGED:
listener.textValueChanged(e);
break;
}
}
}
/**
* Returns a string representing the state of this
* <code>TextComponent</code>. This
* method is intended to be used only for debugging purposes, and the
* content and format of the returned string may vary between
* implementations. The returned string may be empty but may not be
* <code>null</code>.
*
* @return the parameter string of this text component
*/
protected String paramString() {
String str = super.paramString() + ",text=" + getText();
if (editable) {
str += ",editable";
}
return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
}
/**
* Assigns a valid value to the canAccessClipboard instance variable.
*/
private boolean canAccessClipboard() {
SecurityManager sm = System.getSecurityManager();
if (sm == null) return true;
try {
sm.checkPermission(SecurityConstants.AWT.ACCESS_CLIPBOARD_PERMISSION);
return true;
} catch (SecurityException e) {}
return false;
}
/*
* Serialization support.
*/
/**
* The textComponent SerializedDataVersion.
*
* @serial
*/
private int textComponentSerializedDataVersion = 1;
/**
* Writes default serializable fields to stream. Writes
* a list of serializable TextListener(s) as optional data.
* The non-serializable TextListener(s) are detected and
* no attempt is made to serialize them.
*
* @serialData Null terminated sequence of zero or more pairs.
* A pair consists of a String and Object.
* The String indicates the type of object and
* is one of the following :
* textListenerK indicating and TextListener object.
*
* @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
* @see java.awt.Component#textListenerK
*/
private void writeObject(java.io.ObjectOutputStream s)
throws IOException
{
// Serialization support. Since the value of the fields
// selectionStart, selectionEnd, and text aren't necessarily
// up to date, we sync them up with the peer before serializing.
TextComponentPeer peer = (TextComponentPeer)this.peer;
if (peer != null) {
text = peer.getText();
selectionStart = peer.getSelectionStart();
selectionEnd = peer.getSelectionEnd();
}
s.defaultWriteObject();
AWTEventMulticaster.save(s, textListenerK, textListener);
s.writeObject(null);
}
/**
* Read the ObjectInputStream, and if it isn't null,
* add a listener to receive text events fired by the
* TextComponent. Unrecognized keys or values will be
* ignored.
*
* @exception HeadlessException if
* <code>GraphicsEnvironment.isHeadless()</code> returns
* <code>true</code>
* @see #removeTextListener
* @see #addTextListener
* @see java.awt.GraphicsEnvironment#isHeadless
*/
private void readObject(ObjectInputStream s)
throws ClassNotFoundException, IOException, HeadlessException
{
GraphicsEnvironment.checkHeadless();
s.defaultReadObject();
// Make sure the state we just read in for text,
// selectionStart and selectionEnd has legal values
this.text = (text != null) ? text : "";
select(selectionStart, selectionEnd);
Object keyOrNull;
while(null != (keyOrNull = s.readObject())) {
String key = ((String)keyOrNull).intern();
if (textListenerK == key) {
addTextListener((TextListener)(s.readObject()));
} else {
// skip value for unrecognized key
s.readObject();
}
}
enableInputMethodsIfNecessary();
}
/////////////////
// Accessibility support
////////////////
/**
* Gets the AccessibleContext associated with this TextComponent.
* For text components, the AccessibleContext takes the form of an
* AccessibleAWTTextComponent.
* A new AccessibleAWTTextComponent instance is created if necessary.
*
* @return an AccessibleAWTTextComponent that serves as the
* AccessibleContext of this TextComponent
* @since 1.3
*/
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleAWTTextComponent();
}
return accessibleContext;
}
/**
* This class implements accessibility support for the
* <code>TextComponent</code> class. It provides an implementation of the
* Java Accessibility API appropriate to text component user-interface
* elements.
* @since 1.3
*/
protected class AccessibleAWTTextComponent extends AccessibleAWTComponent
implements AccessibleText, TextListener
{
/*
* JDK 1.3 serialVersionUID
*/
private static final long serialVersionUID = 3631432373506317811L;
/**
* Constructs an AccessibleAWTTextComponent. Adds a listener to track
* caret change.
*/
public AccessibleAWTTextComponent() {
TextComponent.this.addTextListener(this);
}
/**
* TextListener notification of a text value change.
*/
public void textValueChanged(TextEvent textEvent) {
Integer cpos = Integer.valueOf(TextComponent.this.getCaretPosition());
firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, cpos);
}
/**
* Gets the state set of the TextComponent.
* The AccessibleStateSet of an object is composed of a set of
* unique AccessibleStates. A change in the AccessibleStateSet
* of an object will cause a PropertyChangeEvent to be fired
* for the AccessibleContext.ACCESSIBLE_STATE_PROPERTY property.
*
* @return an instance of AccessibleStateSet containing the
* current state set of the object
* @see AccessibleStateSet
* @see AccessibleState
* @see #addPropertyChangeListener
*/
public AccessibleStateSet getAccessibleStateSet() {
AccessibleStateSet states = super.getAccessibleStateSet();
if (TextComponent.this.isEditable()) {
states.add(AccessibleState.EDITABLE);
}
return states;
}
/**
* Gets the role of this object.
*
* @return an instance of AccessibleRole describing the role of the
* object (AccessibleRole.TEXT)
* @see AccessibleRole
*/
public AccessibleRole getAccessibleRole() {
return AccessibleRole.TEXT;
}
/**
* Get the AccessibleText associated with this object. In the
* implementation of the Java Accessibility API for this class,
* return this object, which is responsible for implementing the
* AccessibleText interface on behalf of itself.
*
* @return this object
*/
public AccessibleText getAccessibleText() {
return this;
}
// --- interface AccessibleText methods ------------------------
/**
* Many of these methods are just convenience methods; they
* just call the equivalent on the parent
*/
/**
* Given a point in local coordinates, return the zero-based index
* of the character under that Point. If the point is invalid,
* this method returns -1.
*
* @param p the Point in local coordinates
* @return the zero-based index of the character under Point p.
*/
public int getIndexAtPoint(Point p) {
return -1;
}
/**
* Determines the bounding box of the character at the given
* index into the string. The bounds are returned in local
* coordinates. If the index is invalid a null rectangle
* is returned.
*
* @param i the index into the String &gt;= 0
* @return the screen coordinates of the character's bounding box
*/
public Rectangle getCharacterBounds(int i) {
return null;
}
/**
* Returns the number of characters (valid indicies)
*
* @return the number of characters &gt;= 0
*/
public int getCharCount() {
return TextComponent.this.getText().length();
}
/**
* Returns the zero-based offset of the caret.
*
* Note: The character to the right of the caret will have the
* same index value as the offset (the caret is between
* two characters).
*
* @return the zero-based offset of the caret.
*/
public int getCaretPosition() {
return TextComponent.this.getCaretPosition();
}
/**
* Returns the AttributeSet for a given character (at a given index).
*
* @param i the zero-based index into the text
* @return the AttributeSet of the character
*/
public AttributeSet getCharacterAttribute(int i) {
return null; // No attributes in TextComponent
}
/**
* Returns the start offset within the selected text.
* If there is no selection, but there is
* a caret, the start and end offsets will be the same.
* Return 0 if the text is empty, or the caret position
* if no selection.
*
* @return the index into the text of the start of the selection &gt;= 0
*/
public int getSelectionStart() {
return TextComponent.this.getSelectionStart();
}
/**
* Returns the end offset within the selected text.
* If there is no selection, but there is
* a caret, the start and end offsets will be the same.
* Return 0 if the text is empty, or the caret position
* if no selection.
*
* @return the index into the text of the end of the selection &gt;= 0
*/
public int getSelectionEnd() {
return TextComponent.this.getSelectionEnd();
}
/**
* Returns the portion of the text that is selected.
*
* @return the text, null if no selection
*/
public String getSelectedText() {
String selText = TextComponent.this.getSelectedText();
// Fix for 4256662
if (selText == null || selText.equals("")) {
return null;
}
return selText;
}
/**
* Returns the String at a given index.
*
* @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
* or AccessibleText.SENTENCE to retrieve
* @param index an index within the text &gt;= 0
* @return the letter, word, or sentence,
* null for an invalid index or part
*/
public String getAtIndex(int part, int index) {
if (index < 0 || index >= TextComponent.this.getText().length()) {
return null;
}
switch (part) {
case AccessibleText.CHARACTER:
return TextComponent.this.getText().substring(index, index+1);
case AccessibleText.WORD: {
String s = TextComponent.this.getText();
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int end = words.following(index);
return s.substring(words.previous(), end);
}
case AccessibleText.SENTENCE: {
String s = TextComponent.this.getText();
BreakIterator sentence = BreakIterator.getSentenceInstance();
sentence.setText(s);
int end = sentence.following(index);
return s.substring(sentence.previous(), end);
}
default:
return null;
}
}
private static final boolean NEXT = true;
private static final boolean PREVIOUS = false;
/**
* Needed to unify forward and backward searching.
* The method assumes that s is the text assigned to words.
*/
private int findWordLimit(int index, BreakIterator words, boolean direction,
String s) {
// Fix for 4256660 and 4256661.
// Words iterator is different from character and sentence iterators
// in that end of one word is not necessarily start of another word.
// Please see java.text.BreakIterator JavaDoc. The code below is
// based on nextWordStartAfter example from BreakIterator.java.
int last = (direction == NEXT) ? words.following(index)
: words.preceding(index);
int current = (direction == NEXT) ? words.next()
: words.previous();
while (current != BreakIterator.DONE) {
for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
if (Character.isLetter(s.charAt(p))) {
return last;
}
}
last = current;
current = (direction == NEXT) ? words.next()
: words.previous();
}
return BreakIterator.DONE;
}
/**
* Returns the String after a given index.
*
* @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
* or AccessibleText.SENTENCE to retrieve
* @param index an index within the text &gt;= 0
* @return the letter, word, or sentence, null for an invalid
* index or part
*/
public String getAfterIndex(int part, int index) {
if (index < 0 || index >= TextComponent.this.getText().length()) {
return null;
}
switch (part) {
case AccessibleText.CHARACTER:
if (index+1 >= TextComponent.this.getText().length()) {
return null;
}
return TextComponent.this.getText().substring(index+1, index+2);
case AccessibleText.WORD: {
String s = TextComponent.this.getText();
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int start = findWordLimit(index, words, NEXT, s);
if (start == BreakIterator.DONE || start >= s.length()) {
return null;
}
int end = words.following(start);
if (end == BreakIterator.DONE || end >= s.length()) {
return null;
}
return s.substring(start, end);
}
case AccessibleText.SENTENCE: {
String s = TextComponent.this.getText();
BreakIterator sentence = BreakIterator.getSentenceInstance();
sentence.setText(s);
int start = sentence.following(index);
if (start == BreakIterator.DONE || start >= s.length()) {
return null;
}
int end = sentence.following(start);
if (end == BreakIterator.DONE || end >= s.length()) {
return null;
}
return s.substring(start, end);
}
default:
return null;
}
}
/**
* Returns the String before a given index.
*
* @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
* or AccessibleText.SENTENCE to retrieve
* @param index an index within the text &gt;= 0
* @return the letter, word, or sentence, null for an invalid index
* or part
*/
public String getBeforeIndex(int part, int index) {
if (index < 0 || index > TextComponent.this.getText().length()-1) {
return null;
}
switch (part) {
case AccessibleText.CHARACTER:
if (index == 0) {
return null;
}
return TextComponent.this.getText().substring(index-1, index);
case AccessibleText.WORD: {
String s = TextComponent.this.getText();
BreakIterator words = BreakIterator.getWordInstance();
words.setText(s);
int end = findWordLimit(index, words, PREVIOUS, s);
if (end == BreakIterator.DONE) {
return null;
}
int start = words.preceding(end);
if (start == BreakIterator.DONE) {
return null;
}
return s.substring(start, end);
}
case AccessibleText.SENTENCE: {
String s = TextComponent.this.getText();
BreakIterator sentence = BreakIterator.getSentenceInstance();
sentence.setText(s);
int end = sentence.following(index);
end = sentence.previous();
int start = sentence.previous();
if (start == BreakIterator.DONE) {
return null;
}
return s.substring(start, end);
}
default:
return null;
}
}
} // end of AccessibleAWTTextComponent
private boolean checkForEnableIM = true;
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/x_bao/Java8Source.git
git@gitee.com:x_bao/Java8Source.git
x_bao
Java8Source
Java8Source
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /