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