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