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