KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsProgressBarUI


1 /*
2  * @(#)WindowsProgressBarUI.java 1.28 06/12/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import javax.swing.plaf.basic.*;
11 import javax.swing.plaf.*;
12 import javax.swing.*;
13 import java.awt.*;
14
15 import com.sun.java.swing.plaf.windows.TMSchema.*;
16 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
17
18 /**
19  * Windows rendition of the component.
20  * <p>
21  * <strong>Warning:</strong>
22  * Serialized objects of this class will not be compatible with
23  * future Swing releases. The current serialization support is appropriate
24  * for short term storage or RMI between applications running the same
25  * version of Swing. A future release of Swing will provide support for
26  * long term persistence.
27  *
28  * @version 1.28 12/21/06
29  * @author Michael C. Albers
30  */

31 public class WindowsProgressBarUI extends BasicProgressBarUI
32 {
33
34     private Rectangle boxRect;
35
36     public static ComponentUI createUI(JComponent x) {
37     return new WindowsProgressBarUI();
38     }
39
40
41     protected void installDefaults() {
42     super.installDefaults();
43
44     if (XPStyle.getXP() != null) {
45         LookAndFeel.installProperty(progressBar, "opaque", Boolean.FALSE);
46         progressBar.setBorder(null);
47     }
48     }
49
50     protected Dimension getPreferredInnerHorizontal() {
51     XPStyle xp = XPStyle.getXP();
52     if (xp != null) {
53             Dimension dim = xp.getDimension(progressBar, Part.PP_BAR, null,
54                 Prop.NORMALSIZE);
55             if (dim == null) {
56                 // if dim null, it's likely that the NORMALSIZE property is missing
57
// from the current XP theme. This will synthesize a replacement
58
// dimension that's close to the real thing.
59
XPStyle.Skin skin = xp.getSkin(progressBar, Part.PP_BAR);
60                 dim = new Dimension(super.getPreferredInnerHorizontal().width,
61                                     skin.getHeight());
62             }
63             return dim;
64     } else {
65         return super.getPreferredInnerHorizontal();
66     }
67     }
68     
69     protected Dimension getPreferredInnerVertical() {
70         XPStyle xp = XPStyle.getXP();
71         if (xp != null) {
72             Dimension dim = xp.getDimension(progressBar, Part.PP_BARVERT, null,
73                 Prop.NORMALSIZE);
74             if (dim == null) {
75                 // if dim null, it's likely that the NORMALSIZE property is missing
76
// from the current XP theme. This will synthesize a replacement
77
// dimension that's close to the real thing.
78
XPStyle.Skin skin = xp.getSkin(progressBar, Part.PP_BARVERT);
79                 dim = new Dimension(skin.getWidth(),
80                                     super.getPreferredInnerVertical().height);
81             }
82             return dim;
83     } else {
84         return super.getPreferredInnerVertical();
85     }
86     }
87     
88     protected void paintDeterminate(Graphics g, JComponent c) {
89     XPStyle xp = XPStyle.getXP();
90     if (xp != null) {
91         boolean vertical = (progressBar.getOrientation() == JProgressBar.VERTICAL);
92         boolean isLeftToRight = WindowsGraphicsUtils.isLeftToRight(c);
93         int barRectWidth = progressBar.getWidth();
94         int barRectHeight = progressBar.getHeight()-1;
95         // amount of progress to draw
96
int amountFull = getAmountFull(null, barRectWidth, barRectHeight);
97
98         paintXPBackground(g, vertical, barRectWidth, barRectHeight);
99         // Paint progress
100
if (progressBar.isStringPainted()) {
101         // Do not paint the standard stripes from the skin, because they obscure
102
// the text
103
g.setColor(progressBar.getForeground());
104         barRectHeight -= 2;
105         barRectWidth -= 2;
106         Graphics2D g2 = (Graphics2D)g;
107         g2.setStroke(new BasicStroke((float)(vertical ? barRectWidth : barRectHeight),
108                          BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
109         if (!vertical) {
110             if (isLeftToRight) {
111             g2.drawLine(2, barRectHeight / 2 + 1,
112                     amountFull - 2, barRectHeight / 2 + 1);
113             } else {
114             g2.drawLine(2 + barRectWidth,
115                     barRectHeight / 2 + 1,
116                     2 + barRectWidth - (amountFull - 2),
117                     barRectHeight / 2 + 1);
118             }
119             paintString(g, 0, 0, barRectWidth, barRectHeight, amountFull, null);
120         } else {
121             g2.drawLine(barRectWidth/2 + 1, barRectHeight + 1,
122                 barRectWidth/2 + 1, barRectHeight + 1 - amountFull + 2);
123             paintString(g, 2, 2, barRectWidth, barRectHeight, amountFull, null);
124         }
125
126         } else {
127                 Skin skin =
128                     xp.getSkin(progressBar, vertical ? Part.PP_CHUNKVERT : Part.PP_CHUNK);
129         int thickness;
130         if (vertical) {
131             thickness = barRectWidth - 5;
132         } else {
133             thickness = barRectHeight - 5;
134         }
135
136                 int chunkSize = xp.getInt(progressBar, Part.PP_PROGRESS, null,
137                     Prop.PROGRESSCHUNKSIZE, 2);
138                 int spaceSize = xp.getInt(progressBar, Part.PP_PROGRESS, null,
139                     Prop.PROGRESSSPACESIZE, 0);
140         int nChunks = (amountFull-4) / (chunkSize + spaceSize);
141
142         // See if we can squeeze in an extra chunk without spacing after
143
if (spaceSize > 0 && (nChunks * (chunkSize + spaceSize) + chunkSize) < (amountFull-4)) {
144             nChunks++;
145         }
146
147         for (int i = 0; i < nChunks; i++) {
148             if (vertical) {
149             skin.paintSkin(g,
150                        3, barRectHeight - i * (chunkSize + spaceSize) - chunkSize - 2,
151                                        thickness, chunkSize, null);
152             } else {
153             if (isLeftToRight) {
154                 skin.paintSkin(g,
155                        4 + i * (chunkSize + spaceSize), 2,
156                                            chunkSize, thickness, null);
157             } else {
158                 skin.paintSkin(g,
159                        barRectWidth - (2 + (i+1) * (chunkSize + spaceSize)), 2,
160                                            chunkSize, thickness, null);
161             }
162             }
163         }
164         }
165     } else {
166         super.paintDeterminate(g, c);
167     }
168     }
169
170
171     protected void paintIndeterminate(Graphics g, JComponent c) {
172     XPStyle xp = XPStyle.getXP();
173     if (xp != null) {
174         boolean vertical = (progressBar.getOrientation() == JProgressBar.VERTICAL);
175         int barRectWidth = progressBar.getWidth();
176         int barRectHeight = progressBar.getHeight()-1;
177         paintXPBackground(g, vertical, barRectWidth, barRectHeight);
178
179         // Paint the bouncing box.
180
boxRect = getBox(boxRect);
181         if (boxRect != null) {
182         g.setColor(progressBar.getForeground());
183         if (!vertical) {
184             g.fillRect(boxRect.x, boxRect.y + 2, boxRect.width, boxRect.height - 4);
185         } else {
186             g.fillRect(boxRect.x + 2, boxRect.y, boxRect.width - 3, boxRect.height);
187         }
188         if (progressBar.isStringPainted()) {
189             if (!vertical) {
190             paintString(g, -1, -1, barRectWidth, barRectHeight, 0, null);
191             } else {
192             paintString(g, 1, 1, barRectWidth, barRectHeight, 0, null);
193             }
194         }
195         }
196     } else {
197         super.paintIndeterminate(g, c);
198     }
199     }
200
201     private void paintXPBackground(Graphics g, boolean vertical,
202                    int barRectWidth, int barRectHeight) {
203     XPStyle xp = XPStyle.getXP();
204         Part part = vertical ? Part.PP_BARVERT : Part.PP_BAR;
205         Skin skin = xp.getSkin(progressBar, part);
206
207     // Paint background
208
skin.paintSkin(g, 0, 0, barRectWidth, barRectHeight, null);
209     }
210 }
211
212
Popular Tags