jdk/src/share/classes/com/sun/inputmethods/internal/thaiim/ThaiInputMethodImpl.java
changeset 5656 4868963e05e0
parent 5655 8dacdb7bb25b
parent 5645 c98f230a6078
child 5657 7e406ebed9a5
equal deleted inserted replaced
5655:8dacdb7bb25b 5656:4868963e05e0
     1 /*
       
     2  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     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 /*
       
    27  *
       
    28  * (C) Copyright IBM Corp. 2000 - All Rights Reserved
       
    29  *
       
    30  * The original version of this source code and documentation is
       
    31  * copyrighted and owned by IBM. These materials are provided
       
    32  * under terms of a License Agreement between IBM and Sun.
       
    33  * This technology is protected by multiple US and International
       
    34  * patents. This notice and attribution to IBM may not be removed.
       
    35  *
       
    36  */
       
    37 
       
    38 package com.sun.inputmethods.internal.thaiim;
       
    39 
       
    40 import java.awt.im.InputMethodRequests;
       
    41 import java.awt.im.spi.InputMethodContext;
       
    42 
       
    43 import java.awt.Toolkit;
       
    44 import java.awt.event.KeyEvent;
       
    45 import java.awt.event.InputMethodEvent;
       
    46 import java.awt.font.TextAttribute;
       
    47 import java.awt.font.TextHitInfo;
       
    48 
       
    49 import java.text.AttributedCharacterIterator;
       
    50 import java.text.AttributedString;
       
    51 
       
    52 class ThaiInputMethodImpl {
       
    53 
       
    54     private static final char[] keyboardMap = {
       
    55         /* 00 */ '\u0000',
       
    56         /* 01 */ '\u0001',
       
    57         /* 02 */ '\u0002',
       
    58         /* 03 */ '\u0003',
       
    59         /* 04 */ '\u0004',
       
    60         /* 05 */ '\u0005',
       
    61         /* 06 */ '\u0006',
       
    62         /* 07 */ '\u0007',
       
    63         /* 08 */ '\u0008',
       
    64         /* 09 */ '\u0009',
       
    65         /* 0A */ '\012',
       
    66         /* 0B */ '\u000B',
       
    67         /* 0C */ '\u000C',
       
    68         /* 0D */ '\015',
       
    69         /* 0E */ '\u000E',
       
    70         /* 0F */ '\u000F',
       
    71         /* 10 */ '\u0010',
       
    72         /* 11 */ '\u0011',
       
    73         /* 12 */ '\u0012',
       
    74         /* 13 */ '\u0013',
       
    75         /* 14 */ '\u0014',
       
    76         /* 15 */ '\u0015',
       
    77         /* 16 */ '\u0016',
       
    78         /* 17 */ '\u0017',
       
    79         /* 18 */ '\u0018',
       
    80         /* 19 */ '\u0019',
       
    81         /* 1A */ '\u001A',
       
    82         /* 1B */ '\u001B',
       
    83         /* 1C */ '\u001C',
       
    84         /* 1D */ '\u001D',
       
    85         /* 1E */ '\u001E',
       
    86         /* 1F */ '\u001F',
       
    87         /* 20 */ '\u0020',
       
    88         /* 21 */ '\u0e45',   // '!'
       
    89         /* 22 */ '\u002e',   // '"'
       
    90         /* 23 */ '\u0e52',   // '#'
       
    91         /* 24 */ '\u0e53',   // '$'
       
    92         /* 25 */ '\u0e54',   // '%'
       
    93         /* 26 */ '\u0e4e',   // '&'
       
    94         /* 27 */ '\u0e07',   // '''
       
    95         /* 28 */ '\u0e56',   // '('
       
    96         /* 29 */ '\u0e57',   // ')'
       
    97         /* 2A */ '\u0e55',   // '*'
       
    98         /* 2B */ '\u0e59',   // '+'
       
    99         /* 2C */ '\u0e21',   // ','
       
   100         /* 2D */ '\u0e02',   // '-'
       
   101         /* 2E */ '\u0e43',   // '.'
       
   102         /* 2F */ '\u0e1d',   // '/'
       
   103         /* 30 */ '\u0e08',   // '0'
       
   104         /* 31 */ '\u0e3f',   // '1'
       
   105         /* 32 */ '\u002f',   // '2'
       
   106         /* 33 */ '\u002d',   // '3'
       
   107         /* 34 */ '\u0e20',   // '4'
       
   108         /* 35 */ '\u0e16',   // '5'
       
   109         /* 36 */ '\u0e38',   // '6'
       
   110         /* 37 */ '\u0e36',   // '7'
       
   111         /* 38 */ '\u0e04',   // '8'
       
   112         /* 39 */ '\u0e15',   // '9'
       
   113         /* 3A */ '\u0e0b',   // ':'
       
   114         /* 3B */ '\u0e27',   // ';'
       
   115         /* 3C */ '\u0e12',   // '<'
       
   116         /* 3D */ '\u0e0a',   // '='
       
   117         /* 3E */ '\u0e2c',   // '>'
       
   118         /* 3F */ '\u0e26',   // '?'
       
   119         /* 40 */ '\u0e51',   // '@'
       
   120         /* 41 */ '\u0e24',   // 'A'
       
   121         /* 42 */ '\u0e3a',   // 'B'
       
   122         /* 43 */ '\u0e09',   // 'C'
       
   123         /* 44 */ '\u0e0f',   // 'D'
       
   124         /* 45 */ '\u0e0e',   // 'E'
       
   125         /* 46 */ '\u0e42',   // 'F'
       
   126         /* 47 */ '\u0e0c',   // 'G'
       
   127         /* 48 */ '\u0e47',   // 'H'
       
   128         /* 49 */ '\u0e13',   // 'I'
       
   129         /* 4A */ '\u0e4b',   // 'J'
       
   130         /* 4B */ '\u0e29',   // 'K'
       
   131         /* 4C */ '\u0e28',   // 'L'
       
   132         /* 4D */ '\u003f',   // 'M'
       
   133         /* 4E */ '\u0e4c',   // 'N'
       
   134         /* 4F */ '\u0e2f',   // 'O'
       
   135         /* 50 */ '\u0e0d',   // 'P'
       
   136         /* 51 */ '\u0e50',   // 'Q'
       
   137         /* 52 */ '\u0e11',   // 'R'
       
   138         /* 53 */ '\u0e06',   // 'S'
       
   139         /* 54 */ '\u0e18',   // 'T'
       
   140         /* 55 */ '\u0e4a',   // 'U'
       
   141         /* 56 */ '\u0e2e',   // 'V'
       
   142         /* 57 */ '\u0022',   // 'W'
       
   143         /* 58 */ '\u0029',   // 'X'
       
   144         /* 59 */ '\u0e4d',   // 'Y'
       
   145         /* 5A */ '\u0028',   // 'Z'
       
   146         /* 5B */ '\u0e1a',   // '['
       
   147         /* 5C */ '\u0e05',   // '\'
       
   148         /* 5D */ '\u0e25',   // ']'
       
   149         /* 5E */ '\u0e39',   // '^'
       
   150         /* 5F */ '\u0e58',   // '_'
       
   151         /* 60 */ '\u0e4f',   // '`'
       
   152         /* 61 */ '\u0e1f',   // 'a'
       
   153         /* 62 */ '\u0e34',   // 'b'
       
   154         /* 63 */ '\u0e41',   // 'c'
       
   155         /* 64 */ '\u0e01',   // 'd'
       
   156         /* 65 */ '\u0e33',   // 'e'
       
   157         /* 66 */ '\u0e14',   // 'f'
       
   158         /* 67 */ '\u0e40',   // 'g'
       
   159         /* 68 */ '\u0e49',   // 'h'
       
   160         /* 69 */ '\u0e23',   // 'i'
       
   161         /* 6A */ '\u0e48',   // 'j'
       
   162         /* 6B */ '\u0e32',   // 'k'
       
   163         /* 6C */ '\u0e2a',   // 'l'
       
   164         /* 6D */ '\u0e17',   // 'm'
       
   165         /* 6E */ '\u0e37',   // 'n'
       
   166         /* 6F */ '\u0e19',   // 'o'
       
   167         /* 70 */ '\u0e22',   // 'p'
       
   168         /* 71 */ '\u0e46',   // 'q'
       
   169         /* 72 */ '\u0e1e',   // 'r'
       
   170         /* 73 */ '\u0e2b',   // 's'
       
   171         /* 74 */ '\u0e30',   // 't'
       
   172         /* 75 */ '\u0e35',   // 'u'
       
   173         /* 76 */ '\u0e2d',   // 'v'
       
   174         /* 77 */ '\u0e44',   // 'w'
       
   175         /* 78 */ '\u0e1b',   // 'x'
       
   176         /* 79 */ '\u0e31',   // 'y'
       
   177         /* 7A */ '\u0e1c',   // 'z'
       
   178         /* 7B */ '\u0e10',   // '{'
       
   179         /* 7C */ '\u0e03',   // '|'
       
   180         /* 7D */ '\u002c',   // '}'
       
   181         /* 7E */ '\u0e5b',   // '~'
       
   182         /* 7F */ '\u007F'    //
       
   183     };
       
   184 
       
   185     // cached TextHitInfo. Only one type of TextHitInfo is required.
       
   186     private static final TextHitInfo ZERO_TRAILING_HIT_INFO = TextHitInfo.trailing(0);
       
   187 
       
   188     private ThaiRules rules;
       
   189 
       
   190     /**
       
   191      * Returns the equivalent character for thai locale.
       
   192      * @param originalChar The original character.
       
   193      */
       
   194     private char getMappedChar( char originalChar )
       
   195     {
       
   196         if (originalChar <= keyboardMap.length) {
       
   197             return keyboardMap[originalChar];
       
   198         }
       
   199 
       
   200         return originalChar;
       
   201     }//getMappedChar()
       
   202 
       
   203     private InputMethodContext context;
       
   204 
       
   205     void setInputMethodContext(InputMethodContext context) {
       
   206         this.context = context;
       
   207         rules = new ThaiRules((InputMethodRequests)context);
       
   208     }
       
   209 
       
   210     void handleKeyTyped(KeyEvent kevent) {
       
   211         char keyChar = kevent.getKeyChar();
       
   212         char currentChar = getMappedChar(keyChar);
       
   213         if (!Character.UnicodeBlock.THAI.equals(Character.UnicodeBlock.of(currentChar))) {
       
   214             // don't care
       
   215             return;
       
   216         } else if (rules.isInputValid(currentChar)) {
       
   217             Character tmp = new Character(currentChar);
       
   218             String tmp2 = tmp.toString();
       
   219             context.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
       
   220                                              (new AttributedString(tmp2)).getIterator(),
       
   221                                              1,
       
   222                                              ZERO_TRAILING_HIT_INFO,
       
   223                                              ZERO_TRAILING_HIT_INFO);
       
   224         } else {
       
   225             // input sequence is not allowed
       
   226             Toolkit.getDefaultToolkit().beep();
       
   227         }
       
   228 
       
   229         kevent.consume();// prevent client from getting this event.
       
   230         return;
       
   231     }//dispatchEvent()
       
   232 
       
   233     void endComposition() {
       
   234     }//endComposition()
       
   235 }