< 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             }


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


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


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


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


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


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


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


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


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




  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             }


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


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


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


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


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


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


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


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


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


< prev index next >