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