Search in sources :

Example 21 with FocusEvent

use of java.awt.event.FocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManagerPeerImpl method deliverFocus.

/*
     * Posts proper lost/gain focus events to the event queue.
     */
@SuppressWarnings("deprecation")
public static boolean deliverFocus(Component lightweightChild, Component target, boolean temporary, boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause, // provided by the descendant peers
Component currentFocusOwner) {
    if (lightweightChild == null) {
        lightweightChild = target;
    }
    Component currentOwner = currentFocusOwner;
    if (currentOwner != null && currentOwner.getPeer() == null) {
        currentOwner = null;
    }
    if (currentOwner != null) {
        FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST, false, lightweightChild, cause);
        if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
            focusLog.finer("Posting focus event: " + fl);
        }
        SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);
    }
    FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED, false, currentOwner, cause);
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        focusLog.finer("Posting focus event: " + fg);
    }
    SunToolkit.postEvent(SunToolkit.targetToAppContext(lightweightChild), fg);
    return true;
}
Also used : Component(java.awt.Component) FocusEvent(java.awt.event.FocusEvent)

Example 22 with FocusEvent

use of java.awt.event.FocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManager method processSynchronousLightweightTransfer.

static boolean processSynchronousLightweightTransfer(Component heavyweight, Component descendant, boolean temporary, boolean focusedWindowChangeAllowed, long time) {
    Window parentWindow = SunToolkit.getContainingWindow(heavyweight);
    if (parentWindow == null || !parentWindow.syncLWRequests) {
        return false;
    }
    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));
    FocusEvent currentFocusOwnerEvent = null;
    FocusEvent newFocusOwnerEvent = null;
    Component currentFocusOwner = manager.getGlobalFocusOwner();
    synchronized (heavyweightRequests) {
        HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
        if (hwFocusRequest == null && heavyweight == manager.getNativeFocusOwner() && allowSyncFocusRequests) {
            if (descendant == currentFocusOwner) {
                // Redundant request.
                return true;
            }
            // '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, CausedFocusEvent.Cause.UNKNOWN);
            heavyweightRequests.add(hwFocusRequest);
            if (currentFocusOwner != null) {
                currentFocusOwnerEvent = new FocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST, temporary, descendant);
            }
            newFocusOwnerEvent = new FocusEvent(descendant, FocusEvent.FOCUS_GAINED, temporary, currentFocusOwner);
        }
    }
    boolean result = false;
    final boolean clearing = clearingCurrentLightweightRequests;
    Throwable caughtEx = null;
    try {
        clearingCurrentLightweightRequests = false;
        synchronized (Component.LOCK) {
            if (currentFocusOwnerEvent != null && currentFocusOwner != null) {
                ((AWTEvent) currentFocusOwnerEvent).isPosted = true;
                caughtEx = dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent);
                result = true;
            }
            if (newFocusOwnerEvent != null && descendant != null) {
                ((AWTEvent) newFocusOwnerEvent).isPosted = true;
                caughtEx = dispatchAndCatchException(caughtEx, descendant, newFocusOwnerEvent);
                result = true;
            }
        }
    } finally {
        clearingCurrentLightweightRequests = clearing;
    }
    if (caughtEx instanceof RuntimeException) {
        throw (RuntimeException) caughtEx;
    } else if (caughtEx instanceof Error) {
        throw (Error) caughtEx;
    }
    return result;
}
Also used : CausedFocusEvent(sun.awt.CausedFocusEvent) FocusEvent(java.awt.event.FocusEvent)

Example 23 with FocusEvent

use of java.awt.event.FocusEvent 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 24 with FocusEvent

use of java.awt.event.FocusEvent in project jdk8u_jdk by JetBrains.

the class KeyboardFocusManager method retargetFocusEvent.

static AWTEvent retargetFocusEvent(AWTEvent event) {
    if (clearingCurrentLightweightRequests) {
        return event;
    }
    KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
        if (event instanceof FocusEvent || event instanceof WindowEvent) {
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
        if (focusLog.isLoggable(PlatformLogger.Level.FINER) && event instanceof KeyEvent) {
            focusLog.finer("    focus owner is {0}", String.valueOf(manager.getGlobalFocusOwner()));
            focusLog.finer(">>> {0}", String.valueOf(event));
        }
    }
    synchronized (heavyweightRequests) {
        /*
             * This code handles FOCUS_LOST event which is generated by
             * DefaultKeyboardFocusManager for FOCUS_GAINED.
             *
             * This code based on knowledge of DefaultKeyboardFocusManager's
             * implementation and might be not applicable for another
             * KeyboardFocusManager.
             *
             * Fix for 4472032
             */
        if (newFocusOwner != null && event.getID() == FocusEvent.FOCUS_LOST) {
            FocusEvent fe = (FocusEvent) event;
            if (manager.getGlobalFocusOwner() == fe.getComponent() && fe.getOppositeComponent() == newFocusOwner) {
                newFocusOwner = null;
                return event;
            }
        }
    }
    processCurrentLightweightRequests();
    switch(event.getID()) {
        case FocusEvent.FOCUS_GAINED:
            {
                event = retargetFocusGained((FocusEvent) event);
                break;
            }
        case FocusEvent.FOCUS_LOST:
            {
                event = retargetFocusLost((FocusEvent) event);
                break;
            }
        default:
    }
    return event;
}
Also used : KeyEvent(java.awt.event.KeyEvent) WindowEvent(java.awt.event.WindowEvent) CausedFocusEvent(sun.awt.CausedFocusEvent) FocusEvent(java.awt.event.FocusEvent)

Example 25 with FocusEvent

use of java.awt.event.FocusEvent in project jgnash by ccavanaugh.

the class JTextFieldEx method installFocusListener.

/*public JTextFieldEx(final String text, final int columns) {
        super(text, columns);
        installFocusListener();
    }

    public JTextFieldEx(final Document doc, final String text, final int columns) {
        super(doc, text, columns);
        installFocusListener();
    }*/
private void installFocusListener() {
    FocusListener focusListener = new FocusAdapter() {

        @Override
        public void focusGained(final FocusEvent e) {
            EventQueue.invokeLater(() -> {
                if (isEditable() && JTextFieldEx.select) {
                    selectAll();
                }
            });
        }
    };
    addFocusListener(focusListener);
}
Also used : FocusAdapter(java.awt.event.FocusAdapter) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Aggregations

FocusEvent (java.awt.event.FocusEvent)68 FocusListener (java.awt.event.FocusListener)32 FocusAdapter (java.awt.event.FocusAdapter)26 ActionEvent (java.awt.event.ActionEvent)15 ActionListener (java.awt.event.ActionListener)14 JLabel (javax.swing.JLabel)12 Dimension (java.awt.Dimension)8 KeyEvent (java.awt.event.KeyEvent)8 JButton (javax.swing.JButton)8 JPanel (javax.swing.JPanel)8 JTextField (javax.swing.JTextField)8 Border (javax.swing.border.Border)8 UIResource (javax.swing.plaf.UIResource)6 GridBagConstraints (java.awt.GridBagConstraints)5 GridBagLayout (java.awt.GridBagLayout)5 Insets (java.awt.Insets)5 KeyAdapter (java.awt.event.KeyAdapter)5 MouseEvent (java.awt.event.MouseEvent)5 JComboBox (javax.swing.JComboBox)5 Component (java.awt.Component)4