Java Source Code: com.ora.intl.ime.SimpleInputMethod


   1: 
   2: package com.ora.intl.ime;
   3: 
   4: import java.awt.*;
   5: import java.awt.event.*;
   6: import java.awt.font.TextHitInfo;
   7: import java.awt.im.InputMethodHighlight;
   8: import java.awt.im.spi.InputMethod;
   9: import java.awt.im.spi.InputMethodContext;
  10: import java.text.AttributedString;
  11: import java.util.Locale;
  12: 
  13:	  public class SimpleInputMethod implements InputMethod {
  14:  // The status window will be displayed
  15:  // when the input method is active
  16:  private static Window statusWindow;
  17:  private InputMethodContext inputMethodContext;
  18:  private Locale locale;
  19:
  20:	    public SimpleInputMethod() {
  21:    // Set locale to Hebrew
  22:    locale = new Locale("iw", "IL");
  23:  }
  24:
  25:  // When the input method is informed it has been activated,
  26:  // display the status window.
  27:	    public void activate() {
  28:	      if (!statusWindow.isVisible()) {
  29:      statusWindow.setVisible(true);
  30:    }
  31:  }
  32:
  33:  // We do not want to hide the window when the input method
  34:  // is deactivated because it might get activated almost
  35:  // immediately by another client component. If another
  36:  // input method is activated, this input method's hideWindows()
  37:  // method will be called first.
  38:	    public void deactivate(boolean isTemporary) {
  39:  }
  40:
  41:  // Any characters that we handle via handleCharacter must be
  42:  // consumed so they aren't passed on to the client component.
  43:	    public void dispatchEvent(AWTEvent event) {
  44:	      if (event.getID() == KeyEvent.KEY_TYPED) {
  45:      KeyEvent e = (KeyEvent) event;
  46:	        if (handleCharacter(e.getKeyChar())) {
  47:        e.consume();
  48:      }
  49:    }
  50:  }
  51:
  52:	    public void dispose() {
  53:  }
  54:
  55:	    public void endComposition() {
  56:  }
  57:
  58:	    public Object getControlObject() {
  59:    return null;
  60:  }
  61:
  62:	    public Locale getLocale() {
  63:    return locale;
  64:  }
  65:
  66:	    public void hideWindows() {
  67:    statusWindow.hide();
  68:  }
  69:
  70:	    public boolean isCompositionEnabled() {
  71:    return true;
  72:  }
  73:
  74:	    public void removeNotify() {
  75:  }
  76:
  77:	    public void setCharacterSubsets(Character.Subset[] subsets) {
  78:  }
  79:
  80:	    public void setCompositionEnabled(boolean enable) {
  81:    throw new UnsupportedOperationException();
  82:  }
  83:
  84:	    public void notifyClientWindowChange(Rectangle location) {
  85:  }
  86:
  87:	    public void reconvert() {
  88:    throw new UnsupportedOperationException();
  89:  }
  90:
  91:  // Once we have been given the InputMethodContext, we can create
  92:  // the status window as a persistent window (the second parameter,
  93:  // attachToInputContext is false). In this case, we simply display
  94:  // the locale of the input method and indicate that it is active.
  95:	    public void setInputMethodContext(InputMethodContext context) {
  96:    inputMethodContext = context;
  97:	      if (statusWindow == null) {
  98:      statusWindow = context.createInputMethodWindow("Simple Input Method",
  99:                                                     false);
 100:      Label label = new Label();
 101:      label.setBackground(Color.lightGray);
 102:      label.setText(locale.getDisplayName() + "Input Method Active");
 103:      statusWindow.add(label);
 104:      label.setSize(200, 50);
 105:      statusWindow.add(label);
 106:      statusWindow.pack();
 107:
 108:      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
 109:      statusWindow.setLocation(d.width - statusWindow.getWidth(),
 110:                               d.height - statusWindow.getHeight());
 111:    }
 112:  }
 113:
 114:  // If we support the locale passed in to us, return true;
 115:  // otherwise, return false.
 116:	    public boolean setLocale(Locale locale) {
 117:    return (locale.equals(this.locale));
 118:  }
 119:
 120:  // This method maps Latin keys to Hebrew letters via a large
 121:  // switch statement. If we wanted to generalize this, it would
 122:  // be better to use a property file that is loaded based on the
 123:  // locale. This way, we could create input methods for other types
 124:  // of scripts such as Arabic, Greek, etc.
 125:	    private boolean handleCharacter(char ch) {
 126:	      switch(ch) {
 127:      case 'a':
 128:        write('\u05D0'); // Hebrew Letter Aleph
 129:        return true;
 130:      case 'b':
 131:        write('\u05D1'); // Hebrew Letter Bet
 132:        return true;
 133:      case 'g':
 134:        write('\u05D2'); // Hebrew Letter Gimmel
 135:        return true;
 136:      case 'd':
 137:        write('\u05D3'); // Hebrew Letter Dalet
 138:        return true;
 139:      case 'h':
 140:        write('\u05D4'); // Hebrew Letter He
 141:        return true;
 142:      case 'v':
 143:        write('\u05D5'); // Hebrew Letter Vav
 144:        return true;
 145:      case 'z':
 146:        write('\u05D6'); // Hebrew Letter Zayin
 147:        return true;
 148:      case 'k':
 149:        write('\u05D7'); // Hebrew Letter Het
 150:        return true;
 151:      case 'j':
 152:        write('\u05D8'); // Hebrew Letter Tet
 153:        return true;
 154:      case 'y':
 155:        write('\u05D9'); // Hebrew Letter Yod
 156:        return true;
 157:      case '\'':
 158:        write('\u05DA'); // Hebrew Letter Final Kaf
 159:        return true;
 160:      case 'c':
 161:        write('\u05DB'); // Hebrew Letter Kaf
 162:        return true;
 163:      case 'l':
 164:        write('\u05DC'); // Hebrew Letter Lamed
 165:        return true;
 166:      case '.':
 167:        write('\u05DD'); // Hebrew Letter Final Mem
 168:        return true;
 169:      case 'm':
 170:        write('\u05DE'); // Hebrew Letter Mem
 171:        return true;
 172:      case ',':
 173:        write('\u05DF'); // Hebrew Letter Final Nun
 174:        return true;
 175:      case 'n':
 176:        write('\u05E0'); // Hebrew Letter Nun
 177:        return true;
 178:      case 's':
 179:        write('\u05E1'); // Hebrew Letter Samekh
 180:        return true;
 181:      case 'i':
 182:        write('\u05E2'); // Hebrew Letter Ayin
 183:        return true;
 184:      case 'p':
 185:        write('\u05E3'); // Hebrew Letter Final Pe
 186:        return true;
 187:      case 'f':
 188:        write('\u05E4'); // Hebrew Letter Pe
 189:        return true;
 190:      case ';':
 191:        write('\u05E5'); // Hebrew Letter Final Tsadi
 192:        return true;
 193:      case 'x':
 194:        write('\u05E6'); // Hebrew Letter Tsadi
 195:        return true;
 196:      case 'q':
 197:        write('\u05E7'); // Hebrew Letter Qof
 198:        return true;
 199:      case 'r':
 200:        write('\u05E8'); // Hebrew Letter Resh
 201:        return true;
 202:      case 'w':
 203:        write('\u05E9'); // Hebrew Letter Shin
 204:        return true;
 205:      case 't':
 206:        write('\u05EA'); // Hebrew Letter Tav
 207:        return true;
 208:    }
 209:    return false;
 210:  }
 211:
 212:  // write() is a helper method that takes a character and immediately
 213:  // commits it. The third parameter of dispatchInputMethodEvent indicates
 214:  // how many characters should be committed.
 215:	    private void write(char ch) {
 216:    AttributedString as = new AttributedString(String.valueOf(ch));
 217:    inputMethodContext.dispatchInputMethodEvent(
 218:                                InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
 219:                                as.getIterator(),
 220:                                1,
 221:                                null,
 222:                                null);
 223:  }
 224:}