< prev index next >

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

Print this page




  51 import java.beans.PropertyChangeSupport;
  52 import java.beans.Transient;
  53 import java.awt.im.InputContext;
  54 import java.awt.im.InputMethodRequests;
  55 import java.awt.dnd.DropTarget;
  56 import java.lang.reflect.InvocationTargetException;
  57 import java.lang.reflect.Method;
  58 import java.security.AccessController;
  59 import java.security.PrivilegedAction;
  60 import java.security.AccessControlContext;
  61 import javax.accessibility.*;
  62 import java.applet.Applet;
  63 
  64 import sun.awt.ComponentFactory;
  65 import sun.security.action.GetPropertyAction;
  66 import sun.awt.AppContext;
  67 import sun.awt.AWTAccessor;
  68 import sun.awt.ConstrainableGraphics;
  69 import sun.awt.SubRegionShowable;
  70 import sun.awt.SunToolkit;
  71 import sun.awt.CausedFocusEvent;
  72 import sun.awt.EmbeddedFrame;
  73 import sun.awt.dnd.SunDropTargetEvent;
  74 import sun.awt.im.CompositionArea;
  75 import sun.font.FontManager;
  76 import sun.font.FontManagerFactory;
  77 import sun.font.SunFontManager;
  78 import sun.java2d.SunGraphics2D;
  79 import sun.java2d.pipe.Region;
  80 import sun.awt.image.VSyncedBSManager;
  81 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  82 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*;
  83 import sun.awt.RequestFocusController;
  84 import sun.java2d.SunGraphicsEnvironment;
  85 import sun.util.logging.PlatformLogger;
  86 
  87 /**
  88  * A <em>component</em> is an object having a graphical representation
  89  * that can be displayed on the screen and that can interact with the
  90  * user. Examples of components are the buttons, checkboxes, and scrollbars
  91  * of a typical graphical user interface. <p>


 861                     if (!comp.isNonOpaqueForMixing()) {
 862                         needShowing = true;
 863                     }
 864 
 865                     if (comp.isMixingNeeded()) {
 866                         if (needHiding) {
 867                             comp.mixOnHiding(comp.isLightweight());
 868                         }
 869                         if (needShowing) {
 870                             comp.mixOnShowing();
 871                         }
 872                     }
 873                 }
 874             }
 875 
 876             public void setGraphicsConfiguration(Component comp,
 877                     GraphicsConfiguration gc)
 878             {
 879                 comp.setGraphicsConfiguration(gc);
 880             }
 881             public boolean requestFocus(Component comp, CausedFocusEvent.Cause cause) {
 882                 return comp.requestFocus(cause);
 883             }
 884             public boolean canBeFocusOwner(Component comp) {
 885                 return comp.canBeFocusOwner();
 886             }
 887 
 888             public boolean isVisible(Component comp) {
 889                 return comp.isVisible_NoClientCode();
 890             }
 891             public void setRequestFocusController
 892                 (RequestFocusController requestController)
 893             {
 894                  Component.setRequestFocusController(requestController);
 895             }
 896             public AppContext getAppContext(Component comp) {
 897                  return comp.appContext;
 898             }
 899             public void setAppContext(Component comp, AppContext appContext) {
 900                  comp.appContext = appContext;
 901             }


7523      * Because the focus behavior of this method is platform-dependent,
7524      * developers are strongly encouraged to use
7525      * <code>requestFocusInWindow</code> when possible.
7526      *
7527      * <p>Note: Not all focus transfers result from invoking this method. As
7528      * such, a component may receive focus without this or any of the other
7529      * {@code requestFocus} methods of {@code Component} being invoked.
7530      *
7531      * @see #requestFocusInWindow
7532      * @see java.awt.event.FocusEvent
7533      * @see #addFocusListener
7534      * @see #isFocusable
7535      * @see #isDisplayable
7536      * @see KeyboardFocusManager#clearGlobalFocusOwner
7537      * @since 1.0
7538      */
7539     public void requestFocus() {
7540         requestFocusHelper(false, true);
7541     }
7542 
7543     boolean requestFocus(CausedFocusEvent.Cause cause) {
7544         return requestFocusHelper(false, true, cause);
7545     }
7546 
7547     /**
7548      * Requests that this <code>Component</code> get the input focus,
7549      * and that this <code>Component</code>'s top-level ancestor
7550      * become the focused <code>Window</code>. This component must be
7551      * displayable, focusable, visible and all of its ancestors (with
7552      * the exception of the top-level Window) must be visible for the
7553      * request to be granted. Every effort will be made to honor the
7554      * request; however, in some cases it may be impossible to do
7555      * so. Developers must never assume that this component is the
7556      * focus owner until this component receives a FOCUS_GAINED
7557      * event. If this request is denied because this component's
7558      * top-level window cannot become the focused window, the request
7559      * will be remembered and will be granted when the window is later
7560      * focused by the user.
7561      * <p>
7562      * This method returns a boolean value. If <code>false</code> is returned,
7563      * the request is <b>guaranteed to fail</b>. If <code>true</code> is


7590      * such, a component may receive focus without this or any of the other
7591      * {@code requestFocus} methods of {@code Component} being invoked.
7592      *
7593      * @param temporary true if the focus change is temporary,
7594      *        such as when the window loses the focus; for
7595      *        more information on temporary focus changes see the
7596      *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7597      * @return <code>false</code> if the focus change request is guaranteed to
7598      *         fail; <code>true</code> if it is likely to succeed
7599      * @see java.awt.event.FocusEvent
7600      * @see #addFocusListener
7601      * @see #isFocusable
7602      * @see #isDisplayable
7603      * @see KeyboardFocusManager#clearGlobalFocusOwner
7604      * @since 1.4
7605      */
7606     protected boolean requestFocus(boolean temporary) {
7607         return requestFocusHelper(temporary, true);
7608     }
7609 
7610     boolean requestFocus(boolean temporary, CausedFocusEvent.Cause cause) {
7611         return requestFocusHelper(temporary, true, cause);
7612     }
7613     /**
7614      * Requests that this Component get the input focus, if this
7615      * Component's top-level ancestor is already the focused
7616      * Window. This component must be displayable, focusable, visible
7617      * and all of its ancestors (with the exception of the top-level
7618      * Window) must be visible for the request to be granted. Every
7619      * effort will be made to honor the request; however, in some
7620      * cases it may be impossible to do so. Developers must never
7621      * assume that this Component is the focus owner until this
7622      * Component receives a FOCUS_GAINED event.
7623      * <p>
7624      * This method returns a boolean value. If <code>false</code> is returned,
7625      * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7626      * returned, the request will succeed <b>unless</b> it is vetoed, or an
7627      * extraordinary event, such as disposal of the Component's peer, occurs
7628      * before the request can be granted by the native windowing system. Again,
7629      * while a return value of <code>true</code> indicates that the request is
7630      * likely to succeed, developers must never assume that this Component is


7641      * different platforms.
7642      *
7643      * <p>Note: Not all focus transfers result from invoking this method. As
7644      * such, a component may receive focus without this or any of the other
7645      * {@code requestFocus} methods of {@code Component} being invoked.
7646      *
7647      * @return <code>false</code> if the focus change request is guaranteed to
7648      *         fail; <code>true</code> if it is likely to succeed
7649      * @see #requestFocus
7650      * @see java.awt.event.FocusEvent
7651      * @see #addFocusListener
7652      * @see #isFocusable
7653      * @see #isDisplayable
7654      * @see KeyboardFocusManager#clearGlobalFocusOwner
7655      * @since 1.4
7656      */
7657     public boolean requestFocusInWindow() {
7658         return requestFocusHelper(false, false);
7659     }
7660 
7661     boolean requestFocusInWindow(CausedFocusEvent.Cause cause) {
7662         return requestFocusHelper(false, false, cause);
7663     }
7664 
7665     /**
7666      * Requests that this <code>Component</code> get the input focus,
7667      * if this <code>Component</code>'s top-level ancestor is already
7668      * the focused <code>Window</code>.  This component must be
7669      * displayable, focusable, visible and all of its ancestors (with
7670      * the exception of the top-level Window) must be visible for the
7671      * request to be granted. Every effort will be made to honor the
7672      * request; however, in some cases it may be impossible to do
7673      * so. Developers must never assume that this component is the
7674      * focus owner until this component receives a FOCUS_GAINED event.
7675      * <p>
7676      * This method returns a boolean value. If <code>false</code> is returned,
7677      * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7678      * returned, the request will succeed <b>unless</b> it is vetoed, or an
7679      * extraordinary event, such as disposal of the component's peer, occurs
7680      * before the request can be granted by the native windowing system. Again,
7681      * while a return value of <code>true</code> indicates that the request is


7706      * {@code requestFocus} methods of {@code Component} being invoked.
7707      *
7708      * @param temporary true if the focus change is temporary,
7709      *        such as when the window loses the focus; for
7710      *        more information on temporary focus changes see the
7711      *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7712      * @return <code>false</code> if the focus change request is guaranteed to
7713      *         fail; <code>true</code> if it is likely to succeed
7714      * @see #requestFocus
7715      * @see java.awt.event.FocusEvent
7716      * @see #addFocusListener
7717      * @see #isFocusable
7718      * @see #isDisplayable
7719      * @see KeyboardFocusManager#clearGlobalFocusOwner
7720      * @since 1.4
7721      */
7722     protected boolean requestFocusInWindow(boolean temporary) {
7723         return requestFocusHelper(temporary, false);
7724     }
7725 
7726     boolean requestFocusInWindow(boolean temporary, CausedFocusEvent.Cause cause) {
7727         return requestFocusHelper(temporary, false, cause);
7728     }
7729 
7730     final boolean requestFocusHelper(boolean temporary,
7731                                      boolean focusedWindowChangeAllowed) {
7732         return requestFocusHelper(temporary, focusedWindowChangeAllowed, CausedFocusEvent.Cause.UNKNOWN);
7733     }
7734 
7735     final boolean requestFocusHelper(boolean temporary,
7736                                      boolean focusedWindowChangeAllowed,
7737                                      CausedFocusEvent.Cause cause)
7738     {
7739         // 1) Check if the event being dispatched is a system-generated mouse event.
7740         AWTEvent currentEvent = EventQueue.getCurrentEvent();
7741         if (currentEvent instanceof MouseEvent &&
7742             SunToolkit.isSystemGenerated(currentEvent))
7743         {
7744             // 2) Sanity check: if the mouse event component source belongs to the same containing window.
7745             Component source = ((MouseEvent)currentEvent).getComponent();
7746             if (source == null || source.getContainingWindow() == getContainingWindow()) {
7747                 focusLog.finest("requesting focus by mouse event \"in window\"");
7748 
7749                 // If both the conditions are fulfilled the focus request should be strictly
7750                 // bounded by the toplevel window. It's assumed that the mouse event activates
7751                 // the window (if it wasn't active) and this makes it possible for a focus
7752                 // request with a strong in-window requirement to change focus in the bounds
7753                 // of the toplevel. If, by any means, due to asynchronous nature of the event
7754                 // dispatching mechanism, the window happens to be natively inactive by the time
7755                 // this focus request is eventually handled, it should not re-activate the
7756                 // toplevel. Otherwise the result may not meet user expectations. See 6981400.
7757                 focusedWindowChangeAllowed = false;


7805         }
7806 
7807         boolean success = peer.requestFocus
7808             (this, temporary, focusedWindowChangeAllowed, time, cause);
7809         if (!success) {
7810             KeyboardFocusManager.getCurrentKeyboardFocusManager
7811                 (appContext).dequeueKeyEvents(time, this);
7812             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7813                 focusLog.finest("Peer request failed");
7814             }
7815         } else {
7816             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7817                 focusLog.finest("Pass for " + this);
7818             }
7819         }
7820         return success;
7821     }
7822 
7823     private boolean isRequestFocusAccepted(boolean temporary,
7824                                            boolean focusedWindowChangeAllowed,
7825                                            CausedFocusEvent.Cause cause)
7826     {
7827         if (!isFocusable() || !isVisible()) {
7828             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7829                 focusLog.finest("Not focusable or not visible");
7830             }
7831             return false;
7832         }
7833 
7834         ComponentPeer peer = this.peer;
7835         if (peer == null) {
7836             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7837                 focusLog.finest("peer is null");
7838             }
7839             return false;
7840         }
7841 
7842         Window window = getContainingWindow();
7843         if (window == null || !window.isFocusableWindow()) {
7844             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7845                 focusLog.finest("Component doesn't have toplevel");


7852         Component focusOwner = KeyboardFocusManager.getMostRecentFocusOwner(window);
7853         if (focusOwner == null) {
7854             // sometimes most recent focus owner may be null, but focus owner is not
7855             // e.g. we reset most recent focus owner if user removes focus owner
7856             focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
7857             if (focusOwner != null && focusOwner.getContainingWindow() != window) {
7858                 focusOwner = null;
7859             }
7860         }
7861 
7862         if (focusOwner == this || focusOwner == null) {
7863             // Controller is supposed to verify focus transfers and for this it
7864             // should know both from and to components.  And it shouldn't verify
7865             // transfers from when these components are equal.
7866             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7867                 focusLog.finest("focus owner is null or this");
7868             }
7869             return true;
7870         }
7871 
7872         if (CausedFocusEvent.Cause.ACTIVATION == cause) {
7873             // we shouldn't call RequestFocusController in case we are
7874             // in activation.  We do request focus on component which
7875             // has got temporary focus lost and then on component which is
7876             // most recent focus owner.  But most recent focus owner can be
7877             // changed by requestFocusXXX() call only, so this transfer has
7878             // been already approved.
7879             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7880                 focusLog.finest("cause is activation");
7881             }
7882             return true;
7883         }
7884 
7885         boolean ret = Component.requestFocusController.acceptRequestFocus(focusOwner,
7886                                                                           this,
7887                                                                           temporary,
7888                                                                           focusedWindowChangeAllowed,
7889                                                                           cause);
7890         if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7891             focusLog.finest("RequestFocusController returns {0}", ret);
7892         }
7893 
7894         return ret;
7895     }
7896 
7897     private static RequestFocusController requestFocusController = new DummyRequestFocusController();
7898 
7899     // Swing access this method through reflection to implement InputVerifier's functionality.
7900     // Perhaps, we should make this method public (later ;)
7901     private static class DummyRequestFocusController implements RequestFocusController {
7902         public boolean acceptRequestFocus(Component from, Component to,
7903                                           boolean temporary, boolean focusedWindowChangeAllowed,
7904                                           CausedFocusEvent.Cause cause)
7905         {
7906             return true;
7907         }
7908     };
7909 
7910     static synchronized void setRequestFocusController(RequestFocusController requestController)
7911     {
7912         if (requestController == null) {
7913             requestFocusController = new DummyRequestFocusController();
7914         } else {
7915             requestFocusController = requestController;
7916         }
7917     }
7918 
7919     /**
7920      * Returns the Container which is the focus cycle root of this Component's
7921      * focus traversal cycle. Each focus traversal cycle has only a single
7922      * focus cycle root and each Component which is not a Container belongs to
7923      * only a single focus traversal cycle. Containers which are focus cycle
7924      * roots belong to two cycles: one rooted at the Container itself, and one


7968     public void transferFocus() {
7969         nextFocus();
7970     }
7971 
7972     /**
7973      * @deprecated As of JDK version 1.1,
7974      * replaced by transferFocus().
7975      */
7976     @Deprecated
7977     public void nextFocus() {
7978         transferFocus(false);
7979     }
7980 
7981     boolean transferFocus(boolean clearOnFailure) {
7982         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7983             focusLog.finer("clearOnFailure = " + clearOnFailure);
7984         }
7985         Component toFocus = getNextFocusCandidate();
7986         boolean res = false;
7987         if (toFocus != null && !toFocus.isFocusOwner() && toFocus != this) {
7988             res = toFocus.requestFocusInWindow(CausedFocusEvent.Cause.TRAVERSAL_FORWARD);
7989         }
7990         if (clearOnFailure && !res) {
7991             if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7992                 focusLog.finer("clear global focus owner");
7993             }
7994             KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
7995         }
7996         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7997             focusLog.finer("returning result: " + res);
7998         }
7999         return res;
8000     }
8001 
8002     final Component getNextFocusCandidate() {
8003         Container rootAncestor = getTraversalRoot();
8004         Component comp = this;
8005         while (rootAncestor != null &&
8006                !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8007         {
8008             comp = rootAncestor;


8048         transferFocusBackward(false);
8049     }
8050 
8051     boolean transferFocusBackward(boolean clearOnFailure) {
8052         Container rootAncestor = getTraversalRoot();
8053         Component comp = this;
8054         while (rootAncestor != null &&
8055                !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8056         {
8057             comp = rootAncestor;
8058             rootAncestor = comp.getFocusCycleRootAncestor();
8059         }
8060         boolean res = false;
8061         if (rootAncestor != null) {
8062             FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
8063             Component toFocus = policy.getComponentBefore(rootAncestor, comp);
8064             if (toFocus == null) {
8065                 toFocus = policy.getDefaultComponent(rootAncestor);
8066             }
8067             if (toFocus != null) {
8068                 res = toFocus.requestFocusInWindow(CausedFocusEvent.Cause.TRAVERSAL_BACKWARD);
8069             }
8070         }
8071         if (clearOnFailure && !res) {
8072             if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8073                 focusLog.finer("clear global focus owner");
8074             }
8075             KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
8076         }
8077         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8078             focusLog.finer("returning result: " + res);
8079         }
8080         return res;
8081     }
8082 
8083     /**
8084      * Transfers the focus up one focus traversal cycle. Typically, the focus
8085      * owner is set to this Component's focus cycle root, and the current focus
8086      * cycle root is set to the new focus owner's focus cycle root. If,
8087      * however, this Component's focus cycle root is a Window, then the focus
8088      * owner is set to the focus cycle root's default Component to focus, and


8093      * @see       Container#setFocusCycleRoot(boolean)
8094      * @since     1.4
8095      */
8096     public void transferFocusUpCycle() {
8097         Container rootAncestor;
8098         for (rootAncestor = getFocusCycleRootAncestor();
8099              rootAncestor != null && !(rootAncestor.isShowing() &&
8100                                        rootAncestor.isFocusable() &&
8101                                        rootAncestor.isEnabled());
8102              rootAncestor = rootAncestor.getFocusCycleRootAncestor()) {
8103         }
8104 
8105         if (rootAncestor != null) {
8106             Container rootAncestorRootAncestor =
8107                 rootAncestor.getFocusCycleRootAncestor();
8108             Container fcr = (rootAncestorRootAncestor != null) ?
8109                 rootAncestorRootAncestor : rootAncestor;
8110 
8111             KeyboardFocusManager.getCurrentKeyboardFocusManager().
8112                 setGlobalCurrentFocusCycleRootPriv(fcr);
8113             rootAncestor.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_UP);
8114         } else {
8115             Window window = getContainingWindow();
8116 
8117             if (window != null) {
8118                 Component toFocus = window.getFocusTraversalPolicy().
8119                     getDefaultComponent(window);
8120                 if (toFocus != null) {
8121                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
8122                         setGlobalCurrentFocusCycleRootPriv(window);
8123                     toFocus.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_UP);
8124                 }
8125             }
8126         }
8127     }
8128 
8129     /**
8130      * Returns <code>true</code> if this <code>Component</code> is the
8131      * focus owner.  This method is obsolete, and has been replaced by
8132      * <code>isFocusOwner()</code>.
8133      *
8134      * @return <code>true</code> if this <code>Component</code> is the
8135      *         focus owner; <code>false</code> otherwise
8136      * @since 1.2
8137      */
8138     public boolean hasFocus() {
8139         return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
8140                 getFocusOwner() == this);
8141     }
8142 
8143     /**




  51 import java.beans.PropertyChangeSupport;
  52 import java.beans.Transient;
  53 import java.awt.im.InputContext;
  54 import java.awt.im.InputMethodRequests;
  55 import java.awt.dnd.DropTarget;
  56 import java.lang.reflect.InvocationTargetException;
  57 import java.lang.reflect.Method;
  58 import java.security.AccessController;
  59 import java.security.PrivilegedAction;
  60 import java.security.AccessControlContext;
  61 import javax.accessibility.*;
  62 import java.applet.Applet;
  63 
  64 import sun.awt.ComponentFactory;
  65 import sun.security.action.GetPropertyAction;
  66 import sun.awt.AppContext;
  67 import sun.awt.AWTAccessor;
  68 import sun.awt.ConstrainableGraphics;
  69 import sun.awt.SubRegionShowable;
  70 import sun.awt.SunToolkit;

  71 import sun.awt.EmbeddedFrame;
  72 import sun.awt.dnd.SunDropTargetEvent;
  73 import sun.awt.im.CompositionArea;
  74 import sun.font.FontManager;
  75 import sun.font.FontManagerFactory;
  76 import sun.font.SunFontManager;
  77 import sun.java2d.SunGraphics2D;
  78 import sun.java2d.pipe.Region;
  79 import sun.awt.image.VSyncedBSManager;
  80 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  81 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*;
  82 import sun.awt.RequestFocusController;
  83 import sun.java2d.SunGraphicsEnvironment;
  84 import sun.util.logging.PlatformLogger;
  85 
  86 /**
  87  * A <em>component</em> is an object having a graphical representation
  88  * that can be displayed on the screen and that can interact with the
  89  * user. Examples of components are the buttons, checkboxes, and scrollbars
  90  * of a typical graphical user interface. <p>


 860                     if (!comp.isNonOpaqueForMixing()) {
 861                         needShowing = true;
 862                     }
 863 
 864                     if (comp.isMixingNeeded()) {
 865                         if (needHiding) {
 866                             comp.mixOnHiding(comp.isLightweight());
 867                         }
 868                         if (needShowing) {
 869                             comp.mixOnShowing();
 870                         }
 871                     }
 872                 }
 873             }
 874 
 875             public void setGraphicsConfiguration(Component comp,
 876                     GraphicsConfiguration gc)
 877             {
 878                 comp.setGraphicsConfiguration(gc);
 879             }
 880             public boolean requestFocus(Component comp, FocusEvent.Cause cause) {
 881                 return comp.requestFocus(cause);
 882             }
 883             public boolean canBeFocusOwner(Component comp) {
 884                 return comp.canBeFocusOwner();
 885             }
 886 
 887             public boolean isVisible(Component comp) {
 888                 return comp.isVisible_NoClientCode();
 889             }
 890             public void setRequestFocusController
 891                 (RequestFocusController requestController)
 892             {
 893                  Component.setRequestFocusController(requestController);
 894             }
 895             public AppContext getAppContext(Component comp) {
 896                  return comp.appContext;
 897             }
 898             public void setAppContext(Component comp, AppContext appContext) {
 899                  comp.appContext = appContext;
 900             }


7522      * Because the focus behavior of this method is platform-dependent,
7523      * developers are strongly encouraged to use
7524      * <code>requestFocusInWindow</code> when possible.
7525      *
7526      * <p>Note: Not all focus transfers result from invoking this method. As
7527      * such, a component may receive focus without this or any of the other
7528      * {@code requestFocus} methods of {@code Component} being invoked.
7529      *
7530      * @see #requestFocusInWindow
7531      * @see java.awt.event.FocusEvent
7532      * @see #addFocusListener
7533      * @see #isFocusable
7534      * @see #isDisplayable
7535      * @see KeyboardFocusManager#clearGlobalFocusOwner
7536      * @since 1.0
7537      */
7538     public void requestFocus() {
7539         requestFocusHelper(false, true);
7540     }
7541 
7542     boolean requestFocus(FocusEvent.Cause cause) {
7543         return requestFocusHelper(false, true, cause);
7544     }
7545 
7546     /**
7547      * Requests that this <code>Component</code> get the input focus,
7548      * and that this <code>Component</code>'s top-level ancestor
7549      * become the focused <code>Window</code>. This component must be
7550      * displayable, focusable, visible and all of its ancestors (with
7551      * the exception of the top-level Window) must be visible for the
7552      * request to be granted. Every effort will be made to honor the
7553      * request; however, in some cases it may be impossible to do
7554      * so. Developers must never assume that this component is the
7555      * focus owner until this component receives a FOCUS_GAINED
7556      * event. If this request is denied because this component's
7557      * top-level window cannot become the focused window, the request
7558      * will be remembered and will be granted when the window is later
7559      * focused by the user.
7560      * <p>
7561      * This method returns a boolean value. If <code>false</code> is returned,
7562      * the request is <b>guaranteed to fail</b>. If <code>true</code> is


7589      * such, a component may receive focus without this or any of the other
7590      * {@code requestFocus} methods of {@code Component} being invoked.
7591      *
7592      * @param temporary true if the focus change is temporary,
7593      *        such as when the window loses the focus; for
7594      *        more information on temporary focus changes see the
7595      *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7596      * @return <code>false</code> if the focus change request is guaranteed to
7597      *         fail; <code>true</code> if it is likely to succeed
7598      * @see java.awt.event.FocusEvent
7599      * @see #addFocusListener
7600      * @see #isFocusable
7601      * @see #isDisplayable
7602      * @see KeyboardFocusManager#clearGlobalFocusOwner
7603      * @since 1.4
7604      */
7605     protected boolean requestFocus(boolean temporary) {
7606         return requestFocusHelper(temporary, true);
7607     }
7608 
7609     boolean requestFocus(boolean temporary, FocusEvent.Cause cause) {
7610         return requestFocusHelper(temporary, true, cause);
7611     }
7612     /**
7613      * Requests that this Component get the input focus, if this
7614      * Component's top-level ancestor is already the focused
7615      * Window. This component must be displayable, focusable, visible
7616      * and all of its ancestors (with the exception of the top-level
7617      * Window) must be visible for the request to be granted. Every
7618      * effort will be made to honor the request; however, in some
7619      * cases it may be impossible to do so. Developers must never
7620      * assume that this Component is the focus owner until this
7621      * Component receives a FOCUS_GAINED event.
7622      * <p>
7623      * This method returns a boolean value. If <code>false</code> is returned,
7624      * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7625      * returned, the request will succeed <b>unless</b> it is vetoed, or an
7626      * extraordinary event, such as disposal of the Component's peer, occurs
7627      * before the request can be granted by the native windowing system. Again,
7628      * while a return value of <code>true</code> indicates that the request is
7629      * likely to succeed, developers must never assume that this Component is


7640      * different platforms.
7641      *
7642      * <p>Note: Not all focus transfers result from invoking this method. As
7643      * such, a component may receive focus without this or any of the other
7644      * {@code requestFocus} methods of {@code Component} being invoked.
7645      *
7646      * @return <code>false</code> if the focus change request is guaranteed to
7647      *         fail; <code>true</code> if it is likely to succeed
7648      * @see #requestFocus
7649      * @see java.awt.event.FocusEvent
7650      * @see #addFocusListener
7651      * @see #isFocusable
7652      * @see #isDisplayable
7653      * @see KeyboardFocusManager#clearGlobalFocusOwner
7654      * @since 1.4
7655      */
7656     public boolean requestFocusInWindow() {
7657         return requestFocusHelper(false, false);
7658     }
7659 
7660     boolean requestFocusInWindow(FocusEvent.Cause cause) {
7661         return requestFocusHelper(false, false, cause);
7662     }
7663 
7664     /**
7665      * Requests that this <code>Component</code> get the input focus,
7666      * if this <code>Component</code>'s top-level ancestor is already
7667      * the focused <code>Window</code>.  This component must be
7668      * displayable, focusable, visible and all of its ancestors (with
7669      * the exception of the top-level Window) must be visible for the
7670      * request to be granted. Every effort will be made to honor the
7671      * request; however, in some cases it may be impossible to do
7672      * so. Developers must never assume that this component is the
7673      * focus owner until this component receives a FOCUS_GAINED event.
7674      * <p>
7675      * This method returns a boolean value. If <code>false</code> is returned,
7676      * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7677      * returned, the request will succeed <b>unless</b> it is vetoed, or an
7678      * extraordinary event, such as disposal of the component's peer, occurs
7679      * before the request can be granted by the native windowing system. Again,
7680      * while a return value of <code>true</code> indicates that the request is


7705      * {@code requestFocus} methods of {@code Component} being invoked.
7706      *
7707      * @param temporary true if the focus change is temporary,
7708      *        such as when the window loses the focus; for
7709      *        more information on temporary focus changes see the
7710      *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7711      * @return <code>false</code> if the focus change request is guaranteed to
7712      *         fail; <code>true</code> if it is likely to succeed
7713      * @see #requestFocus
7714      * @see java.awt.event.FocusEvent
7715      * @see #addFocusListener
7716      * @see #isFocusable
7717      * @see #isDisplayable
7718      * @see KeyboardFocusManager#clearGlobalFocusOwner
7719      * @since 1.4
7720      */
7721     protected boolean requestFocusInWindow(boolean temporary) {
7722         return requestFocusHelper(temporary, false);
7723     }
7724 
7725     boolean requestFocusInWindow(boolean temporary, FocusEvent.Cause cause) {
7726         return requestFocusHelper(temporary, false, cause);
7727     }
7728 
7729     final boolean requestFocusHelper(boolean temporary,
7730                                      boolean focusedWindowChangeAllowed) {
7731         return requestFocusHelper(temporary, focusedWindowChangeAllowed, FocusEvent.Cause.UNKNOWN);
7732     }
7733 
7734     final boolean requestFocusHelper(boolean temporary,
7735                                      boolean focusedWindowChangeAllowed,
7736                                      FocusEvent.Cause cause)
7737     {
7738         // 1) Check if the event being dispatched is a system-generated mouse event.
7739         AWTEvent currentEvent = EventQueue.getCurrentEvent();
7740         if (currentEvent instanceof MouseEvent &&
7741             SunToolkit.isSystemGenerated(currentEvent))
7742         {
7743             // 2) Sanity check: if the mouse event component source belongs to the same containing window.
7744             Component source = ((MouseEvent)currentEvent).getComponent();
7745             if (source == null || source.getContainingWindow() == getContainingWindow()) {
7746                 focusLog.finest("requesting focus by mouse event \"in window\"");
7747 
7748                 // If both the conditions are fulfilled the focus request should be strictly
7749                 // bounded by the toplevel window. It's assumed that the mouse event activates
7750                 // the window (if it wasn't active) and this makes it possible for a focus
7751                 // request with a strong in-window requirement to change focus in the bounds
7752                 // of the toplevel. If, by any means, due to asynchronous nature of the event
7753                 // dispatching mechanism, the window happens to be natively inactive by the time
7754                 // this focus request is eventually handled, it should not re-activate the
7755                 // toplevel. Otherwise the result may not meet user expectations. See 6981400.
7756                 focusedWindowChangeAllowed = false;


7804         }
7805 
7806         boolean success = peer.requestFocus
7807             (this, temporary, focusedWindowChangeAllowed, time, cause);
7808         if (!success) {
7809             KeyboardFocusManager.getCurrentKeyboardFocusManager
7810                 (appContext).dequeueKeyEvents(time, this);
7811             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7812                 focusLog.finest("Peer request failed");
7813             }
7814         } else {
7815             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7816                 focusLog.finest("Pass for " + this);
7817             }
7818         }
7819         return success;
7820     }
7821 
7822     private boolean isRequestFocusAccepted(boolean temporary,
7823                                            boolean focusedWindowChangeAllowed,
7824                                            FocusEvent.Cause cause)
7825     {
7826         if (!isFocusable() || !isVisible()) {
7827             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7828                 focusLog.finest("Not focusable or not visible");
7829             }
7830             return false;
7831         }
7832 
7833         ComponentPeer peer = this.peer;
7834         if (peer == null) {
7835             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7836                 focusLog.finest("peer is null");
7837             }
7838             return false;
7839         }
7840 
7841         Window window = getContainingWindow();
7842         if (window == null || !window.isFocusableWindow()) {
7843             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7844                 focusLog.finest("Component doesn't have toplevel");


7851         Component focusOwner = KeyboardFocusManager.getMostRecentFocusOwner(window);
7852         if (focusOwner == null) {
7853             // sometimes most recent focus owner may be null, but focus owner is not
7854             // e.g. we reset most recent focus owner if user removes focus owner
7855             focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
7856             if (focusOwner != null && focusOwner.getContainingWindow() != window) {
7857                 focusOwner = null;
7858             }
7859         }
7860 
7861         if (focusOwner == this || focusOwner == null) {
7862             // Controller is supposed to verify focus transfers and for this it
7863             // should know both from and to components.  And it shouldn't verify
7864             // transfers from when these components are equal.
7865             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7866                 focusLog.finest("focus owner is null or this");
7867             }
7868             return true;
7869         }
7870 
7871         if (FocusEvent.Cause.ACTIVATION == cause) {
7872             // we shouldn't call RequestFocusController in case we are
7873             // in activation.  We do request focus on component which
7874             // has got temporary focus lost and then on component which is
7875             // most recent focus owner.  But most recent focus owner can be
7876             // changed by requestFocusXXX() call only, so this transfer has
7877             // been already approved.
7878             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7879                 focusLog.finest("cause is activation");
7880             }
7881             return true;
7882         }
7883 
7884         boolean ret = Component.requestFocusController.acceptRequestFocus(focusOwner,
7885                                                                           this,
7886                                                                           temporary,
7887                                                                           focusedWindowChangeAllowed,
7888                                                                           cause);
7889         if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7890             focusLog.finest("RequestFocusController returns {0}", ret);
7891         }
7892 
7893         return ret;
7894     }
7895 
7896     private static RequestFocusController requestFocusController = new DummyRequestFocusController();
7897 
7898     // Swing access this method through reflection to implement InputVerifier's functionality.
7899     // Perhaps, we should make this method public (later ;)
7900     private static class DummyRequestFocusController implements RequestFocusController {
7901         public boolean acceptRequestFocus(Component from, Component to,
7902                                           boolean temporary, boolean focusedWindowChangeAllowed,
7903                                           FocusEvent.Cause cause)
7904         {
7905             return true;
7906         }
7907     };
7908 
7909     static synchronized void setRequestFocusController(RequestFocusController requestController)
7910     {
7911         if (requestController == null) {
7912             requestFocusController = new DummyRequestFocusController();
7913         } else {
7914             requestFocusController = requestController;
7915         }
7916     }
7917 
7918     /**
7919      * Returns the Container which is the focus cycle root of this Component's
7920      * focus traversal cycle. Each focus traversal cycle has only a single
7921      * focus cycle root and each Component which is not a Container belongs to
7922      * only a single focus traversal cycle. Containers which are focus cycle
7923      * roots belong to two cycles: one rooted at the Container itself, and one


7967     public void transferFocus() {
7968         nextFocus();
7969     }
7970 
7971     /**
7972      * @deprecated As of JDK version 1.1,
7973      * replaced by transferFocus().
7974      */
7975     @Deprecated
7976     public void nextFocus() {
7977         transferFocus(false);
7978     }
7979 
7980     boolean transferFocus(boolean clearOnFailure) {
7981         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7982             focusLog.finer("clearOnFailure = " + clearOnFailure);
7983         }
7984         Component toFocus = getNextFocusCandidate();
7985         boolean res = false;
7986         if (toFocus != null && !toFocus.isFocusOwner() && toFocus != this) {
7987             res = toFocus.requestFocusInWindow(FocusEvent.Cause.TRAVERSAL_FORWARD);
7988         }
7989         if (clearOnFailure && !res) {
7990             if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7991                 focusLog.finer("clear global focus owner");
7992             }
7993             KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
7994         }
7995         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7996             focusLog.finer("returning result: " + res);
7997         }
7998         return res;
7999     }
8000 
8001     final Component getNextFocusCandidate() {
8002         Container rootAncestor = getTraversalRoot();
8003         Component comp = this;
8004         while (rootAncestor != null &&
8005                !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8006         {
8007             comp = rootAncestor;


8047         transferFocusBackward(false);
8048     }
8049 
8050     boolean transferFocusBackward(boolean clearOnFailure) {
8051         Container rootAncestor = getTraversalRoot();
8052         Component comp = this;
8053         while (rootAncestor != null &&
8054                !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8055         {
8056             comp = rootAncestor;
8057             rootAncestor = comp.getFocusCycleRootAncestor();
8058         }
8059         boolean res = false;
8060         if (rootAncestor != null) {
8061             FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
8062             Component toFocus = policy.getComponentBefore(rootAncestor, comp);
8063             if (toFocus == null) {
8064                 toFocus = policy.getDefaultComponent(rootAncestor);
8065             }
8066             if (toFocus != null) {
8067                 res = toFocus.requestFocusInWindow(FocusEvent.Cause.TRAVERSAL_BACKWARD);
8068             }
8069         }
8070         if (clearOnFailure && !res) {
8071             if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8072                 focusLog.finer("clear global focus owner");
8073             }
8074             KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
8075         }
8076         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8077             focusLog.finer("returning result: " + res);
8078         }
8079         return res;
8080     }
8081 
8082     /**
8083      * Transfers the focus up one focus traversal cycle. Typically, the focus
8084      * owner is set to this Component's focus cycle root, and the current focus
8085      * cycle root is set to the new focus owner's focus cycle root. If,
8086      * however, this Component's focus cycle root is a Window, then the focus
8087      * owner is set to the focus cycle root's default Component to focus, and


8092      * @see       Container#setFocusCycleRoot(boolean)
8093      * @since     1.4
8094      */
8095     public void transferFocusUpCycle() {
8096         Container rootAncestor;
8097         for (rootAncestor = getFocusCycleRootAncestor();
8098              rootAncestor != null && !(rootAncestor.isShowing() &&
8099                                        rootAncestor.isFocusable() &&
8100                                        rootAncestor.isEnabled());
8101              rootAncestor = rootAncestor.getFocusCycleRootAncestor()) {
8102         }
8103 
8104         if (rootAncestor != null) {
8105             Container rootAncestorRootAncestor =
8106                 rootAncestor.getFocusCycleRootAncestor();
8107             Container fcr = (rootAncestorRootAncestor != null) ?
8108                 rootAncestorRootAncestor : rootAncestor;
8109 
8110             KeyboardFocusManager.getCurrentKeyboardFocusManager().
8111                 setGlobalCurrentFocusCycleRootPriv(fcr);
8112             rootAncestor.requestFocus(FocusEvent.Cause.TRAVERSAL_UP);
8113         } else {
8114             Window window = getContainingWindow();
8115 
8116             if (window != null) {
8117                 Component toFocus = window.getFocusTraversalPolicy().
8118                     getDefaultComponent(window);
8119                 if (toFocus != null) {
8120                     KeyboardFocusManager.getCurrentKeyboardFocusManager().
8121                         setGlobalCurrentFocusCycleRootPriv(window);
8122                     toFocus.requestFocus(FocusEvent.Cause.TRAVERSAL_UP);
8123                 }
8124             }
8125         }
8126     }
8127 
8128     /**
8129      * Returns <code>true</code> if this <code>Component</code> is the
8130      * focus owner.  This method is obsolete, and has been replaced by
8131      * <code>isFocusOwner()</code>.
8132      *
8133      * @return <code>true</code> if this <code>Component</code> is the
8134      *         focus owner; <code>false</code> otherwise
8135      * @since 1.2
8136      */
8137     public boolean hasFocus() {
8138         return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
8139                 getFocusOwner() == this);
8140     }
8141 
8142     /**


< prev index next >