InputContext.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* InputContext.java -- provides the context for text input
  2. Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt.im;
  32. import java.awt.AWTEvent;
  33. import java.awt.AWTException;
  34. import java.awt.Component;
  35. import java.awt.im.spi.InputMethod;
  36. import java.awt.im.spi.InputMethodDescriptor;
  37. import java.io.BufferedReader;
  38. import java.io.InputStreamReader;
  39. import java.io.IOException;
  40. import java.net.URL;
  41. import java.net.URLConnection;
  42. import java.util.ArrayList;
  43. import java.util.Enumeration;
  44. import java.util.HashMap;
  45. import java.util.Locale;
  46. import gnu.java.util.EmptyEnumeration;
  47. /**
  48. * Provides a context for controlling input methods and keyboard layouts.
  49. * This class provides the communication layer between the client component,
  50. * and the various locale-dependent text entry input methods that can be used
  51. * for the client. By default, there is one instance per Window, shared among
  52. * all components, but this limits text entry to one component at a time.
  53. * Thus, text components can create their own instance to allow text entry
  54. * in multiple components at a time.
  55. *
  56. * <p>By using the interfaces of {@link java.awt.im.spi}, you can install
  57. * extensions which allow additional input methods. Some of these may use
  58. * platform native input methods, or keyboard layouts provided by the platform.
  59. * Input methods are unavailable if none have been installed and the platform
  60. * has no underlying native input methods. Extensions are installed as jar
  61. * files, usually accessed in the default extension location or specified by
  62. * the -extdir VM flag. The jar must contain a file named
  63. * "META_INF/services/java.awt.im.spi.InputMethodDescriptor" which lists,
  64. * one entry per line in UTF-8 encoding, each class in the jar that implements
  65. * java.awt.im.spi.InputMethodDescriptor.
  66. *
  67. * @author Eric Blake <ebb9@email.byu.edu>
  68. * @see Component#getInputContext()
  69. * @see Component#enableInputMethods(boolean)
  70. * @since 1.2
  71. * @status updated to 1.4, but unverified
  72. */
  73. public class InputContext
  74. {
  75. /**
  76. * The list of installed input method descriptors.
  77. */
  78. private static final ArrayList descriptors = new ArrayList();
  79. static
  80. {
  81. Enumeration enum;
  82. try
  83. {
  84. enum = ClassLoader.getSystemResources
  85. ("META_INF/services/java.awt.im.spi.InputMethodDescriptor");
  86. }
  87. catch (IOException ex)
  88. {
  89. // XXX Should we do something else?
  90. enum = EmptyEnumeration.getInstance();
  91. }
  92. while (enum.hasMoreElements())
  93. {
  94. URL url = (URL) enum.nextElement();
  95. BufferedReader in;
  96. String line;
  97. try
  98. {
  99. in = new BufferedReader
  100. (new InputStreamReader(url.openConnection().getInputStream(),
  101. "UTF-8"));
  102. line = in.readLine().trim();
  103. }
  104. catch (IOException ignored)
  105. {
  106. continue;
  107. }
  108. outer:
  109. while (line != null)
  110. {
  111. try
  112. {
  113. if (line.charAt(0) != '#')
  114. {
  115. Class c = Class.forName(line);
  116. descriptors.add((InputMethodDescriptor) c.newInstance());
  117. }
  118. line = in.readLine().trim();
  119. }
  120. catch (IOException e)
  121. {
  122. continue outer;
  123. }
  124. catch (Exception ignored)
  125. {
  126. }
  127. }
  128. }
  129. }
  130. /** The current input method; null if no input methods are installed. */
  131. private InputMethod im;
  132. /** Map of locales to the most recently selected input method. */
  133. private final HashMap recent = new HashMap();
  134. /** The list of acceptable character subsets. */
  135. private Character.Subset[] subsets;
  136. /**
  137. * Construct an InputContext. This is protected, so clients must use
  138. * {@link #getInstance()} instead.
  139. */
  140. protected InputContext()
  141. {
  142. }
  143. /**
  144. * Returns a new InputContext.
  145. *
  146. * @return a new instance, initialized to the default locale if available
  147. */
  148. public static InputContext getInstance()
  149. {
  150. InputContext ic = new InputContext();
  151. ic.selectInputMethod(Locale.getDefault());
  152. return ic;
  153. }
  154. /**
  155. * Attempts to select an input method or keyboard layout which supports the
  156. * given locale. This returns true if a locale is available and was selected.
  157. * The following steps are taken in choosing an input method:<ul>
  158. * <li>If the currently selected input method or keyboard layout supports
  159. * the requested locale, it remains selected.</li>
  160. * <li>If there is no input method or keyboard layout available that
  161. * supports the requested locale, the current input method or keyboard
  162. * layout remains selected.</li>
  163. * <li>If the user has previously selected an input method or keyboard
  164. * layout for the requested locale from the user interface, then the most
  165. * recently selected such input method or keyboard layout is reselected.</li>
  166. * <li>Otherwise, an input method or keyboard layout that supports the
  167. * requested locale is selected in an implementation dependent way. This
  168. * implementation chooses the first input method which supports the requested
  169. * locale based on the InputMethodDescriptors loaded from the extensions
  170. * installed on the CLASSPATH.</li>
  171. * </ul>
  172. *
  173. * <p>Before switching away from an input method, any currently uncommitted
  174. * text is committed. Not all host operating systems provide API to
  175. * determine the locale of the currently selected native input method or
  176. * keyboard layout, and to select a native input method or keyboard layout
  177. * by locale. For host operating systems that don't provide such API,
  178. * selectInputMethod assumes that native input methods or keyboard layouts
  179. * provided by the host operating system support only the system's default
  180. * locale.
  181. *
  182. * <p>An example of where this may be called is in a multi-language document,
  183. * when moving the insertion point between sections of different locale, so
  184. * that the user may use the input method appropriate to that section of the
  185. * document.
  186. *
  187. * @param locale the desired new locale
  188. * @return true if the new locale is active
  189. * @throws NullPointerException if locale is null
  190. */
  191. public boolean selectInputMethod(Locale locale)
  192. {
  193. if (im != null && im.setLocale(locale))
  194. {
  195. recent.put(locale, im);
  196. return true;
  197. }
  198. InputMethod next = (InputMethod) recent.get(locale);
  199. outer:
  200. if (next != null)
  201. for (int i = 0, limit = descriptors.size(); i < limit; i++)
  202. {
  203. InputMethodDescriptor d = (InputMethodDescriptor) descriptors.get(i);
  204. Locale[] list;
  205. try
  206. {
  207. list = d.getAvailableLocales();
  208. }
  209. catch (AWTException ignored)
  210. {
  211. continue;
  212. }
  213. for (int j = list.length; --j >= 0; )
  214. if (locale.equals(list[j]))
  215. {
  216. try
  217. {
  218. next = d.createInputMethod();
  219. recent.put(locale, next);
  220. }
  221. catch (Exception ignored)
  222. {
  223. continue;
  224. }
  225. }
  226. }
  227. if (next == null)
  228. return false;
  229. // XXX I'm not sure if this does all the necessary steps in the switch.
  230. if (im != null)
  231. {
  232. try
  233. {
  234. next.setCompositionEnabled(im.isCompositionEnabled());
  235. }
  236. catch (UnsupportedOperationException ignored)
  237. {
  238. }
  239. im.endComposition();
  240. im.deactivate(false);
  241. im.hideWindows();
  242. }
  243. im = next;
  244. im.setLocale(locale);
  245. im.setCharacterSubsets(subsets);
  246. return true;
  247. }
  248. /**
  249. * Returns the current locale of the current input method or keyboard
  250. * layout. Returns null if the input context does not have a current input
  251. * method or keyboard layout or if the current input method's
  252. * {@link InputMethod#getLocale()} method returns null. Not all host
  253. * operating systems provide API to determine the locale of the currently
  254. * selected native input method or keyboard layout. For host operating
  255. * systems that don't provide such API, getLocale assumes that the current
  256. * locale of all native input methods or keyboard layouts provided by the
  257. * host operating system is the system's default locale.
  258. *
  259. * @return the locale of the current input method, or null
  260. * @since 1.3
  261. */
  262. public Locale getLocale()
  263. {
  264. return im == null ? null : im.getLocale();
  265. }
  266. /**
  267. * Sets the subsets of Unicode characters allowed to be input by the current
  268. * input method, as well as subsequent input methods. The value of null
  269. * implies all characters are legal. Applications should not rely on this
  270. * behavior, since native host input methods may not allow restrictions.
  271. * If no current input method is available, this has no immediate effect.
  272. *
  273. * @param subsets the set of Unicode subsets to accept, or null
  274. */
  275. public void setCharacterSubsets(Character.Subset[] subsets)
  276. {
  277. this.subsets = subsets;
  278. if (im != null)
  279. im.setCharacterSubsets(subsets);
  280. }
  281. /**
  282. * Changes the enabled status of the current input method. An input method
  283. * that is enabled for composition interprets incoming events for both
  284. * composition and control purposes, while a disabled input method only
  285. * interprets control commands (including commands to enable itself).
  286. *
  287. * @param enable whether to enable the input method
  288. * @throws UnsupportedOperationException if there is no current input method,
  289. * or the input method does not support enabling
  290. * @see #isCompositionEnabled()
  291. * @since 1.3
  292. */
  293. public void setCompositionEnabled(boolean enable)
  294. {
  295. if (im == null)
  296. throw new UnsupportedOperationException();
  297. im.setCompositionEnabled(enable);
  298. }
  299. /**
  300. * Find out if the current input method is enabled.
  301. *
  302. * @return true if the current input method is enabled
  303. * @throws UnsupportedOperationException if there is no current input method,
  304. * or the input method does not support enabling
  305. * @see #setCompositionEnabled(boolean)
  306. * @since 1.3
  307. */
  308. public boolean isCompositionEnabled()
  309. {
  310. if (im == null)
  311. throw new UnsupportedOperationException();
  312. return im.isCompositionEnabled();
  313. }
  314. /**
  315. * Starts a reconversion operation in the current input method. The input
  316. * method gets theh text to reconvert from the client component, using
  317. * {@link InputMethodRequests#getSelectedText(Attribute[])}. Then the
  318. * composed and committed text produced by the operation is sent back to
  319. * the client using a sequence of InputMethodRequests.
  320. *
  321. * @throws UnsupportedOperationException if there is no current input method,
  322. * or the input method does not support reconversion
  323. * @since 1.3
  324. */
  325. public void reconvert()
  326. {
  327. if (im == null)
  328. throw new UnsupportedOperationException();
  329. im.reconvert();
  330. }
  331. /**
  332. * Dispatches an event to the current input method. This is called
  333. * automatically by AWT. If no input method is available, then the event
  334. * will never be consumed.
  335. *
  336. * @param event the event to dispatch
  337. * @throws NullPointerException if event is null
  338. */
  339. public void dispatchEvent(AWTEvent event)
  340. {
  341. if (im != null)
  342. im.dispatchEvent(event);
  343. }
  344. /**
  345. * Notifies the input context that a client component has been removed from
  346. * its containment hierarchy, or that input method support has been disabled
  347. * for the component. This method is usually called from the client
  348. * component's {@link Component#removeNotify()} method. Potentially pending
  349. * input from input methods for this component is discarded. If no input
  350. * methods are available, then this method has no effect.
  351. *
  352. * @param client the client component
  353. * @throws NullPointerException if client is null
  354. */
  355. public void removeNotify(Component client)
  356. {
  357. // XXX What to do with client information?
  358. if (im != null)
  359. {
  360. im.deactivate(false);
  361. im.removeNotify();
  362. }
  363. }
  364. /**
  365. * Ends any input composition that may currently be going on in this
  366. * context. Depending on the platform and possibly user preferences, this
  367. * may commit or delete uncommitted text. Any changes to the text are
  368. * communicated to the active component using an input method event. If no
  369. * input methods are available, then this method has no effect. This may
  370. * be called for a variety of reasons, such as when the user moves the
  371. * insertion point in the client text outside the range of the composed text,
  372. * or when text is saved to file.
  373. */
  374. public void endComposition()
  375. {
  376. if (im != null)
  377. im.endComposition();
  378. }
  379. /**
  380. * Disposes of the input context and release the resources used by it.
  381. * Called automatically by AWT for the default input context of each
  382. * Window. If no input methods are available, then this method has no
  383. * effect.
  384. */
  385. public void dispose()
  386. {
  387. if (im != null)
  388. {
  389. im.deactivate(false);
  390. im.dispose();
  391. }
  392. }
  393. /**
  394. * Returns a control object from the current input method, or null. A
  395. * control object provides implementation-dependent methods that control
  396. * the behavior of the input method or obtain information from the input
  397. * method. Clients have to compare the result against known input method
  398. * control object types. If no input methods are available or the current
  399. * input method does not provide an input method control object, then null
  400. * is returned.
  401. *
  402. * @return the control object, or null
  403. */
  404. public Object getInputMethodControlObject()
  405. {
  406. return im == null ? null : im.getControlObject();
  407. }
  408. } // class InputContext