jdk/src/share/classes/java/awt/event/InputMethodEvent.java
author erikj
Thu, 07 Jun 2012 18:05:09 -0700
changeset 12813 c10ab96dcf41
parent 5506 202f599c92aa
child 16734 da1901d79073
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) 1997, 2007, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.EventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.font.TextHitInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.text.AttributedCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.text.CharacterIterator;
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    36
import javax.tools.annotation.GenerateNativeHeader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Input method events contain information about text that is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * composed using an input method. Whenever the text changes, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * input method sends an event. If the text component that's currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * using the input method is an active client, the event is dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * to that component. Otherwise, it is dispatched to a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * composition window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The text included with the input method event consists of two parts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * committed text and composed text. Either part may be empty. The two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * parts together replace any uncommitted composed text sent in previous events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * or the currently selected committed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Committed text should be integrated into the text component's persistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * data, it will not be sent again. Composed text may be sent repeatedly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * with changes to reflect the user's editing operations. Committed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * always precedes composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author JavaSoft Asia/Pacific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    59
/* 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
    60
@GenerateNativeHeader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class InputMethodEvent extends AWTEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Serial Version ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final long serialVersionUID = 4727190874778922661L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Marks the first integer id for the range of input method event ids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static final int INPUT_METHOD_FIRST = 1100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The event type indicating changed input method text. This event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * generated by input methods while processing input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final int INPUT_METHOD_TEXT_CHANGED = INPUT_METHOD_FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The event type indicating a changed insertion point in input method text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * This event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * generated by input methods while processing input if only the caret changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final int CARET_POSITION_CHANGED = INPUT_METHOD_FIRST + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Marks the last integer id for the range of input method event ids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final int INPUT_METHOD_LAST = INPUT_METHOD_FIRST + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The time stamp that indicates when the event was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @see #getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    long when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Text object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private transient AttributedCharacterIterator text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private transient int committedCharacterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private transient TextHitInfo caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private transient TextHitInfo visiblePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Constructs an <code>InputMethodEvent</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * source component, type, time, text, caret, and visiblePosition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * The offsets of caret and visiblePosition are relative to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * composed text; that is, the composed text within <code>text</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * if this is an <code>INPUT_METHOD_TEXT_CHANGED</code> event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the composed text within the <code>text</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>Note that passing in an invalid <code>id</code> results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * unspecified behavior. This method throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>IllegalArgumentException</code> if <code>source</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param source the object where the event originated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param id the event type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param when a long integer that specifies the time the event occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param text the combined committed and composed text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *      committed text first; must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *      when the event type is <code>CARET_POSITION_CHANGED</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *      may be <code>null</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *      <code>INPUT_METHOD_TEXT_CHANGED</code> if there's no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *      committed or composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param committedCharacterCount the number of committed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *      characters in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param caret the caret (a.k.a. insertion point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *      <code>null</code> if there's no caret within current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *      composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param visiblePosition the position that's most important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *      to be visible; <code>null</code> if there's no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *      recommendation for a visible position within current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *      composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws IllegalArgumentException if <code>id</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *      in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *      or if id is <code>CARET_POSITION_CHANGED</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *      <code>text</code> is not <code>null</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *      or if <code>committedCharacterCount</code> is not in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *      <code>0</code>..<code>(text.getEndIndex() - text.getBeginIndex())</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws IllegalArgumentException if <code>source</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public InputMethodEvent(Component source, int id, long when,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            AttributedCharacterIterator text, int committedCharacterCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            TextHitInfo caret, TextHitInfo visiblePosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        super(source, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (id < INPUT_METHOD_FIRST || id > INPUT_METHOD_LAST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException("id outside of valid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (id == CARET_POSITION_CHANGED && text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException("text must be null for CARET_POSITION_CHANGED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.when = when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int textLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            textLength = text.getEndIndex() - text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (committedCharacterCount < 0 || committedCharacterCount > textLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new IllegalArgumentException("committedCharacterCount outside of valid range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        this.committedCharacterCount = committedCharacterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.caret = caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.visiblePosition = visiblePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Constructs an <code>InputMethodEvent</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * source component, type, text, caret, and visiblePosition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The offsets of caret and visiblePosition are relative to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * composed text; that is, the composed text within <code>text</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * if this is an <code>INPUT_METHOD_TEXT_CHANGED</code> event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the composed text within the <code>text</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * The time stamp for this event is initialized by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * {@link java.awt.EventQueue#getMostRecentEventTime()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>Note that passing in an invalid <code>id</code> results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * unspecified behavior. This method throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <code>IllegalArgumentException</code> if <code>source</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param source the object where the event originated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param id the event type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param text the combined committed and composed text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *      committed text first; must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *      when the event type is <code>CARET_POSITION_CHANGED</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *      may be <code>null</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *      <code>INPUT_METHOD_TEXT_CHANGED</code> if there's no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *      committed or composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param committedCharacterCount the number of committed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *      characters in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param caret the caret (a.k.a. insertion point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *      <code>null</code> if there's no caret within current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *      composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param visiblePosition the position that's most important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *      to be visible; <code>null</code> if there's no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *      recommendation for a visible position within current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *      composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws IllegalArgumentException if <code>id</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *      in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *      or if id is <code>CARET_POSITION_CHANGED</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *      <code>text</code> is not <code>null</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *      or if <code>committedCharacterCount</code> is not in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *      <code>0</code>..<code>(text.getEndIndex() - text.getBeginIndex())</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @throws IllegalArgumentException if <code>source</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public InputMethodEvent(Component source, int id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            AttributedCharacterIterator text, int committedCharacterCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            TextHitInfo caret, TextHitInfo visiblePosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this(source, id, EventQueue.getMostRecentEventTime(), text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
             committedCharacterCount, caret, visiblePosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Constructs an <code>InputMethodEvent</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * specified source component, type, caret, and visiblePosition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The text is set to <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>committedCharacterCount</code> to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * The offsets of <code>caret</code> and <code>visiblePosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * are relative to the current composed text; that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the composed text within the <code>text</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * event being constructed as a <code>CARET_POSITION_CHANGED</code> event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * For an <code>INPUT_METHOD_TEXT_CHANGED</code> event without text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <code>caret</code> and <code>visiblePosition</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * The time stamp for this event is initialized by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * {@link java.awt.EventQueue#getMostRecentEventTime()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p>Note that passing in an invalid <code>id</code> results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * unspecified behavior. This method throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <code>IllegalArgumentException</code> if <code>source</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param source the object where the event originated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param id the event type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param caret the caret (a.k.a. insertion point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *      <code>null</code> if there's no caret within current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *      composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param visiblePosition the position that's most important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *      to be visible; <code>null</code> if there's no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *      recommendation for a visible position within current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *      composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws IllegalArgumentException if <code>id</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *      in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws IllegalArgumentException if <code>source</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public InputMethodEvent(Component source, int id, TextHitInfo caret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            TextHitInfo visiblePosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        this(source, id, EventQueue.getMostRecentEventTime(), null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
             0, caret, visiblePosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Gets the combined committed and composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Characters from index 0 to index <code>getCommittedCharacterCount() - 1</code> are committed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * text, the remaining characters are composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Always null for CARET_POSITION_CHANGED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * may be null for INPUT_METHOD_TEXT_CHANGED if there's no composed or committed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public AttributedCharacterIterator getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Gets the number of committed characters in the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public int getCommittedCharacterCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return committedCharacterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Gets the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * The offset of the caret is relative to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * composed text; that is, the composed text within getText()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * if this is an <code>INPUT_METHOD_TEXT_CHANGED</code> event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * the composed text within getText() of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return the caret (a.k.a. insertion point).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Null if there's no caret within current composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public TextHitInfo getCaret() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Gets the position that's most important to be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * The offset of the visible position is relative to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * composed text; that is, the composed text within getText()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * if this is an <code>INPUT_METHOD_TEXT_CHANGED</code> event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * the composed text within getText() of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return the position that's most important to be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Null if there's no recommendation for a visible position within current composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public TextHitInfo getVisiblePosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return visiblePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Consumes this event so that it will not be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * in the default manner by the source which originated it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void consume() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        consumed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns whether or not this event has been consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @see #consume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public boolean isConsumed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return consumed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns the time stamp of when this event occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return this event's timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public long getWhen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
      return when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns a parameter string identifying this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * This method is useful for event-logging and for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * It contains the event ID in text form, the characters of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * committed and composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * separated by "+", the number of committed characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * the caret, and the visible position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return a string identifying the event and its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        String typeStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        switch(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
          case INPUT_METHOD_TEXT_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
              typeStr = "INPUT_METHOD_TEXT_CHANGED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
          case CARET_POSITION_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
              typeStr = "CARET_POSITION_CHANGED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
              typeStr = "unknown type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        String textString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            textString = "no text";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            StringBuilder textBuffer = new StringBuilder("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int committedCharacterCount = this.committedCharacterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            char c = text.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            while (committedCharacterCount-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                textBuffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                c = text.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            textBuffer.append("\" + \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            while (c != CharacterIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                textBuffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                c = text.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            textBuffer.append("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            textString = textBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        String countString = committedCharacterCount + " characters committed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        String caretString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (caret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            caretString = "no caret";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            caretString = "caret: " + caret.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        String visiblePositionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (visiblePosition == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            visiblePositionString = "no visible position";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            visiblePositionString = "visible position: " + visiblePosition.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return typeStr + ", " + textString + ", " + countString + ", " + caretString + ", " + visiblePositionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Initializes the <code>when</code> field if it is not present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * object input stream. In that case, the field will be initialized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * invoking {@link java.awt.EventQueue#getMostRecentEventTime()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (when == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            when = EventQueue.getMostRecentEventTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}