1. /*
  2. * @(#)DefaultEditorKit.java 1.63 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text;
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.text.*;
  12. import javax.swing.Action;
  13. import javax.swing.KeyStroke;
  14. import javax.swing.SwingConstants;
  15. import javax.swing.UIManager;
  16. /**
  17. * This is the set of things needed by a text component
  18. * to be a reasonably functioning editor for some <em>type</em>
  19. * of text document. This implementation provides a default
  20. * implementation which treats text as plain text and
  21. * provides a minimal set of actions for a simple editor.
  22. * <p>
  23. * <dl>
  24. * <dt><b><font size=+1>Newlines</font></b>
  25. * <dd>
  26. * There are two properties which deal with newlines. The
  27. * system property, <code>line.separator</code>, is defined to be
  28. * platform-dependent, either "\n", "\r", or "\r\n". There is also
  29. * a property defined in <code>DefaultEditorKit</code>, called
  30. * <a href=#EndOfLineStringProperty><code>EndOfLineStringProperty</code></a>,
  31. * which is defined automatically when a document is loaded, to be
  32. * the first occurrence of any of the newline characters.
  33. * When a document is loaded, <code>EndOfLineStringProperty</code>
  34. * is set appropriately, and when the document is written back out, the
  35. * <code>EndOfLineStringProperty</code> is used. But while the document
  36. * is in memory, the "\n" character is used to define a
  37. * newline, regardless of how the newline is defined when
  38. * the document is on disk. Therefore, for searching purposes,
  39. * "\n" should always be used. When a new document is created,
  40. * and the <code>EndOfLineStringProperty</code> has not been defined,
  41. * it will use the System property when writing out the
  42. * document.
  43. * <p>Note that <code>EndOfLineStringProperty</code> is set
  44. * on the <code>Document</code> using the <code>get/setProperty</code>
  45. * methods. Subclasses may override this behavior.
  46. *
  47. * </dl>
  48. *
  49. * @author Timothy Prinzing
  50. * @version 1.63 01/23/03
  51. */
  52. public class DefaultEditorKit extends EditorKit {
  53. /**
  54. * default constructor for DefaultEditorKit
  55. */
  56. public DefaultEditorKit() {
  57. }
  58. /**
  59. * Gets the MIME type of the data that this
  60. * kit represents support for. The default
  61. * is <code>text/plain</code>.
  62. *
  63. * @return the type
  64. */
  65. public String getContentType() {
  66. return "text/plain";
  67. }
  68. /**
  69. * Fetches a factory that is suitable for producing
  70. * views of any models that are produced by this
  71. * kit. The default is to have the UI produce the
  72. * factory, so this method has no implementation.
  73. *
  74. * @return the view factory
  75. */
  76. public ViewFactory getViewFactory() {
  77. return null;
  78. }
  79. /**
  80. * Fetches the set of commands that can be used
  81. * on a text component that is using a model and
  82. * view produced by this kit.
  83. *
  84. * @return the command list
  85. */
  86. public Action[] getActions() {
  87. return defaultActions;
  88. }
  89. /**
  90. * Fetches a caret that can navigate through views
  91. * produced by the associated ViewFactory.
  92. *
  93. * @return the caret
  94. */
  95. public Caret createCaret() {
  96. return null;
  97. }
  98. /**
  99. * Creates an uninitialized text storage model (PlainDocument)
  100. * that is appropriate for this type of editor.
  101. *
  102. * @return the model
  103. */
  104. public Document createDefaultDocument() {
  105. return new PlainDocument();
  106. }
  107. /**
  108. * Inserts content from the given stream which is expected
  109. * to be in a format appropriate for this kind of content
  110. * handler.
  111. *
  112. * @param in The stream to read from
  113. * @param doc The destination for the insertion.
  114. * @param pos The location in the document to place the
  115. * content >= 0.
  116. * @exception IOException on any I/O error
  117. * @exception BadLocationException if pos represents an invalid
  118. * location within the document.
  119. */
  120. public void read(InputStream in, Document doc, int pos)
  121. throws IOException, BadLocationException {
  122. read(new InputStreamReader(in), doc, pos);
  123. }
  124. /**
  125. * Writes content from a document to the given stream
  126. * in a format appropriate for this kind of content handler.
  127. *
  128. * @param out The stream to write to
  129. * @param doc The source for the write.
  130. * @param pos The location in the document to fetch the
  131. * content >= 0.
  132. * @param len The amount to write out >= 0.
  133. * @exception IOException on any I/O error
  134. * @exception BadLocationException if pos represents an invalid
  135. * location within the document.
  136. */
  137. public void write(OutputStream out, Document doc, int pos, int len)
  138. throws IOException, BadLocationException {
  139. OutputStreamWriter osw = new OutputStreamWriter(out);
  140. write(osw, doc, pos, len);
  141. osw.flush();
  142. }
  143. /**
  144. * Gets the input attributes for the pane. This method exists for
  145. * the benefit of StyledEditorKit so that the read method will
  146. * pick up the correct attributes to apply to inserted text.
  147. * This class's implementation simply returns null.
  148. *
  149. * @return null
  150. */
  151. MutableAttributeSet getInputAttributes() {
  152. return null;
  153. }
  154. /**
  155. * Inserts content from the given stream, which will be
  156. * treated as plain text.
  157. *
  158. * @param in The stream to read from
  159. * @param doc The destination for the insertion.
  160. * @param pos The location in the document to place the
  161. * content >= 0.
  162. * @exception IOException on any I/O error
  163. * @exception BadLocationException if pos represents an invalid
  164. * location within the document.
  165. */
  166. public void read(Reader in, Document doc, int pos)
  167. throws IOException, BadLocationException {
  168. char[] buff = new char[4096];
  169. int nch;
  170. boolean lastWasCR = false;
  171. boolean isCRLF = false;
  172. boolean isCR = false;
  173. int last;
  174. boolean wasEmpty = (doc.getLength() == 0);
  175. AttributeSet attr = getInputAttributes();
  176. // Read in a block at a time, mapping \r\n to \n, as well as single
  177. // \r's to \n's. If a \r\n is encountered, \r\n will be set as the
  178. // newline string for the document, if \r is encountered it will
  179. // be set as the newline character, otherwise the newline property
  180. // for the document will be removed.
  181. while ((nch = in.read(buff, 0, buff.length)) != -1) {
  182. last = 0;
  183. for(int counter = 0; counter < nch; counter++) {
  184. switch(buff[counter]) {
  185. case '\r':
  186. if (lastWasCR) {
  187. isCR = true;
  188. if (counter == 0) {
  189. doc.insertString(pos, "\n", attr);
  190. pos++;
  191. }
  192. else {
  193. buff[counter - 1] = '\n';
  194. }
  195. }
  196. else {
  197. lastWasCR = true;
  198. }
  199. break;
  200. case '\n':
  201. if (lastWasCR) {
  202. if (counter > (last + 1)) {
  203. doc.insertString(pos, new String(buff, last,
  204. counter - last - 1), attr);
  205. pos += (counter - last - 1);
  206. }
  207. // else nothing to do, can skip \r, next write will
  208. // write \n
  209. lastWasCR = false;
  210. last = counter;
  211. isCRLF = true;
  212. }
  213. break;
  214. default:
  215. if (lastWasCR) {
  216. isCR = true;
  217. if (counter == 0) {
  218. doc.insertString(pos, "\n", attr);
  219. pos++;
  220. }
  221. else {
  222. buff[counter - 1] = '\n';
  223. }
  224. lastWasCR = false;
  225. }
  226. break;
  227. }
  228. }
  229. if (last < nch) {
  230. if(lastWasCR) {
  231. if (last < (nch - 1)) {
  232. doc.insertString(pos, new String(buff, last,
  233. nch - last - 1), attr);
  234. pos += (nch - last - 1);
  235. }
  236. }
  237. else {
  238. doc.insertString(pos, new String(buff, last,
  239. nch - last), attr);
  240. pos += (nch - last);
  241. }
  242. }
  243. }
  244. if (lastWasCR) {
  245. doc.insertString(pos, "\n", attr);
  246. isCR = true;
  247. }
  248. if (wasEmpty) {
  249. if (isCRLF) {
  250. doc.putProperty(EndOfLineStringProperty, "\r\n");
  251. }
  252. else if (isCR) {
  253. doc.putProperty(EndOfLineStringProperty, "\r");
  254. }
  255. else {
  256. doc.putProperty(EndOfLineStringProperty, "\n");
  257. }
  258. }
  259. }
  260. /**
  261. * Writes content from a document to the given stream
  262. * as plain text.
  263. *
  264. * @param out The stream to write to
  265. * @param doc The source for the write.
  266. * @param pos The location in the document to fetch the
  267. * content from >= 0.
  268. * @param len The amount to write out >= 0.
  269. * @exception IOException on any I/O error
  270. * @exception BadLocationException if pos is not within 0 and
  271. * the length of the document.
  272. */
  273. public void write(Writer out, Document doc, int pos, int len)
  274. throws IOException, BadLocationException {
  275. if ((pos < 0) || ((pos + len) > doc.getLength())) {
  276. throw new BadLocationException("DefaultEditorKit.write", pos);
  277. }
  278. Segment data = new Segment();
  279. int nleft = len;
  280. int offs = pos;
  281. Object endOfLineProperty = doc.getProperty(EndOfLineStringProperty);
  282. if (endOfLineProperty == null) {
  283. try {
  284. endOfLineProperty = System.getProperty("line.separator");
  285. } catch (SecurityException se) { }
  286. }
  287. String endOfLine;
  288. if (endOfLineProperty instanceof String) {
  289. endOfLine = (String)endOfLineProperty;
  290. }
  291. else {
  292. endOfLine = null;
  293. }
  294. if (endOfLineProperty != null && !endOfLine.equals("\n")) {
  295. // There is an end of line string that isn't \n, have to iterate
  296. // through and find all \n's and translate to end of line string.
  297. while (nleft > 0) {
  298. int n = Math.min(nleft, 4096);
  299. doc.getText(offs, n, data);
  300. int last = data.offset;
  301. char[] array = data.array;
  302. int maxCounter = last + data.count;
  303. for (int counter = last; counter < maxCounter; counter++) {
  304. if (array[counter] == '\n') {
  305. if (counter > last) {
  306. out.write(array, last, counter - last);
  307. }
  308. out.write(endOfLine);
  309. last = counter + 1;
  310. }
  311. }
  312. if (maxCounter > last) {
  313. out.write(array, last, maxCounter - last);
  314. }
  315. offs += n;
  316. nleft -= n;
  317. }
  318. }
  319. else {
  320. // Just write out text, will already have \n, no mapping to
  321. // do.
  322. while (nleft > 0) {
  323. int n = Math.min(nleft, 4096);
  324. doc.getText(offs, n, data);
  325. out.write(data.array, data.offset, data.count);
  326. offs += n;
  327. nleft -= n;
  328. }
  329. }
  330. out.flush();
  331. }
  332. /**
  333. * When reading a document if a CRLF is encountered a property
  334. * with this name is added and the value will be "\r\n".
  335. */
  336. public static final String EndOfLineStringProperty = "__EndOfLine__";
  337. // --- names of well-known actions ---------------------------
  338. /**
  339. * Name of the action to place content into the associated
  340. * document. If there is a selection, it is removed before
  341. * the new content is added.
  342. * @see #getActions
  343. */
  344. public static final String insertContentAction = "insert-content";
  345. /**
  346. * Name of the action to place a line/paragraph break into
  347. * the document. If there is a selection, it is removed before
  348. * the break is added.
  349. * @see #getActions
  350. */
  351. public static final String insertBreakAction = "insert-break";
  352. /**
  353. * Name of the action to place a tab character into
  354. * the document. If there is a selection, it is removed before
  355. * the tab is added.
  356. * @see #getActions
  357. */
  358. public static final String insertTabAction = "insert-tab";
  359. /**
  360. * Name of the action to delete the character of content that
  361. * precedes the current caret position.
  362. * @see #getActions
  363. */
  364. public static final String deletePrevCharAction = "delete-previous";
  365. /**
  366. * Name of the action to delete the character of content that
  367. * follows the current caret position.
  368. * @see #getActions
  369. */
  370. public static final String deleteNextCharAction = "delete-next";
  371. /**
  372. * Name of the action to set the editor into read-only
  373. * mode.
  374. * @see #getActions
  375. */
  376. public static final String readOnlyAction = "set-read-only";
  377. /**
  378. * Name of the action to set the editor into writeable
  379. * mode.
  380. * @see #getActions
  381. */
  382. public static final String writableAction = "set-writable";
  383. /**
  384. * Name of the action to cut the selected region
  385. * and place the contents into the system clipboard.
  386. * @see JTextComponent#cut
  387. * @see #getActions
  388. */
  389. public static final String cutAction = "cut-to-clipboard";
  390. /**
  391. * Name of the action to copy the selected region
  392. * and place the contents into the system clipboard.
  393. * @see JTextComponent#copy
  394. * @see #getActions
  395. */
  396. public static final String copyAction = "copy-to-clipboard";
  397. /**
  398. * Name of the action to paste the contents of the
  399. * system clipboard into the selected region, or before the
  400. * caret if nothing is selected.
  401. * @see JTextComponent#paste
  402. * @see #getActions
  403. */
  404. public static final String pasteAction = "paste-from-clipboard";
  405. /**
  406. * Name of the action to create a beep.
  407. * @see #getActions
  408. */
  409. public static final String beepAction = "beep";
  410. /**
  411. * Name of the action to page up vertically.
  412. * @see #getActions
  413. */
  414. public static final String pageUpAction = "page-up";
  415. /**
  416. * Name of the action to page down vertically.
  417. * @see #getActions
  418. */
  419. public static final String pageDownAction = "page-down";
  420. /**
  421. * Name of the action to page up vertically, and move the
  422. * selection.
  423. * @see #getActions
  424. */
  425. /*public*/ static final String selectionPageUpAction = "selection-page-up";
  426. /**
  427. * Name of the action to page down vertically, and move the
  428. * selection.
  429. * @see #getActions
  430. */
  431. /*public*/ static final String selectionPageDownAction = "selection-page-down";
  432. /**
  433. * Name of the action to page left horizontally, and move the
  434. * selection.
  435. * @see #getActions
  436. */
  437. /*public*/ static final String selectionPageLeftAction = "selection-page-left";
  438. /**
  439. * Name of the action to page right horizontally, and move the
  440. * selection.
  441. * @see #getActions
  442. */
  443. /*public*/ static final String selectionPageRightAction = "selection-page-right";
  444. /**
  445. * Name of the Action for moving the caret
  446. * logically forward one position.
  447. * @see #getActions
  448. */
  449. public static final String forwardAction = "caret-forward";
  450. /**
  451. * Name of the Action for moving the caret
  452. * logically backward one position.
  453. * @see #getActions
  454. */
  455. public static final String backwardAction = "caret-backward";
  456. /**
  457. * Name of the Action for extending the selection
  458. * by moving the caret logically forward one position.
  459. * @see #getActions
  460. */
  461. public static final String selectionForwardAction = "selection-forward";
  462. /**
  463. * Name of the Action for extending the selection
  464. * by moving the caret logically backward one position.
  465. * @see #getActions
  466. */
  467. public static final String selectionBackwardAction = "selection-backward";
  468. /**
  469. * Name of the Action for moving the caret
  470. * logically upward one position.
  471. * @see #getActions
  472. */
  473. public static final String upAction = "caret-up";
  474. /**
  475. * Name of the Action for moving the caret
  476. * logically downward one position.
  477. * @see #getActions
  478. */
  479. public static final String downAction = "caret-down";
  480. /**
  481. * Name of the Action for moving the caret
  482. * logically upward one position, extending the selection.
  483. * @see #getActions
  484. */
  485. public static final String selectionUpAction = "selection-up";
  486. /**
  487. * Name of the Action for moving the caret
  488. * logically downward one position, extending the selection.
  489. * @see #getActions
  490. */
  491. public static final String selectionDownAction = "selection-down";
  492. /**
  493. * Name of the <code>Action</code> for moving the caret
  494. * to the beginning of a word.
  495. * @see #getActions
  496. */
  497. public static final String beginWordAction = "caret-begin-word";
  498. /**
  499. * Name of the Action for moving the caret
  500. * to the end of a word.
  501. * @see #getActions
  502. */
  503. public static final String endWordAction = "caret-end-word";
  504. /**
  505. * Name of the <code>Action</code> for moving the caret
  506. * to the beginning of a word, extending the selection.
  507. * @see #getActions
  508. */
  509. public static final String selectionBeginWordAction = "selection-begin-word";
  510. /**
  511. * Name of the Action for moving the caret
  512. * to the end of a word, extending the selection.
  513. * @see #getActions
  514. */
  515. public static final String selectionEndWordAction = "selection-end-word";
  516. /**
  517. * Name of the <code>Action</code> for moving the caret to the
  518. * beginning of the previous word.
  519. * @see #getActions
  520. */
  521. public static final String previousWordAction = "caret-previous-word";
  522. /**
  523. * Name of the <code>Action</code> for moving the caret to the
  524. * beginning of the next word.
  525. * @see #getActions
  526. */
  527. public static final String nextWordAction = "caret-next-word";
  528. /**
  529. * Name of the <code>Action</code> for moving the selection to the
  530. * beginning of the previous word, extending the selection.
  531. * @see #getActions
  532. */
  533. public static final String selectionPreviousWordAction = "selection-previous-word";
  534. /**
  535. * Name of the <code>Action</code> for moving the selection to the
  536. * beginning of the next word, extending the selection.
  537. * @see #getActions
  538. */
  539. public static final String selectionNextWordAction = "selection-next-word";
  540. /**
  541. * Name of the <code>Action</code> for moving the caret
  542. * to the beginning of a line.
  543. * @see #getActions
  544. */
  545. public static final String beginLineAction = "caret-begin-line";
  546. /**
  547. * Name of the <code>Action</code> for moving the caret
  548. * to the end of a line.
  549. * @see #getActions
  550. */
  551. public static final String endLineAction = "caret-end-line";
  552. /**
  553. * Name of the <code>Action</code> for moving the caret
  554. * to the beginning of a line, extending the selection.
  555. * @see #getActions
  556. */
  557. public static final String selectionBeginLineAction = "selection-begin-line";
  558. /**
  559. * Name of the <code>Action</code> for moving the caret
  560. * to the end of a line, extending the selection.
  561. * @see #getActions
  562. */
  563. public static final String selectionEndLineAction = "selection-end-line";
  564. /**
  565. * Name of the <code>Action</code> for moving the caret
  566. * to the beginning of a paragraph.
  567. * @see #getActions
  568. */
  569. public static final String beginParagraphAction = "caret-begin-paragraph";
  570. /**
  571. * Name of the <code>Action</code> for moving the caret
  572. * to the end of a paragraph.
  573. * @see #getActions
  574. */
  575. public static final String endParagraphAction = "caret-end-paragraph";
  576. /**
  577. * Name of the <code>Action</code> for moving the caret
  578. * to the beginning of a paragraph, extending the selection.
  579. * @see #getActions
  580. */
  581. public static final String selectionBeginParagraphAction = "selection-begin-paragraph";
  582. /**
  583. * Name of the <code>Action</code> for moving the caret
  584. * to the end of a paragraph, extending the selection.
  585. * @see #getActions
  586. */
  587. public static final String selectionEndParagraphAction = "selection-end-paragraph";
  588. /**
  589. * Name of the <code>Action</code> for moving the caret
  590. * to the beginning of the document.
  591. * @see #getActions
  592. */
  593. public static final String beginAction = "caret-begin";
  594. /**
  595. * Name of the <code>Action</code> for moving the caret
  596. * to the end of the document.
  597. * @see #getActions
  598. */
  599. public static final String endAction = "caret-end";
  600. /**
  601. * Name of the <code>Action</code> for moving the caret
  602. * to the beginning of the document.
  603. * @see #getActions
  604. */
  605. public static final String selectionBeginAction = "selection-begin";
  606. /**
  607. * Name of the Action for moving the caret
  608. * to the end of the document.
  609. * @see #getActions
  610. */
  611. public static final String selectionEndAction = "selection-end";
  612. /**
  613. * Name of the Action for selecting a word around the caret.
  614. * @see #getActions
  615. */
  616. public static final String selectWordAction = "select-word";
  617. /**
  618. * Name of the Action for selecting a line around the caret.
  619. * @see #getActions
  620. */
  621. public static final String selectLineAction = "select-line";
  622. /**
  623. * Name of the Action for selecting a paragraph around the caret.
  624. * @see #getActions
  625. */
  626. public static final String selectParagraphAction = "select-paragraph";
  627. /**
  628. * Name of the Action for selecting the entire document
  629. * @see #getActions
  630. */
  631. public static final String selectAllAction = "select-all";
  632. /**
  633. * Name of the Action for removing selection
  634. * @see #getActions
  635. */
  636. /*public*/ static final String unselectAction = "unselect";
  637. /**
  638. * Name of the Action for toggling the component's orientation.
  639. * @see #getActions
  640. */
  641. /*public*/ static final String toggleComponentOrientationAction
  642. = "toggle-componentOrientation";
  643. /**
  644. * Name of the action that is executed by default if
  645. * a <em>key typed event</em> is received and there
  646. * is no keymap entry.
  647. * @see #getActions
  648. */
  649. public static final String defaultKeyTypedAction = "default-typed";
  650. // --- Action implementations ---------------------------------
  651. private static final Action[] defaultActions = {
  652. new InsertContentAction(), new DeletePrevCharAction(),
  653. new DeleteNextCharAction(), new ReadOnlyAction(),
  654. new WritableAction(), new CutAction(),
  655. new CopyAction(), new PasteAction(),
  656. new VerticalPageAction(pageUpAction, -1, false),
  657. new VerticalPageAction(pageDownAction, 1, false),
  658. new VerticalPageAction(selectionPageUpAction, -1, true),
  659. new VerticalPageAction(selectionPageDownAction, 1, true),
  660. new PageAction(selectionPageLeftAction, true, true),
  661. new PageAction(selectionPageRightAction, false, true),
  662. new InsertBreakAction(), new BeepAction(),
  663. new NextVisualPositionAction(forwardAction, false,
  664. SwingConstants.EAST),
  665. new NextVisualPositionAction(backwardAction, false,
  666. SwingConstants.WEST),
  667. new NextVisualPositionAction(selectionForwardAction, true,
  668. SwingConstants.EAST),
  669. new NextVisualPositionAction(selectionBackwardAction, true,
  670. SwingConstants.WEST),
  671. new NextVisualPositionAction(upAction, false,
  672. SwingConstants.NORTH),
  673. new NextVisualPositionAction(downAction, false,
  674. SwingConstants.SOUTH),
  675. new NextVisualPositionAction(selectionUpAction, true,
  676. SwingConstants.NORTH),
  677. new NextVisualPositionAction(selectionDownAction, true,
  678. SwingConstants.SOUTH),
  679. new BeginWordAction(beginWordAction, false),
  680. new EndWordAction(endWordAction, false),
  681. new BeginWordAction(selectionBeginWordAction, true),
  682. new EndWordAction(selectionEndWordAction, true),
  683. new PreviousWordAction(previousWordAction, false),
  684. new NextWordAction(nextWordAction, false),
  685. new PreviousWordAction(selectionPreviousWordAction, true),
  686. new NextWordAction(selectionNextWordAction, true),
  687. new BeginLineAction(beginLineAction, false),
  688. new EndLineAction(endLineAction, false),
  689. new BeginLineAction(selectionBeginLineAction, true),
  690. new EndLineAction(selectionEndLineAction, true),
  691. new BeginParagraphAction(beginParagraphAction, false),
  692. new EndParagraphAction(endParagraphAction, false),
  693. new BeginParagraphAction(selectionBeginParagraphAction, true),
  694. new EndParagraphAction(selectionEndParagraphAction, true),
  695. new BeginAction(beginAction, false),
  696. new EndAction(endAction, false),
  697. new BeginAction(selectionBeginAction, true),
  698. new EndAction(selectionEndAction, true),
  699. new DefaultKeyTypedAction(), new InsertTabAction(),
  700. new SelectWordAction(), new SelectLineAction(),
  701. new SelectParagraphAction(), new SelectAllAction(),
  702. new UnselectAction(), new ToggleComponentOrientationAction(),
  703. new DumpModelAction()
  704. };
  705. /**
  706. * The action that is executed by default if
  707. * a <em>key typed event</em> is received and there
  708. * is no keymap entry. There is a variation across
  709. * different VM's in what gets sent as a <em>key typed</em>
  710. * event, and this action tries to filter out the undesired
  711. * events. This filters the control characters and those
  712. * with the ALT modifier. It allows Control-Alt sequences
  713. * through as these form legitimate unicode characters on
  714. * some PC keyboards.
  715. * <p>
  716. * If the event doesn't get filtered, it will try to insert
  717. * content into the text editor. The content is fetched
  718. * from the command string of the ActionEvent. The text
  719. * entry is done through the <code>replaceSelection</code>
  720. * method on the target text component. This is the
  721. * action that will be fired for most text entry tasks.
  722. * <p>
  723. * <strong>Warning:</strong>
  724. * Serialized objects of this class will not be compatible with
  725. * future Swing releases. The current serialization support is
  726. * appropriate for short term storage or RMI between applications running
  727. * the same version of Swing. As of 1.4, support for long term storage
  728. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  729. * has been added to the <code>java.beans</code> package.
  730. * Please see {@link java.beans.XMLEncoder}.
  731. *
  732. * @see DefaultEditorKit#defaultKeyTypedAction
  733. * @see DefaultEditorKit#getActions
  734. * @see Keymap#setDefaultAction
  735. * @see Keymap#getDefaultAction
  736. */
  737. public static class DefaultKeyTypedAction extends TextAction {
  738. /**
  739. * Creates this object with the appropriate identifier.
  740. */
  741. public DefaultKeyTypedAction() {
  742. super(defaultKeyTypedAction);
  743. }
  744. /**
  745. * The operation to perform when this action is triggered.
  746. *
  747. * @param e the action event
  748. */
  749. public void actionPerformed(ActionEvent e) {
  750. JTextComponent target = getTextComponent(e);
  751. if ((target != null) && (e != null)) {
  752. if ((! target.isEditable()) || (! target.isEnabled())) {
  753. return;
  754. }
  755. String content = e.getActionCommand();
  756. int mod = e.getModifiers();
  757. if ((content != null) && (content.length() > 0) &&
  758. ((mod & ActionEvent.ALT_MASK) == (mod & ActionEvent.CTRL_MASK))) {
  759. char c = content.charAt(0);
  760. if ((c >= 0x20) && (c != 0x7F)) {
  761. target.replaceSelection(content);
  762. }
  763. }
  764. }
  765. }
  766. }
  767. /**
  768. * Places content into the associated document.
  769. * If there is a selection, it is removed before
  770. * the new content is added.
  771. * <p>
  772. * <strong>Warning:</strong>
  773. * Serialized objects of this class will not be compatible with
  774. * future Swing releases. The current serialization support is
  775. * appropriate for short term storage or RMI between applications running
  776. * the same version of Swing. As of 1.4, support for long term storage
  777. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  778. * has been added to the <code>java.beans</code> package.
  779. * Please see {@link java.beans.XMLEncoder}.
  780. *
  781. * @see DefaultEditorKit#insertContentAction
  782. * @see DefaultEditorKit#getActions
  783. */
  784. public static class InsertContentAction extends TextAction {
  785. /**
  786. * Creates this object with the appropriate identifier.
  787. */
  788. public InsertContentAction() {
  789. super(insertContentAction);
  790. }
  791. /**
  792. * The operation to perform when this action is triggered.
  793. *
  794. * @param e the action event
  795. */
  796. public void actionPerformed(ActionEvent e) {
  797. JTextComponent target = getTextComponent(e);
  798. if ((target != null) && (e != null)) {
  799. if ((! target.isEditable()) || (! target.isEnabled())) {
  800. UIManager.getLookAndFeel().provideErrorFeedback(target);
  801. return;
  802. }
  803. String content = e.getActionCommand();
  804. if (content != null) {
  805. target.replaceSelection(content);
  806. } else {
  807. UIManager.getLookAndFeel().provideErrorFeedback(target);
  808. }
  809. }
  810. }
  811. }
  812. /**
  813. * Places a line/paragraph break into the document.
  814. * If there is a selection, it is removed before
  815. * the break is added.
  816. * <p>
  817. * <strong>Warning:</strong>
  818. * Serialized objects of this class will not be compatible with
  819. * future Swing releases. The current serialization support is
  820. * appropriate for short term storage or RMI between applications running
  821. * the same version of Swing. As of 1.4, support for long term storage
  822. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  823. * has been added to the <code>java.beans</code> package.
  824. * Please see {@link java.beans.XMLEncoder}.
  825. *
  826. * @see DefaultEditorKit#insertBreakAction
  827. * @see DefaultEditorKit#getActions
  828. */
  829. public static class InsertBreakAction extends TextAction {
  830. /**
  831. * Creates this object with the appropriate identifier.
  832. */
  833. public InsertBreakAction() {
  834. super(insertBreakAction);
  835. }
  836. /**
  837. * The operation to perform when this action is triggered.
  838. *
  839. * @param e the action event
  840. */
  841. public void actionPerformed(ActionEvent e) {
  842. JTextComponent target = getTextComponent(e);
  843. if (target != null) {
  844. if ((! target.isEditable()) || (! target.isEnabled())) {
  845. UIManager.getLookAndFeel().provideErrorFeedback(target);
  846. return;
  847. }
  848. target.replaceSelection("\n");
  849. }
  850. }
  851. }
  852. /**
  853. * Places a tab character into the document. If there
  854. * is a selection, it is removed before the tab is added.
  855. * <p>
  856. * <strong>Warning:</strong>
  857. * Serialized objects of this class will not be compatible with
  858. * future Swing releases. The current serialization support is
  859. * appropriate for short term storage or RMI between applications running
  860. * the same version of Swing. As of 1.4, support for long term storage
  861. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  862. * has been added to the <code>java.beans</code> package.
  863. * Please see {@link java.beans.XMLEncoder}.
  864. *
  865. * @see DefaultEditorKit#insertTabAction
  866. * @see DefaultEditorKit#getActions
  867. */
  868. public static class InsertTabAction extends TextAction {
  869. /**
  870. * Creates this object with the appropriate identifier.
  871. */
  872. public InsertTabAction() {
  873. super(insertTabAction);
  874. }
  875. /**
  876. * The operation to perform when this action is triggered.
  877. *
  878. * @param e the action event
  879. */
  880. public void actionPerformed(ActionEvent e) {
  881. JTextComponent target = getTextComponent(e);
  882. if (target != null) {
  883. if ((! target.isEditable()) || (! target.isEnabled())) {
  884. UIManager.getLookAndFeel().provideErrorFeedback(target);
  885. return;
  886. }
  887. target.replaceSelection("\t");
  888. }
  889. }
  890. }
  891. /*
  892. * Deletes the character of content that precedes the
  893. * current caret position.
  894. * @see DefaultEditorKit#deletePrevCharAction
  895. * @see DefaultEditorKit#getActions
  896. */
  897. static class DeletePrevCharAction extends TextAction {
  898. /**
  899. * Creates this object with the appropriate identifier.
  900. */
  901. DeletePrevCharAction() {
  902. super(deletePrevCharAction);
  903. }
  904. /**
  905. * The operation to perform when this action is triggered.
  906. *
  907. * @param e the action event
  908. */
  909. public void actionPerformed(ActionEvent e) {
  910. JTextComponent target = getTextComponent(e);
  911. boolean beep = true;
  912. if ((target != null) && (target.isEditable())) {
  913. try {
  914. Document doc = target.getDocument();
  915. Caret caret = target.getCaret();
  916. int dot = caret.getDot();
  917. int mark = caret.getMark();
  918. if (dot != mark) {
  919. doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
  920. beep = false;
  921. } else if (dot > 0) {
  922. int delChars = 1;
  923. if (dot > 1) {
  924. String dotChars = doc.getText(dot - 2, 2);
  925. char c0 = dotChars.charAt(0);
  926. char c1 = dotChars.charAt(1);
  927. if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
  928. c1 >= '\uDC00' && c1 <= '\uDFFF') {
  929. delChars = 2;
  930. }
  931. }
  932. doc.remove(dot - delChars, delChars);
  933. beep = false;
  934. }
  935. } catch (BadLocationException bl) {
  936. }
  937. }
  938. if (beep) {
  939. UIManager.getLookAndFeel().provideErrorFeedback(target);
  940. }
  941. }
  942. }
  943. /*
  944. * Deletes the character of content that follows the
  945. * current caret position.
  946. * @see DefaultEditorKit#deleteNextCharAction
  947. * @see DefaultEditorKit#getActions
  948. */
  949. static class DeleteNextCharAction extends TextAction {
  950. /* Create this object with the appropriate identifier. */
  951. DeleteNextCharAction() {
  952. super(deleteNextCharAction);
  953. }
  954. /** The operation to perform when this action is triggered. */
  955. public void actionPerformed(ActionEvent e) {
  956. JTextComponent target = getTextComponent(e);
  957. boolean beep = true;
  958. if ((target != null) && (target.isEditable())) {
  959. try {
  960. Document doc = target.getDocument();
  961. Caret caret = target.getCaret();
  962. int dot = caret.getDot();
  963. int mark = caret.getMark();
  964. if (dot != mark) {
  965. doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
  966. beep = false;
  967. } else if (dot < doc.getLength()) {
  968. int delChars = 1;
  969. if (dot < doc.getLength() - 1) {
  970. String dotChars = doc.getText(dot, 2);
  971. char c0 = dotChars.charAt(0);
  972. char c1 = dotChars.charAt(1);
  973. if (c0 >= '\uD800' && c0 <= '\uDBFF' &&
  974. c1 >= '\uDC00' && c1 <= '\uDFFF') {
  975. delChars = 2;
  976. }
  977. }
  978. doc.remove(dot, delChars);
  979. beep = false;
  980. }
  981. } catch (BadLocationException bl) {
  982. }
  983. }
  984. if (beep) {
  985. UIManager.getLookAndFeel().provideErrorFeedback(target);
  986. }
  987. }
  988. }
  989. /*
  990. * Sets the editor into read-only mode.
  991. * @see DefaultEditorKit#readOnlyAction
  992. * @see DefaultEditorKit#getActions
  993. */
  994. static class ReadOnlyAction extends TextAction {
  995. /* Create this object with the appropriate identifier. */
  996. ReadOnlyAction() {
  997. super(readOnlyAction);
  998. }
  999. /**
  1000. * The operation to perform when this action is triggered.
  1001. *
  1002. * @param e the action event
  1003. */
  1004. public void actionPerformed(ActionEvent e) {
  1005. JTextComponent target = getTextComponent(e);
  1006. if (target != null) {
  1007. target.setEditable(false);
  1008. }
  1009. }
  1010. }
  1011. /*
  1012. * Sets the editor into writeable mode.
  1013. * @see DefaultEditorKit#writableAction
  1014. * @see DefaultEditorKit#getActions
  1015. */
  1016. static class WritableAction extends TextAction {
  1017. /* Create this object with the appropriate identifier. */
  1018. WritableAction() {
  1019. super(writableAction);
  1020. }
  1021. /**
  1022. * The operation to perform when this action is triggered.
  1023. *
  1024. * @param e the action event
  1025. */
  1026. public void actionPerformed(ActionEvent e) {
  1027. JTextComponent target = getTextComponent(e);
  1028. if (target != null) {
  1029. target.setEditable(true);
  1030. }
  1031. }
  1032. }
  1033. /**
  1034. * Cuts the selected region and place its contents
  1035. * into the system clipboard.
  1036. * <p>
  1037. * <strong>Warning:</strong>
  1038. * Serialized objects of this class will not be compatible with
  1039. * future Swing releases. The current serialization support is
  1040. * appropriate for short term storage or RMI between applications running
  1041. * the same version of Swing. As of 1.4, support for long term storage
  1042. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  1043. * has been added to the <code>java.beans</code> package.
  1044. * Please see {@link java.beans.XMLEncoder}.
  1045. *
  1046. * @see DefaultEditorKit#cutAction
  1047. * @see DefaultEditorKit#getActions
  1048. */
  1049. public static class CutAction extends TextAction {
  1050. /** Create this object with the appropriate identifier. */
  1051. public CutAction() {
  1052. super(cutAction);
  1053. }
  1054. /**
  1055. * The operation to perform when this action is triggered.
  1056. *
  1057. * @param e the action event
  1058. */
  1059. public void actionPerformed(ActionEvent e) {
  1060. JTextComponent target = getTextComponent(e);
  1061. if (target != null) {
  1062. target.cut();
  1063. }
  1064. }
  1065. }
  1066. /**
  1067. * Copies the selected region and place its contents
  1068. * into the system clipboard.
  1069. * <p>
  1070. * <strong>Warning:</strong>
  1071. * Serialized objects of this class will not be compatible with
  1072. * future Swing releases. The current serialization support is
  1073. * appropriate for short term storage or RMI between applications running
  1074. * the same version of Swing. As of 1.4, support for long term storage
  1075. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  1076. * has been added to the <code>java.beans</code> package.
  1077. * Please see {@link java.beans.XMLEncoder}.
  1078. *
  1079. * @see DefaultEditorKit#copyAction
  1080. * @see DefaultEditorKit#getActions
  1081. */
  1082. public static class CopyAction extends TextAction {
  1083. /** Create this object with the appropriate identifier. */
  1084. public CopyAction() {
  1085. super(copyAction);
  1086. }
  1087. /**
  1088. * The operation to perform when this action is triggered.
  1089. *
  1090. * @param e the action event
  1091. */
  1092. public void actionPerformed(ActionEvent e) {
  1093. JTextComponent target = getTextComponent(e);
  1094. if (target != null) {
  1095. target.copy();
  1096. }
  1097. }
  1098. }
  1099. /**
  1100. * Pastes the contents of the system clipboard into the
  1101. * selected region, or before the caret if nothing is
  1102. * selected.
  1103. * <p>
  1104. * <strong>Warning:</strong>
  1105. * Serialized objects of this class will not be compatible with
  1106. * future Swing releases. The current serialization support is
  1107. * appropriate for short term storage or RMI between applications running
  1108. * the same version of Swing. As of 1.4, support for long term storage
  1109. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  1110. * has been added to the <code>java.beans</code> package.
  1111. * Please see {@link java.beans.XMLEncoder}.
  1112. *
  1113. * @see DefaultEditorKit#pasteAction
  1114. * @see DefaultEditorKit#getActions
  1115. */
  1116. public static class PasteAction extends TextAction {
  1117. /** Create this object with the appropriate identifier. */
  1118. public PasteAction() {
  1119. super(pasteAction);
  1120. }
  1121. /**
  1122. * The operation to perform when this action is triggered.
  1123. *
  1124. * @param e the action event
  1125. */
  1126. public void actionPerformed(ActionEvent e) {
  1127. JTextComponent target = getTextComponent(e);
  1128. if (target != null) {
  1129. target.paste();
  1130. }
  1131. }
  1132. }
  1133. /**
  1134. * Creates a beep.
  1135. * <p>
  1136. * <strong>Warning:</strong>
  1137. * Serialized objects of this class will not be compatible with
  1138. * future Swing releases. The current serialization support is
  1139. * appropriate for short term storage or RMI between applications running
  1140. * the same version of Swing. As of 1.4, support for long term storage
  1141. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  1142. * has been added to the <code>java.beans</code> package.
  1143. * Please see {@link java.beans.XMLEncoder}.
  1144. *
  1145. * @see DefaultEditorKit#beepAction
  1146. * @see DefaultEditorKit#getActions
  1147. */
  1148. public static class BeepAction extends TextAction {
  1149. /** Create this object with the appropriate identifier. */
  1150. public BeepAction() {
  1151. super(beepAction);
  1152. }
  1153. /**
  1154. * The operation to perform when this action is triggered.
  1155. *
  1156. * @param e the action event
  1157. */
  1158. public void actionPerformed(ActionEvent e) {
  1159. JTextComponent target = getTextComponent(e);
  1160. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1161. }
  1162. }
  1163. /**
  1164. * Scrolls up/down vertically. The select version of this action extends
  1165. * the selection, instead of simply moving the caret.
  1166. *
  1167. * @see DefaultEditorKit#pageUpAction
  1168. * @see DefaultEditorKit#selectPageUpAction
  1169. * @see DefaultEditorKit#getActions
  1170. */
  1171. static class VerticalPageAction extends TextAction {
  1172. /** Create this object with the appropriate identifier. */
  1173. public VerticalPageAction(String nm, int direction, boolean select) {
  1174. super(nm);
  1175. this.select = select;
  1176. this.direction = direction;
  1177. }
  1178. /** The operation to perform when this action is triggered. */
  1179. public void actionPerformed(ActionEvent e) {
  1180. JTextComponent target = getTextComponent(e);
  1181. if (target != null) {
  1182. Rectangle visible = target.getVisibleRect();
  1183. Rectangle newVis = new Rectangle(visible);
  1184. int selectedIndex = target.getCaretPosition();
  1185. int yOffset = direction * visible.height;
  1186. int initialY = visible.y;
  1187. Caret caret = target.getCaret();
  1188. Point magicPosition = caret.getMagicCaretPosition();
  1189. newVis.y = constrainY(target, visible.y + yOffset, yOffset);
  1190. if (selectedIndex != -1) {
  1191. try {
  1192. Rectangle dotBounds = target.modelToView(
  1193. selectedIndex);
  1194. int x = (magicPosition != null) ? magicPosition.x :
  1195. dotBounds.x;
  1196. int newIndex;
  1197. if (visible.contains(dotBounds.x, dotBounds.y)) {
  1198. // Dot is currently visible, base the new
  1199. // location off the old, or
  1200. newIndex = target.viewToModel(
  1201. new Point(x, constrainY(target,
  1202. dotBounds.y + yOffset, 0)));
  1203. }
  1204. else {
  1205. // Dot isn't visible, choose the top or the bottom
  1206. // for the new location.
  1207. if (direction == -1) {
  1208. newIndex = target.viewToModel(new Point(
  1209. x, newVis.y));
  1210. }
  1211. else {
  1212. newIndex = target.viewToModel(new Point(
  1213. x, newVis.y + visible.height));
  1214. }
  1215. }
  1216. newIndex = constrainOffset(target, newIndex);
  1217. if (newIndex != selectedIndex) {
  1218. // Make sure the new visible location contains
  1219. // the location of dot, otherwise Caret will
  1220. // cause an additional scroll.
  1221. adjustScrollIfNecessary(target, newVis, initialY,
  1222. newIndex);
  1223. if (select) {
  1224. target.moveCaretPosition(newIndex);
  1225. }
  1226. else {
  1227. target.setCaretPosition(newIndex);
  1228. }
  1229. }
  1230. } catch (BadLocationException ble) { }
  1231. }
  1232. if (magicPosition != null) {
  1233. caret.setMagicCaretPosition(magicPosition);
  1234. }
  1235. target.scrollRectToVisible(newVis);
  1236. }
  1237. }
  1238. /**
  1239. * Makes sure <code>y</code> is a valid location in
  1240. * <code>target</code>.
  1241. */
  1242. private int constrainY(JTextComponent target, int y, int vis) {
  1243. if (y < 0) {
  1244. y = 0;
  1245. }
  1246. else if (y + vis > target.getHeight()) {
  1247. y = Math.max(0, target.getHeight() - vis);
  1248. }
  1249. return y;
  1250. }
  1251. /**
  1252. * Ensures that <code>offset</code> is a valid offset into the
  1253. * model for <code>text</code>.
  1254. */
  1255. private int constrainOffset(JTextComponent text, int offset) {
  1256. Document doc = text.getDocument();
  1257. if ((offset != 0) && (offset > doc.getLength())) {
  1258. offset = doc.getLength();
  1259. }
  1260. if (offset < 0) {
  1261. offset = 0;
  1262. }
  1263. return offset;
  1264. }
  1265. /**
  1266. * Adjusts the rectangle that indicates the location to scroll to
  1267. * after selecting <code>index</code>.
  1268. */
  1269. private void adjustScrollIfNecessary(JTextComponent text,
  1270. Rectangle visible, int initialY,
  1271. int index) {
  1272. try {
  1273. Rectangle dotBounds = text.modelToView(index);
  1274. if (dotBounds.y < visible.y ||
  1275. (dotBounds.y > (visible.y + visible.height)) ||
  1276. (dotBounds.y + dotBounds.height) >
  1277. (visible.y + visible.height)) {
  1278. int y;
  1279. if (dotBounds.y < visible.y) {
  1280. y = dotBounds.y;
  1281. }
  1282. else {
  1283. y = dotBounds.y + dotBounds.height - visible.height;
  1284. }
  1285. if ((direction == -1 && y < initialY) ||
  1286. (direction == 1 && y > initialY)) {
  1287. // Only adjust if won't cause scrolling upward.
  1288. visible.y = y;
  1289. }
  1290. }
  1291. } catch (BadLocationException ble) {}
  1292. }
  1293. /**
  1294. * Adjusts the Rectangle to contain the bounds of the character at
  1295. * <code>index</code> in response to a page up.
  1296. */
  1297. private boolean select;
  1298. /**
  1299. * Direction to scroll, 1 is down, -1 is up.
  1300. */
  1301. private int direction;
  1302. }
  1303. /**
  1304. * Pages one view to the left or right.
  1305. */
  1306. static class PageAction extends TextAction {
  1307. /** Create this object with the appropriate identifier. */
  1308. public PageAction(String nm, boolean left, boolean select) {
  1309. super(nm);
  1310. this.select = select;
  1311. this.left = left;
  1312. }
  1313. /** The operation to perform when this action is triggered. */
  1314. public void actionPerformed(ActionEvent e) {
  1315. JTextComponent target = getTextComponent(e);
  1316. if (target != null) {
  1317. int selectedIndex;
  1318. Rectangle visible = new Rectangle();
  1319. target.computeVisibleRect(visible);
  1320. if (left) {
  1321. visible.x = Math.max(0, visible.x - visible.width);
  1322. }
  1323. else {
  1324. visible.x += visible.width;
  1325. }
  1326. selectedIndex = target.getCaretPosition();
  1327. if(selectedIndex != -1) {
  1328. if (left) {
  1329. selectedIndex = target.viewToModel
  1330. (new Point(visible.x, visible.y));
  1331. }
  1332. else {
  1333. selectedIndex = target.viewToModel
  1334. (new Point(visible.x + visible.width - 1,
  1335. visible.y + visible.height - 1));
  1336. }
  1337. Document doc = target.getDocument();
  1338. if ((selectedIndex != 0) &&
  1339. (selectedIndex > (doc.getLength()-1))) {
  1340. selectedIndex = doc.getLength()-1;
  1341. }
  1342. else if(selectedIndex < 0) {
  1343. selectedIndex = 0;
  1344. }
  1345. if (select)
  1346. target.moveCaretPosition(selectedIndex);
  1347. else
  1348. target.setCaretPosition(selectedIndex);
  1349. }
  1350. }
  1351. }
  1352. private boolean select;
  1353. private boolean left;
  1354. }
  1355. static class DumpModelAction extends TextAction {
  1356. DumpModelAction() {
  1357. super("dump-model");
  1358. }
  1359. public void actionPerformed(ActionEvent e) {
  1360. JTextComponent target = getTextComponent(e);
  1361. if (target != null) {
  1362. Document d = target.getDocument();
  1363. if (d instanceof AbstractDocument) {
  1364. ((AbstractDocument) d).dump(System.err);
  1365. }
  1366. }
  1367. }
  1368. }
  1369. /*
  1370. * Action to move the selection by way of the
  1371. * getNextVisualPositionFrom method. Constructor indicates direction
  1372. * to use.
  1373. */
  1374. static class NextVisualPositionAction extends TextAction {
  1375. /**
  1376. * Create this action with the appropriate identifier.
  1377. * @param nm the name of the action, Action.NAME.
  1378. * @param select whether to extend the selection when
  1379. * changing the caret position.
  1380. */
  1381. NextVisualPositionAction(String nm, boolean select, int direction) {
  1382. super(nm);
  1383. this.select = select;
  1384. this.direction = direction;
  1385. }
  1386. /** The operation to perform when this action is triggered. */
  1387. public void actionPerformed(ActionEvent e) {
  1388. JTextComponent target = getTextComponent(e);
  1389. if (target != null) {
  1390. Caret caret = target.getCaret();
  1391. DefaultCaret bidiCaret = (caret instanceof DefaultCaret) ?
  1392. (DefaultCaret)caret : null;
  1393. int dot = caret.getDot();
  1394. Position.Bias[] bias = new Position.Bias[1];
  1395. Point magicPosition = caret.getMagicCaretPosition();
  1396. try {
  1397. if(magicPosition == null &&
  1398. (direction == SwingConstants.NORTH ||
  1399. direction == SwingConstants.SOUTH)) {
  1400. Rectangle r = (bidiCaret != null) ?
  1401. target.getUI().modelToView(target, dot,
  1402. bidiCaret.getDotBias()) :
  1403. target.modelToView(dot);
  1404. magicPosition = new Point(r.x, r.y);
  1405. }
  1406. NavigationFilter filter = target.getNavigationFilter();
  1407. if (filter != null) {
  1408. dot = filter.getNextVisualPositionFrom
  1409. (target, dot, (bidiCaret != null) ?
  1410. bidiCaret.getDotBias() :
  1411. Position.Bias.Forward, direction, bias);
  1412. }
  1413. else {
  1414. dot = target.getUI().getNextVisualPositionFrom
  1415. (target, dot, (bidiCaret != null) ?
  1416. bidiCaret.getDotBias() :
  1417. Position.Bias.Forward, direction, bias);
  1418. }
  1419. if(bias[0] == null) {
  1420. bias[0] = Position.Bias.Forward;
  1421. }
  1422. if(bidiCaret != null) {
  1423. if (select) {
  1424. bidiCaret.moveDot(dot, bias[0]);
  1425. } else {
  1426. bidiCaret.setDot(dot, bias[0]);
  1427. }
  1428. }
  1429. else {
  1430. if (select) {
  1431. caret.moveDot(dot);
  1432. } else {
  1433. caret.setDot(dot);
  1434. }
  1435. }
  1436. if(magicPosition != null &&
  1437. (direction == SwingConstants.NORTH ||
  1438. direction == SwingConstants.SOUTH)) {
  1439. target.getCaret().setMagicCaretPosition(magicPosition);
  1440. }
  1441. } catch (BadLocationException ex) {
  1442. }
  1443. }
  1444. }
  1445. private boolean select;
  1446. private int direction;
  1447. }
  1448. /*
  1449. * Position the caret to the beginning of the word.
  1450. * @see DefaultEditorKit#beginWordAction
  1451. * @see DefaultEditorKit#selectBeginWordAction
  1452. * @see DefaultEditorKit#getActions
  1453. */
  1454. static class BeginWordAction extends TextAction {
  1455. /**
  1456. * Create this action with the appropriate identifier.
  1457. * @param nm the name of the action, Action.NAME.
  1458. * @param select whether to extend the selection when
  1459. * changing the caret position.
  1460. */
  1461. BeginWordAction(String nm, boolean select) {
  1462. super(nm);
  1463. this.select = select;
  1464. }
  1465. /** The operation to perform when this action is triggered. */
  1466. public void actionPerformed(ActionEvent e) {
  1467. JTextComponent target = getTextComponent(e);
  1468. if (target != null) {
  1469. try {
  1470. int offs = target.getCaretPosition();
  1471. int begOffs = Utilities.getWordStart(target, offs);
  1472. if (select) {
  1473. target.moveCaretPosition(begOffs);
  1474. } else {
  1475. target.setCaretPosition(begOffs);
  1476. }
  1477. } catch (BadLocationException bl) {
  1478. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1479. }
  1480. }
  1481. }
  1482. private boolean select;
  1483. }
  1484. /*
  1485. * Position the caret to the end of the word.
  1486. * @see DefaultEditorKit#endWordAction
  1487. * @see DefaultEditorKit#selectEndWordAction
  1488. * @see DefaultEditorKit#getActions
  1489. */
  1490. static class EndWordAction extends TextAction {
  1491. /**
  1492. * Create this action with the appropriate identifier.
  1493. * @param nm the name of the action, Action.NAME.
  1494. * @param select whether to extend the selection when
  1495. * changing the caret position.
  1496. */
  1497. EndWordAction(String nm, boolean select) {
  1498. super(nm);
  1499. this.select = select;
  1500. }
  1501. /** The operation to perform when this action is triggered. */
  1502. public void actionPerformed(ActionEvent e) {
  1503. JTextComponent target = getTextComponent(e);
  1504. if (target != null) {
  1505. try {
  1506. int offs = target.getCaretPosition();
  1507. int endOffs = Utilities.getWordEnd(target, offs);
  1508. if (select) {
  1509. target.moveCaretPosition(endOffs);
  1510. } else {
  1511. target.setCaretPosition(endOffs);
  1512. }
  1513. } catch (BadLocationException bl) {
  1514. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1515. }
  1516. }
  1517. }
  1518. private boolean select;
  1519. }
  1520. /*
  1521. * Position the caret to the beginning of the previous word.
  1522. * @see DefaultEditorKit#previousWordAction
  1523. * @see DefaultEditorKit#selectPreviousWordAction
  1524. * @see DefaultEditorKit#getActions
  1525. */
  1526. static class PreviousWordAction extends TextAction {
  1527. /**
  1528. * Create this action with the appropriate identifier.
  1529. * @param nm the name of the action, Action.NAME.
  1530. * @param select whether to extend the selection when
  1531. * changing the caret position.
  1532. */
  1533. PreviousWordAction(String nm, boolean select) {
  1534. super(nm);
  1535. this.select = select;
  1536. }
  1537. /** The operation to perform when this action is triggered. */
  1538. public void actionPerformed(ActionEvent e) {
  1539. JTextComponent target = getTextComponent(e);
  1540. if (target != null) {
  1541. int offs = target.getCaretPosition();
  1542. boolean failed = false;
  1543. try {
  1544. offs = Utilities.getPreviousWord(target, offs);
  1545. } catch (BadLocationException bl) {
  1546. if (offs != 0) {
  1547. offs = 0;
  1548. }
  1549. else {
  1550. failed = true;
  1551. }
  1552. }
  1553. if (!failed) {
  1554. if (select) {
  1555. target.moveCaretPosition(offs);
  1556. } else {
  1557. target.setCaretPosition(offs);
  1558. }
  1559. }
  1560. else {
  1561. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1562. }
  1563. }
  1564. }
  1565. private boolean select;
  1566. }
  1567. /*
  1568. * Position the caret to the next of the word.
  1569. * @see DefaultEditorKit#nextWordAction
  1570. * @see DefaultEditorKit#selectNextWordAction
  1571. * @see DefaultEditorKit#getActions
  1572. */
  1573. static class NextWordAction extends TextAction {
  1574. /**
  1575. * Create this action with the appropriate identifier.
  1576. * @param nm the name of the action, Action.NAME.
  1577. * @param select whether to extend the selection when
  1578. * changing the caret position.
  1579. */
  1580. NextWordAction(String nm, boolean select) {
  1581. super(nm);
  1582. this.select = select;
  1583. }
  1584. /** The operation to perform when this action is triggered. */
  1585. public void actionPerformed(ActionEvent e) {
  1586. JTextComponent target = getTextComponent(e);
  1587. if (target != null) {
  1588. int offs = target.getCaretPosition();
  1589. boolean failed = false;
  1590. try {
  1591. offs = Utilities.getNextWord(target, offs);
  1592. } catch (BadLocationException bl) {
  1593. int end = target.getDocument().getLength();
  1594. if (offs != end) {
  1595. offs = end;
  1596. }
  1597. else {
  1598. failed = true;
  1599. }
  1600. }
  1601. if (!failed) {
  1602. if (select) {
  1603. target.moveCaretPosition(offs);
  1604. } else {
  1605. target.setCaretPosition(offs);
  1606. }
  1607. }
  1608. else {
  1609. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1610. }
  1611. }
  1612. }
  1613. private boolean select;
  1614. }
  1615. /*
  1616. * Position the caret to the beginning of the line.
  1617. * @see DefaultEditorKit#beginLineAction
  1618. * @see DefaultEditorKit#selectBeginLineAction
  1619. * @see DefaultEditorKit#getActions
  1620. */
  1621. static class BeginLineAction extends TextAction {
  1622. /**
  1623. * Create this action with the appropriate identifier.
  1624. * @param nm the name of the action, Action.NAME.
  1625. * @param select whether to extend the selection when
  1626. * changing the caret position.
  1627. */
  1628. BeginLineAction(String nm, boolean select) {
  1629. super(nm);
  1630. this.select = select;
  1631. }
  1632. /** The operation to perform when this action is triggered. */
  1633. public void actionPerformed(ActionEvent e) {
  1634. JTextComponent target = getTextComponent(e);
  1635. if (target != null) {
  1636. try {
  1637. int offs = target.getCaretPosition();
  1638. int begOffs = Utilities.getRowStart(target, offs);
  1639. if (select) {
  1640. target.moveCaretPosition(begOffs);
  1641. } else {
  1642. target.setCaretPosition(begOffs);
  1643. }
  1644. } catch (BadLocationException bl) {
  1645. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1646. }
  1647. }
  1648. }
  1649. private boolean select;
  1650. }
  1651. /*
  1652. * Position the caret to the end of the line.
  1653. * @see DefaultEditorKit#endLineAction
  1654. * @see DefaultEditorKit#selectEndLineAction
  1655. * @see DefaultEditorKit#getActions
  1656. */
  1657. static class EndLineAction extends TextAction {
  1658. /**
  1659. * Create this action with the appropriate identifier.
  1660. * @param nm the name of the action, Action.NAME.
  1661. * @param select whether to extend the selection when
  1662. * changing the caret position.
  1663. */
  1664. EndLineAction(String nm, boolean select) {
  1665. super(nm);
  1666. this.select = select;
  1667. }
  1668. /** The operation to perform when this action is triggered. */
  1669. public void actionPerformed(ActionEvent e) {
  1670. JTextComponent target = getTextComponent(e);
  1671. if (target != null) {
  1672. try {
  1673. int offs = target.getCaretPosition();
  1674. int endOffs = Utilities.getRowEnd(target, offs);
  1675. if (select) {
  1676. target.moveCaretPosition(endOffs);
  1677. } else {
  1678. target.setCaretPosition(endOffs);
  1679. }
  1680. } catch (BadLocationException bl) {
  1681. UIManager.getLookAndFeel().provideErrorFeedback(target);
  1682. }
  1683. }
  1684. }
  1685. private boolean select;
  1686. }
  1687. /*
  1688. * Position the caret to the beginning of the paragraph.
  1689. * @see DefaultEditorKit#beginParagraphAction
  1690. * @see DefaultEditorKit#selectBeginParagraphAction
  1691. * @see DefaultEditorKit#getActions
  1692. */
  1693. static class BeginParagraphAction extends TextAction {
  1694. /**
  1695. * Create this action with the appropriate identifier.
  1696. * @param nm the name of the action, Action.NAME.
  1697. * @param select whether to extend the selection when
  1698. * changing the caret position.
  1699. */
  1700. BeginParagraphAction(String nm, boolean select) {
  1701. super(nm);
  1702. this.select = select;
  1703. }
  1704. /** The operation to perform when this action is triggered. */
  1705. public void actionPerformed(ActionEvent e) {
  1706. JTextComponent target = getTextComponent(e);
  1707. if (target != null) {
  1708. int offs = target.getCaretPosition();
  1709. Element elem = Utilities.getParagraphElement(target, offs);
  1710. offs = elem.getStartOffset();
  1711. if (select) {
  1712. target.moveCaretPosition(offs);
  1713. } else {
  1714. target.setCaretPosition(offs);
  1715. }
  1716. }
  1717. }
  1718. private boolean select;
  1719. }
  1720. /*
  1721. * Position the caret to the end of the paragraph.
  1722. * @see DefaultEditorKit#endParagraphAction
  1723. * @see DefaultEditorKit#selectEndParagraphAction
  1724. * @see DefaultEditorKit#getActions
  1725. */
  1726. static class EndParagraphAction extends TextAction {
  1727. /**
  1728. * Create this action with the appropriate identifier.
  1729. * @param nm the name of the action, Action.NAME.
  1730. * @param select whether to extend the selection when
  1731. * changing the caret position.
  1732. */
  1733. EndParagraphAction(String nm, boolean select) {
  1734. super(nm);
  1735. this.select = select;
  1736. }
  1737. /** The operation to perform when this action is triggered. */
  1738. public void actionPerformed(ActionEvent e) {
  1739. JTextComponent target = getTextComponent(e);
  1740. if (target != null) {
  1741. int offs = target.getCaretPosition();
  1742. Element elem = Utilities.getParagraphElement(target, offs);
  1743. offs = Math.min(target.getDocument().getLength(),
  1744. elem.getEndOffset());
  1745. if (select) {
  1746. target.moveCaretPosition(offs);
  1747. } else {
  1748. target.setCaretPosition(offs);
  1749. }
  1750. }
  1751. }
  1752. private boolean select;
  1753. }
  1754. /*
  1755. * Move the caret to the beginning of the document.
  1756. * @see DefaultEditorKit#beginAction
  1757. * @see DefaultEditorKit#getActions
  1758. */
  1759. static class BeginAction extends TextAction {
  1760. /* Create this object with the appropriate identifier. */
  1761. BeginAction(String nm, boolean select) {
  1762. super(nm);
  1763. this.select = select;
  1764. }
  1765. /** The operation to perform when this action is triggered. */
  1766. public void actionPerformed(ActionEvent e) {
  1767. JTextComponent target = getTextComponent(e);
  1768. if (target != null) {
  1769. if (select) {
  1770. target.moveCaretPosition(0);
  1771. } else {
  1772. target.setCaretPosition(0);
  1773. }
  1774. }
  1775. }
  1776. private boolean select;
  1777. }
  1778. /*
  1779. * Move the caret to the end of the document.
  1780. * @see DefaultEditorKit#endAction
  1781. * @see DefaultEditorKit#getActions
  1782. */
  1783. static class EndAction extends TextAction {
  1784. /* Create this object with the appropriate identifier. */
  1785. EndAction(String nm, boolean select) {
  1786. super(nm);
  1787. this.select = select;
  1788. }
  1789. /** The operation to perform when this action is triggered. */
  1790. public void actionPerformed(ActionEvent e) {
  1791. JTextComponent target = getTextComponent(e);
  1792. if (target != null) {
  1793. Document doc = target.getDocument();
  1794. int dot = doc.getLength();
  1795. if (select) {
  1796. target.moveCaretPosition(dot);
  1797. } else {
  1798. target.setCaretPosition(dot);
  1799. }
  1800. }
  1801. }
  1802. private boolean select;
  1803. }
  1804. /*
  1805. * Select the word around the caret
  1806. * @see DefaultEditorKit#endAction
  1807. * @see DefaultEditorKit#getActions
  1808. */
  1809. static class SelectWordAction extends TextAction {
  1810. /**
  1811. * Create this action with the appropriate identifier.
  1812. * @param nm the name of the action, Action.NAME.
  1813. * @param select whether to extend the selection when
  1814. * changing the caret position.
  1815. */
  1816. SelectWordAction() {
  1817. super(selectWordAction);
  1818. start = new BeginWordAction("pigdog", false);
  1819. end = new EndWordAction("pigdog", true);
  1820. }
  1821. /** The operation to perform when this action is triggered. */
  1822. public void actionPerformed(ActionEvent e) {
  1823. start.actionPerformed(e);
  1824. end.actionPerformed(e);
  1825. }
  1826. private Action start;
  1827. private Action end;
  1828. }
  1829. /*
  1830. * Select the line around the caret
  1831. * @see DefaultEditorKit#endAction
  1832. * @see DefaultEditorKit#getActions
  1833. */
  1834. static class SelectLineAction extends TextAction {
  1835. /**
  1836. * Create this action with the appropriate identifier.
  1837. * @param nm the name of the action, Action.NAME.
  1838. * @param select whether to extend the selection when
  1839. * changing the caret position.
  1840. */
  1841. SelectLineAction() {
  1842. super(selectLineAction);
  1843. start = new BeginLineAction("pigdog", false);
  1844. end = new EndLineAction("pigdog", true);
  1845. }
  1846. /** The operation to perform when this action is triggered. */
  1847. public void actionPerformed(ActionEvent e) {
  1848. start.actionPerformed(e);
  1849. end.actionPerformed(e);
  1850. }
  1851. private Action start;
  1852. private Action end;
  1853. }
  1854. /*
  1855. * Select the paragraph around the caret
  1856. * @see DefaultEditorKit#endAction
  1857. * @see DefaultEditorKit#getActions
  1858. */
  1859. static class SelectParagraphAction extends TextAction {
  1860. /**
  1861. * Create this action with the appropriate identifier.
  1862. * @param nm the name of the action, Action.NAME.
  1863. * @param select whether to extend the selection when
  1864. * changing the caret position.
  1865. */
  1866. SelectParagraphAction() {
  1867. super(selectParagraphAction);
  1868. start = new BeginParagraphAction("pigdog", false);
  1869. end = new EndParagraphAction("pigdog", true);
  1870. }
  1871. /** The operation to perform when this action is triggered. */
  1872. public void actionPerformed(ActionEvent e) {
  1873. start.actionPerformed(e);
  1874. end.actionPerformed(e);
  1875. }
  1876. private Action start;
  1877. private Action end;
  1878. }
  1879. /*
  1880. * Select the entire document
  1881. * @see DefaultEditorKit#endAction
  1882. * @see DefaultEditorKit#getActions
  1883. */
  1884. static class SelectAllAction extends TextAction {
  1885. /**
  1886. * Create this action with the appropriate identifier.
  1887. * @param nm the name of the action, Action.NAME.
  1888. * @param select whether to extend the selection when
  1889. * changing the caret position.
  1890. */
  1891. SelectAllAction() {
  1892. super(selectAllAction);
  1893. }
  1894. /** The operation to perform when this action is triggered. */
  1895. public void actionPerformed(ActionEvent e) {
  1896. JTextComponent target = getTextComponent(e);
  1897. if (target != null) {
  1898. Document doc = target.getDocument();
  1899. target.setCaretPosition(0);
  1900. target.moveCaretPosition(doc.getLength());
  1901. }
  1902. }
  1903. }
  1904. /*
  1905. * Remove the selection, if any.
  1906. * @see DefaultEditorKit#unselectAction
  1907. * @see DefaultEditorKit#getActions
  1908. */
  1909. static class UnselectAction extends TextAction {
  1910. /**
  1911. * Create this action with the appropriate identifier.
  1912. */
  1913. UnselectAction() {
  1914. super(unselectAction);
  1915. }
  1916. /** The operation to perform when this action is triggered. */
  1917. public void actionPerformed(ActionEvent e) {
  1918. JTextComponent target = getTextComponent(e);
  1919. if (target != null) {
  1920. target.setCaretPosition(target.getCaretPosition());
  1921. }
  1922. }
  1923. }
  1924. /*
  1925. * Toggles the ComponentOrientation of the text component.
  1926. * @see DefaultEditorKit#toggleComponentOrientationAction
  1927. * @see DefaultEditorKit#getActions
  1928. */
  1929. static class ToggleComponentOrientationAction extends TextAction {
  1930. /**
  1931. * Create this action with the appropriate identifier.
  1932. */
  1933. ToggleComponentOrientationAction() {
  1934. super(toggleComponentOrientationAction);
  1935. }
  1936. /** The operation to perform when this action is triggered. */
  1937. public void actionPerformed(ActionEvent e) {
  1938. JTextComponent target = getTextComponent(e);
  1939. if (target != null) {
  1940. ComponentOrientation last = target.getComponentOrientation();
  1941. ComponentOrientation next;
  1942. if( last == ComponentOrientation.RIGHT_TO_LEFT )
  1943. next = ComponentOrientation.LEFT_TO_RIGHT;
  1944. else
  1945. next = ComponentOrientation.RIGHT_TO_LEFT;
  1946. target.setComponentOrientation(next);
  1947. target.repaint();
  1948. }
  1949. }
  1950. }
  1951. }