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