Source for javax.swing.text.JTextComponent

   1: /* JTextComponent.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.AWTEvent;
  42: import java.awt.Color;
  43: import java.awt.Dimension;
  44: import java.awt.Insets;
  45: import java.awt.Point;
  46: import java.awt.Rectangle;
  47: import java.awt.datatransfer.Clipboard;
  48: import java.awt.datatransfer.DataFlavor;
  49: import java.awt.datatransfer.StringSelection;
  50: import java.awt.datatransfer.Transferable;
  51: import java.awt.datatransfer.UnsupportedFlavorException;
  52: import java.awt.event.ActionEvent;
  53: import java.awt.event.ActionListener;
  54: import java.awt.event.InputMethodListener;
  55: import java.awt.event.KeyEvent;
  56: import java.io.IOException;
  57: import java.io.Reader;
  58: import java.io.Writer;
  59: import java.util.Enumeration;
  60: import java.util.Hashtable;
  61: 
  62: import javax.accessibility.Accessible;
  63: import javax.accessibility.AccessibleContext;
  64: import javax.accessibility.AccessibleRole;
  65: import javax.accessibility.AccessibleStateSet;
  66: import javax.accessibility.AccessibleText;
  67: import javax.swing.Action;
  68: import javax.swing.ActionMap;
  69: import javax.swing.InputMap;
  70: import javax.swing.JComponent;
  71: import javax.swing.JViewport;
  72: import javax.swing.KeyStroke;
  73: import javax.swing.Scrollable;
  74: import javax.swing.SwingConstants;
  75: import javax.swing.Timer;
  76: import javax.swing.TransferHandler;
  77: import javax.swing.UIManager;
  78: import javax.swing.event.CaretEvent;
  79: import javax.swing.event.CaretListener;
  80: import javax.swing.event.DocumentEvent;
  81: import javax.swing.event.DocumentListener;
  82: import javax.swing.plaf.ActionMapUIResource;
  83: import javax.swing.plaf.InputMapUIResource;
  84: import javax.swing.plaf.TextUI;
  85: 
  86: public abstract class JTextComponent extends JComponent
  87:   implements Scrollable, Accessible
  88: {
  89:   /**
  90:    * AccessibleJTextComponent
  91:    */
  92:   public class AccessibleJTextComponent extends AccessibleJComponent
  93:     implements AccessibleText, CaretListener, DocumentListener
  94:   {
  95:     private static final long serialVersionUID = 7664188944091413696L;
  96: 
  97:     /**
  98:      * Constructor AccessibleJTextComponent
  99:      * @param component TODO
 100:      */
 101:     public AccessibleJTextComponent()
 102:     {
 103:     }
 104: 
 105:     /**
 106:      * getCaretPosition
 107:      * @return int
 108:      */
 109:     public int getCaretPosition()
 110:     {
 111:       return 0; // TODO
 112:     }
 113: 
 114:     /**
 115:      * getSelectedText
 116:      * @return String
 117:      */
 118:     public String getSelectedText()
 119:     {
 120:       return null; // TODO
 121:     }
 122: 
 123:     /**
 124:      * getSelectionStart
 125:      * @return int
 126:      */
 127:     public int getSelectionStart()
 128:     {
 129:       return 0; // TODO
 130:     }
 131: 
 132:     /**
 133:      * getSelectionEnd
 134:      * @return int
 135:      */
 136:     public int getSelectionEnd()
 137:     {
 138:       return 0; // TODO
 139:     }
 140: 
 141:     /**
 142:      * caretUpdate
 143:      * @param value0 TODO
 144:      */
 145:     public void caretUpdate(CaretEvent value0)
 146:     {
 147:       // TODO
 148:     }
 149: 
 150:     /**
 151:      * getAccessibleStateSet
 152:      * @return AccessibleStateSet
 153:      */
 154:     public AccessibleStateSet getAccessibleStateSet()
 155:     {
 156:       return null; // TODO
 157:     }
 158: 
 159:     /**
 160:      * getAccessibleRole
 161:      * @return AccessibleRole
 162:      */
 163:     public AccessibleRole getAccessibleRole()
 164:     {
 165:       return null; // TODO
 166:     }
 167: 
 168:     /**
 169:      * getAccessibleText
 170:      * @return AccessibleText
 171:      */
 172:     public AccessibleText getAccessibleText()
 173:     {
 174:       return null; // TODO
 175:     }
 176: 
 177:     /**
 178:      * insertUpdate
 179:      * @param value0 TODO
 180:      */
 181:     public void insertUpdate(DocumentEvent value0)
 182:     {
 183:       // TODO
 184:     }
 185: 
 186:     /**
 187:      * removeUpdate
 188:      * @param value0 TODO
 189:      */
 190:     public void removeUpdate(DocumentEvent value0)
 191:     {
 192:       // TODO
 193:     }
 194: 
 195:     /**
 196:      * changedUpdate
 197:      * @param value0 TODO
 198:      */
 199:     public void changedUpdate(DocumentEvent value0)
 200:     {
 201:       // TODO
 202:     }
 203: 
 204:     /**
 205:      * getIndexAtPoint
 206:      * @param value0 TODO
 207:      * @return int
 208:      */
 209:     public int getIndexAtPoint(Point value0)
 210:     {
 211:       return 0; // TODO
 212:     }
 213: 
 214:     /**
 215:      * getRootEditorRect
 216:      * @return Rectangle
 217:      */
 218:     Rectangle getRootEditorRect()
 219:     {
 220:       return null;
 221:     }
 222: 
 223:     /**
 224:      * getCharacterBounds
 225:      * @param value0 TODO
 226:      * @return Rectangle
 227:      */
 228:     public Rectangle getCharacterBounds(int value0)
 229:     {
 230:       return null; // TODO
 231:     }
 232: 
 233:     /**
 234:      * getCharCount
 235:      * @return int
 236:      */
 237:     public int getCharCount()
 238:     {
 239:       return 0; // TODO
 240:     }
 241: 
 242:     /**
 243:      * getCharacterAttribute
 244:      * @param value0 TODO
 245:      * @return AttributeSet
 246:      */
 247:     public AttributeSet getCharacterAttribute(int value0)
 248:     {
 249:       return null; // TODO
 250:     }
 251: 
 252:     /**
 253:      * getAtIndex
 254:      * @param value0 TODO
 255:      * @param value1 TODO
 256:      * @return String
 257:      */
 258:     public String getAtIndex(int value0, int value1)
 259:     {
 260:       return null; // TODO
 261:     }
 262: 
 263:     /**
 264:      * getAfterIndex
 265:      * @param value0 TODO
 266:      * @param value1 TODO
 267:      * @return String
 268:      */
 269:     public String getAfterIndex(int value0, int value1)
 270:     {
 271:       return null; // TODO
 272:     }
 273: 
 274:     /**
 275:      * getBeforeIndex
 276:      * @param value0 TODO
 277:      * @param value1 TODO
 278:      * @return String
 279:      */
 280:     public String getBeforeIndex(int value0, int value1)
 281:     {
 282:       return null; // TODO
 283:     }
 284:   }
 285: 
 286:   public static class KeyBinding
 287:   {
 288:     public KeyStroke key;
 289:     public String actionName;
 290: 
 291:     /**
 292:      * Creates a new <code>KeyBinding</code> instance.
 293:      *
 294:      * @param key a <code>KeyStroke</code> value
 295:      * @param actionName a <code>String</code> value
 296:      */
 297:     public KeyBinding(KeyStroke key, String actionName)
 298:     {
 299:       this.key = key;
 300:       this.actionName = actionName;
 301:     }
 302:   }
 303: 
 304:   /**
 305:    * The timer that lets the caret blink.
 306:    */
 307:   private class CaretBlinkTimer
 308:     extends Timer
 309:     implements ActionListener
 310:   {
 311:     /**
 312:      * Creates a new CaretBlinkTimer object with a default delay of 1 second.
 313:      */
 314:     public CaretBlinkTimer()
 315:     {
 316:       super(1000, null);
 317:       addActionListener(this);
 318:     }
 319: 
 320:     /**
 321:      * Lets the caret blink.
 322:      */
 323:     public void actionPerformed(ActionEvent ev)
 324:     {
 325:       Caret c = caret;
 326:       if (c != null)
 327:     c.setVisible(!c.isVisible());
 328:     }
 329: 
 330:     /**
 331:      * Updates the blink delay according to the current caret.
 332:      */
 333:     public void update()
 334:     {
 335:       stop();
 336:       Caret c = caret;
 337:       if (c != null)
 338:     {
 339:       setDelay(c.getBlinkRate());
 340:       if (editable)
 341:         start();
 342:       else
 343:         c.setVisible(false);
 344:     }
 345:     }
 346:   }
 347: 
 348:   /**
 349:    * According to <a
 350:    * href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html">this
 351:    * report</a>, a pair of private classes wraps a {@link
 352:    * javax.swing.text.Keymap} in the new {@link InputMap} / {@link
 353:    * ActionMap} interfaces, such that old Keymap-using code can make use of
 354:    * the new framework.
 355:    *
 356:    * <p>A little bit of experimentation with these classes reveals the following
 357:    * structure:
 358:    *
 359:    * <ul>
 360:    *
 361:    * <li>KeymapWrapper extends {@link InputMap} and holds a reference to
 362:    * the underlying {@link Keymap}.</li>
 363:    *
 364:    * <li>KeymapWrapper maps {@link KeyStroke} objects to {@link Action}
 365:    * objects, by delegation to the underlying {@link Keymap}.</li>
 366:    *
 367:    * <li>KeymapActionMap extends {@link ActionMap} also holds a reference to
 368:    * the underlying {@link Keymap} but only appears to use it for listing 
 369:    * its keys. </li>
 370:    *
 371:    * <li>KeymapActionMap maps all {@link Action} objects to
 372:    * <em>themselves</em>, whether they exist in the underlying {@link
 373:    * Keymap} or not, and passes other objects to the parent {@link
 374:    * ActionMap} for resolving.
 375:    *
 376:    * </ul>
 377:    */
 378: 
 379:   private class KeymapWrapper extends InputMap
 380:   {
 381:     Keymap map;
 382: 
 383:     public KeymapWrapper(Keymap k)
 384:     {
 385:       map = k;
 386:     }
 387: 
 388:     public int size()
 389:     {
 390:       return map.getBoundKeyStrokes().length + super.size();
 391:     }
 392: 
 393:     public Object get(KeyStroke ks)
 394:     {
 395:       Action mapped = null;
 396:       Keymap m = map;
 397:       while(mapped == null && m != null)
 398:         {
 399:           mapped = m.getAction(ks);
 400:           if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
 401:             mapped = m.getDefaultAction();
 402:           if (mapped == null)
 403:             m = m.getResolveParent();
 404:         }
 405: 
 406:       if (mapped == null)
 407:         return super.get(ks);
 408:       else
 409:         return mapped;
 410:     }
 411: 
 412:     public KeyStroke[] keys()
 413:     {
 414:       KeyStroke[] superKeys = super.keys();
 415:       KeyStroke[] mapKeys = map.getBoundKeyStrokes(); 
 416:       KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
 417:       for (int i = 0; i < superKeys.length; ++i)
 418:         bothKeys[i] = superKeys[i];
 419:       for (int i = 0; i < mapKeys.length; ++i)
 420:         bothKeys[i + superKeys.length] = mapKeys[i];
 421:       return bothKeys;
 422:     }
 423: 
 424:     public KeyStroke[] allKeys()
 425:     {
 426:       KeyStroke[] superKeys = super.allKeys();
 427:       KeyStroke[] mapKeys = map.getBoundKeyStrokes(); 
 428:       KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
 429:       for (int i = 0; i < superKeys.length; ++i)
 430:         bothKeys[i] = superKeys[i];
 431:       for (int i = 0; i < mapKeys.length; ++i)
 432:         bothKeys[i + superKeys.length] = mapKeys[i];
 433:       return bothKeys;
 434:     }
 435:   }
 436: 
 437:   private class KeymapActionMap extends ActionMap
 438:   {
 439:     Keymap map;
 440: 
 441:     public KeymapActionMap(Keymap k)
 442:     {
 443:       map = k;
 444:     }
 445: 
 446:     public Action get(Object cmd)
 447:     {
 448:       if (cmd instanceof Action)
 449:         return (Action) cmd;
 450:       else
 451:         return super.get(cmd);
 452:     }
 453: 
 454:     public int size()
 455:     {
 456:       return map.getBoundKeyStrokes().length + super.size();
 457:     }
 458: 
 459:     public Object[] keys() 
 460:     {
 461:       Object[] superKeys = super.keys();
 462:       Object[] mapKeys = map.getBoundKeyStrokes(); 
 463:       Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
 464:       for (int i = 0; i < superKeys.length; ++i)
 465:         bothKeys[i] = superKeys[i];
 466:       for (int i = 0; i < mapKeys.length; ++i)
 467:         bothKeys[i + superKeys.length] = mapKeys[i];
 468:       return bothKeys;      
 469:     }
 470: 
 471:     public Object[] allKeys()
 472:     {
 473:       Object[] superKeys = super.allKeys();
 474:       Object[] mapKeys = map.getBoundKeyStrokes(); 
 475:       Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
 476:       for (int i = 0; i < superKeys.length; ++i)
 477:         bothKeys[i] = superKeys[i];
 478:       for (int i = 0; i < mapKeys.length; ++i)
 479:         bothKeys[i + superKeys.length] = mapKeys[i];
 480:       return bothKeys;
 481:     }
 482: 
 483:   }
 484: 
 485:   static class DefaultKeymap implements Keymap
 486:   {
 487:     String name;
 488:     Keymap parent;
 489:     Hashtable map;
 490:     Action defaultAction;
 491: 
 492:     public DefaultKeymap(String name)
 493:     {
 494:       this.name = name;
 495:       this.map = new Hashtable();
 496:     }
 497: 
 498:     public void addActionForKeyStroke(KeyStroke key, Action a)
 499:     {
 500:       map.put(key, a);
 501:     }
 502: 
 503:     /**
 504:      * Looks up a KeyStroke either in the current map or the parent Keymap;
 505:      * does <em>not</em> return the default action if lookup fails.
 506:      *
 507:      * @param key The KeyStroke to look up an Action for.
 508:      *
 509:      * @return The mapping for <code>key</code>, or <code>null</code>
 510:      * if no mapping exists in this Keymap or any of its parents.
 511:      */
 512:     public Action getAction(KeyStroke key)
 513:     {
 514:       if (map.containsKey(key))
 515:         return (Action) map.get(key);
 516:       else if (parent != null)
 517:         return parent.getAction(key);
 518:       else
 519:         return null;
 520:     }
 521: 
 522:     public Action[] getBoundActions()
 523:     {
 524:       Action [] ret = new Action[map.size()];
 525:       Enumeration e = map.elements();
 526:       int i = 0;
 527:       while (e.hasMoreElements())
 528:         {
 529:           ret[i++] = (Action) e.nextElement();
 530:         }
 531:       return ret;
 532:     }
 533: 
 534:     public KeyStroke[] getBoundKeyStrokes()
 535:     {
 536:       KeyStroke [] ret = new KeyStroke[map.size()];
 537:       Enumeration e = map.keys();
 538:       int i = 0;
 539:       while (e.hasMoreElements())
 540:         {
 541:           ret[i++] = (KeyStroke) e.nextElement();
 542:         }
 543:       return ret;
 544:     }
 545: 
 546:     public Action getDefaultAction()
 547:     {
 548:       return defaultAction;
 549:     }
 550: 
 551:     public KeyStroke[] getKeyStrokesForAction(Action a)
 552:     {
 553:       int i = 0;
 554:       Enumeration e = map.keys();
 555:       while (e.hasMoreElements())
 556:         {
 557:           if (map.get(e.nextElement()).equals(a))
 558:             ++i;
 559:         }
 560:       KeyStroke [] ret = new KeyStroke[i];
 561:       i = 0;
 562:       e = map.keys();
 563:       while (e.hasMoreElements())
 564:         {          
 565:           KeyStroke k = (KeyStroke) e.nextElement();
 566:           if (map.get(k).equals(a))
 567:             ret[i++] = k;            
 568:         }
 569:       return ret;
 570:     }
 571: 
 572:     public String getName()
 573:     {
 574:       return name;
 575:     }
 576: 
 577:     public Keymap getResolveParent()
 578:     {
 579:       return parent;
 580:     }
 581: 
 582:     public boolean isLocallyDefined(KeyStroke key)
 583:     {
 584:       return map.containsKey(key);
 585:     }
 586: 
 587:     public void removeBindings()
 588:     {
 589:       map.clear();
 590:     }
 591: 
 592:     public void removeKeyStrokeBinding(KeyStroke key)
 593:     {
 594:       map.remove(key);
 595:     }
 596: 
 597:     public void setDefaultAction(Action a)
 598:     {
 599:       defaultAction = a;
 600:     }
 601: 
 602:     public void setResolveParent(Keymap p)
 603:     {
 604:       parent = p;
 605:     }
 606:   }
 607: 
 608:   class DefaultTransferHandler
 609:     extends TransferHandler
 610:   {
 611:     public boolean canImport(JComponent component, DataFlavor[] flavors)
 612:     {
 613:       JTextComponent textComponent = (JTextComponent) component;
 614:       
 615:       if (! (textComponent.isEnabled()
 616:          && textComponent.isEditable()
 617:          && flavors != null))
 618:         return false;
 619: 
 620:       for (int i = 0; i < flavors.length; ++i)
 621:     if (flavors[i].equals(DataFlavor.stringFlavor))
 622:        return true;
 623: 
 624:       return false;
 625:     }
 626:     
 627:     public void exportToClipboard(JComponent component, Clipboard clipboard,
 628:                   int action)
 629:     {
 630:       JTextComponent textComponent = (JTextComponent) component;
 631:       int start = textComponent.getSelectionStart();
 632:       int end = textComponent.getSelectionEnd();
 633: 
 634:       if (start == end)
 635:     return;
 636: 
 637:       try
 638:     {
 639:       // Copy text to clipboard.
 640:       String data = textComponent.getDocument().getText(start, end);
 641:       StringSelection selection = new StringSelection(data);
 642:       clipboard.setContents(selection, null);
 643: 
 644:       // Delete selected text on cut action.
 645:       if (action == MOVE)
 646:         doc.remove(start, end - start);
 647:     }
 648:       catch (BadLocationException e)
 649:     {
 650:       // Ignore this and do nothing.
 651:     }
 652:     }
 653:     
 654:     public int getSourceActions()
 655:     {
 656:       return NONE;
 657:     }
 658: 
 659:     public boolean importData(JComponent component, Transferable transferable)
 660:     {
 661:       DataFlavor flavor = null;
 662:       DataFlavor[] flavors = transferable.getTransferDataFlavors();
 663: 
 664:       if (flavors == null)
 665:     return false;
 666: 
 667:       for (int i = 0; i < flavors.length; ++i)
 668:     if (flavors[i].equals(DataFlavor.stringFlavor))
 669:        flavor = flavors[i];
 670:       
 671:       if (flavor == null)
 672:     return false;
 673: 
 674:       try
 675:     {
 676:       JTextComponent textComponent = (JTextComponent) component;
 677:       String data = (String) transferable.getTransferData(flavor);
 678:       textComponent.replaceSelection(data);
 679:       return true;
 680:     }
 681:       catch (IOException e)
 682:     {
 683:       // Ignored.
 684:     }
 685:       catch (UnsupportedFlavorException e)
 686:     {
 687:       // Ignored.
 688:     }
 689: 
 690:       return false;
 691:     }
 692:   }
 693: 
 694:   private static final long serialVersionUID = -8796518220218978795L;
 695:   
 696:   public static final String DEFAULT_KEYMAP = "default";
 697:   public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
 698:   
 699:   private static DefaultTransferHandler defaultTransferHandler;
 700:   private static Hashtable keymaps = new Hashtable();
 701:   private Keymap keymap;
 702:   private char focusAccelerator = '\0';
 703:   private NavigationFilter navigationFilter;
 704: 
 705:   private CaretBlinkTimer caretBlinkTimer;
 706: 
 707:   /**
 708:    * Get a Keymap from the global keymap table, by name.
 709:    *
 710:    * @param n The name of the Keymap to look up
 711:    *
 712:    * @return A Keymap associated with the provided name, or
 713:    * <code>null</code> if no such Keymap exists
 714:    *
 715:    * @see #addKeymap()
 716:    * @see #removeKeymap()
 717:    * @see #keymaps
 718:    */
 719:   public static Keymap getKeymap(String n)
 720:   {
 721:     return (Keymap) keymaps.get(n);
 722:   }
 723: 
 724:   /**
 725:    * Remove a Keymap from the global Keymap table, by name.
 726:    *
 727:    * @param n The name of the Keymap to remove
 728:    *
 729:    * @return The keymap removed from the global table
 730:    *
 731:    * @see #addKeymap()
 732:    * @see #getKeymap()
 733:    * @see #keymaps
 734:    */  
 735:   public static Keymap removeKeymap(String n)
 736:   {
 737:     Keymap km = (Keymap) keymaps.get(n);
 738:     keymaps.remove(n);
 739:     return km;
 740:   }
 741: 
 742:   /**
 743:    * Create a new Keymap with a specific name and parent, and add the new
 744:    * Keymap to the global keymap table. The name may be <code>null</code>,
 745:    * in which case the new Keymap will <em>not</em> be added to the global
 746:    * Keymap table. The parent may also be <code>null</code>, which is
 747:    * harmless.
 748:    * 
 749:    * @param n The name of the new Keymap, or <code>null</code>
 750:    * @param parent The parent of the new Keymap, or <code>null</code>
 751:    *
 752:    * @return The newly created Keymap
 753:    *
 754:    * @see #removeKeymap()
 755:    * @see #getKeymap()
 756:    * @see #keymaps
 757:    */
 758:   public static Keymap addKeymap(String n, Keymap parent)
 759:   {
 760:     Keymap k = new DefaultKeymap(n);
 761:     k.setResolveParent(parent);
 762:     if (n != null)
 763:       keymaps.put(n, k);
 764:     return k;
 765:   }
 766: 
 767:   /**
 768:    * Get the current Keymap of this component.
 769:    *
 770:    * @return The component's current Keymap
 771:    *
 772:    * @see #setKeymap()
 773:    * @see #keymap
 774:    */
 775:   public Keymap getKeymap() 
 776:   {
 777:     return keymap;
 778:   }
 779: 
 780:   /**
 781:    * Set the current Keymap of this component, installing appropriate
 782:    * {@link KeymapWrapper} and {@link KeymapActionMap} objects in the
 783:    * {@link InputMap} and {@link ActionMap} parent chains, respectively,
 784:    * and fire a property change event with name <code>"keymap"</code>.
 785:    *
 786:    * @see #getKeymap()
 787:    * @see #keymap
 788:    */
 789:   public void setKeymap(Keymap k) 
 790:   {
 791: 
 792:     // phase 1: replace the KeymapWrapper entry in the InputMap chain.
 793:     // the goal here is to always maintain the following ordering:
 794:     //
 795:     //   [InputMap]? -> [KeymapWrapper]? -> [InputMapUIResource]*
 796:     // 
 797:     // that is to say, component-specific InputMaps need to remain children
 798:     // of Keymaps, and Keymaps need to remain children of UI-installed
 799:     // InputMaps (and the order of each group needs to be preserved, of
 800:     // course).
 801:     
 802:     KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
 803:     InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
 804:     if (childInputMap == null)
 805:       setInputMap(JComponent.WHEN_FOCUSED, kw);
 806:     else
 807:       {
 808:         while (childInputMap.getParent() != null 
 809:                && !(childInputMap.getParent() instanceof KeymapWrapper)
 810:                && !(childInputMap.getParent() instanceof InputMapUIResource))
 811:           childInputMap = childInputMap.getParent();
 812: 
 813:         // option 1: there is nobody to replace at the end of the chain
 814:         if (childInputMap.getParent() == null)
 815:           childInputMap.setParent(kw);
 816:         
 817:         // option 2: there is already a KeymapWrapper in the chain which
 818:         // needs replacing (possibly with its own parents, possibly without)
 819:         else if (childInputMap.getParent() instanceof KeymapWrapper)
 820:           {
 821:             if (kw == null)
 822:               childInputMap.setParent(childInputMap.getParent().getParent());
 823:             else
 824:               {
 825:                 kw.setParent(childInputMap.getParent().getParent());
 826:                 childInputMap.setParent(kw);
 827:               }
 828:           }
 829: 
 830:         // option 3: there is an InputMapUIResource in the chain, which marks
 831:         // the place where we need to stop and insert ourselves
 832:         else if (childInputMap.getParent() instanceof InputMapUIResource)
 833:           {
 834:             if (kw != null)
 835:               {
 836:                 kw.setParent(childInputMap.getParent());
 837:                 childInputMap.setParent(kw);
 838:               }
 839:           }
 840:       }
 841: 
 842:     // phase 2: replace the KeymapActionMap entry in the ActionMap chain
 843: 
 844:     KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
 845:     ActionMap childActionMap = getActionMap();
 846:     if (childActionMap == null)
 847:       setActionMap(kam);
 848:     else
 849:       {
 850:         while (childActionMap.getParent() != null 
 851:                && !(childActionMap.getParent() instanceof KeymapActionMap)
 852:                && !(childActionMap.getParent() instanceof ActionMapUIResource))
 853:           childActionMap = childActionMap.getParent();
 854: 
 855:         // option 1: there is nobody to replace at the end of the chain
 856:         if (childActionMap.getParent() == null)
 857:           childActionMap.setParent(kam);
 858:         
 859:         // option 2: there is already a KeymapActionMap in the chain which
 860:         // needs replacing (possibly with its own parents, possibly without)
 861:         else if (childActionMap.getParent() instanceof KeymapActionMap)
 862:           {
 863:             if (kam == null)
 864:               childActionMap.setParent(childActionMap.getParent().getParent());
 865:             else
 866:               {
 867:                 kam.setParent(childActionMap.getParent().getParent());
 868:                 childActionMap.setParent(kam);
 869:               }
 870:           }
 871: 
 872:         // option 3: there is an ActionMapUIResource in the chain, which marks
 873:         // the place where we need to stop and insert ourselves
 874:         else if (childActionMap.getParent() instanceof ActionMapUIResource)
 875:           {
 876:             if (kam != null)
 877:               {
 878:                 kam.setParent(childActionMap.getParent());
 879:                 childActionMap.setParent(kam);
 880:               }
 881:           }
 882:       }
 883: 
 884:     // phase 3: update the explicit keymap field
 885: 
 886:     Keymap old = keymap;
 887:     keymap = k;
 888:     firePropertyChange("keymap", old, k);
 889:   }
 890: 
 891:   /**
 892:    * Resolves a set of bindings against a set of actions and inserts the
 893:    * results into a {@link Keymap}. Specifically, for each provided binding
 894:    * <code>b</code>, if there exists a provided action <code>a</code> such
 895:    * that <code>a.getValue(Action.NAME) == b.ActionName</code> then an
 896:    * entry is added to the Keymap mapping <code>b</code> to
 897:    * <code>a</code>.
 898:    *
 899:    * @param map The Keymap to add new mappings to
 900:    * @param bindings The set of bindings to add to the Keymap
 901:    * @param actions The set of actions to resolve binding names against
 902:    *
 903:    * @see Action#NAME
 904:    * @see Action#getValue()
 905:    * @see KeyBinding#ActionName
 906:    */
 907:   public static void loadKeymap(Keymap map, 
 908:                                 JTextComponent.KeyBinding[] bindings, 
 909:                                 Action[] actions)
 910:   {
 911:     Hashtable acts = new Hashtable(actions.length);
 912:     for (int i = 0; i < actions.length; ++i)
 913:       acts.put(actions[i].getValue(Action.NAME), actions[i]);
 914:     for (int i = 0; i < bindings.length; ++i)
 915:       if (acts.containsKey(bindings[i].actionName))
 916:         map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
 917:   }
 918: 
 919:   /**
 920:    * Returns the set of available Actions this component's associated
 921:    * editor can run.  Equivalent to calling
 922:    * <code>getUI().getEditorKit().getActions()</code>. This set of Actions
 923:    * is a reasonable value to provide as a parameter to {@link
 924:    * #loadKeymap()}, when resolving a set of {@link #KeyBinding} objects
 925:    * against this component.
 926:    *
 927:    * @return The set of available Actions on this component's {@link EditorKit}
 928:    *
 929:    * @see TextUI#getEditorKit()
 930:    * @see EditorKit#getActions()
 931:    */
 932:   public Action[] getActions()
 933:   {
 934:     return getUI().getEditorKit(this).getActions();
 935:   }
 936:     
 937:   // These are package-private to avoid an accessor method.
 938:   Document doc;
 939:   Caret caret;
 940:   boolean editable;
 941:   
 942:   private Highlighter highlighter;
 943:   private Color caretColor;
 944:   private Color disabledTextColor;
 945:   private Color selectedTextColor;
 946:   private Color selectionColor;
 947:   private Insets margin;
 948:   private boolean dragEnabled;
 949: 
 950:   /**
 951:    * Creates a new <code>JTextComponent</code> instance.
 952:    */
 953:   public JTextComponent()
 954:   {
 955:     Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
 956:     boolean creatingKeymap = false;
 957:     if (defkeymap == null)
 958:       {
 959:         defkeymap = addKeymap(DEFAULT_KEYMAP, null);
 960:         defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
 961:         creatingKeymap = true;
 962:       }
 963: 
 964:     caretBlinkTimer = new CaretBlinkTimer();
 965: 
 966:     setFocusable(true);
 967:     setEditable(true);
 968:     enableEvents(AWTEvent.KEY_EVENT_MASK);
 969:     updateUI();
 970:     
 971:     // need to do this after updateUI()
 972:     if (creatingKeymap)
 973:       loadKeymap(defkeymap, 
 974:                  new KeyBinding[] { 
 975:                    new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
 976:                                   DefaultEditorKit.backwardAction),
 977:                    new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
 978:                                   DefaultEditorKit.forwardAction),
 979:                    new KeyBinding(KeyStroke.getKeyStroke("typed \b"),
 980:                                   DefaultEditorKit.deletePrevCharAction),
 981:                    new KeyBinding(KeyStroke.getKeyStroke("typed \u007f"),
 982:                                   DefaultEditorKit.deleteNextCharAction)                   
 983:                  },
 984:                  getActions());
 985:   }
 986: 
 987:   public void setDocument(Document newDoc)
 988:   {
 989:     Document oldDoc = doc;
 990:     doc = newDoc;
 991:     firePropertyChange("document", oldDoc, newDoc);
 992:     revalidate();
 993:     repaint();
 994:   }
 995: 
 996:   public Document getDocument()
 997:   {
 998:     return doc;
 999:   }
1000: 
1001:   /**
1002:    * Get the <code>AccessibleContext</code> of this object.
1003:    *
1004:    * @return an <code>AccessibleContext</code> object
1005:    */
1006:   public AccessibleContext getAccessibleContext()
1007:   {
1008:     return null;
1009:   }
1010: 
1011:   public void setMargin(Insets m)
1012:   {
1013:     margin = m;
1014:   }
1015: 
1016:   public Insets getMargin()
1017:   {
1018:     return margin;
1019:   }
1020: 
1021:   public void setText(String text)
1022:   {
1023:     try
1024:       {
1025:     doc.remove(0, doc.getLength());
1026:     doc.insertString(0, text, null);
1027:       }
1028:     catch (BadLocationException e)
1029:       {
1030:     // This can never happen.
1031:       }
1032:   }
1033: 
1034:   /**
1035:    * Retrieves the current text in this text document.
1036:    *
1037:    * @return the text
1038:    *
1039:    * @exception NullPointerException if the underlaying document is null
1040:    */
1041:   public String getText()
1042:   {
1043:     if (doc == null)
1044:       return null;
1045: 
1046:     try
1047:       {
1048:     return doc.getText(0, doc.getLength());
1049:       }
1050:     catch (BadLocationException e)
1051:       {
1052:     // This should never happen.
1053:     return "";
1054:       }
1055:   }
1056: 
1057:   /**
1058:    * Retrieves a part of the current text in this document.
1059:    *
1060:    * @param offset the postion of the first character
1061:    * @param length the length of the text to retrieve
1062:    *
1063:    * @return the text
1064:    *
1065:    * @exception BadLocationException if arguments do not hold pre-conditions
1066:    */
1067:   public String getText(int offset, int length)
1068:     throws BadLocationException
1069:   {
1070:     return getDocument().getText(offset, length);
1071:   }
1072: 
1073:   /**
1074:    * Retrieves the currently selected text in this text document.
1075:    *
1076:    * @return the selected text
1077:    *
1078:    * @exception NullPointerException if the underlaying document is null
1079:    */
1080:   public String getSelectedText()
1081:   {
1082:     try
1083:       {
1084:     return doc.getText(getSelectionStart(), getSelectionEnd());
1085:       }
1086:     catch (BadLocationException e)
1087:       {
1088:     // This should never happen.
1089:     return null;
1090:       }
1091:   }
1092: 
1093:   /**
1094:    * Returns a string that specifies the name of the Look and Feel class
1095:    * that renders this component.
1096:    *
1097:    * @return the string "TextComponentUI"
1098:    */
1099:   public String getUIClassID()
1100:   {
1101:     return "TextComponentUI";
1102:   }
1103: 
1104:   /**
1105:    * Returns a string representation of this JTextComponent.
1106:    */
1107:   protected String paramString()
1108:   {
1109:     return "JTextComponent";
1110:   }
1111: 
1112:   /**
1113:    * This method returns the label's UI delegate.
1114:    *
1115:    * @return The label's UI delegate.
1116:    */
1117:   public TextUI getUI()
1118:   {
1119:     return (TextUI) ui;
1120:   }
1121: 
1122:   /**
1123:    * This method sets the label's UI delegate.
1124:    *
1125:    * @param ui The label's UI delegate.
1126:    */
1127:   public void setUI(TextUI newUI)
1128:   {
1129:     super.setUI(newUI);
1130:   }
1131: 
1132:   /**
1133:    * This method resets the label's UI delegate to the default UI for the
1134:    * current look and feel.
1135:    */
1136:   public void updateUI()
1137:   {
1138:     setUI((TextUI) UIManager.getUI(this));
1139:   }
1140: 
1141:   public Dimension getPreferredScrollableViewportSize()
1142:   {
1143:     return getPreferredSize();
1144:   }
1145: 
1146:   public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1147:                                         int direction)
1148:   {
1149:     // We return 1/10 of the visible area as documented in Sun's API docs.
1150:     if (orientation == SwingConstants.HORIZONTAL)
1151:       return visible.width / 10;
1152:     else if (orientation == SwingConstants.VERTICAL)
1153:       return visible.height / 10;
1154:     else
1155:       throw new IllegalArgumentException("orientation must be either "
1156:                                       + "javax.swing.SwingConstants.VERTICAL "
1157:                                       + "or "
1158:                                       + "javax.swing.SwingConstants.HORIZONTAL"
1159:                                          );
1160:   }
1161: 
1162:   public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1163:                                          int direction)
1164:   {
1165:     // We return the whole visible area as documented in Sun's API docs.
1166:     if (orientation == SwingConstants.HORIZONTAL)
1167:       return visible.width;
1168:     else if (orientation == SwingConstants.VERTICAL)
1169:       return visible.height;
1170:     else
1171:       throw new IllegalArgumentException("orientation must be either "
1172:                                       + "javax.swing.SwingConstants.VERTICAL "
1173:                                       + "or "
1174:                                       + "javax.swing.SwingConstants.HORIZONTAL"
1175:                                          );
1176:   }
1177: 
1178:   /**
1179:    * Checks whether this text component it editable.
1180:    *
1181:    * @return true if editable, false otherwise
1182:    */
1183:   public boolean isEditable()
1184:   {
1185:     return editable;
1186:   }
1187: 
1188:   /**
1189:    * Enables/disabled this text component's editability.
1190:    *
1191:    * @param newValue true to make it editable, false otherwise.
1192:    */
1193:   public void setEditable(boolean newValue)
1194:   {
1195:     if (editable == newValue)
1196:       return;
1197: 
1198:     if (newValue == true)
1199:       caretBlinkTimer.start();
1200:     else
1201:       {
1202:         caretBlinkTimer.stop();
1203:         caret.setVisible(false);
1204:       }
1205: 
1206:     boolean oldValue = editable;
1207:     editable = newValue;
1208:     firePropertyChange("editable", oldValue, newValue);
1209:   }
1210: 
1211:   /**
1212:    * The <code>Caret</code> object used in this text component.
1213:    *
1214:    * @return the caret object
1215:    */
1216:   public Caret getCaret()
1217:   {
1218:     return caret;
1219:   }
1220: 
1221:   /**
1222:    * Sets a new <code>Caret</code> for this text component.
1223:    *
1224:    * @param newCaret the new <code>Caret</code> to set
1225:    */
1226:   public void setCaret(Caret newCaret)
1227:   {
1228:     if (caret != null)
1229:       caret.deinstall(this);
1230:     
1231:     Caret oldCaret = caret;
1232:     caret = newCaret;
1233: 
1234:     caretBlinkTimer.update();
1235: 
1236:     if (caret != null)
1237:       caret.install(this);
1238:     
1239:     firePropertyChange("caret", oldCaret, newCaret);
1240:   }
1241: 
1242:   public Color getCaretColor()
1243:   {
1244:     return caretColor;
1245:   }
1246: 
1247:   public void setCaretColor(Color newColor)
1248:   {
1249:     Color oldCaretColor = caretColor;
1250:     caretColor = newColor;
1251:     firePropertyChange("caretColor", oldCaretColor, newColor);
1252:   }
1253: 
1254:   public Color getDisabledTextColor()
1255:   {
1256:     return disabledTextColor;
1257:   }
1258: 
1259:   public void setDisabledTextColor(Color newColor)
1260:   {
1261:     Color oldColor = disabledTextColor;
1262:     disabledTextColor = newColor;
1263:     firePropertyChange("disabledTextColor", oldColor, newColor);
1264:   }
1265: 
1266:   public Color getSelectedTextColor()
1267:   {
1268:     return selectedTextColor;
1269:   }
1270: 
1271:   public void setSelectedTextColor(Color newColor)
1272:   {
1273:     Color oldColor = selectedTextColor;
1274:     selectedTextColor = newColor;
1275:     firePropertyChange("selectedTextColor", oldColor, newColor);
1276:   }
1277: 
1278:   public Color getSelectionColor()
1279:   {
1280:     return selectionColor;
1281:   }
1282: 
1283:   public void setSelectionColor(Color newColor)
1284:   {
1285:     Color oldColor = selectionColor;
1286:     selectionColor = newColor;
1287:     firePropertyChange("selectionColor", oldColor, newColor);
1288:   }
1289: 
1290:   /**
1291:    * Retrisves the current caret position.
1292:    *
1293:    * @return the current position
1294:    */
1295:   public int getCaretPosition()
1296:   {
1297:     return caret.getDot();
1298:   }
1299: 
1300:   /**
1301:    * Sets the caret to a new position.
1302:    *
1303:    * @param position the new position
1304:    */
1305:   public void setCaretPosition(int position)
1306:   {
1307:     if (doc == null)
1308:       return;
1309: 
1310:     if (position < 0 || position > doc.getLength())
1311:       throw new IllegalArgumentException();
1312: 
1313:     caret.setDot(position);
1314:   }
1315: 
1316:   /**
1317:    * Moves the caret to a given position. This selects the text between
1318:    * the old and the new position of the caret.
1319:    */
1320:   public void moveCaretPosition(int position)
1321:   {
1322:     if (doc == null)
1323:       return;
1324: 
1325:     if (position < 0 || position > doc.getLength())
1326:       throw new IllegalArgumentException();
1327: 
1328:     caret.moveDot(position);
1329:   }
1330: 
1331:   public Highlighter getHighlighter()
1332:   {
1333:     return highlighter;
1334:   }
1335: 
1336:   public void setHighlighter(Highlighter newHighlighter)
1337:   {
1338:     if (highlighter != null)
1339:       highlighter.deinstall(this);
1340:     
1341:     Highlighter oldHighlighter = highlighter;
1342:     highlighter = newHighlighter;
1343: 
1344:     if (highlighter != null)
1345:       highlighter.install(this);
1346:     
1347:     firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1348:   }
1349: 
1350:   /**
1351:    * Returns the start postion of the currently selected text.
1352:    *
1353:    * @return the start postion
1354:    */
1355:   public int getSelectionStart()
1356:   {
1357:     return Math.min(caret.getDot(), caret.getMark());
1358:   }
1359: 
1360:   /**
1361:    * Selects the text from the given postion to the selection end position.
1362:    *
1363:    * @param end the start positon of the selected text.
1364:    */
1365:   public void setSelectionStart(int start)
1366:   {
1367:     select(start, getSelectionEnd());
1368:   }
1369: 
1370:   /**
1371:    * Returns the end postion of the currently selected text.
1372:    *
1373:    * @return the end postion
1374:    */
1375:   public int getSelectionEnd()
1376:   {
1377:     return Math.max(caret.getDot(), caret.getMark());
1378:   }
1379: 
1380:   /**
1381:    * Selects the text from the selection start postion to the given position.
1382:    *
1383:    * @param end the end positon of the selected text.
1384:    */
1385:   public void setSelectionEnd(int end)
1386:   {
1387:     select(getSelectionStart(), end);
1388:   }
1389: 
1390:   /**
1391:    * Selects a part of the content of the text component.
1392:    *
1393:    * @param start the start position of the selected text
1394:    * @param ent the end position of the selected text
1395:    */
1396:   public void select(int start, int end)
1397:   {
1398:     int length = doc.getLength();
1399:     
1400:     start = Math.max(start, 0);
1401:     start = Math.min(start, length);
1402: 
1403:     end = Math.max(end, 0);
1404:     end = Math.min(end, length);
1405: 
1406:     setCaretPosition(start);
1407:     moveCaretPosition(end);
1408:   }
1409: 
1410:   /**
1411:    * Selects the whole content of the text component.
1412:    */
1413:   public void selectAll()
1414:   {
1415:     select(0, doc.getLength());
1416:   }
1417: 
1418:   public synchronized void replaceSelection(String content)
1419:   {
1420:     int dot = caret.getDot();
1421:     int mark = caret.getMark();
1422: 
1423:     // If content is empty delete selection.
1424:     if (content == null)
1425:       {
1426:     caret.setDot(dot);
1427:     return;
1428:       }
1429: 
1430:     try
1431:       {
1432:     int start = getSelectionStart();
1433:     int end = getSelectionEnd();
1434:     
1435:     // Remove selected text.
1436:     if (dot != mark)
1437:       doc.remove(start, end - start);
1438: 
1439:     // Insert new text.
1440:     doc.insertString(start, content, null);
1441: 
1442:     // Set dot to new position.
1443:     setCaretPosition(start + content.length());
1444:       }
1445:     catch (BadLocationException e)
1446:       {
1447:     // This should never happen.
1448:       }
1449:   }
1450: 
1451:   public boolean getScrollableTracksViewportHeight()
1452:   {
1453:     if (getParent() instanceof JViewport)
1454:       return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1455: 
1456:     return false;
1457:   }
1458: 
1459:   public boolean getScrollableTracksViewportWidth()
1460:   {
1461:     if (getParent() instanceof JViewport)
1462:       return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1463: 
1464:     return false;
1465:   }
1466: 
1467:   /**
1468:    * Adds a <code>CaretListener</code> object to this text component.
1469:    *
1470:    * @param listener the listener to add
1471:    */
1472:   public void addCaretListener(CaretListener listener)
1473:   {
1474:     listenerList.add(CaretListener.class, listener);
1475:   }
1476: 
1477:   /**
1478:    * Removed a <code>CaretListener</code> object from this text component.
1479:    *
1480:    * @param listener the listener to remove
1481:    */
1482:   public void removeCaretListener(CaretListener listener)
1483:   {
1484:     listenerList.remove(CaretListener.class, listener);
1485:   }
1486: 
1487:   /**
1488:    * Returns all added <code>CaretListener</code> objects.
1489:    *
1490:    * @return an array of listeners
1491:    */
1492:   public CaretListener[] getCaretListeners()
1493:   {
1494:     return (CaretListener[]) getListeners(CaretListener.class);
1495:   }
1496: 
1497:   /**
1498:    * Notifies all registered <code>CaretListener</code> objects that the caret
1499:    * was updated.
1500:    *
1501:    * @param event the event to send
1502:    */
1503:   protected void fireCaretUpdate(CaretEvent event)
1504:   {
1505:     CaretListener[] listeners = getCaretListeners();
1506: 
1507:     for (int index = 0; index < listeners.length; ++index)
1508:       listeners[index].caretUpdate(event);
1509:   }
1510: 
1511:   /**
1512:    * Adds an <code>InputListener</code> object to this text component.
1513:    *
1514:    * @param listener the listener to add
1515:    */
1516:   public void addInputMethodListener(InputMethodListener listener)
1517:   {
1518:     listenerList.add(InputMethodListener.class, listener);
1519:   }
1520: 
1521:   /**
1522:    * Removes an <code>InputListener</code> object from this text component.
1523:    *
1524:    * @param listener the listener to remove
1525:    */
1526:   public void removeInputMethodListener(InputMethodListener listener)
1527:   {
1528:     listenerList.remove(InputMethodListener.class, listener);
1529:   }
1530: 
1531:   /**
1532:    * Returns all added <code>InputMethodListener</code> objects.
1533:    *
1534:    * @return an array of listeners
1535:    */
1536:   public InputMethodListener[] getInputMethodListeners()
1537:   {
1538:     return (InputMethodListener[]) getListeners(InputMethodListener.class);
1539:   }
1540: 
1541:   public Rectangle modelToView(int position) throws BadLocationException
1542:   {
1543:     return getUI().modelToView(this, position);
1544:   }
1545: 
1546:   public boolean getDragEnabled()
1547:   {
1548:     return dragEnabled;
1549:   }
1550: 
1551:   public void setDragEnabled(boolean enabled)
1552:   {
1553:     dragEnabled = enabled;
1554:   }
1555: 
1556:   public int viewToModel(Point pt)
1557:   {
1558:     return getUI().viewToModel(this, pt);
1559:   }
1560: 
1561:   public void copy()
1562:   {
1563:     doTransferAction("copy", TransferHandler.getCopyAction());
1564:   }
1565: 
1566:   public void cut()
1567:   {
1568:     doTransferAction("cut", TransferHandler.getCutAction());
1569:   }
1570: 
1571:   public void paste()
1572:   {
1573:     doTransferAction("paste", TransferHandler.getPasteAction());
1574:   }
1575: 
1576:   private void doTransferAction(String name, Action action)
1577:   {
1578:     // Install default TransferHandler if none set.
1579:     if (getTransferHandler() == null)
1580:       {
1581:     if (defaultTransferHandler == null)
1582:       defaultTransferHandler = new DefaultTransferHandler();
1583:     
1584:     setTransferHandler(defaultTransferHandler);
1585:       }
1586: 
1587:     // Perform action.
1588:     ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1589:                     action.getValue(Action.NAME).toString());
1590:     action.actionPerformed(event);
1591:   }
1592: 
1593:   public void setFocusAccelerator(char newKey)
1594:   {
1595:     if (focusAccelerator == newKey)
1596:       return;
1597: 
1598:     char oldKey = focusAccelerator;
1599:     focusAccelerator = newKey;
1600:     firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1601:   }
1602:   
1603:   public char getFocusAccelerator()
1604:   {
1605:     return focusAccelerator;
1606:   }
1607: 
1608:   /**
1609:    * @since 1.4
1610:    */
1611:   public NavigationFilter getNavigationFilter()
1612:   {
1613:     return navigationFilter;
1614:   }
1615: 
1616:   /**
1617:    * @since 1.4
1618:    */
1619:   public void setNavigationFilter(NavigationFilter filter)
1620:   {
1621:     navigationFilter = filter;
1622:   }
1623:   
1624:   /**
1625:    * Read and set the content this component. If not overridden, the
1626:    * method reads the component content as a plain text.
1627:    *
1628:    * The second parameter of this method describes the input stream. It can
1629:    * be String, URL, File and so on. If not null, this object is added to
1630:    * the properties of the associated document under the key
1631:    * {@link Document#StreamDescriptionProperty}.
1632:    *
1633:    * @param input an input stream to read from.
1634:    * @param streamDescription an object, describing the stream.
1635:    *
1636:    * @throws IOException if the reader throws it.
1637:    *
1638:    * @see getDocument()
1639:    * @see Document#getProperty(Object)
1640:    */
1641:   public void read(Reader input, Object streamDescription)
1642:             throws IOException
1643:   {
1644:     if (streamDescription != null)
1645:       {
1646:         Document d = getDocument();
1647:         if (d != null)
1648:           d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1649:       }
1650: 
1651:     StringBuffer b = new StringBuffer();
1652:     int c;
1653: 
1654:     // Read till -1 (EOF).
1655:     while ((c = input.read()) >= 0)
1656:       b.append((char) c);
1657: 
1658:     setText(b.toString());
1659:   }
1660: 
1661:   /**
1662:    * Write the content of this component to the given stream. If not
1663:    * overridden, the method writes the component content as a plain text.
1664:    *
1665:    * @param output the writer to write into.
1666:    *
1667:    * @throws IOException if the writer throws it.
1668:    */
1669:   public void write(Writer output)
1670:              throws IOException
1671:   {
1672:     output.write(getText());
1673:   }  
1674: }