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