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