home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &… the Search for Life CD 3 / 0_CD-ROM.iso / install / jre1_3 / lib / rt.jar / sun / awt / im / InputContext.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  8.9 KB  |  614 lines

  1. package sun.awt.im;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.Component;
  5. import java.awt.Frame;
  6. import java.awt.Rectangle;
  7. import java.awt.Window;
  8. import java.awt.event.ComponentEvent;
  9. import java.awt.event.ComponentListener;
  10. import java.awt.event.FocusEvent;
  11. import java.awt.event.InputEvent;
  12. import java.awt.event.InputMethodEvent;
  13. import java.awt.event.WindowEvent;
  14. import java.awt.event.WindowListener;
  15. import java.awt.im.spi.InputMethod;
  16. import java.util.EventObject;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Locale;
  20.  
  21. public class InputContext extends java.awt.im.InputContext implements ComponentListener, WindowListener {
  22.    private InputMethodLocator inputMethodLocator;
  23.    private InputMethod inputMethod;
  24.    private boolean inputMethodCreationFailed;
  25.    private HashMap usedInputMethods;
  26.    private Component currentClientComponent;
  27.    private Component awtFocussedComponent;
  28.    private boolean isInputMethodActive;
  29.    private Character.Subset[] characterSubsets = null;
  30.    private int suppressDeactivateCount;
  31.    private static InputContext inputMethodWindowContext;
  32.    private static InputMethod previousInputMethod = null;
  33.    private boolean clientWindowNotificationEnabled = false;
  34.    private Window clientWindowListened;
  35.    private Rectangle clientWindowLocation = null;
  36.    private HashMap perInputMethodState;
  37.  
  38.    protected InputContext() {
  39.       this.selectInputMethod(Locale.getDefault());
  40.    }
  41.  
  42.    public synchronized boolean selectInputMethod(Locale var1) {
  43.       if (var1 == null) {
  44.          throw new NullPointerException();
  45.       } else {
  46.          if (this.inputMethod != null) {
  47.             if (this.inputMethod.setLocale(var1)) {
  48.                return true;
  49.             }
  50.          } else if (this.inputMethodLocator != null && this.inputMethodLocator.isLocaleAvailable(var1)) {
  51.             this.inputMethodLocator = this.inputMethodLocator.deriveLocator(var1);
  52.             return true;
  53.          }
  54.  
  55.          InputMethodLocator var2 = InputMethodManager.getInstance().findInputMethod(var1);
  56.          if (var2 != null) {
  57.             this.changeInputMethod(var2);
  58.             return true;
  59.          } else {
  60.             if (this.inputMethod == null && this.inputMethodLocator != null) {
  61.                this.inputMethod = this.getInputMethod();
  62.                if (this.inputMethod != null) {
  63.                   return this.inputMethod.setLocale(var1);
  64.                }
  65.             }
  66.  
  67.             return false;
  68.          }
  69.       }
  70.    }
  71.  
  72.    public Locale getLocale() {
  73.       if (this.inputMethod != null) {
  74.          return this.inputMethod.getLocale();
  75.       } else {
  76.          return this.inputMethodLocator != null ? this.inputMethodLocator.getLocale() : null;
  77.       }
  78.    }
  79.  
  80.    public void setCharacterSubsets(Character.Subset[] var1) {
  81.       if (var1 == null) {
  82.          this.characterSubsets = null;
  83.       } else {
  84.          this.characterSubsets = new Character.Subset[var1.length];
  85.          System.arraycopy(var1, 0, this.characterSubsets, 0, this.characterSubsets.length);
  86.       }
  87.  
  88.       if (this.inputMethod != null) {
  89.          this.inputMethod.setCharacterSubsets(var1);
  90.       }
  91.  
  92.    }
  93.  
  94.    public synchronized void reconvert() {
  95.       InputMethod var1 = this.getInputMethod();
  96.       if (var1 == null) {
  97.          throw new UnsupportedOperationException();
  98.       } else {
  99.          var1.reconvert();
  100.       }
  101.    }
  102.  
  103.    public void dispatchEvent(AWTEvent var1) {
  104.       if (!(var1 instanceof InputMethodEvent)) {
  105.          InputMethod var2 = this.getInputMethod();
  106.          int var3 = var1.getID();
  107.          switch (var3) {
  108.             case 1004:
  109.                this.focusGained((Component)((EventObject)var1).getSource());
  110.                break;
  111.             case 1005:
  112.                this.focusLost((Component)((EventObject)var1).getSource(), ((FocusEvent)var1).isTemporary());
  113.                break;
  114.             default:
  115.                if (var2 != null && var1 instanceof InputEvent) {
  116.                   var2.dispatchEvent(var1);
  117.                }
  118.          }
  119.  
  120.       }
  121.    }
  122.  
  123.    private void focusGained(Component var1) {
  124.       Object var2 = var1.getTreeLock();
  125.       synchronized(var2) {
  126.          synchronized(this) {
  127.             if (!(var1 instanceof CompositionArea) && !(getComponentWindow(var1) instanceof InputMethodWindow)) {
  128.                if (this.inputMethod != null && this.currentClientComponent != null && this.currentClientComponent != var1) {
  129.                   if (!this.isInputMethodActive) {
  130.                      this.activateInputMethod(false);
  131.                   }
  132.  
  133.                   this.endComposition();
  134.                   this.deactivateInputMethod(false);
  135.                }
  136.  
  137.                this.currentClientComponent = var1;
  138.             }
  139.  
  140.             this.awtFocussedComponent = var1;
  141.             if (this.inputMethod instanceof InputMethodAdapter) {
  142.                ((InputMethodAdapter)this.inputMethod).setAWTFocussedComponent(var1);
  143.             }
  144.  
  145.             if (!this.isInputMethodActive) {
  146.                this.activateInputMethod(true);
  147.             }
  148.          }
  149.  
  150.       }
  151.    }
  152.  
  153.    private void activateInputMethod(boolean var1) {
  154.       if (inputMethodWindowContext != null && inputMethodWindowContext != this && inputMethodWindowContext.inputMethodLocator != null && !inputMethodWindowContext.inputMethodLocator.sameInputMethod(this.inputMethodLocator) && inputMethodWindowContext.inputMethod != null) {
  155.          inputMethodWindowContext.inputMethod.hideWindows();
  156.       }
  157.  
  158.       inputMethodWindowContext = this;
  159.       if (this.inputMethod != null) {
  160.          if (previousInputMethod != this.inputMethod && previousInputMethod instanceof InputMethodAdapter) {
  161.             ((InputMethodAdapter)previousInputMethod).stopListening();
  162.          }
  163.  
  164.          previousInputMethod = null;
  165.          if (this.inputMethod instanceof InputMethodAdapter) {
  166.             ((InputMethodAdapter)this.inputMethod).setClientComponent(this.currentClientComponent);
  167.          }
  168.  
  169.          this.inputMethod.activate();
  170.          this.isInputMethodActive = true;
  171.          if (this.perInputMethodState != null) {
  172.             Boolean var2 = (Boolean)this.perInputMethodState.remove(this.inputMethod);
  173.             if (var2 != null) {
  174.                this.clientWindowNotificationEnabled = var2;
  175.             }
  176.          }
  177.  
  178.          if (this.clientWindowNotificationEnabled) {
  179.             if (!this.addedClientWindowListeners()) {
  180.                this.addClientWindowListeners();
  181.             }
  182.  
  183.             this.notifyClientWindowChange(this.clientWindowListened);
  184.          } else if (this.addedClientWindowListeners()) {
  185.             this.removeClientWindowListeners();
  186.          }
  187.       }
  188.  
  189.       InputMethodManager.getInstance().setInputContext(this);
  190.       ((InputMethodContext)this).grabCompositionArea(var1);
  191.    }
  192.  
  193.    static Window getComponentWindow(Component var0) {
  194.       while(var0 != null) {
  195.          if (var0 instanceof Window) {
  196.             return (Window)var0;
  197.          }
  198.  
  199.          var0 = ((Component)var0).getParent();
  200.       }
  201.  
  202.       return null;
  203.    }
  204.  
  205.    private void focusLost(Component var1, boolean var2) {
  206.       Object var3 = var1.getTreeLock();
  207.       synchronized(var3) {
  208.          synchronized(this) {
  209.             boolean var5 = this.suppressDeactivateCount > 0;
  210.             if (var5) {
  211.                --this.suppressDeactivateCount;
  212.             }
  213.  
  214.             if (this.isInputMethodActive && !var5) {
  215.                this.deactivateInputMethod(var2);
  216.             }
  217.  
  218.             this.awtFocussedComponent = null;
  219.             if (this.inputMethod instanceof InputMethodAdapter) {
  220.                ((InputMethodAdapter)this.inputMethod).setAWTFocussedComponent((Component)null);
  221.             }
  222.          }
  223.  
  224.       }
  225.    }
  226.  
  227.    private void deactivateInputMethod(boolean var1) {
  228.       InputMethodManager.getInstance().setInputContext((InputContext)null);
  229.       if (this.inputMethod != null) {
  230.          this.isInputMethodActive = false;
  231.          this.inputMethod.deactivate(var1);
  232.          previousInputMethod = this.inputMethod;
  233.       }
  234.  
  235.    }
  236.  
  237.    synchronized void changeInputMethod(InputMethodLocator var1) {
  238.       if (this.inputMethodLocator == null) {
  239.          this.inputMethodLocator = var1;
  240.          this.inputMethodCreationFailed = false;
  241.       } else if (this.inputMethodLocator.sameInputMethod(var1)) {
  242.          Locale var9 = var1.getLocale();
  243.          if (var9 != null && this.inputMethodLocator.getLocale() != var9) {
  244.             if (this.inputMethod != null) {
  245.                this.inputMethod.setLocale(var9);
  246.             }
  247.  
  248.             this.inputMethodLocator = var1;
  249.          }
  250.  
  251.       } else {
  252.          Locale var2 = this.inputMethodLocator.getLocale();
  253.          boolean var3 = this.isInputMethodActive;
  254.          boolean var4 = false;
  255.          boolean var5 = false;
  256.          if (this.inputMethod != null) {
  257.             try {
  258.                var5 = this.inputMethod.isCompositionEnabled();
  259.                var4 = true;
  260.             } catch (UnsupportedOperationException var8) {
  261.             }
  262.  
  263.             if (this.currentClientComponent != null) {
  264.                if (!this.isInputMethodActive) {
  265.                   this.activateInputMethod(false);
  266.                }
  267.  
  268.                this.endComposition();
  269.                this.deactivateInputMethod(false);
  270.                if (this.inputMethod instanceof InputMethodAdapter) {
  271.                   ((InputMethodAdapter)this.inputMethod).setClientComponent((Component)null);
  272.                }
  273.             }
  274.  
  275.             var2 = this.inputMethod.getLocale();
  276.             if (this.usedInputMethods == null) {
  277.                this.usedInputMethods = new HashMap(5);
  278.             }
  279.  
  280.             if (this.perInputMethodState == null) {
  281.                this.perInputMethodState = new HashMap(5);
  282.             }
  283.  
  284.             this.usedInputMethods.put(this.inputMethodLocator.deriveLocator((Locale)null), this.inputMethod);
  285.             this.perInputMethodState.put(this.inputMethod, new Boolean(this.clientWindowNotificationEnabled));
  286.             this.enableClientWindowNotification(this.inputMethod, false);
  287.             if (this == inputMethodWindowContext) {
  288.                this.inputMethod.hideWindows();
  289.                inputMethodWindowContext = null;
  290.             }
  291.  
  292.             this.inputMethodLocator = null;
  293.             this.inputMethod = null;
  294.             this.inputMethodCreationFailed = false;
  295.          }
  296.  
  297.          if (var1.getLocale() == null && var2 != null && var1.isLocaleAvailable(var2)) {
  298.             var1 = var1.deriveLocator(var2);
  299.          }
  300.  
  301.          this.inputMethodLocator = var1;
  302.          this.inputMethodCreationFailed = false;
  303.          if (var3) {
  304.             this.inputMethod = this.getInputMethodInstance();
  305.             if (this.inputMethod instanceof InputMethodAdapter) {
  306.                ((InputMethodAdapter)this.inputMethod).setAWTFocussedComponent(this.awtFocussedComponent);
  307.             }
  308.  
  309.             this.activateInputMethod(true);
  310.          }
  311.  
  312.          if (var4) {
  313.             this.inputMethod = this.getInputMethod();
  314.             if (this.inputMethod != null) {
  315.                try {
  316.                   this.inputMethod.setCompositionEnabled(var5);
  317.                } catch (UnsupportedOperationException var7) {
  318.                }
  319.             }
  320.          }
  321.  
  322.       }
  323.    }
  324.  
  325.    Component getClientComponent() {
  326.       return this.currentClientComponent;
  327.    }
  328.  
  329.    public synchronized void removeNotify(Component var1) {
  330.       if (var1 == null) {
  331.          throw new NullPointerException();
  332.       } else if (this.inputMethod == null) {
  333.          if (var1 == this.currentClientComponent) {
  334.             this.currentClientComponent = null;
  335.          }
  336.  
  337.       } else {
  338.          if (var1 == this.awtFocussedComponent) {
  339.             this.focusLost(var1, false);
  340.          }
  341.  
  342.          if (var1 == this.currentClientComponent) {
  343.             if (this.isInputMethodActive) {
  344.                this.deactivateInputMethod(false);
  345.             }
  346.  
  347.             this.inputMethod.removeNotify();
  348.             if (this.clientWindowNotificationEnabled && this.addedClientWindowListeners()) {
  349.                this.removeClientWindowListeners();
  350.             }
  351.  
  352.             this.currentClientComponent = null;
  353.             if (this.inputMethod instanceof InputMethodAdapter) {
  354.                ((InputMethodAdapter)this.inputMethod).setClientComponent((Component)null);
  355.             }
  356.  
  357.             ((InputMethodContext)this).releaseCompositionArea();
  358.          }
  359.  
  360.       }
  361.    }
  362.  
  363.    public void dispose() {
  364.       if (this.currentClientComponent != null) {
  365.          throw new IllegalStateException("Can't dispose InputContext while it's active");
  366.       } else {
  367.          if (this.inputMethod != null) {
  368.             if (this == inputMethodWindowContext) {
  369.                this.inputMethod.hideWindows();
  370.                inputMethodWindowContext = null;
  371.             }
  372.  
  373.             if (this.inputMethod == previousInputMethod) {
  374.                previousInputMethod = null;
  375.             }
  376.  
  377.             if (this.clientWindowNotificationEnabled) {
  378.                if (this.addedClientWindowListeners()) {
  379.                   this.removeClientWindowListeners();
  380.                }
  381.  
  382.                this.clientWindowNotificationEnabled = false;
  383.             }
  384.  
  385.             this.inputMethod.dispose();
  386.             if (this.clientWindowNotificationEnabled) {
  387.                this.enableClientWindowNotification(this.inputMethod, false);
  388.             }
  389.  
  390.             this.inputMethod = null;
  391.          }
  392.  
  393.          this.inputMethodLocator = null;
  394.          if (this.usedInputMethods != null && !this.usedInputMethods.isEmpty()) {
  395.             Iterator var1 = this.usedInputMethods.values().iterator();
  396.             this.usedInputMethods = null;
  397.  
  398.             while(var1.hasNext()) {
  399.                ((InputMethod)var1.next()).dispose();
  400.             }
  401.          }
  402.  
  403.          this.clientWindowNotificationEnabled = false;
  404.          this.clientWindowListened = null;
  405.          this.perInputMethodState = null;
  406.       }
  407.    }
  408.  
  409.    public synchronized Object getInputMethodControlObject() {
  410.       InputMethod var1 = this.getInputMethod();
  411.       return var1 != null ? var1.getControlObject() : null;
  412.    }
  413.  
  414.    public void setCompositionEnabled(boolean var1) {
  415.       InputMethod var2 = this.getInputMethod();
  416.       if (var2 == null) {
  417.          throw new UnsupportedOperationException();
  418.       } else {
  419.          var2.setCompositionEnabled(var1);
  420.       }
  421.    }
  422.  
  423.    public boolean isCompositionEnabled() {
  424.       InputMethod var1 = this.getInputMethod();
  425.       if (var1 == null) {
  426.          throw new UnsupportedOperationException();
  427.       } else {
  428.          return var1.isCompositionEnabled();
  429.       }
  430.    }
  431.  
  432.    private synchronized InputMethod getInputMethod() {
  433.       if (this.inputMethod != null) {
  434.          return this.inputMethod;
  435.       } else if (this.inputMethodCreationFailed) {
  436.          return null;
  437.       } else {
  438.          this.inputMethod = this.getInputMethodInstance();
  439.          return this.inputMethod;
  440.       }
  441.    }
  442.  
  443.    private InputMethod getInputMethodInstance() {
  444.       InputMethodLocator var1 = this.inputMethodLocator;
  445.       if (var1 == null) {
  446.          this.inputMethodCreationFailed = true;
  447.          return null;
  448.       } else {
  449.          Locale var2 = var1.getLocale();
  450.          InputMethod var3 = null;
  451.          if (this.usedInputMethods != null) {
  452.             var3 = (InputMethod)this.usedInputMethods.remove(var1.deriveLocator((Locale)null));
  453.             if (var3 != null) {
  454.                if (var2 != null) {
  455.                   var3.setLocale(var2);
  456.                }
  457.  
  458.                var3.setCharacterSubsets(this.characterSubsets);
  459.                Boolean var4 = (Boolean)this.perInputMethodState.remove(var3);
  460.                if (var4 != null) {
  461.                   this.enableClientWindowNotification(var3, var4);
  462.                }
  463.  
  464.                ((InputMethodContext)this).setInputMethodSupportsBelowTheSpot(!(var3 instanceof InputMethodAdapter) || ((InputMethodAdapter)var3).supportsBelowTheSpot());
  465.                return var3;
  466.             }
  467.          }
  468.  
  469.          try {
  470.             var3 = var1.getDescriptor().createInputMethod();
  471.             if (var2 != null) {
  472.                var3.setLocale(var2);
  473.             }
  474.  
  475.             var3.setInputMethodContext((InputMethodContext)this);
  476.             var3.setCharacterSubsets(this.characterSubsets);
  477.          } catch (Exception var6) {
  478.             ((Throwable)var6).printStackTrace();
  479.             this.inputMethodCreationFailed = true;
  480.             if (var3 != null) {
  481.                var3 = null;
  482.             }
  483.          } catch (LinkageError var7) {
  484.             ((Throwable)var7).printStackTrace();
  485.             this.inputMethodCreationFailed = true;
  486.          }
  487.  
  488.          ((InputMethodContext)this).setInputMethodSupportsBelowTheSpot(!(var3 instanceof InputMethodAdapter) || ((InputMethodAdapter)var3).supportsBelowTheSpot());
  489.          return var3;
  490.       }
  491.    }
  492.  
  493.    InputMethodLocator getInputMethodLocator() {
  494.       return this.inputMethod != null ? this.inputMethodLocator.deriveLocator(this.inputMethod.getLocale()) : this.inputMethodLocator;
  495.    }
  496.  
  497.    public synchronized void suppressFocusChange() {
  498.       ++this.suppressDeactivateCount;
  499.    }
  500.  
  501.    public synchronized void endComposition() {
  502.       if (this.inputMethod != null) {
  503.          this.inputMethod.endComposition();
  504.       }
  505.  
  506.    }
  507.  
  508.    synchronized void enableClientWindowNotification(InputMethod var1, boolean var2) {
  509.       if (var1 != this.inputMethod) {
  510.          if (this.perInputMethodState == null) {
  511.             this.perInputMethodState = new HashMap(5);
  512.          }
  513.  
  514.          this.perInputMethodState.put(var1, new Boolean(var2));
  515.       } else {
  516.          if (this.clientWindowNotificationEnabled != var2) {
  517.             this.clientWindowLocation = null;
  518.             this.clientWindowNotificationEnabled = var2;
  519.          }
  520.  
  521.          if (this.clientWindowNotificationEnabled) {
  522.             if (!this.addedClientWindowListeners()) {
  523.                this.addClientWindowListeners();
  524.             }
  525.  
  526.             if (this.clientWindowListened != null) {
  527.                this.clientWindowLocation = null;
  528.                this.notifyClientWindowChange(this.clientWindowListened);
  529.             }
  530.          } else if (this.addedClientWindowListeners()) {
  531.             this.removeClientWindowListeners();
  532.          }
  533.  
  534.       }
  535.    }
  536.  
  537.    private synchronized void notifyClientWindowChange(Window var1) {
  538.       if (this.inputMethod != null) {
  539.          if (((Component)var1).isVisible() && (!(var1 instanceof Frame) || ((Frame)var1).getState() != 1)) {
  540.             Rectangle var2 = ((Component)var1).getBounds();
  541.             if (this.clientWindowLocation == null || !this.clientWindowLocation.equals(var2)) {
  542.                this.clientWindowLocation = var2;
  543.                this.inputMethod.notifyClientWindowChange(this.clientWindowLocation);
  544.             }
  545.  
  546.          } else {
  547.             this.clientWindowLocation = null;
  548.             this.inputMethod.notifyClientWindowChange((Rectangle)null);
  549.          }
  550.       }
  551.    }
  552.  
  553.    private synchronized void addClientWindowListeners() {
  554.       Component var1 = this.getClientComponent();
  555.       if (var1 != null) {
  556.          Window var2 = getComponentWindow(var1);
  557.          if (var2 != null) {
  558.             ((Component)var2).addComponentListener(this);
  559.             var2.addWindowListener(this);
  560.             this.clientWindowListened = var2;
  561.          }
  562.       }
  563.    }
  564.  
  565.    private synchronized void removeClientWindowListeners() {
  566.       this.clientWindowListened.removeComponentListener(this);
  567.       this.clientWindowListened.removeWindowListener(this);
  568.       this.clientWindowListened = null;
  569.    }
  570.  
  571.    private boolean addedClientWindowListeners() {
  572.       return this.clientWindowListened != null;
  573.    }
  574.  
  575.    public void componentResized(ComponentEvent var1) {
  576.       this.notifyClientWindowChange((Window)var1.getComponent());
  577.    }
  578.  
  579.    public void componentMoved(ComponentEvent var1) {
  580.       this.notifyClientWindowChange((Window)var1.getComponent());
  581.    }
  582.  
  583.    public void componentShown(ComponentEvent var1) {
  584.       this.notifyClientWindowChange((Window)var1.getComponent());
  585.    }
  586.  
  587.    public void componentHidden(ComponentEvent var1) {
  588.       this.notifyClientWindowChange((Window)var1.getComponent());
  589.    }
  590.  
  591.    public void windowOpened(WindowEvent var1) {
  592.    }
  593.  
  594.    public void windowClosing(WindowEvent var1) {
  595.    }
  596.  
  597.    public void windowClosed(WindowEvent var1) {
  598.    }
  599.  
  600.    public void windowIconified(WindowEvent var1) {
  601.       this.notifyClientWindowChange(var1.getWindow());
  602.    }
  603.  
  604.    public void windowDeiconified(WindowEvent var1) {
  605.       this.notifyClientWindowChange(var1.getWindow());
  606.    }
  607.  
  608.    public void windowActivated(WindowEvent var1) {
  609.    }
  610.  
  611.    public void windowDeactivated(WindowEvent var1) {
  612.    }
  613. }
  614.