001: /*
002: * Portions Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * @(#)ThaiInputMethodImpl.java 1.12 07/05/05
028: *
029: * (C) Copyright IBM Corp. 2000 - All Rights Reserved
030: *
031: * The original version of this source code and documentation is
032: * copyrighted and owned by IBM. These materials are provided
033: * under terms of a License Agreement between IBM and Sun.
034: * This technology is protected by multiple US and International
035: * patents. This notice and attribution to IBM may not be removed.
036: *
037: */
038:
039: package com.sun.inputmethods.internal.thaiim;
040:
041: import java.awt.im.InputMethodRequests;
042: import java.awt.im.spi.InputMethodContext;
043:
044: import java.awt.Toolkit;
045: import java.awt.event.KeyEvent;
046: import java.awt.event.InputMethodEvent;
047: import java.awt.font.TextAttribute;
048: import java.awt.font.TextHitInfo;
049:
050: import java.text.AttributedCharacterIterator;
051: import java.text.AttributedString;
052:
053: class ThaiInputMethodImpl {
054:
055: private static final char[] keyboardMap = {
056: /* 00 */'\u0000',
057: /* 01 */'\u0001',
058: /* 02 */'\u0002',
059: /* 03 */'\u0003',
060: /* 04 */'\u0004',
061: /* 05 */'\u0005',
062: /* 06 */'\u0006',
063: /* 07 */'\u0007',
064: /* 08 */'\u0008',
065: /* 09 */'\u0009',
066: /* 0A */'\012',
067: /* 0B */'\u000B',
068: /* 0C */'\u000C',
069: /* 0D */'\015',
070: /* 0E */'\u000E',
071: /* 0F */'\u000F',
072: /* 10 */'\u0010',
073: /* 11 */'\u0011',
074: /* 12 */'\u0012',
075: /* 13 */'\u0013',
076: /* 14 */'\u0014',
077: /* 15 */'\u0015',
078: /* 16 */'\u0016',
079: /* 17 */'\u0017',
080: /* 18 */'\u0018',
081: /* 19 */'\u0019',
082: /* 1A */'\u001A',
083: /* 1B */'\u001B',
084: /* 1C */'\u001C',
085: /* 1D */'\u001D',
086: /* 1E */'\u001E',
087: /* 1F */'\u001F',
088: /* 20 */'\u0020',
089: /* 21 */'\u0e45', // '!'
090: /* 22 */'\u002e', // '"'
091: /* 23 */'\u0e52', // '#'
092: /* 24 */'\u0e53', // '$'
093: /* 25 */'\u0e54', // '%'
094: /* 26 */'\u0e4e', // '&'
095: /* 27 */'\u0e07', // '''
096: /* 28 */'\u0e56', // '('
097: /* 29 */'\u0e57', // ')'
098: /* 2A */'\u0e55', // '*'
099: /* 2B */'\u0e59', // '+'
100: /* 2C */'\u0e21', // ','
101: /* 2D */'\u0e02', // '-'
102: /* 2E */'\u0e43', // '.'
103: /* 2F */'\u0e1d', // '/'
104: /* 30 */'\u0e08', // '0'
105: /* 31 */'\u0e3f', // '1'
106: /* 32 */'\u002f', // '2'
107: /* 33 */'\u002d', // '3'
108: /* 34 */'\u0e20', // '4'
109: /* 35 */'\u0e16', // '5'
110: /* 36 */'\u0e38', // '6'
111: /* 37 */'\u0e36', // '7'
112: /* 38 */'\u0e04', // '8'
113: /* 39 */'\u0e15', // '9'
114: /* 3A */'\u0e0b', // ':'
115: /* 3B */'\u0e27', // ';'
116: /* 3C */'\u0e12', // '<'
117: /* 3D */'\u0e0a', // '='
118: /* 3E */'\u0e2c', // '>'
119: /* 3F */'\u0e26', // '?'
120: /* 40 */'\u0e51', // '@'
121: /* 41 */'\u0e24', // 'A'
122: /* 42 */'\u0e3a', // 'B'
123: /* 43 */'\u0e09', // 'C'
124: /* 44 */'\u0e0f', // 'D'
125: /* 45 */'\u0e0e', // 'E'
126: /* 46 */'\u0e42', // 'F'
127: /* 47 */'\u0e0c', // 'G'
128: /* 48 */'\u0e47', // 'H'
129: /* 49 */'\u0e13', // 'I'
130: /* 4A */'\u0e4b', // 'J'
131: /* 4B */'\u0e29', // 'K'
132: /* 4C */'\u0e28', // 'L'
133: /* 4D */'\u003f', // 'M'
134: /* 4E */'\u0e4c', // 'N'
135: /* 4F */'\u0e2f', // 'O'
136: /* 50 */'\u0e0d', // 'P'
137: /* 51 */'\u0e50', // 'Q'
138: /* 52 */'\u0e11', // 'R'
139: /* 53 */'\u0e06', // 'S'
140: /* 54 */'\u0e18', // 'T'
141: /* 55 */'\u0e4a', // 'U'
142: /* 56 */'\u0e2e', // 'V'
143: /* 57 */'\u0022', // 'W'
144: /* 58 */'\u0029', // 'X'
145: /* 59 */'\u0e4d', // 'Y'
146: /* 5A */'\u0028', // 'Z'
147: /* 5B */'\u0e1a', // '['
148: /* 5C */'\u0e05', // '\'
149: /* 5D */'\u0e25', // ']'
150: /* 5E */'\u0e39', // '^'
151: /* 5F */'\u0e58', // '_'
152: /* 60 */'\u0e4f', // '`'
153: /* 61 */'\u0e1f', // 'a'
154: /* 62 */'\u0e34', // 'b'
155: /* 63 */'\u0e41', // 'c'
156: /* 64 */'\u0e01', // 'd'
157: /* 65 */'\u0e33', // 'e'
158: /* 66 */'\u0e14', // 'f'
159: /* 67 */'\u0e40', // 'g'
160: /* 68 */'\u0e49', // 'h'
161: /* 69 */'\u0e23', // 'i'
162: /* 6A */'\u0e48', // 'j'
163: /* 6B */'\u0e32', // 'k'
164: /* 6C */'\u0e2a', // 'l'
165: /* 6D */'\u0e17', // 'm'
166: /* 6E */'\u0e37', // 'n'
167: /* 6F */'\u0e19', // 'o'
168: /* 70 */'\u0e22', // 'p'
169: /* 71 */'\u0e46', // 'q'
170: /* 72 */'\u0e1e', // 'r'
171: /* 73 */'\u0e2b', // 's'
172: /* 74 */'\u0e30', // 't'
173: /* 75 */'\u0e35', // 'u'
174: /* 76 */'\u0e2d', // 'v'
175: /* 77 */'\u0e44', // 'w'
176: /* 78 */'\u0e1b', // 'x'
177: /* 79 */'\u0e31', // 'y'
178: /* 7A */'\u0e1c', // 'z'
179: /* 7B */'\u0e10', // '{'
180: /* 7C */'\u0e03', // '|'
181: /* 7D */'\u002c', // '}'
182: /* 7E */'\u0e5b', // '~'
183: /* 7F */'\u007F' //
184: };
185:
186: // cached TextHitInfo. Only one type of TextHitInfo is required.
187: private static final TextHitInfo ZERO_TRAILING_HIT_INFO = TextHitInfo
188: .trailing(0);
189:
190: private ThaiRules rules;
191:
192: /**
193: * Returns the equivalent character for thai locale.
194: * @param originalChar The original character.
195: */
196: private char getMappedChar(char originalChar) {
197: if (originalChar <= keyboardMap.length) {
198: return keyboardMap[originalChar];
199: }
200:
201: return originalChar;
202: }//getMappedChar()
203:
204: private InputMethodContext context;
205:
206: void setInputMethodContext(InputMethodContext context) {
207: this .context = context;
208: rules = new ThaiRules((InputMethodRequests) context);
209: }
210:
211: void handleKeyTyped(KeyEvent kevent) {
212: char keyChar = kevent.getKeyChar();
213: char currentChar = getMappedChar(keyChar);
214: if (!Character.UnicodeBlock.THAI.equals(Character.UnicodeBlock
215: .of(currentChar))) {
216: // don't care
217: return;
218: } else if (rules.isInputValid(currentChar)) {
219: Character tmp = new Character(currentChar);
220: String tmp2 = tmp.toString();
221: context.dispatchInputMethodEvent(
222: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
223: (new AttributedString(tmp2)).getIterator(), 1,
224: ZERO_TRAILING_HIT_INFO, ZERO_TRAILING_HIT_INFO);
225: } else {
226: // input sequence is not allowed
227: Toolkit.getDefaultToolkit().beep();
228: }
229:
230: kevent.consume();// prevent client from getting this event.
231: return;
232: }//dispatchEvent()
233:
234: void endComposition() {
235: }//endComposition()
236: }
|