1 7 8 package java.awt.event; 9 10 import java.awt.AWTEvent ; 11 import java.awt.Component ; 12 import java.awt.EventQueue ; 13 import java.awt.font.TextHitInfo ; 14 import java.io.IOException ; 15 import java.io.ObjectInputStream ; 16 import java.lang.Integer ; 17 import java.text.AttributedCharacterIterator ; 18 import java.text.CharacterIterator ; 19 20 42 43 public class InputMethodEvent extends AWTEvent { 44 45 48 private static final long serialVersionUID = 4727190874778922661L; 49 50 53 public static final int INPUT_METHOD_FIRST = 1100; 54 55 59 public static final int INPUT_METHOD_TEXT_CHANGED = INPUT_METHOD_FIRST; 60 61 66 public static final int CARET_POSITION_CHANGED = INPUT_METHOD_FIRST + 1; 67 68 71 public static final int INPUT_METHOD_LAST = INPUT_METHOD_FIRST + 1; 72 73 80 long when; 81 82 private transient AttributedCharacterIterator text; 84 private transient int committedCharacterCount; 85 private transient TextHitInfo caret; 86 private transient TextHitInfo visiblePosition; 87 88 131 public InputMethodEvent(Component source, int id, long when, 132 AttributedCharacterIterator text, int committedCharacterCount, 133 TextHitInfo caret, TextHitInfo visiblePosition) { 134 super(source, id); 135 if (id < INPUT_METHOD_FIRST || id > INPUT_METHOD_LAST) { 136 throw new IllegalArgumentException ("id outside of valid range"); 137 } 138 139 if (id == CARET_POSITION_CHANGED && text != null) { 140 throw new IllegalArgumentException ("text must be null for CARET_POSITION_CHANGED"); 141 } 142 143 this.when = when; 144 this.text = text; 145 int textLength = 0; 146 if (text != null) { 147 textLength = text.getEndIndex() - text.getBeginIndex(); 148 } 149 150 if (committedCharacterCount < 0 || committedCharacterCount > textLength) { 151 throw new IllegalArgumentException ("committedCharacterCount outside of valid range"); 152 } 153 this.committedCharacterCount = committedCharacterCount; 154 155 this.caret = caret; 156 this.visiblePosition = visiblePosition; 157 } 158 159 201 public InputMethodEvent(Component source, int id, 202 AttributedCharacterIterator text, int committedCharacterCount, 203 TextHitInfo caret, TextHitInfo visiblePosition) { 204 this(source, id, EventQueue.getMostRecentEventTime(), text, 205 committedCharacterCount, caret, visiblePosition); 206 } 207 208 243 public InputMethodEvent(Component source, int id, TextHitInfo caret, 244 TextHitInfo visiblePosition) { 245 this(source, id, EventQueue.getMostRecentEventTime(), null, 246 0, caret, visiblePosition); 247 } 248 249 258 public AttributedCharacterIterator getText() { 259 return text; 260 } 261 262 265 public int getCommittedCharacterCount() { 266 return committedCharacterCount; 267 } 268 269 281 public TextHitInfo getCaret() { 282 return caret; 283 } 284 285 297 public TextHitInfo getVisiblePosition() { 298 return visiblePosition; 299 } 300 301 305 public void consume() { 306 consumed = true; 307 } 308 309 313 public boolean isConsumed() { 314 return consumed; 315 } 316 317 323 public long getWhen() { 324 return when; 325 } 326 327 337 public String paramString() { 338 String typeStr; 339 switch(id) { 340 case INPUT_METHOD_TEXT_CHANGED: 341 typeStr = "INPUT_METHOD_TEXT_CHANGED"; 342 break; 343 case CARET_POSITION_CHANGED: 344 typeStr = "CARET_POSITION_CHANGED"; 345 break; 346 default: 347 typeStr = "unknown type"; 348 } 349 350 String textString; 351 if (text == null) { 352 textString = "no text"; 353 } else { 354 StringBuffer textBuffer = new StringBuffer ("\""); 355 int committedCharacterCount = this.committedCharacterCount; 356 char c = text.first(); 357 while (committedCharacterCount-- > 0) { 358 textBuffer.append(c); 359 c = text.next(); 360 } 361 textBuffer.append("\" + \""); 362 while (c != CharacterIterator.DONE) { 363 textBuffer.append(c); 364 c = text.next(); 365 } 366 textBuffer.append("\""); 367 textString = textBuffer.toString(); 368 } 369 370 String countString = committedCharacterCount + " characters committed"; 371 372 String caretString; 373 if (caret == null) { 374 caretString = "no caret"; 375 } else { 376 caretString = "caret: " + caret.toString(); 377 } 378 379 String visiblePositionString; 380 if (visiblePosition == null) { 381 visiblePositionString = "no visible position"; 382 } else { 383 visiblePositionString = "visible position: " + visiblePosition.toString(); 384 } 385 386 return typeStr + ", " + textString + ", " + countString + ", " + caretString + ", " + visiblePositionString; 387 } 388 389 394 private void readObject(ObjectInputStream s) throws ClassNotFoundException , IOException { 395 s.defaultReadObject(); 396 if (when == 0) { 397 when = EventQueue.getMostRecentEventTime(); 398 } 399 } 400 } 401 | Popular Tags |