1. /*
  2. * @(#)SynthProgressBarUI.java 1.23 04/04/02
  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.geom.AffineTransform;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. import javax.swing.event.*;
  13. import javax.swing.plaf.*;
  14. import javax.swing.plaf.basic.BasicProgressBarUI;
  15. import java.beans.PropertyChangeListener;
  16. import java.beans.PropertyChangeEvent;
  17. import java.io.Serializable;
  18. import sun.swing.plaf.synth.SynthUI;
  19. import com.sun.java.swing.SwingUtilities2;
  20. /**
  21. * Synth's ProgressBarUI.
  22. *
  23. * @version 1.23, 04/02/04
  24. * @author Joshua Outwater
  25. */
  26. class SynthProgressBarUI extends BasicProgressBarUI implements SynthUI,
  27. PropertyChangeListener {
  28. private SynthStyle style;
  29. private int progressPadding;
  30. public static ComponentUI createUI(JComponent x) {
  31. return new SynthProgressBarUI();
  32. }
  33. protected void installListeners() {
  34. super.installListeners();
  35. progressBar.addPropertyChangeListener(this);
  36. }
  37. protected void uninstallListeners() {
  38. super.uninstallListeners();
  39. progressBar.removePropertyChangeListener(this);
  40. }
  41. protected void installDefaults() {
  42. updateStyle(progressBar);
  43. }
  44. private void updateStyle(JProgressBar c) {
  45. SynthContext context = getContext(c, ENABLED);
  46. SynthStyle oldStyle = style;
  47. style = SynthLookAndFeel.updateStyle(context, this);
  48. if (style != oldStyle) {
  49. setCellLength(style.getInt(context, "ProgressBar.cellLength", 1));
  50. setCellSpacing(style.getInt(context, "ProgressBar.cellSpacing", 0));
  51. progressPadding = style.getInt(context,
  52. "ProgressBar.progressPadding", 0);
  53. }
  54. context.dispose();
  55. }
  56. protected void uninstallDefaults() {
  57. SynthContext context = getContext(progressBar, ENABLED);
  58. style.uninstallDefaults(context);
  59. context.dispose();
  60. style = null;
  61. }
  62. public SynthContext getContext(JComponent c) {
  63. return getContext(c, getComponentState(c));
  64. }
  65. private SynthContext getContext(JComponent c, int state) {
  66. return SynthContext.getContext(SynthContext.class, c,
  67. SynthLookAndFeel.getRegion(c), style, state);
  68. }
  69. private Region getRegion(JComponent c) {
  70. return SynthLookAndFeel.getRegion(c);
  71. }
  72. private int getComponentState(JComponent c) {
  73. return SynthLookAndFeel.getComponentState(c);
  74. }
  75. public void update(Graphics g, JComponent c) {
  76. SynthContext context = getContext(c);
  77. SynthLookAndFeel.update(context, g);
  78. context.getPainter().paintProgressBarBackground(context,
  79. g, 0, 0, c.getWidth(), c.getHeight());
  80. paint(context, g);
  81. context.dispose();
  82. }
  83. public void paint(Graphics g, JComponent c) {
  84. SynthContext context = getContext(c);
  85. paint(context, g);
  86. context.dispose();
  87. }
  88. protected void paint(SynthContext context, Graphics g) {
  89. JProgressBar pBar = (JProgressBar)context.getComponent();
  90. int x = 0, y = 0, width = 0, height = 0;
  91. if (!pBar.isIndeterminate()) {
  92. Insets pBarInsets = pBar.getInsets();
  93. double percentComplete = pBar.getPercentComplete();
  94. if (percentComplete != 0.0) {
  95. if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
  96. x = pBarInsets.left + progressPadding;
  97. y = pBarInsets.top + progressPadding;
  98. width = (int)(percentComplete * (pBar.getWidth()
  99. - (pBarInsets.left + progressPadding
  100. + pBarInsets.right + progressPadding)));
  101. height = pBar.getHeight()
  102. - (pBarInsets.top + progressPadding
  103. + pBarInsets.bottom + progressPadding);
  104. if (!SynthLookAndFeel.isLeftToRight(pBar)) {
  105. x = pBar.getWidth() - pBarInsets.right - width
  106. - progressPadding;
  107. }
  108. } else { // JProgressBar.VERTICAL
  109. x = pBarInsets.left + progressPadding;
  110. width = pBar.getWidth()
  111. - (pBarInsets.left + progressPadding
  112. + pBarInsets.right + progressPadding);
  113. height = (int)(percentComplete * (pBar.getHeight()
  114. - (pBarInsets.top + progressPadding
  115. + pBarInsets.bottom + progressPadding)));
  116. y = pBar.getHeight() - pBarInsets.bottom - height
  117. - progressPadding;
  118. // When the progress bar is vertical we always paint
  119. // from bottom to top, not matter what the component
  120. // orientation is.
  121. }
  122. }
  123. } else {
  124. boxRect = getBox(boxRect);
  125. x = boxRect.x + progressPadding;
  126. y = boxRect.y + progressPadding;
  127. width = boxRect.width - progressPadding - progressPadding;
  128. height = boxRect.height - progressPadding - progressPadding;
  129. }
  130. context.getPainter().paintProgressBarForeground(context, g,
  131. x, y, width, height, pBar.getOrientation());
  132. if (pBar.isStringPainted() && !pBar.isIndeterminate()) {
  133. paintText(context, g, pBar.getString());
  134. }
  135. }
  136. protected void paintText(SynthContext context, Graphics g,
  137. String title) {
  138. Font font = context.getStyle().getFont(context);
  139. FontMetrics metrics = SwingUtilities2.getFontMetrics(progressBar, g,
  140. font);
  141. if (progressBar.isStringPainted()) {
  142. String pBarString = progressBar.getString();
  143. Rectangle bounds = progressBar.getBounds();
  144. int strLength = context.getStyle().getGraphicsUtils(context).
  145. computeStringWidth(context, font, metrics, pBarString);
  146. // Calculate the bounds for the text.
  147. Rectangle textRect = new Rectangle(
  148. (bounds.width / 2) - (strLength / 2),
  149. (bounds.height -
  150. (metrics.getAscent() + metrics.getDescent())) / 2,
  151. 0, 0);
  152. // Progress bar isn't tall enough for the font. Don't paint it.
  153. if (textRect.y < 0) {
  154. return;
  155. }
  156. // Paint the text.
  157. SynthStyle style = context.getStyle();
  158. g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
  159. g.setFont(style.getFont(context));
  160. style.getGraphicsUtils(context).paintText(context, g, title,
  161. textRect.x, textRect.y, -1);
  162. }
  163. }
  164. public void paintBorder(SynthContext context, Graphics g, int x,
  165. int y, int w, int h) {
  166. context.getPainter().paintProgressBarBorder(context, g, x, y, w, h);
  167. }
  168. public void propertyChange(PropertyChangeEvent e) {
  169. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  170. updateStyle((JProgressBar)e.getSource());
  171. }
  172. }
  173. public Dimension getPreferredSize(JComponent c) {
  174. Dimension size;
  175. Insets border = progressBar.getInsets();
  176. FontMetrics fontSizer = progressBar.getFontMetrics(
  177. progressBar.getFont());
  178. if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  179. size = new Dimension(getPreferredInnerHorizontal());
  180. } else {
  181. size = new Dimension(getPreferredInnerVertical());
  182. }
  183. // Ensure that the progress string will fit.
  184. if (progressBar.isStringPainted()) {
  185. String progString = progressBar.getString();
  186. int stringHeight = fontSizer.getHeight() +
  187. fontSizer.getDescent();
  188. if (stringHeight > size.height) {
  189. size.height = stringHeight;
  190. }
  191. // This is also for completeness.
  192. int stringWidth = SwingUtilities2.stringWidth(
  193. progressBar, fontSizer, progString);
  194. if (stringWidth > size.width) {
  195. size.width = stringWidth;
  196. }
  197. }
  198. size.width += border.left + border.right;
  199. size.height += border.top + border.bottom;
  200. return size;
  201. }
  202. }