Java Code Examples for java.awt.event.FocusEvent#getOppositeComponent()

The following examples show how to use java.awt.event.FocusEvent#getOppositeComponent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyboardFocusManager.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new FocusEvent(source, fe.getID(), temporary, opposite,
                                    FocusEvent.Cause.UNEXPECTED);
    }
}
 
Example 2
Source File: KeyboardFocusManager.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 3
Source File: FocusDebuggerAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void eventDispatched(AWTEvent event) {
  if (event instanceof FocusEvent) {
    FocusEvent focusEvent = (FocusEvent)event;
    Component fromComponent = focusEvent.getComponent();
    Component oppositeComponent = focusEvent.getOppositeComponent();

    paintFocusBorders(true);

    switch (event.getID()) {
      case FocusEvent.FOCUS_GAINED:
        myCurrent = fromComponent;
        myPrevious = oppositeComponent;
        myTemporary = focusEvent.isTemporary();
        break;
      case FocusEvent.FOCUS_LOST:
        myTemporary = focusEvent.isTemporary();
      default:
        break;
    }
  }
}
 
Example 4
Source File: KeyboardFocusManager.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 5
Source File: KeyboardFocusManager.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
    synchronized (heavyweightRequests) {
        // Any other case represents a failure condition which we did
        // not expect. We need to clearFocusRequestList() and patch up
        // the event as best as possible.

        if (removeFirstRequest()) {
            return (FocusEvent)retargetFocusEvent(fe);
        }

        Component source = fe.getComponent();
        Component opposite = fe.getOppositeComponent();
        boolean temporary = false;
        if (fe.getID() == FocusEvent.FOCUS_LOST &&
            (opposite == null || isTemporary(opposite, source)))
        {
            temporary = true;
        }
        return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
                                    CausedFocusEvent.Cause.NATIVE_SYSTEM);
    }
}
 
Example 6
Source File: BaseTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void focusGained(FocusEvent fe) {
    Component c = fe.getOppositeComponent();

    /*
    //handy for debugging
    System.out.println("Focus gained to " + (fe.getComponent().getName() == null ? fe.getComponent().getClass().getName() : fe.getComponent().getName()) + " temporary: " + fe.isTemporary()
    + " from " + (fe.getOppositeComponent() == null ? "null" :
        (fe.getOppositeComponent().getName() == null ? fe.getOppositeComponent().getClass().getName() : fe.getOppositeComponent().getName()))
    );
     */
    PropUtils.log(BaseTable.class, fe);

    if (!isKnownComponent(c)) {
        fireChange();
    }

    if (!inEditRequest() && !inEditorRemoveRequest() && (fe.getComponent() == this)) {
        //            System.out.println("Painting due to focus gain " + fe.getComponent());
        //            repaint(0,0,getWidth(),getHeight());
        paintSelectionRow();
    }
}
 
Example 7
Source File: EditorContextDispatcher.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void focusLost(FocusEvent e) {
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("Focus Lost from "+e.getComponent());
        logger.fine("  opposite component is: "+e.getOppositeComponent());
    }
    e.getComponent().removeFocusListener(this);
    synchronized (EditorContextDispatcher.this) {
        if (e.getOppositeComponent() == currentTextComponent.get()) {
            if (logger.isLoggable(Level.FINE)) {
                logger.fine("Opposite is current. No update.");
            }
            return ;
        }
    }
    update(true);
}
 
Example 8
Source File: DarkTextUI.java    From darklaf with MIT License 5 votes vote down vote up
@Override
public void focusLost(final FocusEvent e) {
    Caret caret = editor.getCaret();
    JPopupMenu popupMenu = editor.getComponentPopupMenu();
    Component other = e.getOppositeComponent();
    MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath();
    if (popupMenu != null && other != null && SwingUtilities.isDescendingFrom(popupMenu, other)
        || path != null && path.length > 0 && path[0] == popupMenu) return;
    if (caret instanceof DarkCaret) {
        ((DarkCaret) caret).setPaintSelectionHighlight(false);
    }
    editor.repaint();
}
 
Example 9
Source File: FancyDropDownButton.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void focusLost(FocusEvent e) {
	if (e.getOppositeComponent() != arrowButton && e.getOppositeComponent() != getPopupMenu()) {
		hovered = false;
		setBorderPainted(false);
		arrowButton.setBorderPainted(false);
		setForeground(NORMAL_TEXTCOLOR);
		repaint();
	}
}
 
Example 10
Source File: DefaultKeyboardFocusManager.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private void restoreFocus(FocusEvent fe, Window newFocusedWindow) {
    Component realOppositeComponent = this.realOppositeComponentWR.get();
    Component vetoedComponent = fe.getComponent();

    if (newFocusedWindow != null && restoreFocus(newFocusedWindow,
                                                 vetoedComponent, false))
    {
    } else if (realOppositeComponent != null &&
               doRestoreFocus(realOppositeComponent, vetoedComponent, false)) {
    } else if (fe.getOppositeComponent() != null &&
               doRestoreFocus(fe.getOppositeComponent(), vetoedComponent, false)) {
    } else {
        clearGlobalFocusOwnerPriv();
    }
}
 
Example 11
Source File: KeyboardFocusManager.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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:
            /* do nothing */
    }
    return event;
}
 
Example 12
Source File: FocusWatcher.java    From consulo with Apache License 2.0 4 votes vote down vote up
public final void focusLost(final FocusEvent e){
  Component component = e.getOppositeComponent();
  if(component != null && !SwingUtilities.isDescendingFrom(component, myTopComponent)){
    focusLostImpl(e);
  }
}
 
Example 13
Source File: KeyboardFocusManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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:
            /* do nothing */
    }
    return event;
}
 
Example 14
Source File: KeyboardFocusManager.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
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:
            /* do nothing */
    }
    return event;
}
 
Example 15
Source File: KeyboardFocusManager.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
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:
            /* do nothing */
    }
    return event;
}
 
Example 16
Source File: KeyboardFocusManager.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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:
            /* do nothing */
    }
    return event;
}
 
Example 17
Source File: KeyboardFocusManager.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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:
            /* do nothing */
    }
    return event;
}
 
Example 18
Source File: KeyboardFocusManager.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static FocusEvent retargetFocusGained(FocusEvent fe) {
    assert (fe.getID() == FocusEvent.FOCUS_GAINED);

    Component currentFocusOwner = getCurrentKeyboardFocusManager().
        getGlobalFocusOwner();
    Component source = fe.getComponent();
    Component opposite = fe.getOppositeComponent();
    Component nativeSource = getHeavyweight(source);

    synchronized (heavyweightRequests) {
        HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();

        if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
        {
            return retargetUnexpectedFocusEvent(fe);
        }

        if (source != null && nativeSource == null && hwFocusRequest != null) {
            // if source w/o peer and
            // if source is equal to first lightweight
            // then we should correct source and nativeSource
            if (source == hwFocusRequest.getFirstLightweightRequest().component)
            {
                source = hwFocusRequest.heavyweight;
                nativeSource = source; // source is heavuweight itself
            }
        }
        if (hwFocusRequest != null &&
            nativeSource == hwFocusRequest.heavyweight)
        {
            // Focus change as a result of a known call to requestFocus(),
            // or known click on a peer focusable heavyweight Component.

            heavyweightRequests.removeFirst();

            LightweightFocusRequest lwFocusRequest =
                hwFocusRequest.lightweightRequests.removeFirst();

            Component newSource = lwFocusRequest.component;
            if (currentFocusOwner != null) {
                /*
                 * Since we receive FOCUS_GAINED when current focus
                 * owner is not null, correcponding FOCUS_LOST is supposed
                 * to be lost.  And so,  we keep new focus owner
                 * to determine synthetic FOCUS_LOST event which will be
                 * generated by KeyboardFocusManager for this FOCUS_GAINED.
                 *
                 * This code based on knowledge of
                 * DefaultKeyboardFocusManager's implementation and might
                 * be not applicable for another KeyboardFocusManager.
                 */
                newFocusOwner = newSource;
            }

            boolean temporary = (opposite == null ||
                                 isTemporary(newSource, opposite))
                    ? false
                    : lwFocusRequest.temporary;

            if (hwFocusRequest.lightweightRequests.size() > 0) {
                currentLightweightRequests =
                    hwFocusRequest.lightweightRequests;
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            processCurrentLightweightRequests();
                        }
                    });
            }

            // 'opposite' will be fixed by
            // DefaultKeyboardFocusManager.realOppositeComponent
            return new CausedFocusEvent(newSource,
                                  FocusEvent.FOCUS_GAINED, temporary,
                                  opposite, lwFocusRequest.cause);
        }

        if (currentFocusOwner != null
            && currentFocusOwner.getContainingWindow() == source
            && (hwFocusRequest == null || source != hwFocusRequest.heavyweight))
        {
            // Special case for FOCUS_GAINED in top-levels
            // If it arrives as the result of activation we should skip it
            // This event will not have appropriate request record and
            // on arrival there will be already some focus owner set.
            return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false,
                                        null, CausedFocusEvent.Cause.ACTIVATION);
        }

        return retargetUnexpectedFocusEvent(fe);
    } // end synchronized(heavyweightRequests)
}
 
Example 19
Source File: CausedFocusEvent.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Retargets the original focus event to the new target.  If the
 * original focus event is CausedFocusEvent, it remains such and
 * cause is copied.  Otherwise, new CausedFocusEvent is created,
 * with cause as RETARGETED.
 * @return retargeted event, or null if e is null
 */
public static FocusEvent retarget(FocusEvent e, Component newSource) {
    if (e == null) return null;

    return new CausedFocusEvent(newSource, e.getID(), e.isTemporary(), e.getOppositeComponent(),
                                (e instanceof CausedFocusEvent) ? ((CausedFocusEvent)e).getCause() : Cause.RETARGETED);
}
 
Example 20
Source File: CausedFocusEvent.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Retargets the original focus event to the new target.  If the
 * original focus event is CausedFocusEvent, it remains such and
 * cause is copied.  Otherwise, new CausedFocusEvent is created,
 * with cause as RETARGETED.
 * @return retargeted event, or null if e is null
 */
public static FocusEvent retarget(FocusEvent e, Component newSource) {
    if (e == null) return null;

    return new CausedFocusEvent(newSource, e.getID(), e.isTemporary(), e.getOppositeComponent(),
                                (e instanceof CausedFocusEvent) ? ((CausedFocusEvent)e).getCause() : Cause.RETARGETED);
}