1. /*
  2. * @(#)SynthScrollBarUI.java 1.28 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.awt.event.*;
  10. import java.beans.*;
  11. import javax.swing.*;
  12. import javax.swing.event.*;
  13. import javax.swing.plaf.*;
  14. import javax.swing.plaf.basic.*;
  15. import sun.swing.plaf.synth.SynthUI;
  16. /**
  17. * Synth's ScrollBarUI.
  18. *
  19. * @version 1.28, 12/19/03
  20. * @author Scott Violet
  21. */
  22. class SynthScrollBarUI extends BasicScrollBarUI implements
  23. PropertyChangeListener, SynthUI {
  24. private static final Insets tmpInsets = new Insets(0, 0, 0, 0);
  25. private SynthStyle style;
  26. private SynthStyle thumbStyle;
  27. private SynthStyle trackStyle;
  28. private int scrollBarWidth;
  29. public static ComponentUI createUI(JComponent c) {
  30. return new SynthScrollBarUI();
  31. }
  32. protected void installDefaults() {
  33. trackHighlight = NO_HIGHLIGHT;
  34. if (scrollbar.getLayout() == null ||
  35. (scrollbar.getLayout() instanceof UIResource)) {
  36. scrollbar.setLayout(this);
  37. }
  38. updateStyle(scrollbar);
  39. }
  40. protected void configureScrollBarColors() {
  41. }
  42. private void updateStyle(JScrollBar c) {
  43. SynthStyle oldStyle = style;
  44. SynthContext context = getContext(c, ENABLED);
  45. style = SynthLookAndFeel.updateStyle(context, this);
  46. if (style != oldStyle) {
  47. Insets insets = c.getInsets();
  48. scrollBarWidth = style.getInt(context,"ScrollBar.thumbHeight", 14);
  49. minimumThumbSize = new Dimension();
  50. if (c.getOrientation() == JScrollBar.VERTICAL) {
  51. minimumThumbSize.width = scrollBarWidth;
  52. minimumThumbSize.height = 7;
  53. scrollBarWidth += insets.left + insets.right;
  54. } else {
  55. minimumThumbSize.width = 7;
  56. minimumThumbSize.height = scrollBarWidth;
  57. scrollBarWidth += insets.top + insets.bottom;
  58. }
  59. maximumThumbSize = (Dimension)style.get(context,
  60. "ScrollBar.maximumThumbSize");
  61. if (maximumThumbSize == null) {
  62. maximumThumbSize = new Dimension(4096, 4097);
  63. }
  64. if (oldStyle != null) {
  65. uninstallKeyboardActions();
  66. installKeyboardActions();
  67. }
  68. }
  69. context.dispose();
  70. context = getContext(c, Region.SCROLL_BAR_TRACK, ENABLED);
  71. trackStyle = SynthLookAndFeel.updateStyle(context, this);
  72. context.dispose();
  73. context = getContext(c, Region.SCROLL_BAR_THUMB, ENABLED);
  74. thumbStyle = SynthLookAndFeel.updateStyle(context, this);
  75. context.dispose();
  76. }
  77. protected void installListeners() {
  78. super.installListeners();
  79. scrollbar.addPropertyChangeListener(this);
  80. }
  81. protected void uninstallListeners() {
  82. super.uninstallListeners();
  83. scrollbar.removePropertyChangeListener(this);
  84. }
  85. protected void uninstallDefaults(){
  86. SynthContext context = getContext(scrollbar, ENABLED);
  87. style.uninstallDefaults(context);
  88. context.dispose();
  89. style = null;
  90. context = getContext(scrollbar, Region.SCROLL_BAR_TRACK, ENABLED);
  91. trackStyle.uninstallDefaults(context);
  92. context.dispose();
  93. trackStyle = null;
  94. context = getContext(scrollbar, Region.SCROLL_BAR_THUMB, ENABLED);
  95. thumbStyle.uninstallDefaults(context);
  96. context.dispose();
  97. thumbStyle = null;
  98. super.uninstallDefaults();
  99. }
  100. public SynthContext getContext(JComponent c) {
  101. return getContext(c, getComponentState(c));
  102. }
  103. private SynthContext getContext(JComponent c, int state) {
  104. return SynthContext.getContext(SynthContext.class, c,
  105. SynthLookAndFeel.getRegion(c), style, state);
  106. }
  107. private Region getRegion(JComponent c) {
  108. return SynthLookAndFeel.getRegion(c);
  109. }
  110. private int getComponentState(JComponent c) {
  111. return SynthLookAndFeel.getComponentState(c);
  112. }
  113. private SynthContext getContext(JComponent c, Region region) {
  114. return getContext(c, region, getComponentState(c, region));
  115. }
  116. private SynthContext getContext(JComponent c, Region region, int state) {
  117. SynthStyle style = trackStyle;
  118. if (region == Region.SCROLL_BAR_THUMB) {
  119. style = thumbStyle;
  120. }
  121. return SynthContext.getContext(SynthContext.class, c, region, style,
  122. state);
  123. }
  124. private int getComponentState(JComponent c, Region region) {
  125. if (region == Region.SCROLL_BAR_THUMB && isThumbRollover() &&
  126. c.isEnabled()) {
  127. return MOUSE_OVER;
  128. }
  129. return SynthLookAndFeel.getComponentState(c);
  130. }
  131. public boolean getSupportsAbsolutePositioning() {
  132. SynthContext context = getContext(scrollbar);
  133. boolean value = style.getBoolean(context,
  134. "ScrollBar.allowsAbsolutePositioning", false);
  135. context.dispose();
  136. return value;
  137. }
  138. public void update(Graphics g, JComponent c) {
  139. SynthContext context = getContext(c);
  140. SynthLookAndFeel.update(context, g);
  141. context.getPainter().paintScrollBarBackground(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. SynthContext subcontext = getContext(scrollbar,
  153. Region.SCROLL_BAR_TRACK);
  154. paintTrack(subcontext, g, getTrackBounds());
  155. subcontext.dispose();
  156. subcontext = getContext(scrollbar, Region.SCROLL_BAR_THUMB);
  157. paintThumb(subcontext, g, getThumbBounds());
  158. subcontext.dispose();
  159. }
  160. public void paintBorder(SynthContext context, Graphics g, int x,
  161. int y, int w, int h) {
  162. context.getPainter().paintScrollBarBorder(context, g, x, y, w, h);
  163. }
  164. protected void paintTrack(SynthContext ss, Graphics g,
  165. Rectangle trackBounds) {
  166. SynthLookAndFeel.updateSubregion(ss, g, trackBounds);
  167. ss.getPainter().paintScrollBarTrackBackground(ss, g, trackBounds.x,
  168. trackBounds.y, trackBounds.width, trackBounds.height);
  169. ss.getPainter().paintScrollBarTrackBorder(ss, g, trackBounds.x,
  170. trackBounds.y, trackBounds.width, trackBounds.height);
  171. }
  172. protected void paintThumb(SynthContext ss, Graphics g,
  173. Rectangle thumbBounds) {
  174. int orientation = scrollbar.getOrientation();
  175. ss.getPainter().paintScrollBarThumbBackground(ss, g, thumbBounds.x,
  176. thumbBounds.y, thumbBounds.width, thumbBounds.height,
  177. orientation);
  178. ss.getPainter().paintScrollBarThumbBorder(ss, g, thumbBounds.x,
  179. thumbBounds.y, thumbBounds.width, thumbBounds.height,
  180. orientation);
  181. }
  182. /**
  183. * A vertical scrollbar's preferred width is the maximum of
  184. * preferred widths of the (non <code>null</code>)
  185. * increment/decrement buttons,
  186. * and the minimum width of the thumb. The preferred height is the
  187. * sum of the preferred heights of the same parts. The basis for
  188. * the preferred size of a horizontal scrollbar is similar.
  189. * <p>
  190. * The <code>preferredSize</code> is only computed once, subsequent
  191. * calls to this method just return a cached size.
  192. *
  193. * @param c the <code>JScrollBar</code> that's delegating this method to us
  194. * @return the preferred size of a Basic JScrollBar
  195. * @see #getMaximumSize
  196. * @see #getMinimumSize
  197. */
  198. public Dimension getPreferredSize(JComponent c) {
  199. return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
  200. ? new Dimension(scrollBarWidth, 48)
  201. : new Dimension(48, scrollBarWidth);
  202. }
  203. protected JButton createDecreaseButton(int orientation) {
  204. SynthArrowButton synthArrowButton = new SynthArrowButton(orientation);
  205. synthArrowButton.setName("ScrollBar.button");
  206. return synthArrowButton;
  207. }
  208. protected JButton createIncreaseButton(int orientation) {
  209. SynthArrowButton synthArrowButton = new SynthArrowButton(orientation);
  210. synthArrowButton.setName("ScrollBar.button");
  211. return synthArrowButton;
  212. }
  213. protected void setThumbRollover(boolean active) {
  214. if (isThumbRollover() != active) {
  215. scrollbar.repaint(getThumbBounds());
  216. super.setThumbRollover(active);
  217. }
  218. }
  219. private void updateButtonDirections() {
  220. int orient = scrollbar.getOrientation();
  221. if (scrollbar.getComponentOrientation().isLeftToRight()) {
  222. ((SynthArrowButton)incrButton).setDirection(
  223. orient == HORIZONTAL? EAST : SOUTH);
  224. ((SynthArrowButton)decrButton).setDirection(
  225. orient == HORIZONTAL? WEST : NORTH);
  226. }
  227. else {
  228. ((SynthArrowButton)incrButton).setDirection(
  229. orient == HORIZONTAL? WEST : SOUTH);
  230. ((SynthArrowButton)decrButton).setDirection(
  231. orient == HORIZONTAL ? EAST : NORTH);
  232. }
  233. }
  234. //
  235. // PropertyChangeListener
  236. //
  237. public void propertyChange(PropertyChangeEvent e) {
  238. String propertyName = e.getPropertyName();
  239. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  240. updateStyle((JScrollBar)e.getSource());
  241. }
  242. else if ("orientation" == propertyName) {
  243. updateButtonDirections();
  244. }
  245. else if ("componentOrientation" == propertyName) {
  246. updateButtonDirections();
  247. }
  248. }
  249. }