Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / text / DefaultEditorKit.java
blob3b3fc1f7238615cc5708dba7dad145c19fc9c3f8
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.Toolkit;
42 import java.awt.event.ActionEvent;
43 import java.io.BufferedReader;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
47 import java.io.OutputStream;
48 import java.io.OutputStreamWriter;
49 import java.io.Reader;
50 import java.io.Writer;
52 import javax.swing.Action;
54 /**
55 * The default implementation of {@link EditorKit}. This <code>EditorKit</code>
56 * a plain text <code>Document</code> and several commands that together
57 * make up a basic editor, like cut / copy + paste.
59 * @author original author unknown
60 * @author Roman Kennke (roman@kennke.org)
62 public class DefaultEditorKit extends EditorKit
64 /**
65 * Creates a beep on the PC speaker.
67 * @see Toolkit#beep()
69 public static class BeepAction extends TextAction
71 /**
72 * Creates a new <code>BeepAction</code>.
74 public BeepAction()
76 super(beepAction);
79 /**
80 * Performs the <code>Action</code>.
82 * @param event the action event describing the user action
84 public void actionPerformed(ActionEvent event)
86 Toolkit.getDefaultToolkit().beep();
90 /**
91 * Copies the selected content into the system clipboard.
93 * @see Toolkit#getSystemClipboard()
94 * @see CutAction
95 * @see PasteAction
97 public static class CopyAction extends TextAction
101 * Create a new <code>CopyAction</code>.
103 public CopyAction()
105 super(copyAction);
109 * Performs the <code>Action</code>.
111 * @param event the action event describing the user action
113 public void actionPerformed(ActionEvent event)
115 // FIXME: Implement me. Tookit.getSystemClipboard should be used
116 // for that.
122 * Copies the selected content into the system clipboard and deletes the
123 * selection.
125 * @see Toolkit#getSystemClipboard()
126 * @see CopyAction
127 * @see PasteAction
129 public static class CutAction extends TextAction
133 * Create a new <code>CutAction</code>.
135 public CutAction()
137 super(cutAction);
141 * Performs the <code>Action</code>.
143 * @param event the action event describing the user action
145 public void actionPerformed(ActionEvent event)
147 // FIXME: Implement me. Tookit.getSystemClipboard should be used
148 // for that.
153 * Copies content from the system clipboard into the editor.
155 * @see Toolkit#getSystemClipboard()
156 * @see CopyAction
157 * @see CutAction
159 public static class PasteAction extends TextAction
163 * Create a new <code>PasteAction</code>.
165 public PasteAction()
167 super(pasteAction);
171 * Performs the <code>Action</code>.
173 * @param event the action event describing the user action
175 public void actionPerformed(ActionEvent event)
177 // FIXME: Implement me. Tookit.getSystemClipboard should be used
178 // for that.
183 * This action is executed as default action when a KEY_TYPED
184 * event is received and no keymap entry exists for that. The purpose
185 * of this action is to filter out a couple of characters. This includes
186 * the control characters and characters with the ALT-modifier.
188 * If an event does not get filtered, it is inserted into the document
189 * of the text component. If there is some text selected in the text
190 * component, this text will be replaced.
192 public static class DefaultKeyTypedAction
193 extends TextAction
197 * Creates a new <code>DefaultKeyTypedAction</code>.
199 public DefaultKeyTypedAction()
201 super(defaultKeyTypedAction);
205 * Performs the <code>Action</code>.
207 * @param event the action event describing the user action
209 public void actionPerformed(ActionEvent event)
211 // first we filter the following events:
212 // - control characters
213 // - key events with the ALT modifier (FIXME: filter that too!)
214 char c = event.getActionCommand().charAt(0);
215 if (Character.isISOControl(c))
216 return;
218 JTextComponent t = getTextComponent(event);
219 if (t != null)
223 t.getDocument().insertString(t.getCaret().getDot(),
224 event.getActionCommand(), null);
226 catch (BadLocationException be)
228 // FIXME: we're not authorized to throw this.. swallow it?
235 * This action inserts a newline character into the document
236 * of the text component. This is typically triggered by hitting
237 * ENTER on the keyboard.
239 public static class InsertBreakAction extends TextAction
243 * Creates a new <code>InsertBreakAction</code>.
245 public InsertBreakAction()
247 super(insertBreakAction);
251 * Performs the <code>Action</code>.
253 * @param event the action event describing the user action
255 public void actionPerformed(ActionEvent event)
257 JTextComponent t = getTextComponent(event);
258 t.replaceSelection("\n");
263 * Places content into the associated editor. If there currently is a
264 * selection, this selection is replaced.
266 // FIXME: Figure out what this Action is supposed to do. Obviously text
267 // that is entered by the user is inserted through DefaultKeyTypedAction.
268 public static class InsertContentAction extends TextAction
272 * Creates a new <code>InsertContentAction</code>.
274 public InsertContentAction()
276 super(insertContentAction);
280 * Performs the <code>Action</code>.
282 * @param event the action event describing the user action
284 public void actionPerformed(ActionEvent event)
286 // FIXME: Figure out what this Action is supposed to do. Obviously text
287 // that is entered by the user is inserted through DefaultKeyTypedAction.
292 * Inserts a TAB character into the text editor.
294 public static class InsertTabAction extends TextAction
298 * Creates a new <code>TabAction</code>.
300 public InsertTabAction()
302 super(insertTabAction);
306 * Performs the <code>Action</code>.
308 * @param event the action event describing the user action
310 public void actionPerformed(ActionEvent event)
312 // FIXME: Implement this.
317 * The serial version of DefaultEditorKit.
319 private static final long serialVersionUID = 9017245433028523428L;
322 * The name of the <code>Action</code> that moves the caret one character
323 * backwards.
325 * @see #getActions()
327 public static final String backwardAction = "caret-backward";
330 * The name of the <code>Action</code> that creates a beep in the speaker.
332 * @see #getActions()
334 public static final String beepAction = "beep";
337 * The name of the <code>Action</code> that moves the caret to the beginning
338 * of the <code>Document</code>.
340 * @see #getActions()
342 public static final String beginAction = "caret-begin";
345 * The name of the <code>Action</code> that moves the caret to the beginning
346 * of the current line.
348 * @see #getActions()
350 public static final String beginLineAction = "caret-begin-line";
353 * The name of the <code>Action</code> that moves the caret to the beginning
354 * of the current paragraph.
356 * @see #getActions()
358 public static final String beginParagraphAction = "caret-begin-paragraph";
361 * The name of the <code>Action</code> that moves the caret to the beginning
362 * of the current word.
364 * @see #getActions()
366 public static final String beginWordAction = "caret-begin-word";
369 * The name of the <code>Action</code> that copies the selected content
370 * into the system clipboard.
372 * @see #getActions()
374 public static final String copyAction = "copy-to-clipboard";
377 * The name of the <code>Action</code> that copies the selected content
378 * into the system clipboard and removes the selection.
380 * @see #getActions()
382 public static final String cutAction = "cut-to-clipboard";
385 * The name of the <code>Action</code> that is performed by default if
386 * a key is typed and there is no keymap entry.
388 * @see #getActions()
390 public static final String defaultKeyTypedAction = "default-typed";
393 * The name of the <code>Action</code> that deletes the character that
394 * follows the current caret position.
396 * @see #getActions()
398 public static final String deleteNextCharAction = "delete-next";
401 * The name of the <code>Action</code> that deletes the character that
402 * precedes the current caret position.
404 * @see #getActions()
406 public static final String deletePrevCharAction = "delete-previous";
409 * The name of the <code>Action</code> that moves the caret one line down.
411 * @see #getActions()
413 public static final String downAction = "caret-down";
416 * The name of the <code>Action</code> that moves the caret to the end
417 * of the <code>Document</code>.
419 * @see #getActions()
421 public static final String endAction = "caret-end";
424 * The name of the <code>Action</code> that moves the caret to the end
425 * of the current line.
427 * @see #getActions()
429 public static final String endLineAction = "caret-end-line";
432 * When a document is read and an CRLF is encountered, then we add a property
433 * with this name and a value of &quot;\r\n&quot;.
435 public static final String EndOfLineStringProperty = "__EndOfLine__";
438 * The name of the <code>Action</code> that moves the caret to the end
439 * of the current paragraph.
441 * @see #getActions()
443 public static final String endParagraphAction = "caret-end-paragraph";
446 * The name of the <code>Action</code> that moves the caret to the end
447 * of the current word.
449 * @see #getActions()
451 public static final String endWordAction = "caret-end-word";
454 * The name of the <code>Action</code> that moves the caret one character
455 * forward.
457 * @see #getActions()
459 public static final String forwardAction = "caret-forward";
462 * The name of the <code>Action</code> that inserts a line break.
464 * @see #getActions()
466 public static final String insertBreakAction = "insert-break";
469 * The name of the <code>Action</code> that inserts some content.
471 * @see #getActions()
473 public static final String insertContentAction = "insert-content";
476 * The name of the <code>Action</code> that inserts a TAB.
478 * @see #getActions()
480 public static final String insertTabAction = "insert-tab";
483 * The name of the <code>Action</code> that moves the caret to the beginning
484 * of the next word.
486 * @see #getActions()
488 public static final String nextWordAction = "caret-next-word";
491 * The name of the <code>Action</code> that moves the caret one page down.
493 * @see #getActions()
495 public static final String pageDownAction = "page-down";
498 * The name of the <code>Action</code> that moves the caret one page up.
500 * @see #getActions()
502 public static final String pageUpAction = "page-up";
505 * The name of the <code>Action</code> that copies content from the system
506 * clipboard into the document.
508 * @see #getActions()
510 public static final String pasteAction = "paste-from-clipboard";
513 * The name of the <code>Action</code> that moves the caret to the beginning
514 * of the previous word.
516 * @see #getActions()
518 public static final String previousWordAction = "caret-previous-word";
521 * The name of the <code>Action</code> that sets the editor in read only
522 * mode.
524 * @see #getActions()
526 public static final String readOnlyAction = "set-read-only";
529 * The name of the <code>Action</code> that selects the whole document.
531 * @see #getActions()
533 public static final String selectAllAction = "select-all";
536 * The name of the <code>Action</code> that moves the caret one character
537 * backwards, possibly extending the current selection.
539 * @see #getActions()
541 public static final String selectionBackwardAction = "selection-backward";
544 * The name of the <code>Action</code> that moves the caret to the beginning
545 * of the document, possibly extending the current selection.
547 * @see #getActions()
549 public static final String selectionBeginAction = "selection-begin";
552 * The name of the <code>Action</code> that moves the caret to the beginning
553 * of the current line, possibly extending the current selection.
555 * @see #getActions()
557 public static final String selectionBeginLineAction = "selection-begin-line";
560 * The name of the <code>Action</code> that moves the caret to the beginning
561 * of the current paragraph, possibly extending the current selection.
563 * @see #getActions()
565 public static final String selectionBeginParagraphAction =
566 "selection-begin-paragraph";
569 * The name of the <code>Action</code> that moves the caret to the beginning
570 * of the current word, possibly extending the current selection.
572 * @see #getActions()
574 public static final String selectionBeginWordAction = "selection-begin-word";
577 * The name of the <code>Action</code> that moves the caret one line down,
578 * possibly extending the current selection.
580 * @see #getActions()
582 public static final String selectionDownAction = "selection-down";
585 * The name of the <code>Action</code> that moves the caret to the end
586 * of the document, possibly extending the current selection.
588 * @see #getActions()
590 public static final String selectionEndAction = "selection-end";
593 * The name of the <code>Action</code> that moves the caret to the end
594 * of the current line, possibly extending the current selection.
596 * @see #getActions()
598 public static final String selectionEndLineAction = "selection-end-line";
601 * The name of the <code>Action</code> that moves the caret to the end
602 * of the current paragraph, possibly extending the current selection.
604 * @see #getActions()
606 public static final String selectionEndParagraphAction =
607 "selection-end-paragraph";
610 * The name of the <code>Action</code> that moves the caret to the end
611 * of the current word, possibly extending the current selection.
613 * @see #getActions()
615 public static final String selectionEndWordAction = "selection-end-word";
618 * The name of the <code>Action</code> that moves the caret one character
619 * forwards, possibly extending the current selection.
621 * @see #getActions()
623 public static final String selectionForwardAction = "selection-forward";
626 * The name of the <code>Action</code> that moves the caret to the beginning
627 * of the next word, possibly extending the current selection.
629 * @see #getActions()
631 public static final String selectionNextWordAction = "selection-next-word";
634 * The name of the <code>Action</code> that moves the caret to the beginning
635 * of the previous word, possibly extending the current selection.
637 * @see #getActions()
639 public static final String selectionPreviousWordAction =
640 "selection-previous-word";
643 * The name of the <code>Action</code> that moves the caret one line up,
644 * possibly extending the current selection.
646 * @see #getActions()
648 public static final String selectionUpAction = "selection-up";
651 * The name of the <code>Action</code> that selects the line around the
652 * caret.
654 * @see #getActions()
656 public static final String selectLineAction = "select-line";
659 * The name of the <code>Action</code> that selects the paragraph around the
660 * caret.
662 * @see #getActions()
664 public static final String selectParagraphAction = "select-paragraph";
667 * The name of the <code>Action</code> that selects the word around the
668 * caret.
670 * @see #getActions()
672 public static final String selectWordAction = "select-word";
675 * The name of the <code>Action</code> that moves the caret one line up.
677 * @see #getActions()
679 public static final String upAction = "caret-up";
682 * The name of the <code>Action</code> that sets the editor in read-write
683 * mode.
685 * @see #getActions()
687 public static final String writableAction = "set-writable";
690 * Creates a new <code>DefaultEditorKit</code>.
692 public DefaultEditorKit()
694 // Nothing to do here.
698 * The <code>Action</code>s that are supported by the
699 * <code>DefaultEditorKit</code>.
701 // TODO: All these inner classes look ugly. Maybe work out a better way
702 // to handle this.
703 private static Action[] defaultActions =
704 new Action[] {
705 new BeepAction(),
706 new CopyAction(),
707 new CutAction(),
708 new DefaultKeyTypedAction(),
709 new InsertBreakAction(),
710 new InsertContentAction(),
711 new InsertTabAction(),
712 new PasteAction(),
713 new TextAction(deleteNextCharAction)
715 public void actionPerformed(ActionEvent event)
717 JTextComponent t = getTextComponent(event);
718 if (t != null)
722 int pos = t.getCaret().getDot();
723 if (pos < t.getDocument().getEndPosition().getOffset())
725 t.getDocument().remove(t.getCaret().getDot(), 1);
728 catch (BadLocationException e)
730 // FIXME: we're not authorized to throw this.. swallow it?
735 new TextAction(deletePrevCharAction)
737 public void actionPerformed(ActionEvent event)
739 JTextComponent t = getTextComponent(event);
740 if (t != null)
744 int pos = t.getCaret().getDot();
745 if (pos > t.getDocument().getStartPosition().getOffset())
747 t.getDocument().remove(pos - 1, 1);
748 t.getCaret().setDot(pos - 1);
751 catch (BadLocationException e)
753 // FIXME: we're not authorized to throw this.. swallow it?
758 new TextAction(backwardAction)
760 public void actionPerformed(ActionEvent event)
762 JTextComponent t = getTextComponent(event);
763 if (t != null)
765 t.getCaret().setDot(Math.max(t.getCaret().getDot() - 1,
766 t.getDocument().getStartPosition().getOffset()));
770 new TextAction(forwardAction)
772 public void actionPerformed(ActionEvent event)
774 JTextComponent t = getTextComponent(event);
775 if (t != null)
777 t.getCaret().setDot(Math.min(t.getCaret().getDot() + 1,
778 t.getDocument().getEndPosition().getOffset()));
782 new TextAction(selectionBackwardAction)
784 public void actionPerformed(ActionEvent event)
786 JTextComponent t = getTextComponent(event);
787 if (t != null)
789 t.getCaret().moveDot(Math.max(t.getCaret().getDot() - 1,
790 t.getDocument().getStartPosition().getOffset()));
794 new TextAction(selectionForwardAction)
796 public void actionPerformed(ActionEvent event)
798 JTextComponent t = getTextComponent(event);
799 if (t != null)
801 t.getCaret().moveDot(Math.min(t.getCaret().getDot() + 1,
802 t.getDocument().getEndPosition().getOffset()));
809 * Creates the <code>Caret</code> for this <code>EditorKit</code>. This
810 * returns a {@link DefaultCaret} in this case.
812 * @return the <code>Caret</code> for this <code>EditorKit</code>
814 public Caret createCaret()
816 return new DefaultCaret();
820 * Creates the default {@link Document} that this <code>EditorKit</code>
821 * supports. This is a {@link PlainDocument} in this case.
823 * @return the default {@link Document} that this <code>EditorKit</code>
824 * supports
826 public Document createDefaultDocument()
828 return new PlainDocument();
832 * Returns the <code>Action</code>s supported by this <code>EditorKit</code>.
834 * @return the <code>Action</code>s supported by this <code>EditorKit</code>
836 public Action[] getActions()
838 return defaultActions;
842 * Returns the content type that this <code>EditorKit</code> supports.
843 * The <code>DefaultEditorKit</code> supports the content type
844 * <code>text/plain</code>.
846 * @return the content type that this <code>EditorKit</code> supports
848 public String getContentType()
850 return "text/plain";
854 * Returns a {@link ViewFactory} that is able to create {@link View}s for
855 * the <code>Element</code>s that are used in this <code>EditorKit</code>'s
856 * model. This returns null which lets the UI of the text component supply
857 * <code>View</code>s.
859 * @return a {@link ViewFactory} that is able to create {@link View}s for
860 * the <code>Element</code>s that are used in this
861 * <code>EditorKit</code>'s model
863 public ViewFactory getViewFactory()
865 return null;
869 * Reads a document of the supported content type from an {@link InputStream}
870 * into the actual {@link Document} object.
872 * @param in the stream from which to read the document
873 * @param document the document model into which the content is read
874 * @param offset the offset inside to document where the content is inserted
876 * @throws BadLocationException if <code>offset</code> is an invalid location
877 * inside <code>document</code>
878 * @throws IOException if something goes wrong while reading from
879 * <code>in</code>
881 public void read(InputStream in, Document document, int offset)
882 throws BadLocationException, IOException
884 read(new InputStreamReader(in), document, offset);
888 * Reads a document of the supported content type from a {@link Reader}
889 * into the actual {@link Document} object.
891 * @param in the reader from which to read the document
892 * @param document the document model into which the content is read
893 * @param offset the offset inside to document where the content is inserted
895 * @throws BadLocationException if <code>offset</code> is an invalid location
896 * inside <code>document</code>
897 * @throws IOException if something goes wrong while reading from
898 * <code>in</code>
900 public void read(Reader in, Document document, int offset)
901 throws BadLocationException, IOException
903 BufferedReader reader = new BufferedReader(in);
905 String line;
906 StringBuffer content = new StringBuffer();
908 while ((line = reader.readLine()) != null)
910 content.append(line);
911 content.append("\n");
914 document.insertString(offset, content.toString(),
915 SimpleAttributeSet.EMPTY);
919 * Writes the <code>Document</code> (or a fragment of the
920 * <code>Document</code>) to an {@link OutputStream} in the
921 * supported content type format.
923 * @param out the stream to write to
924 * @param document the document that should be written out
925 * @param offset the beginning offset from where to write
926 * @param len the length of the fragment to write
928 * @throws BadLocationException if <code>offset</code> or
929 * <code>offset + len</code>is an invalid location inside
930 * <code>document</code>
931 * @throws IOException if something goes wrong while writing to
932 * <code>out</code>
934 public void write(OutputStream out, Document document, int offset, int len)
935 throws BadLocationException, IOException
937 write(new OutputStreamWriter(out), document, offset, len);
941 * Writes the <code>Document</code> (or a fragment of the
942 * <code>Document</code>) to a {@link Writer} in the
943 * supported content type format.
945 * @param out the writer to write to
946 * @param document the document that should be written out
947 * @param offset the beginning offset from where to write
948 * @param len the length of the fragment to write
950 * @throws BadLocationException if <code>offset</code> is an
951 * invalid location inside <code>document</code>.
952 * @throws IOException if something goes wrong while writing to
953 * <code>out</code>
955 public void write(Writer out, Document document, int offset, int len)
956 throws BadLocationException, IOException
958 // Throw a BLE if offset is invalid
959 if (offset < 0 || offset > document.getLength())
960 throw new BadLocationException("Tried to write to invalid location",
961 offset);
963 // If they gave an overly large len, just adjust it
964 if (offset + len > document.getLength())
965 len = document.getLength() - offset;
967 out.write(document.getText(offset, len));