Search in sources :

Example 6 with LightweightPeer

use of java.awt.peer.LightweightPeer in project jdk8u_jdk by JetBrains.

the class Component method getGraphics.

/**
     * Creates a graphics context for this component. This method will
     * return <code>null</code> if this component is currently not
     * displayable.
     * @return a graphics context for this component, or <code>null</code>
     *             if it has none
     * @see       #paint
     * @since     JDK1.0
     */
public Graphics getGraphics() {
    if (peer instanceof LightweightPeer) {
        // to the parent.
        if (parent == null)
            return null;
        Graphics g = parent.getGraphics();
        if (g == null)
            return null;
        if (g instanceof ConstrainableGraphics) {
            ((ConstrainableGraphics) g).constrain(x, y, width, height);
        } else {
            g.translate(x, y);
            g.setClip(0, 0, width, height);
        }
        g.setFont(getFont());
        return g;
    } else {
        ComponentPeer peer = this.peer;
        return (peer != null) ? peer.getGraphics() : null;
    }
}
Also used : LightweightPeer(java.awt.peer.LightweightPeer) ConstrainableGraphics(sun.awt.ConstrainableGraphics) ConstrainableGraphics(sun.awt.ConstrainableGraphics) ComponentPeer(java.awt.peer.ComponentPeer)

Example 7 with LightweightPeer

use of java.awt.peer.LightweightPeer 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 LightweightPeer

use of java.awt.peer.LightweightPeer in project jdk8u_jdk by JetBrains.

the class Component method requestFocusHelper.

final boolean requestFocusHelper(boolean temporary, boolean focusedWindowChangeAllowed, CausedFocusEvent.Cause cause) {
    // 1) Check if the event being dispatched is a system-generated mouse event.
    AWTEvent currentEvent = EventQueue.getCurrentEvent();
    if (currentEvent instanceof MouseEvent && SunToolkit.isSystemGenerated(currentEvent)) {
        // 2) Sanity check: if the mouse event component source belongs to the same containing window.
        Component source = ((MouseEvent) currentEvent).getComponent();
        if (source == null || source.getContainingWindow() == getContainingWindow()) {
            focusLog.finest("requesting focus by mouse event \"in window\"");
            // If both the conditions are fulfilled the focus request should be strictly
            // bounded by the toplevel window. It's assumed that the mouse event activates
            // the window (if it wasn't active) and this makes it possible for a focus
            // request with a strong in-window requirement to change focus in the bounds
            // of the toplevel. If, by any means, due to asynchronous nature of the event
            // dispatching mechanism, the window happens to be natively inactive by the time
            // this focus request is eventually handled, it should not re-activate the
            // toplevel. Otherwise the result may not meet user expectations. See 6981400.
            focusedWindowChangeAllowed = false;
        }
    }
    if (!isRequestFocusAccepted(temporary, focusedWindowChangeAllowed, cause)) {
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
            focusLog.finest("requestFocus is not accepted");
        }
        return false;
    }
    // Update most-recent map
    KeyboardFocusManager.setMostRecentFocusOwner(this);
    Component window = this;
    while ((window != null) && !(window instanceof Window)) {
        if (!window.isVisible()) {
            if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
                focusLog.finest("component is recurively invisible");
            }
            return false;
        }
        window = window.parent;
    }
    ComponentPeer peer = this.peer;
    Component heavyweight = (peer instanceof LightweightPeer) ? getNativeContainer() : this;
    if (heavyweight == null || !heavyweight.isVisible()) {
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
            focusLog.finest("Component is not a part of visible hierarchy");
        }
        return false;
    }
    peer = heavyweight.peer;
    if (peer == null) {
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
            focusLog.finest("Peer is null");
        }
        return false;
    }
    // Focus this Component
    long time = 0;
    if (EventQueue.isDispatchThread()) {
        time = Toolkit.getEventQueue().getMostRecentKeyEventTime();
    } else {
        // A focus request made from outside EDT should not be associated with any event
        // and so its time stamp is simply set to the current time.
        time = System.currentTimeMillis();
    }
    boolean success = peer.requestFocus(this, temporary, focusedWindowChangeAllowed, time, cause);
    if (!success) {
        KeyboardFocusManager.getCurrentKeyboardFocusManager(appContext).dequeueKeyEvents(time, this);
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
            focusLog.finest("Peer request failed");
        }
    } else {
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
            focusLog.finest("Pass for " + this);
        }
    }
    return success;
}
Also used : LightweightPeer(java.awt.peer.LightweightPeer) ComponentPeer(java.awt.peer.ComponentPeer)

Example 9 with LightweightPeer

use of java.awt.peer.LightweightPeer in project jdk8u_jdk by JetBrains.

the class DefaultKeyboardFocusManager method dispatchKeyEvent.

/**
     * Called by <code>dispatchEvent</code> if no other
     * KeyEventDispatcher in the dispatcher chain dispatched the KeyEvent, or
     * if no other KeyEventDispatchers are registered. If the event has not
     * been consumed, its target is enabled, and the focus owner is not null,
     * this method dispatches the event to its target. This method will also
     * subsequently dispatch the event to all registered
     * KeyEventPostProcessors. After all this operations are finished,
     * the event is passed to peers for processing.
     * <p>
     * In all cases, this method returns <code>true</code>, since
     * DefaultKeyboardFocusManager is designed so that neither
     * <code>dispatchEvent</code>, nor the AWT event dispatcher, should take
     * further action on the event in any situation.
     *
     * @param e the KeyEvent to be dispatched
     * @return <code>true</code>
     * @see Component#dispatchEvent
     */
public boolean dispatchKeyEvent(KeyEvent e) {
    Component focusOwner = (((AWTEvent) e).isPosted) ? getFocusOwner() : e.getComponent();
    if (focusOwner != null && focusOwner.isShowing() && focusOwner.canBeFocusOwner()) {
        if (!e.isConsumed()) {
            Component comp = e.getComponent();
            if (comp != null && comp.isEnabled()) {
                redispatchEvent(comp, e);
            }
        }
    }
    boolean stopPostProcessing = false;
    java.util.List<KeyEventPostProcessor> processors = getKeyEventPostProcessors();
    if (processors != null) {
        for (java.util.Iterator<KeyEventPostProcessor> iter = processors.iterator(); !stopPostProcessing && iter.hasNext(); ) {
            stopPostProcessing = iter.next().postProcessKeyEvent(e);
        }
    }
    if (!stopPostProcessing) {
        postProcessKeyEvent(e);
    }
    // Allow the peer to process KeyEvent
    Component source = e.getComponent();
    ComponentPeer peer = source.getPeer();
    if (peer == null || peer instanceof LightweightPeer) {
        // if focus owner is lightweight then its native container
        // processes event
        Container target = source.getNativeContainer();
        if (target != null) {
            peer = target.getPeer();
        }
    }
    if (peer != null) {
        peer.handleEvent(e);
    }
    return true;
}
Also used : LightweightPeer(java.awt.peer.LightweightPeer) ComponentPeer(java.awt.peer.ComponentPeer)

Example 10 with LightweightPeer

use of java.awt.peer.LightweightPeer in project jdk8u_jdk by JetBrains.

the class Component method addNotify.

/**
     * Makes this <code>Component</code> displayable by connecting it to a
     * native screen resource.
     * This method is called internally by the toolkit and should
     * not be called directly by programs.
     * <p>
     * This method changes layout-related information, and therefore,
     * invalidates the component hierarchy.
     *
     * @see       #isDisplayable
     * @see       #removeNotify
     * @see #invalidate
     * @since JDK1.0
     */
public void addNotify() {
    synchronized (getTreeLock()) {
        ComponentPeer peer = this.peer;
        if (peer == null || peer instanceof LightweightPeer) {
            if (peer == null) {
                // Update both the Component's peer variable and the local
                // variable we use for thread safety.
                this.peer = peer = getToolkit().createComponent(this);
            }
            // be enabled.
            if (parent != null) {
                long mask = 0;
                if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
                    mask |= AWTEvent.MOUSE_EVENT_MASK;
                }
                if ((mouseMotionListener != null) || ((eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0)) {
                    mask |= AWTEvent.MOUSE_MOTION_EVENT_MASK;
                }
                if ((mouseWheelListener != null) || ((eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0)) {
                    mask |= AWTEvent.MOUSE_WHEEL_EVENT_MASK;
                }
                if (focusListener != null || (eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0) {
                    mask |= AWTEvent.FOCUS_EVENT_MASK;
                }
                if (keyListener != null || (eventMask & AWTEvent.KEY_EVENT_MASK) != 0) {
                    mask |= AWTEvent.KEY_EVENT_MASK;
                }
                if (mask != 0) {
                    parent.proxyEnableEvents(mask);
                }
            }
        } else {
            // It's native. If the parent is lightweight it will need some
            // help.
            Container parent = getContainer();
            if (parent != null && parent.isLightweight()) {
                relocateComponent();
                if (!parent.isRecursivelyVisibleUpToHeavyweightContainer()) {
                    peer.setVisible(false);
                }
            }
        }
        invalidate();
        int npopups = (popups != null ? popups.size() : 0);
        for (int i = 0; i < npopups; i++) {
            PopupMenu popup = popups.elementAt(i);
            popup.addNotify();
        }
        if (dropTarget != null)
            dropTarget.addNotify(peer);
        peerFont = getFont();
        if (getContainer() != null && !isAddNotifyComplete) {
            getContainer().increaseComponentCount(this);
        }
        // Update stacking order
        updateZOrder();
        if (!isAddNotifyComplete) {
            mixOnShowing();
        }
        isAddNotifyComplete = true;
        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 : LightweightPeer(java.awt.peer.LightweightPeer) ComponentPeer(java.awt.peer.ComponentPeer)

Aggregations

LightweightPeer (java.awt.peer.LightweightPeer)11 ComponentPeer (java.awt.peer.ComponentPeer)10 ConstrainableGraphics (sun.awt.ConstrainableGraphics)2 Component (java.awt.Component)1 DropTargetPeer (java.awt.dnd.peer.DropTargetPeer)1 InputContext (java.awt.im.InputContext)1 AppContext (sun.awt.AppContext)1 CausedFocusEvent (sun.awt.CausedFocusEvent)1 SunToolkit (sun.awt.SunToolkit)1 WindowClosingListener (sun.awt.WindowClosingListener)1 SunDropTargetEvent (sun.awt.dnd.SunDropTargetEvent)1 CompositionArea (sun.awt.im.CompositionArea)1