1. /*
  2. * @(#)SynthPopupMenuUI.java 1.21 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 javax.swing.plaf.*;
  11. import javax.swing.plaf.basic.*;
  12. import javax.swing.border.*;
  13. import java.applet.Applet;
  14. import java.awt.Component;
  15. import java.awt.Container;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics;
  18. import java.awt.KeyboardFocusManager;
  19. import java.awt.Window;
  20. import java.awt.event.*;
  21. import java.awt.AWTEvent;
  22. import java.awt.Toolkit;
  23. import java.beans.PropertyChangeListener;
  24. import java.beans.PropertyChangeEvent;
  25. import java.util.*;
  26. import sun.swing.plaf.synth.SynthUI;
  27. /**
  28. * Synth's PopupMenuUI.
  29. *
  30. * @version 1.21, 12/19/03
  31. * @author Georges Saab
  32. * @author David Karlton
  33. * @author Arnaud Weber
  34. */
  35. class SynthPopupMenuUI extends BasicPopupMenuUI implements
  36. PropertyChangeListener, SynthUI {
  37. /**
  38. * Maximum size of the text portion of the children menu items.
  39. */
  40. private int maxTextWidth;
  41. /**
  42. * Maximum size of the text for the acclerator portion of the children
  43. * menu items.
  44. */
  45. private int maxAcceleratorWidth;
  46. private SynthStyle style;
  47. public static ComponentUI createUI(JComponent x) {
  48. return new SynthPopupMenuUI();
  49. }
  50. public void installDefaults() {
  51. if (popupMenu.getLayout() == null ||
  52. popupMenu.getLayout() instanceof UIResource) {
  53. popupMenu.setLayout(new DefaultMenuLayout(
  54. popupMenu, BoxLayout.Y_AXIS));
  55. }
  56. updateStyle(popupMenu);
  57. }
  58. private void updateStyle(JComponent c) {
  59. SynthContext context = getContext(c, ENABLED);
  60. SynthStyle oldStyle = style;
  61. style = SynthLookAndFeel.updateStyle(context, this);
  62. if (style != oldStyle) {
  63. if (oldStyle != null) {
  64. uninstallKeyboardActions();
  65. installKeyboardActions();
  66. }
  67. }
  68. context.dispose();
  69. }
  70. protected void installListeners() {
  71. super.installListeners();
  72. popupMenu.addPropertyChangeListener(this);
  73. }
  74. protected void uninstallDefaults() {
  75. SynthContext context = getContext(popupMenu, ENABLED);
  76. style.uninstallDefaults(context);
  77. context.dispose();
  78. style = null;
  79. if (popupMenu.getLayout() instanceof UIResource) {
  80. popupMenu.setLayout(null);
  81. }
  82. }
  83. protected void uninstallListeners() {
  84. super.uninstallListeners();
  85. popupMenu.removePropertyChangeListener(this);
  86. }
  87. public SynthContext getContext(JComponent c) {
  88. return getContext(c, getComponentState(c));
  89. }
  90. private SynthContext getContext(JComponent c, int state) {
  91. return SynthContext.getContext(SynthContext.class, c,
  92. SynthLookAndFeel.getRegion(c), style, state);
  93. }
  94. private Region getRegion(JComponent c) {
  95. return SynthLookAndFeel.getRegion(c);
  96. }
  97. private int getComponentState(JComponent c) {
  98. return SynthLookAndFeel.getComponentState(c);
  99. }
  100. /**
  101. * Resets the max text and accerator widths.
  102. */
  103. void resetAcceleratorWidths() {
  104. maxTextWidth = maxAcceleratorWidth = 0;
  105. }
  106. /**
  107. * Adjusts the width needed to display the maximum menu item string.
  108. *
  109. * @param width Text width.
  110. * @return max width
  111. */
  112. int adjustTextWidth(int width) {
  113. maxTextWidth = Math.max(maxTextWidth, width);
  114. return maxTextWidth;
  115. }
  116. /**
  117. * Adjusts the width needed to display the maximum accelerator.
  118. *
  119. * @param width Text width.
  120. * @return max width
  121. */
  122. int adjustAcceleratorWidth(int width) {
  123. maxAcceleratorWidth = Math.max(maxAcceleratorWidth, width);
  124. return maxAcceleratorWidth;
  125. }
  126. /**
  127. * Maximum size to display text of children menu items.
  128. */
  129. int getMaxTextWidth() {
  130. return maxTextWidth;
  131. }
  132. /**
  133. * Maximum size needed to display accelerators of children menu items.
  134. */
  135. int getMaxAcceleratorWidth() {
  136. return maxAcceleratorWidth;
  137. }
  138. public void update(Graphics g, JComponent c) {
  139. SynthContext context = getContext(c);
  140. SynthLookAndFeel.update(context, g);
  141. context.getPainter().paintPopupMenuBackground(context,
  142. g, 0, 0, c.getWidth(), c.getHeight());
  143. paint(context, g);
  144. context.dispose();
  145. }
  146. public void paint(Graphics g, JComponent c) {
  147. SynthContext context = getContext(c);
  148. paint(context, g);
  149. context.dispose();
  150. }
  151. protected void paint(SynthContext context, Graphics g) {
  152. }
  153. public void paintBorder(SynthContext context, Graphics g, int x,
  154. int y, int w, int h) {
  155. context.getPainter().paintPopupMenuBorder(context, g, x, y, w, h);
  156. }
  157. public void propertyChange(PropertyChangeEvent e) {
  158. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  159. updateStyle(popupMenu);
  160. }
  161. }
  162. }