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