Search in sources :

Example 1 with CausedFocusEvent

use of sun.awt.CausedFocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManager method retargetFocusLost.

static FocusEvent retargetFocusLost(FocusEvent fe) {
    assert (fe.getID() == FocusEvent.FOCUS_LOST);
    Component currentFocusOwner = getCurrentKeyboardFocusManager().getGlobalFocusOwner();
    Component opposite = fe.getOppositeComponent();
    Component nativeOpposite = getHeavyweight(opposite);
    synchronized (heavyweightRequests) {
        HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
        if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) {
            if (currentFocusOwner != null) {
                // Call to KeyboardFocusManager.clearGlobalFocusOwner()
                heavyweightRequests.removeFirst();
                return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, false, null, CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
            }
        // Otherwise, fall through to failure case below
        } else if (opposite == null) {
            // Focus leaving application
            if (currentFocusOwner != null) {
                return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, true, null, CausedFocusEvent.Cause.ACTIVATION);
            } else {
                return fe;
            }
        } else if (hwFocusRequest != null && (nativeOpposite == hwFocusRequest.heavyweight || nativeOpposite == null && opposite == hwFocusRequest.getFirstLightweightRequest().component)) {
            if (currentFocusOwner == null) {
                return fe;
            }
            // Focus change as a result of a known call to requestFocus(),
            // or click on a peer focusable heavyweight Component.
            // If a focus transfer is made across top-levels, then the
            // FOCUS_LOST event is always temporary, and the FOCUS_GAINED
            // event is always permanent. Otherwise, the stored temporary
            // value is honored.
            LightweightFocusRequest lwFocusRequest = hwFocusRequest.lightweightRequests.getFirst();
            boolean temporary = isTemporary(opposite, currentFocusOwner) ? true : lwFocusRequest.temporary;
            return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, temporary, lwFocusRequest.component, lwFocusRequest.cause);
        } else if (focusedWindowChanged(opposite, currentFocusOwner)) {
            // But we know the opposite, we now it is temporary - dispatch the event.
            if (!fe.isTemporary() && currentFocusOwner != null) {
                // Create copy of the event with only difference in temporary parameter.
                fe = new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, true, opposite, CausedFocusEvent.Cause.ACTIVATION);
            }
            return fe;
        }
        return retargetUnexpectedFocusEvent(fe);
    }
// end synchronized(heavyweightRequests)
}
Also used : CausedFocusEvent(sun.awt.CausedFocusEvent)

Example 2 with CausedFocusEvent

use of sun.awt.CausedFocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManager method shouldNativelyFocusHeavyweight.

/**
     * Indicates whether the native implementation should proceed with a
     * pending, native focus request. Before changing the focus at the native
     * level, the AWT implementation should always call this function for
     * permission. This function will reject the request if a duplicate request
     * preceded it, or if the specified heavyweight Component already owns the
     * focus and no native focus changes are pending. Otherwise, the request
     * will be approved and the focus request list will be updated so that,
     * if necessary, the proper descendant will be focused when the
     * corresponding FOCUS_GAINED event on the heavyweight is received.
     *
     * An implementation must ensure that calls to this method and native
     * focus changes are atomic. If this is not guaranteed, then the ordering
     * of the focus request list may be incorrect, leading to errors in the
     * type-ahead mechanism. Typically this is accomplished by only calling
     * this function from the native event pumping thread, or by holding a
     * global, native lock during invocation.
     */
static int shouldNativelyFocusHeavyweight(Component heavyweight, Component descendant, boolean temporary, boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause) {
    if (log.isLoggable(PlatformLogger.Level.FINE)) {
        if (heavyweight == null) {
            log.fine("Assertion (heavyweight != null) failed");
        }
        if (time == 0) {
            log.fine("Assertion (time != 0) failed");
        }
    }
    if (descendant == null) {
        // Focus transfers from a lightweight child back to the
        // heavyweight Container should be treated like lightweight
        // focus transfers.
        descendant = heavyweight;
    }
    KeyboardFocusManager manager = getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant));
    KeyboardFocusManager thisManager = getCurrentKeyboardFocusManager();
    Component currentFocusOwner = thisManager.getGlobalFocusOwner();
    Component nativeFocusOwner = thisManager.getNativeFocusOwner();
    Window nativeFocusedWindow = thisManager.getNativeFocusedWindow();
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        focusLog.finer("SNFH for {0} in {1}", String.valueOf(descendant), String.valueOf(heavyweight));
    }
    if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
        focusLog.finest("0. Current focus owner {0}", String.valueOf(currentFocusOwner));
        focusLog.finest("0. Native focus owner {0}", String.valueOf(nativeFocusOwner));
        focusLog.finest("0. Native focused window {0}", String.valueOf(nativeFocusedWindow));
    }
    synchronized (heavyweightRequests) {
        HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
            focusLog.finest("Request {0}", String.valueOf(hwFocusRequest));
        }
        if (hwFocusRequest == null && heavyweight == nativeFocusOwner && heavyweight.getContainingWindow() == nativeFocusedWindow) {
            if (descendant == currentFocusOwner) {
                // Redundant request.
                if (focusLog.isLoggable(PlatformLogger.Level.FINEST))
                    focusLog.finest("1. SNFH_FAILURE for {0}", String.valueOf(descendant));
                return SNFH_FAILURE;
            }
            // 'heavyweight' owns the native focus and there are no pending
            // requests. 'heavyweight' must be a Container and
            // 'descendant' must not be the focus owner. Otherwise,
            // we would never have gotten this far.
            manager.enqueueKeyEvents(time, descendant);
            hwFocusRequest = new HeavyweightFocusRequest(heavyweight, descendant, temporary, cause);
            heavyweightRequests.add(hwFocusRequest);
            if (currentFocusOwner != null) {
                FocusEvent currentFocusOwnerEvent = new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, temporary, descendant, cause);
                // Fix 5028014. Rolled out.
                // SunToolkit.postPriorityEvent(currentFocusOwnerEvent);
                SunToolkit.postEvent(currentFocusOwner.appContext, currentFocusOwnerEvent);
            }
            FocusEvent newFocusOwnerEvent = new CausedFocusEvent(descendant, FocusEvent.FOCUS_GAINED, temporary, currentFocusOwner, cause);
            // Fix 5028014. Rolled out.
            // SunToolkit.postPriorityEvent(newFocusOwnerEvent);
            SunToolkit.postEvent(descendant.appContext, newFocusOwnerEvent);
            if (focusLog.isLoggable(PlatformLogger.Level.FINEST))
                focusLog.finest("2. SNFH_HANDLED for {0}", String.valueOf(descendant));
            return SNFH_SUCCESS_HANDLED;
        } else if (hwFocusRequest != null && hwFocusRequest.heavyweight == heavyweight) {
            // lightweight focus transfers.
            if (hwFocusRequest.addLightweightRequest(descendant, temporary, cause)) {
                manager.enqueueKeyEvents(time, descendant);
            }
            if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
                focusLog.finest("3. SNFH_HANDLED for lightweight" + descendant + " in " + heavyweight);
            }
            return SNFH_SUCCESS_HANDLED;
        } else {
            if (!focusedWindowChangeAllowed) {
                // acceptable value.
                if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) {
                    int size = heavyweightRequests.size();
                    hwFocusRequest = (HeavyweightFocusRequest) ((size >= 2) ? heavyweightRequests.get(size - 2) : null);
                }
                if (focusedWindowChanged(heavyweight, (hwFocusRequest != null) ? hwFocusRequest.heavyweight : nativeFocusedWindow)) {
                    if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
                        focusLog.finest("4. SNFH_FAILURE for " + descendant);
                    }
                    return SNFH_FAILURE;
                }
            }
            manager.enqueueKeyEvents(time, descendant);
            heavyweightRequests.add(new HeavyweightFocusRequest(heavyweight, descendant, temporary, cause));
            if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
                focusLog.finest("5. SNFH_PROCEED for " + descendant);
            }
            return SNFH_SUCCESS_PROCEED;
        }
    }
}
Also used : CausedFocusEvent(sun.awt.CausedFocusEvent) CausedFocusEvent(sun.awt.CausedFocusEvent) FocusEvent(java.awt.event.FocusEvent)

Example 3 with CausedFocusEvent

use of sun.awt.CausedFocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManager method processCurrentLightweightRequests.

static void processCurrentLightweightRequests() {
    KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
    LinkedList<LightweightFocusRequest> localLightweightRequests = null;
    Component globalFocusOwner = manager.getGlobalFocusOwner();
    if ((globalFocusOwner != null) && (globalFocusOwner.appContext != AppContext.getAppContext())) {
        // now and wait for a next event.
        return;
    }
    synchronized (heavyweightRequests) {
        if (currentLightweightRequests != null) {
            clearingCurrentLightweightRequests = true;
            disableRestoreFocus = true;
            localLightweightRequests = currentLightweightRequests;
            allowSyncFocusRequests = (localLightweightRequests.size() < 2);
            currentLightweightRequests = null;
        } else {
            // do nothing
            return;
        }
    }
    Throwable caughtEx = null;
    try {
        if (localLightweightRequests != null) {
            Component lastFocusOwner = null;
            Component currentFocusOwner = null;
            for (Iterator<KeyboardFocusManager.LightweightFocusRequest> iter = localLightweightRequests.iterator(); iter.hasNext(); ) {
                currentFocusOwner = manager.getGlobalFocusOwner();
                LightweightFocusRequest lwFocusRequest = iter.next();
                /*
                     * WARNING: This is based on DKFM's logic solely!
                     *
                     * We allow to trigger restoreFocus() in the dispatching process
                     * only if we have the last request to dispatch. If the last request
                     * fails, focus will be restored to either the component of the last
                     * previously succedded request, or to to the focus owner that was
                     * before this clearing process.
                     */
                if (!iter.hasNext()) {
                    disableRestoreFocus = false;
                }
                FocusEvent currentFocusOwnerEvent = null;
                /*
                     * We're not dispatching FOCUS_LOST while the current focus owner is null.
                     * But regardless of whether it's null or not, we're clearing ALL the local
                     * lw requests.
                     */
                if (currentFocusOwner != null) {
                    currentFocusOwnerEvent = new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, lwFocusRequest.temporary, lwFocusRequest.component, lwFocusRequest.cause);
                }
                FocusEvent newFocusOwnerEvent = new CausedFocusEvent(lwFocusRequest.component, FocusEvent.FOCUS_GAINED, lwFocusRequest.temporary, currentFocusOwner == null ? lastFocusOwner : currentFocusOwner, lwFocusRequest.cause);
                if (currentFocusOwner != null) {
                    ((AWTEvent) currentFocusOwnerEvent).isPosted = true;
                    caughtEx = dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent);
                }
                ((AWTEvent) newFocusOwnerEvent).isPosted = true;
                caughtEx = dispatchAndCatchException(caughtEx, lwFocusRequest.component, newFocusOwnerEvent);
                if (manager.getGlobalFocusOwner() == lwFocusRequest.component) {
                    lastFocusOwner = lwFocusRequest.component;
                }
            }
        }
    } finally {
        clearingCurrentLightweightRequests = false;
        disableRestoreFocus = false;
        localLightweightRequests = null;
        allowSyncFocusRequests = true;
    }
    if (caughtEx instanceof RuntimeException) {
        throw (RuntimeException) caughtEx;
    } else if (caughtEx instanceof Error) {
        throw (Error) caughtEx;
    }
}
Also used : CausedFocusEvent(sun.awt.CausedFocusEvent) CausedFocusEvent(sun.awt.CausedFocusEvent) FocusEvent(java.awt.event.FocusEvent)

Aggregations

CausedFocusEvent (sun.awt.CausedFocusEvent)3 FocusEvent (java.awt.event.FocusEvent)2