home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 8.5 KB | 245 lines |
- /*
- * @(#)InputMethodEvent.java 1.7 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.event;
-
- import java.awt.AWTEvent;
- import java.awt.Component;
- import java.lang.Integer;
- import java.awt.font.TextHitInfo;
- import java.text.AttributedCharacterIterator;
- import java.text.CharacterIterator;
-
- /**
- * Input method events contain information about text that is being
- * composed using an input method. Whenever the text changes, the
- * input method sends an event to the text editing component that
- * uses it.
- *
- * <p>
- * The text included with the input method event consists of two parts:
- * committed text and composed text. Either part may be empty. The two
- * parts together replace any uncommitted composed text sent in previous events,
- * or the currently selected committed text.
- * Committed text should be integrated into the text component's persistent
- * data, it will not be sent again. Composed text may be sent repeatedly,
- * with changes to reflect the user's editing operations. Committed text
- * always precedes composed text.
- *
- * @version 1.7 03/18/98
- * @author JavaSoft Asia/Pacific
- */
-
- public class InputMethodEvent extends AWTEvent {
-
- /**
- * Marks the first integer id for the range of input method event ids.
- */
- public static final int INPUT_METHOD_FIRST = 1100;
-
- /**
- * The event type indicating changed input method text. This event is
- * generated by input methods while processing input.
- */
- public static final int INPUT_METHOD_TEXT_CHANGED = INPUT_METHOD_FIRST;
-
- /**
- * The event type indicating a changed insertion point in input method text.
- * This event is
- * generated by input methods while processing input if only the caret changed.
- */
- public static final int CARET_POSITION_CHANGED = INPUT_METHOD_FIRST + 1;
-
- /**
- * Marks the last integer id for the range of input method event ids.
- */
- public static final int INPUT_METHOD_LAST = INPUT_METHOD_FIRST + 1;
-
- /**
- * Constant indicating that the entire text sent with this event is committed.
- */
- public static final int ALL_CHARACTERS_COMMITTED = Integer.MAX_VALUE;
-
- // Text object
- private AttributedCharacterIterator text;
- private int committedCharacterCount;
- private TextHitInfo caret;
- private TextHitInfo visiblePosition;
-
- /**
- * Constructs an InputMethodEvent with the specified source component, type,
- * text, caret, and visiblePosition.
- *
- * @param source The object where the event originated.
- * @param id The event type.
- * @param text The combined committed and composed text, committed text first.
- * @param committedCharacterCount The number of committed characters in the text, ALL_CHARACTERS_COMMITTED if all characters are committed.
- * @param caret The caret (a.k.a. insertion point), with offset relative to <code>text</code>. Null if there's no caret.
- * @param visiblePosition The position that's most important to be visible. Null if there's no recommendation.
- */
- public InputMethodEvent(Component source, int id,
- AttributedCharacterIterator text, int committedCharacterCount,
- TextHitInfo caret, TextHitInfo visiblePosition) {
- super(source, id);
- if (id < INPUT_METHOD_FIRST || id > INPUT_METHOD_LAST) {
- throw new IllegalArgumentException("id outside of valid range");
- }
-
- this.text = text;
- int textLength = 0;
- if (text != null) {
- textLength = text.getEndIndex() - text.getBeginIndex();
- }
-
- if (committedCharacterCount != ALL_CHARACTERS_COMMITTED &&
- (committedCharacterCount < 0 || committedCharacterCount > textLength)) {
- throw new IllegalArgumentException("committedCharacterCount outside of valid range");
- }
- this.committedCharacterCount = committedCharacterCount;
-
- this.caret = caret;
- this.visiblePosition = visiblePosition;
- }
-
- /**
- * Constructs an InputMethodEvent with the specified source component, type,
- * caret, and visiblePosition.
- *
- * @param source The object where the event originated.
- * @param id The event type.
- * @param caret The caret (a.k.a. insertion point), with offset relative to <code>text</code>. Null if there's no caret.
- * @param visiblePosition The position that's most important to be visible. Null if there's no recommendation.
- */
- public InputMethodEvent(Component source, int id, TextHitInfo caret,
- TextHitInfo visiblePosition) {
- this(source, id, null, 0, caret, visiblePosition);
- }
-
- /**
- * Gets the combined committed and composed text. If <code>getCommittedCharacterCount</code>
- * returns ALL_CHARACTERS_COMMITTED, then the entire text is committed. Otherwise,
- * Characters from offset 0 to offset <code>getCommittedCharacterCount()</code> are committed
- * text, the remaining characters are composed text.
- *
- * @return the text
- */
- public AttributedCharacterIterator getText() {
- return text;
- }
-
- /**
- * Gets the number of committed characters in the text. If all characters are committed,
- * ALL_CHARACTERS_COMMITTED is returned.
- */
- public int getCommittedCharacterCount() {
- return committedCharacterCount;
- }
-
- /**
- * Gets the caret. Null if there's no caret.
- */
- public TextHitInfo getCaret() {
- return caret;
- }
-
- /**
- * Gets the position that's most important to be visible. Null if there's no recommendation.
- */
- public TextHitInfo getVisiblePosition() {
- return visiblePosition;
- }
-
- /**
- * Consumes this event so that it will not be processed
- * in the default manner by the source which originated it.
- */
- public void consume() {
- consumed = true;
- }
-
- /**
- * Returns whether or not this event has been consumed.
- * @see #consume
- */
- public boolean isConsumed() {
- return consumed;
- }
-
- /**
- * Returns a parameter string identifying this event.
- * This method is useful for event-logging and for debugging.
- *
- * @return a string identifying the event and its attributes
- */
- public String paramString() {
- String typeStr;
- switch(id) {
- case INPUT_METHOD_TEXT_CHANGED:
- typeStr = "INPUT_METHOD_TEXT_CHANGED";
- break;
- case CARET_POSITION_CHANGED:
- typeStr = "CARET_POSITION_CHANGED";
- break;
- default:
- typeStr = "unknown type";
- }
-
- String textString;
- if (text == null) {
- textString = "no text";
- } else {
- StringBuffer textBuffer = new StringBuffer("\"");
- int committedCharacterCount = this.committedCharacterCount;
- if (committedCharacterCount == ALL_CHARACTERS_COMMITTED) {
- committedCharacterCount = text.getEndIndex() - text.getBeginIndex();
- }
- char c = text.first();
- while (committedCharacterCount-- > 0) {
- textBuffer.append(c);
- c = text.next();
- }
- textBuffer.append("\" + \"");
- while (c != CharacterIterator.DONE) {
- textBuffer.append(c);
- c = text.next();
- }
- textBuffer.append("\"");
- textString = textBuffer.toString();
- }
-
- String countString;
- if (committedCharacterCount == ALL_CHARACTERS_COMMITTED) {
- countString = "ALL_CHARACTERS_COMMITTED";
- } else {
- countString = committedCharacterCount + " characters committed";
- }
-
- String caretString;
- if (caret == null) {
- caretString = "no caret";
- } else {
- caretString = "caret: " + caret.toString();
- }
-
- String visiblePositionString;
- if (visiblePosition == null) {
- visiblePositionString = "no visible position";
- } else {
- visiblePositionString = "visible position: " + visiblePosition.toString();
- }
-
- return typeStr + ", " + textString + ", " + countString + ", " + caretString + ", " + visiblePositionString;
- }
- }
-