1. /*
  2. * @(#)FocusManager.java 1.25 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. /**
  10. * This class has been obsoleted by the 1.4 focus APIs. While client code may
  11. * still use this class, developers are strongly encouraged to use
  12. * <code>java.awt.KeyboardFocusManager</code> and
  13. * <code>java.awt.DefaultKeyboardFocusManager</code> instead. Please see the
  14. * Focus Specification for more information.
  15. *
  16. * @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
  17. *
  18. * @version 1.25, 01/23/03
  19. * @author Arnaud Weber
  20. * @author David Mendenhall
  21. */
  22. public abstract class FocusManager extends DefaultKeyboardFocusManager {
  23. /**
  24. * This field is obsolete, and its use is discouraged since its
  25. * specification is incompatible with the 1.4 focus APIs.
  26. * The current FocusManager is no longer a property of the UI.
  27. * Client code must query for the current FocusManager using
  28. * <code>KeyboardFocusManager.getCurrentKeyboardFocusManager()</code>.
  29. * See the Focus Specification for more information.
  30. *
  31. * @see java.awt.KeyboardFocusManager#getCurrentKeyboardFocusManager
  32. * @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
  33. */
  34. public static final String FOCUS_MANAGER_CLASS_PROPERTY =
  35. "FocusManagerClassName";
  36. private static boolean enabled = true;
  37. /**
  38. * Returns the current <code>KeyboardFocusManager</code> instance
  39. * for the calling thread's context.
  40. *
  41. * @return this thread's context's <code>KeyboardFocusManager</code>
  42. * @see #setCurrentManager
  43. */
  44. public static FocusManager getCurrentManager() {
  45. KeyboardFocusManager manager =
  46. KeyboardFocusManager.getCurrentKeyboardFocusManager();
  47. if (manager instanceof FocusManager) {
  48. return (FocusManager)manager;
  49. } else {
  50. return new DelegatingDefaultFocusManager(manager);
  51. }
  52. }
  53. /**
  54. * Sets the current <code>KeyboardFocusManager</code> instance
  55. * for the calling thread's context. If <code>null</code> is
  56. * specified, then the current <code>KeyboardFocusManager</code>
  57. * is replaced with a new instance of
  58. * <code>DefaultKeyboardFocusManager</code>.
  59. * <p>
  60. * If a <code>SecurityManager</code> is installed,
  61. * the calling thread must be granted the <code>AWTPermission</code>
  62. * "replaceKeyboardFocusManager" in order to replace the
  63. * the current <code>KeyboardFocusManager</code>.
  64. * If this permission is not granted,
  65. * this method will throw a <code>SecurityException</code>,
  66. * and the current <code>KeyboardFocusManager</code> will be unchanged.
  67. *
  68. * @param aFocusManager the new <code>KeyboardFocusManager</code>
  69. * for this thread's context
  70. * @see #getCurrentManager
  71. * @see java.awt.DefaultKeyboardFocusManager
  72. * @throws SecurityException if the calling thread does not have permission
  73. * to replace the current <code>KeyboardFocusManager</code>
  74. */
  75. public static void setCurrentManager(FocusManager aFocusManager)
  76. throws SecurityException
  77. {
  78. // Note: This method is not backward-compatible with 1.3 and earlier
  79. // releases. It now throws a SecurityException in an applet, whereas
  80. // in previous releases, it did not. This issue was discussed at
  81. // length, and ultimately approved by Hans.
  82. KeyboardFocusManager toSet =
  83. (aFocusManager instanceof DelegatingDefaultFocusManager)
  84. ? ((DelegatingDefaultFocusManager)aFocusManager).getDelegate()
  85. : aFocusManager;
  86. KeyboardFocusManager.setCurrentKeyboardFocusManager(toSet);
  87. }
  88. /**
  89. * Changes the current <code>KeyboardFocusManager</code>'s default
  90. * <code>FocusTraversalPolicy</code> to
  91. * <code>DefaultFocusTraversalPolicy</code>.
  92. *
  93. * @see java.awt.DefaultFocusTraversalPolicy
  94. * @see java.awt.KeyboardFocusManager#setDefaultFocusTraversalPolicy
  95. * @deprecated as of 1.4, replaced by
  96. * <code>KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy)</code>
  97. */
  98. public static void disableSwingFocusManager() {
  99. if (enabled) {
  100. enabled = false;
  101. KeyboardFocusManager.getCurrentKeyboardFocusManager().
  102. setDefaultFocusTraversalPolicy(
  103. new DefaultFocusTraversalPolicy());
  104. }
  105. }
  106. /**
  107. * Returns whether the application has invoked
  108. * <code>disableSwingFocusManager()</code>.
  109. *
  110. * @see #disableSwingFocusManager
  111. * @deprecated As of 1.4, replaced by
  112. * <code>KeyboardFocusManager.getDefaultFocusTraversalPolicy()</code>
  113. */
  114. public static boolean isFocusManagerEnabled() {
  115. return enabled;
  116. }
  117. }