src/share/classes/java/awt/event/InputMethodEvent.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 


  28 import java.awt.AWTEvent;
  29 import java.awt.Component;
  30 import java.awt.EventQueue;
  31 import java.awt.font.TextHitInfo;
  32 import java.io.IOException;
  33 import java.io.ObjectInputStream;
  34 import java.text.AttributedCharacterIterator;
  35 import java.text.CharacterIterator;
  36 import java.lang.annotation.Native;
  37 
  38 /**
  39  * Input method events contain information about text that is being
  40  * composed using an input method. Whenever the text changes, the
  41  * input method sends an event. If the text component that's currently
  42  * using the input method is an active client, the event is dispatched
  43  * to that component. Otherwise, it is dispatched to a separate
  44  * composition window.
  45  *
  46  * <p>
  47  * The text included with the input method event consists of two parts:


 200      *      characters in the text
 201      * @param caret the caret (a.k.a. insertion point);
 202      *      <code>null</code> if there's no caret within current
 203      *      composed text
 204      * @param visiblePosition the position that's most important
 205      *      to be visible; <code>null</code> if there's no
 206      *      recommendation for a visible position within current
 207      *      composed text
 208      * @throws IllegalArgumentException if <code>id</code> is not
 209      *      in the range
 210      *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>;
 211      *      or if id is <code>CARET_POSITION_CHANGED</code> and
 212      *      <code>text</code> is not <code>null</code>;
 213      *      or if <code>committedCharacterCount</code> is not in the range
 214      *      <code>0</code>..<code>(text.getEndIndex() - text.getBeginIndex())</code>
 215      * @throws IllegalArgumentException if <code>source</code> is null
 216      */
 217     public InputMethodEvent(Component source, int id,
 218             AttributedCharacterIterator text, int committedCharacterCount,
 219             TextHitInfo caret, TextHitInfo visiblePosition) {
 220         this(source, id, EventQueue.getMostRecentEventTime(), text,
 221              committedCharacterCount, caret, visiblePosition);



 222     }
 223 
 224     /**
 225      * Constructs an <code>InputMethodEvent</code> with the
 226      * specified source component, type, caret, and visiblePosition.
 227      * The text is set to <code>null</code>,
 228      * <code>committedCharacterCount</code> to 0.
 229      * <p>
 230      * The offsets of <code>caret</code> and <code>visiblePosition</code>
 231      * are relative to the current composed text; that is,
 232      * the composed text within the <code>text</code> of the
 233      * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event if the
 234      * event being constructed as a <code>CARET_POSITION_CHANGED</code> event.
 235      * For an <code>INPUT_METHOD_TEXT_CHANGED</code> event without text,
 236      * <code>caret</code> and <code>visiblePosition</code> must be
 237      * <code>null</code>.
 238      * The time stamp for this event is initialized by invoking
 239      * {@link java.awt.EventQueue#getMostRecentEventTime()}.
 240      * <p>Note that passing in an invalid <code>id</code> results in
 241      * unspecified behavior. This method throws an
 242      * <code>IllegalArgumentException</code> if <code>source</code>
 243      * is <code>null</code>.
 244      *
 245      * @param source the object where the event originated
 246      * @param id the event type
 247      * @param caret the caret (a.k.a. insertion point);
 248      *      <code>null</code> if there's no caret within current
 249      *      composed text
 250      * @param visiblePosition the position that's most important
 251      *      to be visible; <code>null</code> if there's no
 252      *      recommendation for a visible position within current
 253      *      composed text
 254      * @throws IllegalArgumentException if <code>id</code> is not
 255      *      in the range
 256      *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>
 257      * @throws IllegalArgumentException if <code>source</code> is null
 258      */
 259     public InputMethodEvent(Component source, int id, TextHitInfo caret,
 260             TextHitInfo visiblePosition) {
 261         this(source, id, EventQueue.getMostRecentEventTime(), null,
 262              0, caret, visiblePosition);


 263     }
 264 
 265     /**
 266      * Gets the combined committed and composed text.
 267      * Characters from index 0 to index <code>getCommittedCharacterCount() - 1</code> are committed
 268      * text, the remaining characters are composed text.
 269      *
 270      * @return the text.
 271      * Always null for CARET_POSITION_CHANGED;
 272      * may be null for INPUT_METHOD_TEXT_CHANGED if there's no composed or committed text.
 273      */
 274     public AttributedCharacterIterator getText() {
 275         return text;
 276     }
 277 
 278     /**
 279      * Gets the number of committed characters in the text.
 280      * @return the number of committed characters in the text
 281      */
 282     public int getCommittedCharacterCount() {


 394         }
 395 
 396         String visiblePositionString;
 397         if (visiblePosition == null) {
 398             visiblePositionString = "no visible position";
 399         } else {
 400             visiblePositionString = "visible position: " + visiblePosition.toString();
 401         }
 402 
 403         return typeStr + ", " + textString + ", " + countString + ", " + caretString + ", " + visiblePositionString;
 404     }
 405 
 406     /**
 407      * Initializes the <code>when</code> field if it is not present in the
 408      * object input stream. In that case, the field will be initialized by
 409      * invoking {@link java.awt.EventQueue#getMostRecentEventTime()}.
 410      */
 411     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
 412         s.defaultReadObject();
 413         if (when == 0) {
 414             when = EventQueue.getMostRecentEventTime();

 415         }
 416     }
 417 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import sun.awt.AWTAccessor;
  29 
  30 import java.awt.AWTEvent;
  31 import java.awt.Component;
  32 import java.awt.EventQueue;
  33 import java.awt.font.TextHitInfo;
  34 import java.io.IOException;
  35 import java.io.ObjectInputStream;
  36 import java.text.AttributedCharacterIterator;
  37 import java.text.CharacterIterator;
  38 import java.lang.annotation.Native;
  39 
  40 /**
  41  * Input method events contain information about text that is being
  42  * composed using an input method. Whenever the text changes, the
  43  * input method sends an event. If the text component that's currently
  44  * using the input method is an active client, the event is dispatched
  45  * to that component. Otherwise, it is dispatched to a separate
  46  * composition window.
  47  *
  48  * <p>
  49  * The text included with the input method event consists of two parts:


 202      *      characters in the text
 203      * @param caret the caret (a.k.a. insertion point);
 204      *      <code>null</code> if there's no caret within current
 205      *      composed text
 206      * @param visiblePosition the position that's most important
 207      *      to be visible; <code>null</code> if there's no
 208      *      recommendation for a visible position within current
 209      *      composed text
 210      * @throws IllegalArgumentException if <code>id</code> is not
 211      *      in the range
 212      *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>;
 213      *      or if id is <code>CARET_POSITION_CHANGED</code> and
 214      *      <code>text</code> is not <code>null</code>;
 215      *      or if <code>committedCharacterCount</code> is not in the range
 216      *      <code>0</code>..<code>(text.getEndIndex() - text.getBeginIndex())</code>
 217      * @throws IllegalArgumentException if <code>source</code> is null
 218      */
 219     public InputMethodEvent(Component source, int id,
 220             AttributedCharacterIterator text, int committedCharacterCount,
 221             TextHitInfo caret, TextHitInfo visiblePosition) {
 222         this(source, id,
 223                 AWTAccessor.getEventQueueAccessor()
 224                         .getMostRecentEventTimeForTarget(source),
 225                 text, committedCharacterCount,
 226                 caret, visiblePosition);
 227     }
 228 
 229     /**
 230      * Constructs an <code>InputMethodEvent</code> with the
 231      * specified source component, type, caret, and visiblePosition.
 232      * The text is set to <code>null</code>,
 233      * <code>committedCharacterCount</code> to 0.
 234      * <p>
 235      * The offsets of <code>caret</code> and <code>visiblePosition</code>
 236      * are relative to the current composed text; that is,
 237      * the composed text within the <code>text</code> of the
 238      * preceding <code>INPUT_METHOD_TEXT_CHANGED</code> event if the
 239      * event being constructed as a <code>CARET_POSITION_CHANGED</code> event.
 240      * For an <code>INPUT_METHOD_TEXT_CHANGED</code> event without text,
 241      * <code>caret</code> and <code>visiblePosition</code> must be
 242      * <code>null</code>.
 243      * The time stamp for this event is initialized by invoking
 244      * {@link java.awt.EventQueue#getMostRecentEventTime()}.
 245      * <p>Note that passing in an invalid <code>id</code> results in
 246      * unspecified behavior. This method throws an
 247      * <code>IllegalArgumentException</code> if <code>source</code>
 248      * is <code>null</code>.
 249      *
 250      * @param source the object where the event originated
 251      * @param id the event type
 252      * @param caret the caret (a.k.a. insertion point);
 253      *      <code>null</code> if there's no caret within current
 254      *      composed text
 255      * @param visiblePosition the position that's most important
 256      *      to be visible; <code>null</code> if there's no
 257      *      recommendation for a visible position within current
 258      *      composed text
 259      * @throws IllegalArgumentException if <code>id</code> is not
 260      *      in the range
 261      *      <code>INPUT_METHOD_FIRST</code>..<code>INPUT_METHOD_LAST</code>
 262      * @throws IllegalArgumentException if <code>source</code> is null
 263      */
 264     public InputMethodEvent(Component source, int id, TextHitInfo caret,
 265             TextHitInfo visiblePosition) {
 266         this(source, id,
 267                 AWTAccessor.getEventQueueAccessor()
 268                         .getMostRecentEventTimeForTarget(source),
 269                 null, 0, caret, visiblePosition);
 270     }
 271 
 272     /**
 273      * Gets the combined committed and composed text.
 274      * Characters from index 0 to index <code>getCommittedCharacterCount() - 1</code> are committed
 275      * text, the remaining characters are composed text.
 276      *
 277      * @return the text.
 278      * Always null for CARET_POSITION_CHANGED;
 279      * may be null for INPUT_METHOD_TEXT_CHANGED if there's no composed or committed text.
 280      */
 281     public AttributedCharacterIterator getText() {
 282         return text;
 283     }
 284 
 285     /**
 286      * Gets the number of committed characters in the text.
 287      * @return the number of committed characters in the text
 288      */
 289     public int getCommittedCharacterCount() {


 401         }
 402 
 403         String visiblePositionString;
 404         if (visiblePosition == null) {
 405             visiblePositionString = "no visible position";
 406         } else {
 407             visiblePositionString = "visible position: " + visiblePosition.toString();
 408         }
 409 
 410         return typeStr + ", " + textString + ", " + countString + ", " + caretString + ", " + visiblePositionString;
 411     }
 412 
 413     /**
 414      * Initializes the <code>when</code> field if it is not present in the
 415      * object input stream. In that case, the field will be initialized by
 416      * invoking {@link java.awt.EventQueue#getMostRecentEventTime()}.
 417      */
 418     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
 419         s.defaultReadObject();
 420         if (when == 0) {
 421             when = AWTAccessor.getEventQueueAccessor()
 422                     .getMostRecentEventTimeForTarget(this.source);
 423         }
 424     }
 425 }