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

Print this page




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


4708         EventQueue.setCurrentEventAndMostRecentTime(e);

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


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





















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








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




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


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

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