1 /* DefaultEditorKit.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.awt.Point;
42 import java.awt.Toolkit;
43 import java.awt.event.ActionEvent;
45 import java.io.BufferedReader;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.io.InputStreamReader;
49 import java.io.OutputStream;
50 import java.io.OutputStreamWriter;
51 import java.io.Reader;
52 import java.io.Writer;
54 import javax.swing.Action;
55 import javax.swing.SwingConstants;
58 * The default implementation of {@link EditorKit}. This <code>EditorKit</code>
59 * a plain text <code>Document</code> and several commands that together
60 * make up a basic editor, like cut / copy + paste.
62 * @author original author unknown
63 * @author Roman Kennke (roman@kennke.org)
64 * @author Robert Schuster (robertschuster@fsfe.org)
66 public class DefaultEditorKit extends EditorKit
68 static class SelectionPreviousWordAction
71 SelectionPreviousWordAction()
73 super(selectionPreviousWordAction);
76 public void actionPerformed(ActionEvent event)
80 JTextComponent t = getTextComponent(event);
84 int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
86 Caret c = t.getCaret();
88 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
91 catch(BadLocationException ble)
98 static class SelectionNextWordAction
101 SelectionNextWordAction()
103 super(selectionNextWordAction);
106 public void actionPerformed(ActionEvent event)
110 JTextComponent t = getTextComponent(event);
114 int offs = Utilities.getNextWord(t, t.getCaretPosition());
116 Caret c = t.getCaret();
118 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
121 catch(BadLocationException ble)
128 static class SelectionBeginWordAction extends TextAction
130 SelectionBeginWordAction()
132 super(selectionBeginWordAction);
135 public void actionPerformed(ActionEvent event)
139 JTextComponent t = getTextComponent(event);
143 int offs = Utilities.getWordStart(t, t.getCaretPosition());
145 Caret c = t.getCaret();
147 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
150 catch(BadLocationException ble)
157 static class SelectionEndWordAction extends TextAction
159 SelectionEndWordAction()
161 super(selectionEndWordAction);
164 public void actionPerformed(ActionEvent event)
168 JTextComponent t = getTextComponent(event);
172 int offs = Utilities.getWordEnd(t, t.getCaretPosition());
174 Caret c = t.getCaret();
176 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
179 catch(BadLocationException ble)
186 static class BeginWordAction extends TextAction
190 super(beginWordAction);
193 public void actionPerformed(ActionEvent event)
197 JTextComponent t = getTextComponent(event);
201 int offs = Utilities.getWordStart(t, t.getCaretPosition());
203 Caret c = t.getCaret();
205 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
208 catch(BadLocationException ble)
215 static class EndWordAction extends TextAction
219 super(endWordAction);
222 public void actionPerformed(ActionEvent event)
226 JTextComponent t = getTextComponent(event);
230 int offs = Utilities.getWordEnd(t, t.getCaretPosition());
232 Caret c = t.getCaret();
234 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
237 catch(BadLocationException ble)
244 static class PreviousWordAction
249 super(previousWordAction);
252 public void actionPerformed(ActionEvent event)
256 JTextComponent t = getTextComponent(event);
260 int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
262 Caret c = t.getCaret();
264 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
267 catch(BadLocationException ble)
274 static class NextWordAction
279 super(nextWordAction);
282 public void actionPerformed(ActionEvent event)
286 JTextComponent t = getTextComponent(event);
290 int offs = Utilities.getNextWord(t, t.getCaretPosition());
292 Caret c = t.getCaret();
294 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
297 catch(BadLocationException ble)
304 static class SelectAllAction
309 super(selectAllAction);
312 public void actionPerformed(ActionEvent event)
314 JTextComponent t = getTextComponent(event);
315 int offs = t.getDocument().getLength();
316 Caret c = t.getCaret();
322 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
324 catch(BadLocationException ble)
331 static class SelectionBeginAction
334 SelectionBeginAction()
336 super(selectionBeginAction);
339 public void actionPerformed(ActionEvent event)
341 JTextComponent t = getTextComponent(event);
342 Caret c = t.getCaret();
346 c.setMagicCaretPosition(t.modelToView(0).getLocation());
348 catch(BadLocationException ble)
355 static class SelectionEndAction
360 super(selectionEndAction);
363 public void actionPerformed(ActionEvent event)
365 JTextComponent t = getTextComponent(event);
366 int offs = t.getDocument().getLength();
367 Caret c = t.getCaret();
371 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
373 catch(BadLocationException ble)
380 static class SelectionBeginLineAction
384 SelectionBeginLineAction()
386 super(selectionBeginLineAction);
389 public void actionPerformed(ActionEvent event)
391 JTextComponent t = getTextComponent(event);
392 Caret c = t.getCaret();
395 int offs = Utilities.getRowStart(t, c.getDot());
396 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
398 catch(BadLocationException ble)
406 static class SelectionEndLineAction
409 SelectionEndLineAction()
411 super(selectionEndLineAction);
414 public void actionPerformed(ActionEvent event)
416 JTextComponent t = getTextComponent(event);
417 Caret c = t.getCaret();
420 int offs = Utilities.getRowEnd(t, c.getDot());
421 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
423 catch(BadLocationException ble)
431 static class SelectLineAction extends TextAction
435 super(selectLineAction);
438 public void actionPerformed(ActionEvent event)
440 JTextComponent t = getTextComponent(event);
441 Caret c = t.getCaret();
444 int offs1 = Utilities.getRowStart(t, c.getDot());
445 int offs2 = Utilities.getRowEnd(t, c.getDot());
450 c.setMagicCaretPosition(t.modelToView(offs2).getLocation());
452 catch(BadLocationException ble)
459 static class SelectWordAction extends TextAction
463 super(selectWordAction);
466 public void actionPerformed(ActionEvent event)
468 JTextComponent t = getTextComponent(event);
469 Caret c = t.getCaret();
470 int dot = c.getDot();
474 int wordStart = Utilities.getWordStart(t, dot);
476 if (dot == wordStart)
478 // Current cursor position is on the first character in a word.
480 c.moveDot(Utilities.getWordEnd(t, wordStart));
484 // Current cursor position is not on the first character
486 int nextWord = Utilities.getNextWord(t, dot);
487 int previousWord = Utilities.getPreviousWord(t, dot);
488 int previousWordEnd = Utilities.getWordEnd(t, previousWord);
490 // Cursor position is in the space between two words. In such a
491 // situation just select the space.
492 if (dot >= previousWordEnd && dot <= nextWord)
494 c.setDot(previousWordEnd);
499 // Cursor position is inside a word. Just select it then.
500 c.setDot(previousWord);
501 c.moveDot(previousWordEnd);
505 // If the position was updated change the magic caret position
507 if (c.getDot() != dot)
508 c.setMagicCaretPosition(t.modelToView(c.getDot()).getLocation());
511 catch(BadLocationException ble)
518 static class SelectionDownAction
519 extends TextAction.VerticalMovementAction
521 SelectionDownAction()
523 super(selectionDownAction, SwingConstants.SOUTH);
526 protected void actionPerformedImpl(Caret c, int offs)
533 static class SelectionUpAction
534 extends TextAction.VerticalMovementAction
538 super(selectionUpAction, SwingConstants.NORTH);
541 protected void actionPerformedImpl(Caret c, int offs)
548 static class SelectionForwardAction
549 extends TextAction.HorizontalMovementAction
551 SelectionForwardAction()
553 super(selectionForwardAction, SwingConstants.EAST);
556 protected void actionPerformedImpl(Caret c, int offs)
562 static class SelectionBackwardAction
563 extends TextAction.HorizontalMovementAction
565 SelectionBackwardAction()
567 super(selectionBackwardAction, SwingConstants.WEST);
570 protected void actionPerformedImpl(Caret c, int offs)
576 static class DownAction
577 extends TextAction.VerticalMovementAction
581 super(downAction, SwingConstants.SOUTH);
584 protected void actionPerformedImpl(Caret c, int offs)
590 static class UpAction
591 extends TextAction.VerticalMovementAction
595 super(upAction, SwingConstants.NORTH);
598 protected void actionPerformedImpl(Caret c, int offs)
605 static class ForwardAction
606 extends TextAction.HorizontalMovementAction
610 super(forwardAction, SwingConstants.EAST);
613 protected void actionPerformedImpl(Caret c, int offs)
620 static class BackwardAction
621 extends TextAction.HorizontalMovementAction
625 super(backwardAction, SwingConstants.WEST);
628 protected void actionPerformedImpl(Caret c, int offs)
635 static class DeletePrevCharAction
638 DeletePrevCharAction()
640 super(deletePrevCharAction);
643 public void actionPerformed(ActionEvent event)
645 JTextComponent t = getTextComponent(event);
650 int pos = t.getSelectionStart();
651 int len = t.getSelectionEnd() - pos;
654 t.getDocument().remove(pos, len);
658 t.getDocument().remove(pos, 1);
659 Caret c = t.getCaret();
661 c.setMagicCaretPosition(t.modelToView(pos).getLocation());
664 catch (BadLocationException e)
666 // FIXME: we're not authorized to throw this.. swallow it?
672 static class DeleteNextCharAction
675 DeleteNextCharAction()
677 super(deleteNextCharAction);
680 public void actionPerformed(ActionEvent event)
682 JTextComponent t = getTextComponent(event);
687 int pos = t.getSelectionStart();
688 int len = t.getSelectionEnd() - pos;
691 t.getDocument().remove(pos, len);
692 else if (pos < t.getDocument().getLength())
693 t.getDocument().remove(pos, 1);
695 Caret c = t.getCaret();
697 c.setMagicCaretPosition(t.modelToView(pos).getLocation());
699 catch (BadLocationException e)
701 // FIXME: we're not authorized to throw this.. swallow it?
707 static class EndLineAction
712 super(endLineAction);
715 public void actionPerformed(ActionEvent event)
717 JTextComponent t = getTextComponent(event);
720 int offs = Utilities.getRowEnd(t, t.getCaretPosition());
724 Caret c = t.getCaret();
726 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
729 catch (BadLocationException ble)
731 // Nothing to do here
736 static class BeginLineAction
741 super(beginLineAction);
744 public void actionPerformed(ActionEvent event)
746 JTextComponent t = getTextComponent(event);
749 int offs = Utilities.getRowStart(t, t.getCaretPosition());
753 Caret c = t.getCaret();
755 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
758 catch (BadLocationException ble)
765 static class BeginAction extends TextAction
773 public void actionPerformed(ActionEvent event)
775 JTextComponent t = getTextComponent(event);
776 Caret c = t.getCaret();
780 c.setMagicCaretPosition(t.modelToView(0).getLocation());
782 catch(BadLocationException ble)
789 static class EndAction extends TextAction
797 public void actionPerformed(ActionEvent event)
799 JTextComponent t = getTextComponent(event);
800 int offs = t.getDocument().getLength();
801 Caret c = t.getCaret();
805 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
807 catch(BadLocationException ble)
815 * Creates a beep on the PC speaker.
817 * @see Toolkit#beep()
819 public static class BeepAction extends TextAction
822 * Creates a new <code>BeepAction</code>.
830 * Performs the <code>Action</code>.
832 * @param event the action event describing the user action
834 public void actionPerformed(ActionEvent event)
836 Toolkit.getDefaultToolkit().beep();
841 * Copies the selected content into the system clipboard.
843 * @see Toolkit#getSystemClipboard()
847 public static class CopyAction extends TextAction
851 * Create a new <code>CopyAction</code>.
859 * Performs the <code>Action</code>.
861 * @param event the action event describing the user action
863 public void actionPerformed(ActionEvent event)
865 getTextComponent(event).copy();
871 * Copies the selected content into the system clipboard and deletes the
874 * @see Toolkit#getSystemClipboard()
878 public static class CutAction extends TextAction
882 * Create a new <code>CutAction</code>.
890 * Performs the <code>Action</code>.
892 * @param event the action event describing the user action
894 public void actionPerformed(ActionEvent event)
896 getTextComponent(event).cut();
901 * Copies content from the system clipboard into the editor.
903 * @see Toolkit#getSystemClipboard()
907 public static class PasteAction extends TextAction
911 * Create a new <code>PasteAction</code>.
919 * Performs the <code>Action</code>.
921 * @param event the action event describing the user action
923 public void actionPerformed(ActionEvent event)
925 getTextComponent(event).paste();
930 * This action is executed as default action when a KEY_TYPED
931 * event is received and no keymap entry exists for that. The purpose
932 * of this action is to filter out a couple of characters. This includes
933 * the control characters and characters with the ALT-modifier.
935 * If an event does not get filtered, it is inserted into the document
936 * of the text component. If there is some text selected in the text
937 * component, this text will be replaced.
939 public static class DefaultKeyTypedAction
944 * Creates a new <code>DefaultKeyTypedAction</code>.
946 public DefaultKeyTypedAction()
948 super(defaultKeyTypedAction);
952 * Performs the <code>Action</code>.
954 * @param event the action event describing the user action
956 public void actionPerformed(ActionEvent event)
958 // first we filter the following events:
959 // - control characters
960 // - key events with the ALT modifier (FIXME: filter that too!)
961 int cp = event.getActionCommand().codePointAt(0);
962 if (Character.isISOControl(cp))
965 JTextComponent t = getTextComponent(event);
966 if (t != null && t.isEnabled() && t.isEditable())
967 t.replaceSelection(event.getActionCommand());
972 * This action inserts a newline character into the document
973 * of the text component. This is typically triggered by hitting
974 * ENTER on the keyboard.
976 public static class InsertBreakAction extends TextAction
980 * Creates a new <code>InsertBreakAction</code>.
982 public InsertBreakAction()
984 super(insertBreakAction);
988 * Performs the <code>Action</code>.
990 * @param event the action event describing the user action
992 public void actionPerformed(ActionEvent event)
994 JTextComponent t = getTextComponent(event);
995 t.replaceSelection("\n");
1000 * Places content into the associated editor. If there currently is a
1001 * selection, this selection is replaced.
1003 // FIXME: Figure out what this Action is supposed to do. Obviously text
1004 // that is entered by the user is inserted through DefaultKeyTypedAction.
1005 public static class InsertContentAction extends TextAction
1009 * Creates a new <code>InsertContentAction</code>.
1011 public InsertContentAction()
1013 super(insertContentAction);
1017 * Performs the <code>Action</code>.
1019 * @param event the action event describing the user action
1021 public void actionPerformed(ActionEvent event)
1023 // FIXME: Figure out what this Action is supposed to do. Obviously text
1024 // that is entered by the user is inserted through DefaultKeyTypedAction.
1029 * Inserts a TAB character into the text editor.
1031 public static class InsertTabAction extends TextAction
1035 * Creates a new <code>TabAction</code>.
1037 public InsertTabAction()
1039 super(insertTabAction);
1043 * Performs the <code>Action</code>.
1045 * @param event the action event describing the user action
1047 public void actionPerformed(ActionEvent event)
1049 JTextComponent t = getTextComponent(event);
1050 t.replaceSelection("\t");
1055 * The serial version of DefaultEditorKit.
1057 private static final long serialVersionUID = 9017245433028523428L;
1060 * The name of the <code>Action</code> that moves the caret one character
1063 * @see #getActions()
1065 public static final String backwardAction = "caret-backward";
1068 * The name of the <code>Action</code> that creates a beep in the speaker.
1070 * @see #getActions()
1072 public static final String beepAction = "beep";
1075 * The name of the <code>Action</code> that moves the caret to the beginning
1076 * of the <code>Document</code>.
1078 * @see #getActions()
1080 public static final String beginAction = "caret-begin";
1083 * The name of the <code>Action</code> that moves the caret to the beginning
1084 * of the current line.
1086 * @see #getActions()
1088 public static final String beginLineAction = "caret-begin-line";
1091 * The name of the <code>Action</code> that moves the caret to the beginning
1092 * of the current paragraph.
1094 * @see #getActions()
1096 public static final String beginParagraphAction = "caret-begin-paragraph";
1099 * The name of the <code>Action</code> that moves the caret to the beginning
1100 * of the current word.
1102 * @see #getActions()
1104 public static final String beginWordAction = "caret-begin-word";
1107 * The name of the <code>Action</code> that copies the selected content
1108 * into the system clipboard.
1110 * @see #getActions()
1112 public static final String copyAction = "copy-to-clipboard";
1115 * The name of the <code>Action</code> that copies the selected content
1116 * into the system clipboard and removes the selection.
1118 * @see #getActions()
1120 public static final String cutAction = "cut-to-clipboard";
1123 * The name of the <code>Action</code> that is performed by default if
1124 * a key is typed and there is no keymap entry.
1126 * @see #getActions()
1128 public static final String defaultKeyTypedAction = "default-typed";
1131 * The name of the <code>Action</code> that deletes the character that
1132 * follows the current caret position.
1134 * @see #getActions()
1136 public static final String deleteNextCharAction = "delete-next";
1139 * The name of the <code>Action</code> that deletes the character that
1140 * precedes the current caret position.
1142 * @see #getActions()
1144 public static final String deletePrevCharAction = "delete-previous";
1147 * The name of the <code>Action</code> that moves the caret one line down.
1149 * @see #getActions()
1151 public static final String downAction = "caret-down";
1154 * The name of the <code>Action</code> that moves the caret to the end
1155 * of the <code>Document</code>.
1157 * @see #getActions()
1159 public static final String endAction = "caret-end";
1162 * The name of the <code>Action</code> that moves the caret to the end
1163 * of the current line.
1165 * @see #getActions()
1167 public static final String endLineAction = "caret-end-line";
1170 * When a document is read and an CRLF is encountered, then we add a property
1171 * with this name and a value of "\r\n".
1173 public static final String EndOfLineStringProperty = "__EndOfLine__";
1176 * The name of the <code>Action</code> that moves the caret to the end
1177 * of the current paragraph.
1179 * @see #getActions()
1181 public static final String endParagraphAction = "caret-end-paragraph";
1184 * The name of the <code>Action</code> that moves the caret to the end
1185 * of the current word.
1187 * @see #getActions()
1189 public static final String endWordAction = "caret-end-word";
1192 * The name of the <code>Action</code> that moves the caret one character
1195 * @see #getActions()
1197 public static final String forwardAction = "caret-forward";
1200 * The name of the <code>Action</code> that inserts a line break.
1202 * @see #getActions()
1204 public static final String insertBreakAction = "insert-break";
1207 * The name of the <code>Action</code> that inserts some content.
1209 * @see #getActions()
1211 public static final String insertContentAction = "insert-content";
1214 * The name of the <code>Action</code> that inserts a TAB.
1216 * @see #getActions()
1218 public static final String insertTabAction = "insert-tab";
1221 * The name of the <code>Action</code> that moves the caret to the beginning
1224 * @see #getActions()
1226 public static final String nextWordAction = "caret-next-word";
1229 * The name of the <code>Action</code> that moves the caret one page down.
1231 * @see #getActions()
1233 public static final String pageDownAction = "page-down";
1236 * The name of the <code>Action</code> that moves the caret one page up.
1238 * @see #getActions()
1240 public static final String pageUpAction = "page-up";
1243 * The name of the <code>Action</code> that copies content from the system
1244 * clipboard into the document.
1246 * @see #getActions()
1248 public static final String pasteAction = "paste-from-clipboard";
1251 * The name of the <code>Action</code> that moves the caret to the beginning
1252 * of the previous word.
1254 * @see #getActions()
1256 public static final String previousWordAction = "caret-previous-word";
1259 * The name of the <code>Action</code> that sets the editor in read only
1262 * @see #getActions()
1264 public static final String readOnlyAction = "set-read-only";
1267 * The name of the <code>Action</code> that selects the whole document.
1269 * @see #getActions()
1271 public static final String selectAllAction = "select-all";
1274 * The name of the <code>Action</code> that moves the caret one character
1275 * backwards, possibly extending the current selection.
1277 * @see #getActions()
1279 public static final String selectionBackwardAction = "selection-backward";
1282 * The name of the <code>Action</code> that moves the caret to the beginning
1283 * of the document, possibly extending the current selection.
1285 * @see #getActions()
1287 public static final String selectionBeginAction = "selection-begin";
1290 * The name of the <code>Action</code> that moves the caret to the beginning
1291 * of the current line, possibly extending the current selection.
1293 * @see #getActions()
1295 public static final String selectionBeginLineAction = "selection-begin-line";
1298 * The name of the <code>Action</code> that moves the caret to the beginning
1299 * of the current paragraph, possibly extending the current selection.
1301 * @see #getActions()
1303 public static final String selectionBeginParagraphAction =
1304 "selection-begin-paragraph";
1307 * The name of the <code>Action</code> that moves the caret to the beginning
1308 * of the current word, possibly extending the current selection.
1310 * @see #getActions()
1312 public static final String selectionBeginWordAction = "selection-begin-word";
1315 * The name of the <code>Action</code> that moves the caret one line down,
1316 * possibly extending the current selection.
1318 * @see #getActions()
1320 public static final String selectionDownAction = "selection-down";
1323 * The name of the <code>Action</code> that moves the caret to the end
1324 * of the document, possibly extending the current selection.
1326 * @see #getActions()
1328 public static final String selectionEndAction = "selection-end";
1331 * The name of the <code>Action</code> that moves the caret to the end
1332 * of the current line, possibly extending the current selection.
1334 * @see #getActions()
1336 public static final String selectionEndLineAction = "selection-end-line";
1339 * The name of the <code>Action</code> that moves the caret to the end
1340 * of the current paragraph, possibly extending the current selection.
1342 * @see #getActions()
1344 public static final String selectionEndParagraphAction =
1345 "selection-end-paragraph";
1348 * The name of the <code>Action</code> that moves the caret to the end
1349 * of the current word, possibly extending the current selection.
1351 * @see #getActions()
1353 public static final String selectionEndWordAction = "selection-end-word";
1356 * The name of the <code>Action</code> that moves the caret one character
1357 * forwards, possibly extending the current selection.
1359 * @see #getActions()
1361 public static final String selectionForwardAction = "selection-forward";
1364 * The name of the <code>Action</code> that moves the caret to the beginning
1365 * of the next word, possibly extending the current selection.
1367 * @see #getActions()
1369 public static final String selectionNextWordAction = "selection-next-word";
1372 * The name of the <code>Action</code> that moves the caret to the beginning
1373 * of the previous word, possibly extending the current selection.
1375 * @see #getActions()
1377 public static final String selectionPreviousWordAction =
1378 "selection-previous-word";
1381 * The name of the <code>Action</code> that moves the caret one line up,
1382 * possibly extending the current selection.
1384 * @see #getActions()
1386 public static final String selectionUpAction = "selection-up";
1389 * The name of the <code>Action</code> that selects the line around the
1392 * @see #getActions()
1394 public static final String selectLineAction = "select-line";
1397 * The name of the <code>Action</code> that selects the paragraph around the
1400 * @see #getActions()
1402 public static final String selectParagraphAction = "select-paragraph";
1405 * The name of the <code>Action</code> that selects the word around the
1408 * @see #getActions()
1410 public static final String selectWordAction = "select-word";
1413 * The name of the <code>Action</code> that moves the caret one line up.
1415 * @see #getActions()
1417 public static final String upAction = "caret-up";
1420 * The name of the <code>Action</code> that sets the editor in read-write
1423 * @see #getActions()
1425 public static final String writableAction = "set-writable";
1428 * Creates a new <code>DefaultEditorKit</code>.
1430 public DefaultEditorKit()
1432 // Nothing to do here.
1436 * The <code>Action</code>s that are supported by the
1437 * <code>DefaultEditorKit</code>.
1439 private static Action[] defaultActions =
1441 // These classes are public because they are so in the RI.
1445 new DefaultKeyTypedAction(),
1446 new InsertBreakAction(),
1447 new InsertContentAction(),
1448 new InsertTabAction(),
1451 // These are (package-)private inner classes.
1452 new DeleteNextCharAction(),
1453 new DeletePrevCharAction(),
1455 new BeginLineAction(),
1456 new SelectionBeginLineAction(),
1458 new EndLineAction(),
1459 new SelectionEndLineAction(),
1461 new BackwardAction(),
1462 new SelectionBackwardAction(),
1464 new ForwardAction(),
1465 new SelectionForwardAction(),
1468 new SelectionUpAction(),
1471 new SelectionDownAction(),
1473 new NextWordAction(),
1474 new SelectionNextWordAction(),
1476 new PreviousWordAction(),
1477 new SelectionPreviousWordAction(),
1480 new SelectionBeginAction(),
1483 new SelectionEndAction(),
1485 new BeginWordAction(),
1486 new SelectionBeginWordAction(),
1488 new EndWordAction(),
1489 new SelectionEndWordAction(),
1491 new SelectAllAction(),
1492 new SelectLineAction(),
1493 new SelectWordAction()
1497 * Creates the <code>Caret</code> for this <code>EditorKit</code>. This
1498 * returns a {@link DefaultCaret} in this case.
1500 * @return the <code>Caret</code> for this <code>EditorKit</code>
1502 public Caret createCaret()
1504 return new DefaultCaret();
1508 * Creates the default {@link Document} that this <code>EditorKit</code>
1509 * supports. This is a {@link PlainDocument} in this case.
1511 * @return the default {@link Document} that this <code>EditorKit</code>
1514 public Document createDefaultDocument()
1516 return new PlainDocument();
1520 * Returns the <code>Action</code>s supported by this <code>EditorKit</code>.
1522 * @return the <code>Action</code>s supported by this <code>EditorKit</code>
1524 public Action[] getActions()
1526 return defaultActions;
1530 * Returns the content type that this <code>EditorKit</code> supports.
1531 * The <code>DefaultEditorKit</code> supports the content type
1532 * <code>text/plain</code>.
1534 * @return the content type that this <code>EditorKit</code> supports
1536 public String getContentType()
1538 return "text/plain";
1542 * Returns a {@link ViewFactory} that is able to create {@link View}s for
1543 * the <code>Element</code>s that are used in this <code>EditorKit</code>'s
1544 * model. This returns null which lets the UI of the text component supply
1545 * <code>View</code>s.
1547 * @return a {@link ViewFactory} that is able to create {@link View}s for
1548 * the <code>Element</code>s that are used in this
1549 * <code>EditorKit</code>'s model
1551 public ViewFactory getViewFactory()
1557 * Reads a document of the supported content type from an {@link InputStream}
1558 * into the actual {@link Document} object.
1560 * @param in the stream from which to read the document
1561 * @param document the document model into which the content is read
1562 * @param offset the offset inside to document where the content is inserted
1564 * @throws BadLocationException if <code>offset</code> is an invalid location
1565 * inside <code>document</code>
1566 * @throws IOException if something goes wrong while reading from
1569 public void read(InputStream in, Document document, int offset)
1570 throws BadLocationException, IOException
1572 read(new InputStreamReader(in), document, offset);
1576 * Reads a document of the supported content type from a {@link Reader}
1577 * into the actual {@link Document} object.
1579 * @param in the reader from which to read the document
1580 * @param document the document model into which the content is read
1581 * @param offset the offset inside to document where the content is inserted
1583 * @throws BadLocationException if <code>offset</code> is an invalid location
1584 * inside <code>document</code>
1585 * @throws IOException if something goes wrong while reading from
1588 public void read(Reader in, Document document, int offset)
1589 throws BadLocationException, IOException
1591 BufferedReader reader = new BufferedReader(in);
1594 StringBuffer content = new StringBuffer();
1596 while ((line = reader.readLine()) != null)
1598 content.append(line);
1599 content.append("\n");
1602 document.insertString(offset, content.substring(0, content.length() - 1),
1603 SimpleAttributeSet.EMPTY);
1607 * Writes the <code>Document</code> (or a fragment of the
1608 * <code>Document</code>) to an {@link OutputStream} in the
1609 * supported content type format.
1611 * @param out the stream to write to
1612 * @param document the document that should be written out
1613 * @param offset the beginning offset from where to write
1614 * @param len the length of the fragment to write
1616 * @throws BadLocationException if <code>offset</code> or
1617 * <code>offset + len</code>is an invalid location inside
1618 * <code>document</code>
1619 * @throws IOException if something goes wrong while writing to
1622 public void write(OutputStream out, Document document, int offset, int len)
1623 throws BadLocationException, IOException
1625 write(new OutputStreamWriter(out), document, offset, len);
1629 * Writes the <code>Document</code> (or a fragment of the
1630 * <code>Document</code>) to a {@link Writer} in the
1631 * supported content type format.
1633 * @param out the writer to write to
1634 * @param document the document that should be written out
1635 * @param offset the beginning offset from where to write
1636 * @param len the length of the fragment to write
1638 * @throws BadLocationException if <code>offset</code> is an
1639 * invalid location inside <code>document</code>.
1640 * @throws IOException if something goes wrong while writing to
1643 public void write(Writer out, Document document, int offset, int len)
1644 throws BadLocationException, IOException
1646 // Throw a BLE if offset is invalid
1647 if (offset < 0 || offset > document.getLength())
1648 throw new BadLocationException("Tried to write to invalid location",
1651 // If they gave an overly large len, just adjust it
1652 if (offset + len > document.getLength())
1653 len = document.getLength() - offset;
1655 out.write(document.getText(offset, len));