jdk/src/share/classes/java/awt/TextComponent.java
author mcherkas
Wed, 16 Jan 2013 17:26:41 +0400
changeset 15318 607db339afcc
parent 13357 6a75209a8aeb
child 16100 379f48d34516
permissions -rw-r--r--
8005492: Reduce number of warnings in sun/awt/* classes Reviewed-by: art, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.peer.TextComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.awt.InputMethodSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.text.BreakIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.text.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.im.InputMethodRequests;
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    38
import javax.tools.annotation.GenerateNativeHeader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The <code>TextComponent</code> class is the superclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * any component that allows the editing of some text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A text component embodies a string of text.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>TextComponent</code> class defines a set of methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * that determine whether or not this text is editable. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * component is editable, it defines another set of methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * that supports a text insertion caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * In addition, the class defines methods that are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * to maintain a current <em>selection</em> from the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The text selection, a substring of the component's text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * is the target of editing operations. It is also referred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * to as the <em>selected text</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    60
/* No native methods here, but the constants are needed in the supporting JNI code */
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    61
@GenerateNativeHeader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class TextComponent extends Component implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The value of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * A <code>null</code> value is the same as "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @see #setText(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see #getText()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * A boolean indicating whether or not this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <code>TextComponent</code> is editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * It will be <code>true</code> if the text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * is editable and <code>false</code> if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @see #isEditable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    boolean editable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * The selection refers to the selected text, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <code>selectionStart</code> is the start position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * of the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see #getSelectionStart()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see #setSelectionStart(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    int selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The selection refers to the selected text, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <code>selectionEnd</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * is the end position of the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see #getSelectionEnd()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #setSelectionEnd(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    int selectionEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // A flag used to tell whether the background has been set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // developer code (as opposed to AWT code).  Used to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // the background color of non-editable TextComponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    boolean backgroundSetByClientCode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * True if this <code>TextComponent</code> has access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * to the System clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    transient private boolean canAccessClipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    transient protected TextListener textListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static final long serialVersionUID = -2214773872412987419L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Constructs a new text component initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * specified text. Sets the value of the cursor to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>Cursor.TEXT_CURSOR</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param      text       the text to be displayed; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *             <code>text</code> is <code>null</code>, the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *             string <code>""</code> will be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @exception  HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *             <code>GraphicsEnvironment.isHeadless</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *             returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see        java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see        java.awt.Cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    TextComponent(String text) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.text = (text != null) ? text : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        checkSystemClipboardAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private void enableInputMethodsIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (checkForEnableIM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            checkForEnableIM = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                boolean shouldEnable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (toolkit instanceof InputMethodSupport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    shouldEnable = ((InputMethodSupport)toolkit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                      .enableInputMethodsForTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                enableInputMethods(shouldEnable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // if something bad happens, just don't enable input methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Enables or disables input method support for this text component. If input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * method support is enabled and the text component also processes key events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * incoming events are offered to the current input method and will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * processed by the component or dispatched to its listeners if the input method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * does not consume them. Whether and how input method support for this text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * component is enabled or disabled by default is implementation dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param enable true to enable, false to disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see #processKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void enableInputMethods(boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        checkForEnableIM = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        super.enableInputMethods(enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    boolean areInputMethodsEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // moved from the constructor above to here and addNotify below,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // this call will initialize the toolkit if not already initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (checkForEnableIM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            enableInputMethodsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // TextComponent handles key events without touching the eventMask or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // having a key listener, so just check whether the flag is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return (eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public InputMethodRequests getInputMethodRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (peer != null) return peer.getInputMethodRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        else return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Makes this Component displayable by connecting it to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * native screen resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This method is called internally by the toolkit and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * not be called directly by programs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see       java.awt.TextComponent#removeNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        enableInputMethodsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Removes the <code>TextComponent</code>'s peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * The peer allows us to modify the appearance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <code>TextComponent</code> without changing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void removeNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                text = peer.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                selectionStart = peer.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                selectionEnd = peer.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            super.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Sets the text that is presented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * text component to be the specified text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param       t   the new text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *                  if this parameter is <code>null</code> then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *                  the text is set to the empty string ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @see         java.awt.TextComponent#getText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public synchronized void setText(String t) {
13357
6a75209a8aeb 7184365: closed/java/awt/event/TextEvent/TextEventSequenceTest/TextEventSequenceTest fails
alexsch
parents: 12813
diff changeset
   238
        boolean skipTextEvent = (text == null || text.isEmpty())
6a75209a8aeb 7184365: closed/java/awt/event/TextEvent/TextEventSequenceTest/TextEventSequenceTest fails
alexsch
parents: 12813
diff changeset
   239
                && (t == null || t.isEmpty());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        text = (t != null) ? t : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        TextComponentPeer peer = (TextComponentPeer)this.peer;
13357
6a75209a8aeb 7184365: closed/java/awt/event/TextEvent/TextEventSequenceTest/TextEventSequenceTest fails
alexsch
parents: 12813
diff changeset
   242
        // Please note that we do not want to post an event
6a75209a8aeb 7184365: closed/java/awt/event/TextEvent/TextEventSequenceTest/TextEventSequenceTest fails
alexsch
parents: 12813
diff changeset
   243
        // if TextArea.setText() or TextField.setText() replaces an empty text
6a75209a8aeb 7184365: closed/java/awt/event/TextEvent/TextEventSequenceTest/TextEventSequenceTest fails
alexsch
parents: 12813
diff changeset
   244
        // by an empty text, that is, if component's text remains unchanged.
6a75209a8aeb 7184365: closed/java/awt/event/TextEvent/TextEventSequenceTest/TextEventSequenceTest fails
alexsch
parents: 12813
diff changeset
   245
        if (peer != null && !skipTextEvent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            peer.setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns the text that is presented by this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * By default, this is an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return the value of this <code>TextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see     java.awt.TextComponent#setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public synchronized String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            text = peer.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Returns the selected text from the text that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * presented by this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return      the selected text of this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @see         java.awt.TextComponent#select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public synchronized String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return getText().substring(getSelectionStart(), getSelectionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Indicates whether or not this text component is editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return     <code>true</code> if this text component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *                  editable; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see        java.awt.TextComponent#setEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public boolean isEditable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return editable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Sets the flag that determines whether or not this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * text component is editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * If the flag is set to <code>true</code>, this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * becomes user editable. If the flag is set to <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * the user cannot change the text of this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * By default, non-editable text components have a background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * of SystemColor.control.  This default can be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * calling setBackground.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param     b   a flag indicating whether this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *                      is user editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see       java.awt.TextComponent#isEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @since     JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public synchronized void setEditable(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (editable == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        editable = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            peer.setEditable(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Gets the background color of this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * By default, non-editable text components have a background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * of SystemColor.control.  This default can be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * calling setBackground.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return This text component's background color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *         If this text component does not have a background color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *         the background color of its parent is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #setBackground(Color)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public Color getBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (!editable && !backgroundSetByClientCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return SystemColor.control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return super.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Sets the background color of this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param c The color to become this text component's color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *        If this parameter is null then this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *        will inherit the background color of its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #getBackground()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public void setBackground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        backgroundSetByClientCode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        super.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Gets the start position of the selected text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return      the start position of the selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see         java.awt.TextComponent#setSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see         java.awt.TextComponent#getSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public synchronized int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            selectionStart = peer.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return selectionStart;
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
     * Sets the selection start for this text component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * the specified position. The new start point is constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * to be at or before the current selection end. It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * cannot be set to less than zero, the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * component's text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * If the caller supplies a value for <code>selectionStart</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * that is out of bounds, the method enforces these constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * silently, and without failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param       selectionStart   the start position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *                        selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @see         java.awt.TextComponent#getSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see         java.awt.TextComponent#setSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public synchronized void setSelectionStart(int selectionStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        /* Route through select method to enforce consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * between selectionStart and selectionEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        select(selectionStart, getSelectionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Gets the end position of the selected text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return      the end position of the selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see         java.awt.TextComponent#setSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @see         java.awt.TextComponent#getSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public synchronized int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            selectionEnd = peer.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return selectionEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Sets the selection end for this text component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * the specified position. The new end point is constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * to be at or after the current selection start. It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * cannot be set beyond the end of the component's text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * If the caller supplies a value for <code>selectionEnd</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * that is out of bounds, the method enforces these constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * silently, and without failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param       selectionEnd   the end position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *                        selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see         java.awt.TextComponent#getSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @see         java.awt.TextComponent#setSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public synchronized void setSelectionEnd(int selectionEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        /* Route through select method to enforce consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * between selectionStart and selectionEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        select(getSelectionStart(), selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Selects the text between the specified start and end positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * This method sets the start and end positions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * selected text, enforcing the restriction that the start position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * must be greater than or equal to zero.  The end position must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * greater than or equal to the start position, and less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * equal to the length of the text component's text.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * character positions are indexed starting with zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * The length of the selection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <code>endPosition</code> - <code>startPosition</code>, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * character at <code>endPosition</code> is not selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * If the start and end positions of the selected text are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * all text is deselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * If the caller supplies values that are inconsistent or out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * bounds, the method enforces these constraints silently, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * without failure. Specifically, if the start position or end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * position is greater than the length of the text, it is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * equal the text length. If the start position is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * it is reset to zero, and if the end position is less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * start position, it is reset to the start position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param        selectionStart the zero-based index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                       character (<code>char</code> value) to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param        selectionEnd the zero-based end position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                       text to be selected; the character (<code>char</code> value) at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                       <code>selectionEnd</code> is not selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see          java.awt.TextComponent#setSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see          java.awt.TextComponent#setSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see          java.awt.TextComponent#selectAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public synchronized void select(int selectionStart, int selectionEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        String text = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (selectionStart < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            selectionStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (selectionStart > text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            selectionStart = text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (selectionEnd > text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            selectionEnd = text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (selectionEnd < selectionStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            selectionEnd = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        this.selectionStart = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        this.selectionEnd = selectionEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            peer.select(selectionStart, selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Selects all the text in this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see        java.awt.TextComponent#select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public synchronized void selectAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        this.selectionStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        this.selectionEnd = getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            peer.select(selectionStart, selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Sets the position of the text insertion caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * The caret position is constrained to be between 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * and the last character of the text, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * If the passed-in value is greater than this range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * the value is set to the last character (or 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * the <code>TextComponent</code> contains no text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * and no error is returned.  If the passed-in value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * less than 0, an <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param        position the position of the text insertion caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @exception    IllegalArgumentException if <code>position</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *               is less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @since        JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public synchronized void setCaretPosition(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (position < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throw new IllegalArgumentException("position less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int maxposition = getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (position > maxposition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            position = maxposition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            peer.setCaretPosition(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            select(position, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Returns the position of the text insertion caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * The caret position is constrained to be between 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * and the last character of the text, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * If the text or caret have not been set, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * caret position is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return       the position of the text insertion caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @see #setCaretPosition(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @since        JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public synchronized int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        int position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            position = peer.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            position = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        int maxposition = getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (position > maxposition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            position = maxposition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Adds the specified text event listener to receive text events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * from this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * If <code>l</code> is <code>null</code>, no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param l the text event listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see             #removeTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see             #getTextListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see             java.awt.event.TextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public synchronized void addTextListener(TextListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        textListener = AWTEventMulticaster.add(textListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Removes the specified text event listener so that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * receives text events from this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * If <code>l</code> is <code>null</code>, no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param           l     the text listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see             #addTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see             #getTextListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @see             java.awt.event.TextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @since           JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public synchronized void removeTextListener(TextListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        textListener = AWTEventMulticaster.remove(textListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Returns an array of all the text listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * registered on this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @return all of this text component's <code>TextListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *         or an empty array if no text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @see #addTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see #removeTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public synchronized TextListener[] getTextListeners() {
15318
607db339afcc 8005492: Reduce number of warnings in sun/awt/* classes
mcherkas
parents: 13357
diff changeset
   609
        return getListeners(TextListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * upon this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <code>TextComponent</code> <code>t</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * for its text listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <pre>TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *          <code><em>Foo</em>Listener</code>s on this text component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @see #getTextListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        EventListener l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if  (listenerType == TextListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            l = textListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return super.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return AWTEventMulticaster.getListeners(l, listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    // REMIND: remove when filtering is done at lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    boolean eventEnabled(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (e.id == TextEvent.TEXT_VALUE_CHANGED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                textListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return super.eventEnabled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Processes events on this text component. If the event is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <code>TextEvent</code>, it invokes the <code>processTextEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * method else it invokes its superclass's <code>processEvent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (e instanceof TextEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            processTextEvent((TextEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        super.processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Processes text events occurring on this text component by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * dispatching them to any registered <code>TextListener</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * NOTE: This method will not be called unless text events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * are enabled for this component. This happens when one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * following occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * <li>A <code>TextListener</code> object is registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * via <code>addTextListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * <li>Text events are enabled via <code>enableEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @param e the text event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @see Component#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    protected void processTextEvent(TextEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        TextListener listener = textListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            case TextEvent.TEXT_VALUE_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                listener.textValueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Returns a string representing the state of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <code>TextComponent</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @return      the parameter string of this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        String str = super.paramString() + ",text=" + getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (editable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            str += ",editable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Assigns a valid value to the canAccessClipboard instance variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private void checkSystemClipboardAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        canAccessClipboard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                sm.checkSystemClipboardAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                canAccessClipboard = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * The textComponent SerializedDataVersion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private int textComponentSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Writes default serializable fields to stream.  Writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * a list of serializable TextListener(s) as optional data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * The non-serializable TextListener(s) are detected and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * no attempt is made to serialize them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @serialData Null terminated sequence of zero or more pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *             A pair consists of a String and Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *             The String indicates the type of object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *             is one of the following :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *             textListenerK indicating and TextListener object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @see java.awt.Component#textListenerK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // Serialization support.  Since the value of the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // selectionStart, selectionEnd, and text aren't necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // up to date, we sync them up with the peer before serializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            text = peer.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            selectionStart = peer.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            selectionEnd = peer.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        AWTEventMulticaster.save(s, textListenerK, textListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Read the ObjectInputStream, and if it isn't null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * add a listener to receive text events fired by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * TextComponent.  Unrecognized keys or values will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * <code>GraphicsEnvironment.isHeadless()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @see #removeTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @see #addTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        throws ClassNotFoundException, IOException, HeadlessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        // Make sure the state we just read in for text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // selectionStart and selectionEnd has legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        this.text = (text != null) ? text : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        select(selectionStart, selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        Object keyOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        while(null != (keyOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            String key = ((String)keyOrNull).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (textListenerK == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                addTextListener((TextListener)(s.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                // skip value for unrecognized key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        enableInputMethodsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        checkSystemClipboardAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
/* To be fully implemented in a future release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        return peer.getIndexAtPoint(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
/* To be fully implemented in a future release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return peer.getCharacterBounds(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Gets the AccessibleContext associated with this TextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * For text components, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * AccessibleAWTTextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * A new AccessibleAWTTextComponent instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @return an AccessibleAWTTextComponent that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *         AccessibleContext of this TextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            accessibleContext = new AccessibleAWTTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <code>TextComponent</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * Java Accessibility API appropriate to text component user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    protected class AccessibleAWTTextComponent extends AccessibleAWTComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        implements AccessibleText, TextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        private static final long serialVersionUID = 3631432373506317811L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * Constructs an AccessibleAWTTextComponent.  Adds a listener to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         * caret change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public AccessibleAWTTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            TextComponent.this.addTextListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
         * TextListener notification of a text value change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        public void textValueChanged(TextEvent textEvent)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            Integer cpos = Integer.valueOf(TextComponent.this.getCaretPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, cpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         * Gets the state set of the TextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * The AccessibleStateSet of an object is composed of a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * unique AccessibleStates.  A change in the AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         * of an object will cause a PropertyChangeEvent to be fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
         * for the AccessibleContext.ACCESSIBLE_STATE_PROPERTY property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         * @return an instance of AccessibleStateSet containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * current state set of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * @see AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (TextComponent.this.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                states.add(AccessibleState.EDITABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         * object (AccessibleRole.TEXT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            return AccessibleRole.TEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * Get the AccessibleText associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * AccessibleText interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        // --- interface AccessibleText methods ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * Many of these methods are just convenience methods; they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * just call the equivalent on the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
         * Given a point in local coordinates, return the zero-based index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
         * of the character under that Point.  If the point is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
         * this method returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         * @param p the Point in local coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         * @return the zero-based index of the character under Point p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        public int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            return TextComponent.this.getIndexAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         * Determines the bounding box of the character at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         * index into the string.  The bounds are returned in local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         * coordinates.  If the index is invalid a null rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * @param i the index into the String >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * @return the screen coordinates of the character's bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        public Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            return TextComponent.this.getCharacterBounds(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
         * Returns the number of characters (valid indicies)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         * @return the number of characters >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        public int getCharCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            return TextComponent.this.getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
         * Returns the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
         * Note: The character to the right of the caret will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
         * same index value as the offset (the caret is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
         * two characters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
         * @return the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            return TextComponent.this.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
         * Returns the AttributeSet for a given character (at a given index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         * @param i the zero-based index into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * @return the AttributeSet of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        public AttributeSet getCharacterAttribute(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return null; // No attributes in TextComponent
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
         * Returns the start offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
         * Return 0 if the text is empty, or the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
         * if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         * @return the index into the text of the start of the selection >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return TextComponent.this.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * Returns the end offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         * Return 0 if the text is empty, or the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
         * if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
         * @return the index into teh text of the end of the selection >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return TextComponent.this.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
         * Returns the portion of the text that is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
         * @return the text, null if no selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        public String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            String selText = TextComponent.this.getSelectedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            // Fix for 4256662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            if (selText == null || selText.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return selText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
         * Returns the String at a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
         * or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         * @return the letter, word, or sentence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         *   null for an invalid index or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            if (index < 0 || index >= TextComponent.this.getText().length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                return TextComponent.this.getText().substring(index, index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            case AccessibleText.WORD:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    BreakIterator words = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    int end = words.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    return s.substring(words.previous(), end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            case AccessibleText.SENTENCE:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    BreakIterator sentence = BreakIterator.getSentenceInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    int end = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    return s.substring(sentence.previous(), end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        private static final boolean NEXT = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        private static final boolean PREVIOUS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
         * Needed to unify forward and backward searching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
         * The method assumes that s is the text assigned to words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        private int findWordLimit(int index, BreakIterator words, boolean direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                                         String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // Fix for 4256660 and 4256661.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            // Words iterator is different from character and sentence iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            // in that end of one word is not necessarily start of another word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            // Please see java.text.BreakIterator JavaDoc. The code below is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            // based on nextWordStartAfter example from BreakIterator.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            int last = (direction == NEXT) ? words.following(index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                                           : words.preceding(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            int current = (direction == NEXT) ? words.next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                                              : words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            while (current != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    if (Character.isLetter(s.charAt(p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                        return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                last = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                current = (direction == NEXT) ? words.next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                                              : words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * Returns the String after a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
         * @return the letter, word, or sentence, null for an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
         *  index or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            if (index < 0 || index >= TextComponent.this.getText().length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                if (index+1 >= TextComponent.this.getText().length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                   return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                return TextComponent.this.getText().substring(index+1, index+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            case AccessibleText.WORD:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    BreakIterator words = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    int start = findWordLimit(index, words, NEXT, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                    if (start == BreakIterator.DONE || start >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                    int end = words.following(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    if (end == BreakIterator.DONE || end >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            case AccessibleText.SENTENCE:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                    BreakIterator sentence = BreakIterator.getSentenceInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    int start = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                    if (start == BreakIterator.DONE || start >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    int end = sentence.following(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                    if (end == BreakIterator.DONE || end >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                return null;
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 the String before a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
         *   or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
         * @return the letter, word, or sentence, null for an invalid index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
         *  or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            if (index < 0 || index > TextComponent.this.getText().length()-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                return TextComponent.this.getText().substring(index-1, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            case AccessibleText.WORD:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    BreakIterator words = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    int end = findWordLimit(index, words, PREVIOUS, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                    if (end == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    int start = words.preceding(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    if (start == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            case AccessibleText.SENTENCE:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    BreakIterator sentence = BreakIterator.getSentenceInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    int end = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    end = sentence.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                    int start = sentence.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    if (start == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    }  // end of AccessibleAWTTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    private boolean checkForEnableIM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
}