java.awt.im

Class InputContext


public class InputContext
extends Object

Provides a context for controlling input methods and keyboard layouts. This class provides the communication layer between the client component, and the various locale-dependent text entry input methods that can be used for the client. By default, there is one instance per Window, shared among all components, but this limits text entry to one component at a time. Thus, text components can create their own instance to allow text entry in multiple components at a time.

By using the interfaces of java.awt.im.spi, you can install extensions which allow additional input methods. Some of these may use platform native input methods, or keyboard layouts provided by the platform. Input methods are unavailable if none have been installed and the platform has no underlying native input methods. Extensions are installed as jar files, usually accessed in the default extension location or specified by the -extdir VM flag. The jar must contain a file named "META_INF/services/java.awt.im.spi.InputMethodDescriptor" which lists, one entry per line in UTF-8 encoding, each class in the jar that implements java.awt.im.spi.InputMethodDescriptor.

Since:
1.2
See Also:
Component.getInputContext(), Component.enableInputMethods(boolean)

Constructor Summary

InputContext()
Construct an InputContext.

Method Summary

void
dispatchEvent(AWTEvent event)
Dispatches an event to the current input method.
void
dispose()
Disposes of the input context and release the resources used by it.
void
endComposition()
Ends any input composition that may currently be going on in this context.
Object
getInputMethodControlObject()
Returns a control object from the current input method, or null.
static InputContext
getInstance()
Returns a new InputContext.
Locale
getLocale()
Returns the current locale of the current input method or keyboard layout.
boolean
isCompositionEnabled()
Find out if the current input method is enabled.
void
reconvert()
Starts a reconversion operation in the current input method.
void
removeNotify(Component client)
Notifies the input context that a client component has been removed from its containment hierarchy, or that input method support has been disabled for the component.
boolean
selectInputMethod(Locale locale)
Attempts to select an input method or keyboard layout which supports the given locale.
void
setCharacterSubsets(Character.Subset[] subsets)
Sets the subsets of Unicode characters allowed to be input by the current input method, as well as subsequent input methods.
void
setCompositionEnabled(boolean enable)
Changes the enabled status of the current input method.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

InputContext

protected InputContext()
Construct an InputContext. This is protected, so clients must use getInstance() instead.

Method Details

dispatchEvent

public void dispatchEvent(AWTEvent event)
Dispatches an event to the current input method. This is called automatically by AWT. If no input method is available, then the event will never be consumed.
Parameters:
event - the event to dispatch
Throws:
NullPointerException - if event is null

dispose

public void dispose()
Disposes of the input context and release the resources used by it. Called automatically by AWT for the default input context of each Window. If no input methods are available, then this method has no effect.

endComposition

public void endComposition()
Ends any input composition that may currently be going on in this context. Depending on the platform and possibly user preferences, this may commit or delete uncommitted text. Any changes to the text are communicated to the active component using an input method event. If no input methods are available, then this method has no effect. This may be called for a variety of reasons, such as when the user moves the insertion point in the client text outside the range of the composed text, or when text is saved to file.

getInputMethodControlObject

public Object getInputMethodControlObject()
Returns a control object from the current input method, or null. A control object provides implementation-dependent methods that control the behavior of the input method or obtain information from the input method. Clients have to compare the result against known input method control object types. If no input methods are available or the current input method does not provide an input method control object, then null is returned.
Returns:
the control object, or null

getInstance

public static InputContext getInstance()
Returns a new InputContext.
Returns:
a new instance, initialized to the default locale if available

getLocale

public Locale getLocale()
Returns the current locale of the current input method or keyboard layout. Returns null if the input context does not have a current input method or keyboard layout or if the current input method's InputMethod.getLocale() method returns null. Not all host operating systems provide API to determine the locale of the currently selected native input method or keyboard layout. For host operating systems that don't provide such API, getLocale assumes that the current locale of all native input methods or keyboard layouts provided by the host operating system is the system's default locale.
Returns:
the locale of the current input method, or null
Since:
1.3

isCompositionEnabled

public boolean isCompositionEnabled()
Find out if the current input method is enabled.
Returns:
true if the current input method is enabled
Throws:
UnsupportedOperationException - if there is no current input method, or the input method does not support enabling
Since:
1.3

reconvert

public void reconvert()
Starts a reconversion operation in the current input method. The input method gets the text to reconvert from the client component, using InputMethodRequests.getSelectedText(AttributedCharacterIterator.Attribute[]). Then the composed and committed text produced by the operation is sent back to the client using a sequence of InputMethodRequests.
Throws:
UnsupportedOperationException - if there is no current input method, or the input method does not support reconversion
Since:
1.3

removeNotify

public void removeNotify(Component client)
Notifies the input context that a client component has been removed from its containment hierarchy, or that input method support has been disabled for the component. This method is usually called from the client component's Component.removeNotify() method. Potentially pending input from input methods for this component is discarded. If no input methods are available, then this method has no effect.
Parameters:
client - the client component
Throws:
NullPointerException - if client is null

selectInputMethod

public boolean selectInputMethod(Locale locale)
Attempts to select an input method or keyboard layout which supports the given locale. This returns true if a locale is available and was selected. The following steps are taken in choosing an input method:
  • If the currently selected input method or keyboard layout supports the requested locale, it remains selected.
  • If there is no input method or keyboard layout available that supports the requested locale, the current input method or keyboard layout remains selected.
  • If the user has previously selected an input method or keyboard layout for the requested locale from the user interface, then the most recently selected such input method or keyboard layout is reselected.
  • Otherwise, an input method or keyboard layout that supports the requested locale is selected in an implementation dependent way. This implementation chooses the first input method which supports the requested locale based on the InputMethodDescriptors loaded from the extensions installed on the CLASSPATH.

Before switching away from an input method, any currently uncommitted text is committed. Not all host operating systems provide API to determine the locale of the currently selected native input method or keyboard layout, and to select a native input method or keyboard layout by locale. For host operating systems that don't provide such API, selectInputMethod assumes that native input methods or keyboard layouts provided by the host operating system support only the system's default locale.

An example of where this may be called is in a multi-language document, when moving the insertion point between sections of different locale, so that the user may use the input method appropriate to that section of the document.

Parameters:
locale - the desired new locale
Returns:
true if the new locale is active
Throws:
NullPointerException - if locale is null

setCharacterSubsets

public void setCharacterSubsets(Character.Subset[] subsets)
Sets the subsets of Unicode characters allowed to be input by the current input method, as well as subsequent input methods. The value of null implies all characters are legal. Applications should not rely on this behavior, since native host input methods may not allow restrictions. If no current input method is available, this has no immediate effect.
Parameters:
subsets - the set of Unicode subsets to accept, or null

setCompositionEnabled

public void setCompositionEnabled(boolean enable)
Changes the enabled status of the current input method. An input method that is enabled for composition interprets incoming events for both composition and control purposes, while a disabled input method only interprets control commands (including commands to enable itself).
Parameters:
enable - whether to enable the input method
Throws:
UnsupportedOperationException - if there is no current input method, or the input method does not support enabling
Since:
1.3

InputContext.java -- provides the context for text input Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.