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