1. /*
  2. * @(#)SynthOptionPaneUI.java 1.14 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 java.awt.*;
  9. import java.beans.*;
  10. import javax.swing.*;
  11. import javax.swing.event.*;
  12. import javax.swing.plaf.*;
  13. import javax.swing.plaf.basic.*;
  14. import sun.swing.DefaultLookup;
  15. import sun.swing.plaf.synth.SynthUI;
  16. /**
  17. * Synth's OptionPaneUI.
  18. *
  19. * @version 1.14, 12/19/03
  20. * @author James Gosling
  21. * @author Scott Violet
  22. * @author Amy Fowler
  23. */
  24. class SynthOptionPaneUI extends BasicOptionPaneUI implements
  25. PropertyChangeListener, SynthUI {
  26. private SynthStyle style;
  27. /**
  28. * Creates a new BasicOptionPaneUI instance.
  29. */
  30. public static ComponentUI createUI(JComponent x) {
  31. return new SynthOptionPaneUI();
  32. }
  33. protected void installDefaults() {
  34. updateStyle(optionPane);
  35. }
  36. protected void installListeners() {
  37. super.installListeners();
  38. optionPane.addPropertyChangeListener(this);
  39. }
  40. private void updateStyle(JComponent c) {
  41. SynthContext context = getContext(c, ENABLED);
  42. SynthStyle oldStyle = style;
  43. style = SynthLookAndFeel.updateStyle(context, this);
  44. if (style != oldStyle) {
  45. minimumSize = (Dimension)style.get(context,
  46. "OptionPane.minimumSize");
  47. if (minimumSize == null) {
  48. minimumSize = new Dimension(262, 90);
  49. }
  50. if (oldStyle != null) {
  51. uninstallKeyboardActions();
  52. installKeyboardActions();
  53. }
  54. }
  55. context.dispose();
  56. }
  57. protected void uninstallDefaults() {
  58. SynthContext context = getContext(optionPane, ENABLED);
  59. style.uninstallDefaults(context);
  60. context.dispose();
  61. style = null;
  62. }
  63. protected void uninstallListeners() {
  64. super.uninstallListeners();
  65. optionPane.removePropertyChangeListener(this);
  66. }
  67. protected void installComponents() {
  68. optionPane.add(createMessageArea());
  69. Container separator = createSeparator();
  70. if (separator != null) {
  71. optionPane.add(separator);
  72. SynthContext context = getContext(optionPane, ENABLED);
  73. optionPane.add(Box.createVerticalStrut(context.getStyle().
  74. getInt(context, "OptionPane.separatorPadding", 6)));
  75. context.dispose();
  76. }
  77. optionPane.add(createButtonArea());
  78. optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
  79. }
  80. public SynthContext getContext(JComponent c) {
  81. return getContext(c, getComponentState(c));
  82. }
  83. private SynthContext getContext(JComponent c, int state) {
  84. return SynthContext.getContext(SynthContext.class, c,
  85. SynthLookAndFeel.getRegion(c), style, state);
  86. }
  87. private Region getRegion(JComponent c) {
  88. return SynthLookAndFeel.getRegion(c);
  89. }
  90. private int getComponentState(JComponent c) {
  91. return SynthLookAndFeel.getComponentState(c);
  92. }
  93. public void update(Graphics g, JComponent c) {
  94. SynthContext context = getContext(c);
  95. SynthLookAndFeel.update(context, g);
  96. context.getPainter().paintOptionPaneBackground(context,
  97. g, 0, 0, c.getWidth(), c.getHeight());
  98. paint(context, g);
  99. context.dispose();
  100. }
  101. public void paint(Graphics g, JComponent c) {
  102. SynthContext context = getContext(c);
  103. paint(context, g);
  104. context.dispose();
  105. }
  106. protected void paint(SynthContext context, Graphics g) {
  107. }
  108. public void paintBorder(SynthContext context, Graphics g, int x,
  109. int y, int w, int h) {
  110. context.getPainter().paintOptionPaneBorder(context, g, x, y, w, h);
  111. }
  112. public void propertyChange(PropertyChangeEvent e) {
  113. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  114. updateStyle((JOptionPane)e.getSource());
  115. }
  116. }
  117. protected boolean getSizeButtonsToSameWidth() {
  118. return DefaultLookup.getBoolean(optionPane, this,
  119. "OptionPane.sameSizeButtons", true);
  120. }
  121. /**
  122. * Messaged from installComponents to create a Container containing the
  123. * body of the message. The icon is the created by calling
  124. * <code>addIcon</code>.
  125. */
  126. protected Container createMessageArea() {
  127. JPanel top = new JPanel();
  128. top.setName("OptionPane.messageArea");
  129. top.setLayout(new BorderLayout());
  130. /* Fill the body. */
  131. Container body = new JPanel(new GridBagLayout());
  132. Container realBody = new JPanel(new BorderLayout());
  133. body.setName("OptionPane.body");
  134. realBody.setName("OptionPane.realBody");
  135. if (getIcon() != null) {
  136. JPanel sep = new JPanel();
  137. sep.setName("OptionPane.separator");
  138. sep.setPreferredSize(new Dimension(15, 1));
  139. realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);
  140. }
  141. realBody.add(body, BorderLayout.CENTER);
  142. GridBagConstraints cons = new GridBagConstraints();
  143. cons.gridx = cons.gridy = 0;
  144. cons.gridwidth = GridBagConstraints.REMAINDER;
  145. cons.gridheight = 1;
  146. SynthContext context = getContext(optionPane, ENABLED);
  147. cons.anchor = context.getStyle().getInt(context,
  148. "OptionPane.messageAnchor", GridBagConstraints.CENTER);
  149. context.dispose();
  150. cons.insets = new Insets(0,0,3,0);
  151. addMessageComponents(body, cons, getMessage(),
  152. getMaxCharactersPerLineCount(), false);
  153. top.add(realBody, BorderLayout.CENTER);
  154. addIcon(top);
  155. return top;
  156. }
  157. protected Container createSeparator() {
  158. JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
  159. separator.setName("OptionPane.separator");
  160. return separator;
  161. }
  162. }