jdk/src/share/classes/javax/swing/text/JTextComponent.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8139 42edb817487b
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: 8139
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: 5449
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: 5449
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: 5449
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
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.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 410
diff changeset
    32
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.print.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.datatransfer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.im.InputContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.im.InputMethodRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.font.TextHitInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.print.Printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.print.PrinterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.print.PrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.print.attribute.PrintRequestAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.text.AttributedCharacterIterator.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.print.attribute.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.swing.PrintingStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.swing.text.TextComponentPrintable;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
    76
import sun.swing.SwingAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <code>JTextComponent</code> is the base class for swing text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * components.  It tries to be compatible with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <code>java.awt.TextComponent</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * where it can reasonably do so.  Also provided are other services
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * for additional flexibility (beyond the pluggable UI and bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * support).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * You can find information on how to use the functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * this class provides in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html">General Rules for Using Text Components</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <dt><b><font size=+1>Caret Changes</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * The caret is a pluggable object in swing text components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Notification of changes to the caret position and the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * are sent to implementations of the <code>CaretListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * interface that have been registered with the text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * The UI will install a default caret unless a customized caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * has been set. <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * By default the caret tracks all the document changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * performed on the Event Dispatching Thread and updates it's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * accordingly if an insertion occurs before or at the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * or a removal occurs before the caret position. <code>DefaultCaret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * tries to make itself visible which may lead to scrolling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * of a text component within <code>JScrollPane</code>. The default caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * behavior can be changed by the {@link DefaultCaret#setUpdatePolicy} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <b>Note</b>: Non-editable text components also have a caret though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * it may not be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <dt><b><font size=+1>Commands</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Text components provide a number of commands that can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * to manipulate the component.  This is essentially the way that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * the component expresses its capabilities.  These are expressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * in terms of the swing <code>Action</code> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * using the <code>TextAction</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * The set of commands supported by the text component can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * found with the {@link #getActions} method.  These actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * can be bound to key events, fired from buttons, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <dt><b><font size=+1>Text Input</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * The text components support flexible and internationalized text input, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * keymaps and the input method framework, while maintaining compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * the AWT listener model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * A {@link javax.swing.text.Keymap} lets an application bind key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * strokes to actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * In order to allow keymaps to be shared across multiple text components, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * can use actions that extend <code>TextAction</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <code>TextAction</code> can determine which <code>JTextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * most recently has or had focus and therefore is the subject of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * the action (In the case that the <code>ActionEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * sent to the action doesn't contain the target text component as its source).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * The <a href="../../../../technotes/guides/imf/spec.html">input method framework</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * lets text components interact with input methods, separate software
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * components that preprocess events to let users enter thousands of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * different characters using keyboards with far fewer keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <code>JTextComponent</code> is an <em>active client</em> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * the framework, so it implements the preferred user interface for interacting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * with input methods. As a consequence, some key events do not reach the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * component because they are handled by an input method, and some text input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * reaches the text component as committed text within an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * java.awt.event.InputMethodEvent} instead of as a key event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * The complete text input is the combination of the characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <code>keyTyped</code> key events and committed text in input method events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * The AWT listener model lets applications attach event listeners to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * components in order to bind events to actions. Swing encourages the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * use of keymaps instead of listeners, but maintains compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * with listeners by giving the listeners a chance to steal an event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * by consuming it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * Keyboard event and input method events are handled in the following stages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * with each stage capable of consuming the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <table border=1 summary="Stages of keyboard and input method event handling">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <th id="stage"><p align="left">Stage</p></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <th id="ke"><p align="left">KeyEvent</p></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <th id="ime"><p align="left">InputMethodEvent</p></th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <tr><td headers="stage">1.   </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *     <td headers="ke">input methods </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *     <td headers="ime">(generated here)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <tr><td headers="stage">2.   </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *     <td headers="ke">focus manager </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *     <td headers="ime"></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *     <td headers="stage">3.   </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *     <td headers="ke">registered key listeners</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *     <td headers="ime">registered input method listeners</tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *     <td headers="stage">4.   </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *     <td headers="ke"></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *     <td headers="ime">input method handling in JTextComponent</tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *     <td headers="stage">5.   </td><td headers="ke ime" colspan=2>keymap handling using the current keymap</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <tr><td headers="stage">6.   </td><td headers="ke">keyboard handling in JComponent (e.g. accelerators, component navigation, etc.)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *     <td headers="ime"></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * To maintain compatibility with applications that listen to key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * events but are not aware of input method events, the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * method handling in stage 4 provides a compatibility mode for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * components that do not process input method events. For these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * components, the committed text is converted to keyTyped key events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * and processed in the key event pipeline starting at stage 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * instead of in the input method event pipeline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * By default the component will create a keymap (named <b>DEFAULT_KEYMAP</b>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * that is shared by all JTextComponent instances as the default keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * Typically a look-and-feel implementation will install a different keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * that resolves to the default keymap for those bindings not found in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * different keymap. The minimal bindings include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * <li>inserting content into the editor for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *  printable keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * <li>removing content with the backspace and del
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *  keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <li>caret movement forward and backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * <dt><b><font size=+1>Model/View Split</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * The text components have a model-view split.  A text component pulls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * together the objects used to represent the model, view, and controller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * The text document model may be shared by other views which act as observers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * of the model (e.g. a document may be shared by multiple components).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * <p align=center><img src="doc-files/editor.gif" alt="Diagram showing interaction between Controller, Document, events, and ViewFactory"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *                  HEIGHT=358 WIDTH=587></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * The model is defined by the {@link Document} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * This is intended to provide a flexible text storage mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * that tracks change during edits and can be extended to more sophisticated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * models.  The model interfaces are meant to capture the capabilities of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * expression given by SGML, a system used to express a wide variety of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * Each modification to the document causes notification of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * details of the change to be sent to all observers in the form of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * {@link DocumentEvent} which allows the views to stay up to date with the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * This event is sent to observers that have implemented the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * {@link DocumentListener}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * interface and registered interest with the model being observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * <dt><b><font size=+1>Location Information</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * The capability of determining the location of text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * the view is provided.  There are two methods, {@link #modelToView}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * and {@link #viewToModel} for determining this information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * <dt><b><font size=+1>Undo/Redo support</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * Support for an edit history mechanism is provided to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * undo/redo operations.  The text component does not itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * provide the history buffer by default, but does provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * the <code>UndoableEdit</code> records that can be used in conjunction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * with a history buffer to provide the undo/redo support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * The support is provided by the Document model, which allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * one to attach UndoableEditListener implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * <dt><b><font size=+1>Thread Safety</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * The swing text components provide some support of thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * safe operations.  Because of the high level of configurability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * of the text components, it is possible to circumvent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * protection provided.  The protection primarily comes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * the model, so the documentation of <code>AbstractDocument</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * describes the assumptions of the protection provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * The methods that are safe to call asynchronously are marked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * with comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * <dt><b><font size=+1>Newlines</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * For a discussion on how newlines are handled, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * <a href="DefaultEditorKit.html">DefaultEditorKit</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * <dt><b><font size=+1>Printing support</font></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * <dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * Several {@link #print print} methods are provided for basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * document printing.  If more advanced printing is needed, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * {@link #getPrintable} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *     attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * @author Igor Kushnirskiy (printing support)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * @see Document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * @see DocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * @see DocumentListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * @see Caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * @see CaretEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * @see CaretListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * @see TextUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * @see View
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * @see ViewFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
public abstract class JTextComponent extends JComponent implements Scrollable, Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Creates a new <code>JTextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Listeners for caret events are established, and the pluggable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * UI installed.  The component is marked as editable.  No layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * is used, because layout is managed by the view subsystem of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * The document model is set to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public JTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // enable InputMethodEvent for on-the-spot pre-editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.INPUT_METHOD_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        caretEvent = new MutableCaretEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        addMouseListener(caretEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        addFocusListener(caretEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        setEditable(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        setDragEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        setLayout(null); // layout is managed by View hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Fetches the user-interface factory for this text-oriented editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public TextUI getUI() { return (TextUI)ui; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Sets the user-interface factory for this text-oriented editor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param ui the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void setUI(TextUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Reloads the pluggable UI.  The key used to fetch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * new interface is <code>getUIClassID()</code>.  The type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * the UI is <code>TextUI</code>.  <code>invalidate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * is called after setting the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        setUI((TextUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Adds a caret listener for notification of any changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * to the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param listener the listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @see javax.swing.event.CaretEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public void addCaretListener(CaretListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        listenerList.add(CaretListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Removes a caret listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param listener the listener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see javax.swing.event.CaretEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public void removeCaretListener(CaretListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        listenerList.remove(CaretListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns an array of all the caret listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * registered on this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return all of this component's <code>CaretListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *         or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *         array if no caret listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @see #addCaretListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see #removeCaretListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public CaretListener[] getCaretListeners() {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
   387
        return listenerList.getListeners(CaretListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * is lazily created using the parameters passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * the fire method.  The listener list is processed in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * last-to-first manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    protected void fireCaretUpdate(CaretEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (listeners[i]==CaretListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                ((CaretListener)listeners[i+1]).caretUpdate(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Associates the editor with a text document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * The currently registered factory is used to build a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * the document, which gets displayed by the editor after revalidation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * A PropertyChange event ("document") is propagated to each listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param doc  the document to display/edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @see #getDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *  description: the text document model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void setDocument(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Document old = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * aquire a read lock on the old model to prevent notification of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * mutations while we disconnecting the old model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (old instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                ((AbstractDocument)old).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                model.removeDocumentListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    ((AccessibleJTextComponent)accessibleContext));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (inputMethodRequestsHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                model.removeDocumentListener((DocumentListener)inputMethodRequestsHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            model = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            // Set the document's run direction property to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // component's ComponentOrientation property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            Boolean runDir = getComponentOrientation().isLeftToRight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                             ? TextAttribute.RUN_DIRECTION_LTR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                             : TextAttribute.RUN_DIRECTION_RTL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (runDir != doc.getProperty(TextAttribute.RUN_DIRECTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                doc.putProperty(TextAttribute.RUN_DIRECTION, runDir );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            firePropertyChange("document", old, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (old instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                ((AbstractDocument)old).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            model.addDocumentListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                ((AccessibleJTextComponent)accessibleContext));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (inputMethodRequestsHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            model.addDocumentListener((DocumentListener)inputMethodRequestsHandler);
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
     * Fetches the model associated with the editor.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * primarily for the UI to get at the minimal amount of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * state required to be a text editor.  Subclasses will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * return the actual type of the model which will typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * be something that extends Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // Override of Component.setComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void setComponentOrientation( ComponentOrientation o ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // Set the document's run direction property to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // ComponentOrientation property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if( doc !=  null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            Boolean runDir = o.isLeftToRight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                             ? TextAttribute.RUN_DIRECTION_LTR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                             : TextAttribute.RUN_DIRECTION_RTL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            doc.putProperty( TextAttribute.RUN_DIRECTION, runDir );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        super.setComponentOrientation( o );
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
     * Fetches the command list for the editor.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * the list of commands supported by the plugged-in UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * augmented by the collection of commands that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * editor itself supports.  These are useful for binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * to events, such as in a keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @return the command list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public Action[] getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return getUI().getEditorKit(this).getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Sets margin space between the text component's border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * and its text.  The text component's default <code>Border</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * object will use this value to create the proper margin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * However, if a non-default border is set on the text component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * it is that <code>Border</code> object's responsibility to create the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * appropriate margin space (else this property will effectively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * be ignored).  This causes a redraw of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * A PropertyChange event ("margin") is sent to all listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param m the space between the border and the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *  description: desired space between the border and text area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public void setMargin(Insets m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        Insets old = margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        margin = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        firePropertyChange("margin", old, m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        invalidate();
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
     * Returns the margin between the text component's border and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * its text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @return the margin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public Insets getMargin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Sets the <code>NavigationFilter</code>. <code>NavigationFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * is used by <code>DefaultCaret</code> and the default cursor movement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * actions as a way to restrict the cursor movement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    public void setNavigationFilter(NavigationFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        navigationFilter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns the <code>NavigationFilter</code>. <code>NavigationFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * is used by <code>DefaultCaret</code> and the default cursor movement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * actions as a way to restrict the cursor movement. A null return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * implies the cursor movement and selection should not be restricted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @return the NavigationFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public NavigationFilter getNavigationFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return navigationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Fetches the caret that allows text-oriented navigation over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @return the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 410
diff changeset
   573
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public Caret getCaret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Sets the caret to be used.  By default this will be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * by the UI that gets installed.  This can be changed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * a custom caret if desired.  Setting the caret results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * PropertyChange event ("caret") being fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param c the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @see #getCaret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *  description: the caret used to select/navigate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public void setCaret(Caret c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (caret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            caret.removeChangeListener(caretEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            caret.deinstall(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        Caret old = caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        caret = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (caret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            caret.install(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            caret.addChangeListener(caretEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        firePropertyChange("caret", old, caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Fetches the object responsible for making highlights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @return the highlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public Highlighter getHighlighter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return highlighter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Sets the highlighter to be used.  By default this will be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * by the UI that gets installed.  This can be changed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * a custom highlighter if desired.  The highlighter can be set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <code>null</code> to disable it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * A PropertyChange event ("highlighter") is fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * when a new highlighter is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @param h the highlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @see #getHighlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *  description: object responsible for background highlights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public void setHighlighter(Highlighter h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (highlighter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            highlighter.deinstall(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        Highlighter old = highlighter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        highlighter = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (highlighter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            highlighter.install(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        firePropertyChange("highlighter", old, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Sets the keymap to use for binding events to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * actions.  Setting to <code>null</code> effectively disables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * keyboard input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * A PropertyChange event ("keymap") is fired when a new keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param map the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @see #getKeymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *  description: set of key event to action bindings to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public void setKeymap(Keymap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        Keymap old = keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        keymap = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        firePropertyChange("keymap", old, keymap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        updateInputMap(old, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Turns on or off automatic drag handling. In order to enable automatic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * drag handling, this property should be set to {@code true}, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * component's {@code TransferHandler} needs to be {@code non-null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * The default value of the {@code dragEnabled} property is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * The job of honoring this property, and recognizing a user drag gesture,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * lies with the look and feel implementation, and in particular, the component's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * {@code TextUI}. When automatic drag handling is enabled, most look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * feels (including those that subclass {@code BasicLookAndFeel}) begin a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * drag and drop operation whenever the user presses the mouse button over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * a selection and then moves the mouse a few pixels. Setting this property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * {@code true} can therefore have a subtle effect on how selections behave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * If a look and feel is used that ignores this property, you can still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * begin a drag and drop operation by calling {@code exportAsDrag} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * component's {@code TransferHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @param b whether or not to enable automatic drag handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *            <code>b</code> is <code>true</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *            <code>GraphicsEnvironment.isHeadless()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @see #getDragEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @see #setTransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @see TransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *  description: determines whether automatic drag handling is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *        bound: false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public void setDragEnabled(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (b && GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        dragEnabled = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Returns whether or not automatic drag handling is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return the value of the {@code dragEnabled} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @see #setDragEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public boolean getDragEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return dragEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * Sets the drop mode for this component. For backward compatibility,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * the default for this property is <code>DropMode.USE_SELECTION</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Usage of <code>DropMode.INSERT</code> is recommended, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * for an improved user experience. It offers similar behavior of dropping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * between text locations, but does so without affecting the actual text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * selection and caret location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <code>JTextComponents</code> support the following drop modes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *    <li><code>DropMode.USE_SELECTION</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *    <li><code>DropMode.INSERT</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * The drop mode is only meaningful if this component has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * <code>TransferHandler</code> that accepts drops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @param dropMode the drop mode to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @throws IllegalArgumentException if the drop mode is unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *         or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @see #getDropMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @see #getDropLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @see #setTransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @see javax.swing.TransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public final void setDropMode(DropMode dropMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (dropMode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            switch (dropMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                case USE_SELECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                case INSERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    this.dropMode = dropMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Returns the drop mode for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return the drop mode for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @see #setDropMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public final DropMode getDropMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        return dropMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   762
    static {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   763
        SwingAccessor.setJTextComponentAccessor(
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   764
            new SwingAccessor.JTextComponentAccessor() {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   765
                public TransferHandler.DropLocation dropLocationForPoint(JTextComponent textComp,
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   766
                                                                         Point p)
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   767
                {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   768
                    return textComp.dropLocationForPoint(p);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   769
                }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   770
                public Object setDropLocation(JTextComponent textComp,
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   771
                                              TransferHandler.DropLocation location,
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   772
                                              Object state, boolean forDrop)
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   773
                {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   774
                    return textComp.setDropLocation(location, state, forDrop);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   775
                }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   776
            });
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   777
    }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1287
diff changeset
   778
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Calculates a drop location in this component, representing where a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * drop at the given point should insert data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Note: This method is meant to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <code>JComponent.dropLocationForPoint()</code>, which is package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * in javax.swing. <code>TransferHandler</code> will detect text components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * and call this method instead via reflection. It's name should therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * not be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @param p the point to calculate a drop location for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @return the drop location, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    DropLocation dropLocationForPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        Position.Bias[] bias = new Position.Bias[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        int index = getUI().viewToModel(this, p, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // viewToModel currently returns null for some HTML content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        // when the point is within the component's top inset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (bias[0] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return new DropLocation(p, index, bias[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Called to set or clear the drop location during a DnD operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * In some cases, the component may need to use it's internal selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * temporarily to indicate the drop location. To help facilitate this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * this method returns and accepts as a parameter a state object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * This state object can be used to store, and later restore, the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * state. Whatever this method returns will be passed back to it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * future calls, as the state parameter. If it wants the DnD system to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * continue storing the same state, it must pass it back every time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Here's how this is used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Let's say that on the first call to this method the component decides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * to save some state (because it is about to use the selection to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * a drop index). It can return a state object to the caller encapsulating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * any saved selection state. On a second call, let's say the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * is being changed to something else. The component doesn't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * restore anything yet, so it simply passes back the same state object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * to have the DnD system continue storing it. Finally, let's say this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * method is messaged with <code>null</code>. This means DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * is finished with this component for now, meaning it should restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * state. At this point, it can use the state parameter to restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * said state, and of course return <code>null</code> since there's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * no longer anything to store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * Note: This method is meant to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * <code>JComponent.setDropLocation()</code>, which is package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * in javax.swing. <code>TransferHandler</code> will detect text components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * and call this method instead via reflection. It's name should therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * not be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @param location the drop location (as calculated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *        <code>dropLocationForPoint</code>) or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *        if there's no longer a valid drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @param state the state object saved earlier for this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *        or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param forDrop whether or not the method is being called because an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *        actual drop occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @return any saved state for this component, or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    Object setDropLocation(TransferHandler.DropLocation location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                           Object state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                           boolean forDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        Object retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        DropLocation textLocation = (DropLocation)location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        if (dropMode == DropMode.USE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (textLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                if (state != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                     * This object represents the state saved earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                     *     If the caret is a DefaultCaret it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                     *     an Object array containing, in order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                     *         - the saved caret mark (Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                     *         - the saved caret dot (Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                     *         - the saved caret visibility (Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                     *         - the saved mark bias (Position.Bias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                     *         - the saved dot bias (Position.Bias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                     *     If the caret is not a DefaultCaret it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                     *     be similar, but will not contain the dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                     *     or mark bias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    Object[] vals = (Object[])state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    if (!forDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        if (caret instanceof DefaultCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                            ((DefaultCaret)caret).setDot(((Integer)vals[0]).intValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                                                         (Position.Bias)vals[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                            ((DefaultCaret)caret).moveDot(((Integer)vals[1]).intValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                                                         (Position.Bias)vals[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                            caret.setDot(((Integer)vals[0]).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                            caret.moveDot(((Integer)vals[1]).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    caret.setVisible(((Boolean)vals[2]).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                if (dropLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    boolean visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    if (caret instanceof DefaultCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                        DefaultCaret dc = (DefaultCaret)caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        visible = dc.isActive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        retVal = new Object[] {Integer.valueOf(dc.getMark()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                               Integer.valueOf(dc.getDot()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                               Boolean.valueOf(visible),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                               dc.getMarkBias(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                                               dc.getDotBias()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        visible = caret.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        retVal = new Object[] {Integer.valueOf(caret.getMark()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                                               Integer.valueOf(caret.getDot()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                               Boolean.valueOf(visible)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    caret.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    retVal = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                if (caret instanceof DefaultCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    ((DefaultCaret)caret).setDot(textLocation.getIndex(), textLocation.getBias());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    caret.setDot(textLocation.getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            if (textLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                if (state != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    caret.setVisible(((Boolean)state).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                if (dropLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    boolean visible = caret instanceof DefaultCaret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                                      ? ((DefaultCaret)caret).isActive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                                      : caret.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    retVal = Boolean.valueOf(visible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    caret.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    retVal = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        DropLocation old = dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        dropLocation = textLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        firePropertyChange("dropLocation", old, dropLocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Returns the location that this component should visually indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * as the drop location during a DnD operation over the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * or {@code null} if no location is to currently be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * This method is not meant for querying the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * from a {@code TransferHandler}, as the drop location is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * set after the {@code TransferHandler}'s <code>canImport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * has returned and has allowed for the location to be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * When this property changes, a property change event with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * name "dropLocation" is fired by the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @return the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @see #setDropMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @see TransferHandler#canImport(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    public final DropLocation getDropLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        return dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Updates the <code>InputMap</code>s in response to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * <code>Keymap</code> change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @param oldKm  the old <code>Keymap</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @param newKm  the new <code>Keymap</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    void updateInputMap(Keymap oldKm, Keymap newKm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        // Locate the current KeymapWrapper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        InputMap km = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        InputMap last = km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        while (km != null && !(km instanceof KeymapWrapper)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            last = km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            km = km.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        if (km != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            // Found it, tweak the InputMap that points to it, as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            // as anything it points to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            if (newKm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                if (last != km) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    last.setParent(km.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    last.setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                InputMap newKM = new KeymapWrapper(newKm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                last.setParent(newKM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                if (last != km) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    newKM.setParent(km.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        else if (newKm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            km = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            if (km != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                // Couldn't find it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                // Set the parent of WHEN_FOCUSED InputMap to be the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                InputMap newKM = new KeymapWrapper(newKm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                newKM.setParent(km.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                km.setParent(newKM);
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
        // Do the same thing with the ActionMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        ActionMap am = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        ActionMap lastAM = am;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        while (am != null && !(am instanceof KeymapActionMap)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            lastAM = am;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            am = am.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (am != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            // Found it, tweak the Actionap that points to it, as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            // as anything it points to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            if (newKm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                if (lastAM != am) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    lastAM.setParent(am.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    lastAM.setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                ActionMap newAM = new KeymapActionMap(newKm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                lastAM.setParent(newAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                if (lastAM != am) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    newAM.setParent(am.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        else if (newKm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            am = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            if (am != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                // Couldn't find it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                // Set the parent of ActionMap to be the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                ActionMap newAM = new KeymapActionMap(newKm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                newAM.setParent(am.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                am.setParent(newAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * Fetches the keymap currently active in this text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @return the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    public Keymap getKeymap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * Adds a new keymap into the keymap hierarchy.  Keymap bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * resolve from bottom up so an attribute specified in a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * will override an attribute specified in the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param nm   the name of the keymap (must be unique within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *   collection of named keymaps in the document); the name may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *   be <code>null</code> if the keymap is unnamed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *   but the caller is responsible for managing the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *   returned as an unnamed keymap can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *   be fetched by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @param parent the parent keymap; this may be <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *   unspecified bindings need not be resolved in some other keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @return the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public static Keymap addKeymap(String nm, Keymap parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        Keymap map = new DefaultKeymap(nm, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        if (nm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            // add a named keymap, a class of bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            getKeymapTable().put(nm, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Removes a named keymap previously added to the document.  Keymaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * with <code>null</code> names may not be removed in this way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @param nm  the name of the keymap to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @return the keymap that was removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    public static Keymap removeKeymap(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        return getKeymapTable().remove(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * Fetches a named keymap previously added to the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * This does not work with <code>null</code>-named keymaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @param nm  the name of the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @return the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    public static Keymap getKeymap(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        return getKeymapTable().get(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    private static HashMap<String,Keymap> getKeymapTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        synchronized (KEYMAP_TABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            HashMap<String,Keymap> keymapTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                (HashMap<String,Keymap>)appContext.get(KEYMAP_TABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            if (keymapTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                keymapTable = new HashMap<String,Keymap>(17);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                appContext.put(KEYMAP_TABLE, keymapTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                //initialize default keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                Keymap binding = addKeymap(DEFAULT_KEYMAP, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                binding.setDefaultAction(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                                         DefaultEditorKit.DefaultKeyTypedAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            return keymapTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Binding record for creating key bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    public static class KeyBinding {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         * The key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        public KeyStroke key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * The name of the action for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        public String actionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
         * Creates a new key binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
         * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
         * @param actionName the name of the action for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        public KeyBinding(KeyStroke key, String actionName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            this.actionName = actionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
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
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Loads a keymap with a bunch of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * bindings.  This can be used to take a static table of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * definitions and load them into some keymap.  The following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * example illustrates an example of binding some keys to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * the cut, copy, and paste actions associated with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * JTextComponent.  A code fragment to accomplish
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * this might look as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *   static final JTextComponent.KeyBinding[] defaultBindings = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *     new JTextComponent.KeyBinding(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *       KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *       DefaultEditorKit.copyAction),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *     new JTextComponent.KeyBinding(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *       KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *       DefaultEditorKit.pasteAction),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *     new JTextComponent.KeyBinding(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *       KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *       DefaultEditorKit.cutAction),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *   JTextComponent c = new JTextPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     *   Keymap k = c.getKeymap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *   JTextComponent.loadKeymap(k, defaultBindings, c.getActions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * The sets of bindings and actions may be empty but must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * non-<code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @param map the keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @param bindings the bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @param actions the set of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    public static void loadKeymap(Keymap map, KeyBinding[] bindings, Action[] actions) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1189
        Hashtable<String, Action> h = new Hashtable<String, Action>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1190
        for (Action a : actions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            String value = (String)a.getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            h.put((value!=null ? value:""), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1194
        for (KeyBinding binding : bindings) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1195
            Action a = h.get(binding.actionName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            if (a != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1197
                map.addActionForKeyStroke(binding.key, a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * Returns true if <code>klass</code> is NOT a JTextComponent and it or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * one of its superclasses (stoping at JTextComponent) overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <code>processInputMethodEvent</code>. It is assumed this will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * invoked from within a <code>doPrivileged</code>, and it is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * assumed <code>klass</code> extends <code>JTextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1209
    private static Boolean isProcessInputMethodEventOverridden(Class<?> klass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        if (klass == JTextComponent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  1213
        Boolean retValue = overrideMap.get(klass.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        if (retValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        Boolean sOverriden = isProcessInputMethodEventOverridden(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                                       klass.getSuperclass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        if (sOverriden.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            // If our superclass has overriden it, then by definition klass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            // overrides it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            overrideMap.put(klass.getName(), sOverriden);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return sOverriden;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        // klass's superclass didn't override it, check for an override in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        // klass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            Class[] classes = new Class[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            classes[0] = InputMethodEvent.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            Method m = klass.getDeclaredMethod("processInputMethodEvent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                                               classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            retValue = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            retValue = Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        overrideMap.put(klass.getName(), retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * Fetches the current color used to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    public Color getCaretColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return caretColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Sets the current color used to render the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * Setting to <code>null</code> effectively restores the default color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Setting the color results in a PropertyChange event ("caretColor")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * being fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @param c the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @see #getCaretColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *  description: the color used to render the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    public void setCaretColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        Color old = caretColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        caretColor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        firePropertyChange("caretColor", old, caretColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * Fetches the current color used to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    public Color getSelectionColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        return selectionColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * Sets the current color used to render the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * Setting the color to <code>null</code> is the same as setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * <code>Color.white</code>.  Setting the color results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * PropertyChange event ("selectionColor").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * @param c the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @see #getSelectionColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *  description: color used to render selection background
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    public void setSelectionColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        Color old = selectionColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        selectionColor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        firePropertyChange("selectionColor", old, selectionColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Fetches the current color used to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    public Color getSelectedTextColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        return selectedTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * Sets the current color used to render the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * Setting the color to <code>null</code> is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * <code>Color.black</code>. Setting the color results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * PropertyChange event ("selectedTextColor") being fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * @param c the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * @see #getSelectedTextColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *  description: color used to render selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    public void setSelectedTextColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        Color old = selectedTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        selectedTextColor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        firePropertyChange("selectedTextColor", old, selectedTextColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * Fetches the current color used to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * disabled text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    public Color getDisabledTextColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        return disabledTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * Sets the current color used to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * disabled text.  Setting the color fires off a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * PropertyChange event ("disabledTextColor").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * @param c the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * @see #getDisabledTextColor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     *  description: color used to render disabled text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    public void setDisabledTextColor(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        Color old = disabledTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        disabledTextColor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        firePropertyChange("disabledTextColor", old, disabledTextColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * Replaces the currently selected content with new content
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * represented by the given string.  If there is no selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * this amounts to an insert of the given text.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * is no replacement text this amounts to a removal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * current selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * This is the method that is used by the default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * of the action for inserting content that gets bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * keymap actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * @param content  the content to replace the selection with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    public void replaceSelection(String content) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                boolean composedTextSaved = saveComposedText(caret.getDot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                int p0 = Math.min(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                int p1 = Math.max(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    ((AbstractDocument)doc).replace(p0, p1 - p0, content,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                        doc.remove(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    if (content != null && content.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                        doc.insertString(p0, content, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                if (composedTextSaved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    restoreComposedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * Fetches a portion of the text represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * component.  Returns an empty string if length is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @param offs the offset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @param len the length >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * @exception BadLocationException if the offset or length are invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    public String getText(int offs, int len) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        return getDocument().getText(offs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * Converts the given location in the model to a place in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * the view coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * The component must have a positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * this translation to be computed (i.e. layout cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * be computed until the component has been sized).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * component does not have to be visible or painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * @param pos the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * @return the coordinates as a rectangle, with (r.x, r.y) as the location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     *   in the coordinate system, or null if the component does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *   not yet have a positive size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * @exception BadLocationException if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @see TextUI#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    public Rectangle modelToView(int pos) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        return getUI().modelToView(this, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * Converts the given place in the view coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * to the nearest representative location in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * The component must have a positive size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * this translation to be computed (i.e. layout cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * be computed until the component has been sized).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * component does not have to be visible or painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @param pt the location in the view to translate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * @return the offset >= 0 from the start of the document,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     *   or -1 if the component does not yet have a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     *   size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @see TextUI#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    public int viewToModel(Point pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        return getUI().viewToModel(this, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * Transfers the currently selected range in the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * text model to the system clipboard, removing the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * from the model.  The current selection is reset.  Does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * for <code>null</code> selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * @see java.awt.datatransfer.Clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    public void cut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        if (isEditable() && isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            invokeAction("cut", TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * Transfers the currently selected range in the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * text model to the system clipboard, leaving the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * in the text model.  The current selection remains intact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * Does nothing for <code>null</code> selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * @see java.awt.datatransfer.Clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    public void copy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        invokeAction("copy", TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * Transfers the contents of the system clipboard into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * associated text model.  If there is a selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * associated view, it is replaced with the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * clipboard.  If there is no selection, the clipboard contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * are inserted in front of the current insert position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * the associated view.  If the clipboard is empty, does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * @see #replaceSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * @see java.awt.datatransfer.Clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    public void paste() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        if (isEditable() && isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            invokeAction("paste", TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * This is a conveniance method that is only useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * <code>cut</code>, <code>copy</code> and <code>paste</code>.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * an <code>Action</code> with the name <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * exist in the <code>ActionMap</code>, this will attemp to install a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * <code>TransferHandler</code> and then use <code>altAction</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    private void invokeAction(String name, Action altAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        ActionMap map = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        Action action = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            action = map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            installDefaultTransferHandlerIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            action = altAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        action.actionPerformed(new ActionEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                               ActionEvent.ACTION_PERFORMED, (String)action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                               getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                               EventQueue.getMostRecentEventTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                               getCurrentEventModifiers()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * If the current <code>TransferHandler</code> is null, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * install a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    private void installDefaultTransferHandlerIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        if (getTransferHandler() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            if (defaultTransferHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                defaultTransferHandler = new DefaultTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            setTransferHandler(defaultTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * Moves the caret to a new position, leaving behind a mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * defined by the last time <code>setCaretPosition</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * called.  This forms a selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * If the document is <code>null</code>, does nothing. The position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * must be between 0 and the length of the component's text or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * @param pos the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * @exception    IllegalArgumentException if the value supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *               for <code>position</code> is less than zero or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     *               than the component's text length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * @see #setCaretPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    public void moveCaretPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            if (pos > doc.getLength() || pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                throw new IllegalArgumentException("bad position: " + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            caret.moveDot(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * The bound property name for the focus accelerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * Sets the key accelerator that will cause the receiving text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * component to get the focus.  The accelerator will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * key combination of the <em>alt</em> key and the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * given (converted to upper case).  By default, there is no focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * accelerator key.  Any previous key accelerator setting will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * superseded.  A '\0' key setting will be registered, and has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * effect of turning off the focus accelerator.  When the new key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * is set, a PropertyChange event (FOCUS_ACCELERATOR_KEY) will be fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * @param aKey the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * @see #getFocusAccelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     *  description: accelerator character used to grab focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    public void setFocusAccelerator(char aKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        aKey = Character.toUpperCase(aKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        char old = focusAccelerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        focusAccelerator = aKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        // Fix for 4341002: value of FOCUS_ACCELERATOR_KEY is wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        // So we fire both FOCUS_ACCELERATOR_KEY, for compatibility,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        // and the correct event here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        firePropertyChange(FOCUS_ACCELERATOR_KEY, old, focusAccelerator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        firePropertyChange("focusAccelerator", old, focusAccelerator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * Returns the key accelerator that will cause the receiving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * text component to get the focus.  Return '\0' if no focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * accelerator has been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * @return the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    public char getFocusAccelerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        return focusAccelerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * Initializes from a stream.  This creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * model of the type appropriate for the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * and initializes the model from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * By default this will load the model as plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * text.  Previous contents of the model are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * @param in the stream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @param desc an object describing the stream; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *   might be a string, a File, a URL, etc.  Some kinds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *   of documents (such as html for example) might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *   able to make use of this information; if non-<code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *   it is added as a property of the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * @exception IOException as thrown by the stream being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     *  used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * @see EditorKit#createDefaultDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @see #setDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @see PlainDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    public void read(Reader in, Object desc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        EditorKit kit = getUI().getEditorKit(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        Document doc = kit.createDefaultDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        if (desc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            doc.putProperty(Document.StreamDescriptionProperty, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            kit.read(in, doc, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * Stores the contents of the model into the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * stream.  By default this will store the model as plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @exception IOException on any I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    public void write(Writer out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            getUI().getEditorKit(this).write(out, doc, 0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    public void removeNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        super.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        if (getFocusedComponent() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            AppContext.getAppContext().remove(FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    // --- java.awt.TextComponent methods ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * Sets the position of the text insertion caret for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * <code>TextComponent</code>.  Note that the caret tracks change,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * so this may move if the underlying text of the component is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * If the document is <code>null</code>, does nothing. The position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * must be between 0 and the length of the component's text or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * @param position the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * @exception    IllegalArgumentException if the value supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     *               for <code>position</code> is less than zero or greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     *               than the component's text length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * description: the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    public void setCaretPosition(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            if (position > doc.getLength() || position < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                throw new IllegalArgumentException("bad position: " + position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            caret.setDot(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * Returns the position of the text insertion caret for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * @return the position of the text insertion caret for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     *  text component >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 410
diff changeset
  1691
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        return caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * Sets the text of this <code>TextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * to the specified text.  If the text is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * or empty, has the effect of simply deleting the old text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * When text has been inserted, the resulting caret location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * is determined by the implementation of the caret class.
410
4bc25999f850 4765383: JTextArea.append(String) not thread safe
peterz
parents: 2
diff changeset
  1702
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * Note that text is not a bound property, so no <code>PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * </code> is fired when it changes. To listen for changes to the text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * use <code>DocumentListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * @param t the new text to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * @see #getText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * @see DefaultCaret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * description: the text of this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    public void setText(String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                ((AbstractDocument)doc).replace(0, doc.getLength(), t,null);
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
                doc.remove(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                doc.insertString(0, t, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * Returns the text contained in this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * If the underlying document is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * will give a <code>NullPointerException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * Note that text is not a bound property, so no <code>PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * </code> is fired when it changes. To listen for changes to the text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * use <code>DocumentListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * @exception NullPointerException if the document is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * @see #setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        String txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            txt = doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            txt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        return txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * Returns the selected text contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * <code>TextComponent</code>.  If the selection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * <code>null</code> or the document empty, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * @exception IllegalArgumentException if the selection doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     *  have a valid mapping into the document for some reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * @see #setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    public String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        String txt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        int p0 = Math.min(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        int p1 = Math.max(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                txt = doc.getText(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        return txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * Returns the boolean indicating whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * <code>TextComponent</code> is editable or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * @return the boolean value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * @see #setEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    public boolean isEditable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        return editable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * Sets the specified boolean to indicate whether or not this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * <code>TextComponent</code> should be editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * A PropertyChange event ("editable") is fired when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * state is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * @param b the boolean to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * @see #isEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * description: specifies if the text can be edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    public void setEditable(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        if (b != editable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            boolean oldVal = editable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            editable = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            enableInputMethods(editable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            firePropertyChange("editable", Boolean.valueOf(oldVal), Boolean.valueOf(editable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * Returns the selected text's start position.  Return 0 for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * empty document, or the value of dot if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * @return the start position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 410
diff changeset
  1817
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        int start = Math.min(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        return start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * Sets the selection start to the specified position.  The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * starting point is constrained to be before or at the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * selection end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * This is available for backward compatibility to code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * that called this method on <code>java.awt.TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * This is implemented to forward to the <code>Caret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * implementation which is where the actual selection is maintained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * @param selectionStart the start position of the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * description: starting location of the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    public void setSelectionStart(int selectionStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        /* Route through select method to enforce consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
         * between selectionStart and selectionEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        select(selectionStart, getSelectionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * Returns the selected text's end position.  Return 0 if the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * is empty, or the value of dot if there is no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * @return the end position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 410
diff changeset
  1850
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        int end = Math.max(caret.getDot(), caret.getMark());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        return end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * Sets the selection end to the specified position.  The new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * end point is constrained to be at or after the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * selection start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * This is available for backward compatibility to code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * that called this method on <code>java.awt.TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * This is implemented to forward to the <code>Caret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * implementation which is where the actual selection is maintained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * @param selectionEnd the end position of the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * description: ending location of the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    public void setSelectionEnd(int selectionEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        /* Route through select method to enforce consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * between selectionStart and selectionEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        select(getSelectionStart(), selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * Selects the text between the specified start and end positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * This method sets the start and end positions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * selected text, enforcing the restriction that the start position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * must be greater than or equal to zero.  The end position must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * greater than or equal to the start position, and less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * equal to the length of the text component's text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * If the caller supplies values that are inconsistent or out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * bounds, the method enforces these constraints silently, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * without failure. Specifically, if the start position or end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * position is greater than the length of the text, it is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * equal the text length. If the start position is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * it is reset to zero, and if the end position is less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * start position, it is reset to the start position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * This call is provided for backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * It is routed to a call to <code>setCaretPosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * followed by a call to <code>moveCaretPosition</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * The preferred way to manage selection is by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * those methods directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * @param selectionStart the start position of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * @param selectionEnd the end position of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * @see #setCaretPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * @see #moveCaretPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    public void select(int selectionStart, int selectionEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        // argument adjustment done by java.awt.TextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        int docLength = getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        if (selectionStart < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            selectionStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        if (selectionStart > docLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            selectionStart = docLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        if (selectionEnd > docLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            selectionEnd = docLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        if (selectionEnd < selectionStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            selectionEnd = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        setCaretPosition(selectionStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        moveCaretPosition(selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * Selects all the text in the <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * Does nothing on a <code>null</code> or empty document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    public void selectAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            setCaretPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            moveCaretPosition(doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    // --- Tooltip Methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * Returns the string to be used as the tooltip for <code>event</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * This will return one of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *  <li>If <code>setToolTipText</code> has been invoked with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     *      non-<code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *      value, it will be returned, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     *  <li>The value from invoking <code>getToolTipText</code> on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     *      the UI will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * By default <code>JTextComponent</code> does not register
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * itself with the <code>ToolTipManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * This means that tooltips will NOT be shown from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * <code>TextUI</code> unless <code>registerComponent</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * been invoked on the <code>ToolTipManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * @param event the event in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * @return the string to be used as the tooltip for <code>event</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * @see javax.swing.JComponent#setToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * @see javax.swing.plaf.TextUI#getToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * @see javax.swing.ToolTipManager#registerComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    public String getToolTipText(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        String retValue = super.getToolTipText(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            TextUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                retValue = ui.getToolTipText(this, new Point(event.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                                                             event.getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    // --- Scrollable methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * Returns the preferred size of the viewport for a view component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * This is implemented to do the default behavior of returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * the preferred size of the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @return the <code>preferredSize</code> of a <code>JViewport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * whose view is this <code>Scrollable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    public Dimension getPreferredScrollableViewportSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        return getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * Components that display logical rows or columns should compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * the scroll increment that will completely expose one new row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     * or column, depending on the value of orientation.  Ideally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * components should handle a partially exposed row or column by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * returning the distance required to completely expose the item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * The default implementation of this is to simply return 10% of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     * the visible area.  Subclasses are likely to be able to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * a much more reasonable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * @param visibleRect the view area visible within the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * @param orientation either <code>SwingConstants.VERTICAL</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     *   <code>SwingConstants.HORIZONTAL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * @param direction less than zero to scroll up/left, greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     *   zero for down/right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * @return the "unit" increment for scrolling in the specified direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * @exception IllegalArgumentException for an invalid orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * @see JScrollBar#setUnitIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        switch(orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        case SwingConstants.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            return visibleRect.height / 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        case SwingConstants.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            return visibleRect.width / 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            throw new IllegalArgumentException("Invalid orientation: " + orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * Components that display logical rows or columns should compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * the scroll increment that will completely expose one block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * of rows or columns, depending on the value of orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * The default implementation of this is to simply return the visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * area.  Subclasses will likely be able to provide a much more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * reasonable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * @param visibleRect the view area visible within the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * @param orientation either <code>SwingConstants.VERTICAL</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     *   <code>SwingConstants.HORIZONTAL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * @param direction less than zero to scroll up/left, greater than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     *  for down/right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * @return the "block" increment for scrolling in the specified direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * @exception IllegalArgumentException for an invalid orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * @see JScrollBar#setBlockIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        switch(orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        case SwingConstants.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            return visibleRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        case SwingConstants.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            return visibleRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            throw new IllegalArgumentException("Invalid orientation: " + orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * Returns true if a viewport should always force the width of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * <code>Scrollable</code> to match the width of the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * For example a normal text view that supported line wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * would return true here, since it would be undesirable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * wrapped lines to disappear beyond the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * edge of the viewport.  Note that returning true for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * <code>Scrollable</code> whose ancestor is a <code>JScrollPane</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * effectively disables horizontal scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * Scrolling containers, like <code>JViewport</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * will use this method each time they are validated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * @return true if a viewport should force the <code>Scrollable</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     *   width to match its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    public boolean getScrollableTracksViewportWidth() {
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  2069
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  2070
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  2071
            return parent.getWidth() > getPreferredSize().width;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * Returns true if a viewport should always force the height of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * <code>Scrollable</code> to match the height of the viewport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * For example a columnar text view that flowed text in left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * right columns could effectively disable vertical scrolling by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * returning true here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * Scrolling containers, like <code>JViewport</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * will use this method each time they are validated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * @return true if a viewport should force the Scrollables height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     *   to match its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    public boolean getScrollableTracksViewportHeight() {
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  2090
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  2091
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4278
diff changeset
  2092
            return parent.getHeight() > getPreferredSize().height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
//////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
// Printing Support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
//////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * A convenience print method that displays a print dialog, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * prints this {@code JTextComponent} in <i>interactive</i> mode with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * header or footer text. Note: this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * blocks until printing is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * Note: In <i>headless</i> mode, no dialogs will be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * <p> This method calls the full featured
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * print} method to perform printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * @return {@code true}, unless printing is canceled by the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * @throws PrinterException if an error in the print system causes the job
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     *         to be aborted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * @throws SecurityException if this thread is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     *                           initiate a print job request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    public boolean print() throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        return print(null, null, true, null, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * A convenience print method that displays a print dialog, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * prints this {@code JTextComponent} in <i>interactive</i> mode with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * the specified header and footer text. Note: this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * blocks until printing is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * Note: In <i>headless</i> mode, no dialogs will be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * <p> This method calls the full featured
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * {@link #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * print} method to perform printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * @param headerFormat the text, in {@code MessageFormat}, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *        used as the header, or {@code null} for no header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @param footerFormat the text, in {@code MessageFormat}, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     *        used as the footer, or {@code null} for no footer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * @return {@code true}, unless printing is canceled by the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * @throws PrinterException if an error in the print system causes the job
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     *         to be aborted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * @throws SecurityException if this thread is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     *                           initiate a print job request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * @see #print(MessageFormat, MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * @see java.text.MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    public boolean print(final MessageFormat headerFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            final MessageFormat footerFormat) throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        return print(headerFormat, footerFormat, true, null, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * Prints the content of this {@code JTextComponent}. Note: this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * blocks until printing is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * Page header and footer text can be added to the output by providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * {@code MessageFormat} arguments. The printing code requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * {@code Strings} from the formats, providing a single item which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * included in the formatted string: an {@code Integer} representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * current page number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * {@code showPrintDialog boolean} parameter allows you to specify whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * a print dialog is displayed to the user. When it is, the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * may use the dialog to change printing attributes or even cancel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * print.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * {@code service} allows you to provide the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * {@code PrintService} for the print dialog, or to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * {@code PrintService} to print to when the dialog is not shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * {@code attributes} can be used to provide the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * initial values for the print dialog, or to supply any needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * attributes when the dialog is not shown. {@code attributes} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * be used to control how the job will print, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * <i>duplex</i> or <i>single-sided</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * {@code interactive boolean} parameter allows you to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * whether to perform printing in <i>interactive</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * mode. If {@code true}, a progress dialog, with an abort option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * is displayed for the duration of printing.  This dialog is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * <i>modal</i> when {@code print} is invoked on the <i>Event Dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * Thread</i> and <i>non-modal</i> otherwise. <b>Warning</b>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * calling this method on the <i>Event Dispatch Thread</i> with {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * interactive false} blocks <i>all</i> events, including repaints, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * being processed until printing is complete. It is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * recommended when printing from an application with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     * visible GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
     * Note: In <i>headless</i> mode, {@code showPrintDialog} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * {@code interactive} parameters are ignored and no dialogs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * This method ensures the {@code document} is not mutated during printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * To indicate it visually, {@code setEnabled(false)} is set for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * duration of printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * This method uses {@link #getPrintable} to render document content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * This method is thread-safe, although most Swing methods are not. Please
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * see <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * How to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * <b>Sample Usage</b>. This code snippet shows a cross-platform print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * dialog and then prints the {@code JTextComponent} in <i>interactive</i> mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * unless the user cancels the dialog:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * textComponent.print(new MessageFormat(&quot;My text component header&quot;),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     *     new MessageFormat(&quot;Footer. Page - {0}&quot;), true, null, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * Executing this code off the <i>Event Dispatch Thread</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * performs printing on the <i>background</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * The following pattern might be used for <i>background</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * printing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     *     FutureTask&lt;Boolean&gt; future =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     *         new FutureTask&lt;Boolean&gt;(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     *             new Callable&lt;Boolean&gt;() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     *                 public Boolean call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     *                     return textComponent.print(.....);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     *                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     *             });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     *     executor.execute(future);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * @param headerFormat the text, in {@code MessageFormat}, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     *        used as the header, or {@code null} for no header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * @param footerFormat the text, in {@code MessageFormat}, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     *        used as the footer, or {@code null} for no footer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * @param showPrintDialog {@code true} to display a print dialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     *        {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * @param service initial {@code PrintService}, or {@code null} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     *        default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * @param attributes the job attributes to be applied to the print job, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     *        {@code null} for none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * @param interactive whether to print in an interactive mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * @return {@code true}, unless printing is canceled by the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * @throws PrinterException if an error in the print system causes the job
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     *         to be aborted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * @throws SecurityException if this thread is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     *                           initiate a print job request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     * @see #getPrintable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * @see java.text.MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * @see java.util.concurrent.FutureTask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
    public boolean print(final MessageFormat headerFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            final MessageFormat footerFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            final boolean showPrintDialog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            final PrintService service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
            final PrintRequestAttributeSet attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
            final boolean interactive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        final PrinterJob job = PrinterJob.getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        final Printable printable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
        final PrintingStatus printingStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        final boolean isHeadless = GraphicsEnvironment.isHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        final boolean isEventDispatchThread =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            SwingUtilities.isEventDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        final Printable textPrintable = getPrintable(headerFormat, footerFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        if (interactive && ! isHeadless) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            printingStatus =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                PrintingStatus.createPrintingStatus(this, job);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            printable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                printingStatus.createNotificationPrintable(textPrintable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            printingStatus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            printable = textPrintable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        if (service != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            job.setPrintService(service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        job.setPrintable(printable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        final PrintRequestAttributeSet attr = (attributes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            ? new HashPrintRequestAttributeSet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            : attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        if (showPrintDialog && ! isHeadless && ! job.printDialog(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
         * there are three cases for printing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
         * 1. print non interactively (! interactive || isHeadless)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
         * 2. print interactively off EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
         * 3. print interactively on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
         * 1 and 2 prints on the current thread (3 prints on another thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
         * 2 and 3 deal with PrintingStatusDialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        final Callable<Object> doPrint =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            new Callable<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                public Object call() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                        job.print(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                        if (printingStatus != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                            printingStatus.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        final FutureTask<Object> futurePrinting =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            new FutureTask<Object>(doPrint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        final Runnable runnablePrinting =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    //disable component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                    boolean wasEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    if (isEventDispatchThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                        if (isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                            wasEnabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                            setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                            wasEnabled = SwingUtilities2.submit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                                new Callable<Boolean>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                                    public Boolean call() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                                        boolean rv = isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                                        if (rv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                                            setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                                        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                                }).get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                        } catch (ExecutionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                            Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                            if (cause instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                                throw (Error) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                            if (cause instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                                throw (RuntimeException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                            throw new AssertionError(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                    getDocument().render(futurePrinting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                    //enable component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                    if (wasEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                        if (isEventDispatchThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                            setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                                SwingUtilities2.submit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                                    new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                                            setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                                    }, null).get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                            } catch (ExecutionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                                Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                                if (cause instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                                    throw (Error) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                                if (cause instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                                    throw (RuntimeException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                                throw new AssertionError(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        if (! interactive || isHeadless) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            runnablePrinting.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            if (isEventDispatchThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                (new Thread(runnablePrinting)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                printingStatus.showModal(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                printingStatus.showModal(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                runnablePrinting.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        //the printing is done successfully or otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        //dialog is hidden if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            futurePrinting.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        } catch (ExecutionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            if (cause instanceof PrinterAbortException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                if (printingStatus != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                    && printingStatus.isAborted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                    throw (PrinterAbortException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            } else if (cause instanceof PrinterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                throw (PrinterException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
            } else if (cause instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                throw (RuntimeException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            } else if (cause instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                throw (Error) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                throw new AssertionError(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     * Returns a {@code Printable} to use for printing the content of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     * {@code JTextComponent}. The returned {@code Printable} prints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     * the document as it looks on the screen except being reformatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
     * to fit the paper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * The returned {@code Printable} can be wrapped inside another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     * {@code Printable} in order to create complex reports and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * documents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * The returned {@code Printable} shares the {@code document} with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     * {@code JTextComponent}. It is the responsibility of the developer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * ensure that the {@code document} is not mutated while this {@code Printable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * is used. Printing behavior is undefined when the {@code document} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * mutated during printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * Page header and footer text can be added to the output by providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     * {@code MessageFormat} arguments. The printing code requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     * {@code Strings} from the formats, providing a single item which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * included in the formatted string: an {@code Integer} representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     * current page number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * The returned {@code Printable} when printed, formats the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * document content appropriately for the page size. For correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * line wrapping the {@code imageable width} of all pages must be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * same. See {@link java.awt.print.PageFormat#getImageableWidth}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * This method is thread-safe, although most Swing methods are not. Please
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * see <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * How to Use Threads</A> for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     * The returned {@code Printable} can be printed on any thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * This implementation returned {@code Printable} performs all painting on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     * the <i>Event Dispatch Thread</i>, regardless of what thread it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     * used on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     * @param headerFormat the text, in {@code MessageFormat}, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     *        used as the header, or {@code null} for no header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     * @param footerFormat the text, in {@code MessageFormat}, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     *        used as the footer, or {@code null} for no footer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     * @return a {@code Printable} for use in printing content of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     *         {@code JTextComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     * @see java.awt.print.Printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * @see java.awt.print.PageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * @see javax.swing.text.Document#render(java.lang.Runnable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
    public Printable getPrintable(final MessageFormat headerFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                                  final MessageFormat footerFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        return TextComponentPrintable.getPrintable(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                   this, headerFormat, footerFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     * Gets the <code>AccessibleContext</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * <code>JTextComponent</code>. For text components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * the <code>AccessibleContext</code> takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * <code>AccessibleJTextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     * A new <code>AccessibleJTextComponent</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     * is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * @return an <code>AccessibleJTextComponent</code> that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     *         <code>AccessibleContext</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     *         <code>JTextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
            accessibleContext = new AccessibleJTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     * <code>JTextComponent</code> class.  It provides an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * the Java Accessibility API appropriate to menu user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    public class AccessibleJTextComponent extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
    implements AccessibleText, CaretListener, DocumentListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
               AccessibleAction, AccessibleEditableText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
               AccessibleExtendedText {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        int caretPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        Point oldLocationOnScreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
         * Constructs an AccessibleJTextComponent.  Adds a listener to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
         * caret change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        public AccessibleJTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
            Document doc = JTextComponent.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                doc.addDocumentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            JTextComponent.this.addCaretListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            caretPos = getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                oldLocationOnScreen = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
            } catch (IllegalComponentStateException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            // Fire a ACCESSIBLE_VISIBLE_DATA_PROPERTY PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
            // when the text component moves (e.g., when scrolling).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
            // Using an anonymous class since making AccessibleJTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            // implement ComponentListener would be an API change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            JTextComponent.this.addComponentListener(new ComponentAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                public void componentMoved(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                        Point newLocationOnScreen = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                        firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                                           oldLocationOnScreen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                                           newLocationOnScreen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                        oldLocationOnScreen = newLocationOnScreen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                    } catch (IllegalComponentStateException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
         * Handles caret updates (fire appropriate property change event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
         * which are AccessibleContext.ACCESSIBLE_CARET_PROPERTY and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
         * AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
         * This keeps track of the dot position internally.  When the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
         * moves, the internal position is updated after firing the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
         * @param e the CaretEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        public void caretUpdate(CaretEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            int dot = e.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            int mark = e.getMark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            if (caretPos != dot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                // the caret moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                firePropertyChange(ACCESSIBLE_CARET_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                    new Integer(caretPos), new Integer(dot));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                caretPos = dot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                    oldLocationOnScreen = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                } catch (IllegalComponentStateException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
            if (mark != dot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                // there is a selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                    getSelectedText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        // DocumentListener methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
         * Handles document insert (fire appropriate property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
         * which is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
         * This tracks the changed offset via the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
         * @param e the DocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            final Integer pos = new Integer (e.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
                Runnable doFire = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
                        firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                                           null, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                SwingUtilities.invokeLater(doFire);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
         * Handles document remove (fire appropriate property change event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
         * which is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
         * This tracks the changed offset via the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
         * @param e the DocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            final Integer pos = new Integer (e.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                Runnable doFire = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                        firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                                           null, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                SwingUtilities.invokeLater(doFire);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         * Handles document remove (fire appropriate property change event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
         * which is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
         * This tracks the changed offset via the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
         * @param e the DocumentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        public void changedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            final Integer pos = new Integer (e.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                Runnable doFire = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                        firePropertyChange(ACCESSIBLE_TEXT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                                           null, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                SwingUtilities.invokeLater(doFire);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
         * Gets the state set of the JTextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
         * The AccessibleStateSet of an object is composed of a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
         * unique AccessibleState's.  A change in the AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
         * of an object will cause a PropertyChangeEvent to be fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
         * for the AccessibleContext.ACCESSIBLE_STATE_PROPERTY property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
         * @return an instance of AccessibleStateSet containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
         * current state set of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
         * @see AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
         * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            if (JTextComponent.this.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                states.add(AccessibleState.EDITABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
         * object (AccessibleRole.TEXT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
            return AccessibleRole.TEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
         * Get the AccessibleText associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
         * AccessibleText interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        // --- interface AccessibleText methods ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
         * Many of these methods are just convenience methods; they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
         * just call the equivalent on the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
         * Given a point in local coordinates, return the zero-based index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
         * of the character under that Point.  If the point is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
         * this method returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
         * @param p the Point in local coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
         * @return the zero-based index of the character under Point p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        public int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            return JTextComponent.this.viewToModel(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
             * Gets the editor's drawing rectangle.  Stolen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
             * from the unfortunately named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
             * BasicTextUI.getVisibleEditorRect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
             * @return the bounding box for the root view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
            Rectangle getRootEditorRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
                Rectangle alloc = JTextComponent.this.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                if ((alloc.width > 0) && (alloc.height > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                        alloc.x = alloc.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                        Insets insets = JTextComponent.this.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                        alloc.x += insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                        alloc.y += insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                        alloc.width -= insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                        alloc.height -= insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                        return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
         * Determines the bounding box of the character at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
         * index into the string.  The bounds are returned in local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
         * coordinates.  If the index is invalid a null rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
         * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
         * The screen coordinates returned are "unscrolled coordinates"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
         * if the JTextComponent is contained in a JScrollPane in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
         * case the resulting rectangle should be composed with the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
         * coordinates.  A good algorithm to use is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
         * <nf>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
         * Accessible a:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
         * AccessibleText at = a.getAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
         * AccessibleComponent ac = a.getAccessibleComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
         * Rectangle r = at.getCharacterBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
         * Point p = ac.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
         * r.x += p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
         * r.y += p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
         * </nf>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
         * Note: the JTextComponent must have a valid size (e.g. have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
         * been added to a parent container whose ancestor container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
         * is a valid top-level window) for this method to be able
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
         * to return a meaningful (non-null) value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
         * @param i the index into the String >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
         * @return the screen coordinates of the character's bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
        public Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
            if (i < 0 || i > model.getLength()-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            TextUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            Rectangle rect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
            Rectangle alloc = getRootEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            if (alloc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
            if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                View rootView = ui.getRootView(JTextComponent.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                if (rootView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                    rootView.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                    Shape bounds = rootView.modelToView(i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                                    Position.Bias.Forward, i+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                                    Position.Bias.Backward, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                    rect = (bounds instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                     (Rectangle)bounds : bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                    ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
            return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
         * Returns the number of characters (valid indices)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
         * @return the number of characters >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        public int getCharCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            return model.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
         * Returns the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
         * Note: The character to the right of the caret will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
         * same index value as the offset (the caret is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
         * two characters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
         * @return the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
        public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
            return JTextComponent.this.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
         * Returns the AttributeSet for a given character (at a given index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
         * @param i the zero-based index into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
         * @return the AttributeSet of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        public AttributeSet getCharacterAttribute(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            Element e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
            if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                for (e = model.getDefaultRootElement(); ! e.isLeaf(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                    int index = e.getElementIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
                    e = e.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                    ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            return e.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
         * Returns the start offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
         * Return 0 if the text is empty, or the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
         * if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
         * @return the index into the text of the start of the selection >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            return JTextComponent.this.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
         * Returns the end offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
         * Return 0 if the text is empty, or the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
         * if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
         * @return the index into teh text of the end of the selection >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
            return JTextComponent.this.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
         * Returns the portion of the text that is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
         * @return the text, null if no selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        public String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
            return JTextComponent.this.getSelectedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
       /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
         * IndexedSegment extends Segment adding the offset into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
         * the model the <code>Segment</code> was asked for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
        private class IndexedSegment extends Segment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
             * Offset into the model that the position represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
            public int modelOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        // TIGER - 4170173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
         * Returns the String at a given index. Whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
         * between words is treated as a word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
         * @param part the CHARACTER, WORD, or SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
         * @return the letter, word, or sentence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
            return getAtIndex(part, index, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
         * Returns the String after a given index. Whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
         * between words is treated as a word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
         * @param part the CHARACTER, WORD, or SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
         * @return the letter, word, or sentence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            return getAtIndex(part, index, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
         * Returns the String before a given index. Whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
         * between words is treated a word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
         * @param part the CHARACTER, WORD, or SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
         * @return the letter, word, or sentence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
            return getAtIndex(part, index, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
         * Gets the word, sentence, or character at <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
         * If <code>direction</code> is non-null this will find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
         * next/previous word/sentence/character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        private String getAtIndex(int part, int index, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
            if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                if (index < 0 || index >= model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
                switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
                    if (index + direction < model.getLength() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
                        index + direction >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
                        return model.getText(index + direction, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                case AccessibleText.WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                case AccessibleText.SENTENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                    IndexedSegment seg = getSegmentAt(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                    if (seg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                        if (direction != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                            int next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                            if (direction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                                next = seg.modelOffset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                                next = seg.modelOffset + direction * seg.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                            if (next >= 0 && next <= model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                                seg = getSegmentAt(part, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                                seg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                        if (seg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                            return new String(seg.array, seg.offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                                                  seg.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
                    ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
         * Returns the paragraph element for the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
        private Element getParagraphElement(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            if (model instanceof PlainDocument ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                PlainDocument sdoc = (PlainDocument)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                return sdoc.getParagraphElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
            } else if (model instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                StyledDocument sdoc = (StyledDocument)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
                return sdoc.getParagraphElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            } else {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  3052
                Element para;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                for (para = model.getDefaultRootElement(); ! para.isLeaf(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
                    int pos = para.getElementIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
                    para = para.getElement(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                if (para == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                return para.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
         * Returns a <code>Segment</code> containing the paragraph text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
         * at <code>index</code>, or null if <code>index</code> isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
         * valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        private IndexedSegment getParagraphElementText(int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                                  throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            Element para = getParagraphElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
            if (para != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
                IndexedSegment segment = new IndexedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
                    int length = para.getEndOffset() - para.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
                    model.getText(para.getStartOffset(), length, segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                segment.modelOffset = para.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                return segment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
         * Returns the Segment at <code>index</code> representing either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
         * the paragraph or sentence as identified by <code>part</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
         * null if a valid paragraph/sentence can't be found. The offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
         * will point to the start of the word/sentence in the array, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
         * the modelOffset will point to the location of the word/sentence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
         * in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        private IndexedSegment getSegmentAt(int part, int index) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                                  BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            IndexedSegment seg = getParagraphElementText(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
            if (seg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            BreakIterator iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            case AccessibleText.WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                iterator = BreakIterator.getWordInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
            case AccessibleText.SENTENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                iterator = BreakIterator.getSentenceInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
            seg.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            iterator.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
            int end = iterator.following(index - seg.modelOffset + seg.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
            if (end == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            if (end > seg.offset + seg.count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
            int begin = iterator.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
            if (begin == BreakIterator.DONE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
                         begin >= seg.offset + seg.count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
            seg.modelOffset = seg.modelOffset + begin - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
            seg.offset = begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            seg.count = end - begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
            return seg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
        // begin AccessibleEditableText methods -----
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
         * Returns the AccessibleEditableText interface for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
         * this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
         * @return the AccessibleEditableText interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
        public AccessibleEditableText getAccessibleEditableText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
         * Sets the text contents to the specified string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
         * @param s the string to set the text contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        public void setTextContents(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
            JTextComponent.this.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
         * Inserts the specified string at the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
         * @param index the index in the text where the string will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
         * be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
         * @param s the string to insert in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        public void insertTextAtIndex(int index, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            Document doc = JTextComponent.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
            if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
                    if (s != null && s.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
                        boolean composedTextSaved = saveComposedText(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                        doc.insertString(index, s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                        if (composedTextSaved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                            restoreComposedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
                    UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
         * Returns the text string between two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
         * @return the text string between the indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        public String getTextRange(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            String txt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
            int p0 = Math.min(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
            int p1 = Math.max(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
            if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
                    Document doc = JTextComponent.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
                    txt = doc.getText(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
                    throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
            return txt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
         * Deletes the text between two indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
        public void delete(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
            if (isEditable() && isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                    int p0 = Math.min(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
                    int p1 = Math.max(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                    if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                        doc.remove(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                UIManager.getLookAndFeel().provideErrorFeedback(JTextComponent.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
         * Cuts the text between two indices into the system clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
        public void cut(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
            selectText(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            JTextComponent.this.cut();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
         * Pastes the text from the system clipboard into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
         * starting at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
        public void paste(int startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
            setCaretPosition(startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
            JTextComponent.this.paste();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
         * Replaces the text between two indices with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
         * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
         * @param s the string to replace the text between two indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
        public void replaceText(int startIndex, int endIndex, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
            selectText(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            JTextComponent.this.replaceSelection(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
         * Selects the text between two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
        public void selectText(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            JTextComponent.this.select(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
         * Sets attributes for the text between two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
         * @param startIndex the starting index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
         * @param endIndex the ending index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
         * @param as the attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
         * @see AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
        public void setAttributes(int startIndex, int endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
            AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
            // Fixes bug 4487492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
            Document doc = JTextComponent.this.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
            if (doc != null && doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                StyledDocument sDoc = (StyledDocument)doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                int offset = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                int length = endIndex - startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
                sDoc.setCharacterAttributes(offset, length, as, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
        // ----- end AccessibleEditableText methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
        // ----- begin AccessibleExtendedText methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
// Probably should replace the helper method getAtIndex() to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
// instead an AccessibleTextSequence also for LINE & ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
// and then make the AccessibleText methods get[At|After|Before]Point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
// call this new method instead and return only the string portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
         * Returns the AccessibleTextSequence at a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
         * If <code>direction</code> is non-null this will find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
         * next/previous word/sentence/character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
         * <code>SENTENCE</code>, <code>LINE</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
         * <code>ATTRIBUTE_RUN</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
         * @param direction is either -1, 0, or 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
         * @return an <code>AccessibleTextSequence</code> specifying the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
         * if <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
         * <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
        private AccessibleTextSequence getSequenceAtIndex(int part,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
            int index, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
            if (index < 0 || index >= model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
            if (direction < -1 || direction > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                return null;    // direction must be 1, 0, or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                    ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                AccessibleTextSequence charSequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                    if (index + direction < model.getLength() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
                        index + direction >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
                        charSequence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
                            new AccessibleTextSequence(index + direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
                            index + direction + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                            model.getText(index + direction, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                    // we are intentionally silent; our contract says we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                    // null if there is any failure in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
                    if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
                        ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                return charSequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
            case AccessibleText.WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
            case AccessibleText.SENTENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
                    ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
                AccessibleTextSequence rangeSequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
                    IndexedSegment seg = getSegmentAt(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                    if (seg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                        if (direction != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                            int next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
                            if (direction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                                next = seg.modelOffset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                                next = seg.modelOffset + seg.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                            if (next >= 0 && next <= model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
                                seg = getSegmentAt(part, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                                seg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                        if (seg != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
                            (seg.offset + seg.count) <= model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
                            rangeSequence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
                                new AccessibleTextSequence (seg.offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
                                seg.offset + seg.count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                                new String(seg.array, seg.offset, seg.count));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                        } // else we leave rangeSequence set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
                } catch(BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
                    // we are intentionally silent; our contract says we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
                    // null if there is any failure in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
                    if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
                        ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                return rangeSequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
            case AccessibleExtendedText.LINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
                AccessibleTextSequence lineSequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
                    ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
                    int startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                        Utilities.getRowStart(JTextComponent.this, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                    int endIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                        Utilities.getRowEnd(JTextComponent.this, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                    if (startIndex >= 0 && endIndex >= startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                        if (direction == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                            lineSequence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                                new AccessibleTextSequence(startIndex, endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                                    model.getText(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
                                        endIndex - startIndex + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                        } else if (direction == -1 && startIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                            endIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                                Utilities.getRowEnd(JTextComponent.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                                    startIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                            startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                                Utilities.getRowStart(JTextComponent.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                                    startIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                            if (startIndex >= 0 && endIndex >= startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                                lineSequence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
                                    new AccessibleTextSequence(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
                                        endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
                                        model.getText(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                                            endIndex - startIndex + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
                        } else if (direction == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                         endIndex < model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                            startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                                Utilities.getRowStart(JTextComponent.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                                    endIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                            endIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                                Utilities.getRowEnd(JTextComponent.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                                    endIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                            if (startIndex >= 0 && endIndex >= startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                                lineSequence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                                    new AccessibleTextSequence(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                                        endIndex, model.getText(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                                            endIndex - startIndex + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                        // already validated 'direction' above...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                } catch(BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                    // we are intentionally silent; our contract says we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                    // null if there is any failure in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                        ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                return lineSequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
            case AccessibleExtendedText.ATTRIBUTE_RUN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                // assumptions: (1) that all characters in a single element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                // share the same attribute set; (2) that adjacent elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                // *may* share the same attribute set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                int attributeRunStartIndex, attributeRunEndIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                String runText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                    ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                    attributeRunStartIndex = attributeRunEndIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                     Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                    int tempIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
                    switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                    case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
                        // going backwards, so find left edge of this run -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
                        // that'll be the end of the previous run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
                        // (off-by-one counting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
                        attributeRunEndIndex = getRunEdge(index, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
                        // now set ourselves up to find the left edge of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
                        // prev. run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
                        tempIndex = attributeRunEndIndex - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                        // going forward, so find right edge of this run -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
                        // that'll be the start of the next run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                        // (off-by-one counting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
                        attributeRunStartIndex = getRunEdge(index, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                        // now set ourselves up to find the right edge of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                        // next run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
                        tempIndex = attributeRunStartIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
                        // interested in the current run, so nothing special to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                        // set up in advance...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
                        // only those three values of direction allowed...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
                        throw new AssertionError(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
                    // set the unset edge; if neither set then we're getting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
                    // both edges of the current run around our 'index'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
                    attributeRunStartIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                        (attributeRunStartIndex != Integer.MIN_VALUE) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
                        attributeRunStartIndex : getRunEdge(tempIndex, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
                    attributeRunEndIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                        (attributeRunEndIndex != Integer.MIN_VALUE) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                        attributeRunEndIndex : getRunEdge(tempIndex, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
                    runText = model.getText(attributeRunStartIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
                                            attributeRunEndIndex -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
                                            attributeRunStartIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
                    // we are intentionally silent; our contract says we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                    // null if there is any failure in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                    if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                        ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                return new AccessibleTextSequence(attributeRunStartIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                                                  attributeRunEndIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                                                  runText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
         * Starting at text position <code>index</code>, and going in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
         * <code>direction</code>, return the edge of run that shares the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
         * same <code>AttributeSet</code> and parent element as those at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
         * Note: we assume the document is already locked...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
        private int getRunEdge(int index, int direction) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
         BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
            if (index < 0 || index >= model.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
                throw new BadLocationException("Location out of bounds", index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            // locate the Element at index
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  3554
            Element indexElement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            // locate the Element at our index/offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
            int elementIndex = -1;        // test for initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
            for (indexElement = model.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
                 ! indexElement.isLeaf(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
                elementIndex = indexElement.getElementIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
                indexElement = indexElement.getElement(elementIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
            if (elementIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                throw new AssertionError(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            // cache the AttributeSet and parentElement atindex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
            AttributeSet indexAS = indexElement.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
            Element parent = indexElement.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
            // find the first Element before/after ours w/the same AttributeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
            // if we are already at edge of the first element in our parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
            // then return that edge
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  3572
            Element edgeElement;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
            switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
                int edgeElementIndex = elementIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
                int elementCount = parent.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
                while ((edgeElementIndex + direction) > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
                       ((edgeElementIndex + direction) < elementCount) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
                       parent.getElement(edgeElementIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                       + direction).getAttributes().isEqual(indexAS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                    edgeElementIndex += direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
                edgeElement = parent.getElement(edgeElementIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
                throw new AssertionError(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
            switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
                return edgeElement.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
                return edgeElement.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
                // we already caught this case earlier; this is to satisfy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
                // the compiler...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        // getTextRange() not needed; defined in AccessibleEditableText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
         * Returns the <code>AccessibleTextSequence</code> at a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
         * <code>SENTENCE</code>, <code>LINE</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
         * <code>ATTRIBUTE_RUN</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
        public AccessibleTextSequence getTextSequenceAt(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            return getSequenceAtIndex(part, index, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
         * Returns the <code>AccessibleTextSequence</code> after a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
         * <code>SENTENCE</code>, <code>LINE</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
         * <code>ATTRIBUTE_RUN</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
         * @return an <code>AccessibleTextSequence</code> specifying the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
         * if <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
        public AccessibleTextSequence getTextSequenceAfter(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
            return getSequenceAtIndex(part, index, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
         * Returns the <code>AccessibleTextSequence</code> before a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
         * <code>SENTENCE</code>, <code>LINE</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
         * <code>ATTRIBUTE_RUN</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
         * @return an <code>AccessibleTextSequence</code> specifying the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
         * if <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
        public AccessibleTextSequence getTextSequenceBefore(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
            return getSequenceAtIndex(part, index, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
         * Returns the <code>Rectangle</code> enclosing the text between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
         * two indicies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
         * @param startIndex the start index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
         * @param endIndex the end index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
         * @return the bounding rectangle of the text if the indices are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
        public Rectangle getTextBounds(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
            if (startIndex < 0 || startIndex > model.getLength()-1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
                endIndex < 0 || endIndex > model.getLength()-1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
                startIndex > endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
            TextUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
            Rectangle rect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
            Rectangle alloc = getRootEditorRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
            if (alloc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
            if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
                ((AbstractDocument)model).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
                View rootView = ui.getRootView(JTextComponent.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
                if (rootView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
                    Shape bounds = rootView.modelToView(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
                                    Position.Bias.Forward, endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
                                    Position.Bias.Backward, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
                    rect = (bounds instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                     (Rectangle)bounds : bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
                if (model instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
                    ((AbstractDocument)model).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
        // ----- end AccessibleExtendedText methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
        // --- interface AccessibleAction methods ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
        public AccessibleAction getAccessibleAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
         * Returns the number of accessible actions available in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
         * If there are more than one, the first one is considered the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
         * "default" action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
         * @return the zero-based number of Actions in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
        public int getAccessibleActionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
            Action [] actions = JTextComponent.this.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
            return actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
         * Returns a description of the specified action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
         * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
         * @return a String description of the action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
         * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
        public String getAccessibleActionDescription(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
            Action [] actions = JTextComponent.this.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
            if (i < 0 || i >= actions.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
            return (String)actions[i].getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
         * Performs the specified Action on the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
         * @param i zero-based index of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
         * @return true if the action was performed; otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
         * @see #getAccessibleActionCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
        public boolean doAccessibleAction(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
            Action [] actions = JTextComponent.this.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
            if (i < 0 || i >= actions.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
            ActionEvent ae =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
                new ActionEvent(JTextComponent.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                                ActionEvent.ACTION_PERFORMED, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
                                EventQueue.getMostRecentEventTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
                                getCurrentEventModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
            actions[i].actionPerformed(ae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
        // ----- end AccessibleAction methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
    // --- serialization ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
        caretEvent = new MutableCaretEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
        addMouseListener(caretEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
        addFocusListener(caretEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
    // --- member variables ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
     * The document model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
    private Document model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
     * The caret used to display the insert position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
     * and navigate throughout the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
     * PENDING(prinz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
     * This should be serializable, default installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
     * by UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
    private transient Caret caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
     * Object responsible for restricting the cursor navigation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
    private NavigationFilter navigationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
     * The object responsible for managing highlights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
     * PENDING(prinz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
     * This should be serializable, default installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
     * by UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
    private transient Highlighter highlighter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
     * The current key bindings in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
     * PENDING(prinz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
     * This should be serializable, default installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
     * by UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
    private transient Keymap keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
    private transient MutableCaretEvent caretEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
    private Color caretColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
    private Color selectionColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
    private Color selectedTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
    private Color disabledTextColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    private boolean editable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
    private Insets margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
    private char focusAccelerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
    private boolean dragEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
     * The drop mode for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
    private DropMode dropMode = DropMode.USE_SELECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
     * The drop location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
    private transient DropLocation dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
     * Represents a drop location for <code>JTextComponent</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
     * @see #getDropLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
    public static final class DropLocation extends TransferHandler.DropLocation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
        private final int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
        private final Position.Bias bias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
        private DropLocation(Point p, int index, Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
            super(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
            this.bias = bias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
         * Returns the index where dropped data should be inserted into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
         * associated component. This index represents a position between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
         * characters, as would be interpreted by a caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
         * @return the drop index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
        public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
         * Returns the bias for the drop index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
         * @return the drop bias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
        public Position.Bias getBias() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
            return bias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
         * Returns a string representation of this drop location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
         * This method is intended to be used for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
         * and the content and format of the returned string may vary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
         * between implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
         * @return a string representation of this drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
            return getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                   + "[dropPoint=" + getDropPoint() + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
                   + "index=" + index + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                   + "bias=" + bias + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
     * TransferHandler used if one hasn't been supplied by the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
    private static DefaultTransferHandler defaultTransferHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
     * Maps from class name to Boolean indicating if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
     * <code>processInputMethodEvent</code> has been overriden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  3922
    private static Map<String, Boolean> overrideMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
     * Returns a string representation of this <code>JTextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
     * Overriding <code>paramString</code> to provide information about the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
     * specific new aspects of the JFC components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
     * @return  a string representation of this <code>JTextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
        String editableString = (editable ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
                                 "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
        String caretColorString = (caretColor != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
                                   caretColor.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
        String selectionColorString = (selectionColor != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
                                       selectionColor.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
        String selectedTextColorString = (selectedTextColor != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
                                          selectedTextColor.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
        String disabledTextColorString = (disabledTextColor != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
                                          disabledTextColor.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
        String marginString = (margin != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
                               margin.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
        ",caretColor=" + caretColorString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
        ",disabledTextColor=" + disabledTextColorString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
        ",editable=" + editableString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
        ",margin=" + marginString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
        ",selectedTextColor=" + selectedTextColorString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
        ",selectionColor=" + selectionColorString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
     * A Simple TransferHandler that exports the data as a String, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
     * imports the data from the String clipboard.  This is only used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
     * if the UI hasn't supplied one, which would only happen if someone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
     * hasn't subclassed Basic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
    static class DefaultTransferHandler extends TransferHandler implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
                                        UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
        public void exportToClipboard(JComponent comp, Clipboard clipboard,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
                                      int action) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
            if (comp instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
                JTextComponent text = (JTextComponent)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
                int p0 = text.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
                int p1 = text.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
                if (p0 != p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
                        Document doc = text.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
                        String srcData = doc.getText(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
                        StringSelection contents =new StringSelection(srcData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
                        // this may throw an IllegalStateException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
                        // but it will be caught and handled in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
                        // action that invoked this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
                        clipboard.setContents(contents, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
                        if (action == TransferHandler.MOVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
                            doc.remove(p0, p1 - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
                    } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
        public boolean importData(JComponent comp, Transferable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
            if (comp instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
                DataFlavor flavor = getFlavor(t.getTransferDataFlavors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
                if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
                    InputContext ic = comp.getInputContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
                    if (ic != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
                        ic.endComposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
                        String data = (String)t.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
                        ((JTextComponent)comp).replaceSelection(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
                    } catch (UnsupportedFlavorException ufe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
        public boolean canImport(JComponent comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
                                 DataFlavor[] transferFlavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
            JTextComponent c = (JTextComponent)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
            if (!(c.isEditable() && c.isEnabled())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
            return (getFlavor(transferFlavors) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
        public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
            return NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
        private DataFlavor getFlavor(DataFlavor[] flavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
            if (flavors != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4026
                for (DataFlavor flavor : flavors) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4027
                    if (flavor.equals(DataFlavor.stringFlavor)) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4028
                        return flavor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
     * Returns the JTextComponent that most recently had focus. The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
     * value may currently have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
    static final JTextComponent getFocusedComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
        return (JTextComponent)AppContext.getAppContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
            get(FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
    private int getCurrentEventModifiers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
        int modifiers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
        AWTEvent currentEvent = EventQueue.getCurrentEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
        if (currentEvent instanceof InputEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
            modifiers = ((InputEvent)currentEvent).getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
        } else if (currentEvent instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
            modifiers = ((ActionEvent)currentEvent).getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
        return modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
    private static final Object KEYMAP_TABLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
        new StringBuilder("JTextComponent_KeymapTable");
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
  4058
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
    // member variables used for on-the-spot input method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
    // editing style support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
    private transient InputMethodRequests inputMethodRequestsHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
    private SimpleAttributeSet composedTextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
    private String composedTextContent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
    private Position composedTextStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
    private Position composedTextEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
    private Position latestCommittedTextStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
    private Position latestCommittedTextEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
    private ComposedTextCaret composedTextCaret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
    private transient Caret originalCaret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
     * Set to true after the check for the override of processInputMethodEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
     * has been checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
    private boolean checkedInputOverride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    private boolean needToSendKeyTypedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
    static class DefaultKeymap implements Keymap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
        DefaultKeymap(String nm, Keymap parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
            this.nm = nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
            this.parent = parent;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4084
            bindings = new Hashtable<KeyStroke, Action>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
         * Fetch the default action to fire if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
         * key is typed (ie a KEY_TYPED KeyEvent is received)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
         * and there is no binding for it.  Typically this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
         * would be some action that inserts text so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
         * the keymap doesn't require an action for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
         * possible key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
        public Action getDefaultAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
            if (defaultAction != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
                return defaultAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
            return (parent != null) ? parent.getDefaultAction() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
         * Set the default action to fire if a key is typed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
        public void setDefaultAction(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
            defaultAction = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
            return nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
        public Action getAction(KeyStroke key) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4114
            Action a = bindings.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
            if ((a == null) && (parent != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
                a = parent.getAction(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
        public KeyStroke[] getBoundKeyStrokes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
            KeyStroke[] keys = new KeyStroke[bindings.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
            int i = 0;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4124
            for (Enumeration<KeyStroke> e = bindings.keys() ; e.hasMoreElements() ;) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4125
                keys[i++] = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
        public Action[] getBoundActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
            Action[] actions = new Action[bindings.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
            int i = 0;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4133
            for (Enumeration<Action> e = bindings.elements() ; e.hasMoreElements() ;) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4134
                actions[i++] = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
            return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
        public KeyStroke[] getKeyStrokesForAction(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
            KeyStroke[] retValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
            // Determine local bindings first.
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4145
            Vector<KeyStroke> keyStrokes = null;
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4146
            for (Enumeration<KeyStroke> keys = bindings.keys(); keys.hasMoreElements();) {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4147
                KeyStroke key = keys.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
                if (bindings.get(key) == a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
                    if (keyStrokes == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4150
                        keyStrokes = new Vector<KeyStroke>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
                    keyStrokes.addElement(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
            // See if the parent has any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
                KeyStroke[] pStrokes = parent.getKeyStrokesForAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
                if (pStrokes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
                    // Remove any bindings defined in the parent that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
                    // are locally defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
                    int rCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
                    for (int counter = pStrokes.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
                         counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
                        if (isLocallyDefined(pStrokes[counter])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
                            pStrokes[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
                            rCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
                    if (rCount > 0 && rCount < pStrokes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
                        if (keyStrokes == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4171
                            keyStrokes = new Vector<KeyStroke>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
                        for (int counter = pStrokes.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
                             counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
                            if (pStrokes[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
                                keyStrokes.addElement(pStrokes[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
                    else if (rCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
                        if (keyStrokes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
                            retValue = pStrokes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
                            retValue = new KeyStroke[keyStrokes.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
                                                    pStrokes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
                            keyStrokes.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
                            System.arraycopy(pStrokes, 0, retValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
                                        keyStrokes.size(), pStrokes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
                            keyStrokes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
            if (keyStrokes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
                retValue = new KeyStroke[keyStrokes.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
                keyStrokes.copyInto(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
        public boolean isLocallyDefined(KeyStroke key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
            return bindings.containsKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
        public void addActionForKeyStroke(KeyStroke key, Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
            bindings.put(key, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
        public void removeKeyStrokeBinding(KeyStroke key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
            bindings.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
        public void removeBindings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
            bindings.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
        public Keymap getResolveParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
        public void setResolveParent(Keymap parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
         * String representation of the keymap... potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
         * a very long string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
            return "Keymap[" + nm + "]" + bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
        String nm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
        Keymap parent;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4236
        Hashtable<KeyStroke, Action> bindings;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
        Action defaultAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
     * KeymapWrapper wraps a Keymap inside an InputMap. For KeymapWrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
     * to be useful it must be used with a KeymapActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
     * KeymapWrapper for the most part, is an InputMap with two parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
     * The first parent visited is ALWAYS the Keymap, with the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
     * parent being the parent inherited from InputMap. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
     * <code>keymap.getAction</code> returns null, implying the Keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
     * does not have a binding for the KeyStroke,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
     * the parent is then visited. If the Keymap has a binding, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
     * Action is returned, if not and the KeyStroke represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
     * KeyTyped event and the Keymap has a defaultAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
     * <code>DefaultActionKey</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
     * <p>KeymapActionMap is then able to transate the object passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
     * to either message the Keymap, or message its default implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
    static class KeymapWrapper extends InputMap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
        static final Object DefaultActionKey = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
        private Keymap keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4261
        KeymapWrapper(Keymap keymap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4262
            this.keymap = keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
        public KeyStroke[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
            KeyStroke[] sKeys = super.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
            KeyStroke[] keymapKeys = keymap.getBoundKeyStrokes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
            int sCount = (sKeys == null) ? 0 : sKeys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
            int keymapCount = (keymapKeys == null) ? 0 : keymapKeys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
            if (sCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
                return keymapKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
            if (keymapCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
                return sKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
            KeyStroke[] retValue = new KeyStroke[sCount + keymapCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
            // There may be some duplication here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
            System.arraycopy(sKeys, 0, retValue, 0, sCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
            System.arraycopy(keymapKeys, 0, retValue, sCount, keymapCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
            // There may be some duplication here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
            KeyStroke[] keymapStrokes = keymap.getBoundKeyStrokes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
            int keymapCount = (keymapStrokes == null) ? 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
                               keymapStrokes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
            return super.size() + keymapCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
        public Object get(KeyStroke keyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
            Object retValue = keymap.getAction(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
            if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
                retValue = super.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
                if (retValue == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
                    keyStroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
                    keymap.getDefaultAction() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
                    // Implies this is a KeyTyped event, use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
                    // action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
                    retValue = DefaultActionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
     * Wraps a Keymap inside an ActionMap. This is used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
     * a KeymapWrapper. If <code>get</code> is passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
     * <code>KeymapWrapper.DefaultActionKey</code>, the default action is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
     * returned, otherwise if the key is an Action, it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
    static class KeymapActionMap extends ActionMap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
        private Keymap keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
        KeymapActionMap(Keymap keymap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
            this.keymap = keymap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
        public Object[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
            Object[] sKeys = super.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
            Object[] keymapKeys = keymap.getBoundActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
            int sCount = (sKeys == null) ? 0 : sKeys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
            int keymapCount = (keymapKeys == null) ? 0 : keymapKeys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
            boolean hasDefault = (keymap.getDefaultAction() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
            if (hasDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
                keymapCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
            if (sCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
                if (hasDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
                    Object[] retValue = new Object[keymapCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
                    if (keymapCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
                        System.arraycopy(keymapKeys, 0, retValue, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
                                         keymapCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
                    retValue[keymapCount - 1] = KeymapWrapper.DefaultActionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
                    return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
                return keymapKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
            if (keymapCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
                return sKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
            Object[] retValue = new Object[sCount + keymapCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
            // There may be some duplication here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
            System.arraycopy(sKeys, 0, retValue, 0, sCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
            if (hasDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
                if (keymapCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
                    System.arraycopy(keymapKeys, 0, retValue, sCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
                                     keymapCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
                retValue[sCount + keymapCount - 1] = KeymapWrapper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
                                                 DefaultActionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
                System.arraycopy(keymapKeys, 0, retValue, sCount, keymapCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
            // There may be some duplication here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
            Object[] actions = keymap.getBoundActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
            int keymapCount = (actions == null) ? 0 : actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
            if (keymap.getDefaultAction() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
                keymapCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
            return super.size() + keymapCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
        public Action get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
            Action retValue = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
            if (retValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
                // Try the Keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
                if (key == KeymapWrapper.DefaultActionKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
                    retValue = keymap.getDefaultAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
                else if (key instanceof Action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
                    // This is a little iffy, technically an Action is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
                    // a valid Key. We're assuming the Action came from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
                    // the InputMap though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
                    retValue = (Action)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
    private static final Object FOCUSED_COMPONENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
        new StringBuilder("JTextComponent_FocusedComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
     * The default keymap that will be shared by all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
     * <code>JTextComponent</code> instances unless they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
     * have had a different keymap set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
    public static final String DEFAULT_KEYMAP = "default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4401
     * Event to use when firing a notification of change to caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
     * position.  This is mutable so that the event can be reused
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
     * since caret events can be fairly high in bandwidth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
    static class MutableCaretEvent extends CaretEvent implements ChangeListener, FocusListener, MouseListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
        MutableCaretEvent(JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
            super(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
        final void fire() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
            JTextComponent c = (JTextComponent) getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
                Caret caret = c.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
                dot = caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
                mark = caret.getMark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
                c.fireCaretUpdate(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
        public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4422
            return "dot=" + dot + "," + "mark=" + mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4425
        // --- CaretEvent methods -----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
        public final int getDot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4428
            return dot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
        public final int getMark() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
            return mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
        // --- ChangeListener methods -------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
        public final void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
            if (! dragActive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
                fire();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
        // --- FocusListener methods -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
        public void focusGained(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
            AppContext.getAppContext().put(FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
                                           fe.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
        public void focusLost(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
        // --- MouseListener methods -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
         * Requests focus on the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
         * text component, and try to set the cursor position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
         * @see MouseListener#mousePressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
        public final void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
            dragActive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
         * Called when the mouse is released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
         * @param e the mouse event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
         * @see MouseListener#mouseReleased
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
        public final void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
            dragActive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
            fire();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
        public final void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
        public final void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
        public final void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
        private boolean dragActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
        private int dot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
        private int mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
    // Process any input method events that the component itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
    // recognizes. The default on-the-spot handling for input method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
    // composed(uncommitted) text is done here after all input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
    // method listeners get called for stealing the events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
    protected void processInputMethodEvent(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4497
        // let listeners handle the events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
        super.processInputMethodEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
        if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
            if (! isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
                switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
                case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
                    replaceInputMethodText(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
                    // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
                case InputMethodEvent.CARET_POSITION_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
                    setInputMethodCaretPosition(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
    // Overrides this method to become an active input method client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
    public InputMethodRequests getInputMethodRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4524
        if (inputMethodRequestsHandler == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4525
            inputMethodRequestsHandler = new InputMethodRequestsHandler();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
            if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
                doc.addDocumentListener((DocumentListener)inputMethodRequestsHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
        return inputMethodRequestsHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
    // Overrides this method to watch the listener installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
    public void addInputMethodListener(InputMethodListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
        super.addInputMethodListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
            needToSendKeyTypedEvent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
            checkedInputOverride = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4547
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
    // Default implementation of the InputMethodRequests interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
    class InputMethodRequestsHandler implements InputMethodRequests, DocumentListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
        // --- InputMethodRequests methods ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
        public AttributedCharacterIterator cancelLatestCommittedText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
                                                Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4557
            if ((doc != null) && (latestCommittedTextStart != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
                && (!latestCommittedTextStart.equals(latestCommittedTextEnd))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
                    int startIndex = latestCommittedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
                    int endIndex = latestCommittedTextEnd.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
                    String latestCommittedText =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
                        doc.getText(startIndex, endIndex - startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
                    doc.remove(startIndex, endIndex - startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
                    return new AttributedString(latestCommittedText).getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
                } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
        public AttributedCharacterIterator getCommittedText(int beginIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
                                        int endIndex, Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
            int composedStartIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
            int composedEndIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
            if (composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
                composedStartIndex = composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4577
                composedEndIndex = composedTextEnd.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
            String committed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4581
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
                if (beginIndex < composedStartIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
                    if (endIndex <= composedStartIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4584
                        committed = getText(beginIndex, endIndex - beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
                        int firstPartLength = composedStartIndex - beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
                        committed = getText(beginIndex, firstPartLength) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
                            getText(composedEndIndex, endIndex - beginIndex - firstPartLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
                    committed = getText(beginIndex + (composedEndIndex - composedStartIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
                                        endIndex - beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
            } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
                throw new IllegalArgumentException("Invalid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
            return new AttributedString(committed).getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
        public int getCommittedTextLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
            int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
            if (doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
                length = doc.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4605
                if (composedTextContent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
                    if (composedTextEnd == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
                          || composedTextStart == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4608
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
                         * fix for : 6355666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
                         * this is the case when this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4611
                         * from DocumentListener. At this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4612
                         * composedTextEnd and composedTextStart are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
                         * not defined yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
                        length -= composedTextContent.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
                        length -= composedTextEnd.getOffset() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4618
                            composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
            return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
        public int getInsertPositionOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
            int composedStartIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
            int composedEndIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
            if (composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
                composedStartIndex = composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
                composedEndIndex = composedTextEnd.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4632
            int caretIndex = getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4634
            if (caretIndex < composedStartIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4635
                return caretIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4636
            } else if (caretIndex < composedEndIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4637
                return composedStartIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4638
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
                return caretIndex - (composedEndIndex - composedStartIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
        public TextHitInfo getLocationOffset(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
            if (composedTextAttribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
                Point p = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
                p.x = x - p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
                p.y = y - p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
                int pos = viewToModel(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
                if ((pos >= composedTextStart.getOffset()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
                    (pos <= composedTextEnd.getOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
                    return TextHitInfo.leading(pos - composedTextStart.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4654
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4655
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
        public Rectangle getTextLocation(TextHitInfo offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4661
            Rectangle r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4664
                r = modelToView(getCaretPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4665
                if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4666
                    Point p = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
                    r.translate(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
            } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4670
                r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
            if (r == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
                r = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
        public AttributedCharacterIterator getSelectedText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
                                                Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
            String selection = JTextComponent.this.getSelectedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
            if (selection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
                return new AttributedString(selection).getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
        // --- DocumentListener methods ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
        public void changedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
            latestCommittedTextStart = latestCommittedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
        public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
            latestCommittedTextStart = latestCommittedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4699
        public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4700
            latestCommittedTextStart = latestCommittedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4704
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4705
    // Replaces the current input method (composed) text according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4706
    // the passed input method event. This method also inserts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4707
    // committed text into the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4708
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4709
    private void replaceInputMethodText(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4710
        int commitCount = e.getCommittedCharacterCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4711
        AttributedCharacterIterator text = e.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
        int composedTextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4714
        // old composed text deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4715
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
        if (composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
                doc.remove(composedTextStart.getOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
                           composedTextEnd.getOffset() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
                           composedTextStart.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4721
            } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4722
            composedTextStart = composedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4723
            composedTextAttribute = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4724
            composedTextContent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4728
            text.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4729
            int committedTextStartIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4730
            int committedTextEndIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4732
            // committed text insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4733
            if (commitCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4734
                // Remember latest committed text start index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
                committedTextStartIndex = caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
                // Need to generate KeyTyped events for the committed text for components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4738
                // that are not aware they are active input method clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
                if (shouldSynthensizeKeyEvents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
                    for (char c = text.current(); commitCount > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
                         c = text.next(), commitCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
                        KeyEvent ke = new KeyEvent(this, KeyEvent.KEY_TYPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
                                                   EventQueue.getMostRecentEventTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
                                                   0, KeyEvent.VK_UNDEFINED, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
                        processKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
                } else {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
  4748
                    StringBuilder strBuf = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4749
                    for (char c = text.current(); commitCount > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4750
                         c = text.next(), commitCount--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4751
                        strBuf.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4752
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4754
                    // map it to an ActionEvent
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
  4755
                    mapCommittedTextToAction(strBuf.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4758
                // Remember latest committed text end index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
                committedTextEndIndex = caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
            // new composed text insertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
            composedTextIndex = text.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
            if (composedTextIndex < text.getEndIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
                createComposedTextAttribute(composedTextIndex, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4767
                    replaceSelection(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4768
                    doc.insertString(caret.getDot(), composedTextContent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4769
                                        composedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4770
                    composedTextStart = doc.createPosition(caret.getDot() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4771
                                                composedTextContent.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4772
                    composedTextEnd = doc.createPosition(caret.getDot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4773
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4774
                    composedTextStart = composedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4775
                    composedTextAttribute = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4776
                    composedTextContent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
            // Save the latest committed text information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4781
            if (committedTextStartIndex != committedTextEndIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4782
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4783
                    latestCommittedTextStart = doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4784
                        createPosition(committedTextStartIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4785
                    latestCommittedTextEnd = doc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4786
                        createPosition(committedTextEndIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4787
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4788
                    latestCommittedTextStart =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
                        latestCommittedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4791
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
                latestCommittedTextStart =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4793
                    latestCommittedTextEnd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
    private void createComposedTextAttribute(int composedIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
                                        AttributedCharacterIterator text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
        Document doc = getDocument();
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
  4801
        StringBuilder strBuf = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
        // create attributed string with no attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
        for (char c = text.setIndex(composedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
             c != CharacterIterator.DONE; c = text.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
            strBuf.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
  4809
        composedTextContent = strBuf.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4810
        composedTextAttribute = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
        composedTextAttribute.addAttribute(StyleConstants.ComposedTextAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4812
                new AttributedString(text, composedIndex, text.getEndIndex()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4815
    /**
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4816
     * Saves composed text around the specified position.
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4817
     *
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4818
     * The composed text (if any) around the specified position is saved
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4819
     * in a backing store and removed from the document.
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4820
     *
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4821
     * @param pos  document position to identify the composed text location
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4822
     * @return  {@code true} if the composed text exists and is saved,
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4823
     *          {@code false} otherwise
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4824
     * @see #restoreComposedText
8139
42edb817487b 6935155: @since tag is missing in JTextComponent.save/restoreComposedText
rupashka
parents: 7668
diff changeset
  4825
     * @since 1.7
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4826
     */
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4827
    protected boolean saveComposedText(int pos) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4828
        if (composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4829
            int start = composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4830
            int len = composedTextEnd.getOffset() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4831
                composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4832
            if (pos >= start && pos <= start + len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4833
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4834
                    getDocument().remove(start, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4835
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4836
                } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4839
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4841
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4842
    /**
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4843
     * Restores composed text previously saved by {@code saveComposedText}.
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4844
     *
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4845
     * The saved composed text is inserted back into the document. This method
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4846
     * should be invoked only if {@code saveComposedText} returns {@code true}.
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4847
     *
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4848
     * @see #saveComposedText
8139
42edb817487b 6935155: @since tag is missing in JTextComponent.save/restoreComposedText
rupashka
parents: 7668
diff changeset
  4849
     * @since 1.7
3750
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4850
     */
4195b035138f 6699856: Creating text in a JTextPane using Chinese text causes undesired behavior
gsm
parents: 3737
diff changeset
  4851
    protected void restoreComposedText() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4852
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4853
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4854
            doc.insertString(caret.getDot(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4855
                             composedTextContent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4856
                             composedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4857
            composedTextStart = doc.createPosition(caret.getDot() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4858
                                composedTextContent.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4859
            composedTextEnd = doc.createPosition(caret.getDot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4860
        } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
    // Map committed text to an ActionEvent. If the committed text length is 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
    // treat it as a KeyStroke, otherwise or there is no KeyStroke defined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
    // treat it just as a default action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
    private void mapCommittedTextToAction(String committedText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
        Keymap binding = getKeymap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4870
        if (binding != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4871
            Action a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4872
            if (committedText.length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4873
                KeyStroke k = KeyStroke.getKeyStroke(committedText.charAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4874
                a = binding.getAction(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4878
                a = binding.getDefaultAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
                ActionEvent ae =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
                    new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
                                    committedText,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
                                    EventQueue.getMostRecentEventTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
                                    getCurrentEventModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
                a.actionPerformed(ae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
    // Sets the caret position according to the passed input method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
    // event. Also, sets/resets composed text caret appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
    private void setInputMethodCaretPosition(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
        int dot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4899
        if (composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4900
            dot = composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4901
            if (!(caret instanceof ComposedTextCaret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4902
                if (composedTextCaret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
                    composedTextCaret = new ComposedTextCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4904
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4905
                originalCaret = caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4906
                // Sets composed text caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4907
                exchangeCaret(originalCaret, composedTextCaret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
            TextHitInfo caretPos = e.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
            if (caretPos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4912
                int index = caretPos.getInsertionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
                dot += index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
                if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
                    // Scroll the component if needed so that the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
                    // becomes visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
                        Rectangle d = modelToView(dot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
                        Rectangle end = modelToView(composedTextEnd.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
                        Rectangle b = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
                        d.x += Math.min(end.x - d.x, b.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
                        scrollRectToVisible(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
                    } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
            caret.setDot(dot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
        } else if (caret instanceof ComposedTextCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4928
            dot = caret.getDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4929
            // Restores original caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4930
            exchangeCaret(caret, originalCaret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4931
            caret.setDot(dot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4935
    private void exchangeCaret(Caret oldCaret, Caret newCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4936
        int blinkRate = oldCaret.getBlinkRate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4937
        setCaret(newCaret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4938
        caret.setBlinkRate(blinkRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4939
        caret.setVisible(hasFocus());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4943
     * Returns true if KeyEvents should be synthesized from an InputEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4945
    private boolean shouldSynthensizeKeyEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4946
        if (!checkedInputOverride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
            checkedInputOverride = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4948
            needToSendKeyTypedEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
                             !isProcessInputMethodEventOverridden();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
        return needToSendKeyTypedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4954
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4955
    // Checks whether the client code overrides processInputMethodEvent.  If it is overridden,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4956
    // need not to generate KeyTyped events for committed text. If it's not, behave as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4957
    // passive input method client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4958
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4959
    private boolean isProcessInputMethodEventOverridden() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4960
        if (overrideMap == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4961
            overrideMap = Collections.synchronizedMap(new HashMap<String, Boolean>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4962
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4963
        Boolean retValue = overrideMap.get(getClass().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4965
        if (retValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4966
            return retValue.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4967
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4968
        Boolean ret = AccessController.doPrivileged(new
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4969
                       PrivilegedAction<Boolean>() {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 466
diff changeset
  4970
            public Boolean run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4971
                return isProcessInputMethodEventOverridden(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4972
                                JTextComponent.this.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4974
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
        return ret.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
    // Checks whether a composed text in this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
    boolean composedTextExists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
        return (composedTextStart != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
    // Caret implementation for editing the composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4989
    class ComposedTextCaret extends DefaultCaret implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4990
        Color bg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4992
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
        // Get the background color of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4994
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4995
        public void install(JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4996
            super.install(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4998
            Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4999
            if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
                StyledDocument sDoc = (StyledDocument)doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
                Element elem = sDoc.getCharacterElement(c.composedTextStart.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5002
                AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
                bg = sDoc.getBackground(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
            if (bg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
                bg = c.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
        // Draw caret in XOR mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
            if(isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
                    Rectangle r = component.modelToView(getDot());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
                    g.setXORMode(bg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
                    g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
                    g.setPaintMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5021
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5022
                    // can't render I guess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
                    //System.err.println("Can't render cursor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5024
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5028
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
        // If some area other than the composed text is clicked by mouse,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
        // issue endComposition() to force commit the composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
        protected void positionCaret(MouseEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
            JTextComponent host = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
            Point pt = new Point(me.getX(), me.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
            int offset = host.viewToModel(pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
            int composedStartIndex = host.composedTextStart.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5037
            if ((offset < composedStartIndex) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5038
                (offset > composedTextEnd.getOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
                    // Issue endComposition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
                    Position newPos = host.getDocument().createPosition(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
                    host.getInputContext().endComposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
                    // Post a caret positioning runnable to assure that the positioning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
                    // occurs *after* committing the composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
                    EventQueue.invokeLater(new DoSetCaretPosition(host, newPos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
                } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
                    System.err.println(ble);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5050
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5051
                // Normal processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5052
                super.positionCaret(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5053
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5057
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5058
    // Runnable class for invokeLater() to set caret position later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5059
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5060
    private class DoSetCaretPosition implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
        JTextComponent host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5062
        Position newPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5064
        DoSetCaretPosition(JTextComponent host, Position newPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
            this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5066
            this.newPos = newPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
            host.setCaretPosition(newPos.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
}