Search in sources :

Example 6 with InputContext

use of java.awt.im.InputContext in project intellij-community by JetBrains.

the class UIUtil method getCurrentKeyboardLayout.

// Experimental!!!
// It seems to work under Windows
@Nullable
public static String getCurrentKeyboardLayout() {
    InputContext instance = InputContext.getInstance();
    Class<? extends InputContext> instanceClass = instance.getClass();
    Class<?> superclass = instanceClass.getSuperclass();
    if (superclass.getName().equals("sun.awt.im.InputContext")) {
        try {
            Object inputMethodLocator = ReflectionUtil.getField(superclass, instance, null, "inputMethodLocator");
            Locale locale = ReflectionUtil.getField(inputMethodLocator.getClass(), inputMethodLocator, Locale.class, "locale");
            return locale.getLanguage().toUpperCase(Locale.getDefault());
        } catch (Exception ignored) {
        }
    }
    return null;
}
Also used : InputContext(java.awt.im.InputContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with InputContext

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

the class Component method dispatchEventImpl.

@SuppressWarnings("deprecation")
void dispatchEventImpl(AWTEvent e) {
    int id = e.getID();
    // Check that this component belongs to this app-context
    AppContext compContext = appContext;
    if (compContext != null && !compContext.equals(AppContext.getAppContext())) {
        if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
            eventLog.fine("Event " + e + " is being dispatched on the wrong AppContext");
        }
    }
    if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
        eventLog.finest("{0}", e);
    }
    /*
         * 0. Set timestamp and modifiers of current event.
         */
    if (!(e instanceof KeyEvent)) {
        // Timestamp of a key event is set later in DKFM.preDispatchKeyEvent(KeyEvent).
        EventQueue.setCurrentEventAndMostRecentTime(e);
    }
    if (e instanceof SunDropTargetEvent) {
        ((SunDropTargetEvent) e).dispatch();
        return;
    }
    if (!e.focusManagerIsDispatching) {
        // lightweight Component support
        if (e.isPosted) {
            e = KeyboardFocusManager.retargetFocusEvent(e);
            e.isPosted = true;
        }
        // and dispatching function
        if (KeyboardFocusManager.getCurrentKeyboardFocusManager().dispatchEvent(e)) {
            return;
        }
    }
    if ((e instanceof FocusEvent) && focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
        focusLog.finest("" + e);
    }
    // stops.
    if (id == MouseEvent.MOUSE_WHEEL && (!eventTypeEnabled(id)) && (peer != null && !peer.handlesWheelScrolling()) && (dispatchMouseWheelToAncestor((MouseWheelEvent) e))) {
        return;
    }
    /*
         * 2. Allow the Toolkit to pass this to AWTEventListeners.
         */
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    toolkit.notifyAWTEventListeners(e);
    /*
         * 3. If no one has consumed a key event, allow the
         *    KeyboardFocusManager to process it.
         */
    if (!e.isConsumed()) {
        if (e instanceof java.awt.event.KeyEvent) {
            KeyboardFocusManager.getCurrentKeyboardFocusManager().processKeyEvent(this, (KeyEvent) e);
            if (e.isConsumed()) {
                return;
            }
        }
    }
    /*
         * 4. Allow input methods to process the event
         */
    if (areInputMethodsEnabled()) {
        // and the input context also handles the Java composition window
        if (((e instanceof InputMethodEvent) && !(this instanceof CompositionArea)) || // c) isConsumed() is always true for semantic events.
        (e instanceof InputEvent) || (e instanceof FocusEvent)) {
            InputContext inputContext = getInputContext();
            if (inputContext != null) {
                inputContext.dispatchEvent(e);
                if (e.isConsumed()) {
                    if ((e instanceof FocusEvent) && focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
                        focusLog.finest("3579: Skipping " + e);
                    }
                    return;
                }
            }
        }
    } else {
        // the active/passive/peered clients loose focus.
        if (id == FocusEvent.FOCUS_GAINED) {
            InputContext inputContext = getInputContext();
            if (inputContext != null && inputContext instanceof sun.awt.im.InputContext) {
                ((sun.awt.im.InputContext) inputContext).disableNativeIM();
            }
        }
    }
    /*
         * 5. Pre-process any special events before delivery
         */
    switch(id) {
        case KeyEvent.KEY_PRESSED:
        case KeyEvent.KEY_RELEASED:
            Container p = (Container) ((this instanceof Container) ? this : parent);
            if (p != null) {
                p.preProcessKeyEvent((KeyEvent) e);
                if (e.isConsumed()) {
                    if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
                        focusLog.finest("Pre-process consumed event");
                    }
                    return;
                }
            }
            break;
        case WindowEvent.WINDOW_CLOSING:
            if (toolkit instanceof WindowClosingListener) {
                windowClosingException = ((WindowClosingListener) toolkit).windowClosingNotify((WindowEvent) e);
                if (checkWindowClosingException()) {
                    return;
                }
            }
            break;
        default:
            break;
    }
    /*
         * 6. Deliver event for normal processing
         */
    if (newEventsOnly) {
        //
        if (eventEnabled(e)) {
            processEvent(e);
        }
    } else if (id == MouseEvent.MOUSE_WHEEL) {
        // newEventsOnly will be false for a listenerless ScrollPane, but
        // MouseWheelEvents still need to be dispatched to it so scrolling
        // can be done.
        autoProcessMouseWheel((MouseWheelEvent) e);
    } else if (!(e instanceof MouseEvent && !postsOldMouseEvents())) {
        //
        // backward compatibility
        //
        Event olde = e.convertToOld();
        if (olde != null) {
            int key = olde.key;
            int modifiers = olde.modifiers;
            postEvent(olde);
            if (olde.isConsumed()) {
                e.consume();
            }
            //
            switch(olde.id) {
                case Event.KEY_PRESS:
                case Event.KEY_RELEASE:
                case Event.KEY_ACTION:
                case Event.KEY_ACTION_RELEASE:
                    if (olde.key != key) {
                        ((KeyEvent) e).setKeyChar(olde.getKeyEventChar());
                    }
                    if (olde.modifiers != modifiers) {
                        ((KeyEvent) e).setModifiers(olde.modifiers);
                    }
                    break;
                default:
                    break;
            }
        }
    }
    /*
         * 8. Special handling for 4061116 : Hook for browser to close modal
         *    dialogs.
         */
    if (id == WindowEvent.WINDOW_CLOSING && !e.isConsumed()) {
        if (toolkit instanceof WindowClosingListener) {
            windowClosingException = ((WindowClosingListener) toolkit).windowClosingDelivered((WindowEvent) e);
            if (checkWindowClosingException()) {
                return;
            }
        }
    }
    /*
         * 9. Allow the peer to process the event.
         * Except KeyEvents, they will be processed by peer after
         * all KeyEventPostProcessors
         * (see DefaultKeyboardFocusManager.dispatchKeyEvent())
         */
    if (!(e instanceof KeyEvent)) {
        ComponentPeer tpeer = peer;
        if (e instanceof FocusEvent && (tpeer == null || tpeer instanceof LightweightPeer)) {
            // if focus owner is lightweight then its native container
            // processes event
            Component source = (Component) e.getSource();
            if (source != null) {
                Container target = source.getNativeContainer();
                if (target != null) {
                    tpeer = target.getPeer();
                }
            }
        }
        if (tpeer != null) {
            tpeer.handleEvent(e);
        }
    }
}
Also used : SunDropTargetEvent(sun.awt.dnd.SunDropTargetEvent) WindowClosingListener(sun.awt.WindowClosingListener) AppContext(sun.awt.AppContext) InputContext(java.awt.im.InputContext) ComponentPeer(java.awt.peer.ComponentPeer) CausedFocusEvent(sun.awt.CausedFocusEvent) LightweightPeer(java.awt.peer.LightweightPeer) CompositionArea(sun.awt.im.CompositionArea) CausedFocusEvent(sun.awt.CausedFocusEvent) SunDropTargetEvent(sun.awt.dnd.SunDropTargetEvent) SunToolkit(sun.awt.SunToolkit)

Example 8 with InputContext

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

the class Component method removeNotify.

/**
     * Makes this <code>Component</code> undisplayable by destroying it native
     * screen resource.
     * <p>
     * This method is called by the toolkit internally and should
     * not be called directly by programs. Code overriding
     * this method should call <code>super.removeNotify</code> as
     * the first line of the overriding method.
     *
     * @see       #isDisplayable
     * @see       #addNotify
     * @since JDK1.0
     */
public void removeNotify() {
    KeyboardFocusManager.clearMostRecentFocusOwner(this);
    if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() == this) {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().setGlobalPermanentFocusOwner(null);
    }
    synchronized (getTreeLock()) {
        if (isFocusOwner() && KeyboardFocusManager.isAutoFocusTransferEnabledFor(this)) {
            transferFocus(true);
        }
        if (getContainer() != null && isAddNotifyComplete) {
            getContainer().decreaseComponentCount(this);
        }
        int npopups = (popups != null ? popups.size() : 0);
        for (int i = 0; i < npopups; i++) {
            PopupMenu popup = popups.elementAt(i);
            popup.removeNotify();
        }
        // before hiding peer.)
        if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) {
            InputContext inputContext = getInputContext();
            if (inputContext != null) {
                inputContext.removeNotify(this);
            }
        }
        ComponentPeer p = peer;
        if (p != null) {
            boolean isLightweight = isLightweight();
            if (bufferStrategy instanceof FlipBufferStrategy) {
                ((FlipBufferStrategy) bufferStrategy).destroyBuffers();
            }
            if (dropTarget != null)
                dropTarget.removeNotify(peer);
            // Hide peer first to stop system events such as cursor moves.
            if (visible) {
                p.setVisible(false);
            }
            // Stop peer updates.
            peer = null;
            peerFont = null;
            Toolkit.getEventQueue().removeSourceEvents(this, false);
            KeyboardFocusManager.getCurrentKeyboardFocusManager().discardKeyEvents(this);
            p.dispose();
            mixOnHiding(isLightweight);
            isAddNotifyComplete = false;
            // Nullifying compoundShape means that the component has normal shape
            // (or has no shape at all).
            this.compoundShape = null;
        }
        if (hierarchyListener != null || (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 || Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK)) {
            HierarchyEvent e = new HierarchyEvent(this, HierarchyEvent.HIERARCHY_CHANGED, this, parent, HierarchyEvent.DISPLAYABILITY_CHANGED | ((isRecursivelyVisible()) ? HierarchyEvent.SHOWING_CHANGED : 0));
            dispatchEvent(e);
        }
    }
}
Also used : InputContext(java.awt.im.InputContext) ComponentPeer(java.awt.peer.ComponentPeer)

Aggregations

InputContext (java.awt.im.InputContext)8 ComponentPeer (java.awt.peer.ComponentPeer)2 CausedFocusEvent (sun.awt.CausedFocusEvent)2 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 Frame (java.awt.Frame)1 KeyEvent (java.awt.event.KeyEvent)1 LightweightPeer (java.awt.peer.LightweightPeer)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JTextComponent (javax.swing.text.JTextComponent)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1 AppContext (sun.awt.AppContext)1 SunToolkit (sun.awt.SunToolkit)1 WindowClosingListener (sun.awt.WindowClosingListener)1 SunDropTargetEvent (sun.awt.dnd.SunDropTargetEvent)1 CompositionArea (sun.awt.im.CompositionArea)1