2
|
1 |
/*
|
|
2 |
* Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
|
15 |
* - Neither the name of Sun Microsystems nor the names of its
|
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
/*
|
|
33 |
*/
|
|
34 |
|
|
35 |
package com.sun.inputmethods.internal.codepointim;
|
|
36 |
import java.text.AttributedCharacterIterator;
|
|
37 |
import java.util.Map;
|
|
38 |
|
|
39 |
import java.awt.AWTEvent;
|
|
40 |
import java.awt.Toolkit;
|
|
41 |
import java.awt.Rectangle;
|
|
42 |
import java.awt.event.InputMethodEvent;
|
|
43 |
import java.awt.event.KeyEvent;
|
|
44 |
import java.awt.font.TextAttribute;
|
|
45 |
import java.awt.font.TextHitInfo;
|
|
46 |
import java.awt.im.InputMethodHighlight;
|
|
47 |
import java.awt.im.spi.InputMethod;
|
|
48 |
import java.awt.im.spi.InputMethodContext;
|
|
49 |
import java.io.IOException;
|
|
50 |
import java.text.AttributedString;
|
|
51 |
import java.util.Locale;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* The Code Point Input Method is a simple input method that allows Unicode
|
|
55 |
* characters to be entered using their code point or code unit values. See the
|
|
56 |
* accompanying file README.txt for more information.
|
|
57 |
*
|
|
58 |
* @author Brian Beck
|
|
59 |
*/
|
|
60 |
public class CodePointInputMethod implements InputMethod {
|
|
61 |
|
|
62 |
private static final int UNSET = 0;
|
|
63 |
private static final int ESCAPE = 1; // \u0000 - \uFFFF
|
|
64 |
private static final int SPECIAL_ESCAPE = 2; // \U000000 - \U10FFFF
|
|
65 |
private static final int SURROGATE_PAIR = 3; // \uD800\uDC00 - \uDBFF\uDFFF
|
|
66 |
|
|
67 |
private InputMethodContext context;
|
|
68 |
private Locale locale;
|
|
69 |
private StringBuffer buffer;
|
|
70 |
private int insertionPoint;
|
|
71 |
private int format = UNSET;
|
|
72 |
|
|
73 |
|
|
74 |
public CodePointInputMethod() throws IOException {
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* This is the input method's main routine. The composed text is stored
|
|
79 |
* in buffer.
|
|
80 |
*/
|
|
81 |
public void dispatchEvent(AWTEvent event) {
|
|
82 |
// This input method handles KeyEvent only.
|
|
83 |
if (!(event instanceof KeyEvent)) {
|
|
84 |
return;
|
|
85 |
}
|
|
86 |
|
|
87 |
KeyEvent e = (KeyEvent) event;
|
|
88 |
int eventID = event.getID();
|
|
89 |
boolean notInCompositionMode = buffer.length() == 0;
|
|
90 |
|
|
91 |
if (eventID == KeyEvent.KEY_PRESSED) {
|
|
92 |
// If we are not in composition mode, pass through
|
|
93 |
if (notInCompositionMode) {
|
|
94 |
return;
|
|
95 |
}
|
|
96 |
|
|
97 |
switch (e.getKeyCode()) {
|
|
98 |
case KeyEvent.VK_LEFT:
|
|
99 |
moveCaretLeft();
|
|
100 |
break;
|
|
101 |
case KeyEvent.VK_RIGHT:
|
|
102 |
moveCaretRight();
|
|
103 |
break;
|
|
104 |
}
|
|
105 |
} else if (eventID == KeyEvent.KEY_TYPED) {
|
|
106 |
char c = e.getKeyChar();
|
|
107 |
|
|
108 |
// If we are not in composition mode, wait a back slash
|
|
109 |
if (notInCompositionMode) {
|
|
110 |
// If the type character is not a back slash, pass through
|
|
111 |
if (c != '\\') {
|
|
112 |
return;
|
|
113 |
}
|
|
114 |
|
|
115 |
startComposition(); // Enter to composition mode
|
|
116 |
} else {
|
|
117 |
switch (c) {
|
|
118 |
case ' ': // Exit from composition mode
|
|
119 |
finishComposition();
|
|
120 |
break;
|
|
121 |
case '\u007f': // Delete
|
|
122 |
deleteCharacter();
|
|
123 |
break;
|
|
124 |
case '\b': // BackSpace
|
|
125 |
deletePreviousCharacter();
|
|
126 |
break;
|
|
127 |
case '\u001b': // Escape
|
|
128 |
cancelComposition();
|
|
129 |
break;
|
|
130 |
case '\n': // Return
|
|
131 |
case '\t': // Tab
|
|
132 |
sendCommittedText();
|
|
133 |
break;
|
|
134 |
default:
|
|
135 |
composeUnicodeEscape(c);
|
|
136 |
break;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
} else { // KeyEvent.KEY_RELEASED
|
|
140 |
// If we are not in composition mode, pass through
|
|
141 |
if (notInCompositionMode) {
|
|
142 |
return;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
e.consume();
|
|
147 |
}
|
|
148 |
|
|
149 |
private void composeUnicodeEscape(char c) {
|
|
150 |
switch (buffer.length()) {
|
|
151 |
case 1: // \\
|
|
152 |
waitEscapeCharacter(c);
|
|
153 |
break;
|
|
154 |
case 2: // \\u or \\U
|
|
155 |
case 3: // \\ux or \\Ux
|
|
156 |
case 4: // \\uxx or \\Uxx
|
|
157 |
waitDigit(c);
|
|
158 |
break;
|
|
159 |
case 5: // \\uxxx or \\Uxxx
|
|
160 |
if (format == SPECIAL_ESCAPE) {
|
|
161 |
waitDigit(c);
|
|
162 |
} else {
|
|
163 |
waitDigit2(c);
|
|
164 |
}
|
|
165 |
break;
|
|
166 |
case 6: // \\uxxxx or \\Uxxxx
|
|
167 |
if (format == SPECIAL_ESCAPE) {
|
|
168 |
waitDigit(c);
|
|
169 |
} else if (format == SURROGATE_PAIR) {
|
|
170 |
waitBackSlashOrLowSurrogate(c);
|
|
171 |
} else {
|
|
172 |
beep();
|
|
173 |
}
|
|
174 |
break;
|
|
175 |
case 7: // \\Uxxxxx
|
|
176 |
// Only SPECIAL_ESCAPE format uses this state.
|
|
177 |
// Since the second "\\u" of SURROGATE_PAIR format is inserted
|
|
178 |
// automatically, users don't have to type these keys.
|
|
179 |
waitDigit(c);
|
|
180 |
break;
|
|
181 |
case 8: // \\uxxxx\\u
|
|
182 |
case 9: // \\uxxxx\\ux
|
|
183 |
case 10: // \\uxxxx\\uxx
|
|
184 |
case 11: // \\uxxxx\\uxxx
|
|
185 |
if (format == SURROGATE_PAIR) {
|
|
186 |
waitDigit(c);
|
|
187 |
} else {
|
|
188 |
beep();
|
|
189 |
}
|
|
190 |
break;
|
|
191 |
default:
|
|
192 |
beep();
|
|
193 |
break;
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
private void waitEscapeCharacter(char c) {
|
|
198 |
if (c == 'u' || c == 'U') {
|
|
199 |
buffer.append(c);
|
|
200 |
insertionPoint++;
|
|
201 |
sendComposedText();
|
|
202 |
format = (c == 'u') ? ESCAPE : SPECIAL_ESCAPE;
|
|
203 |
} else {
|
|
204 |
if (c != '\\') {
|
|
205 |
buffer.append(c);
|
|
206 |
insertionPoint++;
|
|
207 |
}
|
|
208 |
sendCommittedText();
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
private void waitDigit(char c) {
|
|
213 |
if (Character.digit(c, 16) != -1) {
|
|
214 |
buffer.insert(insertionPoint++, c);
|
|
215 |
sendComposedText();
|
|
216 |
} else {
|
|
217 |
beep();
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
private void waitDigit2(char c) {
|
|
222 |
if (Character.digit(c, 16) != -1) {
|
|
223 |
buffer.insert(insertionPoint++, c);
|
|
224 |
char codePoint = (char)getCodePoint(buffer, 2, 5);
|
|
225 |
if (Character.isHighSurrogate(codePoint)) {
|
|
226 |
format = SURROGATE_PAIR;
|
|
227 |
buffer.append("\\u");
|
|
228 |
insertionPoint = 8;
|
|
229 |
} else {
|
|
230 |
format = ESCAPE;
|
|
231 |
}
|
|
232 |
sendComposedText();
|
|
233 |
} else {
|
|
234 |
beep();
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
private void waitBackSlashOrLowSurrogate(char c) {
|
|
239 |
if (insertionPoint == 6) {
|
|
240 |
if (c == '\\') {
|
|
241 |
buffer.append(c);
|
|
242 |
buffer.append('u');
|
|
243 |
insertionPoint = 8;
|
|
244 |
sendComposedText();
|
|
245 |
} else if (Character.digit(c, 16) != -1) {
|
|
246 |
buffer.append("\\u");
|
|
247 |
buffer.append(c);
|
|
248 |
insertionPoint = 9;
|
|
249 |
sendComposedText();
|
|
250 |
} else {
|
|
251 |
beep();
|
|
252 |
}
|
|
253 |
} else {
|
|
254 |
beep();
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Send the composed text to the client.
|
|
260 |
*/
|
|
261 |
private void sendComposedText() {
|
|
262 |
AttributedString as = new AttributedString(buffer.toString());
|
|
263 |
as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT,
|
|
264 |
InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT);
|
|
265 |
context.dispatchInputMethodEvent(
|
|
266 |
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
|
|
267 |
as.getIterator(), 0,
|
|
268 |
TextHitInfo.leading(insertionPoint), null);
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* Send the committed text to the client.
|
|
273 |
*/
|
|
274 |
private void sendCommittedText() {
|
|
275 |
AttributedString as = new AttributedString(buffer.toString());
|
|
276 |
context.dispatchInputMethodEvent(
|
|
277 |
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
|
|
278 |
as.getIterator(), buffer.length(),
|
|
279 |
TextHitInfo.leading(insertionPoint), null);
|
|
280 |
|
|
281 |
buffer.setLength(0);
|
|
282 |
insertionPoint = 0;
|
|
283 |
format = UNSET;
|
|
284 |
}
|
|
285 |
|
|
286 |
/**
|
|
287 |
* Move the insertion point one position to the left in the composed text.
|
|
288 |
* Do not let the caret move to the left of the "\\u" or "\\U".
|
|
289 |
*/
|
|
290 |
private void moveCaretLeft() {
|
|
291 |
int len = buffer.length();
|
|
292 |
if (--insertionPoint < 2) {
|
|
293 |
insertionPoint++;
|
|
294 |
beep();
|
|
295 |
} else if (format == SURROGATE_PAIR && insertionPoint == 7) {
|
|
296 |
insertionPoint = 8;
|
|
297 |
beep();
|
|
298 |
}
|
|
299 |
|
|
300 |
context.dispatchInputMethodEvent(
|
|
301 |
InputMethodEvent.CARET_POSITION_CHANGED,
|
|
302 |
null, 0,
|
|
303 |
TextHitInfo.leading(insertionPoint), null);
|
|
304 |
}
|
|
305 |
|
|
306 |
/**
|
|
307 |
* Move the insertion point one position to the right in the composed text.
|
|
308 |
*/
|
|
309 |
private void moveCaretRight() {
|
|
310 |
int len = buffer.length();
|
|
311 |
if (++insertionPoint > len) {
|
|
312 |
insertionPoint = len;
|
|
313 |
beep();
|
|
314 |
}
|
|
315 |
|
|
316 |
context.dispatchInputMethodEvent(
|
|
317 |
InputMethodEvent.CARET_POSITION_CHANGED,
|
|
318 |
null, 0,
|
|
319 |
TextHitInfo.leading(insertionPoint), null);
|
|
320 |
}
|
|
321 |
|
|
322 |
/**
|
|
323 |
* Delete the character preceding the insertion point in the composed text.
|
|
324 |
* If the insertion point is not at the end of the composed text and the
|
|
325 |
* preceding text is "\\u" or "\\U", ring the bell.
|
|
326 |
*/
|
|
327 |
private void deletePreviousCharacter() {
|
|
328 |
if (insertionPoint == 2) {
|
|
329 |
if (buffer.length() == 2) {
|
|
330 |
cancelComposition();
|
|
331 |
} else {
|
|
332 |
// Do not allow deletion of the leading "\\u" or "\\U" if there
|
|
333 |
// are other digits in the composed text.
|
|
334 |
beep();
|
|
335 |
}
|
|
336 |
} else if (insertionPoint == 8) {
|
|
337 |
if (buffer.length() == 8) {
|
|
338 |
if (format == SURROGATE_PAIR) {
|
|
339 |
buffer.deleteCharAt(--insertionPoint);
|
|
340 |
}
|
|
341 |
buffer.deleteCharAt(--insertionPoint);
|
|
342 |
sendComposedText();
|
|
343 |
} else {
|
|
344 |
// Do not allow deletion of the second "\\u" if there are other
|
|
345 |
// digits in the composed text.
|
|
346 |
beep();
|
|
347 |
}
|
|
348 |
} else {
|
|
349 |
buffer.deleteCharAt(--insertionPoint);
|
|
350 |
if (buffer.length() == 0) {
|
|
351 |
sendCommittedText();
|
|
352 |
} else {
|
|
353 |
sendComposedText();
|
|
354 |
}
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
/**
|
|
359 |
* Delete the character following the insertion point in the composed text.
|
|
360 |
* If the insertion point is at the end of the composed text, ring the bell.
|
|
361 |
*/
|
|
362 |
private void deleteCharacter() {
|
|
363 |
if (insertionPoint < buffer.length()) {
|
|
364 |
buffer.deleteCharAt(insertionPoint);
|
|
365 |
sendComposedText();
|
|
366 |
} else {
|
|
367 |
beep();
|
|
368 |
}
|
|
369 |
}
|
|
370 |
|
|
371 |
private void startComposition() {
|
|
372 |
buffer.append('\\');
|
|
373 |
insertionPoint = 1;
|
|
374 |
sendComposedText();
|
|
375 |
}
|
|
376 |
|
|
377 |
private void cancelComposition() {
|
|
378 |
buffer.setLength(0);
|
|
379 |
insertionPoint = 0;
|
|
380 |
sendCommittedText();
|
|
381 |
}
|
|
382 |
|
|
383 |
private void finishComposition() {
|
|
384 |
int len = buffer.length();
|
|
385 |
if (len == 6 && format != SPECIAL_ESCAPE) {
|
|
386 |
char codePoint = (char)getCodePoint(buffer, 2, 5);
|
|
387 |
if (Character.isValidCodePoint(codePoint) && codePoint != 0xFFFF) {
|
|
388 |
buffer.setLength(0);
|
|
389 |
buffer.append(codePoint);
|
|
390 |
sendCommittedText();
|
|
391 |
return;
|
|
392 |
}
|
|
393 |
} else if (len == 8 && format == SPECIAL_ESCAPE) {
|
|
394 |
int codePoint = getCodePoint(buffer, 2, 7);
|
|
395 |
if (Character.isValidCodePoint(codePoint) && codePoint != 0xFFFF) {
|
|
396 |
buffer.setLength(0);
|
|
397 |
buffer.appendCodePoint(codePoint);
|
|
398 |
sendCommittedText();
|
|
399 |
return;
|
|
400 |
}
|
|
401 |
} else if (len == 12 && format == SURROGATE_PAIR) {
|
|
402 |
char[] codePoint = {
|
|
403 |
(char)getCodePoint(buffer, 2, 5),
|
|
404 |
(char)getCodePoint(buffer, 8, 11)
|
|
405 |
};
|
|
406 |
if (Character.isHighSurrogate(codePoint[0]) &&
|
|
407 |
Character.isLowSurrogate(codePoint[1])) {
|
|
408 |
buffer.setLength(0);
|
|
409 |
buffer.append(codePoint);
|
|
410 |
sendCommittedText();
|
|
411 |
return;
|
|
412 |
}
|
|
413 |
}
|
|
414 |
|
|
415 |
beep();
|
|
416 |
}
|
|
417 |
|
|
418 |
private int getCodePoint(StringBuffer sb, int from, int to) {
|
|
419 |
int value = 0;
|
|
420 |
for (int i = from; i <= to; i++) {
|
|
421 |
value = (value<<4) + Character.digit(sb.charAt(i), 16);
|
|
422 |
}
|
|
423 |
return value;
|
|
424 |
}
|
|
425 |
|
|
426 |
private static void beep() {
|
|
427 |
Toolkit.getDefaultToolkit().beep();
|
|
428 |
}
|
|
429 |
|
|
430 |
|
|
431 |
public void activate() {
|
|
432 |
if (buffer == null) {
|
|
433 |
buffer = new StringBuffer(12);
|
|
434 |
insertionPoint = 0;
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
public void deactivate(boolean isTemporary) {
|
|
439 |
if (!isTemporary) {
|
|
440 |
buffer = null;
|
|
441 |
}
|
|
442 |
}
|
|
443 |
|
|
444 |
public void dispose() {
|
|
445 |
}
|
|
446 |
|
|
447 |
public Object getControlObject() {
|
|
448 |
return null;
|
|
449 |
}
|
|
450 |
|
|
451 |
public void endComposition() {
|
|
452 |
sendCommittedText();
|
|
453 |
}
|
|
454 |
|
|
455 |
public Locale getLocale() {
|
|
456 |
return locale;
|
|
457 |
}
|
|
458 |
|
|
459 |
public void hideWindows() {
|
|
460 |
}
|
|
461 |
|
|
462 |
public boolean isCompositionEnabled() {
|
|
463 |
// always enabled
|
|
464 |
return true;
|
|
465 |
}
|
|
466 |
|
|
467 |
public void notifyClientWindowChange(Rectangle location) {
|
|
468 |
}
|
|
469 |
|
|
470 |
public void reconvert() {
|
|
471 |
// not supported yet
|
|
472 |
throw new UnsupportedOperationException();
|
|
473 |
}
|
|
474 |
|
|
475 |
public void removeNotify() {
|
|
476 |
}
|
|
477 |
|
|
478 |
public void setCharacterSubsets(Character.Subset[] subsets) {
|
|
479 |
}
|
|
480 |
|
|
481 |
public void setCompositionEnabled(boolean enable) {
|
|
482 |
// not supported yet
|
|
483 |
throw new UnsupportedOperationException();
|
|
484 |
}
|
|
485 |
|
|
486 |
public void setInputMethodContext(InputMethodContext context) {
|
|
487 |
this.context = context;
|
|
488 |
}
|
|
489 |
|
|
490 |
/*
|
|
491 |
* The Code Point Input Method supports all locales.
|
|
492 |
*/
|
|
493 |
public boolean setLocale(Locale locale) {
|
|
494 |
this.locale = locale;
|
|
495 |
return true;
|
|
496 |
}
|
|
497 |
}
|