1. /*
  2. * @(#)WindowsRootPaneUI.java 1.15 04/04/16
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import java.awt.Component;
  9. import java.awt.Container;
  10. import java.awt.Event;
  11. import java.awt.KeyEventPostProcessor;
  12. import java.awt.Window;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.KeyEvent;
  15. import javax.swing.AbstractAction;
  16. import javax.swing.ActionMap;
  17. import javax.swing.InputMap;
  18. import javax.swing.KeyStroke;
  19. import javax.swing.JComponent;
  20. import javax.swing.JLabel;
  21. import javax.swing.JRootPane;
  22. import javax.swing.SwingUtilities;
  23. import javax.swing.UIManager;
  24. import javax.swing.AbstractButton;
  25. import javax.swing.JFrame;
  26. import javax.swing.JMenu;
  27. import javax.swing.JMenuBar;
  28. import javax.swing.MenuElement;
  29. import javax.swing.MenuSelectionManager;
  30. import javax.swing.plaf.ActionMapUIResource;
  31. import javax.swing.plaf.ComponentUI;
  32. import javax.swing.plaf.InputMapUIResource;
  33. import javax.swing.plaf.basic.BasicRootPaneUI;
  34. import javax.swing.plaf.basic.ComboPopup;
  35. /**
  36. * Windows implementation of RootPaneUI, there is one shared between all
  37. * JRootPane instances.
  38. *
  39. * @version 1.15 04/16/04
  40. * @author Mark Davidson
  41. * @since 1.4
  42. */
  43. public class WindowsRootPaneUI extends BasicRootPaneUI {
  44. private final static WindowsRootPaneUI windowsRootPaneUI = new WindowsRootPaneUI();
  45. static final AltProcessor altProcessor = new AltProcessor();
  46. public static ComponentUI createUI(JComponent c) {
  47. return windowsRootPaneUI;
  48. }
  49. static class AltProcessor implements KeyEventPostProcessor {
  50. static boolean altKeyPressed = false;
  51. static boolean menuCanceledOnPress = false;
  52. static JRootPane root = null;
  53. static Window winAncestor = null;
  54. void altPressed(KeyEvent ev) {
  55. MenuSelectionManager msm =
  56. MenuSelectionManager.defaultManager();
  57. MenuElement[] path = msm.getSelectedPath();
  58. if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
  59. msm.clearSelectedPath();
  60. menuCanceledOnPress = true;
  61. ev.consume();
  62. } else if(path.length > 0) { // We are in ComboBox
  63. menuCanceledOnPress = false;
  64. WindowsLookAndFeel.setMnemonicHidden(false);
  65. WindowsUtils.repaintMnemonicsInWindow(winAncestor);
  66. ev.consume();
  67. } else {
  68. menuCanceledOnPress = false;
  69. WindowsLookAndFeel.setMnemonicHidden(false);
  70. WindowsUtils.repaintMnemonicsInWindow(winAncestor);
  71. JMenuBar mbar = root != null ? root.getJMenuBar() : null;
  72. if(mbar == null && winAncestor instanceof JFrame) {
  73. mbar = ((JFrame)winAncestor).getJMenuBar();
  74. }
  75. JMenu menu = mbar != null ? mbar.getMenu(0) : null;
  76. if(menu != null) {
  77. ev.consume();
  78. }
  79. }
  80. }
  81. void altReleased(KeyEvent ev) {
  82. if (menuCanceledOnPress) {
  83. WindowsLookAndFeel.setMnemonicHidden(true);
  84. WindowsUtils.repaintMnemonicsInWindow(winAncestor);
  85. return;
  86. }
  87. MenuSelectionManager msm =
  88. MenuSelectionManager.defaultManager();
  89. if (msm.getSelectedPath().length == 0) {
  90. // if no menu is active, we try activating the menubar
  91. JMenuBar mbar = root != null ? root.getJMenuBar() : null;
  92. if(mbar == null && winAncestor instanceof JFrame) {
  93. mbar = ((JFrame)winAncestor).getJMenuBar();
  94. }
  95. JMenu menu = mbar != null ? mbar.getMenu(0) : null;
  96. if (menu != null) {
  97. MenuElement[] path = new MenuElement[2];
  98. path[0] = mbar;
  99. path[1] = menu;
  100. msm.setSelectedPath(path);
  101. } else if(!WindowsLookAndFeel.isMnemonicHidden()) {
  102. WindowsLookAndFeel.setMnemonicHidden(true);
  103. WindowsUtils.repaintMnemonicsInWindow(winAncestor);
  104. }
  105. } else {
  106. if((msm.getSelectedPath())[0] instanceof ComboPopup) {
  107. WindowsLookAndFeel.setMnemonicHidden(true);
  108. WindowsUtils.repaintMnemonicsInWindow(winAncestor);
  109. }
  110. }
  111. }
  112. public boolean postProcessKeyEvent(KeyEvent ev) {
  113. if (ev.getKeyCode() == KeyEvent.VK_ALT) {
  114. root = SwingUtilities.getRootPane(ev.getComponent());
  115. winAncestor = (root == null ? null :
  116. SwingUtilities.getWindowAncestor(root));
  117. if (ev.getID() == KeyEvent.KEY_PRESSED) {
  118. if (!altKeyPressed) {
  119. altPressed(ev);
  120. }
  121. altKeyPressed = true;
  122. return true;
  123. } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
  124. if (altKeyPressed) {
  125. altReleased(ev);
  126. } else {
  127. MenuSelectionManager msm =
  128. MenuSelectionManager.defaultManager();
  129. MenuElement[] path = msm.getSelectedPath();
  130. if (path.length <= 0) {
  131. WindowsLookAndFeel.setMnemonicHidden(true);
  132. WindowsUtils.repaintMnemonicsInWindow(winAncestor);
  133. }
  134. }
  135. altKeyPressed = false;
  136. }
  137. root = null;
  138. winAncestor = null;
  139. } else {
  140. altKeyPressed = false;
  141. }
  142. return false;
  143. }
  144. }
  145. }