1. /*
  2. * @(#)InputContext.java 1.34 03/12/19
  3. *
  4. * Copyright 2004 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.34, 12/19/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. * <p>
  175. * For input methods provided by host operating systems, it is not always possible to
  176. * determine whether this operation is supported. For example, an input method may enable
  177. * composition only for some locales, and do nothing for other locales. For such input
  178. * methods, it is possible that this method does not throw
  179. * {@link java.lang.UnsupportedOperationException UnsupportedOperationException},
  180. * but also does not affect whether composition is enabled.
  181. *
  182. * @param enable whether to enable the current input method for composition
  183. * @throws UnsupportedOperationException if there is no current input
  184. * method available or the current input method does not support
  185. * the enabling/disabling operation
  186. * @see #isCompositionEnabled
  187. * @since 1.3
  188. */
  189. public void setCompositionEnabled(boolean enable) {
  190. // real implementation is in sun.awt.im.InputContext
  191. }
  192. /**
  193. * Determines whether the current input method is enabled for composition.
  194. * An input method that is enabled for composition interprets incoming
  195. * events for both composition and control purposes, while a
  196. * disabled input method does not interpret events for composition.
  197. *
  198. * @return <code>true</code> if the current input method is enabled for
  199. * composition; <code>false</code> otherwise
  200. * @throws UnsupportedOperationException if there is no current input
  201. * method available or the current input method does not support
  202. * checking whether it is enabled for composition
  203. * @see #setCompositionEnabled
  204. * @since 1.3
  205. */
  206. public boolean isCompositionEnabled() {
  207. // real implementation is in sun.awt.im.InputContext
  208. return false;
  209. }
  210. /**
  211. * Asks the current input method to reconvert text from the
  212. * current client component. The input method obtains the text to
  213. * be reconverted from the client component using the
  214. * {@link InputMethodRequests#getSelectedText InputMethodRequests.getSelectedText}
  215. * method. The other <code>InputMethodRequests</code> methods
  216. * must be prepared to deal with further information requests by
  217. * the input method. The composed and/or committed text will be
  218. * sent to the client component as a sequence of
  219. * <code>InputMethodEvent</code>s. If the input method cannot
  220. * reconvert the given text, the text is returned as committed
  221. * text in an <code>InputMethodEvent</code>.
  222. *
  223. * @throws UnsupportedOperationException if there is no current input
  224. * method available or the current input method does not support
  225. * the reconversion operation.
  226. *
  227. * @since 1.3
  228. */
  229. public void reconvert() {
  230. // real implementation is in sun.awt.im.InputContext
  231. }
  232. /**
  233. * Dispatches an event to the active input method. Called by AWT.
  234. * If no input method is available, then the event will never be consumed.
  235. *
  236. * @param event The event
  237. * @exception NullPointerException if <code>event</code> is null
  238. */
  239. public void dispatchEvent(AWTEvent event) {
  240. // real implementation is in sun.awt.im.InputContext
  241. }
  242. /**
  243. * Notifies the input context that a client component has been
  244. * removed from its containment hierarchy, or that input method
  245. * support has been disabled for the component. This method is
  246. * usually called from the client component's
  247. * {@link java.awt.Component#removeNotify() Component.removeNotify}
  248. * method. Potentially pending input from input methods
  249. * for this component is discarded.
  250. * If no input methods are available, then this method has no effect.
  251. *
  252. * @param client Client component
  253. * @exception NullPointerException if <code>client</code> is null
  254. */
  255. public void removeNotify(Component client) {
  256. // real implementation is in sun.awt.im.InputContext
  257. }
  258. /**
  259. * Ends any input composition that may currently be going on in this
  260. * context. Depending on the platform and possibly user preferences,
  261. * this may commit or delete uncommitted text. Any changes to the text
  262. * are communicated to the active component using an input method event.
  263. * If no input methods are available, then this method has no effect.
  264. *
  265. * <p>
  266. * A text editing component may call this in a variety of situations,
  267. * for example, when the user moves the insertion point within the text
  268. * (but outside the composed text), or when the component's text is
  269. * saved to a file or copied to the clipboard.
  270. *
  271. */
  272. public void endComposition() {
  273. // real implementation is in sun.awt.im.InputContext
  274. }
  275. /**
  276. * Disposes of the input context and release the resources used by it.
  277. * Called by AWT for the default input context of each Window.
  278. * If no input methods are available, then this method
  279. * has no effect.
  280. */
  281. public void dispose() {
  282. // real implementation is in sun.awt.im.InputContext
  283. }
  284. /**
  285. * Returns a control object from the current input method, or null. A
  286. * control object provides methods that control the behavior of the
  287. * input method or obtain information from the input method. The type
  288. * of the object is an input method specific class. Clients have to
  289. * compare the result against known input method control object
  290. * classes and cast to the appropriate class to invoke the methods
  291. * provided.
  292. * <p>
  293. * If no input methods are available or the current input method does
  294. * not provide an input method control object, then null is returned.
  295. *
  296. * @return A control object from the current input method, or null.
  297. */
  298. public Object getInputMethodControlObject() {
  299. // real implementation is in sun.awt.im.InputContext
  300. return null;
  301. }
  302. }