src/share/classes/java/awt/TextComponent.java

Print this page




 954          */
 955 
 956         /**
 957          * Given a point in local coordinates, return the zero-based index
 958          * of the character under that Point.  If the point is invalid,
 959          * this method returns -1.
 960          *
 961          * @param p the Point in local coordinates
 962          * @return the zero-based index of the character under Point p.
 963          */
 964         public int getIndexAtPoint(Point p) {
 965             return TextComponent.this.getIndexAtPoint(p);
 966         }
 967 
 968         /**
 969          * Determines the bounding box of the character at the given
 970          * index into the string.  The bounds are returned in local
 971          * coordinates.  If the index is invalid a null rectangle
 972          * is returned.
 973          *
 974          * @param i the index into the String >= 0
 975          * @return the screen coordinates of the character's bounding box
 976          */
 977         public Rectangle getCharacterBounds(int i) {
 978             return TextComponent.this.getCharacterBounds(i);
 979         }
 980 
 981         /**
 982          * Returns the number of characters (valid indicies)
 983          *
 984          * @return the number of characters >= 0
 985          */
 986         public int getCharCount() {
 987             return TextComponent.this.getText().length();
 988         }
 989 
 990         /**
 991          * Returns the zero-based offset of the caret.
 992          *
 993          * Note: The character to the right of the caret will have the
 994          * same index value as the offset (the caret is between
 995          * two characters).
 996          *
 997          * @return the zero-based offset of the caret.
 998          */
 999         public int getCaretPosition() {
1000             return TextComponent.this.getCaretPosition();
1001         }
1002 
1003         /**
1004          * Returns the AttributeSet for a given character (at a given index).
1005          *
1006          * @param i the zero-based index into the text
1007          * @return the AttributeSet of the character
1008          */
1009         public AttributeSet getCharacterAttribute(int i) {
1010             return null; // No attributes in TextComponent
1011         }
1012 
1013         /**
1014          * Returns the start offset within the selected text.
1015          * If there is no selection, but there is
1016          * a caret, the start and end offsets will be the same.
1017          * Return 0 if the text is empty, or the caret position
1018          * if no selection.
1019          *
1020          * @return the index into the text of the start of the selection >= 0
1021          */
1022         public int getSelectionStart() {
1023             return TextComponent.this.getSelectionStart();
1024         }
1025 
1026         /**
1027          * Returns the end offset within the selected text.
1028          * If there is no selection, but there is
1029          * a caret, the start and end offsets will be the same.
1030          * Return 0 if the text is empty, or the caret position
1031          * if no selection.
1032          *
1033          * @return the index into teh text of the end of the selection >= 0
1034          */
1035         public int getSelectionEnd() {
1036             return TextComponent.this.getSelectionEnd();
1037         }
1038 
1039         /**
1040          * Returns the portion of the text that is selected.
1041          *
1042          * @return the text, null if no selection
1043          */
1044         public String getSelectedText() {
1045             String selText = TextComponent.this.getSelectedText();
1046             // Fix for 4256662
1047             if (selText == null || selText.equals("")) {
1048                 return null;
1049             }
1050             return selText;
1051         }
1052 
1053         /**
1054          * Returns the String at a given index.
1055          *
1056          * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
1057          * or AccessibleText.SENTENCE to retrieve
1058          * @param index an index within the text >= 0
1059          * @return the letter, word, or sentence,
1060          *   null for an invalid index or part
1061          */
1062         public String getAtIndex(int part, int index) {
1063             if (index < 0 || index >= TextComponent.this.getText().length()) {
1064                 return null;
1065             }
1066             switch (part) {
1067             case AccessibleText.CHARACTER:
1068                 return TextComponent.this.getText().substring(index, index+1);
1069             case AccessibleText.WORD:  {
1070                     String s = TextComponent.this.getText();
1071                     BreakIterator words = BreakIterator.getWordInstance();
1072                     words.setText(s);
1073                     int end = words.following(index);
1074                     return s.substring(words.previous(), end);
1075                 }
1076             case AccessibleText.SENTENCE:  {
1077                     String s = TextComponent.this.getText();
1078                     BreakIterator sentence = BreakIterator.getSentenceInstance();


1104             int current = (direction == NEXT) ? words.next()
1105                                               : words.previous();
1106             while (current != BreakIterator.DONE) {
1107                 for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
1108                     if (Character.isLetter(s.charAt(p))) {
1109                         return last;
1110                     }
1111                 }
1112                 last = current;
1113                 current = (direction == NEXT) ? words.next()
1114                                               : words.previous();
1115             }
1116             return BreakIterator.DONE;
1117         }
1118 
1119         /**
1120          * Returns the String after a given index.
1121          *
1122          * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
1123          * or AccessibleText.SENTENCE to retrieve
1124          * @param index an index within the text >= 0
1125          * @return the letter, word, or sentence, null for an invalid
1126          *  index or part
1127          */
1128         public String getAfterIndex(int part, int index) {
1129             if (index < 0 || index >= TextComponent.this.getText().length()) {
1130                 return null;
1131             }
1132             switch (part) {
1133             case AccessibleText.CHARACTER:
1134                 if (index+1 >= TextComponent.this.getText().length()) {
1135                    return null;
1136                 }
1137                 return TextComponent.this.getText().substring(index+1, index+2);
1138             case AccessibleText.WORD:  {
1139                     String s = TextComponent.this.getText();
1140                     BreakIterator words = BreakIterator.getWordInstance();
1141                     words.setText(s);
1142                     int start = findWordLimit(index, words, NEXT, s);
1143                     if (start == BreakIterator.DONE || start >= s.length()) {
1144                         return null;


1157                     if (start == BreakIterator.DONE || start >= s.length()) {
1158                         return null;
1159                     }
1160                     int end = sentence.following(start);
1161                     if (end == BreakIterator.DONE || end >= s.length()) {
1162                         return null;
1163                     }
1164                     return s.substring(start, end);
1165                 }
1166             default:
1167                 return null;
1168             }
1169         }
1170 
1171 
1172         /**
1173          * Returns the String before a given index.
1174          *
1175          * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
1176          *   or AccessibleText.SENTENCE to retrieve
1177          * @param index an index within the text >= 0
1178          * @return the letter, word, or sentence, null for an invalid index
1179          *  or part
1180          */
1181         public String getBeforeIndex(int part, int index) {
1182             if (index < 0 || index > TextComponent.this.getText().length()-1) {
1183                 return null;
1184             }
1185             switch (part) {
1186             case AccessibleText.CHARACTER:
1187                 if (index == 0) {
1188                     return null;
1189                 }
1190                 return TextComponent.this.getText().substring(index-1, index);
1191             case AccessibleText.WORD:  {
1192                     String s = TextComponent.this.getText();
1193                     BreakIterator words = BreakIterator.getWordInstance();
1194                     words.setText(s);
1195                     int end = findWordLimit(index, words, PREVIOUS, s);
1196                     if (end == BreakIterator.DONE) {
1197                         return null;




 954          */
 955 
 956         /**
 957          * Given a point in local coordinates, return the zero-based index
 958          * of the character under that Point.  If the point is invalid,
 959          * this method returns -1.
 960          *
 961          * @param p the Point in local coordinates
 962          * @return the zero-based index of the character under Point p.
 963          */
 964         public int getIndexAtPoint(Point p) {
 965             return TextComponent.this.getIndexAtPoint(p);
 966         }
 967 
 968         /**
 969          * Determines the bounding box of the character at the given
 970          * index into the string.  The bounds are returned in local
 971          * coordinates.  If the index is invalid a null rectangle
 972          * is returned.
 973          *
 974          * @param i the index into the String &gt;= 0
 975          * @return the screen coordinates of the character's bounding box
 976          */
 977         public Rectangle getCharacterBounds(int i) {
 978             return TextComponent.this.getCharacterBounds(i);
 979         }
 980 
 981         /**
 982          * Returns the number of characters (valid indicies)
 983          *
 984          * @return the number of characters &gt;= 0
 985          */
 986         public int getCharCount() {
 987             return TextComponent.this.getText().length();
 988         }
 989 
 990         /**
 991          * Returns the zero-based offset of the caret.
 992          *
 993          * Note: The character to the right of the caret will have the
 994          * same index value as the offset (the caret is between
 995          * two characters).
 996          *
 997          * @return the zero-based offset of the caret.
 998          */
 999         public int getCaretPosition() {
1000             return TextComponent.this.getCaretPosition();
1001         }
1002 
1003         /**
1004          * Returns the AttributeSet for a given character (at a given index).
1005          *
1006          * @param i the zero-based index into the text
1007          * @return the AttributeSet of the character
1008          */
1009         public AttributeSet getCharacterAttribute(int i) {
1010             return null; // No attributes in TextComponent
1011         }
1012 
1013         /**
1014          * Returns the start offset within the selected text.
1015          * If there is no selection, but there is
1016          * a caret, the start and end offsets will be the same.
1017          * Return 0 if the text is empty, or the caret position
1018          * if no selection.
1019          *
1020          * @return the index into the text of the start of the selection &gt;= 0
1021          */
1022         public int getSelectionStart() {
1023             return TextComponent.this.getSelectionStart();
1024         }
1025 
1026         /**
1027          * Returns the end offset within the selected text.
1028          * If there is no selection, but there is
1029          * a caret, the start and end offsets will be the same.
1030          * Return 0 if the text is empty, or the caret position
1031          * if no selection.
1032          *
1033          * @return the index into teh text of the end of the selection &gt;= 0
1034          */
1035         public int getSelectionEnd() {
1036             return TextComponent.this.getSelectionEnd();
1037         }
1038 
1039         /**
1040          * Returns the portion of the text that is selected.
1041          *
1042          * @return the text, null if no selection
1043          */
1044         public String getSelectedText() {
1045             String selText = TextComponent.this.getSelectedText();
1046             // Fix for 4256662
1047             if (selText == null || selText.equals("")) {
1048                 return null;
1049             }
1050             return selText;
1051         }
1052 
1053         /**
1054          * Returns the String at a given index.
1055          *
1056          * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
1057          * or AccessibleText.SENTENCE to retrieve
1058          * @param index an index within the text &gt;= 0
1059          * @return the letter, word, or sentence,
1060          *   null for an invalid index or part
1061          */
1062         public String getAtIndex(int part, int index) {
1063             if (index < 0 || index >= TextComponent.this.getText().length()) {
1064                 return null;
1065             }
1066             switch (part) {
1067             case AccessibleText.CHARACTER:
1068                 return TextComponent.this.getText().substring(index, index+1);
1069             case AccessibleText.WORD:  {
1070                     String s = TextComponent.this.getText();
1071                     BreakIterator words = BreakIterator.getWordInstance();
1072                     words.setText(s);
1073                     int end = words.following(index);
1074                     return s.substring(words.previous(), end);
1075                 }
1076             case AccessibleText.SENTENCE:  {
1077                     String s = TextComponent.this.getText();
1078                     BreakIterator sentence = BreakIterator.getSentenceInstance();


1104             int current = (direction == NEXT) ? words.next()
1105                                               : words.previous();
1106             while (current != BreakIterator.DONE) {
1107                 for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
1108                     if (Character.isLetter(s.charAt(p))) {
1109                         return last;
1110                     }
1111                 }
1112                 last = current;
1113                 current = (direction == NEXT) ? words.next()
1114                                               : words.previous();
1115             }
1116             return BreakIterator.DONE;
1117         }
1118 
1119         /**
1120          * Returns the String after a given index.
1121          *
1122          * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
1123          * or AccessibleText.SENTENCE to retrieve
1124          * @param index an index within the text &gt;= 0
1125          * @return the letter, word, or sentence, null for an invalid
1126          *  index or part
1127          */
1128         public String getAfterIndex(int part, int index) {
1129             if (index < 0 || index >= TextComponent.this.getText().length()) {
1130                 return null;
1131             }
1132             switch (part) {
1133             case AccessibleText.CHARACTER:
1134                 if (index+1 >= TextComponent.this.getText().length()) {
1135                    return null;
1136                 }
1137                 return TextComponent.this.getText().substring(index+1, index+2);
1138             case AccessibleText.WORD:  {
1139                     String s = TextComponent.this.getText();
1140                     BreakIterator words = BreakIterator.getWordInstance();
1141                     words.setText(s);
1142                     int start = findWordLimit(index, words, NEXT, s);
1143                     if (start == BreakIterator.DONE || start >= s.length()) {
1144                         return null;


1157                     if (start == BreakIterator.DONE || start >= s.length()) {
1158                         return null;
1159                     }
1160                     int end = sentence.following(start);
1161                     if (end == BreakIterator.DONE || end >= s.length()) {
1162                         return null;
1163                     }
1164                     return s.substring(start, end);
1165                 }
1166             default:
1167                 return null;
1168             }
1169         }
1170 
1171 
1172         /**
1173          * Returns the String before a given index.
1174          *
1175          * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
1176          *   or AccessibleText.SENTENCE to retrieve
1177          * @param index an index within the text &gt;= 0
1178          * @return the letter, word, or sentence, null for an invalid index
1179          *  or part
1180          */
1181         public String getBeforeIndex(int part, int index) {
1182             if (index < 0 || index > TextComponent.this.getText().length()-1) {
1183                 return null;
1184             }
1185             switch (part) {
1186             case AccessibleText.CHARACTER:
1187                 if (index == 0) {
1188                     return null;
1189                 }
1190                 return TextComponent.this.getText().substring(index-1, index);
1191             case AccessibleText.WORD:  {
1192                     String s = TextComponent.this.getText();
1193                     BreakIterator words = BreakIterator.getWordInstance();
1194                     words.setText(s);
1195                     int end = findWordLimit(index, words, PREVIOUS, s);
1196                     if (end == BreakIterator.DONE) {
1197                         return null;