implements InputMethod : InputMethod « I18N « Java Tutorial






import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.InputMethodEvent;
import java.awt.event.KeyEvent;
import java.awt.im.spi.InputMethod;
import java.awt.im.spi.InputMethodContext;
import java.awt.im.spi.InputMethodDescriptor;
import java.text.AttributedString;
import java.util.Locale;

public class SimpleInputMethod implements InputMethod {
  private static Window statusWindow;

  private InputMethodContext inputMethodContext;

  private Locale locale;

  public SimpleInputMethod() {
    locale = new Locale("iw", "IL");
  }

  public void activate() {
    if (!statusWindow.isVisible()) {
      statusWindow.setVisible(true);
    }
  }

  public void deactivate(boolean isTemporary) {
  }

  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.setVisible(false);
  }

  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();
  }

  public void setInputMethodContext(InputMethodContext context) {
    inputMethodContext = context;
    if (statusWindow == null) {
      statusWindow = context.createInputMethodWindow("Simple Input Method", false);
      Label label = new Label();
      label.setText(locale.getDisplayName());
      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());
    }
  }

  public boolean setLocale(Locale locale) {
    return (locale.equals(this.locale));
  }

  private boolean handleCharacter(char ch) {
    switch (ch) {
    case 'a':
      write('\u05D0'); 
      return true;
    case 'b':
      write('\u05D1'); 
      return true;
    }
    return false;
  }

  private void write(char ch) {
    AttributedString as = new AttributedString(String.valueOf(ch));
    inputMethodContext.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, as
        .getIterator(), 1, null, null);
  }
}

class SimpleInputMethodDescriptor implements InputMethodDescriptor {
  private static Locale HEBREW = new Locale("iw", "IL");

  public SimpleInputMethodDescriptor() {
  }

  public InputMethod createInputMethod() throws Exception {
    return new SimpleInputMethod();
  }

  public Locale[] getAvailableLocales() {
    Locale[] locales = { HEBREW };
    return locales;
  }

  public boolean hasDynamicLocaleList() {
    return false;
  }

  public String getInputMethodDisplayName(Locale inputLocale, Locale displayLanguage) {
    return "Hebrew Input Method";
  }

  public Image getInputMethodIcon(Locale inputLocale) {
    return null;
  }
}








13.17.InputMethod
13.17.1.implements InputMethod