1. /*
  2. * @(#)FocusManager.java 1.10 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.util.Hashtable;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.Component;
  11. /**
  12. * Swing Focus Manager
  13. *
  14. * @version 1.10 11/29/01
  15. * @author Arnaud Weber
  16. */
  17. public abstract class FocusManager {
  18. /** This property name is used to get the FocusManager implementation
  19. * that should be used for a given UI
  20. */
  21. public static final String FOCUS_MANAGER_CLASS_PROPERTY =
  22. "FocusManagerClassName";
  23. private static final Object focusManagerKey = FocusManager.class;
  24. /** Return the FocusManager for the calling thread
  25. * There is one FocusManager per thread group
  26. */
  27. public static FocusManager getCurrentManager() {
  28. FocusManager result =
  29. (FocusManager)SwingUtilities.appContextGet(focusManagerKey);
  30. if(result == null) {
  31. String className =
  32. UIManager.getString(FOCUS_MANAGER_CLASS_PROPERTY);
  33. try {
  34. Class c = Class.forName(className);
  35. if(c != null) {
  36. result = (FocusManager) c.newInstance();
  37. }
  38. } catch (ClassNotFoundException e) {
  39. System.out.println("Cannot find class " + className + " " + e);
  40. result = null;
  41. } catch (InstantiationException e) {
  42. System.out.println("Cannot instantiate class " + className + " " + e);
  43. result = null;
  44. } catch (IllegalAccessException e) {
  45. System.out.println("Cannot access class " + className + " " + e);
  46. result = null;
  47. }
  48. if(result == null) {
  49. result = new DefaultFocusManager();
  50. }
  51. SwingUtilities.appContextPut(focusManagerKey, result);
  52. }
  53. return result;
  54. }
  55. /** Set the FocusManager that should be used for the calling
  56. * thread. <b>aFocusManager</b> will be the default focus
  57. * manager for the calling thread's thread group.
  58. */
  59. public static void setCurrentManager(FocusManager aFocusManager) {
  60. if (aFocusManager != null) {
  61. SwingUtilities.appContextPut(focusManagerKey, aFocusManager);
  62. } else {
  63. SwingUtilities.appContextRemove(focusManagerKey);
  64. }
  65. }
  66. /** Disable Swing's focus manager for the calling thread's thread group.
  67. * Call this method if your application mixes java.awt components and
  68. * swing's components. Your application will then use the awt focus
  69. * manager.
  70. */
  71. public static void disableSwingFocusManager() {
  72. setCurrentManager(new DisabledFocusManager());
  73. }
  74. /** Return whether Swing's focus manager is enabled **/
  75. public static boolean isFocusManagerEnabled() {
  76. FocusManager fm = getCurrentManager();
  77. return !(fm instanceof DisabledFocusManager);
  78. }
  79. /** This method is called by JComponents when a key event occurs.
  80. * JComponent gives key events to the focus manager
  81. * first, then to key listeners, then to the keyboard UI dispatcher.
  82. * This method should look at the key event and change the focused
  83. * component if the key event matches the receiver's focus manager
  84. * hot keys. For example the default focus manager will change the
  85. * focus if the key event matches TAB or Shift + TAB.
  86. * The focus manager should call consume() on <b>anEvent</b> if
  87. * <code>anEvent</code> has been processed.
  88. * <code>focusedComponent</code> is the component that currently has
  89. * the focus.
  90. * Note: FocusManager will receive both KEY_PRESSED and KEY_RELEASED
  91. * key events. If one event is consumed, the other one should be consumed
  92. * too.
  93. */
  94. public abstract void processKeyEvent(Component focusedComponent,KeyEvent anEvent);
  95. /** Cause the focus manager to set the focus on the next focusable component
  96. * You can call this method to cause the focus manager to focus the next component
  97. **/
  98. public abstract void focusNextComponent(Component aComponent);
  99. /** Cause the focus manager to set the focus on the previous focusable component
  100. * You can call this methid to cause the focus manager to focus the previous component
  101. */
  102. public abstract void focusPreviousComponent(Component aComponent);
  103. static class DisabledFocusManager extends FocusManager {
  104. public void processKeyEvent(Component focusedComponent,KeyEvent anEvent) {}
  105. public void focusNextComponent(Component c) {}
  106. public void focusPreviousComponent(Component c) {}
  107. }
  108. }