home *** CD-ROM | disk | FTP | other *** search
- package sun.awt.im;
-
- import java.awt.AWTEvent;
- import java.awt.Component;
- import java.awt.Frame;
- import java.awt.Rectangle;
- import java.awt.Window;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.InputEvent;
- import java.awt.event.InputMethodEvent;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.awt.im.spi.InputMethod;
- import java.util.EventObject;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Locale;
-
- public class InputContext extends java.awt.im.InputContext implements ComponentListener, WindowListener {
- private InputMethodLocator inputMethodLocator;
- private InputMethod inputMethod;
- private boolean inputMethodCreationFailed;
- private HashMap usedInputMethods;
- private Component currentClientComponent;
- private Component awtFocussedComponent;
- private boolean isInputMethodActive;
- private Character.Subset[] characterSubsets = null;
- private int suppressDeactivateCount;
- private static InputContext inputMethodWindowContext;
- private static InputMethod previousInputMethod = null;
- private boolean clientWindowNotificationEnabled = false;
- private Window clientWindowListened;
- private Rectangle clientWindowLocation = null;
- private HashMap perInputMethodState;
-
- protected InputContext() {
- this.selectInputMethod(Locale.getDefault());
- }
-
- public synchronized boolean selectInputMethod(Locale var1) {
- if (var1 == null) {
- throw new NullPointerException();
- } else {
- if (this.inputMethod != null) {
- if (this.inputMethod.setLocale(var1)) {
- return true;
- }
- } else if (this.inputMethodLocator != null && this.inputMethodLocator.isLocaleAvailable(var1)) {
- this.inputMethodLocator = this.inputMethodLocator.deriveLocator(var1);
- return true;
- }
-
- InputMethodLocator var2 = InputMethodManager.getInstance().findInputMethod(var1);
- if (var2 != null) {
- this.changeInputMethod(var2);
- return true;
- } else {
- if (this.inputMethod == null && this.inputMethodLocator != null) {
- this.inputMethod = this.getInputMethod();
- if (this.inputMethod != null) {
- return this.inputMethod.setLocale(var1);
- }
- }
-
- return false;
- }
- }
- }
-
- public Locale getLocale() {
- if (this.inputMethod != null) {
- return this.inputMethod.getLocale();
- } else {
- return this.inputMethodLocator != null ? this.inputMethodLocator.getLocale() : null;
- }
- }
-
- public void setCharacterSubsets(Character.Subset[] var1) {
- if (var1 == null) {
- this.characterSubsets = null;
- } else {
- this.characterSubsets = new Character.Subset[var1.length];
- System.arraycopy(var1, 0, this.characterSubsets, 0, this.characterSubsets.length);
- }
-
- if (this.inputMethod != null) {
- this.inputMethod.setCharacterSubsets(var1);
- }
-
- }
-
- public synchronized void reconvert() {
- InputMethod var1 = this.getInputMethod();
- if (var1 == null) {
- throw new UnsupportedOperationException();
- } else {
- var1.reconvert();
- }
- }
-
- public void dispatchEvent(AWTEvent var1) {
- if (!(var1 instanceof InputMethodEvent)) {
- InputMethod var2 = this.getInputMethod();
- int var3 = var1.getID();
- switch (var3) {
- case 1004:
- this.focusGained((Component)((EventObject)var1).getSource());
- break;
- case 1005:
- this.focusLost((Component)((EventObject)var1).getSource(), ((FocusEvent)var1).isTemporary());
- break;
- default:
- if (var2 != null && var1 instanceof InputEvent) {
- var2.dispatchEvent(var1);
- }
- }
-
- }
- }
-
- private void focusGained(Component var1) {
- Object var2 = var1.getTreeLock();
- synchronized(var2) {
- synchronized(this) {
- if (!(var1 instanceof CompositionArea) && !(getComponentWindow(var1) instanceof InputMethodWindow)) {
- if (this.inputMethod != null && this.currentClientComponent != null && this.currentClientComponent != var1) {
- if (!this.isInputMethodActive) {
- this.activateInputMethod(false);
- }
-
- this.endComposition();
- this.deactivateInputMethod(false);
- }
-
- this.currentClientComponent = var1;
- }
-
- this.awtFocussedComponent = var1;
- if (this.inputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)this.inputMethod).setAWTFocussedComponent(var1);
- }
-
- if (!this.isInputMethodActive) {
- this.activateInputMethod(true);
- }
- }
-
- }
- }
-
- private void activateInputMethod(boolean var1) {
- if (inputMethodWindowContext != null && inputMethodWindowContext != this && inputMethodWindowContext.inputMethodLocator != null && !inputMethodWindowContext.inputMethodLocator.sameInputMethod(this.inputMethodLocator) && inputMethodWindowContext.inputMethod != null) {
- inputMethodWindowContext.inputMethod.hideWindows();
- }
-
- inputMethodWindowContext = this;
- if (this.inputMethod != null) {
- if (previousInputMethod != this.inputMethod && previousInputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)previousInputMethod).stopListening();
- }
-
- previousInputMethod = null;
- if (this.inputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)this.inputMethod).setClientComponent(this.currentClientComponent);
- }
-
- this.inputMethod.activate();
- this.isInputMethodActive = true;
- if (this.perInputMethodState != null) {
- Boolean var2 = (Boolean)this.perInputMethodState.remove(this.inputMethod);
- if (var2 != null) {
- this.clientWindowNotificationEnabled = var2;
- }
- }
-
- if (this.clientWindowNotificationEnabled) {
- if (!this.addedClientWindowListeners()) {
- this.addClientWindowListeners();
- }
-
- this.notifyClientWindowChange(this.clientWindowListened);
- } else if (this.addedClientWindowListeners()) {
- this.removeClientWindowListeners();
- }
- }
-
- InputMethodManager.getInstance().setInputContext(this);
- ((InputMethodContext)this).grabCompositionArea(var1);
- }
-
- static Window getComponentWindow(Component var0) {
- while(var0 != null) {
- if (var0 instanceof Window) {
- return (Window)var0;
- }
-
- var0 = ((Component)var0).getParent();
- }
-
- return null;
- }
-
- private void focusLost(Component var1, boolean var2) {
- Object var3 = var1.getTreeLock();
- synchronized(var3) {
- synchronized(this) {
- boolean var5 = this.suppressDeactivateCount > 0;
- if (var5) {
- --this.suppressDeactivateCount;
- }
-
- if (this.isInputMethodActive && !var5) {
- this.deactivateInputMethod(var2);
- }
-
- this.awtFocussedComponent = null;
- if (this.inputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)this.inputMethod).setAWTFocussedComponent((Component)null);
- }
- }
-
- }
- }
-
- private void deactivateInputMethod(boolean var1) {
- InputMethodManager.getInstance().setInputContext((InputContext)null);
- if (this.inputMethod != null) {
- this.isInputMethodActive = false;
- this.inputMethod.deactivate(var1);
- previousInputMethod = this.inputMethod;
- }
-
- }
-
- synchronized void changeInputMethod(InputMethodLocator var1) {
- if (this.inputMethodLocator == null) {
- this.inputMethodLocator = var1;
- this.inputMethodCreationFailed = false;
- } else if (this.inputMethodLocator.sameInputMethod(var1)) {
- Locale var9 = var1.getLocale();
- if (var9 != null && this.inputMethodLocator.getLocale() != var9) {
- if (this.inputMethod != null) {
- this.inputMethod.setLocale(var9);
- }
-
- this.inputMethodLocator = var1;
- }
-
- } else {
- Locale var2 = this.inputMethodLocator.getLocale();
- boolean var3 = this.isInputMethodActive;
- boolean var4 = false;
- boolean var5 = false;
- if (this.inputMethod != null) {
- try {
- var5 = this.inputMethod.isCompositionEnabled();
- var4 = true;
- } catch (UnsupportedOperationException var8) {
- }
-
- if (this.currentClientComponent != null) {
- if (!this.isInputMethodActive) {
- this.activateInputMethod(false);
- }
-
- this.endComposition();
- this.deactivateInputMethod(false);
- if (this.inputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)this.inputMethod).setClientComponent((Component)null);
- }
- }
-
- var2 = this.inputMethod.getLocale();
- if (this.usedInputMethods == null) {
- this.usedInputMethods = new HashMap(5);
- }
-
- if (this.perInputMethodState == null) {
- this.perInputMethodState = new HashMap(5);
- }
-
- this.usedInputMethods.put(this.inputMethodLocator.deriveLocator((Locale)null), this.inputMethod);
- this.perInputMethodState.put(this.inputMethod, new Boolean(this.clientWindowNotificationEnabled));
- this.enableClientWindowNotification(this.inputMethod, false);
- if (this == inputMethodWindowContext) {
- this.inputMethod.hideWindows();
- inputMethodWindowContext = null;
- }
-
- this.inputMethodLocator = null;
- this.inputMethod = null;
- this.inputMethodCreationFailed = false;
- }
-
- if (var1.getLocale() == null && var2 != null && var1.isLocaleAvailable(var2)) {
- var1 = var1.deriveLocator(var2);
- }
-
- this.inputMethodLocator = var1;
- this.inputMethodCreationFailed = false;
- if (var3) {
- this.inputMethod = this.getInputMethodInstance();
- if (this.inputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)this.inputMethod).setAWTFocussedComponent(this.awtFocussedComponent);
- }
-
- this.activateInputMethod(true);
- }
-
- if (var4) {
- this.inputMethod = this.getInputMethod();
- if (this.inputMethod != null) {
- try {
- this.inputMethod.setCompositionEnabled(var5);
- } catch (UnsupportedOperationException var7) {
- }
- }
- }
-
- }
- }
-
- Component getClientComponent() {
- return this.currentClientComponent;
- }
-
- public synchronized void removeNotify(Component var1) {
- if (var1 == null) {
- throw new NullPointerException();
- } else if (this.inputMethod == null) {
- if (var1 == this.currentClientComponent) {
- this.currentClientComponent = null;
- }
-
- } else {
- if (var1 == this.awtFocussedComponent) {
- this.focusLost(var1, false);
- }
-
- if (var1 == this.currentClientComponent) {
- if (this.isInputMethodActive) {
- this.deactivateInputMethod(false);
- }
-
- this.inputMethod.removeNotify();
- if (this.clientWindowNotificationEnabled && this.addedClientWindowListeners()) {
- this.removeClientWindowListeners();
- }
-
- this.currentClientComponent = null;
- if (this.inputMethod instanceof InputMethodAdapter) {
- ((InputMethodAdapter)this.inputMethod).setClientComponent((Component)null);
- }
-
- ((InputMethodContext)this).releaseCompositionArea();
- }
-
- }
- }
-
- public void dispose() {
- if (this.currentClientComponent != null) {
- throw new IllegalStateException("Can't dispose InputContext while it's active");
- } else {
- if (this.inputMethod != null) {
- if (this == inputMethodWindowContext) {
- this.inputMethod.hideWindows();
- inputMethodWindowContext = null;
- }
-
- if (this.inputMethod == previousInputMethod) {
- previousInputMethod = null;
- }
-
- if (this.clientWindowNotificationEnabled) {
- if (this.addedClientWindowListeners()) {
- this.removeClientWindowListeners();
- }
-
- this.clientWindowNotificationEnabled = false;
- }
-
- this.inputMethod.dispose();
- if (this.clientWindowNotificationEnabled) {
- this.enableClientWindowNotification(this.inputMethod, false);
- }
-
- this.inputMethod = null;
- }
-
- this.inputMethodLocator = null;
- if (this.usedInputMethods != null && !this.usedInputMethods.isEmpty()) {
- Iterator var1 = this.usedInputMethods.values().iterator();
- this.usedInputMethods = null;
-
- while(var1.hasNext()) {
- ((InputMethod)var1.next()).dispose();
- }
- }
-
- this.clientWindowNotificationEnabled = false;
- this.clientWindowListened = null;
- this.perInputMethodState = null;
- }
- }
-
- public synchronized Object getInputMethodControlObject() {
- InputMethod var1 = this.getInputMethod();
- return var1 != null ? var1.getControlObject() : null;
- }
-
- public void setCompositionEnabled(boolean var1) {
- InputMethod var2 = this.getInputMethod();
- if (var2 == null) {
- throw new UnsupportedOperationException();
- } else {
- var2.setCompositionEnabled(var1);
- }
- }
-
- public boolean isCompositionEnabled() {
- InputMethod var1 = this.getInputMethod();
- if (var1 == null) {
- throw new UnsupportedOperationException();
- } else {
- return var1.isCompositionEnabled();
- }
- }
-
- private synchronized InputMethod getInputMethod() {
- if (this.inputMethod != null) {
- return this.inputMethod;
- } else if (this.inputMethodCreationFailed) {
- return null;
- } else {
- this.inputMethod = this.getInputMethodInstance();
- return this.inputMethod;
- }
- }
-
- private InputMethod getInputMethodInstance() {
- InputMethodLocator var1 = this.inputMethodLocator;
- if (var1 == null) {
- this.inputMethodCreationFailed = true;
- return null;
- } else {
- Locale var2 = var1.getLocale();
- InputMethod var3 = null;
- if (this.usedInputMethods != null) {
- var3 = (InputMethod)this.usedInputMethods.remove(var1.deriveLocator((Locale)null));
- if (var3 != null) {
- if (var2 != null) {
- var3.setLocale(var2);
- }
-
- var3.setCharacterSubsets(this.characterSubsets);
- Boolean var4 = (Boolean)this.perInputMethodState.remove(var3);
- if (var4 != null) {
- this.enableClientWindowNotification(var3, var4);
- }
-
- ((InputMethodContext)this).setInputMethodSupportsBelowTheSpot(!(var3 instanceof InputMethodAdapter) || ((InputMethodAdapter)var3).supportsBelowTheSpot());
- return var3;
- }
- }
-
- try {
- var3 = var1.getDescriptor().createInputMethod();
- if (var2 != null) {
- var3.setLocale(var2);
- }
-
- var3.setInputMethodContext((InputMethodContext)this);
- var3.setCharacterSubsets(this.characterSubsets);
- } catch (Exception var6) {
- ((Throwable)var6).printStackTrace();
- this.inputMethodCreationFailed = true;
- if (var3 != null) {
- var3 = null;
- }
- } catch (LinkageError var7) {
- ((Throwable)var7).printStackTrace();
- this.inputMethodCreationFailed = true;
- }
-
- ((InputMethodContext)this).setInputMethodSupportsBelowTheSpot(!(var3 instanceof InputMethodAdapter) || ((InputMethodAdapter)var3).supportsBelowTheSpot());
- return var3;
- }
- }
-
- InputMethodLocator getInputMethodLocator() {
- return this.inputMethod != null ? this.inputMethodLocator.deriveLocator(this.inputMethod.getLocale()) : this.inputMethodLocator;
- }
-
- public synchronized void suppressFocusChange() {
- ++this.suppressDeactivateCount;
- }
-
- public synchronized void endComposition() {
- if (this.inputMethod != null) {
- this.inputMethod.endComposition();
- }
-
- }
-
- synchronized void enableClientWindowNotification(InputMethod var1, boolean var2) {
- if (var1 != this.inputMethod) {
- if (this.perInputMethodState == null) {
- this.perInputMethodState = new HashMap(5);
- }
-
- this.perInputMethodState.put(var1, new Boolean(var2));
- } else {
- if (this.clientWindowNotificationEnabled != var2) {
- this.clientWindowLocation = null;
- this.clientWindowNotificationEnabled = var2;
- }
-
- if (this.clientWindowNotificationEnabled) {
- if (!this.addedClientWindowListeners()) {
- this.addClientWindowListeners();
- }
-
- if (this.clientWindowListened != null) {
- this.clientWindowLocation = null;
- this.notifyClientWindowChange(this.clientWindowListened);
- }
- } else if (this.addedClientWindowListeners()) {
- this.removeClientWindowListeners();
- }
-
- }
- }
-
- private synchronized void notifyClientWindowChange(Window var1) {
- if (this.inputMethod != null) {
- if (((Component)var1).isVisible() && (!(var1 instanceof Frame) || ((Frame)var1).getState() != 1)) {
- Rectangle var2 = ((Component)var1).getBounds();
- if (this.clientWindowLocation == null || !this.clientWindowLocation.equals(var2)) {
- this.clientWindowLocation = var2;
- this.inputMethod.notifyClientWindowChange(this.clientWindowLocation);
- }
-
- } else {
- this.clientWindowLocation = null;
- this.inputMethod.notifyClientWindowChange((Rectangle)null);
- }
- }
- }
-
- private synchronized void addClientWindowListeners() {
- Component var1 = this.getClientComponent();
- if (var1 != null) {
- Window var2 = getComponentWindow(var1);
- if (var2 != null) {
- ((Component)var2).addComponentListener(this);
- var2.addWindowListener(this);
- this.clientWindowListened = var2;
- }
- }
- }
-
- private synchronized void removeClientWindowListeners() {
- this.clientWindowListened.removeComponentListener(this);
- this.clientWindowListened.removeWindowListener(this);
- this.clientWindowListened = null;
- }
-
- private boolean addedClientWindowListeners() {
- return this.clientWindowListened != null;
- }
-
- public void componentResized(ComponentEvent var1) {
- this.notifyClientWindowChange((Window)var1.getComponent());
- }
-
- public void componentMoved(ComponentEvent var1) {
- this.notifyClientWindowChange((Window)var1.getComponent());
- }
-
- public void componentShown(ComponentEvent var1) {
- this.notifyClientWindowChange((Window)var1.getComponent());
- }
-
- public void componentHidden(ComponentEvent var1) {
- this.notifyClientWindowChange((Window)var1.getComponent());
- }
-
- public void windowOpened(WindowEvent var1) {
- }
-
- public void windowClosing(WindowEvent var1) {
- }
-
- public void windowClosed(WindowEvent var1) {
- }
-
- public void windowIconified(WindowEvent var1) {
- this.notifyClientWindowChange(var1.getWindow());
- }
-
- public void windowDeiconified(WindowEvent var1) {
- this.notifyClientWindowChange(var1.getWindow());
- }
-
- public void windowActivated(WindowEvent var1) {
- }
-
- public void windowDeactivated(WindowEvent var1) {
- }
- }
-