jdk/src/share/classes/java/awt/TextComponent.java
author erikj
Thu, 07 Jun 2012 18:05:09 -0700
changeset 12813 c10ab96dcf41
parent 5506 202f599c92aa
child 13357 6a75209a8aeb
permissions -rw-r--r--
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI Reviewed-by: ohair, ohrstrom, ihse
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        text = (t != null) ? t : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            peer.setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns the text that is presented by this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * By default, this is an empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the value of this <code>TextComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see     java.awt.TextComponent#setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public synchronized String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            text = peer.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns the selected text from the text that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * presented by this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return      the selected text of this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see         java.awt.TextComponent#select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public synchronized String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return getText().substring(getSelectionStart(), getSelectionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Indicates whether or not this text component is editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return     <code>true</code> if this text component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *                  editable; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see        java.awt.TextComponent#setEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public boolean isEditable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return editable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Sets the flag that determines whether or not this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * text component is editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * If the flag is set to <code>true</code>, this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * becomes user editable. If the flag is set to <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * the user cannot change the text of this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * By default, non-editable text components have a background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * of SystemColor.control.  This default can be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * calling setBackground.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param     b   a flag indicating whether this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *                      is user editable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see       java.awt.TextComponent#isEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @since     JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public synchronized void setEditable(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (editable == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        editable = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            peer.setEditable(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Gets the background color of this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * By default, non-editable text components have a background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * of SystemColor.control.  This default can be overridden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * calling setBackground.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return This text component's background color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         If this text component does not have a background color,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         the background color of its parent is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @see #setBackground(Color)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public Color getBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (!editable && !backgroundSetByClientCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return SystemColor.control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return super.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Sets the background color of this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param c The color to become this text component's color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *        If this parameter is null then this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *        will inherit the background color of its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see #getBackground()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void setBackground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        backgroundSetByClientCode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        super.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Gets the start position of the selected text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return      the start position of the selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see         java.awt.TextComponent#setSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @see         java.awt.TextComponent#getSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public synchronized int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            selectionStart = peer.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Sets the selection start for this text component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * the specified position. The new start point is constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * to be at or before the current selection end. It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * cannot be set to less than zero, the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * component's text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * If the caller supplies a value for <code>selectionStart</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * that is out of bounds, the method enforces these constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * silently, and without failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param       selectionStart   the start position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *                        selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see         java.awt.TextComponent#getSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see         java.awt.TextComponent#setSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public synchronized void setSelectionStart(int selectionStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        /* Route through select method to enforce consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * between selectionStart and selectionEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        select(selectionStart, getSelectionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Gets the end position of the selected text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return      the end position of the selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @see         java.awt.TextComponent#setSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see         java.awt.TextComponent#getSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public synchronized int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            selectionEnd = peer.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return selectionEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Sets the selection end for this text component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * the specified position. The new end point is constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * to be at or after the current selection start. It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * cannot be set beyond the end of the component's text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * If the caller supplies a value for <code>selectionEnd</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * that is out of bounds, the method enforces these constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * silently, and without failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param       selectionEnd   the end position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *                        selected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see         java.awt.TextComponent#getSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see         java.awt.TextComponent#setSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public synchronized void setSelectionEnd(int selectionEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        /* Route through select method to enforce consistent policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * between selectionStart and selectionEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        select(getSelectionStart(), selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Selects the text between the specified start and end positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * This method sets the start and end positions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * selected text, enforcing the restriction that the start position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * must be greater than or equal to zero.  The end position must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * greater than or equal to the start position, and less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * equal to the length of the text component's text.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * character positions are indexed starting with zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * The length of the selection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <code>endPosition</code> - <code>startPosition</code>, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * character at <code>endPosition</code> is not selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * If the start and end positions of the selected text are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * all text is deselected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * If the caller supplies values that are inconsistent or out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * bounds, the method enforces these constraints silently, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * without failure. Specifically, if the start position or end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * position is greater than the length of the text, it is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * equal the text length. If the start position is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * it is reset to zero, and if the end position is less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * start position, it is reset to the start position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param        selectionStart the zero-based index of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                       character (<code>char</code> value) to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param        selectionEnd the zero-based end position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                       text to be selected; the character (<code>char</code> value) at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                       <code>selectionEnd</code> is not selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see          java.awt.TextComponent#setSelectionStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @see          java.awt.TextComponent#setSelectionEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see          java.awt.TextComponent#selectAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public synchronized void select(int selectionStart, int selectionEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        String text = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (selectionStart < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            selectionStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (selectionStart > text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            selectionStart = text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (selectionEnd > text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            selectionEnd = text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (selectionEnd < selectionStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            selectionEnd = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        this.selectionStart = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        this.selectionEnd = selectionEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            peer.select(selectionStart, selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Selects all the text in this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see        java.awt.TextComponent#select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public synchronized void selectAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        this.selectionStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        this.selectionEnd = getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            peer.select(selectionStart, selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Sets the position of the text insertion caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * The caret position is constrained to be between 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * and the last character of the text, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * If the passed-in value is greater than this range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * the value is set to the last character (or 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * the <code>TextComponent</code> contains no text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * and no error is returned.  If the passed-in value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * less than 0, an <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param        position the position of the text insertion caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @exception    IllegalArgumentException if <code>position</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *               is less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @since        JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public synchronized void setCaretPosition(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (position < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            throw new IllegalArgumentException("position less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        int maxposition = getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (position > maxposition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            position = maxposition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            peer.setCaretPosition(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            select(position, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Returns the position of the text insertion caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * The caret position is constrained to be between 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * and the last character of the text, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * If the text or caret have not been set, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * caret position is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return       the position of the text insertion caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see #setCaretPosition(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @since        JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public synchronized int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            position = peer.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            position = selectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        int maxposition = getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (position > maxposition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            position = maxposition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Adds the specified text event listener to receive text events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * from this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * If <code>l</code> is <code>null</code>, no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @param l the text event listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @see             #removeTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @see             #getTextListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @see             java.awt.event.TextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public synchronized void addTextListener(TextListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        textListener = AWTEventMulticaster.add(textListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Removes the specified text event listener so that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * receives text events from this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * If <code>l</code> is <code>null</code>, no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @param           l     the text listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see             #addTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see             #getTextListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see             java.awt.event.TextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @since           JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public synchronized void removeTextListener(TextListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        textListener = AWTEventMulticaster.remove(textListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns an array of all the text listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * registered on this text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return all of this text component's <code>TextListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *         or an empty array if no text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @see #addTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @see #removeTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public synchronized TextListener[] getTextListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return (TextListener[])(getListeners(TextListener.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * upon this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <code>TextComponent</code> <code>t</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * for its text listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <pre>TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *          <code><em>Foo</em>Listener</code>s on this text component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @see #getTextListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        EventListener l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if  (listenerType == TextListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            l = textListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return super.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return AWTEventMulticaster.getListeners(l, listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    // REMIND: remove when filtering is done at lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    boolean eventEnabled(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (e.id == TextEvent.TEXT_VALUE_CHANGED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if ((eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                textListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return super.eventEnabled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Processes events on this text component. If the event is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <code>TextEvent</code>, it invokes the <code>processTextEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * method else it invokes its superclass's <code>processEvent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (e instanceof TextEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            processTextEvent((TextEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        super.processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Processes text events occurring on this text component by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * dispatching them to any registered <code>TextListener</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * NOTE: This method will not be called unless text events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * are enabled for this component. This happens when one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * following occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * <li>A <code>TextListener</code> object is registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * via <code>addTextListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <li>Text events are enabled via <code>enableEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @param e the text event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @see Component#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    protected void processTextEvent(TextEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        TextListener listener = textListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            case TextEvent.TEXT_VALUE_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                listener.textValueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Returns a string representing the state of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <code>TextComponent</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @return      the parameter string of this text component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        String str = super.paramString() + ",text=" + getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (editable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            str += ",editable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Assigns a valid value to the canAccessClipboard instance variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private void checkSystemClipboardAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        canAccessClipboard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                sm.checkSystemClipboardAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                canAccessClipboard = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * The textComponent SerializedDataVersion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    private int textComponentSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Writes default serializable fields to stream.  Writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * a list of serializable TextListener(s) as optional data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * The non-serializable TextListener(s) are detected and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * no attempt is made to serialize them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @serialData Null terminated sequence of zero or more pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *             A pair consists of a String and Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *             The String indicates the type of object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *             is one of the following :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *             textListenerK indicating and TextListener object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @see java.awt.Component#textListenerK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        // Serialization support.  Since the value of the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // selectionStart, selectionEnd, and text aren't necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // up to date, we sync them up with the peer before serializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            text = peer.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            selectionStart = peer.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            selectionEnd = peer.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        AWTEventMulticaster.save(s, textListenerK, textListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Read the ObjectInputStream, and if it isn't null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * add a listener to receive text events fired by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * TextComponent.  Unrecognized keys or values will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * <code>GraphicsEnvironment.isHeadless()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @see #removeTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @see #addTextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        throws ClassNotFoundException, IOException, HeadlessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        // Make sure the state we just read in for text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        // selectionStart and selectionEnd has legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        this.text = (text != null) ? text : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        select(selectionStart, selectionEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        Object keyOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        while(null != (keyOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            String key = ((String)keyOrNull).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (textListenerK == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                addTextListener((TextListener)(s.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                // skip value for unrecognized key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        enableInputMethodsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        checkSystemClipboardAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
// Accessibility support
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
/* To be fully implemented in a future release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        return peer.getIndexAtPoint(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
/* To be fully implemented in a future release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        TextComponentPeer peer = (TextComponentPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        return peer.getCharacterBounds(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * Gets the AccessibleContext associated with this TextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * For text components, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * AccessibleAWTTextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * A new AccessibleAWTTextComponent instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return an AccessibleAWTTextComponent that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *         AccessibleContext of this TextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            accessibleContext = new AccessibleAWTTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <code>TextComponent</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Java Accessibility API appropriate to text component user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    protected class AccessibleAWTTextComponent extends AccessibleAWTComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        implements AccessibleText, TextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        private static final long serialVersionUID = 3631432373506317811L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
         * Constructs an AccessibleAWTTextComponent.  Adds a listener to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
         * caret change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        public AccessibleAWTTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            TextComponent.this.addTextListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * TextListener notification of a text value change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        public void textValueChanged(TextEvent textEvent)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            Integer cpos = Integer.valueOf(TextComponent.this.getCaretPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null, cpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
         * Gets the state set of the TextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         * The AccessibleStateSet of an object is composed of a set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         * unique AccessibleStates.  A change in the AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         * of an object will cause a PropertyChangeEvent to be fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         * for the AccessibleContext.ACCESSIBLE_STATE_PROPERTY property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * @return an instance of AccessibleStateSet containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * current state set of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         * @see AccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            if (TextComponent.this.isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                states.add(AccessibleState.EDITABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         * object (AccessibleRole.TEXT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            return AccessibleRole.TEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         * Get the AccessibleText associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         * AccessibleText interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // --- interface AccessibleText methods ------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         * Many of these methods are just convenience methods; they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * just call the equivalent on the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * Given a point in local coordinates, return the zero-based index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * of the character under that Point.  If the point is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * this method returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
         * @param p the Point in local coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
         * @return the zero-based index of the character under Point p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        public int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            return TextComponent.this.getIndexAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         * Determines the bounding box of the character at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         * index into the string.  The bounds are returned in local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         * coordinates.  If the index is invalid a null rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         * @param i the index into the String >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         * @return the screen coordinates of the character's bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        public Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            return TextComponent.this.getCharacterBounds(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * Returns the number of characters (valid indicies)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * @return the number of characters >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        public int getCharCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            return TextComponent.this.getText().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * Returns the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         * Note: The character to the right of the caret will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         * same index value as the offset (the caret is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
         * two characters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
         * @return the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            return TextComponent.this.getCaretPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         * Returns the AttributeSet for a given character (at a given index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * @param i the zero-based index into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         * @return the AttributeSet of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        public AttributeSet getCharacterAttribute(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            return null; // No attributes in TextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         * Returns the start offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         * Return 0 if the text is empty, or the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
         * if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
         * @return the index into the text of the start of the selection >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return TextComponent.this.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * Returns the end offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         * Return 0 if the text is empty, or the caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * if no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         * @return the index into teh text of the end of the selection >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            return TextComponent.this.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
         * Returns the portion of the text that is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
         * @return the text, null if no selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        public String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            String selText = TextComponent.this.getSelectedText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            // Fix for 4256662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if (selText == null || selText.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return selText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
         * Returns the String at a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
         * or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
         * @return the letter, word, or sentence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
         *   null for an invalid index or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            if (index < 0 || index >= TextComponent.this.getText().length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                return TextComponent.this.getText().substring(index, index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            case AccessibleText.WORD:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    BreakIterator words = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    int end = words.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    return s.substring(words.previous(), end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            case AccessibleText.SENTENCE:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    BreakIterator sentence = BreakIterator.getSentenceInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    int end = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    return s.substring(sentence.previous(), end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        private static final boolean NEXT = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        private static final boolean PREVIOUS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
         * Needed to unify forward and backward searching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
         * The method assumes that s is the text assigned to words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        private int findWordLimit(int index, BreakIterator words, boolean direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                         String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            // Fix for 4256660 and 4256661.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            // Words iterator is different from character and sentence iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            // in that end of one word is not necessarily start of another word.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // Please see java.text.BreakIterator JavaDoc. The code below is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            // based on nextWordStartAfter example from BreakIterator.java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            int last = (direction == NEXT) ? words.following(index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                                           : words.preceding(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            int current = (direction == NEXT) ? words.next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                                              : words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            while (current != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                for (int p = Math.min(last, current); p < Math.max(last, current); p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    if (Character.isLetter(s.charAt(p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                        return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                last = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                current = (direction == NEXT) ? words.next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                              : words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         * Returns the String after a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         * or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * @return the letter, word, or sentence, null for an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         *  index or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if (index < 0 || index >= TextComponent.this.getText().length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                if (index+1 >= 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
                return TextComponent.this.getText().substring(index+1, index+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            case AccessibleText.WORD:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    BreakIterator words = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    int start = findWordLimit(index, words, NEXT, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    if (start == BreakIterator.DONE || start >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    int end = words.following(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    if (end == BreakIterator.DONE || end >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            case AccessibleText.SENTENCE:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                    BreakIterator sentence = BreakIterator.getSentenceInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                    int start = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    if (start == BreakIterator.DONE || start >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    int end = sentence.following(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    if (end == BreakIterator.DONE || end >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            default:
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         * Returns the String before a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
         *   or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
         * @return the letter, word, or sentence, null for an invalid index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
         *  or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            if (index < 0 || index > TextComponent.this.getText().length()-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                if (index == 0) {
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
                return TextComponent.this.getText().substring(index-1, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            case AccessibleText.WORD:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    BreakIterator words = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                    int end = findWordLimit(index, words, PREVIOUS, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    if (end == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    int start = words.preceding(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    if (start == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            case AccessibleText.SENTENCE:  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    String s = TextComponent.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                    BreakIterator sentence = BreakIterator.getSentenceInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    int end = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    end = sentence.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    int start = sentence.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    if (start == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            default:
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }  // end of AccessibleAWTTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    private boolean checkForEnableIM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
}