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 public class DefaultEditorKit extends EditorKit {
74
75 /**
76 * default constructor for DefaultEditorKit
77 */
78 public DefaultEditorKit() {
79 }
80
81 /**
82 * Gets the MIME type of the data that this
83 * kit represents support for. The default
84 * is <code>text/plain</code>.
85 *
86 * @return the type
87 */
88 public String getContentType() {
89 return "text/plain";
90 }
91
92 /**
93 * Fetches a factory that is suitable for producing
1015 * @param e the action event
1016 */
1017 public void actionPerformed(ActionEvent e) {
1018 JTextComponent target = getTextComponent(e);
1019 if (target != null) {
1020 if ((! target.isEditable()) || (! target.isEnabled())) {
1021 UIManager.getLookAndFeel().provideErrorFeedback(target);
1022 return;
1023 }
1024 target.replaceSelection("\t");
1025 }
1026 }
1027 }
1028
1029 /*
1030 * Deletes the character of content that precedes the
1031 * current caret position.
1032 * @see DefaultEditorKit#deletePrevCharAction
1033 * @see DefaultEditorKit#getActions
1034 */
1035 static class DeletePrevCharAction extends TextAction {
1036
1037 /**
1038 * Creates this object with the appropriate identifier.
1039 */
1040 DeletePrevCharAction() {
1041 super(deletePrevCharAction);
1042 }
1043
1044 /**
1045 * The operation to perform when this action is triggered.
1046 *
1047 * @param e the action event
1048 */
1049 public void actionPerformed(ActionEvent e) {
1050 JTextComponent target = getTextComponent(e);
1051 boolean beep = true;
1052 if ((target != null) && (target.isEditable())) {
1053 try {
1054 Document doc = target.getDocument();
1073 }
1074
1075 doc.remove(dot - delChars, delChars);
1076 beep = false;
1077 }
1078 } catch (BadLocationException bl) {
1079 }
1080 }
1081 if (beep) {
1082 UIManager.getLookAndFeel().provideErrorFeedback(target);
1083 }
1084 }
1085 }
1086
1087 /*
1088 * Deletes the character of content that follows the
1089 * current caret position.
1090 * @see DefaultEditorKit#deleteNextCharAction
1091 * @see DefaultEditorKit#getActions
1092 */
1093 static class DeleteNextCharAction extends TextAction {
1094
1095 /* Create this object with the appropriate identifier. */
1096 DeleteNextCharAction() {
1097 super(deleteNextCharAction);
1098 }
1099
1100 /** The operation to perform when this action is triggered. */
1101 public void actionPerformed(ActionEvent e) {
1102 JTextComponent target = getTextComponent(e);
1103 boolean beep = true;
1104 if ((target != null) && (target.isEditable())) {
1105 try {
1106 Document doc = target.getDocument();
1107 Caret caret = target.getCaret();
1108 int dot = caret.getDot();
1109 int mark = caret.getMark();
1110 if (dot != mark) {
1111 doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
1112 beep = false;
1124 }
1125 }
1126
1127 doc.remove(dot, delChars);
1128 beep = false;
1129 }
1130 } catch (BadLocationException bl) {
1131 }
1132 }
1133 if (beep) {
1134 UIManager.getLookAndFeel().provideErrorFeedback(target);
1135 }
1136 }
1137 }
1138
1139
1140 /*
1141 * Deletes the word that precedes/follows the beginning of the selection.
1142 * @see DefaultEditorKit#getActions
1143 */
1144 static class DeleteWordAction extends TextAction {
1145 DeleteWordAction(String name) {
1146 super(name);
1147 assert (name == deletePrevWordAction)
1148 || (name == deleteNextWordAction);
1149 }
1150 /**
1151 * The operation to perform when this action is triggered.
1152 *
1153 * @param e the action event
1154 */
1155 public void actionPerformed(ActionEvent e) {
1156 final JTextComponent target = getTextComponent(e);
1157 if ((target != null) && (e != null)) {
1158 if ((! target.isEditable()) || (! target.isEnabled())) {
1159 UIManager.getLookAndFeel().provideErrorFeedback(target);
1160 return;
1161 }
1162 boolean beep = true;
1163 try {
1198 int len = Math.abs(end - start);
1199 if (offs >= 0) {
1200 target.getDocument().remove(offs, len);
1201 beep = false;
1202 }
1203 } catch (BadLocationException ignore) {
1204 }
1205 if (beep) {
1206 UIManager.getLookAndFeel().provideErrorFeedback(target);
1207 }
1208 }
1209 }
1210 }
1211
1212
1213 /*
1214 * Sets the editor into read-only mode.
1215 * @see DefaultEditorKit#readOnlyAction
1216 * @see DefaultEditorKit#getActions
1217 */
1218 static class ReadOnlyAction extends TextAction {
1219
1220 /* Create this object with the appropriate identifier. */
1221 ReadOnlyAction() {
1222 super(readOnlyAction);
1223 }
1224
1225 /**
1226 * The operation to perform when this action is triggered.
1227 *
1228 * @param e the action event
1229 */
1230 public void actionPerformed(ActionEvent e) {
1231 JTextComponent target = getTextComponent(e);
1232 if (target != null) {
1233 target.setEditable(false);
1234 }
1235 }
1236 }
1237
1238 /*
1239 * Sets the editor into writeable mode.
1240 * @see DefaultEditorKit#writableAction
1241 * @see DefaultEditorKit#getActions
1242 */
1243 static class WritableAction extends TextAction {
1244
1245 /* Create this object with the appropriate identifier. */
1246 WritableAction() {
1247 super(writableAction);
1248 }
1249
1250 /**
1251 * The operation to perform when this action is triggered.
1252 *
1253 * @param e the action event
1254 */
1255 public void actionPerformed(ActionEvent e) {
1256 JTextComponent target = getTextComponent(e);
1257 if (target != null) {
1258 target.setEditable(true);
1259 }
1260 }
1261 }
1262
1397
1398 /**
1399 * The operation to perform when this action is triggered.
1400 *
1401 * @param e the action event
1402 */
1403 public void actionPerformed(ActionEvent e) {
1404 JTextComponent target = getTextComponent(e);
1405 UIManager.getLookAndFeel().provideErrorFeedback(target);
1406 }
1407 }
1408
1409 /**
1410 * Scrolls up/down vertically. The select version of this action extends
1411 * the selection, instead of simply moving the caret.
1412 *
1413 * @see DefaultEditorKit#pageUpAction
1414 * @see DefaultEditorKit#pageDownAction
1415 * @see DefaultEditorKit#getActions
1416 */
1417 static class VerticalPageAction extends TextAction {
1418
1419 /** Create this object with the appropriate identifier. */
1420 public VerticalPageAction(String nm, int direction, boolean select) {
1421 super(nm);
1422 this.select = select;
1423 this.direction = direction;
1424 }
1425
1426 /** The operation to perform when this action is triggered. */
1427 public void actionPerformed(ActionEvent e) {
1428 JTextComponent target = getTextComponent(e);
1429 if (target != null) {
1430 Rectangle visible = target.getVisibleRect();
1431 Rectangle newVis = new Rectangle(visible);
1432 int selectedIndex = target.getCaretPosition();
1433 int scrollAmount = direction *
1434 target.getScrollableBlockIncrement(
1435 visible, SwingConstants.VERTICAL, direction);
1436 int initialY = visible.y;
1556
1557 return result;
1558 }
1559
1560 /**
1561 * Adjusts the Rectangle to contain the bounds of the character at
1562 * <code>index</code> in response to a page up.
1563 */
1564 private boolean select;
1565
1566 /**
1567 * Direction to scroll, 1 is down, -1 is up.
1568 */
1569 private int direction;
1570 }
1571
1572
1573 /**
1574 * Pages one view to the left or right.
1575 */
1576 static class PageAction extends TextAction {
1577
1578 /** Create this object with the appropriate identifier. */
1579 public PageAction(String nm, boolean left, boolean select) {
1580 super(nm);
1581 this.select = select;
1582 this.left = left;
1583 }
1584
1585 /** The operation to perform when this action is triggered. */
1586 public void actionPerformed(ActionEvent e) {
1587 JTextComponent target = getTextComponent(e);
1588 if (target != null) {
1589 int selectedIndex;
1590 Rectangle visible = new Rectangle();
1591 target.computeVisibleRect(visible);
1592 if (left) {
1593 visible.x = Math.max(0, visible.x - visible.width);
1594 }
1595 else {
1610 Document doc = target.getDocument();
1611 if ((selectedIndex != 0) &&
1612 (selectedIndex > (doc.getLength()-1))) {
1613 selectedIndex = doc.getLength()-1;
1614 }
1615 else if(selectedIndex < 0) {
1616 selectedIndex = 0;
1617 }
1618 if (select)
1619 target.moveCaretPosition(selectedIndex);
1620 else
1621 target.setCaretPosition(selectedIndex);
1622 }
1623 }
1624 }
1625
1626 private boolean select;
1627 private boolean left;
1628 }
1629
1630 static class DumpModelAction extends TextAction {
1631
1632 DumpModelAction() {
1633 super("dump-model");
1634 }
1635
1636 public void actionPerformed(ActionEvent e) {
1637 JTextComponent target = getTextComponent(e);
1638 if (target != null) {
1639 Document d = target.getDocument();
1640 if (d instanceof AbstractDocument) {
1641 ((AbstractDocument) d).dump(System.err);
1642 }
1643 }
1644 }
1645 }
1646
1647 /*
1648 * Action to move the selection by way of the
1649 * getNextVisualPositionFrom method. Constructor indicates direction
1650 * to use.
1651 */
1652 static class NextVisualPositionAction extends TextAction {
1653
1654 /**
1655 * Create this action with the appropriate identifier.
1656 * @param nm the name of the action, Action.NAME.
1657 * @param select whether to extend the selection when
1658 * changing the caret position.
1659 */
1660 NextVisualPositionAction(String nm, boolean select, int direction) {
1661 super(nm);
1662 this.select = select;
1663 this.direction = direction;
1664 }
1665
1666 /** The operation to perform when this action is triggered. */
1667 public void actionPerformed(ActionEvent e) {
1668 JTextComponent target = getTextComponent(e);
1669 if (target != null) {
1670 Caret caret = target.getCaret();
1671 DefaultCaret bidiCaret = (caret instanceof DefaultCaret) ?
1719 if(magicPosition != null &&
1720 (direction == SwingConstants.NORTH ||
1721 direction == SwingConstants.SOUTH)) {
1722 target.getCaret().setMagicCaretPosition(magicPosition);
1723 }
1724 } catch (BadLocationException ex) {
1725 }
1726 }
1727 }
1728
1729 private boolean select;
1730 private int direction;
1731 }
1732
1733 /*
1734 * Position the caret to the beginning of the word.
1735 * @see DefaultEditorKit#beginWordAction
1736 * @see DefaultEditorKit#selectBeginWordAction
1737 * @see DefaultEditorKit#getActions
1738 */
1739 static class BeginWordAction extends TextAction {
1740
1741 /**
1742 * Create this action with the appropriate identifier.
1743 * @param nm the name of the action, Action.NAME.
1744 * @param select whether to extend the selection when
1745 * changing the caret position.
1746 */
1747 BeginWordAction(String nm, boolean select) {
1748 super(nm);
1749 this.select = select;
1750 }
1751
1752 /** The operation to perform when this action is triggered. */
1753 public void actionPerformed(ActionEvent e) {
1754 JTextComponent target = getTextComponent(e);
1755 if (target != null) {
1756 try {
1757 int offs = target.getCaretPosition();
1758 int begOffs = Utilities.getWordStart(target, offs);
1759 if (select) {
1760 target.moveCaretPosition(begOffs);
1761 } else {
1762 target.setCaretPosition(begOffs);
1763 }
1764 } catch (BadLocationException bl) {
1765 UIManager.getLookAndFeel().provideErrorFeedback(target);
1766 }
1767 }
1768 }
1769
1770 private boolean select;
1771 }
1772
1773 /*
1774 * Position the caret to the end of the word.
1775 * @see DefaultEditorKit#endWordAction
1776 * @see DefaultEditorKit#selectEndWordAction
1777 * @see DefaultEditorKit#getActions
1778 */
1779 static class EndWordAction extends TextAction {
1780
1781 /**
1782 * Create this action with the appropriate identifier.
1783 * @param nm the name of the action, Action.NAME.
1784 * @param select whether to extend the selection when
1785 * changing the caret position.
1786 */
1787 EndWordAction(String nm, boolean select) {
1788 super(nm);
1789 this.select = select;
1790 }
1791
1792 /** The operation to perform when this action is triggered. */
1793 public void actionPerformed(ActionEvent e) {
1794 JTextComponent target = getTextComponent(e);
1795 if (target != null) {
1796 try {
1797 int offs = target.getCaretPosition();
1798 int endOffs = Utilities.getWordEnd(target, offs);
1799 if (select) {
1800 target.moveCaretPosition(endOffs);
1801 } else {
1802 target.setCaretPosition(endOffs);
1803 }
1804 } catch (BadLocationException bl) {
1805 UIManager.getLookAndFeel().provideErrorFeedback(target);
1806 }
1807 }
1808 }
1809
1810 private boolean select;
1811 }
1812
1813 /*
1814 * Position the caret to the beginning of the previous word.
1815 * @see DefaultEditorKit#previousWordAction
1816 * @see DefaultEditorKit#selectPreviousWordAction
1817 * @see DefaultEditorKit#getActions
1818 */
1819 static class PreviousWordAction extends TextAction {
1820
1821 /**
1822 * Create this action with the appropriate identifier.
1823 * @param nm the name of the action, Action.NAME.
1824 * @param select whether to extend the selection when
1825 * changing the caret position.
1826 */
1827 PreviousWordAction(String nm, boolean select) {
1828 super(nm);
1829 this.select = select;
1830 }
1831
1832 /** The operation to perform when this action is triggered. */
1833 public void actionPerformed(ActionEvent e) {
1834 JTextComponent target = getTextComponent(e);
1835 if (target != null) {
1836 int offs = target.getCaretPosition();
1837 boolean failed = false;
1838 try {
1858 target.moveCaretPosition(offs);
1859 } else {
1860 target.setCaretPosition(offs);
1861 }
1862 }
1863 else {
1864 UIManager.getLookAndFeel().provideErrorFeedback(target);
1865 }
1866 }
1867 }
1868
1869 private boolean select;
1870 }
1871
1872 /*
1873 * Position the caret to the next of the word.
1874 * @see DefaultEditorKit#nextWordAction
1875 * @see DefaultEditorKit#selectNextWordAction
1876 * @see DefaultEditorKit#getActions
1877 */
1878 static class NextWordAction extends TextAction {
1879
1880 /**
1881 * Create this action with the appropriate identifier.
1882 * @param nm the name of the action, Action.NAME.
1883 * @param select whether to extend the selection when
1884 * changing the caret position.
1885 */
1886 NextWordAction(String nm, boolean select) {
1887 super(nm);
1888 this.select = select;
1889 }
1890
1891 /** The operation to perform when this action is triggered. */
1892 public void actionPerformed(ActionEvent e) {
1893 JTextComponent target = getTextComponent(e);
1894 if (target != null) {
1895 int offs = target.getCaretPosition();
1896 boolean failed = false;
1897 int oldOffs = offs;
1923 target.moveCaretPosition(offs);
1924 } else {
1925 target.setCaretPosition(offs);
1926 }
1927 }
1928 else {
1929 UIManager.getLookAndFeel().provideErrorFeedback(target);
1930 }
1931 }
1932 }
1933
1934 private boolean select;
1935 }
1936
1937 /*
1938 * Position the caret to the beginning of the line.
1939 * @see DefaultEditorKit#beginLineAction
1940 * @see DefaultEditorKit#selectBeginLineAction
1941 * @see DefaultEditorKit#getActions
1942 */
1943 static class BeginLineAction extends TextAction {
1944
1945 /**
1946 * Create this action with the appropriate identifier.
1947 * @param nm the name of the action, Action.NAME.
1948 * @param select whether to extend the selection when
1949 * changing the caret position.
1950 */
1951 BeginLineAction(String nm, boolean select) {
1952 super(nm);
1953 this.select = select;
1954 }
1955
1956 /** The operation to perform when this action is triggered. */
1957 public void actionPerformed(ActionEvent e) {
1958 JTextComponent target = getTextComponent(e);
1959 if (target != null) {
1960 try {
1961 int offs = target.getCaretPosition();
1962 int begOffs = Utilities.getRowStart(target, offs);
1963 if (select) {
1964 target.moveCaretPosition(begOffs);
1965 } else {
1966 target.setCaretPosition(begOffs);
1967 }
1968 } catch (BadLocationException bl) {
1969 UIManager.getLookAndFeel().provideErrorFeedback(target);
1970 }
1971 }
1972 }
1973
1974 private boolean select;
1975 }
1976
1977 /*
1978 * Position the caret to the end of the line.
1979 * @see DefaultEditorKit#endLineAction
1980 * @see DefaultEditorKit#selectEndLineAction
1981 * @see DefaultEditorKit#getActions
1982 */
1983 static class EndLineAction extends TextAction {
1984
1985 /**
1986 * Create this action with the appropriate identifier.
1987 * @param nm the name of the action, Action.NAME.
1988 * @param select whether to extend the selection when
1989 * changing the caret position.
1990 */
1991 EndLineAction(String nm, boolean select) {
1992 super(nm);
1993 this.select = select;
1994 }
1995
1996 /** The operation to perform when this action is triggered. */
1997 public void actionPerformed(ActionEvent e) {
1998 JTextComponent target = getTextComponent(e);
1999 if (target != null) {
2000 try {
2001 int offs = target.getCaretPosition();
2002 int endOffs = Utilities.getRowEnd(target, offs);
2003 if (select) {
2004 target.moveCaretPosition(endOffs);
2005 } else {
2006 target.setCaretPosition(endOffs);
2007 }
2008 } catch (BadLocationException bl) {
2009 UIManager.getLookAndFeel().provideErrorFeedback(target);
2010 }
2011 }
2012 }
2013
2014 private boolean select;
2015 }
2016
2017 /*
2018 * Position the caret to the beginning of the paragraph.
2019 * @see DefaultEditorKit#beginParagraphAction
2020 * @see DefaultEditorKit#selectBeginParagraphAction
2021 * @see DefaultEditorKit#getActions
2022 */
2023 static class BeginParagraphAction extends TextAction {
2024
2025 /**
2026 * Create this action with the appropriate identifier.
2027 * @param nm the name of the action, Action.NAME.
2028 * @param select whether to extend the selection when
2029 * changing the caret position.
2030 */
2031 BeginParagraphAction(String nm, boolean select) {
2032 super(nm);
2033 this.select = select;
2034 }
2035
2036 /** The operation to perform when this action is triggered. */
2037 public void actionPerformed(ActionEvent e) {
2038 JTextComponent target = getTextComponent(e);
2039 if (target != null) {
2040 int offs = target.getCaretPosition();
2041 Element elem = Utilities.getParagraphElement(target, offs);
2042 offs = elem.getStartOffset();
2043 if (select) {
2044 target.moveCaretPosition(offs);
2045 } else {
2046 target.setCaretPosition(offs);
2047 }
2048 }
2049 }
2050
2051 private boolean select;
2052 }
2053
2054 /*
2055 * Position the caret to the end of the paragraph.
2056 * @see DefaultEditorKit#endParagraphAction
2057 * @see DefaultEditorKit#selectEndParagraphAction
2058 * @see DefaultEditorKit#getActions
2059 */
2060 static class EndParagraphAction extends TextAction {
2061
2062 /**
2063 * Create this action with the appropriate identifier.
2064 * @param nm the name of the action, Action.NAME.
2065 * @param select whether to extend the selection when
2066 * changing the caret position.
2067 */
2068 EndParagraphAction(String nm, boolean select) {
2069 super(nm);
2070 this.select = select;
2071 }
2072
2073 /** The operation to perform when this action is triggered. */
2074 public void actionPerformed(ActionEvent e) {
2075 JTextComponent target = getTextComponent(e);
2076 if (target != null) {
2077 int offs = target.getCaretPosition();
2078 Element elem = Utilities.getParagraphElement(target, offs);
2079 offs = Math.min(target.getDocument().getLength(),
2080 elem.getEndOffset());
2081 if (select) {
2082 target.moveCaretPosition(offs);
2083 } else {
2084 target.setCaretPosition(offs);
2085 }
2086 }
2087 }
2088
2089 private boolean select;
2090 }
2091
2092 /*
2093 * Move the caret to the beginning of the document.
2094 * @see DefaultEditorKit#beginAction
2095 * @see DefaultEditorKit#getActions
2096 */
2097 static class BeginAction extends TextAction {
2098
2099 /* Create this object with the appropriate identifier. */
2100 BeginAction(String nm, boolean select) {
2101 super(nm);
2102 this.select = select;
2103 }
2104
2105 /** The operation to perform when this action is triggered. */
2106 public void actionPerformed(ActionEvent e) {
2107 JTextComponent target = getTextComponent(e);
2108 if (target != null) {
2109 if (select) {
2110 target.moveCaretPosition(0);
2111 } else {
2112 target.setCaretPosition(0);
2113 }
2114 }
2115 }
2116
2117 private boolean select;
2118 }
2119
2120 /*
2121 * Move the caret to the end of the document.
2122 * @see DefaultEditorKit#endAction
2123 * @see DefaultEditorKit#getActions
2124 */
2125 static class EndAction extends TextAction {
2126
2127 /* Create this object with the appropriate identifier. */
2128 EndAction(String nm, boolean select) {
2129 super(nm);
2130 this.select = select;
2131 }
2132
2133 /** The operation to perform when this action is triggered. */
2134 public void actionPerformed(ActionEvent e) {
2135 JTextComponent target = getTextComponent(e);
2136 if (target != null) {
2137 Document doc = target.getDocument();
2138 int dot = doc.getLength();
2139 if (select) {
2140 target.moveCaretPosition(dot);
2141 } else {
2142 target.setCaretPosition(dot);
2143 }
2144 }
2145 }
2146
2147 private boolean select;
2148 }
2149
2150 /*
2151 * Select the word around the caret
2152 * @see DefaultEditorKit#endAction
2153 * @see DefaultEditorKit#getActions
2154 */
2155 static class SelectWordAction extends TextAction {
2156
2157 /**
2158 * Create this action with the appropriate identifier.
2159 * @param nm the name of the action, Action.NAME.
2160 * @param select whether to extend the selection when
2161 * changing the caret position.
2162 */
2163 SelectWordAction() {
2164 super(selectWordAction);
2165 start = new BeginWordAction("pigdog", false);
2166 end = new EndWordAction("pigdog", true);
2167 }
2168
2169 /** The operation to perform when this action is triggered. */
2170 public void actionPerformed(ActionEvent e) {
2171 start.actionPerformed(e);
2172 end.actionPerformed(e);
2173 }
2174
2175 private Action start;
2176 private Action end;
2177 }
2178
2179 /*
2180 * Select the line around the caret
2181 * @see DefaultEditorKit#endAction
2182 * @see DefaultEditorKit#getActions
2183 */
2184 static class SelectLineAction extends TextAction {
2185
2186 /**
2187 * Create this action with the appropriate identifier.
2188 * @param nm the name of the action, Action.NAME.
2189 * @param select whether to extend the selection when
2190 * changing the caret position.
2191 */
2192 SelectLineAction() {
2193 super(selectLineAction);
2194 start = new BeginLineAction("pigdog", false);
2195 end = new EndLineAction("pigdog", true);
2196 }
2197
2198 /** The operation to perform when this action is triggered. */
2199 public void actionPerformed(ActionEvent e) {
2200 start.actionPerformed(e);
2201 end.actionPerformed(e);
2202 }
2203
2204 private Action start;
2205 private Action end;
2206 }
2207
2208 /*
2209 * Select the paragraph around the caret
2210 * @see DefaultEditorKit#endAction
2211 * @see DefaultEditorKit#getActions
2212 */
2213 static class SelectParagraphAction extends TextAction {
2214
2215 /**
2216 * Create this action with the appropriate identifier.
2217 * @param nm the name of the action, Action.NAME.
2218 * @param select whether to extend the selection when
2219 * changing the caret position.
2220 */
2221 SelectParagraphAction() {
2222 super(selectParagraphAction);
2223 start = new BeginParagraphAction("pigdog", false);
2224 end = new EndParagraphAction("pigdog", true);
2225 }
2226
2227 /** The operation to perform when this action is triggered. */
2228 public void actionPerformed(ActionEvent e) {
2229 start.actionPerformed(e);
2230 end.actionPerformed(e);
2231 }
2232
2233 private Action start;
2234 private Action end;
2235 }
2236
2237 /*
2238 * Select the entire document
2239 * @see DefaultEditorKit#endAction
2240 * @see DefaultEditorKit#getActions
2241 */
2242 static class SelectAllAction extends TextAction {
2243
2244 /**
2245 * Create this action with the appropriate identifier.
2246 * @param nm the name of the action, Action.NAME.
2247 * @param select whether to extend the selection when
2248 * changing the caret position.
2249 */
2250 SelectAllAction() {
2251 super(selectAllAction);
2252 }
2253
2254 /** The operation to perform when this action is triggered. */
2255 public void actionPerformed(ActionEvent e) {
2256 JTextComponent target = getTextComponent(e);
2257 if (target != null) {
2258 Document doc = target.getDocument();
2259 target.setCaretPosition(0);
2260 target.moveCaretPosition(doc.getLength());
2261 }
2262 }
2263
2264 }
2265
2266 /*
2267 * Remove the selection, if any.
2268 * @see DefaultEditorKit#unselectAction
2269 * @see DefaultEditorKit#getActions
2270 */
2271 static class UnselectAction extends TextAction {
2272
2273 /**
2274 * Create this action with the appropriate identifier.
2275 */
2276 UnselectAction() {
2277 super(unselectAction);
2278 }
2279
2280 /** The operation to perform when this action is triggered. */
2281 public void actionPerformed(ActionEvent e) {
2282 JTextComponent target = getTextComponent(e);
2283 if (target != null) {
2284 target.setCaretPosition(target.getCaretPosition());
2285 }
2286 }
2287
2288 }
2289
2290 /*
2291 * Toggles the ComponentOrientation of the text component.
2292 * @see DefaultEditorKit#toggleComponentOrientationAction
2293 * @see DefaultEditorKit#getActions
2294 */
2295 static class ToggleComponentOrientationAction extends TextAction {
2296
2297 /**
2298 * Create this action with the appropriate identifier.
2299 */
2300 ToggleComponentOrientationAction() {
2301 super(toggleComponentOrientationAction);
2302 }
2303
2304 /** The operation to perform when this action is triggered. */
2305 public void actionPerformed(ActionEvent e) {
2306 JTextComponent target = getTextComponent(e);
2307 if (target != null) {
2308 ComponentOrientation last = target.getComponentOrientation();
2309 ComponentOrientation next;
2310 if( last == ComponentOrientation.RIGHT_TO_LEFT )
2311 next = ComponentOrientation.LEFT_TO_RIGHT;
2312 else
2313 next = ComponentOrientation.RIGHT_TO_LEFT;
2314 target.setComponentOrientation(next);
|
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 public class DefaultEditorKit extends EditorKit {
74 private static final long serialVersionUID = 2948709989644140410L;
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
1016 * @param e the action event
1017 */
1018 public void actionPerformed(ActionEvent e) {
1019 JTextComponent target = getTextComponent(e);
1020 if (target != null) {
1021 if ((! target.isEditable()) || (! target.isEnabled())) {
1022 UIManager.getLookAndFeel().provideErrorFeedback(target);
1023 return;
1024 }
1025 target.replaceSelection("\t");
1026 }
1027 }
1028 }
1029
1030 /*
1031 * Deletes the character of content that precedes the
1032 * current caret position.
1033 * @see DefaultEditorKit#deletePrevCharAction
1034 * @see DefaultEditorKit#getActions
1035 */
1036 @SuppressWarnings("serial") // Superclass is not serializable across versions
1037 static class DeletePrevCharAction extends TextAction {
1038
1039 /**
1040 * Creates this object with the appropriate identifier.
1041 */
1042 DeletePrevCharAction() {
1043 super(deletePrevCharAction);
1044 }
1045
1046 /**
1047 * The operation to perform when this action is triggered.
1048 *
1049 * @param e the action event
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();
1075 }
1076
1077 doc.remove(dot - delChars, delChars);
1078 beep = false;
1079 }
1080 } catch (BadLocationException bl) {
1081 }
1082 }
1083 if (beep) {
1084 UIManager.getLookAndFeel().provideErrorFeedback(target);
1085 }
1086 }
1087 }
1088
1089 /*
1090 * Deletes the character of content that follows the
1091 * current caret position.
1092 * @see DefaultEditorKit#deleteNextCharAction
1093 * @see DefaultEditorKit#getActions
1094 */
1095 @SuppressWarnings("serial") // Superclass is not serializable across versions
1096 static class DeleteNextCharAction extends TextAction {
1097
1098 /* Create this object with the appropriate identifier. */
1099 DeleteNextCharAction() {
1100 super(deleteNextCharAction);
1101 }
1102
1103 /** The operation to perform when this action is triggered. */
1104 public void actionPerformed(ActionEvent e) {
1105 JTextComponent target = getTextComponent(e);
1106 boolean beep = true;
1107 if ((target != null) && (target.isEditable())) {
1108 try {
1109 Document doc = target.getDocument();
1110 Caret caret = target.getCaret();
1111 int dot = caret.getDot();
1112 int mark = caret.getMark();
1113 if (dot != mark) {
1114 doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
1115 beep = false;
1127 }
1128 }
1129
1130 doc.remove(dot, delChars);
1131 beep = false;
1132 }
1133 } catch (BadLocationException bl) {
1134 }
1135 }
1136 if (beep) {
1137 UIManager.getLookAndFeel().provideErrorFeedback(target);
1138 }
1139 }
1140 }
1141
1142
1143 /*
1144 * Deletes the word that precedes/follows the beginning of the selection.
1145 * @see DefaultEditorKit#getActions
1146 */
1147 @SuppressWarnings("serial") // Superclass is not serializable across versions
1148 static class DeleteWordAction extends TextAction {
1149 DeleteWordAction(String name) {
1150 super(name);
1151 assert (name == deletePrevWordAction)
1152 || (name == deleteNextWordAction);
1153 }
1154 /**
1155 * The operation to perform when this action is triggered.
1156 *
1157 * @param e the action event
1158 */
1159 public void actionPerformed(ActionEvent e) {
1160 final JTextComponent target = getTextComponent(e);
1161 if ((target != null) && (e != null)) {
1162 if ((! target.isEditable()) || (! target.isEnabled())) {
1163 UIManager.getLookAndFeel().provideErrorFeedback(target);
1164 return;
1165 }
1166 boolean beep = true;
1167 try {
1202 int len = Math.abs(end - start);
1203 if (offs >= 0) {
1204 target.getDocument().remove(offs, len);
1205 beep = false;
1206 }
1207 } catch (BadLocationException ignore) {
1208 }
1209 if (beep) {
1210 UIManager.getLookAndFeel().provideErrorFeedback(target);
1211 }
1212 }
1213 }
1214 }
1215
1216
1217 /*
1218 * Sets the editor into read-only mode.
1219 * @see DefaultEditorKit#readOnlyAction
1220 * @see DefaultEditorKit#getActions
1221 */
1222 @SuppressWarnings("serial") // Superclass is not serializable across versions
1223 static class ReadOnlyAction extends TextAction {
1224
1225 /* Create this object with the appropriate identifier. */
1226 ReadOnlyAction() {
1227 super(readOnlyAction);
1228 }
1229
1230 /**
1231 * The operation to perform when this action is triggered.
1232 *
1233 * @param e the action event
1234 */
1235 public void actionPerformed(ActionEvent e) {
1236 JTextComponent target = getTextComponent(e);
1237 if (target != null) {
1238 target.setEditable(false);
1239 }
1240 }
1241 }
1242
1243 /*
1244 * Sets the editor into writeable mode.
1245 * @see DefaultEditorKit#writableAction
1246 * @see DefaultEditorKit#getActions
1247 */
1248 @SuppressWarnings("serial") // Superclass is not serializable across versions
1249 static class WritableAction extends TextAction {
1250
1251 /* Create this object with the appropriate identifier. */
1252 WritableAction() {
1253 super(writableAction);
1254 }
1255
1256 /**
1257 * The operation to perform when this action is triggered.
1258 *
1259 * @param e the action event
1260 */
1261 public void actionPerformed(ActionEvent e) {
1262 JTextComponent target = getTextComponent(e);
1263 if (target != null) {
1264 target.setEditable(true);
1265 }
1266 }
1267 }
1268
1403
1404 /**
1405 * The operation to perform when this action is triggered.
1406 *
1407 * @param e the action event
1408 */
1409 public void actionPerformed(ActionEvent e) {
1410 JTextComponent target = getTextComponent(e);
1411 UIManager.getLookAndFeel().provideErrorFeedback(target);
1412 }
1413 }
1414
1415 /**
1416 * Scrolls up/down vertically. The select version of this action extends
1417 * the selection, instead of simply moving the caret.
1418 *
1419 * @see DefaultEditorKit#pageUpAction
1420 * @see DefaultEditorKit#pageDownAction
1421 * @see DefaultEditorKit#getActions
1422 */
1423 @SuppressWarnings("serial") // Superclass is not serializable across versions
1424 static class VerticalPageAction extends TextAction {
1425
1426 /** Create this object with the appropriate identifier. */
1427 public VerticalPageAction(String nm, int direction, boolean select) {
1428 super(nm);
1429 this.select = select;
1430 this.direction = direction;
1431 }
1432
1433 /** The operation to perform when this action is triggered. */
1434 public void actionPerformed(ActionEvent e) {
1435 JTextComponent target = getTextComponent(e);
1436 if (target != null) {
1437 Rectangle visible = target.getVisibleRect();
1438 Rectangle newVis = new Rectangle(visible);
1439 int selectedIndex = target.getCaretPosition();
1440 int scrollAmount = direction *
1441 target.getScrollableBlockIncrement(
1442 visible, SwingConstants.VERTICAL, direction);
1443 int initialY = visible.y;
1563
1564 return result;
1565 }
1566
1567 /**
1568 * Adjusts the Rectangle to contain the bounds of the character at
1569 * <code>index</code> in response to a page up.
1570 */
1571 private boolean select;
1572
1573 /**
1574 * Direction to scroll, 1 is down, -1 is up.
1575 */
1576 private int direction;
1577 }
1578
1579
1580 /**
1581 * Pages one view to the left or right.
1582 */
1583 @SuppressWarnings("serial") // Superclass is not serializable across versions
1584 static class PageAction extends TextAction {
1585
1586 /** Create this object with the appropriate identifier. */
1587 public PageAction(String nm, boolean left, boolean select) {
1588 super(nm);
1589 this.select = select;
1590 this.left = left;
1591 }
1592
1593 /** The operation to perform when this action is triggered. */
1594 public void actionPerformed(ActionEvent e) {
1595 JTextComponent target = getTextComponent(e);
1596 if (target != null) {
1597 int selectedIndex;
1598 Rectangle visible = new Rectangle();
1599 target.computeVisibleRect(visible);
1600 if (left) {
1601 visible.x = Math.max(0, visible.x - visible.width);
1602 }
1603 else {
1618 Document doc = target.getDocument();
1619 if ((selectedIndex != 0) &&
1620 (selectedIndex > (doc.getLength()-1))) {
1621 selectedIndex = doc.getLength()-1;
1622 }
1623 else if(selectedIndex < 0) {
1624 selectedIndex = 0;
1625 }
1626 if (select)
1627 target.moveCaretPosition(selectedIndex);
1628 else
1629 target.setCaretPosition(selectedIndex);
1630 }
1631 }
1632 }
1633
1634 private boolean select;
1635 private boolean left;
1636 }
1637
1638 @SuppressWarnings("serial") // Superclass is not serializable across versions
1639 static class DumpModelAction extends TextAction {
1640
1641 DumpModelAction() {
1642 super("dump-model");
1643 }
1644
1645 public void actionPerformed(ActionEvent e) {
1646 JTextComponent target = getTextComponent(e);
1647 if (target != null) {
1648 Document d = target.getDocument();
1649 if (d instanceof AbstractDocument) {
1650 ((AbstractDocument) d).dump(System.err);
1651 }
1652 }
1653 }
1654 }
1655
1656 /*
1657 * Action to move the selection by way of the
1658 * getNextVisualPositionFrom method. Constructor indicates direction
1659 * to use.
1660 */
1661 @SuppressWarnings("serial") // Superclass is not serializable across versions
1662 static class NextVisualPositionAction extends TextAction {
1663
1664 /**
1665 * Create this action with the appropriate identifier.
1666 * @param nm the name of the action, Action.NAME.
1667 * @param select whether to extend the selection when
1668 * changing the caret position.
1669 */
1670 NextVisualPositionAction(String nm, boolean select, int direction) {
1671 super(nm);
1672 this.select = select;
1673 this.direction = direction;
1674 }
1675
1676 /** The operation to perform when this action is triggered. */
1677 public void actionPerformed(ActionEvent e) {
1678 JTextComponent target = getTextComponent(e);
1679 if (target != null) {
1680 Caret caret = target.getCaret();
1681 DefaultCaret bidiCaret = (caret instanceof DefaultCaret) ?
1729 if(magicPosition != null &&
1730 (direction == SwingConstants.NORTH ||
1731 direction == SwingConstants.SOUTH)) {
1732 target.getCaret().setMagicCaretPosition(magicPosition);
1733 }
1734 } catch (BadLocationException ex) {
1735 }
1736 }
1737 }
1738
1739 private boolean select;
1740 private int direction;
1741 }
1742
1743 /*
1744 * Position the caret to the beginning of the word.
1745 * @see DefaultEditorKit#beginWordAction
1746 * @see DefaultEditorKit#selectBeginWordAction
1747 * @see DefaultEditorKit#getActions
1748 */
1749 @SuppressWarnings("serial") // Superclass is not serializable across versions
1750 static class BeginWordAction extends TextAction {
1751
1752 /**
1753 * Create this action with the appropriate identifier.
1754 * @param nm the name of the action, Action.NAME.
1755 * @param select whether to extend the selection when
1756 * changing the caret position.
1757 */
1758 BeginWordAction(String nm, boolean select) {
1759 super(nm);
1760 this.select = select;
1761 }
1762
1763 /** The operation to perform when this action is triggered. */
1764 public void actionPerformed(ActionEvent e) {
1765 JTextComponent target = getTextComponent(e);
1766 if (target != null) {
1767 try {
1768 int offs = target.getCaretPosition();
1769 int begOffs = Utilities.getWordStart(target, offs);
1770 if (select) {
1771 target.moveCaretPosition(begOffs);
1772 } else {
1773 target.setCaretPosition(begOffs);
1774 }
1775 } catch (BadLocationException bl) {
1776 UIManager.getLookAndFeel().provideErrorFeedback(target);
1777 }
1778 }
1779 }
1780
1781 private boolean select;
1782 }
1783
1784 /*
1785 * Position the caret to the end of the word.
1786 * @see DefaultEditorKit#endWordAction
1787 * @see DefaultEditorKit#selectEndWordAction
1788 * @see DefaultEditorKit#getActions
1789 */
1790 @SuppressWarnings("serial") // Superclass is not serializable across versions
1791 static class EndWordAction extends TextAction {
1792
1793 /**
1794 * Create this action with the appropriate identifier.
1795 * @param nm the name of the action, Action.NAME.
1796 * @param select whether to extend the selection when
1797 * changing the caret position.
1798 */
1799 EndWordAction(String nm, boolean select) {
1800 super(nm);
1801 this.select = select;
1802 }
1803
1804 /** The operation to perform when this action is triggered. */
1805 public void actionPerformed(ActionEvent e) {
1806 JTextComponent target = getTextComponent(e);
1807 if (target != null) {
1808 try {
1809 int offs = target.getCaretPosition();
1810 int endOffs = Utilities.getWordEnd(target, offs);
1811 if (select) {
1812 target.moveCaretPosition(endOffs);
1813 } else {
1814 target.setCaretPosition(endOffs);
1815 }
1816 } catch (BadLocationException bl) {
1817 UIManager.getLookAndFeel().provideErrorFeedback(target);
1818 }
1819 }
1820 }
1821
1822 private boolean select;
1823 }
1824
1825 /*
1826 * Position the caret to the beginning of the previous word.
1827 * @see DefaultEditorKit#previousWordAction
1828 * @see DefaultEditorKit#selectPreviousWordAction
1829 * @see DefaultEditorKit#getActions
1830 */
1831 @SuppressWarnings("serial") // Superclass is not serializable across versions
1832 static class PreviousWordAction extends TextAction {
1833
1834 /**
1835 * Create this action with the appropriate identifier.
1836 * @param nm the name of the action, Action.NAME.
1837 * @param select whether to extend the selection when
1838 * changing the caret position.
1839 */
1840 PreviousWordAction(String nm, boolean select) {
1841 super(nm);
1842 this.select = select;
1843 }
1844
1845 /** The operation to perform when this action is triggered. */
1846 public void actionPerformed(ActionEvent e) {
1847 JTextComponent target = getTextComponent(e);
1848 if (target != null) {
1849 int offs = target.getCaretPosition();
1850 boolean failed = false;
1851 try {
1871 target.moveCaretPosition(offs);
1872 } else {
1873 target.setCaretPosition(offs);
1874 }
1875 }
1876 else {
1877 UIManager.getLookAndFeel().provideErrorFeedback(target);
1878 }
1879 }
1880 }
1881
1882 private boolean select;
1883 }
1884
1885 /*
1886 * Position the caret to the next of the word.
1887 * @see DefaultEditorKit#nextWordAction
1888 * @see DefaultEditorKit#selectNextWordAction
1889 * @see DefaultEditorKit#getActions
1890 */
1891 @SuppressWarnings("serial") // Superclass is not serializable across versions
1892 static class NextWordAction extends TextAction {
1893
1894 /**
1895 * Create this action with the appropriate identifier.
1896 * @param nm the name of the action, Action.NAME.
1897 * @param select whether to extend the selection when
1898 * changing the caret position.
1899 */
1900 NextWordAction(String nm, boolean select) {
1901 super(nm);
1902 this.select = select;
1903 }
1904
1905 /** The operation to perform when this action is triggered. */
1906 public void actionPerformed(ActionEvent e) {
1907 JTextComponent target = getTextComponent(e);
1908 if (target != null) {
1909 int offs = target.getCaretPosition();
1910 boolean failed = false;
1911 int oldOffs = offs;
1937 target.moveCaretPosition(offs);
1938 } else {
1939 target.setCaretPosition(offs);
1940 }
1941 }
1942 else {
1943 UIManager.getLookAndFeel().provideErrorFeedback(target);
1944 }
1945 }
1946 }
1947
1948 private boolean select;
1949 }
1950
1951 /*
1952 * Position the caret to the beginning of the line.
1953 * @see DefaultEditorKit#beginLineAction
1954 * @see DefaultEditorKit#selectBeginLineAction
1955 * @see DefaultEditorKit#getActions
1956 */
1957 @SuppressWarnings("serial") // Superclass is not serializable across versions
1958 static class BeginLineAction extends TextAction {
1959
1960 /**
1961 * Create this action with the appropriate identifier.
1962 * @param nm the name of the action, Action.NAME.
1963 * @param select whether to extend the selection when
1964 * changing the caret position.
1965 */
1966 BeginLineAction(String nm, boolean select) {
1967 super(nm);
1968 this.select = select;
1969 }
1970
1971 /** The operation to perform when this action is triggered. */
1972 public void actionPerformed(ActionEvent e) {
1973 JTextComponent target = getTextComponent(e);
1974 if (target != null) {
1975 try {
1976 int offs = target.getCaretPosition();
1977 int begOffs = Utilities.getRowStart(target, offs);
1978 if (select) {
1979 target.moveCaretPosition(begOffs);
1980 } else {
1981 target.setCaretPosition(begOffs);
1982 }
1983 } catch (BadLocationException bl) {
1984 UIManager.getLookAndFeel().provideErrorFeedback(target);
1985 }
1986 }
1987 }
1988
1989 private boolean select;
1990 }
1991
1992 /*
1993 * Position the caret to the end of the line.
1994 * @see DefaultEditorKit#endLineAction
1995 * @see DefaultEditorKit#selectEndLineAction
1996 * @see DefaultEditorKit#getActions
1997 */
1998 @SuppressWarnings("serial") // Superclass is not serializable across versions
1999 static class EndLineAction extends TextAction {
2000
2001 /**
2002 * Create this action with the appropriate identifier.
2003 * @param nm the name of the action, Action.NAME.
2004 * @param select whether to extend the selection when
2005 * changing the caret position.
2006 */
2007 EndLineAction(String nm, boolean select) {
2008 super(nm);
2009 this.select = select;
2010 }
2011
2012 /** The operation to perform when this action is triggered. */
2013 public void actionPerformed(ActionEvent e) {
2014 JTextComponent target = getTextComponent(e);
2015 if (target != null) {
2016 try {
2017 int offs = target.getCaretPosition();
2018 int endOffs = Utilities.getRowEnd(target, offs);
2019 if (select) {
2020 target.moveCaretPosition(endOffs);
2021 } else {
2022 target.setCaretPosition(endOffs);
2023 }
2024 } catch (BadLocationException bl) {
2025 UIManager.getLookAndFeel().provideErrorFeedback(target);
2026 }
2027 }
2028 }
2029
2030 private boolean select;
2031 }
2032
2033 /*
2034 * Position the caret to the beginning of the paragraph.
2035 * @see DefaultEditorKit#beginParagraphAction
2036 * @see DefaultEditorKit#selectBeginParagraphAction
2037 * @see DefaultEditorKit#getActions
2038 */
2039 @SuppressWarnings("serial") // Superclass is not serializable across versions
2040 static class BeginParagraphAction extends TextAction {
2041
2042 /**
2043 * Create this action with the appropriate identifier.
2044 * @param nm the name of the action, Action.NAME.
2045 * @param select whether to extend the selection when
2046 * changing the caret position.
2047 */
2048 BeginParagraphAction(String nm, boolean select) {
2049 super(nm);
2050 this.select = select;
2051 }
2052
2053 /** The operation to perform when this action is triggered. */
2054 public void actionPerformed(ActionEvent e) {
2055 JTextComponent target = getTextComponent(e);
2056 if (target != null) {
2057 int offs = target.getCaretPosition();
2058 Element elem = Utilities.getParagraphElement(target, offs);
2059 offs = elem.getStartOffset();
2060 if (select) {
2061 target.moveCaretPosition(offs);
2062 } else {
2063 target.setCaretPosition(offs);
2064 }
2065 }
2066 }
2067
2068 private boolean select;
2069 }
2070
2071 /*
2072 * Position the caret to the end of the paragraph.
2073 * @see DefaultEditorKit#endParagraphAction
2074 * @see DefaultEditorKit#selectEndParagraphAction
2075 * @see DefaultEditorKit#getActions
2076 */
2077 @SuppressWarnings("serial") // Superclass is not serializable across versions
2078 static class EndParagraphAction extends TextAction {
2079
2080 /**
2081 * Create this action with the appropriate identifier.
2082 * @param nm the name of the action, Action.NAME.
2083 * @param select whether to extend the selection when
2084 * changing the caret position.
2085 */
2086 EndParagraphAction(String nm, boolean select) {
2087 super(nm);
2088 this.select = select;
2089 }
2090
2091 /** The operation to perform when this action is triggered. */
2092 public void actionPerformed(ActionEvent e) {
2093 JTextComponent target = getTextComponent(e);
2094 if (target != null) {
2095 int offs = target.getCaretPosition();
2096 Element elem = Utilities.getParagraphElement(target, offs);
2097 offs = Math.min(target.getDocument().getLength(),
2098 elem.getEndOffset());
2099 if (select) {
2100 target.moveCaretPosition(offs);
2101 } else {
2102 target.setCaretPosition(offs);
2103 }
2104 }
2105 }
2106
2107 private boolean select;
2108 }
2109
2110 /*
2111 * Move the caret to the beginning of the document.
2112 * @see DefaultEditorKit#beginAction
2113 * @see DefaultEditorKit#getActions
2114 */
2115 @SuppressWarnings("serial") // Superclass is not serializable across versions
2116 static class BeginAction extends TextAction {
2117
2118 /* Create this object with the appropriate identifier. */
2119 BeginAction(String nm, boolean select) {
2120 super(nm);
2121 this.select = select;
2122 }
2123
2124 /** The operation to perform when this action is triggered. */
2125 public void actionPerformed(ActionEvent e) {
2126 JTextComponent target = getTextComponent(e);
2127 if (target != null) {
2128 if (select) {
2129 target.moveCaretPosition(0);
2130 } else {
2131 target.setCaretPosition(0);
2132 }
2133 }
2134 }
2135
2136 private boolean select;
2137 }
2138
2139 /*
2140 * Move the caret to the end of the document.
2141 * @see DefaultEditorKit#endAction
2142 * @see DefaultEditorKit#getActions
2143 */
2144 @SuppressWarnings("serial") // Superclass is not serializable across versions
2145 static class EndAction extends TextAction {
2146
2147 /* Create this object with the appropriate identifier. */
2148 EndAction(String nm, boolean select) {
2149 super(nm);
2150 this.select = select;
2151 }
2152
2153 /** The operation to perform when this action is triggered. */
2154 public void actionPerformed(ActionEvent e) {
2155 JTextComponent target = getTextComponent(e);
2156 if (target != null) {
2157 Document doc = target.getDocument();
2158 int dot = doc.getLength();
2159 if (select) {
2160 target.moveCaretPosition(dot);
2161 } else {
2162 target.setCaretPosition(dot);
2163 }
2164 }
2165 }
2166
2167 private boolean select;
2168 }
2169
2170 /*
2171 * Select the word around the caret
2172 * @see DefaultEditorKit#endAction
2173 * @see DefaultEditorKit#getActions
2174 */
2175 @SuppressWarnings("serial") // Superclass is not serializable across versions
2176 static class SelectWordAction extends TextAction {
2177
2178 /**
2179 * Create this action with the appropriate identifier.
2180 * @param nm the name of the action, Action.NAME.
2181 * @param select whether to extend the selection when
2182 * changing the caret position.
2183 */
2184 SelectWordAction() {
2185 super(selectWordAction);
2186 start = new BeginWordAction("pigdog", false);
2187 end = new EndWordAction("pigdog", true);
2188 }
2189
2190 /** The operation to perform when this action is triggered. */
2191 public void actionPerformed(ActionEvent e) {
2192 start.actionPerformed(e);
2193 end.actionPerformed(e);
2194 }
2195
2196 private Action start;
2197 private Action end;
2198 }
2199
2200 /*
2201 * Select the line around the caret
2202 * @see DefaultEditorKit#endAction
2203 * @see DefaultEditorKit#getActions
2204 */
2205 @SuppressWarnings("serial") // Superclass is not serializable across versions
2206 static class SelectLineAction extends TextAction {
2207
2208 /**
2209 * Create this action with the appropriate identifier.
2210 * @param nm the name of the action, Action.NAME.
2211 * @param select whether to extend the selection when
2212 * changing the caret position.
2213 */
2214 SelectLineAction() {
2215 super(selectLineAction);
2216 start = new BeginLineAction("pigdog", false);
2217 end = new EndLineAction("pigdog", true);
2218 }
2219
2220 /** The operation to perform when this action is triggered. */
2221 public void actionPerformed(ActionEvent e) {
2222 start.actionPerformed(e);
2223 end.actionPerformed(e);
2224 }
2225
2226 private Action start;
2227 private Action end;
2228 }
2229
2230 /*
2231 * Select the paragraph around the caret
2232 * @see DefaultEditorKit#endAction
2233 * @see DefaultEditorKit#getActions
2234 */
2235 @SuppressWarnings("serial") // Superclass is not serializable across versions
2236 static class SelectParagraphAction extends TextAction {
2237
2238 /**
2239 * Create this action with the appropriate identifier.
2240 * @param nm the name of the action, Action.NAME.
2241 * @param select whether to extend the selection when
2242 * changing the caret position.
2243 */
2244 SelectParagraphAction() {
2245 super(selectParagraphAction);
2246 start = new BeginParagraphAction("pigdog", false);
2247 end = new EndParagraphAction("pigdog", true);
2248 }
2249
2250 /** The operation to perform when this action is triggered. */
2251 public void actionPerformed(ActionEvent e) {
2252 start.actionPerformed(e);
2253 end.actionPerformed(e);
2254 }
2255
2256 private Action start;
2257 private Action end;
2258 }
2259
2260 /*
2261 * Select the entire document
2262 * @see DefaultEditorKit#endAction
2263 * @see DefaultEditorKit#getActions
2264 */
2265 @SuppressWarnings("serial") // Superclass is not serializable across versions
2266 static class SelectAllAction extends TextAction {
2267
2268 /**
2269 * Create this action with the appropriate identifier.
2270 * @param nm the name of the action, Action.NAME.
2271 * @param select whether to extend the selection when
2272 * changing the caret position.
2273 */
2274 SelectAllAction() {
2275 super(selectAllAction);
2276 }
2277
2278 /** The operation to perform when this action is triggered. */
2279 public void actionPerformed(ActionEvent e) {
2280 JTextComponent target = getTextComponent(e);
2281 if (target != null) {
2282 Document doc = target.getDocument();
2283 target.setCaretPosition(0);
2284 target.moveCaretPosition(doc.getLength());
2285 }
2286 }
2287
2288 }
2289
2290 /*
2291 * Remove the selection, if any.
2292 * @see DefaultEditorKit#unselectAction
2293 * @see DefaultEditorKit#getActions
2294 */
2295 @SuppressWarnings("serial") // Superclass is not serializable across versions
2296 static class UnselectAction extends TextAction {
2297
2298 /**
2299 * Create this action with the appropriate identifier.
2300 */
2301 UnselectAction() {
2302 super(unselectAction);
2303 }
2304
2305 /** The operation to perform when this action is triggered. */
2306 public void actionPerformed(ActionEvent e) {
2307 JTextComponent target = getTextComponent(e);
2308 if (target != null) {
2309 target.setCaretPosition(target.getCaretPosition());
2310 }
2311 }
2312
2313 }
2314
2315 /*
2316 * Toggles the ComponentOrientation of the text component.
2317 * @see DefaultEditorKit#toggleComponentOrientationAction
2318 * @see DefaultEditorKit#getActions
2319 */
2320 @SuppressWarnings("serial") // Superclass is not serializable across versions
2321 static class ToggleComponentOrientationAction extends TextAction {
2322
2323 /**
2324 * Create this action with the appropriate identifier.
2325 */
2326 ToggleComponentOrientationAction() {
2327 super(toggleComponentOrientationAction);
2328 }
2329
2330 /** The operation to perform when this action is triggered. */
2331 public void actionPerformed(ActionEvent e) {
2332 JTextComponent target = getTextComponent(e);
2333 if (target != null) {
2334 ComponentOrientation last = target.getComponentOrientation();
2335 ComponentOrientation next;
2336 if( last == ComponentOrientation.RIGHT_TO_LEFT )
2337 next = ComponentOrientation.LEFT_TO_RIGHT;
2338 else
2339 next = ComponentOrientation.RIGHT_TO_LEFT;
2340 target.setComponentOrientation(next);
|