Source for javax.swing.text.DefaultEditorKit

   1: /* DefaultEditorKit.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing.text;
  40: 
  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;
  51: 
  52: import javax.swing.Action;
  53: 
  54: public class DefaultEditorKit extends EditorKit
  55: {
  56:   public static class BeepAction
  57:     extends TextAction
  58:   {
  59:     public BeepAction()
  60:     {
  61:       super(beepAction);
  62:     }
  63: 
  64:     public void actionPerformed(ActionEvent event)
  65:     {
  66:       Toolkit.getDefaultToolkit().beep();
  67:     }
  68:   }
  69: 
  70:   public static class CopyAction 
  71:     extends TextAction
  72:   {
  73:     public CopyAction()
  74:     {
  75:       super(copyAction);
  76:     }
  77:     public void actionPerformed(ActionEvent event)
  78:     {
  79:     }
  80:   }
  81: 
  82:   public static class CutAction 
  83:     extends TextAction
  84:   {
  85:     public CutAction()
  86:     {
  87:       super(cutAction);
  88:     }
  89: 
  90:     public void actionPerformed(ActionEvent event)
  91:     {
  92:     }
  93:   }
  94: 
  95:   /**
  96:    * This action is executed as default action when a KEY_TYPED
  97:    * event is received and no keymap entry exists for that. The purpose
  98:    * of this action is to filter out a couple of characters. This includes
  99:    * the control characters and characters with the ALT-modifier.
 100:    * 
 101:    * If an event does not get filtered, it is inserted into the document
 102:    * of the text component. If there is some text selected in the text component,
 103:    * this text will be replaced.
 104:    */
 105:   public static class DefaultKeyTypedAction 
 106:     extends TextAction
 107:   {
 108:     public DefaultKeyTypedAction()
 109:     {
 110:       super(defaultKeyTypedAction);
 111:     }
 112: 
 113:     public void actionPerformed(ActionEvent event)
 114:     {
 115:       // first we filter the following events:
 116:       // - control characters
 117:       // - key events with the ALT modifier (FIXME: filter that too!)
 118:       char c = event.getActionCommand().charAt(0);
 119:       if (Character.isISOControl(c))
 120:         return;
 121: 
 122:       JTextComponent t = getTextComponent(event);
 123:       if (t != null)
 124:         {
 125:           try
 126:             {
 127:               t.getDocument().insertString(t.getCaret().getDot(), event.getActionCommand(), null);
 128:               t.getCaret().setDot(Math.min(t.getCaret().getDot() + 1,
 129:                                            t.getDocument().getEndPosition().getOffset()));
 130:             }
 131:           catch (BadLocationException be)
 132:             {
 133:               // FIXME: we're not authorized to throw this.. swallow it?
 134:             }
 135:         }
 136:     }
 137:   }
 138: 
 139:   /**
 140:    * This action inserts a newline character into the document
 141:    * of the text component. This is typically triggered by hitting
 142:    * ENTER on the keyboard.
 143:    */
 144:   public static class InsertBreakAction 
 145:     extends TextAction
 146:   {
 147:     public InsertBreakAction()
 148:     {
 149:       super(insertBreakAction);
 150:     }
 151: 
 152:     public void actionPerformed(ActionEvent event)
 153:     {
 154:       JTextComponent t = getTextComponent(event);
 155:       t.replaceSelection("\n");
 156:     }
 157:   }
 158: 
 159:   public static class InsertContentAction 
 160:     extends TextAction
 161:   {
 162:     public InsertContentAction()
 163:     {
 164:       super(insertContentAction);
 165:     }
 166:     public void actionPerformed(ActionEvent event)
 167:     {
 168:     }
 169:   }
 170: 
 171:   public static class InsertTabAction 
 172:     extends TextAction
 173:   {
 174:     public InsertTabAction()
 175:     {
 176:       super(insertTabAction);
 177:     }
 178: 
 179:     public void actionPerformed(ActionEvent event)
 180:     {
 181:     }
 182:   }
 183: 
 184:   public static class PasteAction 
 185:     extends TextAction
 186:   {
 187:     public PasteAction()
 188:     {
 189:       super(pasteAction);
 190:     }
 191: 
 192:     public void actionPerformed(ActionEvent event)
 193:     {
 194:     }
 195:   }
 196: 
 197:   private static final long serialVersionUID = 9017245433028523428L;
 198:   
 199:   public static final String backwardAction = "caret-backward";
 200:   public static final String beepAction = "beep";
 201:   public static final String beginAction = "caret-begin";
 202:   public static final String beginLineAction = "caret-begin-line";
 203:   public static final String beginParagraphAction = "caret-begin-paragraph";
 204:   public static final String beginWordAction = "caret-begin-word";
 205:   public static final String copyAction = "copy-to-clipboard";
 206:   public static final String cutAction = "cut-to-clipboard";
 207:   public static final String defaultKeyTypedAction = "default-typed";
 208:   public static final String deleteNextCharAction = "delete-next";
 209:   public static final String deletePrevCharAction = "delete-previous";
 210:   public static final String downAction = "caret-down";
 211:   public static final String endAction = "caret-end";
 212:   public static final String endLineAction = "caret-end-line";
 213:   public static final String EndOfLineStringProperty = "__EndOfLine__";
 214:   public static final String endParagraphAction = "caret-end-paragraph";
 215:   public static final String endWordAction = "caret-end-word";
 216:   public static final String forwardAction = "caret-forward";
 217:   public static final String insertBreakAction = "insert-break";
 218:   public static final String insertContentAction = "insert-content";
 219:   public static final String insertTabAction = "insert-tab";
 220:   public static final String nextWordAction = "caret-next-word";
 221:   public static final String pageDownAction = "page-down";
 222:   public static final String pageUpAction = "page-up";
 223:   public static final String pasteAction = "paste-from-clipboard";
 224:   public static final String previousWordAction = "caret-previous-word";
 225:   public static final String readOnlyAction = "set-read-only";
 226:   public static final String selectAllAction = "select-all";
 227:   public static final String selectionBackwardAction = "selection-backward";
 228:   public static final String selectionBeginAction = "selection-begin";
 229:   public static final String selectionBeginLineAction = "selection-begin-line";
 230:   public static final String selectionBeginParagraphAction =
 231:     "selection-begin-paragraph";
 232:   public static final String selectionBeginWordAction = "selection-begin-word";
 233:   public static final String selectionDownAction = "selection-down";
 234:   public static final String selectionEndAction = "selection-end";
 235:   public static final String selectionEndLineAction = "selection-end-line";
 236:   public static final String selectionEndParagraphAction =
 237:     "selection-end-paragraph";
 238:   public static final String selectionEndWordAction = "selection-end-word";
 239:   public static final String selectionForwardAction = "selection-forward";
 240:   public static final String selectionNextWordAction = "selection-next-word";
 241:   public static final String selectionPreviousWordAction =
 242:     "selection-previous-word";
 243:   public static final String selectionUpAction = "selection-up";
 244:   public static final String selectLineAction = "select-line";
 245:   public static final String selectParagraphAction = "select-paragraph";
 246:   public static final String selectWordAction = "select-word";
 247:   public static final String upAction = "caret-up";
 248:   public static final String writableAction = "set-writable";
 249: 
 250:   public DefaultEditorKit()
 251:   {
 252:   }
 253: 
 254:   private static Action[] defaultActions = 
 255:   new Action[] {
 256:     new BeepAction(),
 257:     new CopyAction(),
 258:     new CutAction(),
 259:     new DefaultKeyTypedAction(),
 260:     new InsertBreakAction(),
 261:     new InsertContentAction(),
 262:     new InsertTabAction(),
 263:     new PasteAction(),
 264:     new TextAction(deleteNextCharAction) 
 265:     { 
 266:       public void actionPerformed(ActionEvent event)
 267:       {
 268:         JTextComponent t = getTextComponent(event);
 269:         if (t != null)
 270:           {
 271:             try
 272:               {
 273:                 int pos = t.getCaret().getDot();
 274:                 if (pos < t.getDocument().getEndPosition().getOffset())
 275:                   {
 276:                     t.getDocument().remove(t.getCaret().getDot(), 1);
 277:                   }
 278:               }
 279:             catch (BadLocationException e)
 280:               {
 281:                 // FIXME: we're not authorized to throw this.. swallow it?
 282:               }
 283:           }
 284:       }
 285:     },
 286:     new TextAction(deletePrevCharAction) 
 287:     { 
 288:       public void actionPerformed(ActionEvent event)
 289:       {
 290:         JTextComponent t = getTextComponent(event);
 291:         if (t != null)
 292:           {
 293:             try
 294:               {
 295:                 int pos = t.getCaret().getDot();
 296:                 if (pos > t.getDocument().getStartPosition().getOffset())
 297:                   {
 298:                     t.getDocument().remove(pos - 1, 1);
 299:                     t.getCaret().setDot(pos - 1);
 300:                   }
 301:               }
 302:             catch (BadLocationException e)
 303:               {
 304:                 // FIXME: we're not authorized to throw this.. swallow it?
 305:               }
 306:           }
 307:       }
 308:     },
 309:     new TextAction(backwardAction) 
 310:     { 
 311:       public void actionPerformed(ActionEvent event)
 312:       {
 313:         JTextComponent t = getTextComponent(event);
 314:         if (t != null)
 315:           {
 316:             t.getCaret().setDot(Math.max(t.getCaret().getDot() - 1,
 317:                                          t.getDocument().getStartPosition().getOffset()));
 318:           }
 319:       }
 320:     },
 321:     new TextAction(forwardAction) 
 322:     { 
 323:       public void actionPerformed(ActionEvent event)
 324:       {
 325:         JTextComponent t = getTextComponent(event);
 326:         if (t != null)
 327:           {
 328:             t.getCaret().setDot(Math.min(t.getCaret().getDot() + 1,
 329:                                          t.getDocument().getEndPosition().getOffset()));
 330:           }
 331:       }
 332:     },
 333:     new TextAction(selectionBackwardAction)
 334:     {
 335:       public void actionPerformed(ActionEvent event)
 336:       {
 337:     JTextComponent t = getTextComponent(event);
 338:     if (t != null)
 339:       {
 340:         t.getCaret().moveDot(Math.max(t.getCaret().getDot() - 1,
 341:                       t.getDocument().getStartPosition().getOffset()));
 342:       }
 343:       }
 344:     },
 345:     new TextAction(selectionForwardAction)
 346:     {
 347:       public void actionPerformed(ActionEvent event)
 348:       {
 349:         JTextComponent t = getTextComponent(event);
 350:         if (t != null)
 351:           {
 352:             t.getCaret().moveDot(Math.min(t.getCaret().getDot() + 1,
 353:                                           t.getDocument().getEndPosition().getOffset()));
 354:           }
 355:       }
 356:     },
 357:   };
 358: 
 359:   public Caret createCaret()
 360:   {
 361:     return new DefaultCaret();
 362:   }
 363: 
 364:   public Document createDefaultDocument()
 365:   {
 366:     return new PlainDocument();
 367:   }
 368:     
 369:   public Action[] getActions()
 370:   {
 371:     return defaultActions;
 372:   }
 373: 
 374:   public String getContentType()
 375:   {
 376:     return "text/plain";
 377:   }
 378:   
 379:   public ViewFactory getViewFactory()
 380:   {
 381:     return null;
 382:   }
 383: 
 384:   public void read(InputStream in, Document document, int offset)
 385:     throws BadLocationException, IOException
 386:   {
 387:     read(new InputStreamReader(in), document, offset);
 388:   }
 389: 
 390:   public void read(Reader in, Document document, int offset)
 391:     throws BadLocationException, IOException
 392:   {
 393:     BufferedReader reader = new BufferedReader(in);
 394: 
 395:     String line;
 396:     StringBuffer content = new StringBuffer();
 397: 
 398:     while ((line = reader.readLine()) != null)
 399:       {
 400:     content.append(line);
 401:     content.append("\n");
 402:       }
 403:     
 404:     document.insertString(offset, content.toString(),
 405:               SimpleAttributeSet.EMPTY);
 406:   }
 407: 
 408:   public void write(OutputStream out, Document document, int offset, int len)
 409:     throws BadLocationException, IOException
 410:   {
 411:     write(new OutputStreamWriter(out), document, offset, len);
 412:   }
 413: 
 414:   public void write(Writer out, Document document, int offset, int len)
 415:     throws BadLocationException, IOException
 416:   {
 417:   }
 418: }