1. /*
  2. * @(#)WindowsPopupMenuUI.java 1.21 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.KeyEventPostProcessor;
  10. import java.awt.KeyboardFocusManager;
  11. import java.awt.Window;
  12. import java.awt.event.KeyEvent;
  13. import javax.swing.*;
  14. import javax.swing.event.*;
  15. import javax.swing.plaf.*;
  16. import javax.swing.plaf.basic.*;
  17. /**
  18. * Windows rendition of the component.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. */
  27. public class WindowsPopupMenuUI extends BasicPopupMenuUI {
  28. static MnemonicListener mnemonicListener = null;
  29. public static ComponentUI createUI(JComponent c) {
  30. return new WindowsPopupMenuUI();
  31. }
  32. public void installListeners() {
  33. super.installListeners();
  34. if (! UIManager.getBoolean("Button.showMnemonics") &&
  35. mnemonicListener == null) {
  36. mnemonicListener = new MnemonicListener();
  37. MenuSelectionManager.defaultManager().
  38. addChangeListener(mnemonicListener);
  39. }
  40. }
  41. /**
  42. * Returns the <code>Popup</code> that will be responsible for
  43. * displaying the <code>JPopupMenu</code>.
  44. *
  45. * @param popupMenu JPopupMenu requesting Popup
  46. * @param x Screen x location Popup is to be shown at
  47. * @param y Screen y location Popup is to be shown at.
  48. * @return Popup that will show the JPopupMenu
  49. * @since 1.4
  50. */
  51. public Popup getPopup(JPopupMenu popupMenu, int x, int y) {
  52. PopupFactory popupFactory = PopupFactory.getSharedInstance();
  53. return popupFactory.getPopup(popupMenu.getInvoker(), popupMenu, x, y);
  54. }
  55. static class MnemonicListener implements ChangeListener {
  56. JRootPane repaintRoot = null;
  57. public void stateChanged(ChangeEvent ev) {
  58. MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();
  59. MenuElement[] path = msm.getSelectedPath();
  60. if (path.length == 0) {
  61. if(!WindowsLookAndFeel.isMnemonicHidden()) {
  62. // menu was canceled -- hide mnemonics
  63. WindowsLookAndFeel.setMnemonicHidden(true);
  64. if (repaintRoot != null) {
  65. Window win =
  66. SwingUtilities.getWindowAncestor(repaintRoot);
  67. WindowsUtils.repaintMnemonicsInWindow(win);
  68. }
  69. }
  70. } else {
  71. Component c = (Component)path[0];
  72. if (c instanceof JPopupMenu) c = ((JPopupMenu)c).getInvoker();
  73. repaintRoot = SwingUtilities.getRootPane(c);
  74. }
  75. }
  76. }
  77. }