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