1. /*
  2. * @(#)WindowsMenuBarUI.java 1.14 03/05/06
  3. *
  4. * Copyright 2003 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 javax.swing.plaf.basic.*;
  9. import javax.swing.*;
  10. import javax.swing.plaf.*;
  11. import java.awt.*;
  12. import java.awt.event.ActionEvent;
  13. /**
  14. * Windows rendition of the component.
  15. * <p>
  16. * <strong>Warning:</strong>
  17. * Serialized objects of this class will not be compatible with
  18. * future Swing releases. The current serialization support is appropriate
  19. * for short term storage or RMI between applications running the same
  20. * version of Swing. A future release of Swing will provide support for
  21. * long term persistence.
  22. */
  23. public class WindowsMenuBarUI extends BasicMenuBarUI
  24. {
  25. public static ComponentUI createUI(JComponent x) {
  26. return new WindowsMenuBarUI();
  27. }
  28. protected void installDefaults() {
  29. // The following is added for 1.4.2 only. In 1.5 we will be using a new
  30. // DesktopProperty for the menubar background color.
  31. UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
  32. XPStyle xp = XPStyle.getXP();
  33. if (xp != null) {
  34. Color color = xp.getColor("sysmetrics.menubar", null);
  35. if (color != null) {
  36. // Override default from WindowsLookAndFeel
  37. lafDefaults.put("MenuBar.background", new ColorUIResource(color));
  38. }
  39. } else {
  40. // Restore default from WindowsLookAndFeel
  41. Object classicBackgroundProperty =
  42. lafDefaults.get("MenuBar.classicBackground");
  43. if ((classicBackgroundProperty instanceof Object[]) &&
  44. ((Object[])classicBackgroundProperty).length > 0) {
  45. lafDefaults.put("MenuBar.background",
  46. ((Object[])classicBackgroundProperty)[0]);
  47. }
  48. }
  49. super.installDefaults();
  50. }
  51. protected void installKeyboardActions() {
  52. super.installKeyboardActions();
  53. ActionMap map = SwingUtilities.getUIActionMap(menuBar);
  54. if (map == null) {
  55. map = new ActionMapUIResource();
  56. SwingUtilities.replaceUIActionMap(menuBar, map);
  57. }
  58. map.put("takeFocus", new TakeFocus());
  59. }
  60. /**
  61. * Action that activates the menu (e.g. when F10 is pressed).
  62. * Unlike BasicMenuBarUI.TakeFocus, this Action will not show menu popup.
  63. */
  64. private static class TakeFocus extends AbstractAction {
  65. public void actionPerformed(ActionEvent e) {
  66. JMenuBar menuBar = (JMenuBar)e.getSource();
  67. JMenu menu = menuBar.getMenu(0);
  68. if (menu != null) {
  69. MenuSelectionManager msm =
  70. MenuSelectionManager.defaultManager();
  71. MenuElement path[] = new MenuElement[2];
  72. path[0] = (MenuElement)menuBar;
  73. path[1] = (MenuElement)menu;
  74. msm.setSelectedPath(path);
  75. // show mnemonics
  76. WindowsLookAndFeel.setMnemonicHidden(false);
  77. WindowsLookAndFeel.repaintRootPane(menuBar);
  78. }
  79. }
  80. }
  81. }