jdk/src/java.desktop/share/classes/sun/awt/im/CompositionArea.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 11126
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.InputMethodEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.event.InputMethodListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.font.TextHitInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.font.TextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.im.InputMethodRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.text.AttributedCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.JFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.swing.JPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.swing.border.LineBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * A composition area is used to display text that's being composed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * using an input method in its own user interface environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * typically in a root window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author JavaSoft International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
1311
2b1552286d23 6607310: InputContext may cause loading of swing classes even for non-Swing applets
peytoia
parents: 2
diff changeset
    59
// This class is final due to the 6607310 fix. Refer to the CR for details.
2b1552286d23 6607310: InputContext may cause loading of swing classes even for non-Swing applets
peytoia
parents: 2
diff changeset
    60
public final class CompositionArea extends JPanel implements InputMethodListener {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private CompositionAreaHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private TextLayout composedTextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private TextHitInfo caret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private JFrame compositionWindow;
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    67
    private static final int TEXT_ORIGIN_X = 5;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    68
    private static final int TEXT_ORIGIN_Y = 15;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    69
    private static final int PASSIVE_WIDTH = 480;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    70
    private static final int WIDTH_MARGIN=10;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
    71
    private static final int HEIGHT_MARGIN=3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    CompositionArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // create composition window with localized title
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String windowTitle = Toolkit.getProperty("AWT.CompositionWindowTitle", "Input Window");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        compositionWindow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            (JFrame)InputMethodContext.createInputMethodWindow(windowTitle, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        setBorder(LineBorder.createGrayLineBorder());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        setForeground(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        setBackground(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // if we get the focus, we still want to let the client's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // input context handle the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        enableInputMethods(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        enableEvents(AWTEvent.KEY_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        compositionWindow.getContentPane().add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        compositionWindow.addWindowListener(new FrameWindowAdapter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        addInputMethodListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        compositionWindow.enableInputMethods(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        compositionWindow.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Dimension windowSize = compositionWindow.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Dimension screenSize = (getToolkit()).getScreenSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        compositionWindow.setLocation(screenSize.width - windowSize.width-20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                    screenSize.height - windowSize.height-100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        compositionWindow.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Sets the composition area handler that currently owns this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * composition area, and its input context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    synchronized void setHandlerInfo(CompositionAreaHandler handler, InputContext inputContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.handler = handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ((InputMethodWindow) compositionWindow).setInputContext(inputContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see java.awt.Component#getInputMethodRequests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public InputMethodRequests getInputMethodRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // returns a 0-width rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private Rectangle getCaretRectangle(TextHitInfo caret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int caretLocation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        TextLayout layout = composedTextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (layout != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            caretLocation = Math.round(layout.getCaretInfo(caret)[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        FontMetrics metrics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            metrics = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return new Rectangle(TEXT_ORIGIN_X + caretLocation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                             TEXT_ORIGIN_Y - metrics.getAscent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                             0, metrics.getAscent() + metrics.getDescent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        g.setColor(getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        TextLayout layout = composedTextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (layout != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            layout.draw((Graphics2D) g, TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (caret != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            Rectangle rectangle = getCaretRectangle(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            g.setXORMode(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            g.fillRect(rectangle.x, rectangle.y, 1, rectangle.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            g.setPaintMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // shows/hides the composition window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    void setCompositionAreaVisible(boolean visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        compositionWindow.setVisible(visible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // returns true if composition area is visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    boolean isCompositionAreaVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return compositionWindow.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // workaround for the Solaris focus lost problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    class FrameWindowAdapter extends WindowAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public void windowActivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // InputMethodListener methods - just forward to the current handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void inputMethodTextChanged(InputMethodEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        handler.inputMethodTextChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void caretPositionChanged(InputMethodEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        handler.caretPositionChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Sets the text and caret to be displayed in this composition area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Shows the window if it contains text, hides it if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    void setText(AttributedCharacterIterator composedText, TextHitInfo caret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        composedTextLayout = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (composedText == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // there's no composed text to display, so hide the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            compositionWindow.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.caret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            /* since we have composed text, make sure the window is shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
               This is necessary to get a valid graphics object. See 6181385.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (!compositionWindow.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                compositionWindow.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                updateWindowLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                FontRenderContext context = ((Graphics2D)g).getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                composedTextLayout = new TextLayout(composedText, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                Rectangle2D bounds = composedTextLayout.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                this.caret = caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                // Resize the composition area to just fit the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                FontMetrics metrics = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                Rectangle2D maxCharBoundsRec = metrics.getMaxCharBounds(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                int newHeight = (int)maxCharBoundsRec.getHeight() + HEIGHT_MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                int newFrameHeight = newHeight +compositionWindow.getInsets().top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                               +compositionWindow.getInsets().bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                // If it's a passive client, set the width always to PASSIVE_WIDTH (480px)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                InputMethodRequests req = handler.getClientInputMethodRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                int newWidth = (req==null) ? PASSIVE_WIDTH : (int)bounds.getWidth() + WIDTH_MARGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                int newFrameWidth = newWidth + compositionWindow.getInsets().left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                             + compositionWindow.getInsets().right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                setPreferredSize(new Dimension(newWidth, newHeight));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                compositionWindow.setSize(new Dimension(newFrameWidth, newFrameHeight));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                // show the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Sets the caret to be displayed in this composition area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * The text is not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    void setCaret(TextHitInfo caret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        this.caret = caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (compositionWindow.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                g.dispose();
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
     * Positions the composition window near (usually below) the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * insertion point in the client component if the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * component is an active client (below-the-spot input).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    void updateWindowLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        InputMethodRequests req = handler.getClientInputMethodRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (req == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // not an active client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        Point windowLocation = new Point();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        Rectangle caretRect = req.getTextLocation(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Dimension windowSize = compositionWindow.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        final int SPACING = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (caretRect.x + windowSize.width > screenSize.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            windowLocation.x = screenSize.width - windowSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            windowLocation.x = caretRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (caretRect.y + caretRect.height + SPACING + windowSize.height > screenSize.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            windowLocation.y = caretRect.y - SPACING - windowSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            windowLocation.y = caretRect.y + caretRect.height + SPACING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        compositionWindow.setLocation(windowLocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // support for InputMethodRequests methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    Rectangle getTextLocation(TextHitInfo offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        Rectangle rectangle = getCaretRectangle(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        Point location = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        rectangle.translate(location.x, location.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
   TextHitInfo getLocationOffset(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        TextLayout layout = composedTextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (layout == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            Point location = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            x -= location.x + TEXT_ORIGIN_X;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            y -= location.y + TEXT_ORIGIN_Y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (layout.getBounds().contains(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return layout.hitTestChar(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    // Disables or enables decorations of the composition window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    void setCompositionAreaUndecorated(boolean setUndecorated){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
          if (compositionWindow.isDisplayable()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
              compositionWindow.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
          compositionWindow.setUndecorated(setUndecorated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
          compositionWindow.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
11126
00a6b63e7ba4 7117465: Warning cleanup for IMF classes
naoto
parents: 5506
diff changeset
   316
    // Proclaim serial compatibility with 1.7.0
00a6b63e7ba4 7117465: Warning cleanup for IMF classes
naoto
parents: 5506
diff changeset
   317
    private static final long serialVersionUID = -1057247068746557444L;
00a6b63e7ba4 7117465: Warning cleanup for IMF classes
naoto
parents: 5506
diff changeset
   318
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}