FileDocCategorySizeDatePackage
ArabicHebrewInputMethod.javaAPI DocExample12669Thu May 17 12:34:02 BST 2001com.ora.intl.ime.arabichebrewim

ArabicHebrewInputMethod

public class ArabicHebrewInputMethod extends Object implements InputMethod

Fields Summary
private static Locale
HEBREW
private static Locale
ARABIC
private static Locale[]
SUPPORTED_LOCALES
private static Properties
arabichebrewNames
private static Window
statusWindow
private Vector
lookupCandidates
private int
lookupCandidateCount
private LookupList
lookupList
private int
lookupSelection
private InputMethodContext
inputMethodContext
private Locale
locale
private boolean
transliterated
private StringBuffer
rawText
private String
transliteratedText
Constructors Summary
public ArabicHebrewInputMethod()


      
    if (arabichebrewNames == null) {
      arabichebrewNames = loadProperties("names.properties");
    }
    rawText = new StringBuffer();
  
Methods Summary
private voidacceptSelection()

    sendText(true);
    rawText.setLength(0);
    transliteratedText = null;
    transliterated = false;
  
public voidactivate()

    if (!statusWindow.isVisible()) {
      statusWindow.setVisible(true);
    }
    updateStatusWindow(locale);
  
voidcloseLookupWindow()

    if (lookupList != null) {
      lookupList.setVisible(false);
      lookupList = null;
    }
  
public voiddeactivate(boolean isTemporary)

    closeLookupWindow();
  
public voiddispatchEvent(java.awt.AWTEvent event)

    if (event.getID() == KeyEvent.KEY_TYPED) {
      KeyEvent e = (KeyEvent) event;
      if (handleCharacter(e.getKeyChar())) {
        e.consume();
      }
    }
  
public voiddispose()

    closeLookupWindow();
  
public voidendComposition()

    if (rawText.length() != 0) {
      acceptSelection();
    }
    closeLookupWindow();
  
public java.lang.ObjectgetControlObject()

    return null;
  
public java.util.LocalegetLocale()

    return locale;
  
private booleanhandleCharacter(char ch)

    // if the lookup window is active...
    if (lookupList != null) {
      if (ch == ' ") {
        if (++lookupSelection == lookupCandidateCount) {
          lookupSelection = 0;
        }
        selectCandidate(lookupSelection);
        return true;
      } else if (ch == '\n") {
        acceptSelection();
        closeLookupWindow();
        return true;
      }
      // otherwise, if the Latin letters have already been
      // transliterated to Arabic or Hebrew (transliterated state)
    } else if (transliterated) {
      if (ch == ' ") {
        showDerivedWords();
        return true;
      } else if (ch == '\n") {
        acceptSelection();
        return true;
      } else {
        Toolkit.getDefaultToolkit().beep();
        return true;
      }
      // initial state - we need to transliterate from Latin to Hebrew
    } else {
      if (ch == ' ") {
        transliterateLatinToRoot();
        return true;
      } else if (ch == '\n") {
        if (rawText.length() != 0) {
          acceptSelection();
          return true;
        }
      } else if (ch == '\b") {
        if (rawText.length() != 0) {
          rawText.setLength(rawText.length() - 1);
          sendText(false);
          return true;
        }
      // if a Latin letter is entered...
      } else if (Character.UnicodeBlock.of(ch) ==
        Character.UnicodeBlock.BASIC_LATIN && Character.isLetter(ch)) {
        rawText.append(ch);
        sendText(false);
        return true;
      // if we are already in the middle of composition, don't allow a
      // character other than a Latin letter to be entered.
      } else if (rawText.length() != 0) {
        Toolkit.getDefaultToolkit().beep();
        return true;
      }
    }
    return false;
  
public voidhideWindows()

    closeLookupWindow();
    statusWindow.hide();
  
public booleanisCompositionEnabled()

    return true;
  
private java.util.PropertiesloadProperties(java.lang.String fileName)

    Properties props = new Properties();
    InputStream stream = this.getClass().getResourceAsStream(fileName);
    props.load(stream);
    stream.close();
    return props;
  
public voidnotifyClientWindowChange(java.awt.Rectangle location)

  
voidopenLookupWindow()

    lookupList = new LookupList(this, inputMethodContext, lookupCandidates);
    lookupList.selectCandidate(lookupSelection);
  
public voidreconvert()

    throw new UnsupportedOperationException();
  
public voidremoveNotify()

  
voidselectCandidate(int candidate)

    lookupSelection = candidate;
    lookupList.selectCandidate(lookupSelection);
    transliteratedText = (String) lookupCandidates.elementAt(lookupSelection);
    sendText(false);
  
private voidsendText(boolean committed)

    String text;
    InputMethodHighlight highlight;
    int committedCharacterCount = 0;
    TextHitInfo caret = null;

    if (transliterated) {
      text = transliteratedText;
      highlight = InputMethodHighlight.SELECTED_CONVERTED_TEXT_HIGHLIGHT;
    } else {
      text = new String(rawText);
      highlight = InputMethodHighlight.SELECTED_RAW_TEXT_HIGHLIGHT;
    }

    AttributedString as = new AttributedString(text);
    if (committed) {
      committedCharacterCount = text.length();
    } else if (text.length() > 0) {
      as.addAttribute(TextAttribute.INPUT_METHOD_HIGHLIGHT, highlight);
      caret = TextHitInfo.leading(text.length());
    }

    inputMethodContext.dispatchInputMethodEvent(
                                    InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                                    as.getIterator(),
                                    committedCharacterCount,
                                    caret,
                                    null);
  
public voidsetCharacterSubsets(java.lang.Character$Subset[] subsets)

  
public voidsetCompositionEnabled(boolean enable)

    throw new UnsupportedOperationException();
  
public voidsetInputMethodContext(java.awt.im.spi.InputMethodContext context)

    inputMethodContext = context;
    if (statusWindow == null) {
      statusWindow = context.createInputMethodWindow(
                                                    "ArabicHebrew Input Method",
                                                    false);
      Label label = new Label();
      label.setBackground(Color.white);
      statusWindow.add(label);
      updateStatusWindow(locale);
      label.setSize(200, 50);
      statusWindow.pack();

      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      statusWindow.setLocation(d.width - statusWindow.getWidth(),
                               d.height - statusWindow.getHeight());
    }
  
public booleansetLocale(java.util.Locale locale)

    for (int i = 0; i < SUPPORTED_LOCALES.length; i++) {
      if (locale.equals(SUPPORTED_LOCALES[i])) {
        this.locale = locale;
        if (statusWindow != null) {
          updateStatusWindow(locale);
        }
        return true;
      }
    }
    return false;
  
private voidshowDerivedWords()

    String lookupName;
    lookupName = rawText.toString() + "_" + locale + ".properties";
    Properties lookupList;
    String lookupString;

    try {
      lookupList = loadProperties(lookupName);
    } catch (IOException e) {
      // no lookup information
      return;
    }

    lookupCandidates = new Vector();
    lookupCandidateCount = 1;

    while (true) {
      lookupString = (String)lookupList.getProperty(
                                         String.valueOf(lookupCandidateCount));
      if (lookupString == null) {
        lookupCandidateCount--;
        break;
      }

      lookupCandidates.add(lookupString);
      lookupCandidateCount++;
    }

    lookupSelection = 0;
    openLookupWindow();
  
private voidtransliterateLatinToRoot()

    String lookupName;
    lookupName = rawText.toString();
    String localeLookupName = lookupName + "_" + locale;

    while (true) {
      transliteratedText = (String) arabichebrewNames.get(localeLookupName);
      if (transliteratedText != null) {
        break;
      }
      int index = localeLookupName.lastIndexOf("_");
      if (index == -1) {
        break;
      }
      localeLookupName = localeLookupName.substring(0, index);
    }

    if (transliteratedText != null) {
      transliterated = true;
      sendText(false);
    } else {
      Toolkit.getDefaultToolkit().beep();
    }
  
voidupdateStatusWindow(java.util.Locale locale)

    Label label = (Label) statusWindow.getComponent(0);
    String localeName = locale == null ? "null" : locale.getDisplayName();
    label.setText(localeName + " IME Active");