Back to index

im-sdk  12.3.91
PCE.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 sun.awt.im.iiimp;
00045 
00046 import java.util.Hashtable;
00047 import java.util.Stack;
00048 import java.util.Vector;
00049 import java.util.Locale;
00050 import java.util.StringTokenizer;
00051 import java.awt.AWTEvent;
00052 import java.awt.event.KeyEvent;
00053 import java.awt.im.InputContext;
00054 import com.sun.iiim.*;
00055 
00062 public class PCE extends LWE {
00063 
00064     private Stack syntaxStack;
00065     private Hashtable icTable;
00066     private LWESyntax syntax;
00067     
00068     public void dispatchEvent(IIIMEvent e) {
00069 
00070        SyntaxOnOff syntaxOnOff = null;
00071 
00072        InputContext ic = getInputContext();
00073        if (ic == null) {
00074            return;
00075        }
00076        
00077        if (icTable.size() == 0) {
00078            syntax = (LWESyntax)syntaxStack.peek();
00079            if (syntax == null) {
00080               return;
00081            }
00082            syntaxOnOff = new SyntaxOnOff(syntax);
00083            icTable.put(ic, syntaxOnOff);
00084        } else {
00085            if (icTable.containsKey(ic)) {
00086               syntaxOnOff = (SyntaxOnOff)icTable.get(ic);
00087            } else {
00088               syntax = syntax.getCopy();
00089               syntaxOnOff = new SyntaxOnOff(syntax);
00090               icTable.put(ic, syntaxOnOff);
00091            }
00092        }
00093 
00094        if (!syntaxOnOff.willProcess(e)) {
00095            // Conversion Off Mode
00096            return;
00097        }
00098        if (e.isConsumed()) {
00099            // This e is trigger key
00100            return;
00101        }
00102 
00103        IIIMEvent[] events = null;
00104        try {
00105            events = syntax.getEventList(e);
00106        } catch(InvalidSyntaxException ex) {
00107            if (Manager.DEBUG) {
00108               ex.printStackTrace();
00109            }
00110        }
00111 
00112        e.consume();
00113        
00114        if (events != null) {
00115            for (int i = 0; i < events.length; i++) {
00116               markProcessedUpperListener(events[i], this);
00117               int id = events[i].getID();
00118               switch(id) {
00119                 case IIIMEvent.ACTION:
00120                   debug(" action = " + ((IIIMActionEvent)events[i]).getAction());
00121                   dispatchActionEvent((IIIMActionEvent)events[i]);
00122                   break;
00123                 case IIIMEvent.COMMITTED:
00124                   dispatchCommittedEvent((IIIMCommittedEvent)events[i]);
00125                   break;
00126                 case IIIMEvent.PREEDIT:
00127                   dispatchPreeditEvent((IIIMPreeditEvent)events[i]);
00128                   break;
00129                 default:
00130                   debug(" LWESyntax procudes unknown event...");
00131               }
00132            }
00133        }
00134     }
00135 
00136     public String getName() {
00137        return "PCE";
00138     }
00139 
00140     public IIIMEvent[] getTriggerEvent() {
00141        return triggerOns;
00142     }
00143 
00144     public IIIMEvent[] getTriggerOffEvent() {
00145        return triggerOffs;
00146     }
00147 
00148     public String[] getEngineSctipt() {
00149        return new String[] {"Japanese", "Chinese"};
00150     }
00151 
00156     public PCE() {
00157        syntaxStack = new Stack();
00158        icTable = new Hashtable();
00159        if (!initTrigger) {
00160            setupTriggerEvents();
00161        }
00162     }
00163 
00164     public void pushSyntax(LWESyntax syntax) {
00165        syntaxStack.push(syntax);
00166        icTable.clear();
00167     }
00168 
00169     public LWESyntax popSyntax() {
00170        LWESyntax syntax = (LWESyntax)syntaxStack.pop();
00171 
00172        return syntax;
00173     }
00174 
00175     private static final String defaultTrigger = "key<Control+P>";
00176     private static IIIMEvent[] triggerOns, triggerOffs;
00177     private static boolean initTrigger = false;
00178 
00179     /*
00180      * Trigger string representation is
00181      * key<modifier+..+keyname> or action<actioname>.
00182      * More than one trigger can be represented with delimiter ',' or ':'.
00183      * For example in ~/.iiimp propery file
00184      *     iiimf.pce.trigger.on = key<Control+Space>,action<PCETriggerOn>
00185      */
00186     private static void setupTriggerEvents() {
00187        initTrigger = true;
00188 
00189        String trigger =
00190            Manager.getProperty("iiimf.pce.trigger", defaultTrigger);
00191        String triggerOn =
00192            Manager.getProperty("iiimf.pce.trigger.on", defaultTrigger);
00193        String triggerOff
00194            = Manager.getProperty("iiimf.pce.trigger.off", defaultTrigger);
00195        
00196        String onString = trigger.equals(triggerOn) ?
00197            trigger : trigger + "," + triggerOn;
00198        triggerOns = makeIIIMEvents(onString);
00199 
00200        if (triggerOn.equals(triggerOff)) {
00201            triggerOffs = triggerOns;
00202        } else {
00203            String offString = trigger.equals(triggerOff) ?
00204               trigger : trigger + "," + triggerOff;
00205            triggerOffs = makeIIIMEvents(offString);
00206        }
00207     }
00208 
00209     private static IIIMEvent[] makeIIIMEvents(String triggers) {
00210        StringTokenizer ston = new StringTokenizer(triggers, ",:");
00211        Vector vector = new Vector();
00212        while(ston.hasMoreTokens()) {
00213            String trigger = ston.nextToken();
00214            int lastIndex = trigger.lastIndexOf('>');
00215            if (lastIndex < 4)
00216               // ignore wrong representation
00217               continue;
00218            if (trigger.startsWith("key<")) {
00219               String key = trigger.substring(4, lastIndex);
00220               vector.add(new IIIMKeyEvent(key));
00221            } else if (trigger.startsWith("action<")) {
00222               if (lastIndex <= 7)
00223                   continue;
00224               String action = trigger.substring(7, lastIndex);
00225               vector.add(new IIIMActionEvent(action));
00226            }
00227        }
00228        IIIMEvent[] iea = new IIIMEvent[vector.size()];
00229        for (int i = 0; i < iea.length; i++) {
00230            iea[i] = (IIIMEvent)vector.elementAt(i);
00231        }
00232        return iea;
00233     }
00234 
00235     class SyntaxOnOff {
00236 
00237        private LWESyntax syntax;
00238        private boolean on = false;
00239 
00240 
00241        // KEY_TYPED event does not hold modifier information
00242        // so this variable is needed...
00243        private boolean controlDown = false;
00244 
00245        SyntaxOnOff(LWESyntax syntax) {
00246            this.syntax = syntax;
00247            on = false;
00248        }
00249 
00250        LWESyntax getSyntax() {
00251            return syntax;
00252        }
00253 
00254        boolean willProcess(IIIMEvent e) {
00255            if (on) {
00256               return checkTrigger(e, triggerOffs);
00257            } else {
00258               return checkTrigger(e, triggerOns);
00259            }
00260        }
00261 
00262        boolean checkTrigger(IIIMEvent e, IIIMEvent[] triggers) {
00263            AWTEvent ae = e.getAWTEvent();
00264            if (ae != null && ae instanceof KeyEvent) {
00265               KeyEvent ke = (KeyEvent)ae;
00266               int id = ke.getID();
00267               for (int i = 0; i < triggers.length; i++) {
00268                   if (triggers[i] instanceof IIIMKeyEvent) {
00269                      if (((IIIMKeyEvent)triggers[i]).match(ke)) {
00270                          e.consume();
00271                          if (id == KeyEvent.KEY_RELEASED) {
00272                             on = !on;
00273                          }
00274                          break;
00275                      }
00276                   }
00277               }
00278            }
00279            return on;
00280        }
00281     }
00282 
00283     public Locale[] getSupportLocales() {
00284        return null;
00285     }
00286     
00287     private void debug(String str) {
00288        if (Manager.DEBUG) {
00289            System.err.println(str);
00290        }
00291     }
00292 }