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             EventQueue.setCurrentEventAndMostRecentTime(e);
4710         }
4711 
4712         /*
4713          * 1. Pre-dispatchers. Do any necessary retargeting/reordering here
4714          *    before we notify AWTEventListeners.
4715          */
4716 
4717         if (e instanceof SunDropTargetEvent) {
4718             ((SunDropTargetEvent)e).dispatch();
4719             return;
4720         }
4721 
4722         if (!e.focusManagerIsDispatching) {
4723             // Invoke the private focus retargeting method which provides
4724             // lightweight Component support
4725             if (e.isPosted) {
4726                 e = KeyboardFocusManager.retargetFocusEvent(e);
4727                 e.isPosted = true;
4728             }
4729 
4730             // 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         AWTEvent currentEvent = EventQueue.getCurrentEvent();
7613         if (currentEvent instanceof MouseEvent) {
7614             Component source = (Component)currentEvent.getSource();
7615             if (source != null && source.getContainingWindow() == getContainingWindow()) {
7616                 focusLog.finest("requesting focus by mouse event \"in window\"");
7617                 // A focus request to a component in a window caused by a mouse event
7618                 // in the same window should be "in window". The requestor (the system or
7619                 // a user mouse event listener) assumes the window is focused due to the
7620                 // mouse click that precedes the request. So if by some reason the window,
7621                 // by the time the request is handled, loses focus the request should be
7622                 // rejected. Otherwise it may break the order of user actions. See 6981400.
7623                 focusedWindowChangeAllowed = false;
7624             }
7625         }
7626         if (!isRequestFocusAccepted(temporary, focusedWindowChangeAllowed, cause)) {
7627             if (focusLog.isLoggable(PlatformLogger.FINEST)) {
7628                 focusLog.finest("requestFocus is not accepted");
7629             }
7630             return false;
7631         }

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