jdk/src/share/classes/sun/awt/im/InputMethodContext.java
author pchelko
Mon, 13 Jan 2014 19:09:24 +0400
changeset 23260 d68cbf54738f
parent 11126 00a6b63e7ba4
child 23010 6dadb192ad81
permissions -rw-r--r--
8029893: Remove reflection from DataTransferer.getInstance Reviewed-by: anthony, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.im;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.HeadlessException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.InputMethodEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.font.TextHitInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.im.InputMethodRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.im.spi.InputMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.text.AttributedCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.text.AttributedCharacterIterator.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.text.AttributedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.text.CharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.JFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.awt.InputMethodSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The InputMethodContext class provides methods that input methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * can use to communicate with their client components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * It is a subclass of InputContext, which provides methods for use by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author JavaSoft International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class InputMethodContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
       extends sun.awt.im.InputContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
       implements java.awt.im.spi.InputMethodContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean dispatchingCommittedText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Creation of the context's composition area handler is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // delayed until we really need a composition area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private CompositionAreaHandler compositionAreaHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Object compositionAreaHandlerLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static private boolean belowTheSpotInputRequested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private boolean inputMethodSupportsBelowTheSpot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // check whether we should use below-the-spot input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // get property from command line
11126
00a6b63e7ba4 7117465: Warning cleanup for IMF classes
naoto
parents: 5506
diff changeset
    75
        String inputStyle = AccessController.doPrivileged
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                (new GetPropertyAction("java.awt.im.style", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // get property from awt.properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (inputStyle == null) {
11126
00a6b63e7ba4 7117465: Warning cleanup for IMF classes
naoto
parents: 5506
diff changeset
    79
            inputStyle = Toolkit.getProperty("java.awt.im.style", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        belowTheSpotInputRequested = "below-the-spot".equals(inputStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Constructs an InputMethodContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public InputMethodContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    void setInputMethodSupportsBelowTheSpot(boolean supported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        inputMethodSupportsBelowTheSpot = supported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
   boolean useBelowTheSpotInput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return belowTheSpotInputRequested && inputMethodSupportsBelowTheSpot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private boolean haveActiveClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Component client = getClientComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return client != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
               && client.getInputMethodRequests() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // implements java.awt.im.spi.InputMethodContext.dispatchInputMethodEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void dispatchInputMethodEvent(int id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                AttributedCharacterIterator text, int committedCharacterCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                TextHitInfo caret, TextHitInfo visiblePosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // We need to record the client component as the source so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // that we have correct information if we later have to break up this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // event into key events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Component source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        source = getClientComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (source != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            InputMethodEvent event = new InputMethodEvent(source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    id, text, committedCharacterCount, caret, visiblePosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (haveActiveClient() && !useBelowTheSpotInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                source.dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                getCompositionAreaHandler(true).processInputMethodEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Dispatches committed text to a client component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Called by composition window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param client The component that the text should get dispatched to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param text The iterator providing access to the committed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *        (and possible composed) text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param committedCharacterCount The number of committed characters in the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    synchronized void dispatchCommittedText(Component client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                 AttributedCharacterIterator text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                 int committedCharacterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // note that the client is not always the current client component -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // some host input method adapters may dispatch input method events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // through the Java event queue, and we may have switched clients while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // the event was in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (committedCharacterCount == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                || text.getEndIndex() <= text.getBeginIndex()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        dispatchingCommittedText = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            InputMethodRequests req = client.getInputMethodRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (req != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // active client -> send text as InputMethodEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                int beginIndex = text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                AttributedCharacterIterator toBeCommitted =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    (new AttributedString(text, beginIndex, beginIndex + committedCharacterCount)).getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                InputMethodEvent inputEvent = new InputMethodEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        toBeCommitted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        committedCharacterCount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                client.dispatchEvent(inputEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // passive client -> send text as KeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                char keyChar = text.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                while (committedCharacterCount-- > 0 && keyChar != CharacterIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    KeyEvent keyEvent = new KeyEvent(client, KeyEvent.KEY_TYPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                                 time, 0, KeyEvent.VK_UNDEFINED, keyChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    client.dispatchEvent(keyEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    keyChar = text.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            dispatchingCommittedText = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void dispatchEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // some host input method adapters may dispatch input method events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // through the Java event queue. If the component that the event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // intended for isn't an active client, or if we're using below-the-spot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // input, we need to dispatch this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // to the input window. Note that that component is not necessarily the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // current client component, since we may have switched clients while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // the event was in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (event instanceof InputMethodEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (((Component) event.getSource()).getInputMethodRequests() == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    || (useBelowTheSpotInput() && !dispatchingCommittedText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                getCompositionAreaHandler(true).processInputMethodEvent((InputMethodEvent) event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // make sure we don't dispatch our own key events back to the input method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (!dispatchingCommittedText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                super.dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Gets this context's composition area handler, creating it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * If requested, it grabs the composition area for use by this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * The composition area's text is not updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private CompositionAreaHandler getCompositionAreaHandler(boolean grab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        synchronized(compositionAreaHandlerLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (compositionAreaHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                compositionAreaHandler = new CompositionAreaHandler(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            compositionAreaHandler.setClientComponent(getClientComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (grab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                compositionAreaHandler.grabCompositionArea(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return compositionAreaHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Grabs the composition area for use by this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * If doUpdate is true, updates the composition area with previously sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    void grabCompositionArea(boolean doUpdate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        synchronized(compositionAreaHandlerLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (compositionAreaHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                compositionAreaHandler.grabCompositionArea(doUpdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                // if this context hasn't seen a need for a composition area yet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // just close it without creating the machinery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                CompositionAreaHandler.closeCompositionArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Releases and closes the composition area if it is currently owned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * this context's composition area handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    void releaseCompositionArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        synchronized(compositionAreaHandlerLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (compositionAreaHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                compositionAreaHandler.releaseCompositionArea();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Calls CompositionAreaHandler.isCompositionAreaVisible() to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * whether the composition area is visible or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Notice that this method is always called on the AWT event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    boolean isCompositionAreaVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (compositionAreaHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return compositionAreaHandler.isCompositionAreaVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Calls CompositionAreaHandler.setCompositionAreaVisible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * show or hide the composition area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * As isCompositionAreaVisible method, it is always called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * on AWT event dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void setCompositionAreaVisible(boolean visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (compositionAreaHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            compositionAreaHandler.setCompositionAreaVisible(visible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Calls the current client component's implementation of getTextLocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public Rectangle getTextLocation(TextHitInfo offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return getReq().getTextLocation(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Calls the current client component's implementation of getLocationOffset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public TextHitInfo getLocationOffset(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return getReq().getLocationOffset(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Calls the current client component's implementation of getInsertPositionOffset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public int getInsertPositionOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return getReq().getInsertPositionOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Calls the current client component's implementation of getCommittedText.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public AttributedCharacterIterator getCommittedText(int beginIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                                       int endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                                       Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return getReq().getCommittedText(beginIndex, endIndex, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Calls the current client component's implementation of getCommittedTextLength.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public int getCommittedTextLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return getReq().getCommittedTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Calls the current client component's implementation of cancelLatestCommittedText.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public AttributedCharacterIterator cancelLatestCommittedText(Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return getReq().cancelLatestCommittedText(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Calls the current client component's implementation of getSelectedText.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public AttributedCharacterIterator getSelectedText(Attribute[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return getReq().getSelectedText(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private InputMethodRequests getReq() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (haveActiveClient() && !useBelowTheSpotInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return getClientComponent().getInputMethodRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return getCompositionAreaHandler(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // implements java.awt.im.spi.InputMethodContext.createInputMethodWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public Window createInputMethodWindow(String title, boolean attachToInputContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        InputContext context = attachToInputContext ? this : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return createInputMethodWindow(title, context, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // implements java.awt.im.spi.InputMethodContext.createInputMethodJFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public JFrame createInputMethodJFrame(String title, boolean attachToInputContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        InputContext context = attachToInputContext ? this : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return (JFrame)createInputMethodWindow(title, context, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static Window createInputMethodWindow(String title, InputContext context, boolean isSwing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (isSwing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return new InputMethodJFrame(title, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (toolkit instanceof InputMethodSupport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return ((InputMethodSupport)toolkit).createInputMethodWindow(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    title, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        throw new InternalError("Input methods must be supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see java.awt.im.spi.InputMethodContext#enableClientWindowNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public void enableClientWindowNotification(InputMethod inputMethod, boolean enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        super.enableClientWindowNotification(inputMethod, enable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
   * Disables or enables decorations for the composition window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
   void setCompositionAreaUndecorated(boolean undecorated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (compositionAreaHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            compositionAreaHandler.setCompositionAreaUndecorated(undecorated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}