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