1. /*
  2. * @(#)WindowsProgressBarUI.java 1.21 03/04/22
  3. *
  4. * Copyright 2003 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.21 04/22/03
  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. progressBar.setOpaque(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. int barRectWidth = progressBar.getWidth();
  59. int barRectHeight = progressBar.getHeight()-1;
  60. // amount of progress to draw
  61. int amountFull = getAmountFull(null, barRectWidth, barRectHeight);
  62. paintXPBackground(g, vertical, barRectWidth, barRectHeight);
  63. // Paint progress
  64. if (progressBar.isStringPainted()) {
  65. // Do not paint the standard stripes from the skin, because they obscure
  66. // the text
  67. g.setColor(progressBar.getForeground());
  68. barRectHeight -= 2;
  69. barRectWidth -= 2;
  70. Graphics2D g2 = (Graphics2D)g;
  71. g2.setStroke(new BasicStroke((float)(vertical ? barRectWidth : barRectHeight),
  72. BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
  73. if (!vertical) {
  74. g2.drawLine(2, barRectHeight / 2 + 1,
  75. amountFull - 2, barRectHeight / 2 + 1);
  76. paintString(g, 0, 0, barRectWidth, barRectHeight, amountFull, null);
  77. } else {
  78. g2.drawLine(barRectWidth2 + 1, barRectHeight + 1,
  79. barRectWidth2 + 1, barRectHeight + 1 - amountFull + 2);
  80. paintString(g, 2, 2, barRectWidth, barRectHeight, amountFull, null);
  81. }
  82. } else {
  83. XPStyle.Skin skin =
  84. xp.getSkin(vertical ? "progress.chunkvert" : "progress.chunk");
  85. int thickness;
  86. if (vertical) {
  87. thickness = barRectWidth - 5;
  88. } else {
  89. thickness = barRectHeight - 5;
  90. }
  91. int chunkSize = xp.getInt("progress.progresschunksize", 2);
  92. int spaceSize = xp.getInt("progress.progressspacesize", 0);
  93. int nChunks = (amountFull-4) / (chunkSize + spaceSize);
  94. // See if we can squeeze in an extra chunk without spacing after
  95. if (spaceSize > 0 && (nChunks * (chunkSize + spaceSize) + chunkSize) < (amountFull-4)) {
  96. nChunks++;
  97. }
  98. for (int i = 0; i < nChunks; i++) {
  99. if (vertical) {
  100. skin.paintSkin(g,
  101. 3, barRectHeight - i * (chunkSize + spaceSize) - chunkSize - 2,
  102. thickness, chunkSize, 0);
  103. } else {
  104. skin.paintSkin(g,
  105. 4 + i * (chunkSize + spaceSize), 2,
  106. chunkSize, thickness, 0);
  107. }
  108. }
  109. }
  110. } else {
  111. super.paintDeterminate(g, c);
  112. }
  113. }
  114. protected void paintIndeterminate(Graphics g, JComponent c) {
  115. XPStyle xp = XPStyle.getXP();
  116. if (xp != null) {
  117. boolean vertical = (progressBar.getOrientation() == JProgressBar.VERTICAL);
  118. int barRectWidth = progressBar.getWidth();
  119. int barRectHeight = progressBar.getHeight()-1;
  120. paintXPBackground(g, vertical, barRectWidth, barRectHeight);
  121. // Paint the bouncing box.
  122. boxRect = getBox(boxRect);
  123. if (boxRect != null) {
  124. g.setColor(progressBar.getForeground());
  125. if (!vertical) {
  126. g.fillRect(boxRect.x, boxRect.y + 2, boxRect.width, boxRect.height - 4);
  127. } else {
  128. g.fillRect(boxRect.x + 2, boxRect.y, boxRect.width - 3, boxRect.height);
  129. }
  130. if (progressBar.isStringPainted()) {
  131. if (!vertical) {
  132. paintString(g, -1, -1, barRectWidth, barRectHeight, 0, null);
  133. } else {
  134. paintString(g, 1, 1, barRectWidth, barRectHeight, 0, null);
  135. }
  136. }
  137. }
  138. } else {
  139. super.paintIndeterminate(g, c);
  140. }
  141. }
  142. private void paintXPBackground(Graphics g, boolean vertical,
  143. int barRectWidth, int barRectHeight) {
  144. XPStyle xp = XPStyle.getXP();
  145. String category = vertical ? "progress.barvert" : "progress.bar";
  146. XPStyle.Skin skin = xp.getSkin(category);
  147. // Paint background
  148. Color fillColor = xp.getColor(category + ".fillcolorhint", null);
  149. if (fillColor != null) {
  150. g.setColor(fillColor);
  151. g.fillRect(2, 2, barRectWidth-4, barRectHeight-4);
  152. }
  153. skin.paintSkin(g, 0, 0, barRectWidth, barRectHeight, 0);
  154. }
  155. }