1. /*
  2. * @(#)SynthMenuBarUI.java 1.10 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.synth;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import java.awt.Color;
  11. import java.awt.Component;
  12. import java.awt.Container;
  13. import java.awt.Dimension;
  14. import java.awt.Graphics;
  15. import java.awt.Insets;
  16. import java.awt.Point;
  17. import java.awt.Rectangle;
  18. import java.awt.event.*;
  19. import java.beans.PropertyChangeEvent;
  20. import java.beans.PropertyChangeListener;
  21. import javax.swing.border.*;
  22. import javax.swing.plaf.*;
  23. import javax.swing.plaf.basic.*;
  24. import sun.swing.plaf.synth.SynthUI;
  25. /**
  26. * Synth's MenuBarUI.
  27. *
  28. * @version 1.10, 12/19/03
  29. * @author Scott Violet
  30. */
  31. class SynthMenuBarUI extends BasicMenuBarUI implements PropertyChangeListener,
  32. SynthUI {
  33. private SynthStyle style;
  34. public static ComponentUI createUI(JComponent x) {
  35. return new SynthMenuBarUI();
  36. }
  37. protected void installDefaults() {
  38. if (menuBar.getLayout() == null ||
  39. menuBar.getLayout() instanceof UIResource) {
  40. menuBar.setLayout(new DefaultMenuLayout(menuBar,BoxLayout.LINE_AXIS));
  41. }
  42. updateStyle(menuBar);
  43. }
  44. protected void installListeners() {
  45. super.installListeners();
  46. menuBar.addPropertyChangeListener(this);
  47. }
  48. private void updateStyle(JMenuBar c) {
  49. SynthContext context = getContext(c, ENABLED);
  50. SynthStyle oldStyle = style;
  51. style = SynthLookAndFeel.updateStyle(context, this);
  52. if (style != oldStyle) {
  53. if (oldStyle != null) {
  54. uninstallKeyboardActions();
  55. installKeyboardActions();
  56. }
  57. }
  58. context.dispose();
  59. }
  60. protected void uninstallDefaults() {
  61. SynthContext context = getContext(menuBar, ENABLED);
  62. style.uninstallDefaults(context);
  63. context.dispose();
  64. style = null;
  65. }
  66. protected void uninstallListeners() {
  67. super.uninstallListeners();
  68. menuBar.removePropertyChangeListener(this);
  69. }
  70. public SynthContext getContext(JComponent c) {
  71. return getContext(c, getComponentState(c));
  72. }
  73. private SynthContext getContext(JComponent c, int state) {
  74. return SynthContext.getContext(SynthContext.class, c,
  75. SynthLookAndFeel.getRegion(c), style, state);
  76. }
  77. private Region getRegion(JComponent c) {
  78. return SynthLookAndFeel.getRegion(c);
  79. }
  80. private int getComponentState(JComponent c) {
  81. return SynthLookAndFeel.getComponentState(c);
  82. }
  83. public void update(Graphics g, JComponent c) {
  84. SynthContext context = getContext(c);
  85. SynthLookAndFeel.update(context, g);
  86. context.getPainter().paintMenuBarBackground(context,
  87. g, 0, 0, c.getWidth(), c.getHeight());
  88. paint(context, g);
  89. context.dispose();
  90. }
  91. public void paint(Graphics g, JComponent c) {
  92. SynthContext context = getContext(c);
  93. paint(context, g);
  94. context.dispose();
  95. }
  96. protected void paint(SynthContext context, Graphics g) {
  97. }
  98. public void paintBorder(SynthContext context, Graphics g, int x,
  99. int y, int w, int h) {
  100. context.getPainter().paintMenuBarBorder(context, g, x, y, w, h);
  101. }
  102. public void propertyChange(PropertyChangeEvent e) {
  103. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  104. updateStyle((JMenuBar)e.getSource());
  105. }
  106. }
  107. }