KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalProgressBarUI


1 /*
2  * @(#)MetalProgressBarUI.java 1.26 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
8 package javax.swing.plaf.metal;
9
10 import javax.swing.*;
11 import javax.swing.plaf.*;
12 import javax.swing.plaf.basic.*;
13 import java.awt.*;
14
15 /**
16  * The Metal implementation of ProgressBarUI.
17  * <p>
18  * <strong>Warning:</strong>
19  * Serialized objects of this class will not be compatible with
20  * future Swing releases. The current serialization support is
21  * appropriate for short term storage or RMI between applications running
22  * the same version of Swing. As of 1.4, support for long term storage
23  * of all JavaBeans<sup><font size="-2">TM</font></sup>
24  * has been added to the <code>java.beans</code> package.
25  * Please see {@link java.beans.XMLEncoder}.
26  *
27  * @version 1.26 12/19/03
28  * @author Michael C. Albers
29  */

30 public class MetalProgressBarUI extends BasicProgressBarUI {
31
32     private Rectangle innards;
33     private Rectangle box;
34
35     public static ComponentUI createUI(JComponent c) {
36     return new MetalProgressBarUI JavaDoc();
37     }
38
39     /**
40      * Draws a bit of special highlighting on the progress bar.
41      * The core painting is deferred to the BasicProgressBar's
42      * <code>paintDeterminate</code> method.
43      */

44     public void paintDeterminate(Graphics g, JComponent c) {
45     super.paintDeterminate(g,c);
46
47         if (!(g instanceof Graphics2D)) {
48             return;
49         }
50
51     if (progressBar.isBorderPainted()) {
52         Insets b = progressBar.getInsets(); // area for border
53
int barRectWidth = progressBar.getWidth() - (b.left + b.right);
54         int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
55         int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
56             boolean isLeftToRight = MetalUtils.isLeftToRight(c);
57             int startX, startY, endX, endY;
58         
59             // The progress bar border is painted according to a light source.
60
// This light source is stationary and does not change when the
61
// component orientation changes.
62
startX = b.left;
63             startY = b.top;
64             endX = b.left + barRectWidth - 1;
65             endY = b.top + barRectHeight - 1;
66
67             Graphics2D g2 = (Graphics2D)g;
68             g2.setStroke(new BasicStroke(1.f));
69
70         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
71                 // Draw light line lengthwise across the progress bar.
72
g2.setColor(MetalLookAndFeel.getControlShadow());
73                 g2.drawLine(startX, startY, endX, startY);
74
75                 if (amountFull > 0) {
76                     // Draw darker lengthwise line over filled area.
77
g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
78                     
79                     if (isLeftToRight) {
80                         g2.drawLine(startX, startY,
81                                 startX + amountFull - 1, startY);
82                     } else {
83                         g2.drawLine(endX, startY,
84                                 endX - amountFull + 1, startY);
85                         if (progressBar.getPercentComplete() != 1.f) {
86                     g2.setColor(MetalLookAndFeel.getControlShadow());
87                         }
88                     }
89                 }
90                 // Draw a line across the width. The color is determined by
91
// the code above.
92
g2.drawLine(startX, startY, startX, endY);
93
94         } else { // VERTICAL
95
// Draw light line lengthwise across the progress bar.
96
g2.setColor(MetalLookAndFeel.getControlShadow());
97         g2.drawLine(startX, startY, startX, endY);
98         
99         if (amountFull > 0) {
100                     // Draw darker lengthwise line over filled area.
101
g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
102             g2.drawLine(startX, endY,
103                             startX, endY - amountFull + 1);
104         }
105                 // Draw a line across the width. The color is determined by
106
// the code above.
107
g2.setColor(MetalLookAndFeel.getControlShadow());
108
109                 if (progressBar.getPercentComplete() == 1.f) {
110             g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
111                 }
112         g2.drawLine(startX, startY, endX, startY);
113         }
114     }
115     }
116
117     /**
118      * Draws a bit of special highlighting on the progress bar
119      * and bouncing box.
120      * The core painting is deferred to the BasicProgressBar's
121      * <code>paintIndeterminate</code> method.
122      */

123     public void paintIndeterminate(Graphics g, JComponent c) {
124         super.paintIndeterminate(g, c);
125
126         if (!progressBar.isBorderPainted() || (!(g instanceof Graphics2D))) {
127             return;
128         }
129
130         Insets b = progressBar.getInsets(); // area for border
131
int barRectWidth = progressBar.getWidth() - (b.left + b.right);
132         int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
133         int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
134         boolean isLeftToRight = MetalUtils.isLeftToRight(c);
135         int startX, startY, endX, endY;
136         Rectangle box = null;
137         box = getBox(box);
138     
139         // The progress bar border is painted according to a light source.
140
// This light source is stationary and does not change when the
141
// component orientation changes.
142
startX = b.left;
143         startY = b.top;
144         endX = b.left + barRectWidth - 1;
145         endY = b.top + barRectHeight - 1;
146
147         Graphics2D g2 = (Graphics2D)g;
148         g2.setStroke(new BasicStroke(1.f));
149
150         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
151             // Draw light line lengthwise across the progress bar.
152
g2.setColor(MetalLookAndFeel.getControlShadow());
153             g2.drawLine(startX, startY, endX, startY);
154             g2.drawLine(startX, startY, startX, endY);
155
156             // Draw darker lengthwise line over filled area.
157
g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
158             g2.drawLine(box.x, startY, box.x + box.width - 1, startY);
159
160         } else { // VERTICAL
161
// Draw light line lengthwise across the progress bar.
162
g2.setColor(MetalLookAndFeel.getControlShadow());
163             g2.drawLine(startX, startY, startX, endY);
164             g2.drawLine(startX, startY, endX, startY);
165
166             // Draw darker lengthwise line over filled area.
167
g2.setColor(MetalLookAndFeel.getPrimaryControlDarkShadow());
168             g2.drawLine(startX, box.y, startX, box.y + box.height - 1);
169         }
170     }
171 }
172
Popular Tags