Search in sources :

Example 26 with ComponentPeer

use of java.awt.peer.ComponentPeer 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 27 with ComponentPeer

use of java.awt.peer.ComponentPeer 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 28 with ComponentPeer

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

the class DefaultFocusTraversalPolicy method accept.

/**
     * Determines whether a Component is an acceptable choice as the new
     * focus owner. The Component must be visible, displayable, and enabled
     * to be accepted. If client code has explicitly set the focusability
     * of the Component by either overriding
     * <code>Component.isFocusTraversable()</code> or
     * <code>Component.isFocusable()</code>, or by calling
     * <code>Component.setFocusable()</code>, then the Component will be
     * accepted if and only if it is focusable. If, however, the Component is
     * relying on default focusability, then all Canvases, Labels, Panels,
     * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
     * rejected.
     *
     * @param aComponent the Component whose fitness as a focus owner is to
     *        be tested
     * @return <code>true</code> if aComponent meets the above requirements;
     *         <code>false</code> otherwise
     */
protected boolean accept(Component aComponent) {
    if (!(aComponent.isVisible() && aComponent.isDisplayable() && aComponent.isEnabled())) {
        return false;
    }
    // a lightweight Container does not.
    if (!(aComponent instanceof Window)) {
        for (Container enableTest = aComponent.getParent(); enableTest != null; enableTest = enableTest.getParent()) {
            if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
                return false;
            }
            if (enableTest instanceof Window) {
                break;
            }
        }
    }
    boolean focusable = aComponent.isFocusable();
    if (aComponent.isFocusTraversableOverridden()) {
        return focusable;
    }
    ComponentPeer peer = aComponent.getPeer();
    return (peer != null && peer.isFocusable());
}
Also used : ComponentPeer(java.awt.peer.ComponentPeer)

Example 29 with ComponentPeer

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

Example 30 with ComponentPeer

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

the class Component method setForeground.

/**
     * Sets the foreground color of this component.
     * @param c the color to become this component's
     *          foreground color; if this parameter is <code>null</code>
     *          then this component will inherit
     *          the foreground color of its parent
     * @see #getForeground
     * @since JDK1.0
     */
public void setForeground(Color c) {
    Color oldColor = foreground;
    ComponentPeer peer = this.peer;
    foreground = c;
    if (peer != null) {
        c = getForeground();
        if (c != null) {
            peer.setForeground(c);
        }
    }
    // This is a bound property, so report the change to
    // any registered listeners.  (Cheap if there are none.)
    firePropertyChange("foreground", oldColor, c);
}
Also used : ComponentPeer(java.awt.peer.ComponentPeer)

Aggregations

ComponentPeer (java.awt.peer.ComponentPeer)41 LightweightPeer (java.awt.peer.LightweightPeer)10 Component (java.awt.Component)5 Container (java.awt.Container)3 InputContext (java.awt.im.InputContext)2 ConstrainableGraphics (sun.awt.ConstrainableGraphics)2 Pointer (com.sun.jna.Pointer)1 BufferCapabilities (java.awt.BufferCapabilities)1 HeadlessException (java.awt.HeadlessException)1 AffineTransform (java.awt.geom.AffineTransform)1 ColorModel (java.awt.image.ColorModel)1 FramePeer (java.awt.peer.FramePeer)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 TooManyListenersException (java.util.TooManyListenersException)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