1. /*
  2. * @(#)InputContext.java 1.32 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.im;
  8. import java.awt.Component;
  9. import java.util.Locale;
  10. import java.awt.AWTEvent;
  11. import java.lang.Character.Subset;
  12. import sun.awt.im.InputMethodContext;
  13. /**
  14. * Provides methods to control text input facilities such as input
  15. * methods and keyboard layouts.
  16. * Two methods handle both input methods and keyboard layouts: selectInputMethod
  17. * lets a client component select an input method or keyboard layout by locale,
  18. * getLocale lets a client component obtain the locale of the current input method
  19. * or keyboard layout.
  20. * The other methods more specifically support interaction with input methods:
  21. * They let client components control the behavior of input methods, and
  22. * dispatch events from the client component to the input method.
  23. *
  24. * <p>
  25. * By default, one InputContext instance is created per Window instance,
  26. * and this input context is shared by all components within the window's
  27. * container hierarchy. However, this means that only one text input
  28. * operation is possible at any one time within a window, and that the
  29. * text needs to be committed when moving the focus from one text component
  30. * to another. If this is not desired, text components can create their
  31. * own input context instances.
  32. *
  33. * <p>
  34. * The Java 2 platform supports input methods that have been developed in the Java
  35. * programming language, using the interfaces in the {@link java.awt.im.spi} package,
  36. * and installed into a Java 2 runtime environment as extensions. Implementations
  37. * may also support using the native input methods of the platforms they run on;
  38. * however, not all platforms and locales provide input methods. Keyboard layouts
  39. * are provided by the host platform.
  40. *
  41. * <p>
  42. * Input methods are <em>unavailable</em> if (a) no input method written
  43. * in the Java programming language has been installed and (b) the Java 2 implementation
  44. * or the underlying platform does not support native input methods. In this case,
  45. * input contexts can still be created and used; their behavior is specified with
  46. * the individual methods below.
  47. *
  48. * @see java.awt.Component#getInputContext
  49. * @see java.awt.Component#enableInputMethods
  50. * @version 1.32, 01/23/03
  51. * @author JavaSoft Asia/Pacific
  52. * @since 1.2
  53. */
  54. public class InputContext {
  55. /**
  56. * Constructs an InputContext.
  57. * This method is protected so clients cannot instantiate
  58. * InputContext directly. Input contexts are obtained by
  59. * calling {@link #getInstance}.
  60. */
  61. protected InputContext() {
  62. // real implementation is in sun.awt.im.InputContext
  63. }
  64. /**
  65. * Returns a new InputContext instance.
  66. */
  67. public static InputContext getInstance() {
  68. return new sun.awt.im.InputMethodContext();
  69. }
  70. /**
  71. * Attempts to select an input method or keyboard layout that
  72. * supports the given locale, and returns a value indicating whether such
  73. * an input method or keyboard layout has been successfully selected. The
  74. * following steps are taken until an input method has been selected:
  75. *
  76. * <p>
  77. * <ul>
  78. * <li>
  79. * If the currently selected input method or keyboard layout supports the
  80. * requested locale, it remains selected.</li>
  81. *
  82. * <li>
  83. * If there is no input method or keyboard layout available that supports
  84. * the requested locale, the current input method or keyboard layout remains
  85. * selected.</li>
  86. *
  87. * <li>
  88. * If the user has previously selected an input method or keyboard layout
  89. * for the requested locale from the user interface, then the most recently
  90. * selected such input method or keyboard layout is reselected.</li>
  91. *
  92. * <li>
  93. * Otherwise, an input method or keyboard layout that supports the requested
  94. * locale is selected in an implementation dependent way.</li>
  95. *
  96. * <p>
  97. * </ul>
  98. * Before switching away from an input method, any currently uncommitted text
  99. * is committed. If no input method or keyboard layout supporting the requested
  100. * locale is available, then false is returned.
  101. *
  102. * <p>
  103. * Not all host operating systems provide API to determine the locale of
  104. * the currently selected native input method or keyboard layout, and to
  105. * select a native input method or keyboard layout by locale.
  106. * For host operating systems that don't provide such API,
  107. * <code>selectInputMethod</code> assumes that native input methods or
  108. * keyboard layouts provided by the host operating system support only the
  109. * system's default locale.
  110. *
  111. * <p>
  112. * A text editing component may call this method, for example, when
  113. * the user changes the insertion point, so that the user can
  114. * immediately continue typing in the language of the surrounding text.
  115. *
  116. * @param locale The desired new locale.
  117. * @return true if the input method or keyboard layout that's active after
  118. * this call supports the desired locale.
  119. * @exception NullPointerException if <code>locale</code> is null
  120. */
  121. public boolean selectInputMethod(Locale locale) {
  122. // real implementation is in sun.awt.im.InputContext
  123. return false;
  124. }
  125. /**
  126. * Returns the current locale of the current input method or keyboard
  127. * layout.
  128. * Returns null if the input context does not have a current input method
  129. * or keyboard layout or if the current input method's
  130. * {@link java.awt.im.spi.InputMethod#getLocale()} method returns null.
  131. *
  132. * <p>
  133. * Not all host operating systems provide API to determine the locale of
  134. * the currently selected native input method or keyboard layout.
  135. * For host operating systems that don't provide such API,
  136. * <code>getLocale</code> assumes that the current locale of all native
  137. * input methods or keyboard layouts provided by the host operating system
  138. * is the system's default locale.
  139. *
  140. * @return the current locale of the current input method or keyboard layout
  141. * @since 1.3
  142. */
  143. public Locale getLocale() {
  144. // real implementation is in sun.awt.im.InputContext
  145. return null;
  146. }
  147. /**
  148. * Sets the subsets of the Unicode character set that input methods of this input
  149. * context should be allowed to input. Null may be passed in to
  150. * indicate that all characters are allowed. The initial value
  151. * is null. The setting applies to the current input method as well
  152. * as input methods selected after this call is made. However,
  153. * applications cannot rely on this call having the desired effect,
  154. * since this setting cannot be passed on to all host input methods -
  155. * applications still need to apply their own character validation.
  156. * If no input methods are available, then this method has no effect.
  157. *
  158. * @param subsets The subsets of the Unicode character set from which characters may be input
  159. */
  160. public void setCharacterSubsets(Subset[] subsets) {
  161. // real implementation is in sun.awt.im.InputContext
  162. }
  163. /**
  164. * Enables or disables the current input method for composition,
  165. * depending on the value of the parameter <code>enable</code>.
  166. * <p>
  167. * An input method that is enabled for composition interprets incoming
  168. * events for both composition and control purposes, while a
  169. * disabled input method does not interpret events for composition.
  170. * Note however that events are passed on to the input method regardless
  171. * whether it is enabled or not, and that an input method that is disabled
  172. * for composition may still interpret events for control purposes,
  173. * including to enable or disable itself for composition.
  174. *
  175. * @param enable whether to enable the current input method for composition
  176. * @throws UnsupportedOperationException if there is no current input
  177. * method available or the current input method does not support
  178. * the enabling/disabling operation
  179. * @see #isCompositionEnabled
  180. * @since 1.3
  181. */
  182. public void setCompositionEnabled(boolean enable) {
  183. // real implementation is in sun.awt.im.InputContext
  184. }
  185. /**
  186. * Determines whether the current input method is enabled for composition.
  187. * An input method that is enabled for composition interprets incoming
  188. * events for both composition and control purposes, while a
  189. * disabled input method does not interpret events for composition.
  190. *
  191. * @return <code>true</code> if the current input method is enabled for
  192. * composition; <code>false</code> otherwise
  193. * @throws UnsupportedOperationException if there is no current input
  194. * method available or the current input method does not support
  195. * checking whether it is enabled for composition
  196. * @see #setCompositionEnabled
  197. * @since 1.3
  198. */
  199. public boolean isCompositionEnabled() {
  200. // real implementation is in sun.awt.im.InputContext
  201. return false;
  202. }
  203. /**
  204. * Asks the current input method to reconvert text from the
  205. * current client component. The input method obtains the text to
  206. * be reconverted from the client component using the
  207. * {@link InputMethodRequests#getSelectedText InputMethodRequests.getSelectedText}
  208. * method. The other <code>InputMethodRequests</code> methods
  209. * must be prepared to deal with further information requests by
  210. * the input method. The composed and/or committed text will be
  211. * sent to the client component as a sequence of
  212. * <code>InputMethodEvent</code>s. If the input method cannot
  213. * reconvert the given text, the text is returned as committed
  214. * text in an <code>InputMethodEvent</code>.
  215. *
  216. * @throws UnsupportedOperationException if there is no current input
  217. * method available or the current input method does not support
  218. * the reconversion operation.
  219. *
  220. * @since 1.3
  221. */
  222. public void reconvert() {
  223. // real implementation is in sun.awt.im.InputContext
  224. }
  225. /**
  226. * Dispatches an event to the active input method. Called by AWT.
  227. * If no input method is available, then the event will never be consumed.
  228. *
  229. * @param event The event
  230. * @exception NullPointerException if <code>event</code> is null
  231. */
  232. public void dispatchEvent(AWTEvent event) {
  233. // real implementation is in sun.awt.im.InputContext
  234. }
  235. /**
  236. * Notifies the input context that a client component has been
  237. * removed from its containment hierarchy, or that input method
  238. * support has been disabled for the component. This method is
  239. * usually called from the client component's
  240. * {@link java.awt.Component#removeNotify() Component.removeNotify}
  241. * method. Potentially pending input from input methods
  242. * for this component is discarded.
  243. * If no input methods are available, then this method has no effect.
  244. *
  245. * @param client Client component
  246. * @exception NullPointerException if <code>client</code> is null
  247. */
  248. public void removeNotify(Component client) {
  249. // real implementation is in sun.awt.im.InputContext
  250. }
  251. /**
  252. * Ends any input composition that may currently be going on in this
  253. * context. Depending on the platform and possibly user preferences,
  254. * this may commit or delete uncommitted text. Any changes to the text
  255. * are communicated to the active component using an input method event.
  256. * If no input methods are available, then this method has no effect.
  257. *
  258. * <p>
  259. * A text editing component may call this in a variety of situations,
  260. * for example, when the user moves the insertion point within the text
  261. * (but outside the composed text), or when the component's text is
  262. * saved to a file or copied to the clipboard.
  263. *
  264. */
  265. public void endComposition() {
  266. // real implementation is in sun.awt.im.InputContext
  267. }
  268. /**
  269. * Disposes of the input context and release the resources used by it.
  270. * Called by AWT for the default input context of each Window.
  271. * If no input methods are available, then this method
  272. * has no effect.
  273. */
  274. public void dispose() {
  275. // real implementation is in sun.awt.im.InputContext
  276. }
  277. /**
  278. * Returns a control object from the current input method, or null. A
  279. * control object provides methods that control the behavior of the
  280. * input method or obtain information from the input method. The type
  281. * of the object is an input method specific class. Clients have to
  282. * compare the result against known input method control object
  283. * classes and cast to the appropriate class to invoke the methods
  284. * provided.
  285. * <p>
  286. * If no input methods are available or the current input method does
  287. * not provide an input method control object, then null is returned.
  288. *
  289. * @return A control object from the current input method, or null.
  290. */
  291. public Object getInputMethodControlObject() {
  292. // real implementation is in sun.awt.im.InputContext
  293. return null;
  294. }
  295. }