return typeAheadAssertions(newActiveWindow, we);
}
case FocusEvent.FOCUS_GAINED: {
FocusEvent fe = (FocusEvent)e;
CausedFocusEvent.Cause cause = (fe instanceof CausedFocusEvent) ?
((CausedFocusEvent)fe).getCause() : CausedFocusEvent.Cause.UNKNOWN;
Component oldFocusOwner = getGlobalFocusOwner();
Component newFocusOwner = fe.getComponent();
if (oldFocusOwner == newFocusOwner) {
if (focusLog.isLoggable(Level.FINE)){
focusLog.log(Level.FINE, "Skipping {0} because focus owner is the same",
new Object[] {String.valueOf(e)});
}
// We can't just drop the event - there could be
// type-ahead markers associated with it.
dequeueKeyEvents(-1, newFocusOwner);
break;
}
// If there exists a current focus owner, then notify it that
// it has lost focus.
if (oldFocusOwner != null) {
boolean isEventDispatched =
sendMessage(oldFocusOwner,
new CausedFocusEvent(oldFocusOwner,
FocusEvent.FOCUS_LOST,
fe.isTemporary(),
newFocusOwner, cause));
// Failed to dispatch, clear by ourselfves
if (!isEventDispatched) {
setGlobalFocusOwner(null);
if (!fe.isTemporary()) {
setGlobalPermanentFocusOwner(null);
}
}
}
// Because the native windowing system has a different notion
// of the current focus and activation states, it is possible
// that a Component outside of the focused Window receives a
// FOCUS_GAINED event. We synthesize a WINDOW_GAINED_FOCUS
// event in that case.
Component newFocusedWindow = newFocusOwner;
while (newFocusedWindow != null &&
!(newFocusedWindow instanceof Window)) {
newFocusedWindow = newFocusedWindow.parent;
}
Window currentFocusedWindow = getGlobalFocusedWindow();
if (newFocusedWindow != null &&
newFocusedWindow != currentFocusedWindow)
{
sendMessage(newFocusedWindow,
new WindowEvent((Window)newFocusedWindow,
WindowEvent.WINDOW_GAINED_FOCUS,
currentFocusedWindow));
if (newFocusedWindow != getGlobalFocusedWindow()) {
// Focus change was rejected. Will happen if
// newFocusedWindow is not a focusable Window.
// Need to recover type-ahead, but don't bother
// restoring focus. That was done by the
// WINDOW_GAINED_FOCUS handler
dequeueKeyEvents(-1, newFocusOwner);
break;
}
}
setGlobalFocusOwner(newFocusOwner);
if (newFocusOwner != getGlobalFocusOwner()) {
// Focus change was rejected. Will happen if
// newFocusOwner is not focus traversable.
dequeueKeyEvents(-1, newFocusOwner);
if (! disableRestoreFocus ){
restoreFocus(fe, (Window)newFocusedWindow);
}
break;
}
if (!fe.isTemporary()) {
setGlobalPermanentFocusOwner(newFocusOwner);
if (newFocusOwner != getGlobalPermanentFocusOwner()) {
// Focus change was rejected. Unlikely, but possible.
dequeueKeyEvents(-1, newFocusOwner);
if (! disableRestoreFocus ){
restoreFocus(fe, (Window)newFocusedWindow);
}
break;
}
}
setNativeFocusOwner(getHeavyweight(newFocusOwner));
Component realOppositeComponent = this.realOppositeComponent.get();
if (realOppositeComponent != null &&
realOppositeComponent != fe.getOppositeComponent()) {
fe = new CausedFocusEvent(newFocusOwner,
FocusEvent.FOCUS_GAINED,
fe.isTemporary(),
realOppositeComponent, cause);
((AWTEvent) fe).isPosted = true;
}
return typeAheadAssertions(newFocusOwner, fe);
}
case FocusEvent.FOCUS_LOST: {
FocusEvent fe = (FocusEvent)e;
Component currentFocusOwner = getGlobalFocusOwner();
if (currentFocusOwner == null) {
if (focusLog.isLoggable(Level.FINE)) {
focusLog.log(Level.FINE, "Skipping {0} because focus owner is null",
new Object[] {String.valueOf(e)});
}
break;
}
// Ignore cases where a Component loses focus to itself.
// If we make a mistake because of retargeting, then the
// FOCUS_GAINED handler will correct it.
if (currentFocusOwner == fe.getOppositeComponent()) {
if (focusLog.isLoggable(Level.FINE)) {
focusLog.log(Level.FINE, "Skipping {0} because current focus owner is equal to opposite",
new Object[] {String.valueOf(e)});
}
break;
}
setGlobalFocusOwner(null);
if (getGlobalFocusOwner() != null) {
// Focus change was rejected. Unlikely, but possible.
restoreFocus(currentFocusOwner, true);
break;
}
if (!fe.isTemporary()) {
setGlobalPermanentFocusOwner(null);
if (getGlobalPermanentFocusOwner() != null) {
// Focus change was rejected. Unlikely, but possible.
restoreFocus(currentFocusOwner, true);
break;
}
} else {
Window owningWindow = currentFocusOwner.getContainingWindow();
if (owningWindow != null) {
owningWindow.setTemporaryLostComponent(currentFocusOwner);
}
}
setNativeFocusOwner(null);
fe.setSource(currentFocusOwner);
realOppositeComponent = (fe.getOppositeComponent() != null)
? new WeakReference<Component>(currentFocusOwner)
: NULL_COMPONENT_WR;
return typeAheadAssertions(currentFocusOwner, fe);
}