Search in sources :

Example 1 with InputMethodRequests

use of java.awt.im.InputMethodRequests in project jdk8u_jdk by JetBrains.

the class InputContext method focusGained.

/**
     * Handles focus gained events for any component that's using
     * this input context.
     * These events are generated by AWT when the keyboard focus
     * moves to a component.
     * Besides actual client components, the source components
     * may also be the composition area or any component in an
     * input method window.
     * <p>
     * When handling the focus event for a client component, this
     * method checks whether the input context was previously
     * active for a different client component, and if so, calls
     * endComposition for the previous client component.
     *
     * @param source the component gaining the focus
     */
private void focusGained(Component source) {
    /*
         * NOTE: When a Container is removing its Component which
         * invokes this.removeNotify(), the Container has the global
         * Component lock. It is possible to happen that an
         * application thread is calling this.removeNotify() while an
         * AWT event queue thread is dispatching a focus event via
         * this.dispatchEvent(). If an input method uses AWT
         * components (e.g., IIIMP status window), it causes deadlock,
         * for example, Component.show()/hide() in this situation
         * because hide/show tried to obtain the lock.  Therefore,
         * it's necessary to obtain the global Component lock before
         * activating or deactivating an input method.
         */
    synchronized (source.getTreeLock()) {
        synchronized (this) {
            if ("sun.awt.im.CompositionArea".equals(source.getClass().getName())) {
            // no special handling for this one
            } else if (getComponentWindow(source) instanceof InputMethodWindow) {
            // no special handling for this one either
            } else {
                if (!source.isDisplayable()) {
                    // Component is being disposed
                    return;
                }
                // until we really know we're switching to a different component
                if (inputMethod != null) {
                    if (currentClientComponent != null && currentClientComponent != source) {
                        if (!isInputMethodActive) {
                            activateInputMethod(false);
                        }
                        endComposition();
                        deactivateInputMethod(false);
                    }
                }
                currentClientComponent = source;
            }
            awtFocussedComponent = source;
            if (inputMethod instanceof InputMethodAdapter) {
                ((InputMethodAdapter) inputMethod).setAWTFocussedComponent(source);
            }
            // coming up
            if (!isInputMethodActive) {
                activateInputMethod(true);
            }
            // If the client component is an active client with the below-the-spot
            // input style, then make the composition window undecorated without a title bar.
            InputMethodContext inputContext = ((InputMethodContext) this);
            if (!inputContext.isCompositionAreaVisible()) {
                InputMethodRequests req = source.getInputMethodRequests();
                if (req != null && inputContext.useBelowTheSpotInput()) {
                    inputContext.setCompositionAreaUndecorated(true);
                } else {
                    inputContext.setCompositionAreaUndecorated(false);
                }
            }
            // when focus got lost
            if (compositionAreaHidden == true) {
                ((InputMethodContext) this).setCompositionAreaVisible(true);
                compositionAreaHidden = false;
            }
        }
    }
}
Also used : InputMethodRequests(java.awt.im.InputMethodRequests)

Example 2 with InputMethodRequests

use of java.awt.im.InputMethodRequests in project jdk8u_jdk by JetBrains.

the class InputMethodContext method dispatchCommittedText.

/**
     * Dispatches committed text to a client component.
     * Called by composition window.
     *
     * @param client The component that the text should get dispatched to.
     * @param text The iterator providing access to the committed
     *        (and possible composed) text.
     * @param committedCharacterCount The number of committed characters in the text.
     */
synchronized void dispatchCommittedText(Component client, AttributedCharacterIterator text, int committedCharacterCount) {
    // the event was in the queue.
    if (committedCharacterCount == 0 || text.getEndIndex() <= text.getBeginIndex()) {
        return;
    }
    long time = System.currentTimeMillis();
    dispatchingCommittedText = true;
    try {
        InputMethodRequests req = client.getInputMethodRequests();
        if (req != null) {
            // active client -> send text as InputMethodEvent
            int beginIndex = text.getBeginIndex();
            AttributedCharacterIterator toBeCommitted = (new AttributedString(text, beginIndex, beginIndex + committedCharacterCount)).getIterator();
            InputMethodEvent inputEvent = new InputMethodEvent(client, InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, toBeCommitted, committedCharacterCount, null, null);
            client.dispatchEvent(inputEvent);
        } else {
            // passive client -> send text as KeyEvents
            char keyChar = text.first();
            while (committedCharacterCount-- > 0 && keyChar != CharacterIterator.DONE) {
                KeyEvent keyEvent = new KeyEvent(client, KeyEvent.KEY_TYPED, time, 0, KeyEvent.VK_UNDEFINED, keyChar);
                client.dispatchEvent(keyEvent);
                keyChar = text.next();
            }
        }
    } finally {
        dispatchingCommittedText = false;
    }
}
Also used : KeyEvent(java.awt.event.KeyEvent) AttributedString(java.text.AttributedString) InputMethodRequests(java.awt.im.InputMethodRequests) InputMethodEvent(java.awt.event.InputMethodEvent) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 3 with InputMethodRequests

use of java.awt.im.InputMethodRequests in project jdk8u_jdk by JetBrains.

the class CompositionArea method updateWindowLocation.

/**
     * Positions the composition window near (usually below) the
     * insertion point in the client component if the client
     * component is an active client (below-the-spot input).
     */
void updateWindowLocation() {
    InputMethodRequests req = handler.getClientInputMethodRequests();
    if (req == null) {
        // not an active client
        return;
    }
    Point windowLocation = new Point();
    Rectangle caretRect = req.getTextLocation(null);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension windowSize = compositionWindow.getSize();
    final int SPACING = 2;
    if (caretRect.x + windowSize.width > screenSize.width) {
        windowLocation.x = screenSize.width - windowSize.width;
    } else {
        windowLocation.x = caretRect.x;
    }
    if (caretRect.y + caretRect.height + SPACING + windowSize.height > screenSize.height) {
        windowLocation.y = caretRect.y - SPACING - windowSize.height;
    } else {
        windowLocation.y = caretRect.y + caretRect.height + SPACING;
    }
    compositionWindow.setLocation(windowLocation);
}
Also used : InputMethodRequests(java.awt.im.InputMethodRequests) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 4 with InputMethodRequests

use of java.awt.im.InputMethodRequests in project jdk8u_jdk by JetBrains.

the class CompositionArea method setText.

/**
     * Sets the text and caret to be displayed in this composition area.
     * Shows the window if it contains text, hides it if not.
     */
void setText(AttributedCharacterIterator composedText, TextHitInfo caret) {
    composedTextLayout = null;
    if (composedText == null) {
        // there's no composed text to display, so hide the window
        compositionWindow.setVisible(false);
        this.caret = null;
    } else {
        /* since we have composed text, make sure the window is shown.
               This is necessary to get a valid graphics object. See 6181385.
            */
        if (!compositionWindow.isVisible()) {
            compositionWindow.setVisible(true);
        }
        Graphics g = getGraphics();
        if (g == null) {
            return;
        }
        try {
            updateWindowLocation();
            FontRenderContext context = ((Graphics2D) g).getFontRenderContext();
            composedTextLayout = new TextLayout(composedText, context);
            Rectangle2D bounds = composedTextLayout.getBounds();
            this.caret = caret;
            // Resize the composition area to just fit the text.
            FontMetrics metrics = g.getFontMetrics();
            Rectangle2D maxCharBoundsRec = metrics.getMaxCharBounds(g);
            int newHeight = (int) maxCharBoundsRec.getHeight() + HEIGHT_MARGIN;
            int newFrameHeight = newHeight + compositionWindow.getInsets().top + compositionWindow.getInsets().bottom;
            // If it's a passive client, set the width always to PASSIVE_WIDTH (480px)
            InputMethodRequests req = handler.getClientInputMethodRequests();
            int newWidth = (req == null) ? PASSIVE_WIDTH : (int) bounds.getWidth() + WIDTH_MARGIN;
            int newFrameWidth = newWidth + compositionWindow.getInsets().left + compositionWindow.getInsets().right;
            setPreferredSize(new Dimension(newWidth, newHeight));
            compositionWindow.setSize(new Dimension(newFrameWidth, newFrameHeight));
            // show the composed text
            paint(g);
        } finally {
            g.dispose();
        }
    }
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) InputMethodRequests(java.awt.im.InputMethodRequests) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Dimension(java.awt.Dimension) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 5 with InputMethodRequests

use of java.awt.im.InputMethodRequests in project jdk8u_jdk by JetBrains.

the class CompositionAreaHandler method createCompositionArea.

/**
     * Creates the composition area.
     */
private void createCompositionArea() {
    synchronized (compositionAreaLock) {
        compositionArea = new CompositionArea();
        if (compositionAreaOwner != null) {
            compositionArea.setHandlerInfo(compositionAreaOwner, inputMethodContext);
        }
        // If the client component is an active client using below-the-spot style, then
        // make the composition window undecorated without a title bar.
        Component client = clientComponent.get();
        if (client != null) {
            InputMethodRequests req = client.getInputMethodRequests();
            if (req != null && inputMethodContext.useBelowTheSpotInput()) {
                setCompositionAreaUndecorated(true);
            }
        }
    }
}
Also used : InputMethodRequests(java.awt.im.InputMethodRequests) Component(java.awt.Component)

Aggregations

InputMethodRequests (java.awt.im.InputMethodRequests)5 Dimension (java.awt.Dimension)2 Point (java.awt.Point)2 Component (java.awt.Component)1 FontMetrics (java.awt.FontMetrics)1 Graphics (java.awt.Graphics)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 InputMethodEvent (java.awt.event.InputMethodEvent)1 KeyEvent (java.awt.event.KeyEvent)1 FontRenderContext (java.awt.font.FontRenderContext)1 TextLayout (java.awt.font.TextLayout)1 Rectangle2D (java.awt.geom.Rectangle2D)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 AttributedString (java.text.AttributedString)1