KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > DefaultKeyboardFocusManager


1 /*
2  * @(#)DefaultKeyboardFocusManager.java 1.36 07/03/09
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.event.FocusEvent JavaDoc;
10 import java.awt.event.KeyEvent JavaDoc;
11 import java.awt.event.WindowEvent JavaDoc;
12 import java.awt.peer.ComponentPeer;
13 import java.awt.peer.LightweightPeer;
14 import java.beans.PropertyChangeListener JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.ListIterator JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import java.util.logging.*;
21
22 import sun.awt.AppContext;
23 import sun.awt.SunToolkit;
24
25 /**
26  * The default KeyboardFocusManager for AWT applications. Focus traversal is
27  * done in response to a Component's focus traversal keys, and using a
28  * Container's FocusTraversalPolicy.
29  * <p>
30  * Please see
31  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
32  * How to Use the Focus Subsystem</a>,
33  * a section in <em>The Java Tutorial</em>, and the
34  * <a HREF="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
35  * for more information.
36  *
37  * @author David Mendenhall
38  * @version 1.36, 03/09/07
39  *
40  * @see FocusTraversalPolicy
41  * @see Component#setFocusTraversalKeys
42  * @see Component#getFocusTraversalKeys
43  * @since 1.4
44  */

45 public class DefaultKeyboardFocusManager extends KeyboardFocusManager JavaDoc {
46     private static final Logger focusLog = Logger.getLogger("java.awt.focus.DefaultKeyboardFocusManager");
47
48     private Window JavaDoc realOppositeWindow;
49     private Component JavaDoc realOppositeComponent;
50     private int inSendMessage;
51     private LinkedList JavaDoc enqueuedKeyEvents = new LinkedList JavaDoc(),
52         typeAheadMarkers = new LinkedList JavaDoc();
53     private boolean consumeNextKeyTyped;
54
55     private static class TypeAheadMarker {
56         long after;
57         Component JavaDoc untilFocused;
58
59         TypeAheadMarker(long after, Component JavaDoc untilFocused) {
60             this.after = after;
61             this.untilFocused = untilFocused;
62         }
63         /**
64          * Returns string representation of the marker
65          */

66         public String JavaDoc toString() {
67             return ">>> Marker after " + after + " on " + untilFocused;
68         }
69     }
70
71     private Window JavaDoc getOwningFrameDialog(Window JavaDoc window) {
72         while (window != null && !(window instanceof Frame JavaDoc ||
73                                    window instanceof Dialog JavaDoc)) {
74             window = (Window JavaDoc)window.getParent();
75         }
76         return window;
77     }
78
79     /*
80      * This series of restoreFocus methods is used for recovering from a
81      * rejected focus or activation change. Rejections typically occur when
82      * the user attempts to focus a non-focusable Component or Window.
83      */

84     private void restoreFocus(FocusEvent JavaDoc fe, Window JavaDoc newFocusedWindow) {
85         Component JavaDoc realOppositeComponent = this.realOppositeComponent;
86         Component JavaDoc vetoedComponent = fe.getComponent();
87         if (newFocusedWindow != null && restoreFocus(newFocusedWindow,
88                                                      vetoedComponent, false))
89         {
90         } else if (realOppositeComponent != null &&
91                    restoreFocus(realOppositeComponent, false)) {
92         } else if (fe.getOppositeComponent() != null &&
93                    restoreFocus(fe.getOppositeComponent(), false)) {
94         } else {
95             clearGlobalFocusOwner();
96         }
97     }
98     private void restoreFocus(WindowEvent JavaDoc we) {
99         Window JavaDoc realOppositeWindow = this.realOppositeWindow;
100         if (realOppositeWindow != null && restoreFocus(realOppositeWindow,
101                                                        null, false)) {
102         } else if (we.getOppositeWindow() != null &&
103                    restoreFocus(we.getOppositeWindow(), null, false)) {
104         } else {
105             clearGlobalFocusOwner();
106         }
107     }
108     private boolean restoreFocus(Window JavaDoc aWindow, Component JavaDoc vetoedComponent,
109                                  boolean clearOnFailure) {
110         Component JavaDoc toFocus =
111             KeyboardFocusManager.getMostRecentFocusOwner(aWindow);
112         if (toFocus != null && toFocus != vetoedComponent && restoreFocus(toFocus, false)) {
113             return true;
114         } else if (clearOnFailure) {
115             clearGlobalFocusOwner();
116             return true;
117         } else {
118             return false;
119         }
120     }
121     private boolean restoreFocus(Component JavaDoc toFocus, boolean clearOnFailure) {
122         if (toFocus.isShowing() && toFocus.isFocusable() &&
123             toFocus.requestFocus(false)) {
124             return true;
125         } else if (toFocus.nextFocusHelper()) {
126             return true;
127         } else if (clearOnFailure) {
128             clearGlobalFocusOwner();
129             return true;
130         } else {
131             return false;
132         }
133     }
134
135     /**
136      * A special type of SentEvent which updates a counter in the target
137      * KeyboardFocusManager if it is an instance of
138      * DefaultKeyboardFocusManager.
139      */

140     private static class DefaultKeyboardFocusManagerSentEvent
141         extends SentEvent JavaDoc
142     {
143         public DefaultKeyboardFocusManagerSentEvent(AWTEvent JavaDoc nested,
144                                                     AppContext toNotify) {
145             super(nested, toNotify);
146         }
147         public final void dispatch() {
148             KeyboardFocusManager JavaDoc manager =
149                 KeyboardFocusManager.getCurrentKeyboardFocusManager();
150             DefaultKeyboardFocusManager JavaDoc defaultManager =
151                 (manager instanceof DefaultKeyboardFocusManager JavaDoc)
152                 ? (DefaultKeyboardFocusManager JavaDoc)manager
153                 : null;
154
155             if (defaultManager != null) {
156                 synchronized (defaultManager) {
157                     defaultManager.inSendMessage++;
158                 }
159             }
160
161             super.dispatch();
162
163             if (defaultManager != null) {
164                 synchronized (defaultManager) {
165                     defaultManager.inSendMessage--;
166                 }
167             }
168         }
169     }
170
171     /**
172      * Sends a synthetic AWTEvent to a Component. If the Component is in
173      * the current AppContext, then the event is immediately dispatched.
174      * If the Component is in a different AppContext, then the event is
175      * posted to the other AppContext's EventQueue, and this method blocks
176      * until the event is handled or target AppContext is disposed.
177      * Returns true if successfuly dispatched event, false if failed
178      * to dispatch.
179      */

180     static boolean sendMessage(Component JavaDoc target, AWTEvent JavaDoc e) {
181         e.isPosted = true;
182         AppContext myAppContext = AppContext.getAppContext();
183         final AppContext targetAppContext = target.appContext;
184         final SentEvent JavaDoc se =
185             new DefaultKeyboardFocusManagerSentEvent(e, myAppContext);
186
187         if (myAppContext == targetAppContext) {
188             se.dispatch();
189         } else {
190             if (targetAppContext.isDisposed()) {
191                 return false;
192             }
193             SunToolkit.postEvent(targetAppContext, se);
194             if (EventQueue.isDispatchThread()) {
195                 EventDispatchThread JavaDoc edt = (EventDispatchThread JavaDoc)
196                     Thread.currentThread();
197                 edt.pumpEvents(SentEvent.ID, new Conditional JavaDoc() {
198                         public boolean evaluate() {
199                             return !se.dispatched && !targetAppContext.isDisposed();
200                         }
201                     });
202             } else {
203                 synchronized (se) {
204                     while (!se.dispatched && !targetAppContext.isDisposed()) {
205                         try {
206                             se.wait(1000);
207                         } catch (InterruptedException JavaDoc ie) {
208                             break;
209                         }
210                     }
211                 }
212             }
213         }
214         return se.dispatched;
215     }
216
217     /**
218      * This method is called by the AWT event dispatcher requesting that the
219      * current KeyboardFocusManager dispatch the specified event on its behalf.
220      * DefaultKeyboardFocusManagers dispatch all FocusEvents, all WindowEvents
221      * related to focus, and all KeyEvents. These events are dispatched based
222      * on the KeyboardFocusManager's notion of the focus owner and the focused
223      * and active Windows, sometimes overriding the source of the specified
224      * AWTEvent. If this method returns <code>false</code>, then the AWT event
225      * dispatcher will attempt to dispatch the event itself.
226      *
227      * @param e the AWTEvent to be dispatched
228      * @return <code>true</code> if this method dispatched the event;
229      * <code>false</code> otherwise
230      */

231     public boolean dispatchEvent(AWTEvent JavaDoc e) {
232         if (focusLog.isLoggable(Level.FINE) && (e instanceof WindowEvent JavaDoc || e instanceof FocusEvent JavaDoc)) focusLog.fine("" + e);
233         switch (e.getID()) {
234             case WindowEvent.WINDOW_GAINED_FOCUS: {
235                 WindowEvent JavaDoc we = (WindowEvent JavaDoc)e;
236                 Window JavaDoc oldFocusedWindow = getGlobalFocusedWindow();
237                 Window JavaDoc newFocusedWindow = we.getWindow();
238                 if (newFocusedWindow == oldFocusedWindow) {
239                     break;
240                 }
241
242                 // If there exists a current focused window, then notify it
243
// that it has lost focus.
244
if (oldFocusedWindow != null) {
245                     boolean isEventDispatched =
246                         sendMessage(oldFocusedWindow,
247                                 new WindowEvent JavaDoc(oldFocusedWindow,
248                                                 WindowEvent.WINDOW_LOST_FOCUS,
249                                                 newFocusedWindow));
250                     // Failed to dispatch, clear by ourselfves
251
if (!isEventDispatched) {
252                         setGlobalFocusOwner(null);
253                         setGlobalFocusedWindow(null);
254                     }
255                 }
256
257                 // Because the native libraries do not post WINDOW_ACTIVATED
258
// events, we need to synthesize one if the active Window
259
// changed.
260
Window JavaDoc newActiveWindow =
261                     getOwningFrameDialog(newFocusedWindow);
262                 Window JavaDoc currentActiveWindow = getGlobalActiveWindow();
263                 if (newActiveWindow != currentActiveWindow) {
264                     sendMessage(newActiveWindow,
265                                 new WindowEvent JavaDoc(newActiveWindow,
266                                                 WindowEvent.WINDOW_ACTIVATED,
267                                                 currentActiveWindow));
268                     if (newActiveWindow != getGlobalActiveWindow()) {
269                         // Activation change was rejected. Unlikely, but
270
// possible.
271
restoreFocus(we);
272                         break;
273                     }
274                 }
275
276                 setGlobalFocusedWindow(newFocusedWindow);
277
278                 if (newFocusedWindow != getGlobalFocusedWindow()) {
279                     // Focus change was rejected. Will happen if
280
// newFocusedWindow is not a focusable Window.
281
restoreFocus(we);
282                     break;
283                 }
284
285                 setNativeFocusedWindow(newFocusedWindow);
286                 // Restore focus to the Component which last held it. We do
287
// this here so that client code can override our choice in
288
// a WINDOW_GAINED_FOCUS handler.
289
//
290
// Make sure that the focus change request doesn't change the
291
// focused Window in case we are no longer the focused Window
292
// when the request is handled.
293
if (inSendMessage == 0) {
294                     // Identify which Component should initially gain focus
295
// in the Window.
296
//
297
// * If we're in SendMessage, then this is a synthetic
298
// WINDOW_GAINED_FOCUS message which was generated by a
299
// the FOCUS_GAINED handler. Allow the Component to
300
// which the FOCUS_GAINED message was targeted to
301
// receive the focus.
302
// * Otherwise, look up the correct Component here.
303
// We don't use Window.getMostRecentFocusOwner because
304
// window is focused now and 'null' will be returned
305

306
307                     // Calculating of most recent focus owner and focus
308
// request should be synchronized on KeyboardFocusManager.class
309
// to prevent from thread race when user will request
310
// focus between calculation and our request.
311
// But if focus transfer is synchronous, this synchronization
312
// may cause deadlock, thus we don't synchronize this block.
313
Component JavaDoc toFocus = KeyboardFocusManager.
314                         getMostRecentFocusOwner(newFocusedWindow);
315                     if ((toFocus == null) &&
316                         newFocusedWindow.isFocusableWindow())
317                     {
318                         toFocus = newFocusedWindow.getFocusTraversalPolicy().
319                             getInitialComponent(newFocusedWindow);
320                     }
321                     Component JavaDoc tempLost = null;
322                     synchronized(KeyboardFocusManager JavaDoc.class) {
323                         tempLost = newFocusedWindow.setTemporaryLostComponent(null);
324                     }
325
326                     // The component which last has the focus when this window was focused
327
// should receive focus first
328
if (focusLog.isLoggable(Level.FINER)) {
329                         focusLog.log(Level.FINER, "tempLost {0}, toFocus {1}",
330                                      new Object JavaDoc[]{tempLost, toFocus});
331                     }
332                     setInActivation(true);
333                     if (tempLost != null) {
334                         tempLost.requestFocusInWindow();
335                     }
336
337                     if (toFocus != null && toFocus != tempLost) {
338                         // If there is a component which requested focus when this window
339
// was inactive it expects to receive focus after activation.
340
toFocus.requestFocusInWindow();
341                     }
342                     setInActivation(false);
343                 }
344
345                 Window JavaDoc realOppositeWindow = this.realOppositeWindow;
346                 if (realOppositeWindow != we.getOppositeWindow()) {
347                     we = new WindowEvent JavaDoc(newFocusedWindow,
348                                          WindowEvent.WINDOW_GAINED_FOCUS,
349                                          realOppositeWindow);
350                 }
351                 return typeAheadAssertions(newFocusedWindow, we);
352             }
353
354             case WindowEvent.WINDOW_ACTIVATED: {
355                 WindowEvent JavaDoc we = (WindowEvent JavaDoc)e;
356                 Window JavaDoc oldActiveWindow = getGlobalActiveWindow();
357                 Window JavaDoc newActiveWindow = we.getWindow();
358                 if (oldActiveWindow == newActiveWindow) {
359                     break;
360                 }
361
362                 // If there exists a current active window, then notify it that
363
// it has lost activation.
364
if (oldActiveWindow != null) {
365                     boolean isEventDispatched =
366                         sendMessage(oldActiveWindow,
367                                 new WindowEvent JavaDoc(oldActiveWindow,
368                                                 WindowEvent.WINDOW_DEACTIVATED,
369                                                 newActiveWindow));
370                     // Failed to dispatch, clear by ourselfves
371
if (!isEventDispatched) {
372                         setGlobalActiveWindow(null);
373                     }
374                     if (getGlobalActiveWindow() != null) {
375                         // Activation change was rejected. Unlikely, but
376
// possible.
377
break;
378                     }
379                 }
380
381                 setGlobalActiveWindow(newActiveWindow);
382
383                 if (newActiveWindow != getGlobalActiveWindow()) {
384                     // Activation change was rejected. Unlikely, but
385
// possible.
386
break;
387                 }
388
389                 return typeAheadAssertions(newActiveWindow, we);
390             }
391
392             case FocusEvent.FOCUS_GAINED: {
393                 FocusEvent JavaDoc fe = (FocusEvent JavaDoc)e;
394                 Component JavaDoc oldFocusOwner = getGlobalFocusOwner();
395                 Component JavaDoc newFocusOwner = fe.getComponent();
396                 if (oldFocusOwner == newFocusOwner) {
397                     if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Skipping {0} because focus owner is the same",
398                                                                         new Object JavaDoc[] {e});
399                     // We can't just drop the event - there could be
400
// type-ahead markers associated with it.
401
dequeueKeyEvents(-1, newFocusOwner);
402                     break;
403                 }
404
405                 // If there exists a current focus owner, then notify it that
406
// it has lost focus.
407
if (oldFocusOwner != null) {
408                     boolean isEventDispatched =
409                         sendMessage(oldFocusOwner,
410                                     new FocusEvent JavaDoc(oldFocusOwner,
411                                                    FocusEvent.FOCUS_LOST,
412                                                    fe.isTemporary(),
413                                                    newFocusOwner));
414                     // Failed to dispatch, clear by ourselfves
415
if (!isEventDispatched) {
416                         setGlobalFocusOwner(null);
417                         if (!fe.isTemporary()) {
418                             setGlobalPermanentFocusOwner(null);
419                         }
420                     }
421                 }
422
423                 // Because the native windowing system has a different notion
424
// of the current focus and activation states, it is possible
425
// that a Component outside of the focused Window receives a
426
// FOCUS_GAINED event. We synthesize a WINDOW_GAINED_FOCUS
427
// event in that case.
428
Component JavaDoc newFocusedWindow = newFocusOwner;
429                 while (newFocusedWindow != null &&
430                        !(newFocusedWindow instanceof Window JavaDoc)) {
431                     newFocusedWindow = newFocusedWindow.parent;
432                 }
433                 Window JavaDoc currentFocusedWindow = getGlobalFocusedWindow();
434                 if (newFocusedWindow != null &&
435                     newFocusedWindow != currentFocusedWindow)
436                 {
437                     sendMessage(newFocusedWindow,
438                                 new WindowEvent JavaDoc((Window JavaDoc)newFocusedWindow,
439                                         WindowEvent.WINDOW_GAINED_FOCUS,
440                                                 currentFocusedWindow));
441                     if (newFocusedWindow != getGlobalFocusedWindow()) {
442                         // Focus change was rejected. Will happen if
443
// newFocusedWindow is not a focusable Window.
444

445                         // Need to recover type-ahead, but don't bother
446
// restoring focus. That was done by the
447
// WINDOW_GAINED_FOCUS handler
448
dequeueKeyEvents(-1, newFocusOwner);
449                         break;
450                     }
451                 }
452
453                 setGlobalFocusOwner(newFocusOwner);
454
455                 if (newFocusOwner != getGlobalFocusOwner()) {
456                     // Focus change was rejected. Will happen if
457
// newFocusOwner is not focus traversable.
458
dequeueKeyEvents(-1, newFocusOwner);
459                     if (! disableRestoreFocus ){
460                        restoreFocus(fe, (Window JavaDoc)newFocusedWindow);
461                     }
462                     break;
463                 }
464
465                 if (!fe.isTemporary()) {
466                     setGlobalPermanentFocusOwner(newFocusOwner);
467
468                     if (newFocusOwner != getGlobalPermanentFocusOwner()) {
469                         // Focus change was rejected. Unlikely, but possible.
470
dequeueKeyEvents(-1, newFocusOwner);
471                         if (! disableRestoreFocus ){
472                             restoreFocus(fe, (Window JavaDoc)newFocusedWindow);
473                         }
474                         break;
475                     }
476                 }
477
478                 setNativeFocusOwner(getHeavyweight(newFocusOwner));
479
480                 Component JavaDoc realOppositeComponent = this.realOppositeComponent;
481                 if (realOppositeComponent != null &&
482                     realOppositeComponent != fe.getOppositeComponent()) {
483                     fe = new FocusEvent JavaDoc(newFocusOwner,
484                                         FocusEvent.FOCUS_GAINED,
485                                         fe.isTemporary(),
486                                         realOppositeComponent);
487                     ((AWTEvent JavaDoc) fe).isPosted = true;
488                 }
489                 return typeAheadAssertions(newFocusOwner, fe);
490             }
491
492             case FocusEvent.FOCUS_LOST: {
493                 FocusEvent JavaDoc fe = (FocusEvent JavaDoc)e;
494                 Component JavaDoc currentFocusOwner = getGlobalFocusOwner();
495                 if (currentFocusOwner == null) {
496                     if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Skipping {0} because focus owner is null",
497                                                                         new Object JavaDoc[] {e});
498                     break;
499                 }
500                 // Ignore cases where a Component loses focus to itself.
501
// If we make a mistake because of retargeting, then the
502
// FOCUS_GAINED handler will correct it.
503
if (currentFocusOwner == fe.getOppositeComponent()) {
504                     if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Skipping {0} because current focus owner is equal to opposite",
505                                                                       new Object JavaDoc[] {e});
506                     break;
507                 }
508
509                 setGlobalFocusOwner(null);
510
511                 if (getGlobalFocusOwner() != null) {
512                     // Focus change was rejected. Unlikely, but possible.
513
restoreFocus(currentFocusOwner, true);
514                     break;
515                 }
516
517                 if (!fe.isTemporary()) {
518                     setGlobalPermanentFocusOwner(null);
519
520                     if (getGlobalPermanentFocusOwner() != null) {
521                         // Focus change was rejected. Unlikely, but possible.
522
restoreFocus(currentFocusOwner, true);
523                         break;
524                     }
525                 } else {
526                     Window JavaDoc owningWindow = currentFocusOwner.getContainingWindow();
527                     if (owningWindow != null) {
528                         owningWindow.setTemporaryLostComponent(currentFocusOwner);
529                     }
530                 }
531
532                 setNativeFocusOwner(null);
533
534                 fe.setSource(currentFocusOwner);
535
536                 realOppositeComponent = (fe.getOppositeComponent() != null)
537                     ? currentFocusOwner : null;
538
539                 return typeAheadAssertions(currentFocusOwner, fe);
540             }
541
542             case WindowEvent.WINDOW_DEACTIVATED: {
543                 WindowEvent JavaDoc we = (WindowEvent JavaDoc)e;
544                 Window JavaDoc currentActiveWindow = getGlobalActiveWindow();
545                 if (currentActiveWindow == null) {
546                     break;
547                 }
548
549                 if (currentActiveWindow != e.getSource()) {
550                     // The event is lost in time.
551
// Allow listeners to precess the event but do not
552
// change any global states
553
break;
554                 }
555
556                 setGlobalActiveWindow(null);
557                 if (getGlobalActiveWindow() != null) {
558                     // Activation change was rejected. Unlikely, but possible.
559
break;
560                 }
561
562                 we.setSource(currentActiveWindow);
563                 return typeAheadAssertions(currentActiveWindow, we);
564             }
565
566             case WindowEvent.WINDOW_LOST_FOCUS: {
567                 WindowEvent JavaDoc we = (WindowEvent JavaDoc)e;
568                 Window JavaDoc currentFocusedWindow = getGlobalFocusedWindow();
569                 Window JavaDoc losingFocusWindow = we.getWindow();
570                 Window JavaDoc activeWindow = getGlobalActiveWindow();
571                 Window JavaDoc oppositeWindow = we.getOppositeWindow();
572                 if (focusLog.isLoggable(Level.FINE)) focusLog.log(Level.FINE, "Active {0}, Current focused {1}, losing focus {2} opposite {3}",
573                                                                   new Object JavaDoc[] {activeWindow, currentFocusedWindow,
574                                                                                 losingFocusWindow, oppositeWindow});
575                 if (currentFocusedWindow == null) {
576                     break;
577                 }
578
579                 // Special case -- if the native windowing system posts an
580
// event claiming that the active Window has lost focus to the
581
// focused Window, then discard the event. This is an artifact
582
// of the native windowing system not knowing which Window is
583
// really focused.
584
if (inSendMessage == 0 && losingFocusWindow == activeWindow &&
585                     oppositeWindow == currentFocusedWindow)
586                 {
587                     break;
588                 }
589
590                 Component JavaDoc currentFocusOwner = getGlobalFocusOwner();
591                 if (currentFocusOwner != null) {
592                     // The focus owner should always receive a FOCUS_LOST event
593
// before the Window is defocused.
594
Component JavaDoc oppositeComp = null;
595                     if (oppositeWindow != null) {
596                         oppositeComp = oppositeWindow.getTemporaryLostComponent();
597                         if (oppositeComp == null) {
598                             oppositeComp = oppositeWindow.getMostRecentFocusOwner();
599                         }
600                     }
601                     if (oppositeComp == null) {
602                         oppositeComp = oppositeWindow;
603                     }
604                     sendMessage(currentFocusOwner,
605                                 new FocusEvent JavaDoc(currentFocusOwner,
606                                                FocusEvent.FOCUS_LOST,
607                                                true,
608                                                oppositeComp));
609                 }
610
611                 setGlobalFocusedWindow(null);
612                 if (getGlobalFocusedWindow() != null) {
613                     // Focus change was rejected. Unlikely, but possible.
614
restoreFocus(currentFocusedWindow, null, true);
615                     break;
616                 }
617                 setNativeFocusedWindow(null);
618
619                 we.setSource(currentFocusedWindow);
620                 realOppositeWindow = (oppositeWindow != null)
621                     ? currentFocusedWindow
622                     : null;
623                 typeAheadAssertions(currentFocusedWindow, we);
624
625                 if (oppositeWindow == null) {
626                     // Then we need to deactive the active Window as well.
627
// No need to synthesize in other cases, because
628
// WINDOW_ACTIVATED will handle it if necessary.
629
sendMessage(activeWindow,
630                                 new WindowEvent JavaDoc(activeWindow,
631                                                 WindowEvent.WINDOW_DEACTIVATED,
632                                                 null));
633                     if (getGlobalActiveWindow() != null) {
634                         // Activation change was rejected. Unlikely,
635
// but possible.
636
restoreFocus(currentFocusedWindow, null, true);
637                     }
638                 }
639                 break;
640             }
641
642             case KeyEvent.KEY_TYPED:
643             case KeyEvent.KEY_PRESSED:
644             case KeyEvent.KEY_RELEASED:
645                 return typeAheadAssertions(null, e);
646
647             default:
648                 return false;
649         }
650
651         return true;
652     }
653
654     /**
655      * Called by <code>dispatchEvent</code> if no other
656      * KeyEventDispatcher in the dispatcher chain dispatched the KeyEvent, or
657      * if no other KeyEventDispatchers are registered. If the event has not
658      * been consumed, its target is enabled, and the focus owner is not null,
659      * this method dispatches the event to its target. This method will also
660      * subsequently dispatch the event to all registered
661      * KeyEventPostProcessors. After all