1. /*
  2. * @(#)SynthSeparatorUI.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.beans.*;
  9. import javax.swing.*;
  10. import java.awt.Dimension;
  11. import java.awt.Graphics;
  12. import java.awt.Insets;
  13. import javax.swing.plaf.ComponentUI;
  14. import javax.swing.plaf.SeparatorUI;
  15. import javax.swing.plaf.UIResource;
  16. import javax.swing.plaf.DimensionUIResource;
  17. import sun.swing.plaf.synth.SynthUI;
  18. /**
  19. * A Synth L&F implementation of SeparatorUI. This implementation
  20. * is a "combined" view/controller.
  21. *
  22. * @version 1.14 12/19/03
  23. * @author Shannon Hickey
  24. * @author Joshua Outwater
  25. */
  26. class SynthSeparatorUI extends SeparatorUI implements PropertyChangeListener,
  27. SynthUI {
  28. private SynthStyle style;
  29. public static ComponentUI createUI(JComponent c) {
  30. return new SynthSeparatorUI();
  31. }
  32. public void installUI(JComponent c) {
  33. installDefaults((JSeparator)c);
  34. installListeners((JSeparator)c);
  35. }
  36. public void uninstallDefaults(JComponent c) {
  37. uninstallListeners((JSeparator)c);
  38. uninstallDefaults((JSeparator)c);
  39. }
  40. public void installDefaults(JSeparator c) {
  41. updateStyle(c);
  42. }
  43. private void updateStyle(JSeparator sep) {
  44. SynthContext context = getContext(sep, ENABLED);
  45. SynthStyle oldStyle = style;
  46. style = SynthLookAndFeel.updateStyle(context, this);
  47. if (style != oldStyle) {
  48. if (sep instanceof JToolBar.Separator) {
  49. Dimension size = ((JToolBar.Separator)sep).getSeparatorSize();
  50. if (size == null || size instanceof UIResource) {
  51. size = (DimensionUIResource)style.get(
  52. context, "ToolBar.separatorSize");
  53. if (size == null) {
  54. size = new DimensionUIResource(10, 10);
  55. }
  56. ((JToolBar.Separator)sep).setSeparatorSize(size);
  57. }
  58. }
  59. }
  60. context.dispose();
  61. }
  62. public void uninstallDefaults(JSeparator c) {
  63. SynthContext context = getContext(c, ENABLED);
  64. style.uninstallDefaults(context);
  65. context.dispose();
  66. style = null;
  67. }
  68. public void installListeners(JSeparator c) {
  69. c.addPropertyChangeListener(this);
  70. }
  71. public void uninstallListeners(JSeparator c) {
  72. c.removePropertyChangeListener(this);
  73. }
  74. public void update(Graphics g, JComponent c) {
  75. SynthContext context = getContext(c);
  76. SynthLookAndFeel.update(context, g);
  77. context.getPainter().paintSeparatorBackground(context,
  78. g, 0, 0, c.getWidth(), c.getHeight());
  79. paint(context, g);
  80. context.dispose();
  81. }
  82. public void paint(Graphics g, JComponent c) {
  83. SynthContext context = getContext(c);
  84. paint(context, g);
  85. context.dispose();
  86. }
  87. protected void paint(SynthContext context, Graphics g) {
  88. JSeparator separator = (JSeparator)context.getComponent();
  89. context.getPainter().paintSeparatorForeground(context, g, 0, 0,
  90. separator.getWidth(), separator.getHeight(),
  91. separator.getOrientation());
  92. }
  93. public void paintBorder(SynthContext context, Graphics g, int x,
  94. int y, int w, int h) {
  95. context.getPainter().paintSeparatorBorder(context, g, x, y, w, h);
  96. }
  97. public Dimension getPreferredSize(JComponent c) {
  98. SynthContext context = getContext(c);
  99. int thickness = style.getInt(context, "Separator.thickness", 2);
  100. Insets insets = c.getInsets();
  101. Dimension size;
  102. if (((JSeparator)c).getOrientation() == JSeparator.VERTICAL) {
  103. size = new Dimension(insets.left + insets.right + thickness,
  104. insets.top + insets.bottom);
  105. } else {
  106. size = new Dimension(insets.left + insets.right,
  107. insets.top + insets.bottom + thickness);
  108. }
  109. context.dispose();
  110. return size;
  111. }
  112. public Dimension getMinimumSize(JComponent c) {
  113. return getPreferredSize(c);
  114. }
  115. public Dimension getMaximumSize(JComponent c) {
  116. return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  117. }
  118. public SynthContext getContext(JComponent c) {
  119. return getContext(c, getComponentState(c));
  120. }
  121. private SynthContext getContext(JComponent c, int state) {
  122. return SynthContext.getContext(SynthContext.class, c,
  123. SynthLookAndFeel.getRegion(c), style, state);
  124. }
  125. private Region getRegion(JComponent c) {
  126. return SynthLookAndFeel.getRegion(c);
  127. }
  128. private int getComponentState(JComponent c) {
  129. return SynthLookAndFeel.getComponentState(c);
  130. }
  131. public void propertyChange(PropertyChangeEvent evt) {
  132. if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
  133. updateStyle((JSeparator)evt.getSource());
  134. }
  135. }
  136. }