1. /*
  2. * @(#)WindowsProgressBarUI.java 1.24 04/04/16
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import javax.swing.plaf.basic.*;
  9. import javax.swing.plaf.*;
  10. import javax.swing.*;
  11. import java.awt.*;
  12. /**
  13. * Windows rendition of the component.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is appropriate
  18. * for short term storage or RMI between applications running the same
  19. * version of Swing. A future release of Swing will provide support for
  20. * long term persistence.
  21. *
  22. * @version 1.24 04/16/04
  23. * @author Michael C. Albers
  24. */
  25. public class WindowsProgressBarUI extends BasicProgressBarUI
  26. {
  27. private Rectangle boxRect;
  28. public static ComponentUI createUI(JComponent x) {
  29. return new WindowsProgressBarUI();
  30. }
  31. protected void installDefaults() {
  32. super.installDefaults();
  33. if (XPStyle.getXP() != null) {
  34. LookAndFeel.installProperty(progressBar, "opaque", Boolean.FALSE);
  35. progressBar.setBorder(null);
  36. }
  37. }
  38. protected Dimension getPreferredInnerHorizontal() {
  39. XPStyle xp = XPStyle.getXP();
  40. if (xp != null) {
  41. return xp.getDimension("progress.bar.normalsize");
  42. } else {
  43. return super.getPreferredInnerHorizontal();
  44. }
  45. }
  46. protected Dimension getPreferredInnerVertical() {
  47. if (XPStyle.getXP() != null) {
  48. Dimension d = getPreferredInnerHorizontal();
  49. return new Dimension(d.height, d.width); // Reverse values
  50. } else {
  51. return super.getPreferredInnerVertical();
  52. }
  53. }
  54. protected void paintDeterminate(Graphics g, JComponent c) {
  55. XPStyle xp = XPStyle.getXP();
  56. if (xp != null) {
  57. boolean vertical = (progressBar.getOrientation() == JProgressBar.VERTICAL);
  58. boolean isLeftToRight = WindowsGraphicsUtils.isLeftToRight(c);
  59. int barRectWidth = progressBar.getWidth();
  60. int barRectHeight = progressBar.getHeight()-1;
  61. // amount of progress to draw
  62. int amountFull = getAmountFull(null, barRectWidth, barRectHeight);
  63. paintXPBackground(g, vertical, barRectWidth, barRectHeight);
  64. // Paint progress
  65. if (progressBar.isStringPainted()) {
  66. // Do not paint the standard stripes from the skin, because they obscure
  67. // the text
  68. g.setColor(progressBar.getForeground());
  69. barRectHeight -= 2;
  70. barRectWidth -= 2;
  71. Graphics2D g2 = (Graphics2D)g;
  72. g2.setStroke(new BasicStroke((float)(vertical ? barRectWidth : barRectHeight),
  73. BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
  74. if (!vertical) {
  75. if (isLeftToRight) {
  76. g2.drawLine(2, barRectHeight / 2 + 1,
  77. amountFull - 2, barRectHeight / 2 + 1);
  78. } else {
  79. g2.drawLine(2 + barRectWidth,
  80. barRectHeight / 2 + 1,
  81. 2 + barRectWidth - (amountFull - 2),
  82. barRectHeight / 2 + 1);
  83. }
  84. paintString(g, 0, 0, barRectWidth, barRectHeight, amountFull, null);
  85. } else {
  86. g2.drawLine(barRectWidth2 + 1, barRectHeight + 1,
  87. barRectWidth2 + 1, barRectHeight + 1 - amountFull + 2);
  88. paintString(g, 2, 2, barRectWidth, barRectHeight, amountFull, null);
  89. }
  90. } else {
  91. XPStyle.Skin skin =
  92. xp.getSkin(vertical ? "progress.chunkvert" : "progress.chunk");
  93. int thickness;
  94. if (vertical) {
  95. thickness = barRectWidth - 5;
  96. } else {
  97. thickness = barRectHeight - 5;
  98. }
  99. int chunkSize = xp.getInt("progress.progresschunksize", 2);
  100. int spaceSize = xp.getInt("progress.progressspacesize", 0);
  101. int nChunks = (amountFull-4) / (chunkSize + spaceSize);
  102. // See if we can squeeze in an extra chunk without spacing after
  103. if (spaceSize > 0 && (nChunks * (chunkSize + spaceSize) + chunkSize) < (amountFull-4)) {
  104. nChunks++;
  105. }
  106. for (int i = 0; i < nChunks; i++) {
  107. if (vertical) {
  108. skin.paintSkin(g,
  109. 3, barRectHeight - i * (chunkSize + spaceSize) - chunkSize - 2,
  110. thickness, chunkSize, 0);
  111. } else {
  112. if (isLeftToRight) {
  113. skin.paintSkin(g,
  114. 4 + i * (chunkSize + spaceSize), 2,
  115. chunkSize, thickness, 0);
  116. } else {
  117. skin.paintSkin(g,
  118. barRectWidth - (2 + (i+1) * (chunkSize + spaceSize)), 2,
  119. chunkSize, thickness, 0);
  120. }
  121. }
  122. }
  123. }
  124. } else {
  125. super.paintDeterminate(g, c);
  126. }
  127. }
  128. protected void paintIndeterminate(Graphics g, JComponent c) {
  129. XPStyle xp = XPStyle.getXP();
  130. if (xp != null) {
  131. boolean vertical = (progressBar.getOrientation() == JProgressBar.VERTICAL);
  132. int barRectWidth = progressBar.getWidth();
  133. int barRectHeight = progressBar.getHeight()-1;
  134. paintXPBackground(g, vertical, barRectWidth, barRectHeight);
  135. // Paint the bouncing box.
  136. boxRect = getBox(boxRect);
  137. if (boxRect != null) {
  138. g.setColor(progressBar.getForeground());
  139. if (!vertical) {
  140. g.fillRect(boxRect.x, boxRect.y + 2, boxRect.width, boxRect.height - 4);
  141. } else {
  142. g.fillRect(boxRect.x + 2, boxRect.y, boxRect.width - 3, boxRect.height);
  143. }
  144. if (progressBar.isStringPainted()) {
  145. if (!vertical) {
  146. paintString(g, -1, -1, barRectWidth, barRectHeight, 0, null);
  147. } else {
  148. paintString(g, 1, 1, barRectWidth, barRectHeight, 0, null);
  149. }
  150. }
  151. }
  152. } else {
  153. super.paintIndeterminate(g, c);
  154. }
  155. }
  156. private void paintXPBackground(Graphics g, boolean vertical,
  157. int barRectWidth, int barRectHeight) {
  158. XPStyle xp = XPStyle.getXP();
  159. String category = vertical ? "progress.barvert" : "progress.bar";
  160. XPStyle.Skin skin = xp.getSkin(category);
  161. // Paint background
  162. Color fillColor = xp.getColor(category + ".fillcolorhint", null);
  163. if (fillColor != null) {
  164. g.setColor(fillColor);
  165. g.fillRect(2, 2, barRectWidth-4, barRectHeight-4);
  166. }
  167. skin.paintSkin(g, 0, 0, barRectWidth, barRectHeight, 0);
  168. }
  169. }