Back to index

im-sdk  12.3.91
LWE1.java
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 
00044 package com.sun.iiim.sample.lwe;
00045 
00046 import java.util.Hashtable;
00047 import java.util.Locale;
00048 import java.awt.AWTEvent;
00049 import java.awt.Dimension;
00050 import java.awt.event.KeyEvent;
00051 import java.text.AttributedString;
00052 import java.text.AttributedCharacterIterator;
00053 import com.sun.iiim.*;
00054 
00055 class LWE1 extends LWE {
00056 
00057     private Hashtable firstTable;
00058 
00059     private static final String ISO8859_1 =
00060        "x!\u00a1" +
00061        "/c\u00a2" +
00062        "l-\u00a3" +
00063        "ox\u00a4" +
00064        "y=\u00a5" +
00065        "|-\u00a6" +
00066        "ss\u00a7" +
00067        "co\u00a9" +
00068        "ro\u00ae" +
00069        "x?\u00bf" +
00070        "34\u00be";
00071 
00072     private String[] sampleCandidate = {
00073        "\u00a1",
00074        "\u00a2",
00075        "\u00a3",
00076        "\u00a4",
00077        "\u00a5",
00078        "\u00a6",
00079        "\u00a7",
00080        "\u00a8",
00081        "\u00a9",
00082        "\u00aa",
00083        "\u00ab",
00084        "\u00ac",
00085        "\u00ad",
00086        "\u00ae",
00087        "\u00af",
00088     };
00089     
00090     public LWE1() {
00091 
00092        firstTable = new Hashtable();
00093        
00094        for (int i = 0; i < ISO8859_1.length(); i += 3) {
00095            Character c1 = new Character(ISO8859_1.charAt(i));
00096            Hashtable secondTable;
00097            if (firstTable.containsKey(c1)) {
00098               secondTable = (Hashtable)firstTable.get(c1);
00099            } else {
00100               secondTable = new Hashtable();
00101               firstTable.put(c1, secondTable);
00102            }
00103            Character c2 = new Character(ISO8859_1.charAt(i + 1));
00104            Character c3 = new Character(ISO8859_1.charAt(i + 2));
00105            secondTable.put(c2, c3);
00106        }
00107     }
00108 
00109     public void dispatchEvent(IIIMEvent e) {
00110 
00111        AWTEvent aev = e.getAWTEvent();
00112        if (aev == null)
00113            return;
00114 
00115        if (aev instanceof KeyEvent) {
00116            processKeyEvent(e);
00117        }
00118     }
00119 
00120     public String getName() {
00121        return "Sample LWE1";
00122     }
00123 
00124     private boolean beingProcess = false;
00125     private Character first, second, result;
00126     private boolean haveFirst = false;
00127 
00128     private void processKeyEvent(IIIMEvent e) {
00129 
00130        KeyEvent kev = (KeyEvent)e.getAWTEvent();
00131        int id = kev.getID();
00132 
00133        if (!beingProcess && id == KeyEvent.KEY_PRESSED &&
00134            kev.getKeyCode() == KeyEvent.VK_COMPOSE) {
00135            e.consume();
00136            beingProcess = true;
00137            // fire IIIMStatuEvent
00138            AttributedString aString = new AttributedString("COMPOSE");
00139            AttributedCharacterIterator iterator = aString.getIterator();
00140            dispatchStatusEvent(new IIIMStatusEvent(IIIMStatusEvent.DRAW, iterator));
00141            return;
00142        }
00143 
00144        //
00145        // Normal mode
00146        //
00147        if (!beingProcess) {
00148            return;
00149        }
00150 
00151        //
00152        // Being process 
00153        //
00154        e.consume();
00155 
00156        //
00157        // only KEY_TYPED event will produce something
00158        // during processing
00159        //
00160        if (id == KeyEvent.KEY_RELEASED || id == KeyEvent.KEY_PRESSED) {
00161            return;
00162        }
00163 
00164        if (haveFirst) {
00165            Hashtable secondTable = (Hashtable)firstTable.get(first);
00166 
00167            beingProcess = false;
00168            haveFirst = false;
00169 
00170            if (secondTable == null) {
00171               // reset compose
00172               return;
00173            }
00174            
00175            second = new Character(kev.getKeyChar());
00176            result = (Character)secondTable.get(second);
00177 
00178            if (result == null) {
00179               // do nothing
00180               return;
00181            }
00182 
00183            // fire IIIMCommittedString
00184            dispatchCommittedEvent
00185               (new IIIMCommittedEvent(String.valueOf(result.charValue())));
00186            return;
00187        }
00188 
00189        first = new Character(kev.getKeyChar());
00190        haveFirst = true;
00191     }
00192 
00193     public Locale[] getSupportLocales() {
00194        return new Locale[] {Locale.JAPAN};
00195     }
00196 }
00197 
00198 class LWE2 extends LWE {
00199 
00200     private Hashtable firstTable1, firstTable2;
00201 
00202     private static final String HIRAGANA1 =
00203        "a\u3042" +
00204        "i\u3044" +
00205        "u\u3046" +
00206        "e\u3048" +
00207        "o\u304a";
00208 
00209     private static final String HIRAGANA2 =
00210        "ka\u304b" + "ki\u304d" + "ku\u304f" + "ke\u3051" + "ko\u3053" +
00211        "sa\u3055" + "si\u3057" + "su\u3059" + "se\u305b" + "so\u305d" +
00212        "ta\u305f" + "ti\u3061" + "tu\u3064" + "te\u3066" + "to\u3068" +
00213        "na\u306a" + "ni\u306b" + "nu\u306c" + "ne\u306d" + "no\u306e" +
00214        "ma\u307e" + "mi\u307f" + "mu\u3080" + "me\u3081" + "mo\u3082" +
00215        "ha\u306f" + "hi\u3072" + "fu\u3075" + "he\u3078" + "ho\u307b" +
00216        "ya\u3084" + "yu\u3086" + "yo\u3088" + "wa\u308f";
00217 
00218     public LWE2() {
00219 
00220        firstTable1 = new Hashtable();
00221        firstTable2 = new Hashtable();
00222 
00223        for (int i = 0; i < HIRAGANA1.length(); i += 2) {
00224            Character c1 = new Character(HIRAGANA1.charAt(i));
00225            Character c2 = new Character(HIRAGANA1.charAt(i + 1));
00226            firstTable1.put(c1, c2);
00227        }
00228 
00229        for (int i = 0; i < HIRAGANA2.length(); i += 3) {
00230            Character c1 = new Character(HIRAGANA2.charAt(i));
00231            Hashtable secondTable;
00232            if (firstTable2.containsKey(c1)) {
00233               secondTable = (Hashtable)firstTable2.get(c1);
00234            } else {
00235               secondTable = new Hashtable();
00236               firstTable2.put(c1, secondTable);
00237            }
00238            Character c2 = new Character(HIRAGANA2.charAt(i + 1));
00239            Character c3 = new Character(HIRAGANA2.charAt(i + 2));
00240            secondTable.put(c2, c3);
00241        }
00242     }
00243 
00244     public void dispatchEvent(IIIMEvent e) {
00245 
00246        AWTEvent aev = e.getAWTEvent();
00247        if (aev == null)
00248            return;
00249 
00250        if (aev instanceof KeyEvent) {
00251            processKeyEvent(e);
00252        }
00253     }
00254 
00255     public String getName() {
00256        return "Sanple LWE2";
00257     }
00258 
00259     private boolean beingProcess = false;
00260     private Character first, second, result;
00261     private boolean haveFirst = false;
00262 
00263     private boolean triggerConsume = false;
00264 
00265     private void processKeyEvent(IIIMEvent e) {
00266 
00267        KeyEvent kev = (KeyEvent)e.getAWTEvent();
00268        int id = kev.getID();
00269 
00270        if (!beingProcess && id == KeyEvent.KEY_PRESSED &&
00271            kev.getKeyCode() == KeyEvent.VK_SPACE &&
00272            kev.isControlDown()) {
00273            e.consume();
00274            triggerConsume = true;
00275            beingProcess = true;
00276            // fire IIIMStatusEvent
00277            return;
00278        }
00279 
00280        if (triggerConsume && id == KeyEvent.KEY_TYPED) {
00281            triggerConsume = false;
00282            e.consume();
00283            return;
00284        }
00285 
00286        //
00287        // Conversion off
00288        //
00289        if (!beingProcess) {
00290            return;
00291        }
00292        
00293        //
00294        // Being process
00295        //
00296        e.consume();
00297 
00298        //
00299        // Check Conversion off
00300        //
00301        if (id == KeyEvent.KEY_PRESSED &&
00302            kev.getKeyCode() == KeyEvent.VK_SPACE &&
00303            kev.isControlDown()) {
00304            beingProcess = false;
00305            e.consume();
00306            triggerConsume = true;
00307            // fire IIIMStatusEvent / IIIMPreeditEvetn / IIIMCommittedEvent
00308            return;
00309        }
00310        
00311        //
00312        // only KEY_TYPED event will produce something
00313        // during processing.
00314        //
00315        if (id == KeyEvent.KEY_RELEASED || id == KeyEvent.KEY_PRESSED) {
00316            return;
00317        }
00318 
00319        if (haveFirst) {
00320            Hashtable secondTable = (Hashtable)firstTable2.get(first);
00321 
00322            haveFirst = false;
00323 
00324            if (secondTable == null) {
00325               // reset preedit
00326               return;
00327            }
00328 
00329            second = new Character(kev.getKeyChar());
00330            result = (Character)secondTable.get(second);
00331 
00332            if (result == null) {
00333               // reset preedit
00334               return;
00335            }
00336 
00337            // fire IIIMPreeditString
00338            dispatchCommittedEvent(new IIIMCommittedEvent(String.valueOf
00339                                                    (result.charValue())));
00340            return;
00341        }
00342        
00343        //
00344        // check firstTabel1
00345        //
00346        first = new Character(kev.getKeyChar());
00347        result = (Character)firstTable1.get(first);
00348 
00349        if (result != null) {
00350            // fire IIIPreeditEvent
00351            dispatchCommittedEvent(new IIIMCommittedEvent(String.valueOf
00352                                                    (result.charValue())));
00353            return;
00354        }
00355 
00356        haveFirst = true;
00357     }
00358 
00359     public Locale[] getSupportLocales() {
00360        return new Locale[] {Locale.JAPAN};
00361     }
00362 }