< prev index next >

src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.event.FocusEvent;
  28 import java.awt.event.KeyEvent;
  29 import java.awt.event.WindowEvent;
  30 import java.awt.peer.ComponentPeer;
  31 import java.awt.peer.LightweightPeer;
  32 import java.lang.ref.WeakReference;


  33 import java.util.LinkedList;
  34 import java.util.Iterator;
  35 import java.util.ListIterator;
  36 import java.util.Set;
  37 
  38 import sun.util.logging.PlatformLogger;
  39 
  40 import sun.awt.AppContext;
  41 import sun.awt.SunToolkit;
  42 import sun.awt.AWTAccessor;
  43 import sun.awt.TimedWindowEvent;
  44 
  45 /**
  46  * The default KeyboardFocusManager for AWT applications. Focus traversal is
  47  * done in response to a Component's focus traversal keys, and using a
  48  * Container's FocusTraversalPolicy.
  49  * <p>
  50  * Please see
  51  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
  52  * How to Use the Focus Subsystem</a>,


 247      * posted to the other AppContext's EventQueue, and this method blocks
 248      * until the event is handled or target AppContext is disposed.
 249      * Returns true if successfully dispatched event, false if failed
 250      * to dispatch.
 251      */
 252     static boolean sendMessage(Component target, AWTEvent e) {
 253         e.isPosted = true;
 254         AppContext myAppContext = AppContext.getAppContext();
 255         final AppContext targetAppContext = target.appContext;
 256         final SentEvent se =
 257             new DefaultKeyboardFocusManagerSentEvent(e, myAppContext);
 258 
 259         if (myAppContext == targetAppContext) {
 260             se.dispatch();
 261         } else {
 262             if (targetAppContext.isDisposed()) {
 263                 return false;
 264             }
 265             SunToolkit.postEvent(targetAppContext, se);
 266             if (EventQueue.isDispatchThread()) {

 267                 EventDispatchThread edt = (EventDispatchThread)
 268                     Thread.currentThread();
 269                 edt.pumpEvents(SentEvent.ID, new Conditional() {
 270                         public boolean evaluate() {
 271                             return !se.dispatched && !targetAppContext.isDisposed();
 272                         }
 273                     });




















 274             } else {
 275                 synchronized (se) {
 276                     while (!se.dispatched && !targetAppContext.isDisposed()) {
 277                         try {
 278                             se.wait(1000);
 279                         } catch (InterruptedException ie) {
 280                             break;
 281                         }
 282                     }
 283                 }
 284             }
 285         }
 286         return se.dispatched;
 287     }
 288 
 289     /*
 290      * Checks if the focus window event follows key events waiting in the type-ahead
 291      * queue (if any). This may happen when a user types ahead in the window, the client
 292      * listeners hang EDT for a while, and the user switches b/w toplevels. In that
 293      * case the focus window events may be dispatched before the type-ahead events




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.awt.event.FocusEvent;
  28 import java.awt.event.KeyEvent;
  29 import java.awt.event.WindowEvent;
  30 import java.awt.peer.ComponentPeer;
  31 import java.awt.peer.LightweightPeer;
  32 import java.lang.ref.WeakReference;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.util.LinkedList;
  36 import java.util.Iterator;
  37 import java.util.ListIterator;
  38 import java.util.Set;
  39 
  40 import sun.util.logging.PlatformLogger;
  41 
  42 import sun.awt.AppContext;
  43 import sun.awt.SunToolkit;
  44 import sun.awt.AWTAccessor;
  45 import sun.awt.TimedWindowEvent;
  46 
  47 /**
  48  * The default KeyboardFocusManager for AWT applications. Focus traversal is
  49  * done in response to a Component's focus traversal keys, and using a
  50  * Container's FocusTraversalPolicy.
  51  * <p>
  52  * Please see
  53  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
  54  * How to Use the Focus Subsystem</a>,


 249      * posted to the other AppContext's EventQueue, and this method blocks
 250      * until the event is handled or target AppContext is disposed.
 251      * Returns true if successfully dispatched event, false if failed
 252      * to dispatch.
 253      */
 254     static boolean sendMessage(Component target, AWTEvent e) {
 255         e.isPosted = true;
 256         AppContext myAppContext = AppContext.getAppContext();
 257         final AppContext targetAppContext = target.appContext;
 258         final SentEvent se =
 259             new DefaultKeyboardFocusManagerSentEvent(e, myAppContext);
 260 
 261         if (myAppContext == targetAppContext) {
 262             se.dispatch();
 263         } else {
 264             if (targetAppContext.isDisposed()) {
 265                 return false;
 266             }
 267             SunToolkit.postEvent(targetAppContext, se);
 268             if (EventQueue.isDispatchThread()) {
 269                 if (Thread.currentThread() instanceof EventDispatchThread) {
 270                     EventDispatchThread edt = (EventDispatchThread)
 271                             Thread.currentThread();
 272                     edt.pumpEvents(SentEvent.ID, new Conditional() {
 273                         public boolean evaluate() {
 274                             return !se.dispatched && !targetAppContext.isDisposed();
 275                         }
 276                     });
 277                 } else {
 278                     EventQueue eventQueue = AccessController.doPrivileged(
 279                                 (PrivilegedAction<EventQueue>) java.awt.Toolkit
 280                                         .getDefaultToolkit()::getSystemEventQueue);
 281                     SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop();
 282                     new Thread() {
 283                         @Override
 284                         public void run() {
 285                             while(!se.dispatched && !targetAppContext.isDisposed()) {
 286                                 try {
 287                                     Thread.sleep(1000);
 288                                 } catch (InterruptedException e) {
 289                                     break;
 290                                 }
 291                             }
 292                             secondaryLoop.exit();
 293                         }
 294                     }.start();
 295                     secondaryLoop.enter();
 296                 }
 297             } else {
 298                 synchronized (se) {
 299                     while (!se.dispatched && !targetAppContext.isDisposed()) {
 300                         try {
 301                             se.wait(1000);
 302                         } catch (InterruptedException ie) {
 303                             break;
 304                         }
 305                     }
 306                 }
 307             }
 308         }
 309         return se.dispatched;
 310     }
 311 
 312     /*
 313      * Checks if the focus window event follows key events waiting in the type-ahead
 314      * queue (if any). This may happen when a user types ahead in the window, the client
 315      * listeners hang EDT for a while, and the user switches b/w toplevels. In that
 316      * case the focus window events may be dispatched before the type-ahead events


< prev index next >