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 }
7532 * Because the focus behavior of this method is platform-dependent,
7533 * developers are strongly encouraged to use
7534 * <code>requestFocusInWindow</code> when possible.
7535 *
7536 * <p>Note: Not all focus transfers result from invoking this method. As
7537 * such, a component may receive focus without this or any of the other
7538 * {@code requestFocus} methods of {@code Component} being invoked.
7539 *
7540 * @see #requestFocusInWindow
7541 * @see java.awt.event.FocusEvent
7542 * @see #addFocusListener
7543 * @see #isFocusable
7544 * @see #isDisplayable
7545 * @see KeyboardFocusManager#clearGlobalFocusOwner
7546 * @since 1.0
7547 */
7548 public void requestFocus() {
7549 requestFocusHelper(false, true);
7550 }
7551
7552 boolean requestFocus(CausedFocusEvent.Cause cause) {
7553 return requestFocusHelper(false, true, cause);
7554 }
7555
7556 /**
7557 * Requests that this <code>Component</code> get the input focus,
7558 * and that this <code>Component</code>'s top-level ancestor
7559 * become the focused <code>Window</code>. This component must be
7560 * displayable, focusable, visible and all of its ancestors (with
7561 * the exception of the top-level Window) must be visible for the
7562 * request to be granted. Every effort will be made to honor the
7563 * request; however, in some cases it may be impossible to do
7564 * so. Developers must never assume that this component is the
7565 * focus owner until this component receives a FOCUS_GAINED
7566 * event. If this request is denied because this component's
7567 * top-level window cannot become the focused window, the request
7568 * will be remembered and will be granted when the window is later
7569 * focused by the user.
7570 * <p>
7571 * This method returns a boolean value. If <code>false</code> is returned,
7572 * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7599 * such, a component may receive focus without this or any of the other
7600 * {@code requestFocus} methods of {@code Component} being invoked.
7601 *
7602 * @param temporary true if the focus change is temporary,
7603 * such as when the window loses the focus; for
7604 * more information on temporary focus changes see the
7605 *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7606 * @return <code>false</code> if the focus change request is guaranteed to
7607 * fail; <code>true</code> if it is likely to succeed
7608 * @see java.awt.event.FocusEvent
7609 * @see #addFocusListener
7610 * @see #isFocusable
7611 * @see #isDisplayable
7612 * @see KeyboardFocusManager#clearGlobalFocusOwner
7613 * @since 1.4
7614 */
7615 protected boolean requestFocus(boolean temporary) {
7616 return requestFocusHelper(temporary, true);
7617 }
7618
7619 boolean requestFocus(boolean temporary, CausedFocusEvent.Cause cause) {
7620 return requestFocusHelper(temporary, true, cause);
7621 }
7622 /**
7623 * Requests that this Component get the input focus, if this
7624 * Component's top-level ancestor is already the focused
7625 * Window. This component must be displayable, focusable, visible
7626 * and all of its ancestors (with the exception of the top-level
7627 * Window) must be visible for the request to be granted. Every
7628 * effort will be made to honor the request; however, in some
7629 * cases it may be impossible to do so. Developers must never
7630 * assume that this Component is the focus owner until this
7631 * Component receives a FOCUS_GAINED event.
7632 * <p>
7633 * This method returns a boolean value. If <code>false</code> is returned,
7634 * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7635 * returned, the request will succeed <b>unless</b> it is vetoed, or an
7636 * extraordinary event, such as disposal of the Component's peer, occurs
7637 * before the request can be granted by the native windowing system. Again,
7638 * while a return value of <code>true</code> indicates that the request is
7639 * likely to succeed, developers must never assume that this Component is
7650 * different platforms.
7651 *
7652 * <p>Note: Not all focus transfers result from invoking this method. As
7653 * such, a component may receive focus without this or any of the other
7654 * {@code requestFocus} methods of {@code Component} being invoked.
7655 *
7656 * @return <code>false</code> if the focus change request is guaranteed to
7657 * fail; <code>true</code> if it is likely to succeed
7658 * @see #requestFocus
7659 * @see java.awt.event.FocusEvent
7660 * @see #addFocusListener
7661 * @see #isFocusable
7662 * @see #isDisplayable
7663 * @see KeyboardFocusManager#clearGlobalFocusOwner
7664 * @since 1.4
7665 */
7666 public boolean requestFocusInWindow() {
7667 return requestFocusHelper(false, false);
7668 }
7669
7670 boolean requestFocusInWindow(CausedFocusEvent.Cause cause) {
7671 return requestFocusHelper(false, false, cause);
7672 }
7673
7674 /**
7675 * Requests that this <code>Component</code> get the input focus,
7676 * if this <code>Component</code>'s top-level ancestor is already
7677 * the focused <code>Window</code>. This component must be
7678 * displayable, focusable, visible and all of its ancestors (with
7679 * the exception of the top-level Window) must be visible for the
7680 * request to be granted. Every effort will be made to honor the
7681 * request; however, in some cases it may be impossible to do
7682 * so. Developers must never assume that this component is the
7683 * focus owner until this component receives a FOCUS_GAINED event.
7684 * <p>
7685 * This method returns a boolean value. If <code>false</code> is returned,
7686 * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7687 * returned, the request will succeed <b>unless</b> it is vetoed, or an
7688 * extraordinary event, such as disposal of the component's peer, occurs
7689 * before the request can be granted by the native windowing system. Again,
7690 * while a return value of <code>true</code> indicates that the request is
7715 * {@code requestFocus} methods of {@code Component} being invoked.
7716 *
7717 * @param temporary true if the focus change is temporary,
7718 * such as when the window loses the focus; for
7719 * more information on temporary focus changes see the
7720 *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7721 * @return <code>false</code> if the focus change request is guaranteed to
7722 * fail; <code>true</code> if it is likely to succeed
7723 * @see #requestFocus
7724 * @see java.awt.event.FocusEvent
7725 * @see #addFocusListener
7726 * @see #isFocusable
7727 * @see #isDisplayable
7728 * @see KeyboardFocusManager#clearGlobalFocusOwner
7729 * @since 1.4
7730 */
7731 protected boolean requestFocusInWindow(boolean temporary) {
7732 return requestFocusHelper(temporary, false);
7733 }
7734
7735 boolean requestFocusInWindow(boolean temporary, CausedFocusEvent.Cause cause) {
7736 return requestFocusHelper(temporary, false, cause);
7737 }
7738
7739 final boolean requestFocusHelper(boolean temporary,
7740 boolean focusedWindowChangeAllowed) {
7741 return requestFocusHelper(temporary, focusedWindowChangeAllowed, CausedFocusEvent.Cause.UNKNOWN);
7742 }
7743
7744 final boolean requestFocusHelper(boolean temporary,
7745 boolean focusedWindowChangeAllowed,
7746 CausedFocusEvent.Cause cause)
7747 {
7748 // 1) Check if the event being dispatched is a system-generated mouse event.
7749 AWTEvent currentEvent = EventQueue.getCurrentEvent();
7750 if (currentEvent instanceof MouseEvent &&
7751 SunToolkit.isSystemGenerated(currentEvent))
7752 {
7753 // 2) Sanity check: if the mouse event component source belongs to the same containing window.
7754 Component source = ((MouseEvent)currentEvent).getComponent();
7755 if (source == null || source.getContainingWindow() == getContainingWindow()) {
7756 focusLog.finest("requesting focus by mouse event \"in window\"");
7757
7758 // If both the conditions are fulfilled the focus request should be strictly
7759 // bounded by the toplevel window. It's assumed that the mouse event activates
7760 // the window (if it wasn't active) and this makes it possible for a focus
7761 // request with a strong in-window requirement to change focus in the bounds
7762 // of the toplevel. If, by any means, due to asynchronous nature of the event
7763 // dispatching mechanism, the window happens to be natively inactive by the time
7764 // this focus request is eventually handled, it should not re-activate the
7765 // toplevel. Otherwise the result may not meet user expectations. See 6981400.
7766 focusedWindowChangeAllowed = false;
7814 }
7815
7816 boolean success = peer.requestFocus
7817 (this, temporary, focusedWindowChangeAllowed, time, cause);
7818 if (!success) {
7819 KeyboardFocusManager.getCurrentKeyboardFocusManager
7820 (appContext).dequeueKeyEvents(time, this);
7821 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7822 focusLog.finest("Peer request failed");
7823 }
7824 } else {
7825 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7826 focusLog.finest("Pass for " + this);
7827 }
7828 }
7829 return success;
7830 }
7831
7832 private boolean isRequestFocusAccepted(boolean temporary,
7833 boolean focusedWindowChangeAllowed,
7834 CausedFocusEvent.Cause cause)
7835 {
7836 if (!isFocusable() || !isVisible()) {
7837 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7838 focusLog.finest("Not focusable or not visible");
7839 }
7840 return false;
7841 }
7842
7843 ComponentPeer peer = this.peer;
7844 if (peer == null) {
7845 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7846 focusLog.finest("peer is null");
7847 }
7848 return false;
7849 }
7850
7851 Window window = getContainingWindow();
7852 if (window == null || !window.isFocusableWindow()) {
7853 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7854 focusLog.finest("Component doesn't have toplevel");
7861 Component focusOwner = KeyboardFocusManager.getMostRecentFocusOwner(window);
7862 if (focusOwner == null) {
7863 // sometimes most recent focus owner may be null, but focus owner is not
7864 // e.g. we reset most recent focus owner if user removes focus owner
7865 focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
7866 if (focusOwner != null && focusOwner.getContainingWindow() != window) {
7867 focusOwner = null;
7868 }
7869 }
7870
7871 if (focusOwner == this || focusOwner == null) {
7872 // Controller is supposed to verify focus transfers and for this it
7873 // should know both from and to components. And it shouldn't verify
7874 // transfers from when these components are equal.
7875 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7876 focusLog.finest("focus owner is null or this");
7877 }
7878 return true;
7879 }
7880
7881 if (CausedFocusEvent.Cause.ACTIVATION == cause) {
7882 // we shouldn't call RequestFocusController in case we are
7883 // in activation. We do request focus on component which
7884 // has got temporary focus lost and then on component which is
7885 // most recent focus owner. But most recent focus owner can be
7886 // changed by requestFocusXXX() call only, so this transfer has
7887 // been already approved.
7888 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7889 focusLog.finest("cause is activation");
7890 }
7891 return true;
7892 }
7893
7894 boolean ret = Component.requestFocusController.acceptRequestFocus(focusOwner,
7895 this,
7896 temporary,
7897 focusedWindowChangeAllowed,
7898 cause);
7899 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7900 focusLog.finest("RequestFocusController returns {0}", ret);
7901 }
7902
7903 return ret;
7904 }
7905
7906 private static RequestFocusController requestFocusController = new DummyRequestFocusController();
7907
7908 // Swing access this method through reflection to implement InputVerifier's functionality.
7909 // Perhaps, we should make this method public (later ;)
7910 private static class DummyRequestFocusController implements RequestFocusController {
7911 public boolean acceptRequestFocus(Component from, Component to,
7912 boolean temporary, boolean focusedWindowChangeAllowed,
7913 CausedFocusEvent.Cause cause)
7914 {
7915 return true;
7916 }
7917 };
7918
7919 synchronized static void setRequestFocusController(RequestFocusController requestController)
7920 {
7921 if (requestController == null) {
7922 requestFocusController = new DummyRequestFocusController();
7923 } else {
7924 requestFocusController = requestController;
7925 }
7926 }
7927
7928 /**
7929 * Returns the Container which is the focus cycle root of this Component's
7930 * focus traversal cycle. Each focus traversal cycle has only a single
7931 * focus cycle root and each Component which is not a Container belongs to
7932 * only a single focus traversal cycle. Containers which are focus cycle
7933 * roots belong to two cycles: one rooted at the Container itself, and one
7977 public void transferFocus() {
7978 nextFocus();
7979 }
7980
7981 /**
7982 * @deprecated As of JDK version 1.1,
7983 * replaced by transferFocus().
7984 */
7985 @Deprecated
7986 public void nextFocus() {
7987 transferFocus(false);
7988 }
7989
7990 boolean transferFocus(boolean clearOnFailure) {
7991 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7992 focusLog.finer("clearOnFailure = " + clearOnFailure);
7993 }
7994 Component toFocus = getNextFocusCandidate();
7995 boolean res = false;
7996 if (toFocus != null && !toFocus.isFocusOwner() && toFocus != this) {
7997 res = toFocus.requestFocusInWindow(CausedFocusEvent.Cause.TRAVERSAL_FORWARD);
7998 }
7999 if (clearOnFailure && !res) {
8000 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8001 focusLog.finer("clear global focus owner");
8002 }
8003 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
8004 }
8005 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8006 focusLog.finer("returning result: " + res);
8007 }
8008 return res;
8009 }
8010
8011 final Component getNextFocusCandidate() {
8012 Container rootAncestor = getTraversalRoot();
8013 Component comp = this;
8014 while (rootAncestor != null &&
8015 !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8016 {
8017 comp = rootAncestor;
8057 transferFocusBackward(false);
8058 }
8059
8060 boolean transferFocusBackward(boolean clearOnFailure) {
8061 Container rootAncestor = getTraversalRoot();
8062 Component comp = this;
8063 while (rootAncestor != null &&
8064 !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8065 {
8066 comp = rootAncestor;
8067 rootAncestor = comp.getFocusCycleRootAncestor();
8068 }
8069 boolean res = false;
8070 if (rootAncestor != null) {
8071 FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
8072 Component toFocus = policy.getComponentBefore(rootAncestor, comp);
8073 if (toFocus == null) {
8074 toFocus = policy.getDefaultComponent(rootAncestor);
8075 }
8076 if (toFocus != null) {
8077 res = toFocus.requestFocusInWindow(CausedFocusEvent.Cause.TRAVERSAL_BACKWARD);
8078 }
8079 }
8080 if (clearOnFailure && !res) {
8081 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8082 focusLog.finer("clear global focus owner");
8083 }
8084 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
8085 }
8086 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8087 focusLog.finer("returning result: " + res);
8088 }
8089 return res;
8090 }
8091
8092 /**
8093 * Transfers the focus up one focus traversal cycle. Typically, the focus
8094 * owner is set to this Component's focus cycle root, and the current focus
8095 * cycle root is set to the new focus owner's focus cycle root. If,
8096 * however, this Component's focus cycle root is a Window, then the focus
8097 * owner is set to the focus cycle root's default Component to focus, and
8102 * @see Container#setFocusCycleRoot(boolean)
8103 * @since 1.4
8104 */
8105 public void transferFocusUpCycle() {
8106 Container rootAncestor;
8107 for (rootAncestor = getFocusCycleRootAncestor();
8108 rootAncestor != null && !(rootAncestor.isShowing() &&
8109 rootAncestor.isFocusable() &&
8110 rootAncestor.isEnabled());
8111 rootAncestor = rootAncestor.getFocusCycleRootAncestor()) {
8112 }
8113
8114 if (rootAncestor != null) {
8115 Container rootAncestorRootAncestor =
8116 rootAncestor.getFocusCycleRootAncestor();
8117 Container fcr = (rootAncestorRootAncestor != null) ?
8118 rootAncestorRootAncestor : rootAncestor;
8119
8120 KeyboardFocusManager.getCurrentKeyboardFocusManager().
8121 setGlobalCurrentFocusCycleRootPriv(fcr);
8122 rootAncestor.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_UP);
8123 } else {
8124 Window window = getContainingWindow();
8125
8126 if (window != null) {
8127 Component toFocus = window.getFocusTraversalPolicy().
8128 getDefaultComponent(window);
8129 if (toFocus != null) {
8130 KeyboardFocusManager.getCurrentKeyboardFocusManager().
8131 setGlobalCurrentFocusCycleRootPriv(window);
8132 toFocus.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_UP);
8133 }
8134 }
8135 }
8136 }
8137
8138 /**
8139 * Returns <code>true</code> if this <code>Component</code> is the
8140 * focus owner. This method is obsolete, and has been replaced by
8141 * <code>isFocusOwner()</code>.
8142 *
8143 * @return <code>true</code> if this <code>Component</code> is the
8144 * focus owner; <code>false</code> otherwise
8145 * @since 1.2
8146 */
8147 public boolean hasFocus() {
8148 return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
8149 getFocusOwner() == this);
8150 }
8151
8152 /**
|
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 }
7531 * Because the focus behavior of this method is platform-dependent,
7532 * developers are strongly encouraged to use
7533 * <code>requestFocusInWindow</code> when possible.
7534 *
7535 * <p>Note: Not all focus transfers result from invoking this method. As
7536 * such, a component may receive focus without this or any of the other
7537 * {@code requestFocus} methods of {@code Component} being invoked.
7538 *
7539 * @see #requestFocusInWindow
7540 * @see java.awt.event.FocusEvent
7541 * @see #addFocusListener
7542 * @see #isFocusable
7543 * @see #isDisplayable
7544 * @see KeyboardFocusManager#clearGlobalFocusOwner
7545 * @since 1.0
7546 */
7547 public void requestFocus() {
7548 requestFocusHelper(false, true);
7549 }
7550
7551 boolean requestFocus(FocusEvent.Cause cause) {
7552 return requestFocusHelper(false, true, cause);
7553 }
7554
7555 /**
7556 * Requests that this <code>Component</code> get the input focus,
7557 * and that this <code>Component</code>'s top-level ancestor
7558 * become the focused <code>Window</code>. This component must be
7559 * displayable, focusable, visible and all of its ancestors (with
7560 * the exception of the top-level Window) must be visible for the
7561 * request to be granted. Every effort will be made to honor the
7562 * request; however, in some cases it may be impossible to do
7563 * so. Developers must never assume that this component is the
7564 * focus owner until this component receives a FOCUS_GAINED
7565 * event. If this request is denied because this component's
7566 * top-level window cannot become the focused window, the request
7567 * will be remembered and will be granted when the window is later
7568 * focused by the user.
7569 * <p>
7570 * This method returns a boolean value. If <code>false</code> is returned,
7571 * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7598 * such, a component may receive focus without this or any of the other
7599 * {@code requestFocus} methods of {@code Component} being invoked.
7600 *
7601 * @param temporary true if the focus change is temporary,
7602 * such as when the window loses the focus; for
7603 * more information on temporary focus changes see the
7604 *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7605 * @return <code>false</code> if the focus change request is guaranteed to
7606 * fail; <code>true</code> if it is likely to succeed
7607 * @see java.awt.event.FocusEvent
7608 * @see #addFocusListener
7609 * @see #isFocusable
7610 * @see #isDisplayable
7611 * @see KeyboardFocusManager#clearGlobalFocusOwner
7612 * @since 1.4
7613 */
7614 protected boolean requestFocus(boolean temporary) {
7615 return requestFocusHelper(temporary, true);
7616 }
7617
7618 boolean requestFocus(boolean temporary, FocusEvent.Cause cause) {
7619 return requestFocusHelper(temporary, true, cause);
7620 }
7621 /**
7622 * Requests that this Component get the input focus, if this
7623 * Component's top-level ancestor is already the focused
7624 * Window. This component must be displayable, focusable, visible
7625 * and all of its ancestors (with the exception of the top-level
7626 * Window) must be visible for the request to be granted. Every
7627 * effort will be made to honor the request; however, in some
7628 * cases it may be impossible to do so. Developers must never
7629 * assume that this Component is the focus owner until this
7630 * Component receives a FOCUS_GAINED event.
7631 * <p>
7632 * This method returns a boolean value. If <code>false</code> is returned,
7633 * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7634 * returned, the request will succeed <b>unless</b> it is vetoed, or an
7635 * extraordinary event, such as disposal of the Component's peer, occurs
7636 * before the request can be granted by the native windowing system. Again,
7637 * while a return value of <code>true</code> indicates that the request is
7638 * likely to succeed, developers must never assume that this Component is
7649 * different platforms.
7650 *
7651 * <p>Note: Not all focus transfers result from invoking this method. As
7652 * such, a component may receive focus without this or any of the other
7653 * {@code requestFocus} methods of {@code Component} being invoked.
7654 *
7655 * @return <code>false</code> if the focus change request is guaranteed to
7656 * fail; <code>true</code> if it is likely to succeed
7657 * @see #requestFocus
7658 * @see java.awt.event.FocusEvent
7659 * @see #addFocusListener
7660 * @see #isFocusable
7661 * @see #isDisplayable
7662 * @see KeyboardFocusManager#clearGlobalFocusOwner
7663 * @since 1.4
7664 */
7665 public boolean requestFocusInWindow() {
7666 return requestFocusHelper(false, false);
7667 }
7668
7669 boolean requestFocusInWindow(FocusEvent.Cause cause) {
7670 return requestFocusHelper(false, false, cause);
7671 }
7672
7673 /**
7674 * Requests that this <code>Component</code> get the input focus,
7675 * if this <code>Component</code>'s top-level ancestor is already
7676 * the focused <code>Window</code>. This component must be
7677 * displayable, focusable, visible and all of its ancestors (with
7678 * the exception of the top-level Window) must be visible for the
7679 * request to be granted. Every effort will be made to honor the
7680 * request; however, in some cases it may be impossible to do
7681 * so. Developers must never assume that this component is the
7682 * focus owner until this component receives a FOCUS_GAINED event.
7683 * <p>
7684 * This method returns a boolean value. If <code>false</code> is returned,
7685 * the request is <b>guaranteed to fail</b>. If <code>true</code> is
7686 * returned, the request will succeed <b>unless</b> it is vetoed, or an
7687 * extraordinary event, such as disposal of the component's peer, occurs
7688 * before the request can be granted by the native windowing system. Again,
7689 * while a return value of <code>true</code> indicates that the request is
7714 * {@code requestFocus} methods of {@code Component} being invoked.
7715 *
7716 * @param temporary true if the focus change is temporary,
7717 * such as when the window loses the focus; for
7718 * more information on temporary focus changes see the
7719 *<a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
7720 * @return <code>false</code> if the focus change request is guaranteed to
7721 * fail; <code>true</code> if it is likely to succeed
7722 * @see #requestFocus
7723 * @see java.awt.event.FocusEvent
7724 * @see #addFocusListener
7725 * @see #isFocusable
7726 * @see #isDisplayable
7727 * @see KeyboardFocusManager#clearGlobalFocusOwner
7728 * @since 1.4
7729 */
7730 protected boolean requestFocusInWindow(boolean temporary) {
7731 return requestFocusHelper(temporary, false);
7732 }
7733
7734 boolean requestFocusInWindow(boolean temporary, FocusEvent.Cause cause) {
7735 return requestFocusHelper(temporary, false, cause);
7736 }
7737
7738 final boolean requestFocusHelper(boolean temporary,
7739 boolean focusedWindowChangeAllowed) {
7740 return requestFocusHelper(temporary, focusedWindowChangeAllowed, FocusEvent.Cause.UNKNOWN);
7741 }
7742
7743 final boolean requestFocusHelper(boolean temporary,
7744 boolean focusedWindowChangeAllowed,
7745 FocusEvent.Cause cause)
7746 {
7747 // 1) Check if the event being dispatched is a system-generated mouse event.
7748 AWTEvent currentEvent = EventQueue.getCurrentEvent();
7749 if (currentEvent instanceof MouseEvent &&
7750 SunToolkit.isSystemGenerated(currentEvent))
7751 {
7752 // 2) Sanity check: if the mouse event component source belongs to the same containing window.
7753 Component source = ((MouseEvent)currentEvent).getComponent();
7754 if (source == null || source.getContainingWindow() == getContainingWindow()) {
7755 focusLog.finest("requesting focus by mouse event \"in window\"");
7756
7757 // If both the conditions are fulfilled the focus request should be strictly
7758 // bounded by the toplevel window. It's assumed that the mouse event activates
7759 // the window (if it wasn't active) and this makes it possible for a focus
7760 // request with a strong in-window requirement to change focus in the bounds
7761 // of the toplevel. If, by any means, due to asynchronous nature of the event
7762 // dispatching mechanism, the window happens to be natively inactive by the time
7763 // this focus request is eventually handled, it should not re-activate the
7764 // toplevel. Otherwise the result may not meet user expectations. See 6981400.
7765 focusedWindowChangeAllowed = false;
7813 }
7814
7815 boolean success = peer.requestFocus
7816 (this, temporary, focusedWindowChangeAllowed, time, cause);
7817 if (!success) {
7818 KeyboardFocusManager.getCurrentKeyboardFocusManager
7819 (appContext).dequeueKeyEvents(time, this);
7820 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7821 focusLog.finest("Peer request failed");
7822 }
7823 } else {
7824 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7825 focusLog.finest("Pass for " + this);
7826 }
7827 }
7828 return success;
7829 }
7830
7831 private boolean isRequestFocusAccepted(boolean temporary,
7832 boolean focusedWindowChangeAllowed,
7833 FocusEvent.Cause cause)
7834 {
7835 if (!isFocusable() || !isVisible()) {
7836 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7837 focusLog.finest("Not focusable or not visible");
7838 }
7839 return false;
7840 }
7841
7842 ComponentPeer peer = this.peer;
7843 if (peer == null) {
7844 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7845 focusLog.finest("peer is null");
7846 }
7847 return false;
7848 }
7849
7850 Window window = getContainingWindow();
7851 if (window == null || !window.isFocusableWindow()) {
7852 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7853 focusLog.finest("Component doesn't have toplevel");
7860 Component focusOwner = KeyboardFocusManager.getMostRecentFocusOwner(window);
7861 if (focusOwner == null) {
7862 // sometimes most recent focus owner may be null, but focus owner is not
7863 // e.g. we reset most recent focus owner if user removes focus owner
7864 focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
7865 if (focusOwner != null && focusOwner.getContainingWindow() != window) {
7866 focusOwner = null;
7867 }
7868 }
7869
7870 if (focusOwner == this || focusOwner == null) {
7871 // Controller is supposed to verify focus transfers and for this it
7872 // should know both from and to components. And it shouldn't verify
7873 // transfers from when these components are equal.
7874 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7875 focusLog.finest("focus owner is null or this");
7876 }
7877 return true;
7878 }
7879
7880 if (FocusEvent.Cause.ACTIVATION == cause) {
7881 // we shouldn't call RequestFocusController in case we are
7882 // in activation. We do request focus on component which
7883 // has got temporary focus lost and then on component which is
7884 // most recent focus owner. But most recent focus owner can be
7885 // changed by requestFocusXXX() call only, so this transfer has
7886 // been already approved.
7887 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7888 focusLog.finest("cause is activation");
7889 }
7890 return true;
7891 }
7892
7893 boolean ret = Component.requestFocusController.acceptRequestFocus(focusOwner,
7894 this,
7895 temporary,
7896 focusedWindowChangeAllowed,
7897 cause);
7898 if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
7899 focusLog.finest("RequestFocusController returns {0}", ret);
7900 }
7901
7902 return ret;
7903 }
7904
7905 private static RequestFocusController requestFocusController = new DummyRequestFocusController();
7906
7907 // Swing access this method through reflection to implement InputVerifier's functionality.
7908 // Perhaps, we should make this method public (later ;)
7909 private static class DummyRequestFocusController implements RequestFocusController {
7910 public boolean acceptRequestFocus(Component from, Component to,
7911 boolean temporary, boolean focusedWindowChangeAllowed,
7912 FocusEvent.Cause cause)
7913 {
7914 return true;
7915 }
7916 };
7917
7918 synchronized static void setRequestFocusController(RequestFocusController requestController)
7919 {
7920 if (requestController == null) {
7921 requestFocusController = new DummyRequestFocusController();
7922 } else {
7923 requestFocusController = requestController;
7924 }
7925 }
7926
7927 /**
7928 * Returns the Container which is the focus cycle root of this Component's
7929 * focus traversal cycle. Each focus traversal cycle has only a single
7930 * focus cycle root and each Component which is not a Container belongs to
7931 * only a single focus traversal cycle. Containers which are focus cycle
7932 * roots belong to two cycles: one rooted at the Container itself, and one
7976 public void transferFocus() {
7977 nextFocus();
7978 }
7979
7980 /**
7981 * @deprecated As of JDK version 1.1,
7982 * replaced by transferFocus().
7983 */
7984 @Deprecated
7985 public void nextFocus() {
7986 transferFocus(false);
7987 }
7988
7989 boolean transferFocus(boolean clearOnFailure) {
7990 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7991 focusLog.finer("clearOnFailure = " + clearOnFailure);
7992 }
7993 Component toFocus = getNextFocusCandidate();
7994 boolean res = false;
7995 if (toFocus != null && !toFocus.isFocusOwner() && toFocus != this) {
7996 res = toFocus.requestFocusInWindow(FocusEvent.Cause.TRAVERSAL_FORWARD);
7997 }
7998 if (clearOnFailure && !res) {
7999 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8000 focusLog.finer("clear global focus owner");
8001 }
8002 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
8003 }
8004 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8005 focusLog.finer("returning result: " + res);
8006 }
8007 return res;
8008 }
8009
8010 final Component getNextFocusCandidate() {
8011 Container rootAncestor = getTraversalRoot();
8012 Component comp = this;
8013 while (rootAncestor != null &&
8014 !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8015 {
8016 comp = rootAncestor;
8056 transferFocusBackward(false);
8057 }
8058
8059 boolean transferFocusBackward(boolean clearOnFailure) {
8060 Container rootAncestor = getTraversalRoot();
8061 Component comp = this;
8062 while (rootAncestor != null &&
8063 !(rootAncestor.isShowing() && rootAncestor.canBeFocusOwner()))
8064 {
8065 comp = rootAncestor;
8066 rootAncestor = comp.getFocusCycleRootAncestor();
8067 }
8068 boolean res = false;
8069 if (rootAncestor != null) {
8070 FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
8071 Component toFocus = policy.getComponentBefore(rootAncestor, comp);
8072 if (toFocus == null) {
8073 toFocus = policy.getDefaultComponent(rootAncestor);
8074 }
8075 if (toFocus != null) {
8076 res = toFocus.requestFocusInWindow(FocusEvent.Cause.TRAVERSAL_BACKWARD);
8077 }
8078 }
8079 if (clearOnFailure && !res) {
8080 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8081 focusLog.finer("clear global focus owner");
8082 }
8083 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwnerPriv();
8084 }
8085 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
8086 focusLog.finer("returning result: " + res);
8087 }
8088 return res;
8089 }
8090
8091 /**
8092 * Transfers the focus up one focus traversal cycle. Typically, the focus
8093 * owner is set to this Component's focus cycle root, and the current focus
8094 * cycle root is set to the new focus owner's focus cycle root. If,
8095 * however, this Component's focus cycle root is a Window, then the focus
8096 * owner is set to the focus cycle root's default Component to focus, and
8101 * @see Container#setFocusCycleRoot(boolean)
8102 * @since 1.4
8103 */
8104 public void transferFocusUpCycle() {
8105 Container rootAncestor;
8106 for (rootAncestor = getFocusCycleRootAncestor();
8107 rootAncestor != null && !(rootAncestor.isShowing() &&
8108 rootAncestor.isFocusable() &&
8109 rootAncestor.isEnabled());
8110 rootAncestor = rootAncestor.getFocusCycleRootAncestor()) {
8111 }
8112
8113 if (rootAncestor != null) {
8114 Container rootAncestorRootAncestor =
8115 rootAncestor.getFocusCycleRootAncestor();
8116 Container fcr = (rootAncestorRootAncestor != null) ?
8117 rootAncestorRootAncestor : rootAncestor;
8118
8119 KeyboardFocusManager.getCurrentKeyboardFocusManager().
8120 setGlobalCurrentFocusCycleRootPriv(fcr);
8121 rootAncestor.requestFocus(FocusEvent.Cause.TRAVERSAL_UP);
8122 } else {
8123 Window window = getContainingWindow();
8124
8125 if (window != null) {
8126 Component toFocus = window.getFocusTraversalPolicy().
8127 getDefaultComponent(window);
8128 if (toFocus != null) {
8129 KeyboardFocusManager.getCurrentKeyboardFocusManager().
8130 setGlobalCurrentFocusCycleRootPriv(window);
8131 toFocus.requestFocus(FocusEvent.Cause.TRAVERSAL_UP);
8132 }
8133 }
8134 }
8135 }
8136
8137 /**
8138 * Returns <code>true</code> if this <code>Component</code> is the
8139 * focus owner. This method is obsolete, and has been replaced by
8140 * <code>isFocusOwner()</code>.
8141 *
8142 * @return <code>true</code> if this <code>Component</code> is the
8143 * focus owner; <code>false</code> otherwise
8144 * @since 1.2
8145 */
8146 public boolean hasFocus() {
8147 return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
8148 getFocusOwner() == this);
8149 }
8150
8151 /**
|