29 import java.io.*;
30 import java.awt.*;
31 import java.awt.event.ActionEvent;
32 import java.text.*;
33 import javax.swing.Action;
34 import javax.swing.KeyStroke;
35 import javax.swing.SwingConstants;
36 import javax.swing.UIManager;
37
38 /**
39 * This is the set of things needed by a text component
40 * to be a reasonably functioning editor for some <em>type</em>
41 * of text document. This implementation provides a default
42 * implementation which treats text as plain text and
43 * provides a minimal set of actions for a simple editor.
44 *
45 * <dl>
46 * <dt><b>Newlines</b>
47 * <dd>
48 * There are two properties which deal with newlines. The
49 * system property, <code>line.separator</code>, is defined to be
50 * platform-dependent, either "\n", "\r", or "\r\n". There is also
51 * a property defined in <code>DefaultEditorKit</code>, called
52 * <a href=#EndOfLineStringProperty><code>EndOfLineStringProperty</code></a>,
53 * which is defined automatically when a document is loaded, to be
54 * the first occurrence of any of the newline characters.
55 * When a document is loaded, <code>EndOfLineStringProperty</code>
56 * is set appropriately, and when the document is written back out, the
57 * <code>EndOfLineStringProperty</code> is used. But while the document
58 * is in memory, the "\n" character is used to define a
59 * newline, regardless of how the newline is defined when
60 * the document is on disk. Therefore, for searching purposes,
61 * "\n" should always be used. When a new document is created,
62 * and the <code>EndOfLineStringProperty</code> has not been defined,
63 * it will use the System property when writing out the
64 * document.
65 * <p>Note that <code>EndOfLineStringProperty</code> is set
66 * on the <code>Document</code> using the <code>get/putProperty</code>
67 * methods. Subclasses may override this behavior.
68 *
69 * </dl>
70 *
71 * @author Timothy Prinzing
72 */
73 @SuppressWarnings("serial") // Same-version serialization only
74 public class DefaultEditorKit extends EditorKit {
75
76 /**
77 * default constructor for DefaultEditorKit
78 */
79 public DefaultEditorKit() {
80 }
81
82 /**
83 * Gets the MIME type of the data that this
84 * kit represents support for. The default
85 * is <code>text/plain</code>.
86 *
87 * @return the type
88 */
89 public String getContentType() {
90 return "text/plain";
91 }
92
93 /**
94 * Fetches a factory that is suitable for producing
95 * views of any models that are produced by this
96 * kit. The default is to have the UI produce the
97 * factory, so this method has no implementation.
98 *
99 * @return the view factory
100 */
101 public ViewFactory getViewFactory() {
102 return null;
103 }
104
105 /**
556 * logically downward one position.
557 * @see #getActions
558 */
559 public static final String downAction = "caret-down";
560
561 /**
562 * Name of the Action for moving the caret
563 * logically upward one position, extending the selection.
564 * @see #getActions
565 */
566 public static final String selectionUpAction = "selection-up";
567
568 /**
569 * Name of the Action for moving the caret
570 * logically downward one position, extending the selection.
571 * @see #getActions
572 */
573 public static final String selectionDownAction = "selection-down";
574
575 /**
576 * Name of the <code>Action</code> for moving the caret
577 * to the beginning of a word.
578 * @see #getActions
579 */
580 public static final String beginWordAction = "caret-begin-word";
581
582 /**
583 * Name of the Action for moving the caret
584 * to the end of a word.
585 * @see #getActions
586 */
587 public static final String endWordAction = "caret-end-word";
588
589 /**
590 * Name of the <code>Action</code> for moving the caret
591 * to the beginning of a word, extending the selection.
592 * @see #getActions
593 */
594 public static final String selectionBeginWordAction = "selection-begin-word";
595
596 /**
597 * Name of the Action for moving the caret
598 * to the end of a word, extending the selection.
599 * @see #getActions
600 */
601 public static final String selectionEndWordAction = "selection-end-word";
602
603 /**
604 * Name of the <code>Action</code> for moving the caret to the
605 * beginning of the previous word.
606 * @see #getActions
607 */
608 public static final String previousWordAction = "caret-previous-word";
609
610 /**
611 * Name of the <code>Action</code> for moving the caret to the
612 * beginning of the next word.
613 * @see #getActions
614 */
615 public static final String nextWordAction = "caret-next-word";
616
617 /**
618 * Name of the <code>Action</code> for moving the selection to the
619 * beginning of the previous word, extending the selection.
620 * @see #getActions
621 */
622 public static final String selectionPreviousWordAction = "selection-previous-word";
623
624 /**
625 * Name of the <code>Action</code> for moving the selection to the
626 * beginning of the next word, extending the selection.
627 * @see #getActions
628 */
629 public static final String selectionNextWordAction = "selection-next-word";
630
631 /**
632 * Name of the <code>Action</code> for moving the caret
633 * to the beginning of a line.
634 * @see #getActions
635 */
636 public static final String beginLineAction = "caret-begin-line";
637
638 /**
639 * Name of the <code>Action</code> for moving the caret
640 * to the end of a line.
641 * @see #getActions
642 */
643 public static final String endLineAction = "caret-end-line";
644
645 /**
646 * Name of the <code>Action</code> for moving the caret
647 * to the beginning of a line, extending the selection.
648 * @see #getActions
649 */
650 public static final String selectionBeginLineAction = "selection-begin-line";
651
652 /**
653 * Name of the <code>Action</code> for moving the caret
654 * to the end of a line, extending the selection.
655 * @see #getActions
656 */
657 public static final String selectionEndLineAction = "selection-end-line";
658
659 /**
660 * Name of the <code>Action</code> for moving the caret
661 * to the beginning of a paragraph.
662 * @see #getActions
663 */
664 public static final String beginParagraphAction = "caret-begin-paragraph";
665
666 /**
667 * Name of the <code>Action</code> for moving the caret
668 * to the end of a paragraph.
669 * @see #getActions
670 */
671 public static final String endParagraphAction = "caret-end-paragraph";
672
673 /**
674 * Name of the <code>Action</code> for moving the caret
675 * to the beginning of a paragraph, extending the selection.
676 * @see #getActions
677 */
678 public static final String selectionBeginParagraphAction = "selection-begin-paragraph";
679
680 /**
681 * Name of the <code>Action</code> for moving the caret
682 * to the end of a paragraph, extending the selection.
683 * @see #getActions
684 */
685 public static final String selectionEndParagraphAction = "selection-end-paragraph";
686
687 /**
688 * Name of the <code>Action</code> for moving the caret
689 * to the beginning of the document.
690 * @see #getActions
691 */
692 public static final String beginAction = "caret-begin";
693
694 /**
695 * Name of the <code>Action</code> for moving the caret
696 * to the end of the document.
697 * @see #getActions
698 */
699 public static final String endAction = "caret-end";
700
701 /**
702 * Name of the <code>Action</code> for moving the caret
703 * to the beginning of the document.
704 * @see #getActions
705 */
706 public static final String selectionBeginAction = "selection-begin";
707
708 /**
709 * Name of the Action for moving the caret
710 * to the end of the document.
711 * @see #getActions
712 */
713 public static final String selectionEndAction = "selection-end";
714
715 /**
716 * Name of the Action for selecting a word around the caret.
717 * @see #getActions
718 */
719 public static final String selectWordAction = "select-word";
720
721 /**
722 * Name of the Action for selecting a line around the caret.
813 new SelectWordAction(), new SelectLineAction(),
814 new SelectParagraphAction(), new SelectAllAction(),
815 new UnselectAction(), new ToggleComponentOrientationAction(),
816 new DumpModelAction()
817 };
818
819 /**
820 * The action that is executed by default if
821 * a <em>key typed event</em> is received and there
822 * is no keymap entry. There is a variation across
823 * different VM's in what gets sent as a <em>key typed</em>
824 * event, and this action tries to filter out the undesired
825 * events. This filters the control characters and those
826 * with the ALT modifier. It allows Control-Alt sequences
827 * through as these form legitimate unicode characters on
828 * some PC keyboards.
829 * <p>
830 * If the event doesn't get filtered, it will try to insert
831 * content into the text editor. The content is fetched
832 * from the command string of the ActionEvent. The text
833 * entry is done through the <code>replaceSelection</code>
834 * method on the target text component. This is the
835 * action that will be fired for most text entry tasks.
836 * <p>
837 * <strong>Warning:</strong>
838 * Serialized objects of this class will not be compatible with
839 * future Swing releases. The current serialization support is
840 * appropriate for short term storage or RMI between applications running
841 * the same version of Swing. As of 1.4, support for long term storage
842 * of all JavaBeans™
843 * has been added to the <code>java.beans</code> package.
844 * Please see {@link java.beans.XMLEncoder}.
845 *
846 * @see DefaultEditorKit#defaultKeyTypedAction
847 * @see DefaultEditorKit#getActions
848 * @see Keymap#setDefaultAction
849 * @see Keymap#getDefaultAction
850 */
851 @SuppressWarnings("serial") // Same-version serialization only
852 public static class DefaultKeyTypedAction extends TextAction {
853
854 /**
855 * Creates this object with the appropriate identifier.
856 */
857 public DefaultKeyTypedAction() {
858 super(defaultKeyTypedAction);
859 }
860
861 /**
862 * The operation to perform when this action is triggered.
863 *
883 if ((c >= 0x20) && (c != 0x7F)) {
884 target.replaceSelection(content);
885 }
886 }
887 }
888 }
889 }
890 }
891
892 /**
893 * Places content into the associated document.
894 * If there is a selection, it is removed before
895 * the new content is added.
896 * <p>
897 * <strong>Warning:</strong>
898 * Serialized objects of this class will not be compatible with
899 * future Swing releases. The current serialization support is
900 * appropriate for short term storage or RMI between applications running
901 * the same version of Swing. As of 1.4, support for long term storage
902 * of all JavaBeans™
903 * has been added to the <code>java.beans</code> package.
904 * Please see {@link java.beans.XMLEncoder}.
905 *
906 * @see DefaultEditorKit#insertContentAction
907 * @see DefaultEditorKit#getActions
908 */
909 @SuppressWarnings("serial") // Same-version serialization only
910 public static class InsertContentAction extends TextAction {
911
912 /**
913 * Creates this object with the appropriate identifier.
914 */
915 public InsertContentAction() {
916 super(insertContentAction);
917 }
918
919 /**
920 * The operation to perform when this action is triggered.
921 *
922 * @param e the action event
923 */
932 if (content != null) {
933 target.replaceSelection(content);
934 } else {
935 UIManager.getLookAndFeel().provideErrorFeedback(target);
936 }
937 }
938 }
939 }
940
941 /**
942 * Places a line/paragraph break into the document.
943 * If there is a selection, it is removed before
944 * the break is added.
945 * <p>
946 * <strong>Warning:</strong>
947 * Serialized objects of this class will not be compatible with
948 * future Swing releases. The current serialization support is
949 * appropriate for short term storage or RMI between applications running
950 * the same version of Swing. As of 1.4, support for long term storage
951 * of all JavaBeans™
952 * has been added to the <code>java.beans</code> package.
953 * Please see {@link java.beans.XMLEncoder}.
954 *
955 * @see DefaultEditorKit#insertBreakAction
956 * @see DefaultEditorKit#getActions
957 */
958 @SuppressWarnings("serial") // Same-version serialization only
959 public static class InsertBreakAction extends TextAction {
960
961 /**
962 * Creates this object with the appropriate identifier.
963 */
964 public InsertBreakAction() {
965 super(insertBreakAction);
966 }
967
968 /**
969 * The operation to perform when this action is triggered.
970 *
971 * @param e the action event
972 */
975 if (target != null) {
976 if ((! target.isEditable()) || (! target.isEnabled())) {
977 UIManager.getLookAndFeel().provideErrorFeedback(target);
978 return;
979 }
980 target.replaceSelection("\n");
981 }
982 }
983 }
984
985 /**
986 * Places a tab character into the document. If there
987 * is a selection, it is removed before the tab is added.
988 * <p>
989 * <strong>Warning:</strong>
990 * Serialized objects of this class will not be compatible with
991 * future Swing releases. The current serialization support is
992 * appropriate for short term storage or RMI between applications running
993 * the same version of Swing. As of 1.4, support for long term storage
994 * of all JavaBeans™
995 * has been added to the <code>java.beans</code> package.
996 * Please see {@link java.beans.XMLEncoder}.
997 *
998 * @see DefaultEditorKit#insertTabAction
999 * @see DefaultEditorKit#getActions
1000 */
1001 @SuppressWarnings("serial") // Same-version serialization only
1002 public static class InsertTabAction extends TextAction {
1003
1004 /**
1005 * Creates this object with the appropriate identifier.
1006 */
1007 public InsertTabAction() {
1008 super(insertTabAction);
1009 }
1010
1011 /**
1012 * The operation to perform when this action is triggered.
1013 *
1014 * @param e the action event
1015 */
1257 * @param e the action event
1258 */
1259 public void actionPerformed(ActionEvent e) {
1260 JTextComponent target = getTextComponent(e);
1261 if (target != null) {
1262 target.setEditable(true);
1263 }
1264 }
1265 }
1266
1267 /**
1268 * Cuts the selected region and place its contents
1269 * into the system clipboard.
1270 * <p>
1271 * <strong>Warning:</strong>
1272 * Serialized objects of this class will not be compatible with
1273 * future Swing releases. The current serialization support is
1274 * appropriate for short term storage or RMI between applications running
1275 * the same version of Swing. As of 1.4, support for long term storage
1276 * of all JavaBeans™
1277 * has been added to the <code>java.beans</code> package.
1278 * Please see {@link java.beans.XMLEncoder}.
1279 *
1280 * @see DefaultEditorKit#cutAction
1281 * @see DefaultEditorKit#getActions
1282 */
1283 @SuppressWarnings("serial") // Same-version serialization only
1284 public static class CutAction extends TextAction {
1285
1286 /** Create this object with the appropriate identifier. */
1287 public CutAction() {
1288 super(cutAction);
1289 }
1290
1291 /**
1292 * The operation to perform when this action is triggered.
1293 *
1294 * @param e the action event
1295 */
1296 public void actionPerformed(ActionEvent e) {
1297 JTextComponent target = getTextComponent(e);
1298 if (target != null) {
1299 target.cut();
1300 }
1301 }
1302 }
1303
1304 /**
1305 * Copies the selected region and place its contents
1306 * into the system clipboard.
1307 * <p>
1308 * <strong>Warning:</strong>
1309 * Serialized objects of this class will not be compatible with
1310 * future Swing releases. The current serialization support is
1311 * appropriate for short term storage or RMI between applications running
1312 * the same version of Swing. As of 1.4, support for long term storage
1313 * of all JavaBeans™
1314 * has been added to the <code>java.beans</code> package.
1315 * Please see {@link java.beans.XMLEncoder}.
1316 *
1317 * @see DefaultEditorKit#copyAction
1318 * @see DefaultEditorKit#getActions
1319 */
1320 @SuppressWarnings("serial") // Same-version serialization only
1321 public static class CopyAction extends TextAction {
1322
1323 /** Create this object with the appropriate identifier. */
1324 public CopyAction() {
1325 super(copyAction);
1326 }
1327
1328 /**
1329 * The operation to perform when this action is triggered.
1330 *
1331 * @param e the action event
1332 */
1333 public void actionPerformed(ActionEvent e) {
1334 JTextComponent target = getTextComponent(e);
1335 if (target != null) {
1336 target.copy();
1337 }
1338 }
1339 }
1340
1341 /**
1342 * Pastes the contents of the system clipboard into the
1343 * selected region, or before the caret if nothing is
1344 * selected.
1345 * <p>
1346 * <strong>Warning:</strong>
1347 * Serialized objects of this class will not be compatible with
1348 * future Swing releases. The current serialization support is
1349 * appropriate for short term storage or RMI between applications running
1350 * the same version of Swing. As of 1.4, support for long term storage
1351 * of all JavaBeans™
1352 * has been added to the <code>java.beans</code> package.
1353 * Please see {@link java.beans.XMLEncoder}.
1354 *
1355 * @see DefaultEditorKit#pasteAction
1356 * @see DefaultEditorKit#getActions
1357 */
1358 @SuppressWarnings("serial") // Same-version serialization only
1359 public static class PasteAction extends TextAction {
1360
1361 /** Create this object with the appropriate identifier. */
1362 public PasteAction() {
1363 super(pasteAction);
1364 }
1365
1366 /**
1367 * The operation to perform when this action is triggered.
1368 *
1369 * @param e the action event
1370 */
1371 public void actionPerformed(ActionEvent e) {
1372 JTextComponent target = getTextComponent(e);
1373 if (target != null) {
1374 target.paste();
1375 }
1376 }
1377 }
1378
1379 /**
1380 * Creates a beep.
1381 * <p>
1382 * <strong>Warning:</strong>
1383 * Serialized objects of this class will not be compatible with
1384 * future Swing releases. The current serialization support is
1385 * appropriate for short term storage or RMI between applications running
1386 * the same version of Swing. As of 1.4, support for long term storage
1387 * of all JavaBeans™
1388 * has been added to the <code>java.beans</code> package.
1389 * Please see {@link java.beans.XMLEncoder}.
1390 *
1391 * @see DefaultEditorKit#beepAction
1392 * @see DefaultEditorKit#getActions
1393 */
1394 @SuppressWarnings("serial") // Same-version serialization only
1395 public static class BeepAction extends TextAction {
1396
1397 /** Create this object with the appropriate identifier. */
1398 public BeepAction() {
1399 super(beepAction);
1400 }
1401
1402 /**
1403 * The operation to perform when this action is triggered.
1404 *
1405 * @param e the action event
1406 */
1407 public void actionPerformed(ActionEvent e) {
1408 JTextComponent target = getTextComponent(e);
1492 if (select) {
1493 target.moveCaretPosition(newIndex);
1494 } else {
1495 target.setCaretPosition(newIndex);
1496 }
1497 }
1498 }
1499 } catch (BadLocationException ble) { }
1500 } else {
1501 newVis.y = constrainY(target,
1502 initialY + scrollAmount, visible.height);
1503 }
1504 if (magicPosition != null) {
1505 caret.setMagicCaretPosition(magicPosition);
1506 }
1507 target.scrollRectToVisible(newVis);
1508 }
1509 }
1510
1511 /**
1512 * Makes sure <code>y</code> is a valid location in
1513 * <code>target</code>.
1514 */
1515 private int constrainY(JTextComponent target, int y, int vis) {
1516 if (y < 0) {
1517 y = 0;
1518 }
1519 else if (y + vis > target.getHeight()) {
1520 y = Math.max(0, target.getHeight() - vis);
1521 }
1522 return y;
1523 }
1524
1525 /**
1526 * Ensures that <code>offset</code> is a valid offset into the
1527 * model for <code>text</code>.
1528 */
1529 private int constrainOffset(JTextComponent text, int offset) {
1530 Document doc = text.getDocument();
1531
1532 if ((offset != 0) && (offset > doc.getLength())) {
1533 offset = doc.getLength();
1534 }
1535 if (offset < 0) {
1536 offset = 0;
1537 }
1538 return offset;
1539 }
1540
1541 /**
1542 * Returns adjustsed {@code y} position that indicates the location to scroll to
1543 * after selecting <code>index</code>.
1544 */
1545 private int getAdjustedY(JTextComponent text, Rectangle visible, int index) {
1546 int result = visible.y;
1547
1548 try {
1549 Rectangle dotBounds = text.modelToView(index);
1550
1551 if (dotBounds.y < visible.y) {
1552 result = dotBounds.y;
1553 } else {
1554 if ((dotBounds.y > visible.y + visible.height) ||
1555 (dotBounds.y + dotBounds.height > visible.y + visible.height)) {
1556 result = dotBounds.y + dotBounds.height - visible.height;
1557 }
1558 }
1559 } catch (BadLocationException ble) {
1560 }
1561
1562 return result;
1563 }
1564
1565 /**
1566 * Adjusts the Rectangle to contain the bounds of the character at
1567 * <code>index</code> in response to a page up.
1568 */
1569 private boolean select;
1570
1571 /**
1572 * Direction to scroll, 1 is down, -1 is up.
1573 */
1574 private int direction;
1575 }
1576
1577
1578 /**
1579 * Pages one view to the left or right.
1580 */
1581 @SuppressWarnings("serial") // Superclass is not serializable across versions
1582 static class PageAction extends TextAction {
1583
1584 /** Create this object with the appropriate identifier. */
1585 public PageAction(String nm, boolean left, boolean select) {
1586 super(nm);
1587 this.select = select;
|
29 import java.io.*;
30 import java.awt.*;
31 import java.awt.event.ActionEvent;
32 import java.text.*;
33 import javax.swing.Action;
34 import javax.swing.KeyStroke;
35 import javax.swing.SwingConstants;
36 import javax.swing.UIManager;
37
38 /**
39 * This is the set of things needed by a text component
40 * to be a reasonably functioning editor for some <em>type</em>
41 * of text document. This implementation provides a default
42 * implementation which treats text as plain text and
43 * provides a minimal set of actions for a simple editor.
44 *
45 * <dl>
46 * <dt><b>Newlines</b>
47 * <dd>
48 * There are two properties which deal with newlines. The
49 * system property, {@code line.separator}, is defined to be
50 * platform-dependent, either "\n", "\r", or "\r\n". There is also
51 * a property defined in {@code DefaultEditorKit}, called
52 * <a href=#EndOfLineStringProperty>{@code EndOfLineStringProperty}</a>,
53 * which is defined automatically when a document is loaded, to be
54 * the first occurrence of any of the newline characters.
55 * When a document is loaded, {@code EndOfLineStringProperty}
56 * is set appropriately, and when the document is written back out, the
57 * {@code EndOfLineStringProperty} is used. But while the document
58 * is in memory, the "\n" character is used to define a
59 * newline, regardless of how the newline is defined when
60 * the document is on disk. Therefore, for searching purposes,
61 * "\n" should always be used. When a new document is created,
62 * and the {@code EndOfLineStringProperty} has not been defined,
63 * it will use the System property when writing out the
64 * document.
65 * <p>Note that {@code EndOfLineStringProperty} is set
66 * on the {@code Document} using the {@code get/putProperty}
67 * methods. Subclasses may override this behavior.
68 *
69 * </dl>
70 *
71 * @author Timothy Prinzing
72 */
73 @SuppressWarnings("serial") // Same-version serialization only
74 public class DefaultEditorKit extends EditorKit {
75
76 /**
77 * default constructor for DefaultEditorKit
78 */
79 public DefaultEditorKit() {
80 }
81
82 /**
83 * Gets the MIME type of the data that this
84 * kit represents support for. The default
85 * is {@code text/plain}.
86 *
87 * @return the type
88 */
89 public String getContentType() {
90 return "text/plain";
91 }
92
93 /**
94 * Fetches a factory that is suitable for producing
95 * views of any models that are produced by this
96 * kit. The default is to have the UI produce the
97 * factory, so this method has no implementation.
98 *
99 * @return the view factory
100 */
101 public ViewFactory getViewFactory() {
102 return null;
103 }
104
105 /**
556 * logically downward one position.
557 * @see #getActions
558 */
559 public static final String downAction = "caret-down";
560
561 /**
562 * Name of the Action for moving the caret
563 * logically upward one position, extending the selection.
564 * @see #getActions
565 */
566 public static final String selectionUpAction = "selection-up";
567
568 /**
569 * Name of the Action for moving the caret
570 * logically downward one position, extending the selection.
571 * @see #getActions
572 */
573 public static final String selectionDownAction = "selection-down";
574
575 /**
576 * Name of the {@code Action} for moving the caret
577 * to the beginning of a word.
578 * @see #getActions
579 */
580 public static final String beginWordAction = "caret-begin-word";
581
582 /**
583 * Name of the Action for moving the caret
584 * to the end of a word.
585 * @see #getActions
586 */
587 public static final String endWordAction = "caret-end-word";
588
589 /**
590 * Name of the {@code Action} for moving the caret
591 * to the beginning of a word, extending the selection.
592 * @see #getActions
593 */
594 public static final String selectionBeginWordAction = "selection-begin-word";
595
596 /**
597 * Name of the Action for moving the caret
598 * to the end of a word, extending the selection.
599 * @see #getActions
600 */
601 public static final String selectionEndWordAction = "selection-end-word";
602
603 /**
604 * Name of the {@code Action} for moving the caret to the
605 * beginning of the previous word.
606 * @see #getActions
607 */
608 public static final String previousWordAction = "caret-previous-word";
609
610 /**
611 * Name of the {@code Action} for moving the caret to the
612 * beginning of the next word.
613 * @see #getActions
614 */
615 public static final String nextWordAction = "caret-next-word";
616
617 /**
618 * Name of the {@code Action} for moving the selection to the
619 * beginning of the previous word, extending the selection.
620 * @see #getActions
621 */
622 public static final String selectionPreviousWordAction = "selection-previous-word";
623
624 /**
625 * Name of the {@code Action} for moving the selection to the
626 * beginning of the next word, extending the selection.
627 * @see #getActions
628 */
629 public static final String selectionNextWordAction = "selection-next-word";
630
631 /**
632 * Name of the {@code Action} for moving the caret
633 * to the beginning of a line.
634 * @see #getActions
635 */
636 public static final String beginLineAction = "caret-begin-line";
637
638 /**
639 * Name of the {@code Action} for moving the caret
640 * to the end of a line.
641 * @see #getActions
642 */
643 public static final String endLineAction = "caret-end-line";
644
645 /**
646 * Name of the {@code Action} for moving the caret
647 * to the beginning of a line, extending the selection.
648 * @see #getActions
649 */
650 public static final String selectionBeginLineAction = "selection-begin-line";
651
652 /**
653 * Name of the {@code Action} for moving the caret
654 * to the end of a line, extending the selection.
655 * @see #getActions
656 */
657 public static final String selectionEndLineAction = "selection-end-line";
658
659 /**
660 * Name of the {@code Action} for moving the caret
661 * to the beginning of a paragraph.
662 * @see #getActions
663 */
664 public static final String beginParagraphAction = "caret-begin-paragraph";
665
666 /**
667 * Name of the {@code Action} for moving the caret
668 * to the end of a paragraph.
669 * @see #getActions
670 */
671 public static final String endParagraphAction = "caret-end-paragraph";
672
673 /**
674 * Name of the {@code Action} for moving the caret
675 * to the beginning of a paragraph, extending the selection.
676 * @see #getActions
677 */
678 public static final String selectionBeginParagraphAction = "selection-begin-paragraph";
679
680 /**
681 * Name of the {@code Action} for moving the caret
682 * to the end of a paragraph, extending the selection.
683 * @see #getActions
684 */
685 public static final String selectionEndParagraphAction = "selection-end-paragraph";
686
687 /**
688 * Name of the {@code Action} for moving the caret
689 * to the beginning of the document.
690 * @see #getActions
691 */
692 public static final String beginAction = "caret-begin";
693
694 /**
695 * Name of the {@code Action} for moving the caret
696 * to the end of the document.
697 * @see #getActions
698 */
699 public static final String endAction = "caret-end";
700
701 /**
702 * Name of the {@code Action} for moving the caret
703 * to the beginning of the document.
704 * @see #getActions
705 */
706 public static final String selectionBeginAction = "selection-begin";
707
708 /**
709 * Name of the Action for moving the caret
710 * to the end of the document.
711 * @see #getActions
712 */
713 public static final String selectionEndAction = "selection-end";
714
715 /**
716 * Name of the Action for selecting a word around the caret.
717 * @see #getActions
718 */
719 public static final String selectWordAction = "select-word";
720
721 /**
722 * Name of the Action for selecting a line around the caret.
813 new SelectWordAction(), new SelectLineAction(),
814 new SelectParagraphAction(), new SelectAllAction(),
815 new UnselectAction(), new ToggleComponentOrientationAction(),
816 new DumpModelAction()
817 };
818
819 /**
820 * The action that is executed by default if
821 * a <em>key typed event</em> is received and there
822 * is no keymap entry. There is a variation across
823 * different VM's in what gets sent as a <em>key typed</em>
824 * event, and this action tries to filter out the undesired
825 * events. This filters the control characters and those
826 * with the ALT modifier. It allows Control-Alt sequences
827 * through as these form legitimate unicode characters on
828 * some PC keyboards.
829 * <p>
830 * If the event doesn't get filtered, it will try to insert
831 * content into the text editor. The content is fetched
832 * from the command string of the ActionEvent. The text
833 * entry is done through the {@code replaceSelection}
834 * method on the target text component. This is the
835 * action that will be fired for most text entry tasks.
836 * <p>
837 * <strong>Warning:</strong>
838 * Serialized objects of this class will not be compatible with
839 * future Swing releases. The current serialization support is
840 * appropriate for short term storage or RMI between applications running
841 * the same version of Swing. As of 1.4, support for long term storage
842 * of all JavaBeans™
843 * has been added to the {@code java.beans} package.
844 * Please see {@link java.beans.XMLEncoder}.
845 *
846 * @see DefaultEditorKit#defaultKeyTypedAction
847 * @see DefaultEditorKit#getActions
848 * @see Keymap#setDefaultAction
849 * @see Keymap#getDefaultAction
850 */
851 @SuppressWarnings("serial") // Same-version serialization only
852 public static class DefaultKeyTypedAction extends TextAction {
853
854 /**
855 * Creates this object with the appropriate identifier.
856 */
857 public DefaultKeyTypedAction() {
858 super(defaultKeyTypedAction);
859 }
860
861 /**
862 * The operation to perform when this action is triggered.
863 *
883 if ((c >= 0x20) && (c != 0x7F)) {
884 target.replaceSelection(content);
885 }
886 }
887 }
888 }
889 }
890 }
891
892 /**
893 * Places content into the associated document.
894 * If there is a selection, it is removed before
895 * the new content is added.
896 * <p>
897 * <strong>Warning:</strong>
898 * Serialized objects of this class will not be compatible with
899 * future Swing releases. The current serialization support is
900 * appropriate for short term storage or RMI between applications running
901 * the same version of Swing. As of 1.4, support for long term storage
902 * of all JavaBeans™
903 * has been added to the {@code java.beans} package.
904 * Please see {@link java.beans.XMLEncoder}.
905 *
906 * @see DefaultEditorKit#insertContentAction
907 * @see DefaultEditorKit#getActions
908 */
909 @SuppressWarnings("serial") // Same-version serialization only
910 public static class InsertContentAction extends TextAction {
911
912 /**
913 * Creates this object with the appropriate identifier.
914 */
915 public InsertContentAction() {
916 super(insertContentAction);
917 }
918
919 /**
920 * The operation to perform when this action is triggered.
921 *
922 * @param e the action event
923 */
932 if (content != null) {
933 target.replaceSelection(content);
934 } else {
935 UIManager.getLookAndFeel().provideErrorFeedback(target);
936 }
937 }
938 }
939 }
940
941 /**
942 * Places a line/paragraph break into the document.
943 * If there is a selection, it is removed before
944 * the break is added.
945 * <p>
946 * <strong>Warning:</strong>
947 * Serialized objects of this class will not be compatible with
948 * future Swing releases. The current serialization support is
949 * appropriate for short term storage or RMI between applications running
950 * the same version of Swing. As of 1.4, support for long term storage
951 * of all JavaBeans™
952 * has been added to the {@code java.beans} package.
953 * Please see {@link java.beans.XMLEncoder}.
954 *
955 * @see DefaultEditorKit#insertBreakAction
956 * @see DefaultEditorKit#getActions
957 */
958 @SuppressWarnings("serial") // Same-version serialization only
959 public static class InsertBreakAction extends TextAction {
960
961 /**
962 * Creates this object with the appropriate identifier.
963 */
964 public InsertBreakAction() {
965 super(insertBreakAction);
966 }
967
968 /**
969 * The operation to perform when this action is triggered.
970 *
971 * @param e the action event
972 */
975 if (target != null) {
976 if ((! target.isEditable()) || (! target.isEnabled())) {
977 UIManager.getLookAndFeel().provideErrorFeedback(target);
978 return;
979 }
980 target.replaceSelection("\n");
981 }
982 }
983 }
984
985 /**
986 * Places a tab character into the document. If there
987 * is a selection, it is removed before the tab is added.
988 * <p>
989 * <strong>Warning:</strong>
990 * Serialized objects of this class will not be compatible with
991 * future Swing releases. The current serialization support is
992 * appropriate for short term storage or RMI between applications running
993 * the same version of Swing. As of 1.4, support for long term storage
994 * of all JavaBeans™
995 * has been added to the {@code java.beans} package.
996 * Please see {@link java.beans.XMLEncoder}.
997 *
998 * @see DefaultEditorKit#insertTabAction
999 * @see DefaultEditorKit#getActions
1000 */
1001 @SuppressWarnings("serial") // Same-version serialization only
1002 public static class InsertTabAction extends TextAction {
1003
1004 /**
1005 * Creates this object with the appropriate identifier.
1006 */
1007 public InsertTabAction() {
1008 super(insertTabAction);
1009 }
1010
1011 /**
1012 * The operation to perform when this action is triggered.
1013 *
1014 * @param e the action event
1015 */
1257 * @param e the action event
1258 */
1259 public void actionPerformed(ActionEvent e) {
1260 JTextComponent target = getTextComponent(e);
1261 if (target != null) {
1262 target.setEditable(true);
1263 }
1264 }
1265 }
1266
1267 /**
1268 * Cuts the selected region and place its contents
1269 * into the system clipboard.
1270 * <p>
1271 * <strong>Warning:</strong>
1272 * Serialized objects of this class will not be compatible with
1273 * future Swing releases. The current serialization support is
1274 * appropriate for short term storage or RMI between applications running
1275 * the same version of Swing. As of 1.4, support for long term storage
1276 * of all JavaBeans™
1277 * has been added to the {@code java.beans} package.
1278 * Please see {@link java.beans.XMLEncoder}.
1279 *
1280 * @see DefaultEditorKit#cutAction
1281 * @see DefaultEditorKit#getActions
1282 */
1283 @SuppressWarnings("serial") // Same-version serialization only
1284 public static class CutAction extends TextAction {
1285
1286 /** Create this object with the appropriate identifier. */
1287 public CutAction() {
1288 super(cutAction);
1289 }
1290
1291 /**
1292 * The operation to perform when this action is triggered.
1293 *
1294 * @param e the action event
1295 */
1296 public void actionPerformed(ActionEvent e) {
1297 JTextComponent target = getTextComponent(e);
1298 if (target != null) {
1299 target.cut();
1300 }
1301 }
1302 }
1303
1304 /**
1305 * Copies the selected region and place its contents
1306 * into the system clipboard.
1307 * <p>
1308 * <strong>Warning:</strong>
1309 * Serialized objects of this class will not be compatible with
1310 * future Swing releases. The current serialization support is
1311 * appropriate for short term storage or RMI between applications running
1312 * the same version of Swing. As of 1.4, support for long term storage
1313 * of all JavaBeans™
1314 * has been added to the {@code java.beans} package.
1315 * Please see {@link java.beans.XMLEncoder}.
1316 *
1317 * @see DefaultEditorKit#copyAction
1318 * @see DefaultEditorKit#getActions
1319 */
1320 @SuppressWarnings("serial") // Same-version serialization only
1321 public static class CopyAction extends TextAction {
1322
1323 /** Create this object with the appropriate identifier. */
1324 public CopyAction() {
1325 super(copyAction);
1326 }
1327
1328 /**
1329 * The operation to perform when this action is triggered.
1330 *
1331 * @param e the action event
1332 */
1333 public void actionPerformed(ActionEvent e) {
1334 JTextComponent target = getTextComponent(e);
1335 if (target != null) {
1336 target.copy();
1337 }
1338 }
1339 }
1340
1341 /**
1342 * Pastes the contents of the system clipboard into the
1343 * selected region, or before the caret if nothing is
1344 * selected.
1345 * <p>
1346 * <strong>Warning:</strong>
1347 * Serialized objects of this class will not be compatible with
1348 * future Swing releases. The current serialization support is
1349 * appropriate for short term storage or RMI between applications running
1350 * the same version of Swing. As of 1.4, support for long term storage
1351 * of all JavaBeans™
1352 * has been added to the {@code java.beans} package.
1353 * Please see {@link java.beans.XMLEncoder}.
1354 *
1355 * @see DefaultEditorKit#pasteAction
1356 * @see DefaultEditorKit#getActions
1357 */
1358 @SuppressWarnings("serial") // Same-version serialization only
1359 public static class PasteAction extends TextAction {
1360
1361 /** Create this object with the appropriate identifier. */
1362 public PasteAction() {
1363 super(pasteAction);
1364 }
1365
1366 /**
1367 * The operation to perform when this action is triggered.
1368 *
1369 * @param e the action event
1370 */
1371 public void actionPerformed(ActionEvent e) {
1372 JTextComponent target = getTextComponent(e);
1373 if (target != null) {
1374 target.paste();
1375 }
1376 }
1377 }
1378
1379 /**
1380 * Creates a beep.
1381 * <p>
1382 * <strong>Warning:</strong>
1383 * Serialized objects of this class will not be compatible with
1384 * future Swing releases. The current serialization support is
1385 * appropriate for short term storage or RMI between applications running
1386 * the same version of Swing. As of 1.4, support for long term storage
1387 * of all JavaBeans™
1388 * has been added to the {@code java.beans} package.
1389 * Please see {@link java.beans.XMLEncoder}.
1390 *
1391 * @see DefaultEditorKit#beepAction
1392 * @see DefaultEditorKit#getActions
1393 */
1394 @SuppressWarnings("serial") // Same-version serialization only
1395 public static class BeepAction extends TextAction {
1396
1397 /** Create this object with the appropriate identifier. */
1398 public BeepAction() {
1399 super(beepAction);
1400 }
1401
1402 /**
1403 * The operation to perform when this action is triggered.
1404 *
1405 * @param e the action event
1406 */
1407 public void actionPerformed(ActionEvent e) {
1408 JTextComponent target = getTextComponent(e);
1492 if (select) {
1493 target.moveCaretPosition(newIndex);
1494 } else {
1495 target.setCaretPosition(newIndex);
1496 }
1497 }
1498 }
1499 } catch (BadLocationException ble) { }
1500 } else {
1501 newVis.y = constrainY(target,
1502 initialY + scrollAmount, visible.height);
1503 }
1504 if (magicPosition != null) {
1505 caret.setMagicCaretPosition(magicPosition);
1506 }
1507 target.scrollRectToVisible(newVis);
1508 }
1509 }
1510
1511 /**
1512 * Makes sure {@code y} is a valid location in
1513 * {@code target}.
1514 */
1515 private int constrainY(JTextComponent target, int y, int vis) {
1516 if (y < 0) {
1517 y = 0;
1518 }
1519 else if (y + vis > target.getHeight()) {
1520 y = Math.max(0, target.getHeight() - vis);
1521 }
1522 return y;
1523 }
1524
1525 /**
1526 * Ensures that {@code offset} is a valid offset into the
1527 * model for {@code text}.
1528 */
1529 private int constrainOffset(JTextComponent text, int offset) {
1530 Document doc = text.getDocument();
1531
1532 if ((offset != 0) && (offset > doc.getLength())) {
1533 offset = doc.getLength();
1534 }
1535 if (offset < 0) {
1536 offset = 0;
1537 }
1538 return offset;
1539 }
1540
1541 /**
1542 * Returns adjustsed {@code y} position that indicates the location to scroll to
1543 * after selecting {@code index}.
1544 */
1545 private int getAdjustedY(JTextComponent text, Rectangle visible, int index) {
1546 int result = visible.y;
1547
1548 try {
1549 Rectangle dotBounds = text.modelToView(index);
1550
1551 if (dotBounds.y < visible.y) {
1552 result = dotBounds.y;
1553 } else {
1554 if ((dotBounds.y > visible.y + visible.height) ||
1555 (dotBounds.y + dotBounds.height > visible.y + visible.height)) {
1556 result = dotBounds.y + dotBounds.height - visible.height;
1557 }
1558 }
1559 } catch (BadLocationException ble) {
1560 }
1561
1562 return result;
1563 }
1564
1565 /**
1566 * Adjusts the Rectangle to contain the bounds of the character at
1567 * {@code index} in response to a page up.
1568 */
1569 private boolean select;
1570
1571 /**
1572 * Direction to scroll, 1 is down, -1 is up.
1573 */
1574 private int direction;
1575 }
1576
1577
1578 /**
1579 * Pages one view to the left or right.
1580 */
1581 @SuppressWarnings("serial") // Superclass is not serializable across versions
1582 static class PageAction extends TextAction {
1583
1584 /** Create this object with the appropriate identifier. */
1585 public PageAction(String nm, boolean left, boolean select) {
1586 super(nm);
1587 this.select = select;
|