src/share/classes/java/awt/Component.java

Print this page




4693 
4694     @SuppressWarnings("deprecation")
4695     void dispatchEventImpl(AWTEvent e) {
4696         int id = e.getID();
4697 
4698         // Check that this component belongs to this app-context
4699         AppContext compContext = appContext;
4700         if (compContext != null && !compContext.equals(AppContext.getAppContext())) {
4701             if (eventLog.isLoggable(PlatformLogger.FINE)) {
4702                 eventLog.fine("Event " + e + " is being dispatched on the wrong AppContext");
4703             }
4704         }
4705 
4706         if (eventLog.isLoggable(PlatformLogger.FINEST)) {
4707             eventLog.finest("{0}", e);
4708         }
4709 
4710         /*
4711          * 0. Set timestamp and modifiers of current event.
4712          */


4713         EventQueue.setCurrentEventAndMostRecentTime(e);

4714 
4715         /*
4716          * 1. Pre-dispatchers. Do any necessary retargeting/reordering here
4717          *    before we notify AWTEventListeners.
4718          */
4719 
4720         if (e instanceof SunDropTargetEvent) {
4721             ((SunDropTargetEvent)e).dispatch();
4722             return;
4723         }
4724 
4725         if (!e.focusManagerIsDispatching) {
4726             // Invoke the private focus retargeting method which provides
4727             // lightweight Component support
4728             if (e.isPosted) {
4729                 e = KeyboardFocusManager.retargetFocusEvent(e);
4730                 e.isPosted = true;
4731             }
4732 
4733             // Now, with the event properly targeted to a lightweight


7589      * @see KeyboardFocusManager#clearGlobalFocusOwner
7590      * @since 1.4
7591      */
7592     protected boolean requestFocusInWindow(boolean temporary) {
7593         return requestFocusHelper(temporary, false);
7594     }
7595 
7596     boolean requestFocusInWindow(boolean temporary, CausedFocusEvent.Cause cause) {
7597         return requestFocusHelper(temporary, false, cause);
7598     }
7599 
7600     final boolean requestFocusHelper(boolean temporary,
7601                                      boolean focusedWindowChangeAllowed) {
7602         return requestFocusHelper(temporary, focusedWindowChangeAllowed, CausedFocusEvent.Cause.UNKNOWN);
7603     }
7604 
7605     final boolean requestFocusHelper(boolean temporary,
7606                                      boolean focusedWindowChangeAllowed,
7607                                      CausedFocusEvent.Cause cause)
7608     {





















7609         if (!isRequestFocusAccepted(temporary, focusedWindowChangeAllowed, cause)) {
7610             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7611                 focusLog.finest("requestFocus is not accepted");
7612             }
7613             return false;
7614         }
7615 
7616         // Update most-recent map
7617         KeyboardFocusManager.setMostRecentFocusOwner(this);
7618 
7619         Component window = this;
7620         while ( (window != null) && !(window instanceof Window)) {
7621             if (!window.isVisible()) {
7622                 if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7623                     focusLog.finest("component is recurively invisible");
7624                 }
7625                 return false;
7626             }
7627             window = window.parent;
7628         }
7629 
7630         ComponentPeer peer = this.peer;
7631         Component heavyweight = (peer instanceof LightweightPeer)
7632             ? getNativeContainer() : this;
7633         if (heavyweight == null || !heavyweight.isVisible()) {
7634             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7635                 focusLog.finest("Component is not a part of visible hierarchy");
7636             }
7637             return false;
7638         }
7639         peer = heavyweight.peer;
7640         if (peer == null) {
7641             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7642                 focusLog.finest("Peer is null");
7643             }
7644             return false;
7645         }
7646 
7647         // Focus this Component
7648         long time = EventQueue.getMostRecentEventTime();








7649         boolean success = peer.requestFocus
7650             (this, temporary, focusedWindowChangeAllowed, time, cause);
7651         if (!success) {
7652             KeyboardFocusManager.getCurrentKeyboardFocusManager
7653                 (appContext).dequeueKeyEvents(time, this);
7654             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7655                 focusLog.finest("Peer request failed");
7656             }
7657         } else {
7658             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7659                 focusLog.finest("Pass for " + this);
7660             }
7661         }
7662         return success;
7663     }
7664 
7665     private boolean isRequestFocusAccepted(boolean temporary,
7666                                            boolean focusedWindowChangeAllowed,
7667                                            CausedFocusEvent.Cause cause)
7668     {




4693 
4694     @SuppressWarnings("deprecation")
4695     void dispatchEventImpl(AWTEvent e) {
4696         int id = e.getID();
4697 
4698         // Check that this component belongs to this app-context
4699         AppContext compContext = appContext;
4700         if (compContext != null && !compContext.equals(AppContext.getAppContext())) {
4701             if (eventLog.isLoggable(PlatformLogger.FINE)) {
4702                 eventLog.fine("Event " + e + " is being dispatched on the wrong AppContext");
4703             }
4704         }
4705 
4706         if (eventLog.isLoggable(PlatformLogger.FINEST)) {
4707             eventLog.finest("{0}", e);
4708         }
4709 
4710         /*
4711          * 0. Set timestamp and modifiers of current event.
4712          */
4713         if (!(e instanceof KeyEvent)) {
4714             // Timestamp of a key event is set later in DKFM.preDispatchKeyEvent(KeyEvent).
4715             EventQueue.setCurrentEventAndMostRecentTime(e);
4716         }
4717 
4718         /*
4719          * 1. Pre-dispatchers. Do any necessary retargeting/reordering here
4720          *    before we notify AWTEventListeners.
4721          */
4722 
4723         if (e instanceof SunDropTargetEvent) {
4724             ((SunDropTargetEvent)e).dispatch();
4725             return;
4726         }
4727 
4728         if (!e.focusManagerIsDispatching) {
4729             // Invoke the private focus retargeting method which provides
4730             // lightweight Component support
4731             if (e.isPosted) {
4732                 e = KeyboardFocusManager.retargetFocusEvent(e);
4733                 e.isPosted = true;
4734             }
4735 
4736             // Now, with the event properly targeted to a lightweight


7592      * @see KeyboardFocusManager#clearGlobalFocusOwner
7593      * @since 1.4
7594      */
7595     protected boolean requestFocusInWindow(boolean temporary) {
7596         return requestFocusHelper(temporary, false);
7597     }
7598 
7599     boolean requestFocusInWindow(boolean temporary, CausedFocusEvent.Cause cause) {
7600         return requestFocusHelper(temporary, false, cause);
7601     }
7602 
7603     final boolean requestFocusHelper(boolean temporary,
7604                                      boolean focusedWindowChangeAllowed) {
7605         return requestFocusHelper(temporary, focusedWindowChangeAllowed, CausedFocusEvent.Cause.UNKNOWN);
7606     }
7607 
7608     final boolean requestFocusHelper(boolean temporary,
7609                                      boolean focusedWindowChangeAllowed,
7610                                      CausedFocusEvent.Cause cause)
7611     {
7612         // 1) Check if the event being dispatched is a system-generated mouse event.
7613         AWTEvent currentEvent = EventQueue.getCurrentEvent();
7614         if (currentEvent instanceof MouseEvent &&
7615             SunToolkit.isSystemGenerated(currentEvent))
7616         {
7617             // 2) Sanity check: if the mouse event component source belongs to the same containing window.
7618             Component source = ((MouseEvent)currentEvent).getComponent();
7619             if (source == null || source.getContainingWindow() == getContainingWindow()) {
7620                 focusLog.finest("requesting focus by mouse event \"in window\"");
7621 
7622                 // If both the conditions are fulfilled the focus request should be strictly
7623                 // bounded by the toplevel window. It's assumed that the mouse event activates
7624                 // the window (if it wasn't active) and this makes it possible for a focus
7625                 // request with a strong in-window requirement to change focus in the bounds
7626                 // of the toplevel. If, by any means, due to asynchronous nature of the event
7627                 // dispatching mechanism, the window happens to be natively inactive by the time
7628                 // this focus request is eventually handled, it should not re-activate the
7629                 // toplevel. Otherwise the result may not meet user expectations. See 6981400.
7630                 focusedWindowChangeAllowed = false;
7631             }
7632         }
7633         if (!isRequestFocusAccepted(temporary, focusedWindowChangeAllowed, cause)) {
7634             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7635                 focusLog.finest("requestFocus is not accepted");
7636             }
7637             return false;
7638         }

7639         // Update most-recent map
7640         KeyboardFocusManager.setMostRecentFocusOwner(this);
7641 
7642         Component window = this;
7643         while ( (window != null) && !(window instanceof Window)) {
7644             if (!window.isVisible()) {
7645                 if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7646                     focusLog.finest("component is recurively invisible");
7647                 }
7648                 return false;
7649             }
7650             window = window.parent;
7651         }
7652 
7653         ComponentPeer peer = this.peer;
7654         Component heavyweight = (peer instanceof LightweightPeer)
7655             ? getNativeContainer() : this;
7656         if (heavyweight == null || !heavyweight.isVisible()) {
7657             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7658                 focusLog.finest("Component is not a part of visible hierarchy");
7659             }
7660             return false;
7661         }
7662         peer = heavyweight.peer;
7663         if (peer == null) {
7664             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7665                 focusLog.finest("Peer is null");
7666             }
7667             return false;
7668         }
7669 
7670         // Focus this Component
7671         long time = 0;
7672         if (EventQueue.isDispatchThread()) {
7673             time = Toolkit.getEventQueue().getMostRecentKeyEventTime();
7674         } else {
7675             // A focus request made from outside EDT should not be associated with any event
7676             // and so its time stamp is simply set to the current time.
7677             time = System.currentTimeMillis();
7678         }
7679         
7680         boolean success = peer.requestFocus
7681             (this, temporary, focusedWindowChangeAllowed, time, cause);
7682         if (!success) {
7683             KeyboardFocusManager.getCurrentKeyboardFocusManager
7684                 (appContext).dequeueKeyEvents(time, this);
7685             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7686                 focusLog.finest("Peer request failed");
7687             }
7688         } else {
7689             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7690                 focusLog.finest("Pass for " + this);
7691             }
7692         }
7693         return success;
7694     }
7695 
7696     private boolean isRequestFocusAccepted(boolean temporary,
7697                                            boolean focusedWindowChangeAllowed,
7698                                            CausedFocusEvent.Cause cause)
7699     {