2
|
1 |
/*
|
|
2 |
* Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.swing.plaf.synth;
|
|
27 |
|
|
28 |
import java.awt.*;
|
|
29 |
import java.awt.geom.AffineTransform;
|
|
30 |
import java.awt.event.*;
|
|
31 |
import javax.swing.*;
|
|
32 |
import javax.swing.event.*;
|
|
33 |
import javax.swing.plaf.*;
|
|
34 |
import javax.swing.plaf.basic.BasicProgressBarUI;
|
|
35 |
import java.beans.PropertyChangeListener;
|
|
36 |
import java.beans.PropertyChangeEvent;
|
|
37 |
import java.io.Serializable;
|
|
38 |
import sun.swing.plaf.synth.SynthUI;
|
|
39 |
import sun.swing.SwingUtilities2;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Synth's ProgressBarUI.
|
|
43 |
*
|
|
44 |
* @author Joshua Outwater
|
|
45 |
*/
|
|
46 |
class SynthProgressBarUI extends BasicProgressBarUI implements SynthUI,
|
|
47 |
PropertyChangeListener {
|
|
48 |
private SynthStyle style;
|
|
49 |
|
|
50 |
private int progressPadding;
|
|
51 |
private boolean paintOutsideClip;
|
|
52 |
|
|
53 |
public static ComponentUI createUI(JComponent x) {
|
|
54 |
return new SynthProgressBarUI();
|
|
55 |
}
|
|
56 |
|
|
57 |
protected void installListeners() {
|
|
58 |
super.installListeners();
|
|
59 |
progressBar.addPropertyChangeListener(this);
|
|
60 |
}
|
|
61 |
|
|
62 |
protected void uninstallListeners() {
|
|
63 |
super.uninstallListeners();
|
|
64 |
progressBar.removePropertyChangeListener(this);
|
|
65 |
}
|
|
66 |
|
|
67 |
protected void installDefaults() {
|
|
68 |
updateStyle(progressBar);
|
|
69 |
}
|
|
70 |
|
|
71 |
private void updateStyle(JProgressBar c) {
|
|
72 |
SynthContext context = getContext(c, ENABLED);
|
|
73 |
SynthStyle oldStyle = style;
|
|
74 |
style = SynthLookAndFeel.updateStyle(context, this);
|
|
75 |
if (style != oldStyle) {
|
|
76 |
setCellLength(style.getInt(context, "ProgressBar.cellLength", 1));
|
|
77 |
setCellSpacing(style.getInt(context, "ProgressBar.cellSpacing", 0));
|
|
78 |
progressPadding = style.getInt(context,
|
|
79 |
"ProgressBar.progressPadding", 0);
|
|
80 |
paintOutsideClip = style.getBoolean(context,
|
|
81 |
"ProgressBar.paintOutsideClip", false);
|
|
82 |
}
|
|
83 |
context.dispose();
|
|
84 |
}
|
|
85 |
|
|
86 |
protected void uninstallDefaults() {
|
|
87 |
SynthContext context = getContext(progressBar, ENABLED);
|
|
88 |
|
|
89 |
style.uninstallDefaults(context);
|
|
90 |
context.dispose();
|
|
91 |
style = null;
|
|
92 |
}
|
|
93 |
|
|
94 |
public SynthContext getContext(JComponent c) {
|
|
95 |
return getContext(c, getComponentState(c));
|
|
96 |
}
|
|
97 |
|
|
98 |
private SynthContext getContext(JComponent c, int state) {
|
|
99 |
return SynthContext.getContext(SynthContext.class, c,
|
|
100 |
SynthLookAndFeel.getRegion(c), style, state);
|
|
101 |
}
|
|
102 |
|
|
103 |
private Region getRegion(JComponent c) {
|
|
104 |
return SynthLookAndFeel.getRegion(c);
|
|
105 |
}
|
|
106 |
|
|
107 |
private int getComponentState(JComponent c) {
|
|
108 |
return SynthLookAndFeel.getComponentState(c);
|
|
109 |
}
|
|
110 |
|
|
111 |
public int getBaseline(JComponent c, int width, int height) {
|
|
112 |
super.getBaseline(c, width, height);
|
|
113 |
if (progressBar.isStringPainted() &&
|
|
114 |
progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
|
|
115 |
SynthContext context = getContext(c);
|
|
116 |
Font font = context.getStyle().getFont(context);
|
|
117 |
FontMetrics metrics = progressBar.getFontMetrics(font);
|
|
118 |
context.dispose();
|
|
119 |
return (height - metrics.getAscent() - metrics.getDescent()) / 2 +
|
|
120 |
metrics.getAscent();
|
|
121 |
}
|
|
122 |
return -1;
|
|
123 |
}
|
|
124 |
|
|
125 |
protected void setAnimationIndex(int newValue) {
|
|
126 |
if (paintOutsideClip) {
|
|
127 |
if (getAnimationIndex() == newValue) {
|
|
128 |
return;
|
|
129 |
}
|
|
130 |
super.setAnimationIndex(newValue);
|
|
131 |
progressBar.repaint();
|
|
132 |
} else {
|
|
133 |
super.setAnimationIndex(newValue);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
public void update(Graphics g, JComponent c) {
|
|
138 |
SynthContext context = getContext(c);
|
|
139 |
|
|
140 |
SynthLookAndFeel.update(context, g);
|
|
141 |
context.getPainter().paintProgressBarBackground(context,
|
|
142 |
g, 0, 0, c.getWidth(), c.getHeight(),
|
|
143 |
progressBar.getOrientation());
|
|
144 |
paint(context, g);
|
|
145 |
context.dispose();
|
|
146 |
}
|
|
147 |
|
|
148 |
public void paint(Graphics g, JComponent c) {
|
|
149 |
SynthContext context = getContext(c);
|
|
150 |
|
|
151 |
paint(context, g);
|
|
152 |
context.dispose();
|
|
153 |
}
|
|
154 |
|
|
155 |
protected void paint(SynthContext context, Graphics g) {
|
|
156 |
JProgressBar pBar = (JProgressBar)context.getComponent();
|
|
157 |
int x = 0, y = 0, width = 0, height = 0;
|
|
158 |
if (!pBar.isIndeterminate()) {
|
|
159 |
Insets pBarInsets = pBar.getInsets();
|
|
160 |
double percentComplete = pBar.getPercentComplete();
|
|
161 |
if (percentComplete != 0.0) {
|
|
162 |
if (pBar.getOrientation() == JProgressBar.HORIZONTAL) {
|
|
163 |
x = pBarInsets.left + progressPadding;
|
|
164 |
y = pBarInsets.top + progressPadding;
|
|
165 |
width = (int)(percentComplete * (pBar.getWidth()
|
|
166 |
- (pBarInsets.left + progressPadding
|
|
167 |
+ pBarInsets.right + progressPadding)));
|
|
168 |
height = pBar.getHeight()
|
|
169 |
- (pBarInsets.top + progressPadding
|
|
170 |
+ pBarInsets.bottom + progressPadding);
|
|
171 |
|
|
172 |
if (!SynthLookAndFeel.isLeftToRight(pBar)) {
|
|
173 |
x = pBar.getWidth() - pBarInsets.right - width
|
|
174 |
- progressPadding;
|
|
175 |
}
|
|
176 |
} else { // JProgressBar.VERTICAL
|
|
177 |
x = pBarInsets.left + progressPadding;
|
|
178 |
width = pBar.getWidth()
|
|
179 |
- (pBarInsets.left + progressPadding
|
|
180 |
+ pBarInsets.right + progressPadding);
|
|
181 |
height = (int)(percentComplete * (pBar.getHeight()
|
|
182 |
- (pBarInsets.top + progressPadding
|
|
183 |
+ pBarInsets.bottom + progressPadding)));
|
|
184 |
y = pBar.getHeight() - pBarInsets.bottom - height
|
|
185 |
- progressPadding;
|
|
186 |
|
|
187 |
// When the progress bar is vertical we always paint
|
|
188 |
// from bottom to top, not matter what the component
|
|
189 |
// orientation is.
|
|
190 |
}
|
|
191 |
}
|
|
192 |
} else {
|
|
193 |
boxRect = getBox(boxRect);
|
|
194 |
x = boxRect.x + progressPadding;
|
|
195 |
y = boxRect.y + progressPadding;
|
|
196 |
width = boxRect.width - progressPadding - progressPadding;
|
|
197 |
height = boxRect.height - progressPadding - progressPadding;
|
|
198 |
}
|
|
199 |
context.getPainter().paintProgressBarForeground(context, g,
|
|
200 |
x, y, width, height, pBar.getOrientation());
|
|
201 |
|
|
202 |
if (pBar.isStringPainted() && !pBar.isIndeterminate()) {
|
|
203 |
paintText(context, g, pBar.getString());
|
|
204 |
}
|
|
205 |
}
|
|
206 |
|
|
207 |
protected void paintText(SynthContext context, Graphics g,
|
|
208 |
String title) {
|
|
209 |
Font font = context.getStyle().getFont(context);
|
|
210 |
FontMetrics metrics = SwingUtilities2.getFontMetrics(progressBar, g,
|
|
211 |
font);
|
|
212 |
|
|
213 |
if (progressBar.isStringPainted()) {
|
|
214 |
String pBarString = progressBar.getString();
|
|
215 |
Rectangle bounds = progressBar.getBounds();
|
|
216 |
int strLength = context.getStyle().getGraphicsUtils(context).
|
|
217 |
computeStringWidth(context, font, metrics, pBarString);
|
|
218 |
|
|
219 |
// Calculate the bounds for the text.
|
|
220 |
Rectangle textRect = new Rectangle(
|
|
221 |
(bounds.width / 2) - (strLength / 2),
|
|
222 |
(bounds.height -
|
|
223 |
(metrics.getAscent() + metrics.getDescent())) / 2,
|
|
224 |
0, 0);
|
|
225 |
|
|
226 |
// Progress bar isn't tall enough for the font. Don't paint it.
|
|
227 |
if (textRect.y < 0) {
|
|
228 |
return;
|
|
229 |
}
|
|
230 |
|
|
231 |
// Paint the text.
|
|
232 |
SynthStyle style = context.getStyle();
|
|
233 |
g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
|
|
234 |
g.setFont(style.getFont(context));
|
|
235 |
style.getGraphicsUtils(context).paintText(context, g, title,
|
|
236 |
textRect.x, textRect.y, -1);
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
public void paintBorder(SynthContext context, Graphics g, int x,
|
|
241 |
int y, int w, int h) {
|
|
242 |
context.getPainter().paintProgressBarBorder(context, g, x, y, w, h,
|
|
243 |
progressBar.getOrientation());
|
|
244 |
}
|
|
245 |
|
|
246 |
public void propertyChange(PropertyChangeEvent e) {
|
|
247 |
if (SynthLookAndFeel.shouldUpdateStyle(e)) {
|
|
248 |
updateStyle((JProgressBar)e.getSource());
|
|
249 |
}
|
|
250 |
}
|
|
251 |
|
|
252 |
public Dimension getPreferredSize(JComponent c) {
|
|
253 |
Dimension size;
|
|
254 |
Insets border = progressBar.getInsets();
|
|
255 |
FontMetrics fontSizer = progressBar.getFontMetrics(
|
|
256 |
progressBar.getFont());
|
|
257 |
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
|
|
258 |
size = new Dimension(getPreferredInnerHorizontal());
|
|
259 |
} else {
|
|
260 |
size = new Dimension(getPreferredInnerVertical());
|
|
261 |
}
|
|
262 |
// Ensure that the progress string will fit.
|
|
263 |
if (progressBar.isStringPainted()) {
|
|
264 |
String progString = progressBar.getString();
|
|
265 |
int stringHeight = fontSizer.getHeight() +
|
|
266 |
fontSizer.getDescent();
|
|
267 |
if (stringHeight > size.height) {
|
|
268 |
size.height = stringHeight;
|
|
269 |
}
|
|
270 |
// This is also for completeness.
|
|
271 |
int stringWidth = SwingUtilities2.stringWidth(
|
|
272 |
progressBar, fontSizer, progString);
|
|
273 |
if (stringWidth > size.width) {
|
|
274 |
size.width = stringWidth;
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
size.width += border.left + border.right;
|
|
279 |
size.height += border.top + border.bottom;
|
|
280 |
|
|
281 |
return size;
|
|
282 |
}
|
|
283 |
}
|