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