Merge from the pain train
[official-gcc.git] / libjava / javax / swing / text / DefaultEditorKit.java
blobecec70731ce0dcf1773464fe6ed2adb48b91b11e
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 public class DefaultEditorKit extends EditorKit
56 public static class BeepAction
57 extends TextAction
59 public BeepAction()
61 super(beepAction);
64 public void actionPerformed(ActionEvent event)
66 Toolkit.getDefaultToolkit().beep();
70 public static class CopyAction
71 extends TextAction
73 public CopyAction()
75 super(copyAction);
77 public void actionPerformed(ActionEvent event)
82 public static class CutAction
83 extends TextAction
85 public CutAction()
87 super(cutAction);
90 public void actionPerformed(ActionEvent event)
95 public static class DefaultKeyTypedAction
96 extends TextAction
98 public DefaultKeyTypedAction()
100 super(defaultKeyTypedAction);
103 public void actionPerformed(ActionEvent event)
105 JTextComponent t = getTextComponent(event);
106 if (t != null)
110 t.getDocument().insertString(t.getCaret().getDot(), event.getActionCommand(), null);
111 t.getCaret().setDot(Math.min(t.getCaret().getDot() + 1,
112 t.getDocument().getEndPosition().getOffset()));
113 t.repaint();
115 catch (BadLocationException be)
117 // FIXME: we're not authorized to throw this.. swallow it?
123 public static class InsertBreakAction
124 extends TextAction
126 public InsertBreakAction()
128 super(insertBreakAction);
131 public void actionPerformed(ActionEvent event)
136 public static class InsertContentAction
137 extends TextAction
139 public InsertContentAction()
141 super(insertContentAction);
143 public void actionPerformed(ActionEvent event)
148 public static class InsertTabAction
149 extends TextAction
151 public InsertTabAction()
153 super(insertTabAction);
156 public void actionPerformed(ActionEvent event)
161 public static class PasteAction
162 extends TextAction
164 public PasteAction()
166 super(pasteAction);
169 public void actionPerformed(ActionEvent event)
174 private static final long serialVersionUID = 9017245433028523428L;
176 public static final String backwardAction = "caret-backward";
177 public static final String beepAction = "beep";
178 public static final String beginAction = "caret-begin";
179 public static final String beginLineAction = "caret-begin-line";
180 public static final String beginParagraphAction = "caret-begin-paragraph";
181 public static final String beginWordAction = "caret-begin-word";
182 public static final String copyAction = "copy-to-clipboard";
183 public static final String cutAction = "cut-to-clipboard";
184 public static final String defaultKeyTypedAction = "default-typed";
185 public static final String deleteNextCharAction = "delete-next";
186 public static final String deletePrevCharAction = "delete-previous";
187 public static final String downAction = "caret-down";
188 public static final String endAction = "caret-end";
189 public static final String endLineAction = "caret-end-line";
190 public static final String EndOfLineStringProperty = "__EndOfLine__";
191 public static final String endParagraphAction = "caret-end-paragraph";
192 public static final String endWordAction = "caret-end-word";
193 public static final String forwardAction = "caret-forward";
194 public static final String insertBreakAction = "insert-break";
195 public static final String insertContentAction = "insert-content";
196 public static final String insertTabAction = "insert-tab";
197 public static final String nextWordAction = "caret-next-word";
198 public static final String pageDownAction = "page-down";
199 public static final String pageUpAction = "page-up";
200 public static final String pasteAction = "paste-from-clipboard";
201 public static final String previousWordAction = "caret-previous-word";
202 public static final String readOnlyAction = "set-read-only";
203 public static final String selectAllAction = "select-all";
204 public static final String selectionBackwardAction = "selection-backward";
205 public static final String selectionBeginAction = "selection-begin";
206 public static final String selectionBeginLineAction = "selection-begin-line";
207 public static final String selectionBeginParagraphAction =
208 "selection-begin-paragraph";
209 public static final String selectionBeginWordAction = "selection-begin-word";
210 public static final String selectionDownAction = "selection-down";
211 public static final String selectionEndAction = "selection-end";
212 public static final String selectionEndLineAction = "selection-end-line";
213 public static final String selectionEndParagraphAction =
214 "selection-end-paragraph";
215 public static final String selectionEndWordAction = "selection-end-word";
216 public static final String selectionForwardAction = "selection-forward";
217 public static final String selectionNextWordAction = "selection-next-word";
218 public static final String selectionPreviousWordAction =
219 "selection-previous-word";
220 public static final String selectionUpAction = "selection-up";
221 public static final String selectLineAction = "select-line";
222 public static final String selectParagraphAction = "select-paragraph";
223 public static final String selectWordAction = "select-word";
224 public static final String upAction = "caret-up";
225 public static final String writableAction = "set-writable";
227 public DefaultEditorKit()
231 private static Action[] defaultActions =
232 new Action[] {
233 new BeepAction(),
234 new CopyAction(),
235 new CutAction(),
236 new DefaultKeyTypedAction(),
237 new InsertBreakAction(),
238 new InsertContentAction(),
239 new InsertTabAction(),
240 new PasteAction(),
241 new TextAction(deleteNextCharAction)
243 public void actionPerformed(ActionEvent event)
245 JTextComponent t = getTextComponent(event);
246 if (t != null)
250 int pos = t.getCaret().getDot();
251 if (pos < t.getDocument().getEndPosition().getOffset())
253 t.getDocument().remove(t.getCaret().getDot(), 1);
254 t.repaint();
257 catch (BadLocationException e)
259 // FIXME: we're not authorized to throw this.. swallow it?
264 new TextAction(deletePrevCharAction)
266 public void actionPerformed(ActionEvent event)
268 JTextComponent t = getTextComponent(event);
269 if (t != null)
273 int pos = t.getCaret().getDot();
274 if (pos > t.getDocument().getStartPosition().getOffset())
276 t.getDocument().remove(pos - 1, 1);
277 t.getCaret().setDot(pos - 1);
278 t.repaint();
281 catch (BadLocationException e)
283 // FIXME: we're not authorized to throw this.. swallow it?
288 new TextAction(backwardAction)
290 public void actionPerformed(ActionEvent event)
292 JTextComponent t = getTextComponent(event);
293 if (t != null)
295 t.getCaret().setDot(Math.max(t.getCaret().getDot() - 1,
296 t.getDocument().getStartPosition().getOffset()));
300 new TextAction(forwardAction)
302 public void actionPerformed(ActionEvent event)
304 JTextComponent t = getTextComponent(event);
305 if (t != null)
307 t.getCaret().setDot(Math.min(t.getCaret().getDot() + 1,
308 t.getDocument().getEndPosition().getOffset()));
312 new TextAction(selectionBackwardAction)
314 public void actionPerformed(ActionEvent event)
316 JTextComponent t = getTextComponent(event);
317 if (t != null)
319 t.getCaret().moveDot(Math.max(t.getCaret().getDot() - 1,
320 t.getDocument().getStartPosition().getOffset()));
324 new TextAction(selectionForwardAction)
326 public void actionPerformed(ActionEvent event)
328 JTextComponent t = getTextComponent(event);
329 if (t != null)
331 t.getCaret().moveDot(Math.min(t.getCaret().getDot() + 1,
332 t.getDocument().getEndPosition().getOffset()));
338 public Caret createCaret()
340 return new DefaultCaret();
343 public Document createDefaultDocument()
345 return new PlainDocument();
348 public Action[] getActions()
350 return defaultActions;
353 public String getContentType()
355 return "text/plain";
358 public ViewFactory getViewFactory()
360 return null;
363 public void read(InputStream in, Document document, int offset)
364 throws BadLocationException, IOException
366 read(new InputStreamReader(in), document, offset);
369 public void read(Reader in, Document document, int offset)
370 throws BadLocationException, IOException
372 BufferedReader reader = new BufferedReader(in);
374 String line;
375 StringBuffer content = new StringBuffer();
377 while ((line = reader.readLine()) != null)
379 content.append(line);
380 content.append("\n");
383 document.insertString(offset, content.toString(),
384 SimpleAttributeSet.EMPTY);
387 public void write(OutputStream out, Document document, int offset, int len)
388 throws BadLocationException, IOException
390 write(new OutputStreamWriter(out), document, offset, len);
393 public void write(Writer out, Document document, int offset, int len)
394 throws BadLocationException, IOException