1 7 package javax.swing.text; 8 9 import java.io.*; 10 import java.awt.*; 11 import java.awt.event.ActionEvent ; 12 import java.text.*; 13 import javax.swing.Action ; 14 import javax.swing.KeyStroke ; 15 import javax.swing.SwingConstants ; 16 import javax.swing.UIManager ; 17 18 54 public class DefaultEditorKit extends EditorKit { 55 56 59 public DefaultEditorKit() { 60 } 61 62 69 public String getContentType() { 70 return "text/plain"; 71 } 72 73 81 public ViewFactory getViewFactory() { 82 return null; 83 } 84 85 92 public Action [] getActions() { 93 return defaultActions; 94 } 95 96 102 public Caret createCaret() { 103 return null; 104 } 105 106 112 public Document createDefaultDocument() { 113 return new PlainDocument (); 114 } 115 116 129 public void read(InputStream in, Document doc, int pos) 130 throws IOException, BadLocationException { 131 132 read(new InputStreamReader(in), doc, pos); 133 } 134 135 148 public void write(OutputStream out, Document doc, int pos, int len) 149 throws IOException, BadLocationException { 150 OutputStreamWriter osw = new OutputStreamWriter(out); 151 152 write(osw, doc, pos, len); 153 osw.flush(); 154 } 155 156 164 MutableAttributeSet getInputAttributes() { 165 return null; 166 } 167 168 180 public void read(Reader in, Document doc, int pos) 181 throws IOException, BadLocationException { 182 183 char[] buff = new char[4096]; 184 int nch; 185 boolean lastWasCR = false; 186 boolean isCRLF = false; 187 boolean isCR = false; 188 int last; 189 boolean wasEmpty = (doc.getLength() == 0); 190 AttributeSet attr = getInputAttributes(); 191 192 while ((nch = in.read(buff, 0, buff.length)) != -1) { 198 last = 0; 199 for(int counter = 0; counter < nch; counter++) { 200 switch(buff[counter]) { 201 case '\r': 202 if (lastWasCR) { 203 isCR = true; 204 if (counter == 0) { 205 doc.insertString(pos, "\n", attr); 206 pos++; 207 } 208 else { 209 buff[counter - 1] = '\n'; 210 } 211 } 212 else { 213 lastWasCR = true; 214 } 215 break; 216 case '\n': 217 if (lastWasCR) { 218 if (counter > (last + 1)) { 219 doc.insertString(pos, new String (buff, last, 220 counter - last - 1), attr); 221 pos += (counter - last - 1); 222 } 223 lastWasCR = false; 226 last = counter; 227 isCRLF = true; 228 } 229 break; 230 default: 231 if (lastWasCR) { 232 isCR = true; 233 if (counter == 0) { 234 doc.insertString(pos, "\n", attr); 235 pos++; 236 } 237 else { 238 buff[counter - 1] = '\n'; 239 } 240 lastWasCR = false; 241 } 242 break; 243 } 244 } 245 if (last < nch) { 246 if(lastWasCR) { 247 if (last < (nch - 1)) { 248 doc.insertString(pos, new String (buff, last, 249 nch - last - 1), attr); 250 pos += (nch - last - 1); 251 } 252 } 253 else { 254 doc.insertString(pos, new String (buff, last, 255 nch - last), attr); 256 pos += (nch - last); 257 } 258 } 259 } 260 if (lastWasCR) { 261 doc.insertString(pos, "\n", attr); 262 isCR = true; 263 } 264 if (wasEmpty) { 265 if (isCRLF) { 266 doc.putProperty(EndOfLineStringProperty, "\r\n"); 267 } 268 else if (isCR) { 269 doc.putProperty(EndOfLineStringProperty, "\r"); 270 } 271 else { 272 doc.putProperty(EndOfLineStringProperty, "\n"); 273 } 274 } 275 } 276 277 290 public void write(Writer out, Document doc, int pos, int len) 291 throws IOException, BadLocationException { 292 293 if ((pos < 0) || ((pos + len) > doc.getLength())) { 294 throw new BadLocationException ("DefaultEditorKit.write", pos); 295 } 296 Segment data = new Segment (); 297 int nleft = len; 298 int offs = pos; 299 Object endOfLineProperty = doc.getProperty(EndOfLineStringProperty); 300 if (endOfLineProperty == null) { 301 try { 302 endOfLineProperty = System.getProperty("line.separator"); 303 } catch (SecurityException se) { } 304 } 305 String endOfLine; 306 if (endOfLineProperty instanceof String ) { 307 endOfLine = (String )endOfLineProperty; 308 } 309 else { 310 endOfLine = null; 311 } 312 if (endOfLineProperty != null && !endOfLine.equals("\n")) { 313 while (nleft > 0) { 316 int n = Math.min(nleft, 4096); 317 doc.getText(offs, n, data); 318 int last = data.offset; 319 char[] array = data.array; 320 int maxCounter = last + data.count; 321 for (int counter = last; counter < maxCounter; counter++) { 322 if (array[counter] == '\n') { 323 if (counter > last) { 324 out.write(array, last, counter - last); 325 } 326 out.write(endOfLine); 327 last = counter + 1; 328 } 329 } 330 if (maxCounter > last) { 331 out.write(array, last, maxCounter - last); 332 } 333 offs += n; 334 nleft -= n; 335 } 336 } 337 else { 338 while (nleft > 0) { 341 int n = Math.min(nleft, 4096); 342 doc.getText(offs, n, data); 343 out.write(data.array, data.offset, data.count); 344 offs += n; 345 nleft -= n; 346 } 347 } 348 out.flush(); 349 } 350 351 352 356 public static final String EndOfLineStringProperty = "__EndOfLine__"; 357 358 360 366 public static final String insertContentAction = "insert-content"; 367 368 374 public static final String insertBreakAction = "insert-break"; 375 376 382 public static final String insertTabAction = "insert-tab"; 383 384 389 public static final String deletePrevCharAction = "delete-previous"; 390 391 396 public static final String deleteNextCharAction = "delete-next"; 397 398 403 public static final String readOnlyAction = "set-read-only"; 404 405 410 public static final String writableAction = "set-writable"; 411 412 418 public static final String cutAction = "cut-to-clipboard"; 419 420 426 public static final String copyAction = "copy-to-clipboard"; 427 428 435 public static final String pasteAction = "paste-from-clipboard"; 436 437 441 public static final String beepAction = "beep"; 442 443 447 public static final String pageUpAction = "page-up"; 448 449 453 public static final String pageDownAction = "page-down"; 454 455 460 static final String selectionPageUpAction = "selection-page-up"; 461 462 467 static final String selectionPageDownAction = "selection-page-down"; 468 469 474 static final String selectionPageLeftAction = "selection-page-left"; 475 476 481 static final String selectionPageRightAction = "selection-page-right"; 482 483 488 public static final String forwardAction = "caret-forward"; 489 490 495 public static final String backwardAction = "caret-backward"; 496 497 502 public static final String selectionForwardAction = "selection-forward"; 503 504 509 public static final String selectionBackwardAction = "selection-backward"; 510 511 516 public static final String upAction = "caret-up"; 517 518 523 public static final String downAction = "caret-down"; 524 525 530 public static final String selectionUpAction = "selection-up"; 531 532 537 public static final String selectionDownAction = "selection-down"; 538 539 544 public static final String beginWordAction = "caret-begin-word"; 545 546 551 public static final String endWordAction = "caret-end-word"; 552 553 558 public static final String selectionBeginWordAction = "selection-begin-word"; 559 560 565 public static final String selectionEndWordAction = "selection-end-word"; 566 567 572 public static final String previousWordAction = "caret-previous-word"; 573 574 579 public static final String nextWordAction = "caret-next-word"; 580 581 586 public static final String selectionPreviousWordAction = "selection-previous-word"; 587 588 593 public static final String selectionNextWordAction = "selection-next-word"; 594 595 600 public static final String beginLineAction = "caret-begin-line"; 601 602 607 public static final String endLineAction = "caret-end-line"; 608 609 614 public static final String selectionBeginLineAction = "selection-begin-line"; 615 616 621 public static final String selectionEndLineAction = "selection-end-line"; 622 623 628 public static final String beginParagraphAction = "caret-begin-paragraph"; 629 630 635 public static final String endParagraphAction = "caret-end-paragraph"; 636 637 642 public static final String selectionBeginParagraphAction = "selection-begin-paragraph"; 643 644 649 public static final String selectionEndParagraphAction = "selection-end-paragraph"; 650 651 656 public static final String beginAction = "caret-begin"; 657 658 663 public static final String endAction = "caret-end"; 664 665 670 public static final String selectionBeginAction = "selection-begin"; 671 672 677 public static final String selectionEndAction = "selection-end"; 678 679 683 public static final String selectWordAction = "select-word"; 684 685 689 public static final String selectLineAction = "select-line"; 690 691 695 public static final String selectParagraphAction = "select-paragraph"; 696 697 701 public static final String selectAllAction = "select-all"; 702 703 707 static final String unselectAction = "unselect"; 708 709 713 static final String toggleComponentOrientationAction 714 = "toggle-componentOrientation"; 715 716 722 public static final String defaultKeyTypedAction = "default-typed"; 723 724 726 private static final Action [] defaultActions = { 727 new InsertContentAction(), new DeletePrevCharAction(), 728 new DeleteNextCharAction(), new ReadOnlyAction(), 729 new WritableAction(), new CutAction(), 730 new CopyAction(), new PasteAction(), 731 new VerticalPageAction(pageUpAction, -1, false), 732 new VerticalPageAction(pageDownAction, 1, false), 733 new VerticalPageAction(selectionPageUpAction, -1, true), 734 new VerticalPageAction(selectionPageDownAction, 1, true), 735 new PageAction(selectionPageLeftAction, true, true), 736 new PageAction(selectionPageRightAction, false, true), 737 new InsertBreakAction(), new BeepAction(), 738 new NextVisualPositionAction(forwardAction, false, 739 SwingConstants.EAST), 740 new NextVisualPositionAction(backwardAction, false, 741 SwingConstants.WEST), 742 new NextVisualPositionAction(selectionForwardAction, true, 743 SwingConstants.EAST), 744 new NextVisualPositionAction(selectionBackwardAction, true, 745 SwingConstants.WEST), 746 new NextVisualPositionAction(upAction, false, 747 SwingConstants.NORTH), 748 new NextVisualPositionAction(downAction, false, 749 SwingConstants.SOUTH), 750 new NextVisualPositionAction(selectionUpAction, true, 751 SwingConstants.NORTH), 752 new NextVisualPositionAction(selectionDownAction, true, 753 SwingConstants.SOUTH), 754 new BeginWordAction(beginWordAction, false), 755 new EndWordAction(endWordAction, false), 756 new BeginWordAction(selectionBeginWordAction, true), 757 new EndWordAction(selectionEndWordAction, true), 758 new PreviousWordAction(previousWordAction, false), 759 new NextWordAction(nextWordAction, false), 760 new PreviousWordAction(selectionPreviousWordAction, true), 761 new NextWordAction(selectionNextWordAction, true), 762 new BeginLineAction(beginLineAction, false), 763 new EndLineAction(endLineAction, false), 764 new BeginLineAction(selectionBeginLineAction, true), 765 new EndLineAction(selectionEndLineAction, true), 766 new BeginParagraphAction(beginParagraphAction, false), 767 new EndParagraphAction(endParagraphAction, false), 768 new BeginParagraphAction(selectionBeginParagraphAction, true), 769 new EndParagraphAction(selectionEndParagraphAction, true), 770 new BeginAction(beginAction, false), 771 new EndAction(endAction, false), 772 new BeginAction(selectionBeginAction, true), 773 new EndAction(selectionEndAction, true), 774 new DefaultKeyTypedAction(), new InsertTabAction(), 775 new SelectWordAction(), new SelectLineAction(), 776 new SelectParagraphAction(), new SelectAllAction(), 777 new UnselectAction(), new ToggleComponentOrientationAction(), 778 new DumpModelAction() 779 }; 780 781 813 public static class DefaultKeyTypedAction extends TextAction { 814 815 818 public DefaultKeyTypedAction() { 819 super(defaultKeyTypedAction); 820 } 821 822 827 public void actionPerformed(ActionEvent e) { 828 JTextComponent target = getTextComponent(e); 829 if ((target != null) && (e != null)) { 830 if ((! target.isEditable()) || (! target.isEnabled())) { 831 return; 832 } 833 String content = e.getActionCommand(); 834 int mod = e.getModifiers(); 835 if ((content != null) && (content.length() > 0) && 836 ((mod & ActionEvent.ALT_MASK) == (mod & ActionEvent.CTRL_MASK))) { 837 char c = content.charAt(0); 838 if ((c >= 0x20) && (c != 0x7F)) { 839 target.replaceSelection(content); 840 } 841 } 842 } 843 } 844 } 845 846 863 public static class InsertContentAction extends TextAction { 864 865 868 public InsertContentAction() { 869 super(insertContentAction); 870 } 871 872 877 public void actionPerformed(ActionEvent e) { 878 JTextComponent target = getTextComponent(e); 879 if ((target != null) && (e != null)) { 880 if ((! target.isEditable()) || (! target.isEnabled())) { 881 UIManager.getLookAndFeel().provideErrorFeedback(target); 882 return; 883 } 884 String content = e.getActionCommand(); 885 if (content != null) { 886 target.replaceSelection(content); 887 } else { 888 UIManager.getLookAndFeel().provideErrorFeedback(target); 889 } 890 } 891 } 892 } 893 894 911 public static class InsertBreakAction extends TextAction { 912 913 916 public InsertBreakAction() { 917 super(insertBreakAction); 918 } 919 920 925 public void actionPerformed(ActionEvent e) { 926 JTextComponent target = getTextComponent(e); 927 if (target != null) { 928 if ((! target.isEditable()) || (! target.isEnabled())) { 929 UIManager.getLookAndFeel().provideErrorFeedback(target); 930 return; 931 } 932 target.replaceSelection("\n"); 933 } 934 } 935 } 936 937 953 public static class InsertTabAction extends TextAction { 954 955 958 public InsertTabAction() { 959 super(insertTabAction); 960 } 961 962 967 public void actionPerformed(ActionEvent e) { 968 JTextComponent target = getTextComponent(e); 969 if (target != null) { 970 if ((! target.isEditable()) || (! target.isEnabled())) { 971 UIManager.getLookAndFeel().provideErrorFeedback(target); 972 return; 973 } 974 target.replaceSelection("\t"); 975 } 976 } 977 } 978 979 985 static class DeletePrevCharAction extends TextAction { 986 987 990 DeletePrevCharAction() { 991 super(deletePrevCharAction); 992 } 993 994 999 public void actionPerformed(ActionEvent e) { 1000 JTextComponent target = getTextComponent(e); 1001 boolean beep = true; 1002 if ((target != null) && (target.isEditable())) { 1003 try { 1004 Document doc = target.getDocument(); 1005 Caret caret = target.getCaret(); 1006 int dot = caret.getDot(); 1007 int mark = caret.getMark(); 1008 if (dot != mark) { 1009 doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); 1010 beep = false; 1011 } else if (dot > 0) { 1012 int delChars = 1; 1013 1014 if (dot > 1) { 1015 String dotChars = doc.getText(dot - 2, 2); 1016 char c0 = dotChars.charAt(0); 1017 char c1 = dotChars.charAt(1); 1018 1019 if (c0 >= '\uD800' && c0 <= '\uDBFF' && 1020 c1 >= '\uDC00' && c1 <= '\uDFFF') { 1021 delChars = 2; 1022 } 1023 } 1024 1025 doc.remove(dot - delChars, delChars); 1026 beep = false; 1027 } 1028 } catch (BadLocationException bl) { 1029 } 1030 } 1031 if (beep) { 1032 UIManager.getLookAndFeel().provideErrorFeedback(target); 1033 } 1034 } 1035 } 1036 1037 1043 static class DeleteNextCharAction extends TextAction { 1044 1045 1046 DeleteNextCharAction() { 1047 super(deleteNextCharAction); 1048 } 1049 1050 1051 public void actionPerformed(ActionEvent e) { 1052 JTextComponent target = getTextComponent(e); 1053 boolean beep = true; 1054 if ((target != null) && (target.isEditable())) { 1055 try { 1056 Document doc = target.getDocument(); 1057 Caret caret = target.getCaret(); 1058 int dot = caret.getDot(); 1059 int mark = caret.getMark(); 1060 if (dot != mark) { 1061 doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); 1062 beep = false; 1063 } else if (dot < doc.getLength()) { 1064 int delChars = 1; 1065 1066 if (dot < doc.getLength() - 1) { 1067 String dotChars = doc.getText(dot, 2); 1068 char c0 = dotChars.charAt(0); 1069 char c1 = dotChars.charAt(1); 1070 1071 if (c0 >= '\uD800' && c0 <= '\uDBFF' && 1072 c1 >= '\uDC00' && c1 <= '\uDFFF') { 1073 delChars = 2; 1074 } 1075 } 1076 1077 doc.remove(dot, delChars); 1078 beep = false; 1079 } 1080 } catch (BadLocationException bl) { 1081 } 1082 } 1083 if (beep) { 1084 UIManager.getLookAndFeel().provideErrorFeedback(target); 1085 } 1086 } 1087 } 1088 1089 1094 static class ReadOnlyAction extends TextAction { 1095 1096 1097 ReadOnlyAction() { 1098 super(readOnlyAction); 1099 } 1100 1101 1106 public void actionPerformed(ActionEvent e) { 1107 JTextComponent target = getTextComponent(e); 1108 if (target != null) { 1109 target.setEditable(false); 1110 } 1111 } 1112 } 1113 1114 1119 static class WritableAction extends TextAction { 1120 1121 1122 WritableAction() { 1123 super(writableAction); 1124 } 1125 1126 1131 public void actionPerformed(ActionEvent e) { 1132 JTextComponent target = getTextComponent(e); 1133 if (target != null) { 1134 target.setEditable(true); 1135 } 1136 } 1137 } 1138 1139 1155 public static class CutAction extends TextAction { 1156 1157 1158 public CutAction() { 1159 super(cutAction); 1160 } 1161 1162 1167 public void actionPerformed(ActionEvent e) { 1168 JTextComponent target = getTextComponent(e); 1169 if (target != null) { 1170 target.cut(); 1171 } 1172 } 1173 } 1174 1175 1191 public static class CopyAction extends TextAction { 1192 1193 1194 public CopyAction() { 1195 super(copyAction); 1196 } 1197 1198 1203 public void actionPerformed(ActionEvent e) { 1204 JTextComponent target = getTextComponent(e); 1205 if (target != null) { 1206 target.copy(); 1207 } 1208 } 1209 } 1210 1211 1228 public static class PasteAction extends TextAction { 1229 1230 1231 public PasteAction() { 1232 super(pasteAction); 1233 } 1234 1235 1240 public void actionPerformed(ActionEvent e) { 1241 JTextComponent target = getTextComponent(e); 1242 if (target != null) { 1243 target.paste(); 1244 } 1245 } 1246 } 1247 1248 1263 public static class BeepAction extends TextAction { 1264 1265 1266 public BeepAction() { 1267 super(beepAction); 1268 } 1269 1270 1275 public void actionPerformed(ActionEvent e) { 1276 JTextComponent target = getTextComponent(e); 1277 UIManager.getLookAndFeel().provideErrorFeedback(target); 1278 } 1279 } 1280 1281 1289 static class VerticalPageAction extends TextAction { 1290 1291 1292 public VerticalPageAction(String nm, int direction, boolean select) { 1293 super(nm); 1294 this.select = select; 1295 this.direction = direction; 1296 } 1297 1298 1299 public void actionPerformed(ActionEvent e) { 1300 JTextComponent target = getTextComponent(e); 1301 if (target != null) { 1302 Rectangle visible = target.getVisibleRect(); 1303 Rectangle newVis = new Rectangle(visible); 1304 int selectedIndex = target.getCaretPosition(); 1305 int scrollAmount = target.getScrollableBlockIncrement( 1306 visible, SwingConstants.VERTICAL, direction); 1307 int initialY = visible.y; 1308 Caret caret = target.getCaret(); 1309 Point magicPosition = caret.getMagicCaretPosition(); 1310 int yOffset; 1311 1312 if (selectedIndex != -1) { 1313 try { 1314 Rectangle dotBounds = target.modelToView( 1315 selectedIndex); 1316 int x = (magicPosition != null) ? magicPosition.x : 1317 dotBounds.x; 1318 int h = dotBounds.height; 1320 yOffset = direction * 1321 (int)Math.ceil(scrollAmount / (double)h) * h; 1322 newVis.y = constrainY(target, initialY + yOffset, yOffset); 1323 1324 int newIndex; 1325 1326 if (visible.contains(dotBounds.x, dotBounds.y)) { 1327 newIndex = target.viewToModel( 1330 new Point(x, constrainY(target, 1331 dotBounds.y + yOffset, 0))); 1332 } 1333 else { 1334 if (direction == -1) { 1337 newIndex = target.viewToModel(new Point( 1338 x, newVis.y)); 1339 } 1340 else { 1341 newIndex = target.viewToModel(new Point( 1342 x, newVis.y + visible.height)); 1343 } 1344 } 1345 newIndex = constrainOffset(target, newIndex); 1346 if (newIndex != selectedIndex) { 1347 adjustScrollIfNecessary(target, newVis, initialY, 1351 newIndex); 1352 if (select) { 1353 target.moveCaretPosition(newIndex); 1354 } 1355 else { 1356 target.setCaretPosition(newIndex); 1357 } 1358 } 1359 } catch (BadLocationException ble) { } 1360 } else { 1361 yOffset = direction * scrollAmount; 1362 newVis.y = constrainY(target, initialY + yOffset, yOffset); 1363 } 1364 if (magicPosition != null) { 1365 caret.setMagicCaretPosition(magicPosition); 1366 } 1367 target.scrollRectToVisible(newVis); 1368 } 1369 } 1370 1371 1375 private int constrainY(JTextComponent target, int y, int vis) { 1376 if (y < 0) { 1377 y = 0; 1378 } 1379 else if (y + vis > target.getHeight()) { 1380 y = Math.max(0, target.getHeight() - vis); 1381 } 1382 return y; 1383 } 1384 1385 1389 private int constrainOffset(JTextComponent text, int offset) { 1390 Document doc = text.getDocument(); 1391 1392 if ((offset != 0) && (offset > doc.getLength())) { 1393 offset = doc.getLength(); 1394 } 1395 if (offset < 0) { 1396 offset = 0; 1397 } 1398 return offset; 1399 } 1400 1401 1405 private void adjustScrollIfNecessary(JTextComponent text, 1406 Rectangle visible, int initialY, 1407 int index) { 1408 try { 1409 Rectangle dotBounds = text.modelToView(index); 1410 1411 if (dotBounds.y < visible.y || 1412 (dotBounds.y > (visible.y + visible.height)) || 1413 (dotBounds.y + dotBounds.height) > 1414 (visible.y + visible.height)) { 1415 int y; 1416 1417 if (dotBounds.y < visible.y) { 1418 y = dotBounds.y; 1419 } 1420 else { 1421 y = dotBounds.y + dotBounds.height - visible.height; 1422 } 1423 if ((direction == -1 && y < initialY) || 1424 (direction == 1 && y > initialY)) { 1425 visible.y = y; 1427 } 1428 } 1429 } catch (BadLocationException ble) {} 1430 } 1431 1432 1436 private boolean select; 1437 1438 1441 private int direction; 1442 } 1443 1444 1445 1448 static class PageAction extends TextAction { 1449 1450 1451 public PageAction(String nm, boolean left, boolean select) { 1452 super(nm); 1453 this.select = select; 1454 this.left = left; 1455 } 1456 1457 1458 public void actionPerformed(ActionEvent e) { 1459 JTextComponent target = getTextComponent(e); 1460 if (target != null) { 1461 int selectedIndex; 1462 Rectangle visible = new Rectangle(); 1463 target.computeVisibleRect(visible); 1464 if (left) { 1465 visible.x = Math.max(0, visible.x - visible.width); 1466 } 1467 else { 1468 visible.x += visible.width; 1469 } 1470 1471 selectedIndex = target.getCaretPosition(); 1472 if(selectedIndex != -1) { 1473 if (left) { 1474 selectedIndex = target.viewToModel 1475 (new Point(visible.x, visible.y)); 1476 } 1477 else { 1478 selectedIndex = target.viewToModel 1479 (new Point(visible.x + visible.width - 1, 1480 visible.y + visible.height - 1)); 1481 } 1482 Document doc = target.getDocument(); 1483 if ((selectedIndex != 0) && 1484 (selectedIndex > (doc.getLength()-1))) { 1485 selectedIndex = doc.getLength()-1; 1486 } 1487 else if(selectedIndex < 0) { 1488 selectedIndex = 0; 1489 } 1490 if (select) 1491 target.moveCaretPosition(selectedIndex); 1492 else 1493 target.setCaretPosition(selectedIndex); 1494 } 1495 } 1496 } 1497 1498 private boolean select; 1499 private boolean left; 1500 } 1501 1502 static class DumpModelAction extends TextAction { 1503 1504 DumpModelAction() { 1505 super("dump-model"); 1506 } 1507 1508 public void actionPerformed(ActionEvent e) { 1509 JTextComponent target = getTextComponent(e); 1510 if (target != null) { 1511 Document d = target.getDocument(); 1512 if (d instanceof AbstractDocument ) { 1513 ((AbstractDocument ) d).dump(System.err); 1514 } 1515 } 1516 } 1517 } 1518 1519 1524 static class NextVisualPositionAction extends TextAction { 1525 1526 1532 NextVisualPositionAction(String nm, boolean select, int direction) { 1533 super(nm); 1534 this.select = select; 1535 this.direction = direction; 1536 } 1537 1538 1539 public void actionPerformed(ActionEvent e) { 1540 JTextComponent target = getTextComponent(e); 1541 if (target != null) { 1542 Caret caret = target.getCaret(); 1543 DefaultCaret bidiCaret = (caret instanceof DefaultCaret ) ? 1544 (DefaultCaret )caret : null; 1545 int dot = caret.getDot(); 1546 Position.Bias [] bias = new Position.Bias [1]; 1547 Point magicPosition = caret.getMagicCaretPosition(); 1548 1549 try { 1550 if(magicPosition == null && 1551 (direction == SwingConstants.NORTH || 1552 direction == SwingConstants.SOUTH)) { 1553 Rectangle r = (bidiCaret != null) ? 1554 target.getUI().modelToView(target, dot, 1555 bidiCaret.getDotBias()) : 1556 target.modelToView(dot); 1557 magicPosition = new Point(r.x, r.y); 1558 } 1559 1560 NavigationFilter filter = target.getNavigationFilter(); 1561 1562 if (filter != null) { 1563 dot = filter.getNextVisualPositionFrom 1564 (target, dot, (bidiCaret != null) ? 1565 bidiCaret.getDotBias() : 1566 Position.Bias.Forward, direction, bias); 1567 } 1568 else { 1569 dot = target.getUI().getNextVisualPositionFrom 1570 (target, dot, (bidiCaret != null) ? 1571 bidiCaret.getDotBias() : 1572 Position.Bias.Forward, direction, bias); 1573 } 1574 if(bias[0] == null) { 1575 bias[0] = Position.Bias.Forward; 1576 } 1577 if(bidiCaret != null) { 1578 if (select) { 1579 bidiCaret.moveDot(dot, bias[0]); 1580 } else { 1581 bidiCaret.setDot(dot, bias[0]); 1582 } 1583 } 1584 else { 1585 if (select) { 1586 caret.moveDot(dot); 1587 } else { 1588 caret.setDot(dot); 1589 } 1590 } 1591 if(magicPosition != null && 1592 (direction == SwingConstants.NORTH || 1593 direction == SwingConstants.SOUTH)) { 1594 target.getCaret().setMagicCaretPosition(magicPosition); 1595 } 1596 } catch (BadLocationException ex) { 1597 } 1598 } 1599 } 1600 1601 private boolean select; 1602 private int direction; 1603 } 1604 1605 1611 static class BeginWordAction extends TextAction { 1612 1613 1619 BeginWordAction(String nm, boolean select) { 1620 super(nm); 1621 this.select = select; 1622 } 1623 1624 1625 public void actionPerformed(ActionEvent e) { 1626 JTextComponent target = getTextComponent(e); 1627 if (target != null) { 1628 try { 1629 int offs = target.getCaretPosition(); 1630 int begOffs = Utilities.getWordStart(target, offs); 1631 if (select) { 1632 target.moveCaretPosition(begOffs); 1633 } else { 1634 target.setCaretPosition(begOffs); 1635 } 1636 } catch (BadLocationException bl) { 1637 UIManager.getLookAndFeel().provideErrorFeedback(target); 1638 } 1639 } 1640 } 1641 1642 private boolean select; 1643 } 1644 1645 1651 static class EndWordAction extends TextAction { 1652 1653 1659 EndWordAction(String nm, boolean select) { 1660 super(nm); 1661 this.select = select; 1662 } 1663 1664 1665 public void actionPerformed(ActionEvent e) { 1666 JTextComponent target = getTextComponent(e); 1667 if (target != null) { 1668 try { 1669 int offs = target.getCaretPosition(); 1670 int endOffs = Utilities.getWordEnd(target, offs); 1671 if (select) { 1672 target.moveCaretPosition(endOffs); 1673 } else { 1674 target.setCaretPosition(endOffs); 1675 } 1676 } catch (BadLocationException bl) { 1677 UIManager.getLookAndFeel().provideErrorFeedback(target); 1678 } 1679 } 1680 } 1681 1682 private boolean select; 1683 } 1684 1685 1691 static class PreviousWordAction extends TextAction { 1692 1693 1699 PreviousWordAction(String nm, boolean select) { 1700 super(nm); 1701 this.select = select; 1702 } 1703 1704 1705 public void actionPerformed(ActionEvent e) { 1706 JTextComponent target = getTextComponent(e); 1707 if (target != null) { 1708 int offs = target.getCaretPosition(); 1709 boolean failed = false; 1710 try { 1711 Element curPara = 1712 Utilities.getParagraphElement(target, offs); 1713 offs = Utilities.getPreviousWord(target, offs); 1714 if(offs < curPara.getStartOffset()) { 1715 offs = Utilities.getParagraphElement(target, offs). 1718 getEndOffset() - 1; 1719 } 1720 } catch (BadLocationException bl) { 1721 if (offs != 0) { 1722 offs = 0; 1723 } 1724 else { 1725 failed = true; 1726 } 1727 } 1728 if (!failed) { 1729 if (select) { 1730 target.moveCaretPosition(offs); 1731 } else { 1732 target.setCaretPosition(offs); 1733 } 1734 } 1735 else { 1736 UIManager.getLookAndFeel().provideErrorFeedback(target); 1737 } 1738 } 1739 } 1740 1741 private boolean select; 1742 } 1743 1744 1750 static class NextWordAction extends TextAction { 1751 1752 1758 NextWordAction(String nm, boolean select) { 1759 super(nm); 1760 this.select = select; 1761 } 1762 1763 1764 public void actionPerformed(ActionEvent e) { 1765 JTextComponent target = getTextComponent(e); 1766 if (target != null) { 1767 int offs = target.getCaretPosition(); 1768 boolean failed = false; 1769 int oldOffs = offs; 1770 Element curPara = 1771 Utilities.getParagraphElement(target, offs); 1772 try { 1773 offs = Utilities.getNextWord(target, offs); 1774 if(offs >= curPara.getEndOffset() && 1775 oldOffs != curPara.getEndOffset() - 1) { 1776 offs = curPara.getEndOffset() - 1; 1779 } 1780 } catch (BadLocationException bl) { 1781 int end = target.getDocument().getLength(); 1782 if (offs != end) { 1783 if(oldOffs != curPara.getEndOffset() - 1) { 1784 offs = curPara.getEndOffset() - 1; 1785 } else { 1786 offs = end; 1787 } 1788 } 1789 else { 1790 failed = true; 1791 } 1792 } 1793 if (!failed) { 1794 if (select) { 1795 target.moveCaretPosition(offs); 1796 } else { 1797 target.setCaretPosition(offs); 1798 } 1799 } 1800 else { 1801 UIManager.getLookAndFeel().provideErrorFeedback(target); 1802 } 1803 } 1804 } 1805 1806 private boolean select; 1807 } 1808 1809 1815 static class BeginLineAction extends TextAction { 1816 1817 1823 BeginLineAction(String nm, boolean select) { 1824 super(nm); 1825 this.select = select; 1826 } 1827 1828 1829 public void actionPerformed(ActionEvent e) { 1830 JTextComponent target = getTextComponent(e); 1831 if (target != null) { 1832 try { 1833 int offs = target.getCaretPosition(); 1834 int begOffs = Utilities.getRowStart(target, offs); 1835 if (select) { 1836 target.moveCaretPosition(begOffs); 1837 } else { 1838 target.setCaretPosition(begOffs); 1839 } 1840 } catch (BadLocationException bl) { 1841 UIManager.getLookAndFeel().provideErrorFeedback(target); 1842 } 1843 } 1844 } 1845 1846 private boolean select; 1847 } 1848 1849 1855 static class EndLineAction extends TextAction { 1856 1857 1863 EndLineAction(String nm, boolean select) { 1864 super(nm); 1865 this.select = select; 1866 } 1867 1868 1869 public void actionPerformed(ActionEvent e) { 1870 JTextComponent target = getTextComponent(e); 1871 if (target != null) { 1872 try { 1873 int offs = target.getCaretPosition(); 1874 int endOffs = Utilities.getRowEnd(target, offs); 1875 if (select) { 1876 target.moveCaretPosition(endOffs); 1877 } else { 1878 target.setCaretPosition(endOffs); 1879 } 1880 } catch (BadLocationException bl) { 1881 UIManager.getLookAndFeel().provideErrorFeedback(target); 1882 } 1883 } 1884 } 1885 1886 private boolean select; 1887 } 1888 1889 1895 static class BeginParagraphAction extends TextAction { 1896 1897 1903 BeginParagraphAction(String nm, boolean select) { 1904 super(nm); 1905 this.select = select; 1906 } 1907 1908 1909 public void actionPerformed(ActionEvent e) { 1910 JTextComponent target = getTextComponent(e); 1911 if (target != null) { 1912 int offs = target.getCaretPosition(); 1913 Element elem = Utilities.getParagraphElement(target, offs); 1914 offs = elem.getStartOffset(); 1915 if (select) { 1916 target.moveCaretPosition(offs); 1917 } else { 1918 target.setCaretPosition(offs); 1919 } 1920 } 1921 } 1922 1923 private boolean select; 1924 } 1925 1926 1932 static class EndParagraphAction extends TextAction { 1933 1934 1940 EndParagraphAction(String nm, boolean select) { 1941 super(nm); 1942 this.select = select; 1943 } 1944 1945 1946 public void actionPerformed(ActionEvent e) { 1947 JTextComponent target = getTextComponent(e); 1948 if (target != null) { 1949 int offs = target.getCaretPosition(); 1950 Element elem = Utilities.getParagraphElement(target, offs); 1951 offs = Math.min(target.getDocument().getLength(), 1952 elem.getEndOffset()); 1953 if (select) { 1954 target.moveCaretPosition(offs); 1955 } else { 1956 target.setCaretPosition(offs); 1957 } 1958 } 1959 } 1960 1961 private boolean select; 1962 } 1963 1964 1969 static class BeginAction extends TextAction { 1970 1971 1972 BeginAction(String nm, boolean select) { 1973 super(nm); 1974 this.select = select; 1975 } 1976 1977 1978 public void actionPerformed(ActionEvent e) { 1979 JTextComponent target = getTextComponent(e); 1980 if (target != null) { 1981 if (select) { 1982 target.moveCaretPosition(0); 1983 } else { 1984 target.setCaretPosition(0); 1985 } 1986 } 1987 } 1988 1989 private boolean select; 1990 } 1991 1992 1997 static class EndAction extends TextAction { 1998 1999 2000 EndAction(String nm, boolean select) { 2001 super(nm); 2002 this.select = select; 2003 } 2004 2005 2006 public void actionPerformed(ActionEvent e) { 2007 JTextComponent target = getTextComponent(e); 2008 if (target != null) { 2009 Document doc = target.getDocument(); 2010 int dot = doc.getLength(); 2011 if (select) { 2012 target.moveCaretPosition(dot); 2013 } else { 2014 target.setCaretPosition(dot); 2015 } 2016 } 2017 } 2018 2019 private boolean select; 2020 } 2021 2022 2027 static class SelectWordAction extends TextAction { 2028 2029 2035 SelectWordAction() { 2036 super(selectWordAction); 2037 start = new BeginWordAction("pigdog", false); 2038 end = new EndWordAction("pigdog", true); 2039 } 2040 2041 2042 public void actionPerformed(ActionEvent e) { 2043 start.actionPerformed(e); 2044 end.actionPerformed(e); 2045 } 2046 2047 private Action start; 2048 private Action end; 2049 } 2050 2051 2056 static class SelectLineAction extends TextAction { 2057 2058 2064 SelectLineAction() { 2065 super(selectLineAction); 2066 start = new BeginLineAction("pigdog", false); 2067 end = new EndLineAction("pigdog", true); 2068 } 2069 2070 2071 public void actionPerformed(ActionEvent e) { 2072 start.actionPerformed(e); 2073 end.actionPerformed(e); 2074 } 2075 2076 private Action start; 2077 private Action end; 2078 } 2079 2080 2085 static class SelectParagraphAction extends TextAction { 2086 2087 2093 SelectParagraphAction() { 2094 super(selectParagraphAction); 2095 start = new BeginParagraphAction("pigdog", false); 2096 end = new EndParagraphAction("pigdog", true); 2097 } 2098 2099 2100 public void actionPerformed(ActionEvent e) { 2101 start.actionPerformed(e); 2102 end.actionPerformed(e); 2103 } 2104 2105 private Action start; 2106 private Action end; 2107 } 2108 2109 2114 static class SelectAllAction extends TextAction { 2115 2116 2122 SelectAllAction() { 2123 super(selectAllAction); 2124 } 2125 2126 2127 public void actionPerformed(ActionEvent e) { 2128 JTextComponent target = getTextComponent(e); 2129 if (target != null) { 2130 Document doc = target.getDocument(); 2131 target.setCaretPosition(0); 2132 target.moveCaretPosition(doc.getLength()); 2133 } 2134 } 2135 2136 } 2137 2138 2143 static class UnselectAction extends TextAction { 2144 2145 2148 UnselectAction() { 2149 super(unselectAction); 2150 } 2151 2152 2153 public void actionPerformed(ActionEvent e) { 2154 JTextComponent target = getTextComponent(e); 2155 if (target != null) { 2156 target.setCaretPosition(target.getCaretPosition()); 2157 } 2158 } 2159 2160 } 2161 2162 2167 static class ToggleComponentOrientationAction extends TextAction { 2168 2169 2172 ToggleComponentOrientationAction() { 2173 super(toggleComponentOrientationAction); 2174 } 2175 2176 2177 public void actionPerformed(ActionEvent e) { 2178 JTextComponent target = getTextComponent(e); 2179 if (target != null) { 2180 ComponentOrientation last = target.getComponentOrientation(); 2181 ComponentOrientation next; 2182 if( last == ComponentOrientation.RIGHT_TO_LEFT ) 2183 next = ComponentOrientation.LEFT_TO_RIGHT; 2184 else 2185 next = ComponentOrientation.RIGHT_TO_LEFT; 2186 target.setComponentOrientation(next); 2187 target.repaint(); 2188 } 2189 } 2190 } 2191 2192} 2193 | Popular Tags |