1. /*
  2. * @(#)MetalProgressBarUI.java 1.25 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.metal;
  8. import javax.swing.*;
  9. import javax.swing.plaf.*;
  10. import javax.swing.plaf.basic.*;
  11. import java.awt.*;
  12. /**
  13. * The Metal implementation of ProgressBarUI.
  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
  18. * appropriate for short term storage or RMI between applications running
  19. * the same version of Swing. As of 1.4, support for long term storage
  20. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  21. * has been added to the <code>java.beans</code> package.
  22. * Please see {@link java.beans.XMLEncoder}.
  23. *
  24. * @version 1.25 01/23/03
  25. * @author Michael C. Albers
  26. */
  27. public class MetalProgressBarUI extends BasicProgressBarUI {
  28. private Rectangle innards;
  29. private Rectangle box;
  30. public static ComponentUI createUI(JComponent c) {
  31. return new MetalProgressBarUI();
  32. }
  33. /**
  34. * Draws a bit of special highlighting on the progress bar.
  35. * The core painting is deferred to the BasicProgressBar's
  36. * <code>paintDeterminate</code> method.
  37. */
  38. public void paintDeterminate(Graphics g, JComponent c) {
  39. super.paintDeterminate(g,c);
  40. if (!(g instanceof Graphics2D)) {
  41. return;
  42. }
  43. if (progressBar.isBorderPainted()) {
  44. Insets b = progressBar.getInsets(); // area for border
  45. int barRectWidth = progressBar.getWidth() - (b.left + b.right);
  46. int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
  47. int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
  48. boolean isLeftToRight = MetalUtils.isLeftToRight(c);
  49. int startX, startY, endX, endY;
  50. // The progress bar border is painted according to a light source.
  51. // This light source is stationary and does not change when the
  52. // component orientation changes.
  53. startX = b.left;
  54. startY = b.top;
  55. endX = b.left + barRectWidth - 1;
  56. endY = b.top + barRectHeight - 1;
  57. Graphics2D g2 = (Graphics2D)g;
  58. g2.setStroke(new BasicStroke(1.f));
  59. if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  60. // Draw light line lengthwise across the progress bar.
  61. g2.setColor(MetalLookAndFeel.getControlShadow());
  62. g2.drawLine(startX, startY, endX, startY);
  63. if (amountFull > 0) {
  64. // Draw darker lengthwise line over filled area.
  65. g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  66. if (isLeftToRight) {
  67. g2.drawLine(startX, startY,
  68. startX + amountFull - 1, startY);
  69. } else {
  70. g2.drawLine(endX, startY,
  71. endX - amountFull + 1, startY);
  72. if (progressBar.getPercentComplete() != 1.f) {
  73. g2.setColor(MetalLookAndFeel.getControlShadow());
  74. }
  75. }
  76. }
  77. // Draw a line across the width. The color is determined by
  78. // the code above.
  79. g2.drawLine(startX, startY, startX, endY);
  80. } else { // VERTICAL
  81. // Draw light line lengthwise across the progress bar.
  82. g2.setColor(MetalLookAndFeel.getControlShadow());
  83. g2.drawLine(startX, startY, startX, endY);
  84. if (amountFull > 0) {
  85. // Draw darker lengthwise line over filled area.
  86. g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  87. g2.drawLine(startX, endY,
  88. startX, endY - amountFull + 1);
  89. }
  90. // Draw a line across the width. The color is determined by
  91. // the code above.
  92. g2.setColor(MetalLookAndFeel.getControlShadow());
  93. if (progressBar.getPercentComplete() == 1.f) {
  94. g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  95. }
  96. g2.drawLine(startX, startY, endX, startY);
  97. }
  98. }
  99. }
  100. /**
  101. * Draws a bit of special highlighting on the progress bar
  102. * and bouncing box.
  103. * The core painting is deferred to the BasicProgressBar's
  104. * <code>paintIndeterminate</code> method.
  105. */
  106. public void paintIndeterminate(Graphics g, JComponent c) {
  107. super.paintIndeterminate(g, c);
  108. if (!progressBar.isBorderPainted() || (!(g instanceof Graphics2D))) {
  109. return;
  110. }
  111. Insets b = progressBar.getInsets(); // area for border
  112. int barRectWidth = progressBar.getWidth() - (b.left + b.right);
  113. int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
  114. int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
  115. boolean isLeftToRight = MetalUtils.isLeftToRight(c);
  116. int startX, startY, endX, endY;
  117. Rectangle box = null;
  118. box = getBox(box);
  119. // The progress bar border is painted according to a light source.
  120. // This light source is stationary and does not change when the
  121. // component orientation changes.
  122. startX = b.left;
  123. startY = b.top;
  124. endX = b.left + barRectWidth - 1;
  125. endY = b.top + barRectHeight - 1;
  126. Graphics2D g2 = (Graphics2D)g;
  127. g2.setStroke(new BasicStroke(1.f));
  128. if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  129. // Draw light line lengthwise across the progress bar.
  130. g2.setColor(MetalLookAndFeel.getControlShadow());
  131. g2.drawLine(startX, startY, endX, startY);
  132. g2.drawLine(startX, startY, startX, endY);
  133. // Draw darker lengthwise line over filled area.
  134. g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  135. g2.drawLine(box.x, startY, box.x + box.width - 1, startY);
  136. } else { // VERTICAL
  137. // Draw light line lengthwise across the progress bar.
  138. g2.setColor(MetalLookAndFeel.getControlShadow());
  139. g2.drawLine(startX, startY, startX, endY);
  140. g2.drawLine(startX, startY, endX, startY);
  141. // Draw darker lengthwise line over filled area.
  142. g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
  143. g2.drawLine(startX, box.y, startX, box.y + box.height - 1);
  144. }
  145. }
  146. }