FileDocCategorySizeDatePackage
InputMethodContext.javaAPI DocAndroid 1.5 API17945Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.im

InputMethodContext

public class InputMethodContext extends InputContext implements InputMethodContext
Implementation of InputMethodContext interface, also provides all useful functionality of InputContext

Fields Summary
private InputMethod
inputMethod
private Component
client
private final Map
imInstances
private final Map
localeIM
private final Set
notifyIM
private boolean
pendingClientNotify
a flag indicating that IM should be notified of client window position/visibility changes as soon as it is activated(new client appears)
private Component
nextComp
private final org.apache.harmony.awt.wtk.NativeIM
nativeIM
Constructors Summary
public InputMethodContext()

        notifyIM = new HashSet<InputMethod>();
//???AWT:        imWindows = new HashSet<Window>();
        imInstances = new HashMap<InputMethodDescriptor, InputMethod>();
        localeIM = new HashMap<Locale, InputMethod>();
        selectInputMethod(Locale.US); // not default?
        nativeIM = (NativeIM) inputMethod;
    
Methods Summary
public java.text.AttributedCharacterIteratorcancelLatestCommittedText(java.text.AttributedCharacterIterator.Attribute[] attributes)

        return getIMRequests().cancelLatestCommittedText(attributes);
    
private voidcloseIM(java.awt.im.spi.InputMethod im)

        if (im == null) {
            return;
        }
        if (im.isCompositionEnabled()) {
            im.endComposition();
        }
        
        im.deactivate(true);
        im.hideWindows();
        
    
public voiddispatchInputMethodEvent(int id, java.text.AttributedCharacterIterator text, int committedCharacterCount, java.awt.font.TextHitInfo caret, java.awt.font.TextHitInfo visiblePosition)

        if (client == null) {
            return;
        }
        //???AWT
        /*
        InputMethodEvent ime = new InputMethodEvent(client, id, text,
                                                    committedCharacterCount,
                                                    caret, visiblePosition);
        

        if ((client.getInputMethodRequests() != null) &&
            !IMManager.belowTheSpot()) {
            
            client.dispatchEvent(ime);
        } else {
            
            // show/hide composition window if necessary
            if (committedCharacterCount < text.getEndIndex()) {
                IMManager.showCompositionWindow(getCompositionWindow());
            } else {
                getCompositionWindow().hide();
            }
            composeWindow.getActiveClient().dispatchEvent(ime);
        }
        */
        
    
public voiddispose()

        if (inputMethod != null) {
            closeIM(inputMethod);
            inputMethod.dispose();
        }
        notifyIM.clear();
        super.dispose();
    
public voidenableClientWindowNotification(java.awt.im.spi.InputMethod inputMethod, boolean enable)

        if (enable) {
            notifyIM.add(inputMethod);
            //???AWT
            /*
            if (client != null) {
                notifyClientWindowChange(IMManager.getWindow(client).getBounds());
            } else {
                pendingClientNotify = true;
            }
            */
        } else {
            notifyIM.remove(inputMethod);
        }
        
    
public voidendComposition()

        if (inputMethod != null) {
            inputMethod.endComposition();
        }
        super.endComposition();
    
public final java.awt.ComponentgetClient()

        return client;
    
public java.text.AttributedCharacterIteratorgetCommittedText(int beginIndex, int endIndex, java.text.AttributedCharacterIterator.Attribute[] attributes)

        return getIMRequests().getCommittedText(beginIndex, endIndex,
                                                attributes);
    
public intgetCommittedTextLength()

        return getIMRequests().getCommittedTextLength();
    
private java.awt.im.spi.InputMethodgetIMInstance(java.util.Iterator descriptors, java.util.Locale locale)
Gets input method instance for the given locale from the given list of descriptors

param
descriptors iterator of the list of IM descriptors
param
locale the locale to be supported by the IM
return
input method instance
throws
Exception

        while (descriptors.hasNext()) {
            InputMethodDescriptor desc = descriptors.next();
            Locale[] locs = desc.getAvailableLocales();
            for (Locale element : locs) {
                if (locale.equals(element)) {
                    return getIMInstance(desc);
                }
            }
        }
        return null;
    
private java.awt.im.spi.InputMethodgetIMInstance(java.awt.im.spi.InputMethodDescriptor imd)

        InputMethod im = imInstances.get(imd);
        if (im == null) {
            im = imd.createInputMethod();
            im.setInputMethodContext(this);
            imInstances.put(imd, im);
        }
        return im;
    
private java.awt.im.InputMethodRequestsgetIMRequests()
Gets input method requests for the current client irrespective of input style.

return
input method requests of composition window if client is passive, otherwise input method requests of client

        InputMethodRequests imRequests = null;
    
        if (client != null) {
            imRequests = client.getInputMethodRequests();
            //???AWT
            /*
            if (imRequests == null) {                
                imRequests = getCompositionWindow().getInputMethodRequests();
            }
            */
        }
        
        return imRequests;
    
public final java.awt.im.spi.InputMethodgetInputMethod()

        return inputMethod;
    
public java.lang.ObjectgetInputMethodControlObject()

        if (inputMethod != null) {
            return inputMethod.getControlObject();
        }
        return super.getInputMethodControlObject();
    
public intgetInsertPositionOffset()

        return getIMRequests().getInsertPositionOffset();
    
public java.util.LocalegetLocale()

        if (inputMethod != null) {
            return inputMethod.getLocale();
        }
        return super.getLocale();
    
public java.awt.font.TextHitInfogetLocationOffset(int x, int y)

        InputMethodRequests imr = getStyleIMRequests();
        if (imr != null) {
            return imr.getLocationOffset(x, y);
        }
        return null;
    
public final org.apache.harmony.awt.wtk.NativeIMgetNativeIM()

        return nativeIM;
    
public java.text.AttributedCharacterIteratorgetSelectedText(java.text.AttributedCharacterIterator.Attribute[] attributes)

        return getIMRequests().getSelectedText(attributes);
    
private java.awt.im.InputMethodRequestsgetStyleIMRequests()
Gets input method requests for the current client & input style.

return
input method requests of composition window if input style is "below-the-spot"(or client is passive), otherwise client input method requests

        //???AWT
        /*
        if (IMManager.belowTheSpot()) {
            return getCompositionWindow().getInputMethodRequests();
        }
        */
        return getIMRequests();
    
public java.awt.RectanglegetTextLocation(java.awt.font.TextHitInfo offset)

        
        return getStyleIMRequests().getTextLocation(offset);
    
private voidinitIM(java.awt.im.spi.InputMethod im, java.util.Locale locale)

        if (im == null) {
            return;
        }
        im.setLocale(locale);
        im.setCharacterSubsets(null);
        //???AWT: activateIM(im);
        try {
            im.setCompositionEnabled(inputMethod != null ? 
                                     inputMethod.isCompositionEnabled() : true);
        } catch (UnsupportedOperationException uoe) {

        }
        
    
public booleanisCompositionEnabled()

        if (inputMethod != null) {
            return inputMethod.isCompositionEnabled();
        }
        return super.isCompositionEnabled();
    
public voidnotifyClientWindowChange(java.awt.Rectangle bounds)
To be called by AWT when client Window's bounds/visibility/state change

        if (notifyIM.contains(inputMethod)) {
            inputMethod.notifyClientWindowChange(bounds);
        }
        pendingClientNotify = false;
    
public voidreconvert()

        if (inputMethod != null) {
            inputMethod.reconvert();
        }
        super.reconvert();
    
voidselectIM(java.awt.im.spi.InputMethodDescriptor imd, java.util.Locale locale)
Is called when IM is selected from UI

        try {
            switchToIM(locale, getIMInstance(imd));            
        } catch (Exception e) {
            e.printStackTrace();
        }
    
public booleanselectInputMethod(java.util.Locale locale)

        
        
        if ((inputMethod != null) && inputMethod.setLocale(locale)) {
            return true;
        }
        // first
        // take last user-selected IM for locale            
        InputMethod newIM = localeIM.get(locale);
        
        // if not found search through IM descriptors
        // and take already created instance if exists
        // or create, store new IM instance in descriptor->instance map
        //???AWT
        /*
        if (newIM == null) {
            try {
                newIM = getIMInstance(IMManager.getIMDescriptors().iterator(),
                                      locale);
            } catch (Exception e) {
                // ignore exceptions - just return false
            }
        }
        */
        
        return switchToIM(locale, newIM);
    
public voidsetCharacterSubsets(java.lang.Character.Subset[] subsets)

        if (inputMethod != null) {
            inputMethod.setCharacterSubsets(subsets);
        }
        super.setCharacterSubsets(subsets);
    
public voidsetCompositionEnabled(boolean enable)

        if (inputMethod != null) {
            inputMethod.setCompositionEnabled(enable);
        }
        super.setCompositionEnabled(enable);
    
private booleanswitchToIM(java.util.Locale locale, java.awt.im.spi.InputMethod newIM)

        //???AWT
        /*
        if (newIM != null) {
            closeIM(inputMethod);
            client = KeyboardFocusManager.
            getCurrentKeyboardFocusManager().getFocusOwner();
            initIM(newIM, locale);
            inputMethod = newIM;
            
            return true;
        }
        */
        return false;