001: /*
002: * @(#)InputContext.java 1.15 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.awt.im;
029:
030: import java.awt.Component;
031: import java.util.Locale;
032: import java.awt.AWTEvent;
033: import java.awt.Toolkit;
034: import java.awt.event.FocusEvent;
035: import java.awt.event.ComponentEvent;
036:
037: //import sun.awt.SunToolkit;
038:
039: /**
040: * An InputContext object manages the communication between text editing
041: * components and input methods. It dispatches events between them, and
042: * forwards requests for information from the input method to the text
043: * editing component.
044: *
045: * <p>
046: * By default, one InputContext instance is created per Window instance,
047: * and this input context is shared by all components within the window's
048: * container hierarchy. However, this means that only one text input
049: * operation is possible at any one time within a window, and that the
050: * text needs to be committed when moving the focus from one text component
051: * to another. If this is not desired, text components can create their
052: * own input context instances.
053: *
054: * @see java.awt.Component#getInputContext
055: * @version 1.10, 08/19/02
056: * @author Sun Microsystems, Inc.
057: */
058:
059: public class InputContext extends java.awt.im.InputContext {
060: private InputMethod inputMethod;
061: private boolean inputMethodCreationFailed;
062: private Component currentClientComponent;
063:
064: /**
065: * Constructs an InputContext.
066: */
067: protected InputContext() {
068: }
069:
070: /**
071: * Dispatches an event to the active input method. Called by AWT.
072: *
073: * @param event The event
074: */
075: public synchronized void dispatchEvent(AWTEvent event) {
076: InputMethod inputMethod = getInputMethod();
077: int id = event.getID();
078: switch (id) {
079: case ComponentEvent.COMPONENT_HIDDEN:
080: dispose();
081: currentClientComponent = null;
082: break;
083:
084: case FocusEvent.FOCUS_GAINED:
085: activate((Component) event.getSource());
086: if (inputMethod != null)
087: inputMethod.dispatchEvent(event);
088: break;
089:
090: case FocusEvent.FOCUS_LOST:
091: if (inputMethod != null)
092: inputMethod.dispatchEvent(event);
093: deactivate((Component) event.getSource());
094: break;
095:
096: default:
097: if (inputMethod != null)
098: inputMethod.dispatchEvent(event);
099: }
100: }
101:
102: /**
103: * Activates input context for the given component. If the input
104: * context previously was active for a different component and there
105: * is uncommitted text for that component, the text is committed.
106: * Called by AWT when the keyboard focus moves to the component.
107: *
108: * @param client The client component into which text is entered.
109: */
110: synchronized void activate(Component client) {
111: currentClientComponent = client;
112: if (inputMethod != null) {
113: if (inputMethod instanceof InputMethodAdapter) {
114: ((InputMethodAdapter) inputMethod)
115: .setClientComponent(client);
116: }
117: inputMethod.activate();
118: }
119: }
120:
121: /**
122: * Deactivates input context for the given component. Called by AWT
123: * when the keyboard focus moves away from the component.
124: *
125: * @param target The component into which text was entered.
126: */
127: synchronized void deactivate(Component client) {
128: if (inputMethod != null) {
129: inputMethod.deactivate();
130: }
131: currentClientComponent = null;
132: }
133:
134: /**
135: * Returns the client component.
136: */
137: Component getClientComponent() {
138: Component client = currentClientComponent;
139: return client;
140: }
141:
142: /**
143: * Disposes of the input context and release the resources used by it.
144: * Called by AWT.
145: */
146: public void dispose() {
147: if (inputMethod != null) {
148: inputMethod.dispose();
149: inputMethod = null;
150: }
151: }
152:
153: InputMethod getInputMethod() {
154: if (inputMethod != null) {
155: return inputMethod;
156: }
157: if (inputMethodCreationFailed) {
158: return null;
159: }
160: inputMethodCreationFailed = true;
161: /***
162: ** try {
163: ** Toolkit toolkit = Toolkit.getDefaultToolkit();
164: ** inputMethod = ((SunToolkit) toolkit).getInputMethodAdapter();
165: ** } catch (Exception e) {
166: ** // there are a number of bad things that can happen while creating
167: ** // the input method. In any case, we just continue without an
168: ** // input method.
169: ** inputMethodCreationFailed = true;
170: ** }
171: ** if (inputMethod != null) {
172: ** inputMethod.setInputContext((InputMethodContext)this);
173: ** }
174: ***/
175: return inputMethod;
176: }
177:
178: public boolean selectInputMethod(Locale locale) {
179: if (locale == null)
180: throw new NullPointerException("Locale is null");
181: return false;
182: }
183:
184: boolean compositionEnabled = false;
185:
186: public void setCompositionEnabled(boolean enable) {
187: compositionEnabled = enable;
188: }
189:
190: public boolean isCompositionEnabled() {
191: return compositionEnabled;
192: }
193:
194: public void removeNotify(Component client) {
195: if (client == null)
196: throw new NullPointerException("Component is null");
197: }
198:
199: }
|