Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / text / DefaultEditorKit.java
blob1b686182b6a151553a77cac2047756e84dc3f665
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
56 /**
57 * The default implementation of {@link EditorKit}. This <code>EditorKit</code>
58 * a plain text <code>Document</code> and several commands that together
59 * make up a basic editor, like cut / copy + paste.
61 * @author original author unknown
62 * @author Roman Kennke (roman@kennke.org)
64 public class DefaultEditorKit extends EditorKit
66 static class SelectionPreviousWordAction
67 extends TextAction
69 SelectionPreviousWordAction()
71 super(selectionPreviousWordAction);
74 public void actionPerformed(ActionEvent event)
76 try
78 JTextComponent t = getTextComponent(event);
80 if (t != null)
82 int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
84 Caret c = t.getCaret();
85 c.moveDot(offs);
86 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
89 catch(BadLocationException ble)
91 // Can't happen.
96 static class SelectionNextWordAction
97 extends TextAction
99 SelectionNextWordAction()
101 super(selectionNextWordAction);
104 public void actionPerformed(ActionEvent event)
108 JTextComponent t = getTextComponent(event);
110 if (t != null)
112 int offs = Utilities.getNextWord(t, t.getCaretPosition());
114 Caret c = t.getCaret();
115 c.moveDot(offs);
116 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
119 catch(BadLocationException ble)
121 // Can't happen.
126 static class PreviousWordAction
127 extends TextAction
129 PreviousWordAction()
131 super(previousWordAction);
134 public void actionPerformed(ActionEvent event)
138 JTextComponent t = getTextComponent(event);
140 if (t != null)
142 int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
144 Caret c = t.getCaret();
145 c.setDot(offs);
146 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
149 catch(BadLocationException ble)
151 // Can't happen.
156 static class NextWordAction
157 extends TextAction
159 NextWordAction()
161 super(nextWordAction);
164 public void actionPerformed(ActionEvent event)
168 JTextComponent t = getTextComponent(event);
170 if (t != null)
172 int offs = Utilities.getNextWord(t, t.getCaretPosition());
174 Caret c = t.getCaret();
175 c.setDot(offs);
176 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
179 catch(BadLocationException ble)
181 // Can't happen.
186 static class SelectAllAction
187 extends TextAction
189 SelectAllAction()
191 super(selectAllAction);
194 public void actionPerformed(ActionEvent event)
196 JTextComponent t = getTextComponent(event);
197 int offs = t.getDocument().getLength();
198 Caret c = t.getCaret();
199 c.setDot(0);
200 c.moveDot(offs);
204 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
206 catch(BadLocationException ble)
208 // Can't happen.
213 static class SelectionBeginAction
214 extends TextAction
216 SelectionBeginAction()
218 super(selectionBeginAction);
221 public void actionPerformed(ActionEvent event)
223 JTextComponent t = getTextComponent(event);
224 Caret c = t.getCaret();
225 c.moveDot(0);
228 c.setMagicCaretPosition(t.modelToView(0).getLocation());
230 catch(BadLocationException ble)
232 // Can't happen.
237 static class SelectionEndAction
238 extends TextAction
240 SelectionEndAction()
242 super(selectionEndAction);
245 public void actionPerformed(ActionEvent event)
247 JTextComponent t = getTextComponent(event);
248 int offs = t.getDocument().getLength();
249 Caret c = t.getCaret();
250 c.moveDot(offs);
253 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
255 catch(BadLocationException ble)
257 // Can't happen.
262 static class SelectionEndLineAction
263 extends TextAction
265 SelectionEndLineAction()
267 super(selectionEndLineAction);
270 public void actionPerformed(ActionEvent event)
272 JTextComponent t = getTextComponent(event);
275 Point p = t.modelToView(t.getCaret().getDot()).getLocation();
276 int cur = t.getCaretPosition();
277 int y = p.y;
278 int length = t.getDocument().getLength();
279 while (y == p.y && cur < length)
280 y = t.modelToView(++cur).getLocation().y;
281 if (cur != length)
282 cur--;
284 Caret c = t.getCaret();
285 c.moveDot(cur);
286 c.setMagicCaretPosition(t.modelToView(cur).getLocation());
288 catch (BadLocationException ble)
290 // Nothing to do here
295 static class SelectionBeginLineAction
296 extends TextAction
298 SelectionBeginLineAction()
300 super(selectionBeginLineAction);
303 public void actionPerformed(ActionEvent event)
305 JTextComponent t = getTextComponent(event);
309 // TODO: There is a more efficent solution, but
310 // viewToModel doesn't work properly.
311 Point p = t.modelToView(t.getCaret().getDot()).getLocation();
313 int cur = t.getCaretPosition();
314 int y = p.y;
316 while (y == p.y && cur > 0)
317 y = t.modelToView(--cur).getLocation().y;
318 if (cur != 0)
319 cur++;
321 Caret c = t.getCaret();
322 c.moveDot(cur);
323 c.setMagicCaretPosition(t.modelToView(cur).getLocation());
325 catch (BadLocationException ble)
327 // Do nothing here.
332 static class SelectionDownAction
333 extends TextAction
335 SelectionDownAction()
337 super(selectionDownAction);
340 public void actionPerformed(ActionEvent event)
342 JTextComponent t = getTextComponent(event);
345 if (t != null)
347 Caret c = t.getCaret();
348 // The magic caret position may be null when the caret
349 // has not moved yet.
350 Point mcp = c.getMagicCaretPosition();
351 int x = (mcp != null) ? mcp.x : 0;
352 int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x);
354 if (pos > -1)
355 t.moveCaretPosition(pos);
358 catch(BadLocationException ble)
360 // FIXME: Swallowing allowed?
365 static class SelectionUpAction
366 extends TextAction
368 SelectionUpAction()
370 super(selectionUpAction);
373 public void actionPerformed(ActionEvent event)
375 JTextComponent t = getTextComponent(event);
378 if (t != null)
380 Caret c = t.getCaret();
381 // The magic caret position may be null when the caret
382 // has not moved yet.
383 Point mcp = c.getMagicCaretPosition();
384 int x = (mcp != null) ? mcp.x : 0;
385 int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x);
387 if (pos > -1)
388 t.moveCaretPosition(pos);
391 catch(BadLocationException ble)
393 // FIXME: Swallowing allowed?
398 static class SelectionForwardAction
399 extends TextAction
401 SelectionForwardAction()
403 super(selectionForwardAction);
406 public void actionPerformed(ActionEvent event)
408 JTextComponent t = getTextComponent(event);
409 if (t != null)
411 int offs = t.getCaretPosition() + 1;
413 if(offs <= t.getDocument().getLength())
415 Caret c = t.getCaret();
416 c.moveDot(offs);
419 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
421 catch(BadLocationException ble)
423 // Can't happen.
430 static class SelectionBackwardAction
431 extends TextAction
433 SelectionBackwardAction()
435 super(selectionBackwardAction);
438 public void actionPerformed(ActionEvent event)
440 JTextComponent t = getTextComponent(event);
441 if (t != null)
443 int offs = t.getCaretPosition() - 1;
445 if(offs >= 0)
447 Caret c = t.getCaret();
448 c.moveDot(offs);
451 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
453 catch(BadLocationException ble)
455 // Can't happen.
462 static class DownAction
463 extends TextAction
465 DownAction()
467 super(downAction);
470 public void actionPerformed(ActionEvent event)
472 JTextComponent t = getTextComponent(event);
475 if (t != null)
477 Caret c = t.getCaret();
478 // The magic caret position may be null when the caret
479 // has not moved yet.
480 Point mcp = c.getMagicCaretPosition();
481 int x = (mcp != null) ? mcp.x : 0;
482 int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x);
484 if (pos > -1)
485 t.setCaretPosition(pos);
488 catch(BadLocationException ble)
490 // FIXME: Swallowing allowed?
495 static class UpAction
496 extends TextAction
498 UpAction()
500 super(upAction);
503 public void actionPerformed(ActionEvent event)
505 JTextComponent t = getTextComponent(event);
508 if (t != null)
510 Caret c = t.getCaret();
511 // The magic caret position may be null when the caret
512 // has not moved yet.
513 Point mcp = c.getMagicCaretPosition();
514 int x = (mcp != null) ? mcp.x : 0;
515 int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x);
517 if (pos > -1)
518 t.setCaretPosition(pos);
521 catch(BadLocationException ble)
523 // FIXME: Swallowing allowed?
528 static class ForwardAction
529 extends TextAction
531 ForwardAction()
533 super(forwardAction);
536 public void actionPerformed(ActionEvent event)
538 JTextComponent t = getTextComponent(event);
539 if (t != null)
541 int offs = t.getCaretPosition() + 1;
542 if (offs <= t.getDocument().getLength())
544 Caret c = t.getCaret();
545 c.setDot(offs);
549 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
551 catch (BadLocationException ble)
553 // Should not happen.
561 static class BackwardAction
562 extends TextAction
564 BackwardAction()
566 super(backwardAction);
569 public void actionPerformed(ActionEvent event)
571 JTextComponent t = getTextComponent(event);
572 if (t != null)
574 int offs = t.getCaretPosition() - 1;
575 if (offs >= 0)
577 Caret c = t.getCaret();
578 c.setDot(offs);
582 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
584 catch (BadLocationException ble)
586 // Should not happen.
593 static class DeletePrevCharAction
594 extends TextAction
596 DeletePrevCharAction()
598 super(deletePrevCharAction);
601 public void actionPerformed(ActionEvent event)
603 JTextComponent t = getTextComponent(event);
604 if (t != null)
608 int pos = t.getSelectionStart();
609 int len = t.getSelectionEnd() - pos;
611 if (len > 0)
612 t.getDocument().remove(pos, len);
613 else if (pos > 0)
615 pos--;
616 t.getDocument().remove(pos, 1);
617 Caret c = t.getCaret();
618 c.setDot(pos);
619 c.setMagicCaretPosition(t.modelToView(pos).getLocation());
622 catch (BadLocationException e)
624 // FIXME: we're not authorized to throw this.. swallow it?
630 static class DeleteNextCharAction
631 extends TextAction
633 DeleteNextCharAction()
635 super(deleteNextCharAction);
638 public void actionPerformed(ActionEvent event)
640 JTextComponent t = getTextComponent(event);
641 if (t != null)
645 int pos = t.getSelectionStart();
646 int len = t.getSelectionEnd() - pos;
648 if (len > 0)
649 t.getDocument().remove(pos, len);
650 else if (pos < t.getDocument().getLength())
651 t.getDocument().remove(pos, 1);
653 Caret c = t.getCaret();
654 c.setDot(pos);
655 c.setMagicCaretPosition(t.modelToView(pos).getLocation());
657 catch (BadLocationException e)
659 // FIXME: we're not authorized to throw this.. swallow it?
665 static class EndLineAction
666 extends TextAction
668 EndLineAction()
670 super(endLineAction);
673 public void actionPerformed(ActionEvent event)
675 JTextComponent t = getTextComponent(event);
678 int offs = Utilities.getRowEnd(t, t.getCaretPosition());
680 if (offs > -1)
682 Caret c = t.getCaret();
683 c.setDot(offs);
684 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
687 catch (BadLocationException ble)
689 // Nothing to do here
694 static class BeginLineAction
695 extends TextAction
697 BeginLineAction()
699 super(beginLineAction);
702 public void actionPerformed(ActionEvent event)
704 JTextComponent t = getTextComponent(event);
707 int offs = Utilities.getRowStart(t, t.getCaretPosition());
709 if (offs > -1)
711 Caret c = t.getCaret();
712 c.setDot(offs);
713 c.setMagicCaretPosition(t.modelToView(offs).getLocation());
716 catch (BadLocationException ble)
718 // Do nothing here.
724 * Creates a beep on the PC speaker.
726 * @see Toolkit#beep()
728 public static class BeepAction extends TextAction
731 * Creates a new <code>BeepAction</code>.
733 public BeepAction()
735 super(beepAction);
739 * Performs the <code>Action</code>.
741 * @param event the action event describing the user action
743 public void actionPerformed(ActionEvent event)
745 Toolkit.getDefaultToolkit().beep();
750 * Copies the selected content into the system clipboard.
752 * @see Toolkit#getSystemClipboard()
753 * @see CutAction
754 * @see PasteAction
756 public static class CopyAction extends TextAction
760 * Create a new <code>CopyAction</code>.
762 public CopyAction()
764 super(copyAction);
768 * Performs the <code>Action</code>.
770 * @param event the action event describing the user action
772 public void actionPerformed(ActionEvent event)
774 getTextComponent(event).copy();
780 * Copies the selected content into the system clipboard and deletes the
781 * selection.
783 * @see Toolkit#getSystemClipboard()
784 * @see CopyAction
785 * @see PasteAction
787 public static class CutAction extends TextAction
791 * Create a new <code>CutAction</code>.
793 public CutAction()
795 super(cutAction);
799 * Performs the <code>Action</code>.
801 * @param event the action event describing the user action
803 public void actionPerformed(ActionEvent event)
805 getTextComponent(event).cut();
810 * Copies content from the system clipboard into the editor.
812 * @see Toolkit#getSystemClipboard()
813 * @see CopyAction
814 * @see CutAction
816 public static class PasteAction extends TextAction
820 * Create a new <code>PasteAction</code>.
822 public PasteAction()
824 super(pasteAction);
828 * Performs the <code>Action</code>.
830 * @param event the action event describing the user action
832 public void actionPerformed(ActionEvent event)
834 getTextComponent(event).paste();
839 * This action is executed as default action when a KEY_TYPED
840 * event is received and no keymap entry exists for that. The purpose
841 * of this action is to filter out a couple of characters. This includes
842 * the control characters and characters with the ALT-modifier.
844 * If an event does not get filtered, it is inserted into the document
845 * of the text component. If there is some text selected in the text
846 * component, this text will be replaced.
848 public static class DefaultKeyTypedAction
849 extends TextAction
853 * Creates a new <code>DefaultKeyTypedAction</code>.
855 public DefaultKeyTypedAction()
857 super(defaultKeyTypedAction);
861 * Performs the <code>Action</code>.
863 * @param event the action event describing the user action
865 public void actionPerformed(ActionEvent event)
867 // first we filter the following events:
868 // - control characters
869 // - key events with the ALT modifier (FIXME: filter that too!)
870 char c = event.getActionCommand().charAt(0);
871 if (Character.isISOControl(c))
872 return;
874 JTextComponent t = getTextComponent(event);
875 if (t != null && t.isEnabled() && t.isEditable())
876 t.replaceSelection(event.getActionCommand());
881 * This action inserts a newline character into the document
882 * of the text component. This is typically triggered by hitting
883 * ENTER on the keyboard.
885 public static class InsertBreakAction extends TextAction
889 * Creates a new <code>InsertBreakAction</code>.
891 public InsertBreakAction()
893 super(insertBreakAction);
897 * Performs the <code>Action</code>.
899 * @param event the action event describing the user action
901 public void actionPerformed(ActionEvent event)
903 JTextComponent t = getTextComponent(event);
904 t.replaceSelection("\n");
909 * Places content into the associated editor. If there currently is a
910 * selection, this selection is replaced.
912 // FIXME: Figure out what this Action is supposed to do. Obviously text
913 // that is entered by the user is inserted through DefaultKeyTypedAction.
914 public static class InsertContentAction extends TextAction
918 * Creates a new <code>InsertContentAction</code>.
920 public InsertContentAction()
922 super(insertContentAction);
926 * Performs the <code>Action</code>.
928 * @param event the action event describing the user action
930 public void actionPerformed(ActionEvent event)
932 // FIXME: Figure out what this Action is supposed to do. Obviously text
933 // that is entered by the user is inserted through DefaultKeyTypedAction.
938 * Inserts a TAB character into the text editor.
940 public static class InsertTabAction extends TextAction
944 * Creates a new <code>TabAction</code>.
946 public InsertTabAction()
948 super(insertTabAction);
952 * Performs the <code>Action</code>.
954 * @param event the action event describing the user action
956 public void actionPerformed(ActionEvent event)
958 JTextComponent t = getTextComponent(event);
959 t.replaceSelection("\t");
964 * The serial version of DefaultEditorKit.
966 private static final long serialVersionUID = 9017245433028523428L;
969 * The name of the <code>Action</code> that moves the caret one character
970 * backwards.
972 * @see #getActions()
974 public static final String backwardAction = "caret-backward";
977 * The name of the <code>Action</code> that creates a beep in the speaker.
979 * @see #getActions()
981 public static final String beepAction = "beep";
984 * The name of the <code>Action</code> that moves the caret to the beginning
985 * of the <code>Document</code>.
987 * @see #getActions()
989 public static final String beginAction = "caret-begin";
992 * The name of the <code>Action</code> that moves the caret to the beginning
993 * of the current line.
995 * @see #getActions()
997 public static final String beginLineAction = "caret-begin-line";
1000 * The name of the <code>Action</code> that moves the caret to the beginning
1001 * of the current paragraph.
1003 * @see #getActions()
1005 public static final String beginParagraphAction = "caret-begin-paragraph";
1008 * The name of the <code>Action</code> that moves the caret to the beginning
1009 * of the current word.
1011 * @see #getActions()
1013 public static final String beginWordAction = "caret-begin-word";
1016 * The name of the <code>Action</code> that copies the selected content
1017 * into the system clipboard.
1019 * @see #getActions()
1021 public static final String copyAction = "copy-to-clipboard";
1024 * The name of the <code>Action</code> that copies the selected content
1025 * into the system clipboard and removes the selection.
1027 * @see #getActions()
1029 public static final String cutAction = "cut-to-clipboard";
1032 * The name of the <code>Action</code> that is performed by default if
1033 * a key is typed and there is no keymap entry.
1035 * @see #getActions()
1037 public static final String defaultKeyTypedAction = "default-typed";
1040 * The name of the <code>Action</code> that deletes the character that
1041 * follows the current caret position.
1043 * @see #getActions()
1045 public static final String deleteNextCharAction = "delete-next";
1048 * The name of the <code>Action</code> that deletes the character that
1049 * precedes the current caret position.
1051 * @see #getActions()
1053 public static final String deletePrevCharAction = "delete-previous";
1056 * The name of the <code>Action</code> that moves the caret one line down.
1058 * @see #getActions()
1060 public static final String downAction = "caret-down";
1063 * The name of the <code>Action</code> that moves the caret to the end
1064 * of the <code>Document</code>.
1066 * @see #getActions()
1068 public static final String endAction = "caret-end";
1071 * The name of the <code>Action</code> that moves the caret to the end
1072 * of the current line.
1074 * @see #getActions()
1076 public static final String endLineAction = "caret-end-line";
1079 * When a document is read and an CRLF is encountered, then we add a property
1080 * with this name and a value of &quot;\r\n&quot;.
1082 public static final String EndOfLineStringProperty = "__EndOfLine__";
1085 * The name of the <code>Action</code> that moves the caret to the end
1086 * of the current paragraph.
1088 * @see #getActions()
1090 public static final String endParagraphAction = "caret-end-paragraph";
1093 * The name of the <code>Action</code> that moves the caret to the end
1094 * of the current word.
1096 * @see #getActions()
1098 public static final String endWordAction = "caret-end-word";
1101 * The name of the <code>Action</code> that moves the caret one character
1102 * forward.
1104 * @see #getActions()
1106 public static final String forwardAction = "caret-forward";
1109 * The name of the <code>Action</code> that inserts a line break.
1111 * @see #getActions()
1113 public static final String insertBreakAction = "insert-break";
1116 * The name of the <code>Action</code> that inserts some content.
1118 * @see #getActions()
1120 public static final String insertContentAction = "insert-content";
1123 * The name of the <code>Action</code> that inserts a TAB.
1125 * @see #getActions()
1127 public static final String insertTabAction = "insert-tab";
1130 * The name of the <code>Action</code> that moves the caret to the beginning
1131 * of the next word.
1133 * @see #getActions()
1135 public static final String nextWordAction = "caret-next-word";
1138 * The name of the <code>Action</code> that moves the caret one page down.
1140 * @see #getActions()
1142 public static final String pageDownAction = "page-down";
1145 * The name of the <code>Action</code> that moves the caret one page up.
1147 * @see #getActions()
1149 public static final String pageUpAction = "page-up";
1152 * The name of the <code>Action</code> that copies content from the system
1153 * clipboard into the document.
1155 * @see #getActions()
1157 public static final String pasteAction = "paste-from-clipboard";
1160 * The name of the <code>Action</code> that moves the caret to the beginning
1161 * of the previous word.
1163 * @see #getActions()
1165 public static final String previousWordAction = "caret-previous-word";
1168 * The name of the <code>Action</code> that sets the editor in read only
1169 * mode.
1171 * @see #getActions()
1173 public static final String readOnlyAction = "set-read-only";
1176 * The name of the <code>Action</code> that selects the whole document.
1178 * @see #getActions()
1180 public static final String selectAllAction = "select-all";
1183 * The name of the <code>Action</code> that moves the caret one character
1184 * backwards, possibly extending the current selection.
1186 * @see #getActions()
1188 public static final String selectionBackwardAction = "selection-backward";
1191 * The name of the <code>Action</code> that moves the caret to the beginning
1192 * of the document, possibly extending the current selection.
1194 * @see #getActions()
1196 public static final String selectionBeginAction = "selection-begin";
1199 * The name of the <code>Action</code> that moves the caret to the beginning
1200 * of the current line, possibly extending the current selection.
1202 * @see #getActions()
1204 public static final String selectionBeginLineAction = "selection-begin-line";
1207 * The name of the <code>Action</code> that moves the caret to the beginning
1208 * of the current paragraph, possibly extending the current selection.
1210 * @see #getActions()
1212 public static final String selectionBeginParagraphAction =
1213 "selection-begin-paragraph";
1216 * The name of the <code>Action</code> that moves the caret to the beginning
1217 * of the current word, possibly extending the current selection.
1219 * @see #getActions()
1221 public static final String selectionBeginWordAction = "selection-begin-word";
1224 * The name of the <code>Action</code> that moves the caret one line down,
1225 * possibly extending the current selection.
1227 * @see #getActions()
1229 public static final String selectionDownAction = "selection-down";
1232 * The name of the <code>Action</code> that moves the caret to the end
1233 * of the document, possibly extending the current selection.
1235 * @see #getActions()
1237 public static final String selectionEndAction = "selection-end";
1240 * The name of the <code>Action</code> that moves the caret to the end
1241 * of the current line, possibly extending the current selection.
1243 * @see #getActions()
1245 public static final String selectionEndLineAction = "selection-end-line";
1248 * The name of the <code>Action</code> that moves the caret to the end
1249 * of the current paragraph, possibly extending the current selection.
1251 * @see #getActions()
1253 public static final String selectionEndParagraphAction =
1254 "selection-end-paragraph";
1257 * The name of the <code>Action</code> that moves the caret to the end
1258 * of the current word, possibly extending the current selection.
1260 * @see #getActions()
1262 public static final String selectionEndWordAction = "selection-end-word";
1265 * The name of the <code>Action</code> that moves the caret one character
1266 * forwards, possibly extending the current selection.
1268 * @see #getActions()
1270 public static final String selectionForwardAction = "selection-forward";
1273 * The name of the <code>Action</code> that moves the caret to the beginning
1274 * of the next word, possibly extending the current selection.
1276 * @see #getActions()
1278 public static final String selectionNextWordAction = "selection-next-word";
1281 * The name of the <code>Action</code> that moves the caret to the beginning
1282 * of the previous word, possibly extending the current selection.
1284 * @see #getActions()
1286 public static final String selectionPreviousWordAction =
1287 "selection-previous-word";
1290 * The name of the <code>Action</code> that moves the caret one line up,
1291 * possibly extending the current selection.
1293 * @see #getActions()
1295 public static final String selectionUpAction = "selection-up";
1298 * The name of the <code>Action</code> that selects the line around the
1299 * caret.
1301 * @see #getActions()
1303 public static final String selectLineAction = "select-line";
1306 * The name of the <code>Action</code> that selects the paragraph around the
1307 * caret.
1309 * @see #getActions()
1311 public static final String selectParagraphAction = "select-paragraph";
1314 * The name of the <code>Action</code> that selects the word around the
1315 * caret.
1317 * @see #getActions()
1319 public static final String selectWordAction = "select-word";
1322 * The name of the <code>Action</code> that moves the caret one line up.
1324 * @see #getActions()
1326 public static final String upAction = "caret-up";
1329 * The name of the <code>Action</code> that sets the editor in read-write
1330 * mode.
1332 * @see #getActions()
1334 public static final String writableAction = "set-writable";
1337 * Creates a new <code>DefaultEditorKit</code>.
1339 public DefaultEditorKit()
1341 // Nothing to do here.
1345 * The <code>Action</code>s that are supported by the
1346 * <code>DefaultEditorKit</code>.
1348 // TODO: All these inner classes look ugly. Maybe work out a better way
1349 // to handle this.
1350 private static Action[] defaultActions =
1351 new Action[] {
1352 // These classes are public because they are so in the RI.
1353 new BeepAction(),
1354 new CopyAction(),
1355 new CutAction(),
1356 new DefaultKeyTypedAction(),
1357 new InsertBreakAction(),
1358 new InsertContentAction(),
1359 new InsertTabAction(),
1360 new PasteAction(),
1362 // These are (package-)private inner classes.
1363 new DeleteNextCharAction(),
1364 new DeletePrevCharAction(),
1366 new BeginLineAction(),
1367 new SelectionBeginLineAction(),
1369 new EndLineAction(),
1370 new SelectionEndLineAction(),
1372 new BackwardAction(),
1373 new SelectionBackwardAction(),
1375 new ForwardAction(),
1376 new SelectionForwardAction(),
1378 new UpAction(),
1379 new SelectionUpAction(),
1381 new DownAction(),
1382 new SelectionDownAction(),
1384 new NextWordAction(),
1385 new SelectionNextWordAction(),
1387 new PreviousWordAction(),
1388 new SelectionPreviousWordAction(),
1390 new SelectionBeginAction(),
1391 new SelectionEndAction(),
1392 new SelectAllAction(),
1396 * Creates the <code>Caret</code> for this <code>EditorKit</code>. This
1397 * returns a {@link DefaultCaret} in this case.
1399 * @return the <code>Caret</code> for this <code>EditorKit</code>
1401 public Caret createCaret()
1403 return new DefaultCaret();
1407 * Creates the default {@link Document} that this <code>EditorKit</code>
1408 * supports. This is a {@link PlainDocument} in this case.
1410 * @return the default {@link Document} that this <code>EditorKit</code>
1411 * supports
1413 public Document createDefaultDocument()
1415 return new PlainDocument();
1419 * Returns the <code>Action</code>s supported by this <code>EditorKit</code>.
1421 * @return the <code>Action</code>s supported by this <code>EditorKit</code>
1423 public Action[] getActions()
1425 return defaultActions;
1429 * Returns the content type that this <code>EditorKit</code> supports.
1430 * The <code>DefaultEditorKit</code> supports the content type
1431 * <code>text/plain</code>.
1433 * @return the content type that this <code>EditorKit</code> supports
1435 public String getContentType()
1437 return "text/plain";
1441 * Returns a {@link ViewFactory} that is able to create {@link View}s for
1442 * the <code>Element</code>s that are used in this <code>EditorKit</code>'s
1443 * model. This returns null which lets the UI of the text component supply
1444 * <code>View</code>s.
1446 * @return a {@link ViewFactory} that is able to create {@link View}s for
1447 * the <code>Element</code>s that are used in this
1448 * <code>EditorKit</code>'s model
1450 public ViewFactory getViewFactory()
1452 return null;
1456 * Reads a document of the supported content type from an {@link InputStream}
1457 * into the actual {@link Document} object.
1459 * @param in the stream from which to read the document
1460 * @param document the document model into which the content is read
1461 * @param offset the offset inside to document where the content is inserted
1463 * @throws BadLocationException if <code>offset</code> is an invalid location
1464 * inside <code>document</code>
1465 * @throws IOException if something goes wrong while reading from
1466 * <code>in</code>
1468 public void read(InputStream in, Document document, int offset)
1469 throws BadLocationException, IOException
1471 read(new InputStreamReader(in), document, offset);
1475 * Reads a document of the supported content type from a {@link Reader}
1476 * into the actual {@link Document} object.
1478 * @param in the reader from which to read the document
1479 * @param document the document model into which the content is read
1480 * @param offset the offset inside to document where the content is inserted
1482 * @throws BadLocationException if <code>offset</code> is an invalid location
1483 * inside <code>document</code>
1484 * @throws IOException if something goes wrong while reading from
1485 * <code>in</code>
1487 public void read(Reader in, Document document, int offset)
1488 throws BadLocationException, IOException
1490 BufferedReader reader = new BufferedReader(in);
1492 String line;
1493 StringBuffer content = new StringBuffer();
1495 while ((line = reader.readLine()) != null)
1497 content.append(line);
1498 content.append("\n");
1501 document.insertString(offset, content.substring(0, content.length() - 1),
1502 SimpleAttributeSet.EMPTY);
1506 * Writes the <code>Document</code> (or a fragment of the
1507 * <code>Document</code>) to an {@link OutputStream} in the
1508 * supported content type format.
1510 * @param out the stream to write to
1511 * @param document the document that should be written out
1512 * @param offset the beginning offset from where to write
1513 * @param len the length of the fragment to write
1515 * @throws BadLocationException if <code>offset</code> or
1516 * <code>offset + len</code>is an invalid location inside
1517 * <code>document</code>
1518 * @throws IOException if something goes wrong while writing to
1519 * <code>out</code>
1521 public void write(OutputStream out, Document document, int offset, int len)
1522 throws BadLocationException, IOException
1524 write(new OutputStreamWriter(out), document, offset, len);
1528 * Writes the <code>Document</code> (or a fragment of the
1529 * <code>Document</code>) to a {@link Writer} in the
1530 * supported content type format.
1532 * @param out the writer to write to
1533 * @param document the document that should be written out
1534 * @param offset the beginning offset from where to write
1535 * @param len the length of the fragment to write
1537 * @throws BadLocationException if <code>offset</code> is an
1538 * invalid location inside <code>document</code>.
1539 * @throws IOException if something goes wrong while writing to
1540 * <code>out</code>
1542 public void write(Writer out, Document document, int offset, int len)
1543 throws BadLocationException, IOException
1545 // Throw a BLE if offset is invalid
1546 if (offset < 0 || offset > document.getLength())
1547 throw new BadLocationException("Tried to write to invalid location",
1548 offset);
1550 // If they gave an overly large len, just adjust it
1551 if (offset + len > document.getLength())
1552 len = document.getLength() - offset;
1554 out.write(document.getText(offset, len));