FileDocCategorySizeDatePackage
SimpleInputMethod.javaAPI DocExample6872Thu May 17 12:32:28 BST 2001com.ora.intl.ime

SimpleInputMethod.java

package com.ora.intl.ime;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextHitInfo;
import java.awt.im.InputMethodHighlight;
import java.awt.im.spi.InputMethod;
import java.awt.im.spi.InputMethodContext;
import java.text.AttributedString;
import java.util.Locale;

public class SimpleInputMethod implements InputMethod {
  // The status window will be displayed
  // when the input method is active
  private static Window statusWindow;
  private InputMethodContext inputMethodContext;
  private Locale locale;

  public SimpleInputMethod() {
    // Set locale to Hebrew
    locale = new Locale("iw", "IL");
  }

  // When the input method is informed it has been activated,
  // display the status window.
  public void activate() {
    if (!statusWindow.isVisible()) {
      statusWindow.setVisible(true);
    }
  }

  // We do not want to hide the window when the input method
  // is deactivated because it might get activated almost
  // immediately by another client component. If another
  // input method is activated, this input method's hideWindows()
  // method will be called first.
  public void deactivate(boolean isTemporary) {
  }

  // Any characters that we handle via handleCharacter must be
  // consumed so they aren't passed on to the client component.
  public void dispatchEvent(AWTEvent event) {
    if (event.getID() == KeyEvent.KEY_TYPED) {
      KeyEvent e = (KeyEvent) event;
      if (handleCharacter(e.getKeyChar())) {
        e.consume();
      }
    }
  }

  public void dispose() {
  }

  public void endComposition() {
  }

  public Object getControlObject() {
    return null;
  }

  public Locale getLocale() {
    return locale;
  }

  public void hideWindows() {
    statusWindow.hide();
  }

  public boolean isCompositionEnabled() {
    return true;
  }

  public void removeNotify() {
  }

  public void setCharacterSubsets(Character.Subset[] subsets) {
  }

  public void setCompositionEnabled(boolean enable) {
    throw new UnsupportedOperationException();
  }

  public void notifyClientWindowChange(Rectangle location) {
  }

  public void reconvert() {
    throw new UnsupportedOperationException();
  }

  // Once we have been given the InputMethodContext, we can create
  // the status window as a persistent window (the second parameter,
  // attachToInputContext is false). In this case, we simply display
  // the locale of the input method and indicate that it is active.
  public void setInputMethodContext(InputMethodContext context) {
    inputMethodContext = context;
    if (statusWindow == null) {
      statusWindow = context.createInputMethodWindow("Simple Input Method",
                                                     false);
      Label label = new Label();
      label.setBackground(Color.lightGray);
      label.setText(locale.getDisplayName() + "Input Method Active");
      statusWindow.add(label);
      label.setSize(200, 50);
      statusWindow.add(label);
      statusWindow.pack();

      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      statusWindow.setLocation(d.width - statusWindow.getWidth(),
                               d.height - statusWindow.getHeight());
    }
  }

  // If we support the locale passed in to us, return true;
  // otherwise, return false.
  public boolean setLocale(Locale locale) {
    return (locale.equals(this.locale));
  }

  // This method maps Latin keys to Hebrew letters via a large
  // switch statement. If we wanted to generalize this, it would
  // be better to use a property file that is loaded based on the
  // locale. This way, we could create input methods for other types
  // of scripts such as Arabic, Greek, etc.
  private boolean handleCharacter(char ch) {
    switch(ch) {
      case 'a':
        write('\u05D0'); // Hebrew Letter Aleph
        return true;
      case 'b':
        write('\u05D1'); // Hebrew Letter Bet
        return true;
      case 'g':
        write('\u05D2'); // Hebrew Letter Gimmel
        return true;
      case 'd':
        write('\u05D3'); // Hebrew Letter Dalet
        return true;
      case 'h':
        write('\u05D4'); // Hebrew Letter He
        return true;
      case 'v':
        write('\u05D5'); // Hebrew Letter Vav
        return true;
      case 'z':
        write('\u05D6'); // Hebrew Letter Zayin
        return true;
      case 'k':
        write('\u05D7'); // Hebrew Letter Het
        return true;
      case 'j':
        write('\u05D8'); // Hebrew Letter Tet
        return true;
      case 'y':
        write('\u05D9'); // Hebrew Letter Yod
        return true;
      case '\'':
        write('\u05DA'); // Hebrew Letter Final Kaf
        return true;
      case 'c':
        write('\u05DB'); // Hebrew Letter Kaf
        return true;
      case 'l':
        write('\u05DC'); // Hebrew Letter Lamed
        return true;
      case '.':
        write('\u05DD'); // Hebrew Letter Final Mem
        return true;
      case 'm':
        write('\u05DE'); // Hebrew Letter Mem
        return true;
      case ',':
        write('\u05DF'); // Hebrew Letter Final Nun
        return true;
      case 'n':
        write('\u05E0'); // Hebrew Letter Nun
        return true;
      case 's':
        write('\u05E1'); // Hebrew Letter Samekh
        return true;
      case 'i':
        write('\u05E2'); // Hebrew Letter Ayin
        return true;
      case 'p':
        write('\u05E3'); // Hebrew Letter Final Pe
        return true;
      case 'f':
        write('\u05E4'); // Hebrew Letter Pe
        return true;
      case ';':
        write('\u05E5'); // Hebrew Letter Final Tsadi
        return true;
      case 'x':
        write('\u05E6'); // Hebrew Letter Tsadi
        return true;
      case 'q':
        write('\u05E7'); // Hebrew Letter Qof
        return true;
      case 'r':
        write('\u05E8'); // Hebrew Letter Resh
        return true;
      case 'w':
        write('\u05E9'); // Hebrew Letter Shin
        return true;
      case 't':
        write('\u05EA'); // Hebrew Letter Tav
        return true;
    }
    return false;
  }

  // write() is a helper method that takes a character and immediately
  // commits it. The third parameter of dispatchInputMethodEvent indicates
  // how many characters should be committed.
  private void write(char ch) {
    AttributedString as = new AttributedString(String.valueOf(ch));
    inputMethodContext.dispatchInputMethodEvent(
                                InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                                as.getIterator(),
                                1,
                                null,
                                null);
  }
}