gt:         if (highlight.isSelected()) {
context: /*
   * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Oracle in the LICENSE file that accompanied this code.
   *
   * This code is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   * version 2 for more details (a copy is included in the LICENSE file that
   * accompanied this code).
   *
   * You should have received a copy of the GNU General Public License version
   * 2 along with this work; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   *
   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  
  package sun.awt;
  
  import java.util.Collections;
  import java.util.Locale;
  import java.util.Map;
  import java.util.HashMap;
  import java.awt.AWTEvent;
  import java.awt.AWTException;
  import java.awt.Component;
  import java.awt.Container;
  import java.awt.EventQueue;
  import java.awt.Window;
  import java.awt.im.InputContext;
  import java.awt.im.InputMethodHighlight;
  import java.awt.im.spi.InputMethodContext;
  import sun.awt.im.InputMethodAdapter;
  import java.awt.event.InputEvent;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseEvent;
  import java.awt.event.FocusEvent;
  import java.awt.event.ComponentEvent;
  import java.awt.event.WindowEvent;
  import java.awt.event.InputMethodEvent;
  import java.awt.font.TextAttribute;
  import java.awt.font.TextHitInfo;
  import java.awt.peer.ComponentPeer;
  import java.lang.Character.Subset;
  import java.text.AttributedString;
  import java.text.AttributedCharacterIterator;
  
  import java.io.File;
  import java.io.FileReader;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.lang.ref.WeakReference;
  import sun.util.logging.PlatformLogger;
  import java.util.StringTokenizer;
  import java.util.regex.Pattern;
  
  
  /**
   * Input Method Adapter for XIM
   *
   * @author JavaSoft International
   */
  public abstract class X11InputMethod extends InputMethodAdapter {
      private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11InputMethod");
      /*
       * The following XIM* values must be the same as those defined in
       * Xlib.h
       */
      private static final int XIMReverse = (1<<0);
      private static final int XIMUnderline = (1<<1);
      private static final int XIMHighlight = (1<<2);
      private static final int XIMPrimary = (1<<5);
      private static final int XIMSecondary = (1<<6);
      private static final int XIMTertiary = (1<<7);
  
      /*
       * visible position values
       */
      private static final int XIMVisibleToForward = (1<<8);
      private static final int XIMVisibleToBackward = (1<<9);
      private static final int XIMVisibleCenter = (1<<10);
      private static final int XIMVisibleMask = (XIMVisibleToForward|
                                                 XIMVisibleToBackward|
                                                 XIMVisibleCenter);
  
      private Locale locale;
      private static boolean isXIMOpened = false;
      protected Container clientComponentWindow = null;
      private Component awtFocussedComponent = null;
      private Component lastXICFocussedComponent = null;
      private boolean   isLastXICActive = false;
      private boolean   isLastTemporary = false;
      private boolean   isActive = false;
      private boolean   isActiveClient = false;
      private static Map[] highlightStyles;
      private boolean disposed = false;
  
      //reset the XIC if necessary
      private boolean   needResetXIC = false;
      private WeakReference<Component> needResetXICClient = new WeakReference<>(null);
  
      // The use of compositionEnableSupported is to reduce unnecessary
      // native calls if set/isCompositionEnabled
      // throws UnsupportedOperationException.
      // It is set to false if that exception is thrown first time
      // either of the two methods are called.
      private boolean compositionEnableSupported = true;
      // The savedCompositionState indicates the composition mode when
      // endComposition or setCompositionEnabled is called. It doesn't always
      // reflect the actual composition state because it doesn't get updated
      // when the user changes the composition state through direct interaction
      // with the input method. It is used to save the composition mode when
      // focus is traversed across different client components sharing the
      // same java input context. Also if set/isCompositionEnabled are not
      // supported, it remains false.
      private boolean savedCompositionState = false;
  
      // variables to keep track of preedit context.
      // these variables need to be accessed within AWT_LOCK/UNLOCK
      private String committedText = null;
      private StringBuffer composedText = null;
      private IntBuffer rawFeedbacks;
  
      // private data (X11InputMethodData structure defined in
      // awt_InputMethod.c) for native methods
      // this structure needs to be accessed within AWT_LOCK/UNLOCK
      transient private long pData = 0; // accessed by native
  
      // Initialize highlight mapping table
      static {
          Map styles[] = new Map[4];
          HashMap map;
  
          // UNSELECTED_RAW_TEXT_HIGHLIGHT
          map = new HashMap(1);
          map.put(TextAttribute.WEIGHT,
                    TextAttribute.WEIGHT_BOLD);
          styles[0] = Collections.unmodifiableMap(map);
  
          // SELECTED_RAW_TEXT_HIGHLIGHT
          map = new HashMap(1);
          map.put(TextAttribute.SWAP_COLORS,
                    TextAttribute.SWAP_COLORS_ON);
          styles[1] = Collections.unmodifiableMap(map);
  
          // UNSELECTED_CONVERTED_TEXT_HIGHLIGHT
          map = new HashMap(1);
          map.put(TextAttribute.INPUT_METHOD_UNDERLINE,
                    TextAttribute.UNDERLINE_LOW_ONE_PIXEL);
          styles[2] = Collections.unmodifiableMap(map);
  
          // SELECTED_CONVERTED_TEXT_HIGHLIGHT
          map = new HashMap(1);
          map.put(TextAttribute.SWAP_COLORS,
                    TextAttribute.SWAP_COLORS_ON);
          styles[3] = Collections.unmodifiableMap(map);
  
          highlightStyles = styles;
      }
  
      static {
          initIDs();
      }
  
      /**
       * Initialize JNI field and method IDs for fields that may be
         accessed from C.
       */
      private static native void initIDs();
  
      /**
       * Constructs an X11InputMethod instance. It initializes the XIM
       * environment if it's not done yet.
       *
       * @exception AWTException if XOpenIM() failed.
       */
      public X11InputMethod() throws AWTException {
          // supports only the locale in which the VM is started
          locale = X11InputMethodDescriptor.getSupportedLocale();
          if (initXIM() == false) {
              throw new AWTException("Cannot open X Input Method");
          }
      }
  
      protected void finalize() throws Throwable {
          dispose();
          super.finalize();
      }
  
      /**
       * Invokes openIM() that invokes XOpenIM() if it's not opened yet.
       * @return  true if openXIM() is successful or it's already been opened.
       */
      private synchronized boolean initXIM() {
          if (isXIMOpened == false)
              isXIMOpened = openXIM();
          return isXIMOpened;
      }
  
      protected abstract boolean openXIM();
  
      protected boolean isDisposed() {
          return disposed;
      }
  
      protected abstract void setXICFocus(ComponentPeer peer,
                                      boolean value, boolean active);
  
      /**
       * Does nothing - this adapter doesn't use the input method context.
       *
       * @see java.awt.im.spi.InputMethod#setInputMethodContext
       */
      public void setInputMethodContext(InputMethodContext context) {
      }
  
      /**
       * Set locale to input. If input method doesn't support specified locale,
       * false will be returned and its behavior is not changed.
       *
       * @param lang locale to input
       * @return the true is returned when specified locale is supported.
       */
      public boolean setLocale(Locale lang) {
          if (lang.equals(locale)) {
              return true;
          }
          // special compatibility rule for Japanese and Korean
          if (locale.equals(Locale.JAPAN) && lang.equals(Locale.JAPANESE) ||
                  locale.equals(Locale.KOREA) && lang.equals(Locale.KOREAN)) {
              return true;
          }
          return false;
      }
  
      /**
       * Returns current input locale.
       */
      public Locale getLocale() {
          return locale;
      }
  
      /**
       * Does nothing - XIM doesn't let you specify which characters you expect.
       *
       * @see java.awt.im.spi.InputMethod#setCharacterSubsets
       */
      public void setCharacterSubsets(Subset[] subsets) {
      }
  
      /**
       * Dispatch event to input method. InputContext dispatch event with this
       * method. Input method set consume flag if event is consumed in
       * input method.
       *
       * @param e event
       */
      public void dispatchEvent(AWTEvent e) {
      }
  
  
      protected final void resetXICifneeded(){
          /* needResetXIC is used to indicate whether to call
             resetXIC on the active client. resetXIC will always be
             called on the passive client when endComposition is called.
          */
          if (needResetXIC && haveActiveClient() &&
              getClientComponent() != needResetXICClient.get()){
              resetXIC();
  
              // needs to reset the last xic focussed component.
              lastXICFocussedComponent = null;
              isLastXICActive = false;
  
              needResetXICClient.clear();
              needResetXIC = false;
          }
      }
  
      /**
       * Reset the composition state to the current composition state.
       */
      private void resetCompositionState() {
          if (compositionEnableSupported) {
              try {
                  /* Restore the composition mode to the last saved composition
                     mode. */
                  setCompositionEnabled(savedCompositionState);
              } catch (UnsupportedOperationException e) {
                  compositionEnableSupported = false;
              }
          }
      }
  
      /**
       * Query and then return the current composition state.
       * @returns the composition state if isCompositionEnabled call
       * is successful. Otherwise, it returns false.
       */
      private boolean getCompositionState() {
          boolean compositionState = false;
          if (compositionEnableSupported) {
              try {
                  compositionState = isCompositionEnabled();
              } catch (UnsupportedOperationException e) {
                  compositionEnableSupported = false;
              }
          }
          return compositionState;
      }
  
      /**
       * Activate input method.
       */
      public synchronized void activate() {
          clientComponentWindow = getClientComponentWindow();
          if (clientComponentWindow == null)
              return;
  
          if (lastXICFocussedComponent != null){
              if (log.isLoggable(PlatformLogger.FINE)) log.fine("XICFocused {0}, AWTFocused {1}",
                                                                lastXICFocussedComponent, awtFocussedComponent);
          }
  
          if (pData == 0) {
              if (!createXIC()) {
                  return;
              }
              disposed = false;
          }
  
          /*  reset input context if necessary and set the XIC focus
          */
          resetXICifneeded();
          ComponentPeer lastXICFocussedComponentPeer = null;
          ComponentPeer awtFocussedComponentPeer = getPeer(awtFocussedComponent);
  
          if (lastXICFocussedComponent != null) {
             lastXICFocussedComponentPeer = getPeer(lastXICFocussedComponent);
          }
  
          /* If the last XIC focussed component has a different peer as the
             current focussed component, change the XIC focus to the newly
             focussed component.
          */
          if (isLastTemporary || lastXICFocussedComponentPeer != awtFocussedComponentPeer ||
              isLastXICActive != haveActiveClient()) {
              if (lastXICFocussedComponentPeer != null) {
                  setXICFocus(lastXICFocussedComponentPeer, false, isLastXICActive);
              }
              if (awtFocussedComponentPeer != null) {
                  setXICFocus(awtFocussedComponentPeer, true, haveActiveClient());
              }
              lastXICFocussedComponent = awtFocussedComponent;
              isLastXICActive = haveActiveClient();
          }
          resetCompositionState();
          isActive = true;
      }
  
      protected abstract boolean createXIC();
  
      /**
       * Deactivate input method.
       */
      public synchronized void deactivate(boolean isTemporary) {
          boolean   isAc =  haveActiveClient();
          /* Usually as the client component, let's call it component A,
             loses the focus, this method is called. Then when another client
             component, let's call it component B,  gets the focus, activate is first called on
             the previous focused compoent which is A, then endComposition is called on A,
             deactivate is called on A again. And finally activate is called on the newly
             focused component B. Here is the call sequence.
  
             A loses focus               B gains focus
             -------------> deactivate A -------------> activate A -> endComposition A ->
             deactivate A -> activate B ----....
  
             So in order to carry the composition mode across the components sharing the same
             input context, we save it when deactivate is called so that when activate is
             called, it can be restored correctly till activate is called on the newly focused
             component. (See also sun/awt/im/InputContext and bug 6184471).
             Last note, getCompositionState should be called before setXICFocus since
             setXICFocus here sets the XIC to 0.
          */
          savedCompositionState = getCompositionState();
  
          if (isTemporary){
              //turn the status window off...
              turnoffStatusWindow();
          }
  
          /* Delay resetting the XIC focus until activate is called and the newly
             focussed component has a different peer as the last focussed component.
          */
          lastXICFocussedComponent = awtFocussedComponent;
          isLastXICActive = isAc;
          isLastTemporary = isTemporary;
          isActive = false;
      }
  
      /**
       * Explicitly disable the native IME. Native IME is not disabled when
       * deactivate is called.
       */
      public void disableInputMethod() {
          if (lastXICFocussedComponent != null) {
              setXICFocus(getPeer(lastXICFocussedComponent), false, isLastXICActive);
              lastXICFocussedComponent = null;
              isLastXICActive = false;
  
              resetXIC();
              needResetXICClient.clear();
              needResetXIC = false;
          }
      }
  
      // implements java.awt.im.spi.InputMethod.hideWindows
      public void hideWindows() {
          // ??? need real implementation
      }
  
      /**
       * @see java.awt.Toolkit#mapInputMethodHighlight
       */
      public static Map mapInputMethodHighlight(InputMethodHighlight highlight) {
          int index;
          int state = highlight.getState();
          if (state == InputMethodHighlight.RAW_TEXT) {
              index = 0;
          } else if (state == InputMethodHighlight.CONVERTED_TEXT) {
              index = 2;
          } else {
              return null;
          }
  