1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.datatransfer.*;
  30 import java.beans.*;
  31 import java.awt.event.ActionEvent;
  32 import java.awt.event.ActionListener;
  33 import java.io.*;
  34 import javax.swing.*;
  35 import javax.swing.event.*;
  36 import javax.swing.plaf.*;
  37 import java.util.EventListener;
  38 import sun.swing.SwingUtilities2;
  39 
  40 /**
  41  * A default implementation of Caret.  The caret is rendered as
  42  * a vertical line in the color specified by the CaretColor property
  43  * of the associated JTextComponent.  It can blink at the rate specified
  44  * by the BlinkRate property.
  45  * <p>
  46  * This implementation expects two sources of asynchronous notification.
  47  * The timer thread fires asynchronously, and causes the caret to simply
  48  * repaint the most recent bounding box.  The caret also tracks change
  49  * as the document is modified.  Typically this will happen on the
  50  * event dispatch thread as a result of some mouse or keyboard event.
  51  * The caret behavior on both synchronous and asynchronous documents updates
  52  * is controlled by <code>UpdatePolicy</code> property. The repaint of the
  53  * new caret location will occur on the event thread in any case, as calls to
  54  * <code>modelToView</code> are only safe on the event thread.
  55  * <p>
  56  * The caret acts as a mouse and focus listener on the text component
  57  * it has been installed in, and defines the caret semantics based upon
  58  * those events.  The listener methods can be reimplemented to change the
  59  * semantics.
  60  * By default, the first mouse button will be used to set focus and caret
  61  * position.  Dragging the mouse pointer with the first mouse button will
  62  * sweep out a selection that is contiguous in the model.  If the associated
  63  * text component is editable, the caret will become visible when focus
  64  * is gained, and invisible when focus is lost.
  65  * <p>
  66  * The Highlighter bound to the associated text component is used to
  67  * render the selection by default.
  68  * Selection appearance can be customized by supplying a
  69  * painter to use for the highlights.  By default a painter is used that
  70  * will render a solid color as specified in the associated text component
  71  * in the <code>SelectionColor</code> property.  This can easily be changed
  72  * by reimplementing the
  73  * {@link #getSelectionPainter getSelectionPainter}
  74  * method.
  75  * <p>
  76  * A customized caret appearance can be achieved by reimplementing
  77  * the paint method.  If the paint method is changed, the damage method
  78  * should also be reimplemented to cause a repaint for the area needed
  79  * to render the caret.  The caret extends the Rectangle class which
  80  * is used to hold the bounding box for where the caret was last rendered.
  81  * This enables the caret to repaint in a thread-safe manner when the
  82  * caret moves without making a call to modelToView which is unstable
  83  * between model updates and view repair (i.e. the order of delivery
  84  * to DocumentListeners is not guaranteed).
  85  * <p>
  86  * The magic caret position is set to null when the caret position changes.
  87  * A timer is used to determine the new location (after the caret change).
  88  * When the timer fires, if the magic caret position is still null it is
  89  * reset to the current caret position. Any actions that change
  90  * the caret position and want the magic caret position to remain the
  91  * same, must remember the magic caret position, change the cursor, and
  92  * then set the magic caret position to its original value. This has the
  93  * benefit that only actions that want the magic caret position to persist
  94  * (such as open/down) need to know about it.
  95  * <p>
  96  * <strong>Warning:</strong>
  97  * Serialized objects of this class will not be compatible with
  98  * future Swing releases. The current serialization support is
  99  * appropriate for short term storage or RMI between applications running
 100  * the same version of Swing.  As of 1.4, support for long term storage
 101  * of all JavaBeans&trade;
 102  * has been added to the <code>java.beans</code> package.
 103  * Please see {@link java.beans.XMLEncoder}.
 104  *
 105  * @author  Timothy Prinzing
 106  * @see     Caret
 107  */
 108 @SuppressWarnings("serial") // Same-version serialization only
 109 public class DefaultCaret extends Rectangle implements Caret, FocusListener, MouseListener, MouseMotionListener {
 110 
 111     /**
 112      * Indicates that the caret position is to be updated only when
 113      * document changes are performed on the Event Dispatching Thread.
 114      * @see #setUpdatePolicy
 115      * @see #getUpdatePolicy
 116      * @since 1.5
 117      */
 118     public static final int UPDATE_WHEN_ON_EDT = 0;
 119 
 120     /**
 121      * Indicates that the caret should remain at the same
 122      * absolute position in the document regardless of any document
 123      * updates, except when the document length becomes less than
 124      * the current caret position due to removal. In that case the caret
 125      * position is adjusted to the end of the document.
 126      *
 127      * @see #setUpdatePolicy
 128      * @see #getUpdatePolicy
 129      * @since 1.5
 130      */
 131     public static final int NEVER_UPDATE = 1;
 132 
 133     /**
 134      * Indicates that the caret position is to be <b>always</b>
 135      * updated accordingly to the document changes regardless whether
 136      * the document updates are performed on the Event Dispatching Thread
 137      * or not.
 138      *
 139      * @see #setUpdatePolicy
 140      * @see #getUpdatePolicy
 141      * @since 1.5
 142      */
 143     public static final int ALWAYS_UPDATE = 2;
 144 
 145     /**
 146      * Constructs a default caret.
 147      */
 148     public DefaultCaret() {
 149     }
 150 
 151     /**
 152      * Sets the caret movement policy on the document updates. Normally
 153      * the caret updates its absolute position within the document on
 154      * insertions occurred before or at the caret position and
 155      * on removals before the caret position. 'Absolute position'
 156      * means here the position relative to the start of the document.
 157      * For example if
 158      * a character is typed within editable text component it is inserted
 159      * at the caret position and the caret moves to the next absolute
 160      * position within the document due to insertion and if
 161      * <code>BACKSPACE</code> is typed then caret decreases its absolute
 162      * position due to removal of a character before it. Sometimes
 163      * it may be useful to turn off the caret position updates so that
 164      * the caret stays at the same absolute position within the
 165      * document position regardless of any document updates.
 166      * <p>
 167      * The following update policies are allowed:
 168      * <ul>
 169      *   <li><code>NEVER_UPDATE</code>: the caret stays at the same
 170      *       absolute position in the document regardless of any document
 171      *       updates, except when document length becomes less than
 172      *       the current caret position due to removal. In that case caret
 173      *       position is adjusted to the end of the document.
 174      *       The caret doesn't try to keep itself visible by scrolling
 175      *       the associated view when using this policy. </li>
 176      *   <li><code>ALWAYS_UPDATE</code>: the caret always tracks document
 177      *       changes. For regular changes it increases its position
 178      *       if an insertion occurs before or at its current position,
 179      *       and decreases position if a removal occurs before
 180      *       its current position. For undo/redo updates it is always
 181      *       moved to the position where update occurred. The caret
 182      *       also tries to keep itself visible by calling
 183      *       <code>adjustVisibility</code> method.</li>
 184      *   <li><code>UPDATE_WHEN_ON_EDT</code>: acts like <code>ALWAYS_UPDATE</code>
 185      *       if the document updates are performed on the Event Dispatching Thread
 186      *       and like <code>NEVER_UPDATE</code> if updates are performed on
 187      *       other thread. </li>
 188      * </ul> <p>
 189      * The default property value is <code>UPDATE_WHEN_ON_EDT</code>.
 190      *
 191      * @param policy one of the following values : <code>UPDATE_WHEN_ON_EDT</code>,
 192      * <code>NEVER_UPDATE</code>, <code>ALWAYS_UPDATE</code>
 193      * @throws IllegalArgumentException if invalid value is passed
 194      *
 195      * @see #getUpdatePolicy
 196      * @see #adjustVisibility
 197      * @see #UPDATE_WHEN_ON_EDT
 198      * @see #NEVER_UPDATE
 199      * @see #ALWAYS_UPDATE
 200      *
 201      * @since 1.5
 202      */
 203     public void setUpdatePolicy(int policy) {
 204         updatePolicy = policy;
 205     }
 206 
 207     /**
 208      * Gets the caret movement policy on document updates.
 209      *
 210      * @return one of the following values : <code>UPDATE_WHEN_ON_EDT</code>,
 211      * <code>NEVER_UPDATE</code>, <code>ALWAYS_UPDATE</code>
 212      *
 213      * @see #setUpdatePolicy
 214      * @see #UPDATE_WHEN_ON_EDT
 215      * @see #NEVER_UPDATE
 216      * @see #ALWAYS_UPDATE
 217      *
 218      * @since 1.5
 219      */
 220     public int getUpdatePolicy() {
 221         return updatePolicy;
 222     }
 223 
 224     /**
 225      * Gets the text editor component that this caret is
 226      * is bound to.
 227      *
 228      * @return the component
 229      */
 230     protected final JTextComponent getComponent() {
 231         return component;
 232     }
 233 
 234     /**
 235      * Cause the caret to be painted.  The repaint
 236      * area is the bounding box of the caret (i.e.
 237      * the caret rectangle or <em>this</em>).
 238      * <p>
 239      * This method is thread safe, although most Swing methods
 240      * are not. Please see
 241      * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency
 242      * in Swing</A> for more information.
 243      */
 244     protected final synchronized void repaint() {
 245         if (component != null) {
 246             component.repaint(x, y, width, height);
 247         }
 248     }
 249 
 250     /**
 251      * Damages the area surrounding the caret to cause
 252      * it to be repainted in a new location.  If paint()
 253      * is reimplemented, this method should also be
 254      * reimplemented.  This method should update the
 255      * caret bounds (x, y, width, and height).
 256      *
 257      * @param r  the current location of the caret
 258      * @see #paint
 259      */
 260     protected synchronized void damage(Rectangle r) {
 261         if (r != null) {
 262             int damageWidth = getCaretWidth(r.height);
 263             x = r.x - 4 - (damageWidth >> 1);
 264             y = r.y;
 265             width = 9 + damageWidth;
 266             height = r.height;
 267             repaint();
 268         }
 269     }
 270 
 271     /**
 272      * Scrolls the associated view (if necessary) to make
 273      * the caret visible.  Since how this should be done
 274      * is somewhat of a policy, this method can be
 275      * reimplemented to change the behavior.  By default
 276      * the scrollRectToVisible method is called on the
 277      * associated component.
 278      *
 279      * @param nloc the new position to scroll to
 280      */
 281     protected void adjustVisibility(Rectangle nloc) {
 282         if(component == null) {
 283             return;
 284         }
 285         if (SwingUtilities.isEventDispatchThread()) {
 286                 component.scrollRectToVisible(nloc);
 287         } else {
 288             SwingUtilities.invokeLater(new SafeScroller(nloc));
 289         }
 290     }
 291 
 292     /**
 293      * Gets the painter for the Highlighter.
 294      *
 295      * @return the painter
 296      */
 297     protected Highlighter.HighlightPainter getSelectionPainter() {
 298         return DefaultHighlighter.DefaultPainter;
 299     }
 300 
 301     /**
 302      * Tries to set the position of the caret from
 303      * the coordinates of a mouse event, using viewToModel().
 304      *
 305      * @param e the mouse event
 306      */
 307     @SuppressWarnings("deprecation")
 308     protected void positionCaret(MouseEvent e) {
 309         Point pt = new Point(e.getX(), e.getY());
 310         Position.Bias[] biasRet = new Position.Bias[1];
 311         int pos = component.getUI().viewToModel(component, pt, biasRet);
 312         if(biasRet[0] == null)
 313             biasRet[0] = Position.Bias.Forward;
 314         if (pos >= 0) {
 315             setDot(pos, biasRet[0]);
 316         }
 317     }
 318 
 319     /**
 320      * Tries to move the position of the caret from
 321      * the coordinates of a mouse event, using viewToModel().
 322      * This will cause a selection if the dot and mark
 323      * are different.
 324      *
 325      * @param e the mouse event
 326      */
 327     @SuppressWarnings("deprecation")
 328     protected void moveCaret(MouseEvent e) {
 329         Point pt = new Point(e.getX(), e.getY());
 330         Position.Bias[] biasRet = new Position.Bias[1];
 331         int pos = component.getUI().viewToModel(component, pt, biasRet);
 332         if(biasRet[0] == null)
 333             biasRet[0] = Position.Bias.Forward;
 334         if (pos >= 0) {
 335             moveDot(pos, biasRet[0]);
 336         }
 337     }
 338 
 339     // --- FocusListener methods --------------------------
 340 
 341     /**
 342      * Called when the component containing the caret gains
 343      * focus.  This is implemented to set the caret to visible
 344      * if the component is editable.
 345      *
 346      * @param e the focus event
 347      * @see FocusListener#focusGained
 348      */
 349     public void focusGained(FocusEvent e) {
 350         if (component.isEnabled()) {
 351             if (component.isEditable()) {
 352                 setVisible(true);
 353             }
 354             setSelectionVisible(true);
 355         }
 356     }
 357 
 358     /**
 359      * Called when the component containing the caret loses
 360      * focus.  This is implemented to set the caret to visibility
 361      * to false.
 362      *
 363      * @param e the focus event
 364      * @see FocusListener#focusLost
 365      */
 366     public void focusLost(FocusEvent e) {
 367         setVisible(false);
 368         setSelectionVisible(ownsSelection || e.isTemporary());
 369     }
 370 
 371 
 372     /**
 373      * Selects word based on the MouseEvent
 374      */
 375     private void selectWord(MouseEvent e) {
 376         if (selectedWordEvent != null
 377             && selectedWordEvent.getX() == e.getX()
 378             && selectedWordEvent.getY() == e.getY()) {
 379             //we already done selection for this
 380             return;
 381         }
 382                     Action a = null;
 383                     ActionMap map = getComponent().getActionMap();
 384                     if (map != null) {
 385                         a = map.get(DefaultEditorKit.selectWordAction);
 386                     }
 387                     if (a == null) {
 388                         if (selectWord == null) {
 389                             selectWord = new DefaultEditorKit.SelectWordAction();
 390                         }
 391                         a = selectWord;
 392                     }
 393                     a.actionPerformed(new ActionEvent(getComponent(),
 394                                                       ActionEvent.ACTION_PERFORMED, null, e.getWhen(), e.getModifiers()));
 395         selectedWordEvent = e;
 396     }
 397 
 398     // --- MouseListener methods -----------------------------------
 399 
 400     /**
 401      * Called when the mouse is clicked.  If the click was generated
 402      * from button1, a double click selects a word,
 403      * and a triple click the current line.
 404      *
 405      * @param e the mouse event
 406      * @see MouseListener#mouseClicked
 407      */
 408     public void mouseClicked(MouseEvent e) {
 409         if (getComponent() == null) {
 410             return;
 411         }
 412 
 413         int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e);
 414 
 415         if (! e.isConsumed()) {
 416             if (SwingUtilities.isLeftMouseButton(e)) {
 417                 // mouse 1 behavior
 418                 if(nclicks == 1) {
 419                     selectedWordEvent = null;
 420                 } else if(nclicks == 2
 421                           && SwingUtilities2.canEventAccessSystemClipboard(e)) {
 422                     selectWord(e);
 423                     selectedWordEvent = null;
 424                 } else if(nclicks == 3
 425                           && SwingUtilities2.canEventAccessSystemClipboard(e)) {
 426                     Action a = null;
 427                     ActionMap map = getComponent().getActionMap();
 428                     if (map != null) {
 429                         a = map.get(DefaultEditorKit.selectLineAction);
 430                     }
 431                     if (a == null) {
 432                         if (selectLine == null) {
 433                             selectLine = new DefaultEditorKit.SelectLineAction();
 434                         }
 435                         a = selectLine;
 436                     }
 437                     a.actionPerformed(new ActionEvent(getComponent(),
 438                                                       ActionEvent.ACTION_PERFORMED, null, e.getWhen(), e.getModifiers()));
 439                 }
 440             } else if (SwingUtilities.isMiddleMouseButton(e)) {
 441                 // mouse 2 behavior
 442                 if (nclicks == 1 && component.isEditable() && component.isEnabled()
 443                     && SwingUtilities2.canEventAccessSystemClipboard(e)) {
 444                     // paste system selection, if it exists
 445                     JTextComponent c = (JTextComponent) e.getSource();
 446                     if (c != null) {
 447                         try {
 448                             Toolkit tk = c.getToolkit();
 449                             Clipboard buffer = tk.getSystemSelection();
 450                             if (buffer != null) {
 451                                 // platform supports system selections, update it.
 452                                 adjustCaret(e);
 453                                 TransferHandler th = c.getTransferHandler();
 454                                 if (th != null) {
 455                                     Transferable trans = null;
 456 
 457                                     try {
 458                                         trans = buffer.getContents(null);
 459                                     } catch (IllegalStateException ise) {
 460                                         // clipboard was unavailable
 461                                         UIManager.getLookAndFeel().provideErrorFeedback(c);
 462                                     }
 463 
 464                                     if (trans != null) {
 465                                         th.importData(c, trans);
 466                                     }
 467                                 }
 468                                 adjustFocus(true);
 469                             }
 470                         } catch (HeadlessException he) {
 471                             // do nothing... there is no system clipboard
 472                         }
 473                     }
 474                 }
 475             }
 476         }
 477     }
 478 
 479     /**
 480      * If button 1 is pressed, this is implemented to
 481      * request focus on the associated text component,
 482      * and to set the caret position. If the shift key is held down,
 483      * the caret will be moved, potentially resulting in a selection,
 484      * otherwise the
 485      * caret position will be set to the new location.  If the component
 486      * is not enabled, there will be no request for focus.
 487      *
 488      * @param e the mouse event
 489      * @see MouseListener#mousePressed
 490      */
 491     public void mousePressed(MouseEvent e) {
 492         int nclicks = SwingUtilities2.getAdjustedClickCount(getComponent(), e);
 493 
 494         if (SwingUtilities.isLeftMouseButton(e)) {
 495             if (e.isConsumed()) {
 496                 shouldHandleRelease = true;
 497             } else {
 498                 shouldHandleRelease = false;
 499                 adjustCaretAndFocus(e);
 500                 if (nclicks == 2
 501                     && SwingUtilities2.canEventAccessSystemClipboard(e)) {
 502                     selectWord(e);
 503                 }
 504             }
 505         }
 506     }
 507 
 508     void adjustCaretAndFocus(MouseEvent e) {
 509         adjustCaret(e);
 510         adjustFocus(false);
 511     }
 512 
 513     /**
 514      * Adjusts the caret location based on the MouseEvent.
 515      */
 516     private void adjustCaret(MouseEvent e) {
 517         if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0 &&
 518             getDot() != -1) {
 519             moveCaret(e);
 520         } else if (!e.isPopupTrigger()) {
 521             positionCaret(e);
 522         }
 523     }
 524 
 525     /**
 526      * Adjusts the focus, if necessary.
 527      *
 528      * @param inWindow if true indicates requestFocusInWindow should be used
 529      */
 530     private void adjustFocus(boolean inWindow) {
 531         if ((component != null) && component.isEnabled() &&
 532                                    component.isRequestFocusEnabled()) {
 533             if (inWindow) {
 534                 component.requestFocusInWindow();
 535             }
 536             else {
 537                 component.requestFocus();
 538             }
 539         }
 540     }
 541 
 542     /**
 543      * Called when the mouse is released.
 544      *
 545      * @param e the mouse event
 546      * @see MouseListener#mouseReleased
 547      */
 548     public void mouseReleased(MouseEvent e) {
 549         if (!e.isConsumed()
 550                 && shouldHandleRelease
 551                 && SwingUtilities.isLeftMouseButton(e)) {
 552 
 553             adjustCaretAndFocus(e);
 554         }
 555     }
 556 
 557     /**
 558      * Called when the mouse enters a region.
 559      *
 560      * @param e the mouse event
 561      * @see MouseListener#mouseEntered
 562      */
 563     public void mouseEntered(MouseEvent e) {
 564     }
 565 
 566     /**
 567      * Called when the mouse exits a region.
 568      *
 569      * @param e the mouse event
 570      * @see MouseListener#mouseExited
 571      */
 572     public void mouseExited(MouseEvent e) {
 573     }
 574 
 575     // --- MouseMotionListener methods -------------------------
 576 
 577     /**
 578      * Moves the caret position
 579      * according to the mouse pointer's current
 580      * location.  This effectively extends the
 581      * selection.  By default, this is only done
 582      * for mouse button 1.
 583      *
 584      * @param e the mouse event
 585      * @see MouseMotionListener#mouseDragged
 586      */
 587     public void mouseDragged(MouseEvent e) {
 588         if ((! e.isConsumed()) && SwingUtilities.isLeftMouseButton(e)) {
 589             moveCaret(e);
 590         }
 591     }
 592 
 593     /**
 594      * Called when the mouse is moved.
 595      *
 596      * @param e the mouse event
 597      * @see MouseMotionListener#mouseMoved
 598      */
 599     public void mouseMoved(MouseEvent e) {
 600     }
 601 
 602     // ---- Caret methods ---------------------------------
 603 
 604     /**
 605      * Renders the caret as a vertical line.  If this is reimplemented
 606      * the damage method should also be reimplemented as it assumes the
 607      * shape of the caret is a vertical line.  Sets the caret color to
 608      * the value returned by getCaretColor().
 609      * <p>
 610      * If there are multiple text directions present in the associated
 611      * document, a flag indicating the caret bias will be rendered.
 612      * This will occur only if the associated document is a subclass
 613      * of AbstractDocument and there are multiple bidi levels present
 614      * in the bidi element structure (i.e. the text has multiple
 615      * directions associated with it).
 616      *
 617      * @param g the graphics context
 618      * @see #damage
 619      */
 620     @SuppressWarnings("deprecation")
 621     public void paint(Graphics g) {
 622         if(isVisible()) {
 623             try {
 624                 TextUI mapper = component.getUI();
 625                 Rectangle r = mapper.modelToView(component, dot, dotBias);
 626 
 627                 if ((r == null) || ((r.width == 0) && (r.height == 0))) {
 628                     return;
 629                 }
 630                 if (width > 0 && height > 0 &&
 631                                 !this._contains(r.x, r.y, r.width, r.height)) {
 632                     // We seem to have gotten out of sync and no longer
 633                     // contain the right location, adjust accordingly.
 634                     Rectangle clip = g.getClipBounds();
 635 
 636                     if (clip != null && !clip.contains(this)) {
 637                         // Clip doesn't contain the old location, force it
 638                         // to be repainted lest we leave a caret around.
 639                         repaint();
 640                     }
 641                     // This will potentially cause a repaint of something
 642                     // we're already repainting, but without changing the
 643                     // semantics of damage we can't really get around this.
 644                     damage(r);
 645                 }
 646                 g.setColor(component.getCaretColor());
 647                 int paintWidth = getCaretWidth(r.height);
 648                 r.x -= paintWidth  >> 1;
 649                 g.fillRect(r.x, r.y, paintWidth, r.height);
 650 
 651                 // see if we should paint a flag to indicate the bias
 652                 // of the caret.
 653                 // PENDING(prinz) this should be done through
 654                 // protected methods so that alternative LAF
 655                 // will show bidi information.
 656                 Document doc = component.getDocument();
 657                 if (doc instanceof AbstractDocument) {
 658                     Element bidi = ((AbstractDocument)doc).getBidiRootElement();
 659                     if ((bidi != null) && (bidi.getElementCount() > 1)) {
 660                         // there are multiple directions present.
 661                         flagXPoints[0] = r.x + ((dotLTR) ? paintWidth : 0);
 662                         flagYPoints[0] = r.y;
 663                         flagXPoints[1] = flagXPoints[0];
 664                         flagYPoints[1] = flagYPoints[0] + 4;
 665                         flagXPoints[2] = flagXPoints[0] + ((dotLTR) ? 4 : -4);
 666                         flagYPoints[2] = flagYPoints[0];
 667                         g.fillPolygon(flagXPoints, flagYPoints, 3);
 668                     }
 669                 }
 670             } catch (BadLocationException e) {
 671                 // can't render I guess
 672                 //System.err.println("Can't render cursor");
 673             }
 674         }
 675     }
 676 
 677     /**
 678      * Called when the UI is being installed into the
 679      * interface of a JTextComponent.  This can be used
 680      * to gain access to the model that is being navigated
 681      * by the implementation of this interface.  Sets the dot
 682      * and mark to 0, and establishes document, property change,
 683      * focus, mouse, and mouse motion listeners.
 684      *
 685      * @param c the component
 686      * @see Caret#install
 687      */
 688     public void install(JTextComponent c) {
 689         component = c;
 690         Document doc = c.getDocument();
 691         dot = mark = 0;
 692         dotLTR = markLTR = true;
 693         dotBias = markBias = Position.Bias.Forward;
 694         if (doc != null) {
 695             doc.addDocumentListener(handler);
 696         }
 697         c.addPropertyChangeListener(handler);
 698         c.addFocusListener(this);
 699         c.addMouseListener(this);
 700         c.addMouseMotionListener(this);
 701 
 702         // if the component already has focus, it won't
 703         // be notified.
 704         if (component.hasFocus()) {
 705             focusGained(null);
 706         }
 707 
 708         Number ratio = (Number) c.getClientProperty("caretAspectRatio");
 709         if (ratio != null) {
 710             aspectRatio = ratio.floatValue();
 711         } else {
 712             aspectRatio = -1;
 713         }
 714 
 715         Integer width = (Integer) c.getClientProperty("caretWidth");
 716         if (width != null) {
 717             caretWidth = width.intValue();
 718         } else {
 719             caretWidth = -1;
 720         }
 721     }
 722 
 723     /**
 724      * Called when the UI is being removed from the
 725      * interface of a JTextComponent.  This is used to
 726      * unregister any listeners that were attached.
 727      *
 728      * @param c the component
 729      * @see Caret#deinstall
 730      */
 731     public void deinstall(JTextComponent c) {
 732         c.removeMouseListener(this);
 733         c.removeMouseMotionListener(this);
 734         c.removeFocusListener(this);
 735         c.removePropertyChangeListener(handler);
 736         Document doc = c.getDocument();
 737         if (doc != null) {
 738             doc.removeDocumentListener(handler);
 739         }
 740         synchronized(this) {
 741             component = null;
 742         }
 743         if (flasher != null) {
 744             flasher.stop();
 745         }
 746 
 747 
 748     }
 749 
 750     /**
 751      * Adds a listener to track whenever the caret position has
 752      * been changed.
 753      *
 754      * @param l the listener
 755      * @see Caret#addChangeListener
 756      */
 757     public void addChangeListener(ChangeListener l) {
 758         listenerList.add(ChangeListener.class, l);
 759     }
 760 
 761     /**
 762      * Removes a listener that was tracking caret position changes.
 763      *
 764      * @param l the listener
 765      * @see Caret#removeChangeListener
 766      */
 767     public void removeChangeListener(ChangeListener l) {
 768         listenerList.remove(ChangeListener.class, l);
 769     }
 770 
 771     /**
 772      * Returns an array of all the change listeners
 773      * registered on this caret.
 774      *
 775      * @return all of this caret's <code>ChangeListener</code>s
 776      *         or an empty
 777      *         array if no change listeners are currently registered
 778      *
 779      * @see #addChangeListener
 780      * @see #removeChangeListener
 781      *
 782      * @since 1.4
 783      */
 784     public ChangeListener[] getChangeListeners() {
 785         return listenerList.getListeners(ChangeListener.class);
 786     }
 787 
 788     /**
 789      * Notifies all listeners that have registered interest for
 790      * notification on this event type.  The event instance
 791      * is lazily created using the parameters passed into
 792      * the fire method.  The listener list is processed last to first.
 793      *
 794      * @see EventListenerList
 795      */
 796     protected void fireStateChanged() {
 797         // Guaranteed to return a non-null array
 798         Object[] listeners = listenerList.getListenerList();
 799         // Process the listeners last to first, notifying
 800         // those that are interested in this event
 801         for (int i = listeners.length-2; i>=0; i-=2) {
 802             if (listeners[i]==ChangeListener.class) {
 803                 // Lazily create the event:
 804                 if (changeEvent == null)
 805                     changeEvent = new ChangeEvent(this);
 806                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
 807             }
 808         }
 809     }
 810 
 811     /**
 812      * Returns an array of all the objects currently registered
 813      * as <code><em>Foo</em>Listener</code>s
 814      * upon this caret.
 815      * <code><em>Foo</em>Listener</code>s are registered using the
 816      * <code>add<em>Foo</em>Listener</code> method.
 817      *
 818      * <p>
 819      *
 820      * You can specify the <code>listenerType</code> argument
 821      * with a class literal,
 822      * such as
 823      * <code><em>Foo</em>Listener.class</code>.
 824      * For example, you can query a
 825      * <code>DefaultCaret</code> <code>c</code>
 826      * for its change listeners with the following code:
 827      *
 828      * <pre>ChangeListener[] cls = (ChangeListener[])(c.getListeners(ChangeListener.class));</pre>
 829      *
 830      * If no such listeners exist, this method returns an empty array.
 831      * @param <T> the listener type
 832      * @param listenerType the type of listeners requested
 833      * @return an array of all objects registered as
 834      *          <code><em>Foo</em>Listener</code>s on this component,
 835      *          or an empty array if no such
 836      *          listeners have been added
 837      * @exception ClassCastException if <code>listenerType</code>
 838      *          doesn't specify a class or interface that implements
 839      *          <code>java.util.EventListener</code>
 840      *
 841      * @see #getChangeListeners
 842      *
 843      * @since 1.3
 844      */
 845     public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
 846         return listenerList.getListeners(listenerType);
 847     }
 848 
 849     /**
 850      * Changes the selection visibility.
 851      *
 852      * @param vis the new visibility
 853      */
 854     public void setSelectionVisible(boolean vis) {
 855         if (vis != selectionVisible) {
 856             selectionVisible = vis;
 857             if (selectionVisible) {
 858                 // show
 859                 Highlighter h = component.getHighlighter();
 860                 if ((dot != mark) && (h != null) && (selectionTag == null)) {
 861                     int p0 = Math.min(dot, mark);
 862                     int p1 = Math.max(dot, mark);
 863                     Highlighter.HighlightPainter p = getSelectionPainter();
 864                     try {
 865                         selectionTag = h.addHighlight(p0, p1, p);
 866                         updateOwnsSelection();
 867                     } catch (BadLocationException bl) {
 868                         selectionTag = null;
 869                     }
 870                 }
 871             } else {
 872                 // hide
 873                 if (selectionTag != null) {
 874                     Highlighter h = component.getHighlighter();
 875                     h.removeHighlight(selectionTag);
 876                     selectionTag = null;
 877                     updateOwnsSelection();
 878                 }
 879             }
 880         }
 881     }
 882 
 883     /**
 884      * Checks whether the current selection is visible.
 885      *
 886      * @return true if the selection is visible
 887      */
 888     public boolean isSelectionVisible() {
 889         return selectionVisible;
 890     }
 891 
 892     /**
 893      * Determines if the caret is currently active.
 894      * <p>
 895      * This method returns whether or not the <code>Caret</code>
 896      * is currently in a blinking state. It does not provide
 897      * information as to whether it is currently blinked on or off.
 898      * To determine if the caret is currently painted use the
 899      * <code>isVisible</code> method.
 900      *
 901      * @return <code>true</code> if active else <code>false</code>
 902      * @see #isVisible
 903      *
 904      * @since 1.5
 905      */
 906     public boolean isActive() {
 907         return active;
 908     }
 909 
 910     /**
 911      * Indicates whether or not the caret is currently visible. As the
 912      * caret flashes on and off the return value of this will change
 913      * between true, when the caret is painted, and false, when the
 914      * caret is not painted. <code>isActive</code> indicates whether
 915      * or not the caret is in a blinking state, such that it <b>can</b>
 916      * be visible, and <code>isVisible</code> indicates whether or not
 917      * the caret <b>is</b> actually visible.
 918      * <p>
 919      * Subclasses that wish to render a different flashing caret
 920      * should override paint and only paint the caret if this method
 921      * returns true.
 922      *
 923      * @return true if visible else false
 924      * @see Caret#isVisible
 925      * @see #isActive
 926      */
 927     public boolean isVisible() {
 928         return visible;
 929     }
 930 
 931     /**
 932      * Sets the caret visibility, and repaints the caret.
 933      * It is important to understand the relationship between this method,
 934      * <code>isVisible</code> and <code>isActive</code>.
 935      * Calling this method with a value of <code>true</code> activates the
 936      * caret blinking. Setting it to <code>false</code> turns it completely off.
 937      * To determine whether the blinking is active, you should call
 938      * <code>isActive</code>. In effect, <code>isActive</code> is an
 939      * appropriate corresponding "getter" method for this one.
 940      * <code>isVisible</code> can be used to fetch the current
 941      * visibility status of the caret, meaning whether or not it is currently
 942      * painted. This status will change as the caret blinks on and off.
 943      * <p>
 944      * Here's a list showing the potential return values of both
 945      * <code>isActive</code> and <code>isVisible</code>
 946      * after calling this method:
 947      * <p>
 948      * <b><code>setVisible(true)</code></b>:
 949      * <ul>
 950      *     <li>isActive(): true</li>
 951      *     <li>isVisible(): true or false depending on whether
 952      *         or not the caret is blinked on or off</li>
 953      * </ul>
 954      * <p>
 955      * <b><code>setVisible(false)</code></b>:
 956      * <ul>
 957      *     <li>isActive(): false</li>
 958      *     <li>isVisible(): false</li>
 959      * </ul>
 960      *
 961      * @param e the visibility specifier
 962      * @see #isActive
 963      * @see Caret#setVisible
 964      */
 965     @SuppressWarnings("deprecation")
 966     public void setVisible(boolean e) {
 967         // focus lost notification can come in later after the
 968         // caret has been deinstalled, in which case the component
 969         // will be null.
 970         active = e;
 971         if (component != null) {
 972             TextUI mapper = component.getUI();
 973             if (visible != e) {
 974                 visible = e;
 975                 // repaint the caret
 976                 try {
 977                     Rectangle loc = mapper.modelToView(component, dot,dotBias);
 978                     damage(loc);
 979                 } catch (BadLocationException badloc) {
 980                     // hmm... not legally positioned
 981                 }
 982             }
 983         }
 984         if (flasher != null) {
 985             if (visible) {
 986                 flasher.start();
 987             } else {
 988                 flasher.stop();
 989             }
 990         }
 991     }
 992 
 993     /**
 994      * Sets the caret blink rate.
 995      *
 996      * @param rate the rate in milliseconds, 0 to stop blinking
 997      * @see Caret#setBlinkRate
 998      */
 999     public void setBlinkRate(int rate) {
1000         if (rate != 0) {
1001             if (flasher == null) {
1002                 flasher = new Timer(rate, handler);
1003             }
1004             flasher.setDelay(rate);
1005         } else {
1006             if (flasher != null) {
1007                 flasher.stop();
1008                 flasher.removeActionListener(handler);
1009                 flasher = null;
1010             }
1011         }
1012     }
1013 
1014     /**
1015      * Gets the caret blink rate.
1016      *
1017      * @return the delay in milliseconds.  If this is
1018      *  zero the caret will not blink.
1019      * @see Caret#getBlinkRate
1020      */
1021     public int getBlinkRate() {
1022         return (flasher == null) ? 0 : flasher.getDelay();
1023     }
1024 
1025     /**
1026      * Fetches the current position of the caret.
1027      *
1028      * @return the position &gt;= 0
1029      * @see Caret#getDot
1030      */
1031     public int getDot() {
1032         return dot;
1033     }
1034 
1035     /**
1036      * Fetches the current position of the mark.  If there is a selection,
1037      * the dot and mark will not be the same.
1038      *
1039      * @return the position &gt;= 0
1040      * @see Caret#getMark
1041      */
1042     public int getMark() {
1043         return mark;
1044     }
1045 
1046     /**
1047      * Sets the caret position and mark to the specified position,
1048      * with a forward bias. This implicitly sets the
1049      * selection range to zero.
1050      *
1051      * @param dot the position &gt;= 0
1052      * @see #setDot(int, Position.Bias)
1053      * @see Caret#setDot
1054      */
1055     public void setDot(int dot) {
1056         setDot(dot, Position.Bias.Forward);
1057     }
1058 
1059     /**
1060      * Moves the caret position to the specified position,
1061      * with a forward bias.
1062      *
1063      * @param dot the position &gt;= 0
1064      * @see #moveDot(int, javax.swing.text.Position.Bias)
1065      * @see Caret#moveDot
1066      */
1067     public void moveDot(int dot) {
1068         moveDot(dot, Position.Bias.Forward);
1069     }
1070 
1071     // ---- Bidi methods (we could put these in a subclass)
1072 
1073     /**
1074      * Moves the caret position to the specified position, with the
1075      * specified bias.
1076      *
1077      * @param dot the position &gt;= 0
1078      * @param dotBias the bias for this position, not <code>null</code>
1079      * @throws IllegalArgumentException if the bias is <code>null</code>
1080      * @see Caret#moveDot
1081      * @since 1.6
1082      */
1083     public void moveDot(int dot, Position.Bias dotBias) {
1084         if (dotBias == null) {
1085             throw new IllegalArgumentException("null bias");
1086         }
1087 
1088         if (! component.isEnabled()) {
1089             // don't allow selection on disabled components.
1090             setDot(dot, dotBias);
1091             return;
1092         }
1093         if (dot != this.dot) {
1094             NavigationFilter filter = component.getNavigationFilter();
1095 
1096             if (filter != null) {
1097                 filter.moveDot(getFilterBypass(), dot, dotBias);
1098             }
1099             else {
1100                 handleMoveDot(dot, dotBias);
1101             }
1102         }
1103     }
1104 
1105     void handleMoveDot(int dot, Position.Bias dotBias) {
1106         changeCaretPosition(dot, dotBias);
1107 
1108         if (selectionVisible) {
1109             Highlighter h = component.getHighlighter();
1110             if (h != null) {
1111                 int p0 = Math.min(dot, mark);
1112                 int p1 = Math.max(dot, mark);
1113 
1114                 // if p0 == p1 then there should be no highlight, remove it if necessary
1115                 if (p0 == p1) {
1116                     if (selectionTag != null) {
1117                         h.removeHighlight(selectionTag);
1118                         selectionTag = null;
1119                         updateOwnsSelection();
1120                     }
1121                 // otherwise, change or add the highlight
1122                 } else {
1123                     try {
1124                         if (selectionTag != null) {
1125                             h.changeHighlight(selectionTag, p0, p1);
1126                         } else {
1127                             Highlighter.HighlightPainter p = getSelectionPainter();
1128                             selectionTag = h.addHighlight(p0, p1, p);
1129                         }
1130                         updateOwnsSelection();
1131                     } catch (BadLocationException e) {
1132                         throw new StateInvariantError("Bad caret position");
1133                     }
1134                 }
1135             }
1136         }
1137     }
1138 
1139     /**
1140      * Sets the caret position and mark to the specified position, with the
1141      * specified bias. This implicitly sets the selection range
1142      * to zero.
1143      *
1144      * @param dot the position &gt;= 0
1145      * @param dotBias the bias for this position, not <code>null</code>
1146      * @throws IllegalArgumentException if the bias is <code>null</code>
1147      * @see Caret#setDot
1148      * @since 1.6
1149      */
1150     public void setDot(int dot, Position.Bias dotBias) {
1151         if (dotBias == null) {
1152             throw new IllegalArgumentException("null bias");
1153         }
1154 
1155         NavigationFilter filter = component.getNavigationFilter();
1156 
1157         if (filter != null) {
1158             filter.setDot(getFilterBypass(), dot, dotBias);
1159         }
1160         else {
1161             handleSetDot(dot, dotBias);
1162         }
1163     }
1164 
1165     void handleSetDot(int dot, Position.Bias dotBias) {
1166         // move dot, if it changed
1167         Document doc = component.getDocument();
1168         if (doc != null) {
1169             dot = Math.min(dot, doc.getLength());
1170         }
1171         dot = Math.max(dot, 0);
1172 
1173         // The position (0,Backward) is out of range so disallow it.
1174         if( dot == 0 )
1175             dotBias = Position.Bias.Forward;
1176 
1177         mark = dot;
1178         if (this.dot != dot || this.dotBias != dotBias ||
1179             selectionTag != null || forceCaretPositionChange) {
1180             changeCaretPosition(dot, dotBias);
1181             updateOwnsSelection();
1182         }
1183         this.markBias = this.dotBias;
1184         this.markLTR = dotLTR;
1185         Highlighter h = component.getHighlighter();
1186         if ((h != null) && (selectionTag != null)) {
1187             h.removeHighlight(selectionTag);
1188             selectionTag = null;
1189             updateOwnsSelection();
1190         }
1191     }
1192 
1193     /**
1194      * Returns the bias of the caret position.
1195      *
1196      * @return the bias of the caret position
1197      * @since 1.6
1198      */
1199     public Position.Bias getDotBias() {
1200         return dotBias;
1201     }
1202 
1203     /**
1204      * Returns the bias of the mark.
1205      *
1206      * @return the bias of the mark
1207      * @since 1.6
1208      */
1209     public Position.Bias getMarkBias() {
1210         return markBias;
1211     }
1212 
1213     boolean isDotLeftToRight() {
1214         return dotLTR;
1215     }
1216 
1217     boolean isMarkLeftToRight() {
1218         return markLTR;
1219     }
1220 
1221     boolean isPositionLTR(int position, Position.Bias bias) {
1222         Document doc = component.getDocument();
1223         if(bias == Position.Bias.Backward && --position < 0)
1224             position = 0;
1225         return AbstractDocument.isLeftToRight(doc, position, position);
1226     }
1227 
1228     Position.Bias guessBiasForOffset(int offset, Position.Bias lastBias,
1229                                      boolean lastLTR) {
1230         // There is an abiguous case here. That if your model looks like:
1231         // abAB with the cursor at abB]A (visual representation of
1232         // 3 forward) deleting could either become abB] or
1233         // ab[B. I'ld actually prefer abB]. But, if I implement that
1234         // a delete at abBA] would result in aBA] vs a[BA which I
1235         // think is totally wrong. To get this right we need to know what
1236         // was deleted. And we could get this from the bidi structure
1237         // in the change event. So:
1238         // PENDING: base this off what was deleted.
1239         if(lastLTR != isPositionLTR(offset, lastBias)) {
1240             lastBias = Position.Bias.Backward;
1241         }
1242         else if(lastBias != Position.Bias.Backward &&
1243                 lastLTR != isPositionLTR(offset, Position.Bias.Backward)) {
1244             lastBias = Position.Bias.Backward;
1245         }
1246         if (lastBias == Position.Bias.Backward && offset > 0) {
1247             try {
1248                 Segment s = new Segment();
1249                 component.getDocument().getText(offset - 1, 1, s);
1250                 if (s.count > 0 && s.array[s.offset] == '\n') {
1251                     lastBias = Position.Bias.Forward;
1252                 }
1253             }
1254             catch (BadLocationException ble) {}
1255         }
1256         return lastBias;
1257     }
1258 
1259     // ---- local methods --------------------------------------------
1260 
1261     /**
1262      * Sets the caret position (dot) to a new location.  This
1263      * causes the old and new location to be repainted.  It
1264      * also makes sure that the caret is within the visible
1265      * region of the view, if the view is scrollable.
1266      */
1267     void changeCaretPosition(int dot, Position.Bias dotBias) {
1268         // repaint the old position and set the new value of
1269         // the dot.
1270         repaint();
1271 
1272 
1273         // Make sure the caret is visible if this window has the focus.
1274         if (flasher != null && flasher.isRunning()) {
1275             visible = true;
1276             flasher.restart();
1277         }
1278 
1279         // notify listeners at the caret moved
1280         this.dot = dot;
1281         this.dotBias = dotBias;
1282         dotLTR = isPositionLTR(dot, dotBias);
1283         fireStateChanged();
1284 
1285         updateSystemSelection();
1286 
1287         setMagicCaretPosition(null);
1288 
1289         // We try to repaint the caret later, since things
1290         // may be unstable at the time this is called
1291         // (i.e. we don't want to depend upon notification
1292         // order or the fact that this might happen on
1293         // an unsafe thread).
1294         Runnable callRepaintNewCaret = new Runnable() {
1295             public void run() {
1296                 repaintNewCaret();
1297             }
1298         };
1299         SwingUtilities.invokeLater(callRepaintNewCaret);
1300     }
1301 
1302     /**
1303      * Repaints the new caret position, with the
1304      * assumption that this is happening on the
1305      * event thread so that calling <code>modelToView</code>
1306      * is safe.
1307      */
1308     @SuppressWarnings("deprecation")
1309     void repaintNewCaret() {
1310         if (component != null) {
1311             TextUI mapper = component.getUI();
1312             Document doc = component.getDocument();
1313             if ((mapper != null) && (doc != null)) {
1314                 // determine the new location and scroll if
1315                 // not visible.
1316                 Rectangle newLoc;
1317                 try {
1318                     newLoc = mapper.modelToView(component, this.dot, this.dotBias);
1319                 } catch (BadLocationException e) {
1320                     newLoc = null;
1321                 }
1322                 if (newLoc != null) {
1323                     adjustVisibility(newLoc);
1324                     // If there is no magic caret position, make one
1325                     if (getMagicCaretPosition() == null) {
1326                         setMagicCaretPosition(new Point(newLoc.x, newLoc.y));
1327                     }
1328                 }
1329 
1330                 // repaint the new position
1331                 damage(newLoc);
1332             }
1333         }
1334     }
1335 
1336     private void updateSystemSelection() {
1337         if ( ! SwingUtilities2.canCurrentEventAccessSystemClipboard() ) {
1338             return;
1339         }
1340         if (this.dot != this.mark && component != null && component.hasFocus()) {
1341             Clipboard clip = getSystemSelection();
1342             if (clip != null) {
1343                 String selectedText;
1344                 if (component instanceof JPasswordField
1345                     && component.getClientProperty("JPasswordField.cutCopyAllowed") !=
1346                     Boolean.TRUE) {
1347                     //fix for 4793761
1348                     StringBuilder txt = null;
1349                     char echoChar = ((JPasswordField)component).getEchoChar();
1350                     int p0 = Math.min(getDot(), getMark());
1351                     int p1 = Math.max(getDot(), getMark());
1352                     for (int i = p0; i < p1; i++) {
1353                         if (txt == null) {
1354                             txt = new StringBuilder();
1355                         }
1356                         txt.append(echoChar);
1357                     }
1358                     selectedText = (txt != null) ? txt.toString() : null;
1359                 } else {
1360                     selectedText = component.getSelectedText();
1361                 }
1362                 try {
1363                     clip.setContents(
1364                         new StringSelection(selectedText), getClipboardOwner());
1365 
1366                     ownsSelection = true;
1367                 } catch (IllegalStateException ise) {
1368                     // clipboard was unavailable
1369                     // no need to provide error feedback to user since updating
1370                     // the system selection is not a user invoked action
1371                 }
1372             }
1373         }
1374     }
1375 
1376     private Clipboard getSystemSelection() {
1377         try {
1378             return component.getToolkit().getSystemSelection();
1379         } catch (HeadlessException he) {
1380             // do nothing... there is no system clipboard
1381         } catch (SecurityException se) {
1382             // do nothing... there is no allowed system clipboard
1383         }
1384         return null;
1385     }
1386 
1387     private ClipboardOwner getClipboardOwner() {
1388         return handler;
1389     }
1390 
1391     /**
1392      * This is invoked after the document changes to verify the current
1393      * dot/mark is valid. We do this in case the <code>NavigationFilter</code>
1394      * changed where to position the dot, that resulted in the current location
1395      * being bogus.
1396      */
1397     private void ensureValidPosition() {
1398         int length = component.getDocument().getLength();
1399         if (dot > length || mark > length) {
1400             // Current location is bogus and filter likely vetoed the
1401             // change, force the reset without giving the filter a
1402             // chance at changing it.
1403             handleSetDot(length, Position.Bias.Forward);
1404         }
1405     }
1406 
1407 
1408     /**
1409      * Saves the current caret position.  This is used when
1410      * caret up/down actions occur, moving between lines
1411      * that have uneven end positions.
1412      *
1413      * @param p the position
1414      * @see #getMagicCaretPosition
1415      */
1416     public void setMagicCaretPosition(Point p) {
1417         magicCaretPosition = p;
1418     }
1419 
1420     /**
1421      * Gets the saved caret position.
1422      *
1423      * @return the position
1424      * see #setMagicCaretPosition
1425      */
1426     public Point getMagicCaretPosition() {
1427         return magicCaretPosition;
1428     }
1429 
1430     /**
1431      * Compares this object to the specified object.
1432      * The superclass behavior of comparing rectangles
1433      * is not desired, so this is changed to the Object
1434      * behavior.
1435      *
1436      * @param     obj   the object to compare this font with
1437      * @return    <code>true</code> if the objects are equal;
1438      *            <code>false</code> otherwise
1439      */
1440     public boolean equals(Object obj) {
1441         return (this == obj);
1442     }
1443 
1444     public String toString() {
1445         String s = "Dot=(" + dot + ", " + dotBias + ")";
1446         s += " Mark=(" + mark + ", " + markBias + ")";
1447         return s;
1448     }
1449 
1450     private NavigationFilter.FilterBypass getFilterBypass() {
1451         if (filterBypass == null) {
1452             filterBypass = new DefaultFilterBypass();
1453         }
1454         return filterBypass;
1455     }
1456 
1457     // Rectangle.contains returns false if passed a rect with a w or h == 0,
1458     // this won't (assuming X,Y are contained with this rectangle).
1459     private boolean _contains(int X, int Y, int W, int H) {
1460         int w = this.width;
1461         int h = this.height;
1462         if ((w | h | W | H) < 0) {
1463             // At least one of the dimensions is negative...
1464             return false;
1465         }
1466         // Note: if any dimension is zero, tests below must return false...
1467         int x = this.x;
1468         int y = this.y;
1469         if (X < x || Y < y) {
1470             return false;
1471         }
1472         if (W > 0) {
1473             w += x;
1474             W += X;
1475             if (W <= X) {
1476                 // X+W overflowed or W was zero, return false if...
1477                 // either original w or W was zero or
1478                 // x+w did not overflow or
1479                 // the overflowed x+w is smaller than the overflowed X+W
1480                 if (w >= x || W > w) return false;
1481             } else {
1482                 // X+W did not overflow and W was not zero, return false if...
1483                 // original w was zero or
1484                 // x+w did not overflow and x+w is smaller than X+W
1485                 if (w >= x && W > w) return false;
1486             }
1487         }
1488         else if ((x + w) < X) {
1489             return false;
1490         }
1491         if (H > 0) {
1492             h += y;
1493             H += Y;
1494             if (H <= Y) {
1495                 if (h >= y || H > h) return false;
1496             } else {
1497                 if (h >= y && H > h) return false;
1498             }
1499         }
1500         else if ((y + h) < Y) {
1501             return false;
1502         }
1503         return true;
1504     }
1505 
1506     int getCaretWidth(int height) {
1507         if (aspectRatio > -1) {
1508             return (int) (aspectRatio * height) + 1;
1509         }
1510 
1511         if (caretWidth > -1) {
1512             return caretWidth;
1513         } else {
1514             Object property = UIManager.get("Caret.width");
1515             if (property instanceof Integer) {
1516                 return ((Integer) property).intValue();
1517             } else {
1518                 return 1;
1519             }
1520         }
1521     }
1522 
1523     // --- serialization ---------------------------------------------
1524 
1525     private void readObject(ObjectInputStream s)
1526       throws ClassNotFoundException, IOException
1527     {
1528         ObjectInputStream.GetField f = s.readFields();
1529 
1530         EventListenerList newListenerList = (EventListenerList) f.get("listenerList", null);
1531         if (newListenerList == null) {
1532             throw new InvalidObjectException("Null listenerList");
1533         }
1534         listenerList = newListenerList;
1535         component = (JTextComponent) f.get("component", null);
1536         updatePolicy = f.get("updatePolicy", 0);
1537         visible = f.get("visible", false);
1538         active = f.get("active", false);
1539         dot = f.get("dot", 0);
1540         mark = f.get("mark", 0);
1541         selectionTag = f.get("selectionTag", null);
1542         selectionVisible = f.get("selectionVisible", false);
1543         flasher = (Timer) f.get("flasher", null);
1544         magicCaretPosition = (Point) f.get("magicCaretPosition", null);
1545         dotLTR = f.get("dotLTR", false);
1546         markLTR = f.get("markLTR", false);
1547         ownsSelection = f.get("ownsSelection", false);
1548         forceCaretPositionChange = f.get("forceCaretPositionChange", false);
1549         caretWidth = f.get("caretWidth", 0);
1550         aspectRatio = f.get("aspectRatio", 0.0f);
1551 
1552         handler = new Handler();
1553         if (!s.readBoolean()) {
1554             dotBias = Position.Bias.Forward;
1555         }
1556         else {
1557             dotBias = Position.Bias.Backward;
1558         }
1559         if (!s.readBoolean()) {
1560             markBias = Position.Bias.Forward;
1561         }
1562         else {
1563             markBias = Position.Bias.Backward;
1564         }
1565     }
1566 
1567     private void writeObject(ObjectOutputStream s) throws IOException {
1568         s.defaultWriteObject();
1569         s.writeBoolean((dotBias == Position.Bias.Backward));
1570         s.writeBoolean((markBias == Position.Bias.Backward));
1571     }
1572 
1573     // ---- member variables ------------------------------------------
1574 
1575     /**
1576      * The event listener list.
1577      */
1578     protected EventListenerList listenerList = new EventListenerList();
1579 
1580     /**
1581      * The change event for the model.
1582      * Only one ChangeEvent is needed per model instance since the
1583      * event's only (read-only) state is the source property.  The source
1584      * of events generated here is always "this".
1585      */
1586     protected transient ChangeEvent changeEvent = null;
1587 
1588     // package-private to avoid inner classes private member
1589     // access bug
1590     JTextComponent component;
1591 
1592     int updatePolicy = UPDATE_WHEN_ON_EDT;
1593     boolean visible;
1594     boolean active;
1595     int dot;
1596     int mark;
1597     Object selectionTag;
1598     boolean selectionVisible;
1599     Timer flasher;
1600     Point magicCaretPosition;
1601     transient Position.Bias dotBias;
1602     transient Position.Bias markBias;
1603     boolean dotLTR;
1604     boolean markLTR;
1605     transient Handler handler = new Handler();
1606     private transient int[] flagXPoints = new int[3];
1607     private transient int[] flagYPoints = new int[3];
1608     private transient NavigationFilter.FilterBypass filterBypass;
1609     private static transient Action selectWord = null;
1610     private static transient Action selectLine = null;
1611     /**
1612      * This is used to indicate if the caret currently owns the selection.
1613      * This is always false if the system does not support the system
1614      * clipboard.
1615      */
1616     private boolean ownsSelection;
1617 
1618     /**
1619      * If this is true, the location of the dot is updated regardless of
1620      * the current location. This is set in the DocumentListener
1621      * such that even if the model location of dot hasn't changed (perhaps do
1622      * to a forward delete) the visual location is updated.
1623      */
1624     private boolean forceCaretPositionChange;
1625 
1626     /**
1627      * Whether or not mouseReleased should adjust the caret and focus.
1628      * This flag is set by mousePressed if it wanted to adjust the caret
1629      * and focus but couldn't because of a possible DnD operation.
1630      */
1631     private transient boolean shouldHandleRelease;
1632 
1633 
1634     /**
1635      * holds last MouseEvent which caused the word selection
1636      */
1637     private transient MouseEvent selectedWordEvent = null;
1638 
1639     /**
1640      * The width of the caret in pixels.
1641      */
1642     private int caretWidth = -1;
1643     private float aspectRatio = -1;
1644 
1645     class SafeScroller implements Runnable {
1646 
1647         SafeScroller(Rectangle r) {
1648             this.r = r;
1649         }
1650 
1651         public void run() {
1652             if (component != null) {
1653                 component.scrollRectToVisible(r);
1654             }
1655         }
1656 
1657         Rectangle r;
1658     }
1659 
1660 
1661     class Handler implements PropertyChangeListener, DocumentListener, ActionListener, ClipboardOwner {
1662 
1663         // --- ActionListener methods ----------------------------------
1664 
1665         /**
1666          * Invoked when the blink timer fires.  This is called
1667          * asynchronously.  The simply changes the visibility
1668          * and repaints the rectangle that last bounded the caret.
1669          *
1670          * @param e the action event
1671          */
1672         @SuppressWarnings("deprecation")
1673         public void actionPerformed(ActionEvent e) {
1674             if (width == 0 || height == 0) {
1675                 // setVisible(true) will cause a scroll, only do this if the
1676                 // new location is really valid.
1677                 if (component != null) {
1678                     TextUI mapper = component.getUI();
1679                     try {
1680                         Rectangle r = mapper.modelToView(component, dot,
1681                                                          dotBias);
1682                         if (r != null && r.width != 0 && r.height != 0) {
1683                             damage(r);
1684                         }
1685                     } catch (BadLocationException ble) {
1686                     }
1687                 }
1688             }
1689             visible = !visible;
1690             repaint();
1691         }
1692 
1693         // --- DocumentListener methods --------------------------------
1694 
1695         /**
1696          * Updates the dot and mark if they were changed by
1697          * the insertion.
1698          *
1699          * @param e the document event
1700          * @see DocumentListener#insertUpdate
1701          */
1702         public void insertUpdate(DocumentEvent e) {
1703             if (getUpdatePolicy() == NEVER_UPDATE ||
1704                     (getUpdatePolicy() == UPDATE_WHEN_ON_EDT &&
1705                     !SwingUtilities.isEventDispatchThread())) {
1706 
1707                 if ((e.getOffset() <= dot || e.getOffset() <= mark)
1708                         && selectionTag != null) {
1709                     try {
1710                         component.getHighlighter().changeHighlight(selectionTag,
1711                                 Math.min(dot, mark), Math.max(dot, mark));
1712                     } catch (BadLocationException e1) {
1713                         e1.printStackTrace();
1714                     }
1715                 }
1716                 return;
1717             }
1718             int offset = e.getOffset();
1719             int length = e.getLength();
1720             int newDot = dot;
1721             short changed = 0;
1722 
1723             if (e instanceof AbstractDocument.UndoRedoDocumentEvent) {
1724                 setDot(offset + length);
1725                 return;
1726             }
1727             if (newDot >= offset) {
1728                 newDot += length;
1729                 changed |= 1;
1730             }
1731             int newMark = mark;
1732             if (newMark >= offset) {
1733                 newMark += length;
1734                 changed |= 2;
1735             }
1736 
1737             if (changed != 0) {
1738                 Position.Bias dotBias = DefaultCaret.this.dotBias;
1739                 if (dot == offset) {
1740                     Document doc = component.getDocument();
1741                     boolean isNewline;
1742                     try {
1743                         Segment s = new Segment();
1744                         doc.getText(newDot - 1, 1, s);
1745                         isNewline = (s.count > 0 &&
1746                                 s.array[s.offset] == '\n');
1747                     } catch (BadLocationException ble) {
1748                         isNewline = false;
1749                     }
1750                     if (isNewline) {
1751                         dotBias = Position.Bias.Forward;
1752                     } else {
1753                         dotBias = Position.Bias.Backward;
1754                     }
1755                 }
1756                 if (newMark == newDot) {
1757                     setDot(newDot, dotBias);
1758                     ensureValidPosition();
1759                 }
1760                 else {
1761                     setDot(newMark, markBias);
1762                     if (getDot() == newMark) {
1763                         // Due this test in case the filter vetoed the
1764                         // change in which case this probably won't be
1765                         // valid either.
1766                         moveDot(newDot, dotBias);
1767                     }
1768                     ensureValidPosition();
1769                 }
1770             }
1771         }
1772 
1773         /**
1774          * Updates the dot and mark if they were changed
1775          * by the removal.
1776          *
1777          * @param e the document event
1778          * @see DocumentListener#removeUpdate
1779          */
1780         public void removeUpdate(DocumentEvent e) {
1781             if (getUpdatePolicy() == NEVER_UPDATE ||
1782                     (getUpdatePolicy() == UPDATE_WHEN_ON_EDT &&
1783                     !SwingUtilities.isEventDispatchThread())) {
1784 
1785                 int length = component.getDocument().getLength();
1786                 dot = Math.min(dot, length);
1787                 mark = Math.min(mark, length);
1788                 if ((e.getOffset() < dot || e.getOffset() < mark)
1789                         && selectionTag != null) {
1790                     try {
1791                         component.getHighlighter().changeHighlight(selectionTag,
1792                                 Math.min(dot, mark), Math.max(dot, mark));
1793                     } catch (BadLocationException e1) {
1794                         e1.printStackTrace();
1795                     }
1796                 }
1797                 return;
1798             }
1799             int offs0 = e.getOffset();
1800             int offs1 = offs0 + e.getLength();
1801             int newDot = dot;
1802             boolean adjustDotBias = false;
1803             int newMark = mark;
1804             boolean adjustMarkBias = false;
1805 
1806             if(e instanceof AbstractDocument.UndoRedoDocumentEvent) {
1807                 setDot(offs0);
1808                 return;
1809             }
1810             if (newDot >= offs1) {
1811                 newDot -= (offs1 - offs0);
1812                 if(newDot == offs1) {
1813                     adjustDotBias = true;
1814                 }
1815             } else if (newDot >= offs0) {
1816                 newDot = offs0;
1817                 adjustDotBias = true;
1818             }
1819             if (newMark >= offs1) {
1820                 newMark -= (offs1 - offs0);
1821                 if(newMark == offs1) {
1822                     adjustMarkBias = true;
1823                 }
1824             } else if (newMark >= offs0) {
1825                 newMark = offs0;
1826                 adjustMarkBias = true;
1827             }
1828             if (newMark == newDot) {
1829                 forceCaretPositionChange = true;
1830                 try {
1831                     setDot(newDot, guessBiasForOffset(newDot, dotBias,
1832                             dotLTR));
1833                 } finally {
1834                     forceCaretPositionChange = false;
1835                 }
1836                 ensureValidPosition();
1837             } else {
1838                 Position.Bias dotBias = DefaultCaret.this.dotBias;
1839                 Position.Bias markBias = DefaultCaret.this.markBias;
1840                 if(adjustDotBias) {
1841                     dotBias = guessBiasForOffset(newDot, dotBias, dotLTR);
1842                 }
1843                 if(adjustMarkBias) {
1844                     markBias = guessBiasForOffset(mark, markBias, markLTR);
1845                 }
1846                 setDot(newMark, markBias);
1847                 if (getDot() == newMark) {
1848                     // Due this test in case the filter vetoed the change
1849                     // in which case this probably won't be valid either.
1850                     moveDot(newDot, dotBias);
1851                 }
1852                 ensureValidPosition();
1853             }
1854         }
1855 
1856         /**
1857          * Gives notification that an attribute or set of attributes changed.
1858          *
1859          * @param e the document event
1860          * @see DocumentListener#changedUpdate
1861          */
1862         public void changedUpdate(DocumentEvent e) {
1863             if (getUpdatePolicy() == NEVER_UPDATE ||
1864                     (getUpdatePolicy() == UPDATE_WHEN_ON_EDT &&
1865                     !SwingUtilities.isEventDispatchThread())) {
1866                 return;
1867             }
1868             if(e instanceof AbstractDocument.UndoRedoDocumentEvent) {
1869                 setDot(e.getOffset() + e.getLength());
1870             }
1871         }
1872 
1873         // --- PropertyChangeListener methods -----------------------
1874 
1875         /**
1876          * This method gets called when a bound property is changed.
1877          * We are looking for document changes on the editor.
1878          */
1879         public void propertyChange(PropertyChangeEvent evt) {
1880             Object oldValue = evt.getOldValue();
1881             Object newValue = evt.getNewValue();
1882             if ((oldValue instanceof Document) || (newValue instanceof Document)) {
1883                 setDot(0);
1884                 if (oldValue != null) {
1885                     ((Document)oldValue).removeDocumentListener(this);
1886                 }
1887                 if (newValue != null) {
1888                     ((Document)newValue).addDocumentListener(this);
1889                 }
1890             } else if("enabled".equals(evt.getPropertyName())) {
1891                 Boolean enabled = (Boolean) evt.getNewValue();
1892                 if(component.isFocusOwner()) {
1893                     if(enabled == Boolean.TRUE) {
1894                         if(component.isEditable()) {
1895                             setVisible(true);
1896                         }
1897                         setSelectionVisible(true);
1898                     } else {
1899                         setVisible(false);
1900                         setSelectionVisible(false);
1901                     }
1902                 }
1903             } else if("caretWidth".equals(evt.getPropertyName())) {
1904                 Integer newWidth = (Integer) evt.getNewValue();
1905                 if (newWidth != null) {
1906                     caretWidth = newWidth.intValue();
1907                 } else {
1908                     caretWidth = -1;
1909                 }
1910                 repaint();
1911             } else if("caretAspectRatio".equals(evt.getPropertyName())) {
1912                 Number newRatio = (Number) evt.getNewValue();
1913                 if (newRatio != null) {
1914                     aspectRatio = newRatio.floatValue();
1915                 } else {
1916                     aspectRatio = -1;
1917                 }
1918                 repaint();
1919             }
1920         }
1921 
1922 
1923         //
1924         // ClipboardOwner
1925         //
1926         /**
1927          * Toggles the visibility of the selection when ownership is lost.
1928          */
1929         public void lostOwnership(Clipboard clipboard,
1930                                       Transferable contents) {
1931             if (ownsSelection) {
1932                 ownsSelection = false;
1933                 if (component != null && !component.hasFocus()) {
1934                     setSelectionVisible(false);
1935                 }
1936             }
1937         }
1938     }
1939 
1940     /**
1941      * Updates ownsSelection based on text selection in the caret.
1942      */
1943     private void updateOwnsSelection() {
1944         ownsSelection = (selectionTag != null)
1945                 && SwingUtilities2.canAccessSystemClipboard();
1946     }
1947 
1948     private class DefaultFilterBypass extends NavigationFilter.FilterBypass {
1949         public Caret getCaret() {
1950             return DefaultCaret.this;
1951         }
1952 
1953         public void setDot(int dot, Position.Bias bias) {
1954             handleSetDot(dot, bias);
1955         }
1956 
1957         public void moveDot(int dot, Position.Bias bias) {
1958             handleMoveDot(dot, bias);
1959         }
1960     }
1961 }