1. /*
  2. * @(#)MetalMenuBarUI.java 1.5 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.metal;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.plaf.ComponentUI;
  11. import javax.swing.plaf.UIResource;
  12. import javax.swing.plaf.basic.*;
  13. /**
  14. * Metal implementation of <code>MenuBarUI</code>. This class is responsible
  15. * for providing the metal look and feel for <code>JMenuBar</code>s.
  16. *
  17. * @version 1.5 12/19/03
  18. * @see javax.swing.plaf.MenuBarUI
  19. * @since 1.5
  20. */
  21. public class MetalMenuBarUI extends BasicMenuBarUI {
  22. /**
  23. * Creates the <code>ComponentUI</code> implementation for the passed
  24. * in component.
  25. *
  26. * @param x JComponent to create the ComponentUI implementation for
  27. * @return ComponentUI implementation for <code>x</code>
  28. * @throws NullPointerException if <code>x</code> is null
  29. */
  30. public static ComponentUI createUI(JComponent x) {
  31. if (x == null) {
  32. throw new NullPointerException("Must pass in a non-null component");
  33. }
  34. return new MetalMenuBarUI();
  35. }
  36. /**
  37. * Configures the specified component appropriate for the metal look and
  38. * feel.
  39. *
  40. * @param c the component where this UI delegate is being installed
  41. * @throws NullPointerException if <code>c</code> is null.
  42. */
  43. public void installUI(JComponent c) {
  44. super.installUI(c);
  45. MetalToolBarUI.register(c);
  46. }
  47. /**
  48. * Reverses configuration which was done on the specified component during
  49. * <code>installUI</code>.
  50. *
  51. * @param c the component where this UI delegate is being installed
  52. * @throws NullPointerException if <code>c</code> is null.
  53. */
  54. public void uninstallUI(JComponent c) {
  55. super.uninstallUI(c);
  56. MetalToolBarUI.unregister(c);
  57. }
  58. /**
  59. * If necessary paints the background of the component, then
  60. * invokes <code>paint</code>.
  61. *
  62. * @param g Graphics to paint to
  63. * @param c JComponent painting on
  64. * @throws NullPointerException if <code>g</code> or <code>c</code> is
  65. * null
  66. * @see javax.swing.plaf.ComponentUI#update
  67. * @see javax.swing.plaf.ComponentUI#paint
  68. * @since 1.5
  69. */
  70. public void update(Graphics g, JComponent c) {
  71. boolean isOpaque = c.isOpaque();
  72. if (isOpaque && (c.getBackground() instanceof UIResource) &&
  73. UIManager.get("MenuBar.gradient") != null) {
  74. if (MetalToolBarUI.doesMenuBarBorderToolBar((JMenuBar)c)) {
  75. JToolBar tb = (JToolBar)MetalToolBarUI.
  76. findRegisteredComponentOfType(c, JToolBar.class);
  77. if (tb.isOpaque() &&tb.getBackground() instanceof UIResource) {
  78. MetalUtils.drawGradient(c, g, "MenuBar.gradient", 0, 0,
  79. c.getWidth(), c.getHeight() +
  80. tb.getHeight(), true);
  81. paint(g, c);
  82. return;
  83. }
  84. }
  85. MetalUtils.drawGradient(c, g, "MenuBar.gradient", 0, 0,
  86. c.getWidth(), c.getHeight(),true);
  87. paint(g, c);
  88. }
  89. else {
  90. super.update(g, c);
  91. }
  92. }
  93. }