实施InputMethod : 输入法 « 本土化 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 本土化 » 输入法 
13. 17. 1. 实施InputMethod
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 = (KeyEventevent;
      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(20050);
      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. 输入法
13. 17. 1. 实施InputMethod
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.