1 |
14 |
jlechner |
/* BasicProgressBarUI.java --
|
2 |
|
|
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package javax.swing.plaf.basic;
|
40 |
|
|
|
41 |
|
|
import java.awt.Color;
|
42 |
|
|
import java.awt.Dimension;
|
43 |
|
|
import java.awt.Font;
|
44 |
|
|
import java.awt.FontMetrics;
|
45 |
|
|
import java.awt.Graphics;
|
46 |
|
|
import java.awt.Insets;
|
47 |
|
|
import java.awt.Point;
|
48 |
|
|
import java.awt.Rectangle;
|
49 |
|
|
import java.awt.Shape;
|
50 |
|
|
import java.awt.event.ActionEvent;
|
51 |
|
|
import java.awt.event.ActionListener;
|
52 |
|
|
import java.awt.event.ComponentAdapter;
|
53 |
|
|
import java.awt.event.ComponentEvent;
|
54 |
|
|
import java.awt.event.ComponentListener;
|
55 |
|
|
import java.beans.PropertyChangeEvent;
|
56 |
|
|
import java.beans.PropertyChangeListener;
|
57 |
|
|
|
58 |
|
|
import javax.swing.JComponent;
|
59 |
|
|
import javax.swing.JProgressBar;
|
60 |
|
|
import javax.swing.LookAndFeel;
|
61 |
|
|
import javax.swing.SwingConstants;
|
62 |
|
|
import javax.swing.SwingUtilities;
|
63 |
|
|
import javax.swing.Timer;
|
64 |
|
|
import javax.swing.UIManager;
|
65 |
|
|
import javax.swing.event.AncestorEvent;
|
66 |
|
|
import javax.swing.event.AncestorListener;
|
67 |
|
|
import javax.swing.event.ChangeEvent;
|
68 |
|
|
import javax.swing.event.ChangeListener;
|
69 |
|
|
import javax.swing.plaf.ComponentUI;
|
70 |
|
|
import javax.swing.plaf.ProgressBarUI;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* The Basic Look and Feel UI delegate for the
|
74 |
|
|
* JProgressBar.
|
75 |
|
|
*/
|
76 |
|
|
public class BasicProgressBarUI extends ProgressBarUI
|
77 |
|
|
{
|
78 |
|
|
/**
|
79 |
|
|
* A helper class that listens for ChangeEvents
|
80 |
|
|
* from the progressBar's model.
|
81 |
|
|
*
|
82 |
|
|
* @specnote Apparently this class was intended to be protected,
|
83 |
|
|
* but was made public by a compiler bug and is now
|
84 |
|
|
* public for compatibility.
|
85 |
|
|
*/
|
86 |
|
|
public class ChangeHandler implements ChangeListener
|
87 |
|
|
{
|
88 |
|
|
/**
|
89 |
|
|
* Called every time the state of the model changes.
|
90 |
|
|
*
|
91 |
|
|
* @param e The ChangeEvent given by the model.
|
92 |
|
|
*/
|
93 |
|
|
public void stateChanged(ChangeEvent e)
|
94 |
|
|
{
|
95 |
|
|
// Nothing to do but repaint.
|
96 |
|
|
progressBar.repaint();
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* This helper class is used to listen for
|
102 |
|
|
* PropertyChangeEvents from the progressBar.
|
103 |
|
|
*/
|
104 |
|
|
private class PropertyChangeHandler implements PropertyChangeListener
|
105 |
|
|
{
|
106 |
|
|
/**
|
107 |
|
|
* Called every time the properties of the
|
108 |
|
|
* progressBar change.
|
109 |
|
|
*
|
110 |
|
|
* @param e The PropertyChangeEvent given by the progressBar.
|
111 |
|
|
*/
|
112 |
|
|
public void propertyChange(PropertyChangeEvent e)
|
113 |
|
|
{
|
114 |
|
|
// Only need to listen for indeterminate changes.
|
115 |
|
|
// All other things are done on a repaint.
|
116 |
|
|
if (e.getPropertyName().equals("indeterminate"))
|
117 |
|
|
if (((Boolean) e.getNewValue()).booleanValue()
|
118 |
|
|
&& progressBar.isShowing())
|
119 |
|
|
startAnimationTimer();
|
120 |
|
|
else
|
121 |
|
|
stopAnimationTimer();
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Receives notification when the progressbar is becoming visible or
|
127 |
|
|
* invisible and starts/stops the animation timer accordingly.
|
128 |
|
|
*
|
129 |
|
|
* @author Roman Kennke (kennke@aicas.com)
|
130 |
|
|
*/
|
131 |
|
|
private class AncestorHandler implements AncestorListener
|
132 |
|
|
{
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Receives notification when the progressbar is becoming visible. This
|
136 |
|
|
* starts the animation timer if the progressbar is indeterminate.
|
137 |
|
|
*
|
138 |
|
|
* @param event the ancestor event
|
139 |
|
|
*/
|
140 |
|
|
public void ancestorAdded(AncestorEvent event)
|
141 |
|
|
{
|
142 |
|
|
if (progressBar.isIndeterminate())
|
143 |
|
|
startAnimationTimer();
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/**
|
147 |
|
|
* Receives notification when the progressbar is becoming invisible. This
|
148 |
|
|
* stops the animation timer if the progressbar is indeterminate.
|
149 |
|
|
*
|
150 |
|
|
* @param event the ancestor event
|
151 |
|
|
*/
|
152 |
|
|
public void ancestorRemoved(AncestorEvent event)
|
153 |
|
|
{
|
154 |
|
|
stopAnimationTimer();
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
/**
|
158 |
|
|
* Receives notification when an ancestor has been moved. We don't need to
|
159 |
|
|
* do anything here.
|
160 |
|
|
*/
|
161 |
|
|
public void ancestorMoved(AncestorEvent event)
|
162 |
|
|
{
|
163 |
|
|
// Nothing to do here.
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/**
|
169 |
|
|
* This helper class is used to listen for
|
170 |
|
|
* the animationTimer's intervals. On every interval,
|
171 |
|
|
* the bouncing box should move.
|
172 |
|
|
*/
|
173 |
|
|
private class Animator implements ActionListener
|
174 |
|
|
{
|
175 |
|
|
/**
|
176 |
|
|
* Called every time the animationTimer reaches
|
177 |
|
|
* its interval.
|
178 |
|
|
*
|
179 |
|
|
* @param e The ActionEvent given by the timer.
|
180 |
|
|
*/
|
181 |
|
|
public void actionPerformed(ActionEvent e)
|
182 |
|
|
{
|
183 |
|
|
// Incrementing the animation index will cause
|
184 |
|
|
// a repaint.
|
185 |
|
|
incrementAnimationIndex();
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* Receives notification when the size of the progress bar changes and
|
191 |
|
|
* invalidates the layout information for the box calculation in
|
192 |
|
|
* {@link BasicProgressBarUI#getBox(Rectangle)}.
|
193 |
|
|
*
|
194 |
|
|
* @author Roman Kennke (kennke@aicas.com)
|
195 |
|
|
*/
|
196 |
|
|
private class ComponentHandler extends ComponentAdapter
|
197 |
|
|
{
|
198 |
|
|
/**
|
199 |
|
|
* Receives notification when the size of the progress bar changes and
|
200 |
|
|
* invalidates the layout information for the box calculation in
|
201 |
|
|
* {@link BasicProgressBarUI#getBox}.
|
202 |
|
|
*
|
203 |
|
|
* @param e the component event
|
204 |
|
|
*/
|
205 |
|
|
public void componentResized(ComponentEvent e)
|
206 |
|
|
{
|
207 |
|
|
boxDependent = -1;
|
208 |
|
|
boxIndependent = -1;
|
209 |
|
|
incr = -1;
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
/**
|
214 |
|
|
* Holds the value of the bouncing box that is returned by {@link #getBox}.
|
215 |
|
|
*
|
216 |
|
|
* @since 1.5
|
217 |
|
|
*/
|
218 |
|
|
protected Rectangle boxRect;
|
219 |
|
|
|
220 |
|
|
/** The timer used to move the bouncing box. */
|
221 |
|
|
private transient Timer animationTimer;
|
222 |
|
|
|
223 |
|
|
// The total number of frames must be an even number.
|
224 |
|
|
// The total number of frames is calculated from
|
225 |
|
|
// the cycleTime and repaintInterval given by
|
226 |
|
|
// the basic Look and Feel defaults.
|
227 |
|
|
//
|
228 |
|
|
// +-----------------------------------------------+
|
229 |
|
|
// | frame0 | frame1 | frame2 | frame 3 | frame 4 |
|
230 |
|
|
// | | frame7 | frame6 | frame 5 | |
|
231 |
|
|
// +-----------------------------------------------+
|
232 |
|
|
|
233 |
|
|
/** The current animation index. */
|
234 |
|
|
private transient int animationIndex;
|
235 |
|
|
|
236 |
|
|
/** The total number of frames.*/
|
237 |
|
|
private transient int numFrames;
|
238 |
|
|
|
239 |
|
|
/** The helper that moves the bouncing box. */
|
240 |
|
|
private transient Animator animation;
|
241 |
|
|
|
242 |
|
|
/** The helper that listens for property change events. */
|
243 |
|
|
private transient PropertyChangeHandler propertyListener;
|
244 |
|
|
|
245 |
|
|
/** The Listener for the model. */
|
246 |
|
|
protected ChangeListener changeListener;
|
247 |
|
|
|
248 |
|
|
/** The progressBar for this UI. */
|
249 |
|
|
protected JProgressBar progressBar;
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
/**
|
253 |
|
|
* The size of the box returned by {@link #getBox} in the orientation
|
254 |
|
|
* direction of the progress bar. This is package private to avoid accessor
|
255 |
|
|
* method.
|
256 |
|
|
*/
|
257 |
|
|
transient double boxDependent = - 1;
|
258 |
|
|
|
259 |
|
|
/**
|
260 |
|
|
* The size of the box returned by {@link #getBox} against the orientation
|
261 |
|
|
* direction of the progress bar. This is package private to avoid accessor
|
262 |
|
|
* method.
|
263 |
|
|
*/
|
264 |
|
|
transient int boxIndependent = - 1;
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* The increment for box animation. This is package private to avoid accessor
|
268 |
|
|
* method.
|
269 |
|
|
*/
|
270 |
|
|
transient double incr = -1;
|
271 |
|
|
|
272 |
|
|
/** The length of the cell. The cell is the painted part. */
|
273 |
|
|
private transient int cellLength;
|
274 |
|
|
|
275 |
|
|
/** The gap between cells. */
|
276 |
|
|
private transient int cellSpacing;
|
277 |
|
|
|
278 |
|
|
/** The color of the text when the bar is not over it.*/
|
279 |
|
|
private transient Color selectionBackground;
|
280 |
|
|
|
281 |
|
|
/** The color of the text when the bar is over it. */
|
282 |
|
|
private transient Color selectionForeground;
|
283 |
|
|
|
284 |
|
|
/**
|
285 |
|
|
* Listens for notification when the component becomes showing and
|
286 |
|
|
* starts/stops the animation timer.
|
287 |
|
|
*/
|
288 |
|
|
private AncestorListener ancestorListener;
|
289 |
|
|
|
290 |
|
|
/**
|
291 |
|
|
* Listens for resize events on the progress bar and invalidates some
|
292 |
|
|
* layout info.
|
293 |
|
|
*/
|
294 |
|
|
private ComponentListener componentListener;
|
295 |
|
|
|
296 |
|
|
/**
|
297 |
|
|
* Creates a new BasicProgressBarUI object.
|
298 |
|
|
*/
|
299 |
|
|
public BasicProgressBarUI()
|
300 |
|
|
{
|
301 |
|
|
super();
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/**
|
305 |
|
|
* Creates a new BasicProgressBarUI for the component.
|
306 |
|
|
*
|
307 |
|
|
* @param x The JComponent to create the UI for.
|
308 |
|
|
*
|
309 |
|
|
* @return A new BasicProgressBarUI.
|
310 |
|
|
*/
|
311 |
|
|
public static ComponentUI createUI(JComponent x)
|
312 |
|
|
{
|
313 |
|
|
return new BasicProgressBarUI();
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
/**
|
317 |
|
|
* This method returns the length of the bar (from the minimum)
|
318 |
|
|
* in pixels (or units that the Graphics object draws in) based
|
319 |
|
|
* on the progressBar's getPercentComplete() value.
|
320 |
|
|
*
|
321 |
|
|
* @param b The insets of the progressBar.
|
322 |
|
|
* @param width The width of the progressBar.
|
323 |
|
|
* @param height The height of the progressBar.
|
324 |
|
|
*
|
325 |
|
|
* @return The length of the bar that should be painted in pixels.
|
326 |
|
|
*/
|
327 |
|
|
protected int getAmountFull(Insets b, int width, int height)
|
328 |
|
|
{
|
329 |
|
|
double percentDone = progressBar.getPercentComplete();
|
330 |
|
|
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
|
331 |
|
|
return (int) (percentDone * (width - b.left - b.right));
|
332 |
|
|
else
|
333 |
|
|
return (int) (percentDone * (height - b.top - b.bottom));
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
/**
|
337 |
|
|
* The current animation index.
|
338 |
|
|
*
|
339 |
|
|
* @return The current animation index.
|
340 |
|
|
*/
|
341 |
|
|
protected int getAnimationIndex()
|
342 |
|
|
{
|
343 |
|
|
return animationIndex;
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
/**
|
347 |
|
|
* This method returns the size and position of the bouncing box
|
348 |
|
|
* for the current animation index. It stores the values in the
|
349 |
|
|
* given rectangle and returns it. It returns null if no box should
|
350 |
|
|
* be drawn.
|
351 |
|
|
*
|
352 |
|
|
* @param r The bouncing box rectangle.
|
353 |
|
|
*
|
354 |
|
|
* @return The bouncing box rectangle.
|
355 |
|
|
*/
|
356 |
|
|
protected Rectangle getBox(Rectangle r)
|
357 |
|
|
{
|
358 |
|
|
if (!progressBar.isIndeterminate())
|
359 |
|
|
return null;
|
360 |
|
|
if (r == null)
|
361 |
|
|
r = new Rectangle();
|
362 |
|
|
|
363 |
|
|
Rectangle vr = new Rectangle();
|
364 |
|
|
SwingUtilities.calculateInnerArea(progressBar, vr);
|
365 |
|
|
|
366 |
|
|
// Recalculate the metrics only when size of the progressbar has changed.
|
367 |
|
|
if (incr == -1 || boxDependent == -1 || boxIndependent == -1)
|
368 |
|
|
{
|
369 |
|
|
//numFrames has to be an even number as defined by spec.
|
370 |
|
|
int iterations = numFrames / 2;
|
371 |
|
|
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
|
372 |
|
|
{
|
373 |
|
|
boxDependent = vr.width / 6.;
|
374 |
|
|
incr = ((double) (vr.width - boxDependent)) / (double) iterations;
|
375 |
|
|
boxIndependent = vr.height;
|
376 |
|
|
}
|
377 |
|
|
else
|
378 |
|
|
{
|
379 |
|
|
boxDependent = vr.height / 6.;
|
380 |
|
|
incr = ((double) (vr.height - boxDependent)) / (double) iterations;
|
381 |
|
|
boxIndependent = vr.width;
|
382 |
|
|
}
|
383 |
|
|
}
|
384 |
|
|
|
385 |
|
|
int index = getAnimationIndex();
|
386 |
|
|
if (animationIndex > (numFrames) / 2)
|
387 |
|
|
index = numFrames - getAnimationIndex();
|
388 |
|
|
|
389 |
|
|
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
|
390 |
|
|
{
|
391 |
|
|
r.x = vr.x + (int) (incr * index);
|
392 |
|
|
r.y = vr.y;
|
393 |
|
|
r.width = (int) boxDependent;
|
394 |
|
|
r.height = (int) boxIndependent;
|
395 |
|
|
}
|
396 |
|
|
else
|
397 |
|
|
{
|
398 |
|
|
r.x = vr.x;
|
399 |
|
|
r.y = vr.height - (int) (incr * index) + vr.y - (int) boxDependent;
|
400 |
|
|
r.width = (int) boxIndependent;
|
401 |
|
|
r.height = (int) boxDependent;
|
402 |
|
|
}
|
403 |
|
|
return r;
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
/**
|
407 |
|
|
* This method returns the length of the cells.
|
408 |
|
|
*
|
409 |
|
|
* @return The cell length.
|
410 |
|
|
*/
|
411 |
|
|
protected int getCellLength()
|
412 |
|
|
{
|
413 |
|
|
return cellLength;
|
414 |
|
|
}
|
415 |
|
|
|
416 |
|
|
/**
|
417 |
|
|
* This method returns the spacing between cells.
|
418 |
|
|
*
|
419 |
|
|
* @return The cell gap.
|
420 |
|
|
*/
|
421 |
|
|
protected int getCellSpacing()
|
422 |
|
|
{
|
423 |
|
|
return cellSpacing;
|
424 |
|
|
}
|
425 |
|
|
|
426 |
|
|
/**
|
427 |
|
|
* This method returns the maximum size of the JComponent.
|
428 |
|
|
* If it returns null, it is up to the LayoutManager
|
429 |
|
|
* to give it a size.
|
430 |
|
|
*
|
431 |
|
|
* @param c The component to find a maximum size for.
|
432 |
|
|
*
|
433 |
|
|
* @return The maximum size.
|
434 |
|
|
*/
|
435 |
|
|
public Dimension getMaximumSize(JComponent c)
|
436 |
|
|
{
|
437 |
|
|
Insets insets = c.getInsets();
|
438 |
|
|
Dimension ret;
|
439 |
|
|
int orientation = progressBar.getOrientation();
|
440 |
|
|
if (orientation == JProgressBar.VERTICAL)
|
441 |
|
|
{
|
442 |
|
|
ret = getPreferredInnerVertical();
|
443 |
|
|
ret.height = Short.MAX_VALUE;
|
444 |
|
|
ret.width += insets.left + insets.right;
|
445 |
|
|
}
|
446 |
|
|
else
|
447 |
|
|
{
|
448 |
|
|
ret = getPreferredInnerHorizontal();
|
449 |
|
|
ret.width = Short.MAX_VALUE;
|
450 |
|
|
ret.height += insets.top + insets.bottom;
|
451 |
|
|
}
|
452 |
|
|
return ret;
|
453 |
|
|
}
|
454 |
|
|
|
455 |
|
|
/**
|
456 |
|
|
* This method returns the minimum size of the JComponent.
|
457 |
|
|
* If it returns null, it is up to the LayoutManager to
|
458 |
|
|
* give it a size.
|
459 |
|
|
*
|
460 |
|
|
* @param c The component to find a minimum size for.
|
461 |
|
|
*
|
462 |
|
|
* @return The minimum size.
|
463 |
|
|
*/
|
464 |
|
|
public Dimension getMinimumSize(JComponent c)
|
465 |
|
|
{
|
466 |
|
|
Insets insets = c.getInsets();
|
467 |
|
|
Dimension ret;
|
468 |
|
|
int orientation = progressBar.getOrientation();
|
469 |
|
|
if (orientation == JProgressBar.VERTICAL)
|
470 |
|
|
{
|
471 |
|
|
ret = getPreferredInnerVertical();
|
472 |
|
|
ret.height = 10;
|
473 |
|
|
ret.width += insets.left + insets.right;
|
474 |
|
|
}
|
475 |
|
|
else
|
476 |
|
|
{
|
477 |
|
|
ret = getPreferredInnerHorizontal();
|
478 |
|
|
ret.width = 10;
|
479 |
|
|
ret.height += insets.top + insets.bottom;
|
480 |
|
|
}
|
481 |
|
|
return ret;
|
482 |
|
|
}
|
483 |
|
|
|
484 |
|
|
/**
|
485 |
|
|
* This method returns the preferred size of the inner
|
486 |
|
|
* rectangle (the bounds without the insets) if the
|
487 |
|
|
* progressBar is horizontal.
|
488 |
|
|
*
|
489 |
|
|
* @return The preferred size of the progressBar minus
|
490 |
|
|
* insets if it's horizontal.
|
491 |
|
|
*/
|
492 |
|
|
protected Dimension getPreferredInnerHorizontal()
|
493 |
|
|
{
|
494 |
|
|
Font font = progressBar.getFont();
|
495 |
|
|
FontMetrics fm = progressBar.getFontMetrics(font);
|
496 |
|
|
|
497 |
|
|
int stringWidth = 0;
|
498 |
|
|
String str = progressBar.getString();
|
499 |
|
|
if (str != null)
|
500 |
|
|
stringWidth = fm.stringWidth(progressBar.getString());
|
501 |
|
|
Insets i = progressBar.getInsets();
|
502 |
|
|
int prefWidth = Math.max(200 - i.left - i.right, stringWidth);
|
503 |
|
|
|
504 |
|
|
int stringHeight = 0;
|
505 |
|
|
if (str != null)
|
506 |
|
|
stringHeight = fm.getHeight();
|
507 |
|
|
int prefHeight = Math.max(16 - i.top - i.bottom, stringHeight);
|
508 |
|
|
|
509 |
|
|
return new Dimension(prefWidth, prefHeight);
|
510 |
|
|
}
|
511 |
|
|
|
512 |
|
|
/**
|
513 |
|
|
* This method returns the preferred size of the inner
|
514 |
|
|
* rectangle (the bounds without insets) if the
|
515 |
|
|
* progressBar is vertical.
|
516 |
|
|
*
|
517 |
|
|
* @return The preferred size of the progressBar minus
|
518 |
|
|
* insets if it's vertical.
|
519 |
|
|
*/
|
520 |
|
|
protected Dimension getPreferredInnerVertical()
|
521 |
|
|
{
|
522 |
|
|
Font font = progressBar.getFont();
|
523 |
|
|
FontMetrics fm = progressBar.getFontMetrics(font);
|
524 |
|
|
|
525 |
|
|
int stringWidth = 0;
|
526 |
|
|
String str = progressBar.getString();
|
527 |
|
|
if (str != null)
|
528 |
|
|
stringWidth = fm.stringWidth(progressBar.getString());
|
529 |
|
|
Insets i = progressBar.getInsets();
|
530 |
|
|
int prefHeight = Math.max(200 - i.left - i.right, stringWidth);
|
531 |
|
|
|
532 |
|
|
int stringHeight = 0;
|
533 |
|
|
if (str != null)
|
534 |
|
|
stringHeight = fm.getHeight();
|
535 |
|
|
int prefWidth = Math.max(16 - i.top - i.bottom, stringHeight);
|
536 |
|
|
|
537 |
|
|
return new Dimension(prefWidth, prefHeight);
|
538 |
|
|
}
|
539 |
|
|
|
540 |
|
|
/**
|
541 |
|
|
* This method returns the preferred size of the
|
542 |
|
|
* given JComponent. If it returns null, then it
|
543 |
|
|
* is up to the LayoutManager to give it a size.
|
544 |
|
|
*
|
545 |
|
|
* @param c The component to find the preferred size for.
|
546 |
|
|
*
|
547 |
|
|
* @return The preferred size of the component.
|
548 |
|
|
*/
|
549 |
|
|
public Dimension getPreferredSize(JComponent c)
|
550 |
|
|
{
|
551 |
|
|
Insets insets = c.getInsets();
|
552 |
|
|
Dimension ret;
|
553 |
|
|
int orientation = progressBar.getOrientation();
|
554 |
|
|
if (orientation == JProgressBar.VERTICAL)
|
555 |
|
|
ret = getPreferredInnerVertical();
|
556 |
|
|
else
|
557 |
|
|
ret = getPreferredInnerHorizontal();
|
558 |
|
|
ret.width += insets.left + insets.right;
|
559 |
|
|
ret.height += insets.top + insets.bottom;
|
560 |
|
|
return ret;
|
561 |
|
|
}
|
562 |
|
|
|
563 |
|
|
/**
|
564 |
|
|
* This method returns the Color that the text is shown in when the bar is
|
565 |
|
|
* not over the text.
|
566 |
|
|
*
|
567 |
|
|
* @return The color of the text when the bar is not over it.
|
568 |
|
|
*/
|
569 |
|
|
protected Color getSelectionBackground()
|
570 |
|
|
{
|
571 |
|
|
return selectionBackground;
|
572 |
|
|
}
|
573 |
|
|
|
574 |
|
|
/**
|
575 |
|
|
* This method returns the Color that the text is shown in when the bar is
|
576 |
|
|
* over the text.
|
577 |
|
|
*
|
578 |
|
|
* @return The color of the text when the bar is over it.
|
579 |
|
|
*/
|
580 |
|
|
protected Color getSelectionForeground()
|
581 |
|
|
{
|
582 |
|
|
return selectionForeground;
|
583 |
|
|
}
|
584 |
|
|
|
585 |
|
|
/**
|
586 |
|
|
* This method returns the point (the top left of the bounding box)
|
587 |
|
|
* where the text should be painted.
|
588 |
|
|
*
|
589 |
|
|
* @param g The Graphics object to measure FontMetrics with.
|
590 |
|
|
* @param progressString The string to paint.
|
591 |
|
|
* @param x The x coordinate of the overall bounds box.
|
592 |
|
|
* @param y The y coordinate of the overall bounds box.
|
593 |
|
|
* @param width The width of the overall bounds box.
|
594 |
|
|
* @param height The height of the overall bounds box.
|
595 |
|
|
*
|
596 |
|
|
* @return The top left of the bounding box where text should be painted.
|
597 |
|
|
*/
|
598 |
|
|
protected Point getStringPlacement(Graphics g, String progressString, int x,
|
599 |
|
|
int y, int width, int height)
|
600 |
|
|
{
|
601 |
|
|
Rectangle tr = new Rectangle();
|
602 |
|
|
Rectangle vr = new Rectangle(x, y, width, height);
|
603 |
|
|
Rectangle ir = new Rectangle();
|
604 |
|
|
|
605 |
|
|
Font f = g.getFont();
|
606 |
|
|
FontMetrics fm = g.getFontMetrics(f);
|
607 |
|
|
|
608 |
|
|
SwingUtilities.layoutCompoundLabel(progressBar, fm, progressString, null,
|
609 |
|
|
SwingConstants.CENTER,
|
610 |
|
|
SwingConstants.CENTER,
|
611 |
|
|
SwingConstants.CENTER,
|
612 |
|
|
SwingConstants.CENTER, vr, ir, tr, 0);
|
613 |
|
|
return new Point(tr.x, tr.y);
|
614 |
|
|
}
|
615 |
|
|
|
616 |
|
|
/**
|
617 |
|
|
* This method increments the animation index.
|
618 |
|
|
*/
|
619 |
|
|
protected void incrementAnimationIndex()
|
620 |
|
|
{
|
621 |
|
|
animationIndex++;
|
622 |
|
|
//numFrames is like string length, it should be named numFrames or something
|
623 |
|
|
if (animationIndex >= numFrames)
|
624 |
|
|
animationIndex = 0;
|
625 |
|
|
progressBar.repaint();
|
626 |
|
|
}
|
627 |
|
|
|
628 |
|
|
/**
|
629 |
|
|
* This method paints the progressBar. It delegates its responsibilities
|
630 |
|
|
* to paintDeterminate and paintIndeterminate.
|
631 |
|
|
*
|
632 |
|
|
* @param g The Graphics object to paint with.
|
633 |
|
|
* @param c The JComponent to paint.
|
634 |
|
|
*/
|
635 |
|
|
public void paint(Graphics g, JComponent c)
|
636 |
|
|
{
|
637 |
|
|
if (! progressBar.isIndeterminate())
|
638 |
|
|
paintDeterminate(g, c);
|
639 |
|
|
else
|
640 |
|
|
paintIndeterminate(g, c);
|
641 |
|
|
}
|
642 |
|
|
|
643 |
|
|
/**
|
644 |
|
|
* This method is called if the painting to be done is
|
645 |
|
|
* for a determinate progressBar.
|
646 |
|
|
*
|
647 |
|
|
* @param g The Graphics object to paint with.
|
648 |
|
|
* @param c The JComponent to paint.
|
649 |
|
|
*/
|
650 |
|
|
protected void paintDeterminate(Graphics g, JComponent c)
|
651 |
|
|
{
|
652 |
|
|
Color saved = g.getColor();
|
653 |
|
|
int space = getCellSpacing();
|
654 |
|
|
int len = getCellLength();
|
655 |
|
|
int max = progressBar.getMaximum();
|
656 |
|
|
int min = progressBar.getMinimum();
|
657 |
|
|
int value = progressBar.getValue();
|
658 |
|
|
|
659 |
|
|
Rectangle vr = SwingUtilities.calculateInnerArea(c, new Rectangle());
|
660 |
|
|
Rectangle or = progressBar.getBounds();
|
661 |
|
|
Insets insets = c.getInsets();
|
662 |
|
|
|
663 |
|
|
int amountFull = getAmountFull(insets, or.width, or.height);
|
664 |
|
|
|
665 |
|
|
if (progressBar.getOrientation() == JProgressBar.HORIZONTAL)
|
666 |
|
|
{
|
667 |
|
|
g.setColor(c.getForeground());
|
668 |
|
|
g.fillRect(vr.x, vr.y, amountFull, vr.height);
|
669 |
|
|
}
|
670 |
|
|
else
|
671 |
|
|
{
|
672 |
|
|
g.setColor(c.getForeground());
|
673 |
|
|
g.fillRect(vr.x, vr.y + vr.height - amountFull, vr.width, amountFull);
|
674 |
|
|
}
|
675 |
|
|
|
676 |
|
|
if (progressBar.isStringPainted() && !progressBar.getString().equals(""))
|
677 |
|
|
paintString(g, 0, 0, or.width, or.height, amountFull, insets);
|
678 |
|
|
g.setColor(saved);
|
679 |
|
|
}
|
680 |
|
|
|
681 |
|
|
/**
|
682 |
|
|
* This method is called if the painting to be done is for
|
683 |
|
|
* an indeterminate progressBar.
|
684 |
|
|
*
|
685 |
|
|
* @param g The Graphics object to paint with.
|
686 |
|
|
* @param c The JComponent to paint.
|
687 |
|
|
*/
|
688 |
|
|
protected void paintIndeterminate(Graphics g, JComponent c)
|
689 |
|
|
{
|
690 |
|
|
//need to paint the box at it's current position. no text is painted since
|
691 |
|
|
//all we're doing is bouncing back and forth
|
692 |
|
|
Color saved = g.getColor();
|
693 |
|
|
Insets insets = c.getInsets();
|
694 |
|
|
|
695 |
|
|
Rectangle or = c.getBounds();
|
696 |
|
|
Rectangle vr = new Rectangle();
|
697 |
|
|
SwingUtilities.calculateInnerArea(c, vr);
|
698 |
|
|
|
699 |
|
|
g.setColor(c.getBackground());
|
700 |
|
|
g.fillRect(vr.x, vr.y, vr.width, vr.height);
|
701 |
|
|
|
702 |
|
|
boxRect = getBox(boxRect);
|
703 |
|
|
|
704 |
|
|
g.setColor(c.getForeground());
|
705 |
|
|
g.fillRect(boxRect.x, boxRect.y, boxRect.width, boxRect.height);
|
706 |
|
|
|
707 |
|
|
if (progressBar.isStringPainted() && !progressBar.getString().equals(""))
|
708 |
|
|
paintString(g, 0, 0, or.width, or.height,
|
709 |
|
|
getAmountFull(insets, or.width, or.height), insets);
|
710 |
|
|
|
711 |
|
|
g.setColor(saved);
|
712 |
|
|
}
|
713 |
|
|
|
714 |
|
|
/**
|
715 |
|
|
* This method paints the string for the progressBar.
|
716 |
|
|
*
|
717 |
|
|
* @param g The Graphics object to paint with.
|
718 |
|
|
* @param x The x coordinate of the progressBar.
|
719 |
|
|
* @param y The y coordinate of the progressBar.
|
720 |
|
|
* @param width The width of the progressBar.
|
721 |
|
|
* @param height The height of the progressBar.
|
722 |
|
|
* @param amountFull The amount of the progressBar that has its bar filled.
|
723 |
|
|
* @param b The insets of the progressBar.
|
724 |
|
|
*/
|
725 |
|
|
protected void paintString(Graphics g, int x, int y, int width, int height,
|
726 |
|
|
int amountFull, Insets b)
|
727 |
|
|
{
|
728 |
|
|
// FIXME: We do not support vertical text painting because Java2D is needed
|
729 |
|
|
// for this.
|
730 |
|
|
if (progressBar.getOrientation() == JProgressBar.VERTICAL)
|
731 |
|
|
return;
|
732 |
|
|
|
733 |
|
|
// We want to place in the exact center of the bar.
|
734 |
|
|
Point placement = getStringPlacement(g, progressBar.getString(),
|
735 |
|
|
x + b.left, y + b.top,
|
736 |
|
|
width - b.left - b.right,
|
737 |
|
|
height - b.top - b.bottom);
|
738 |
|
|
|
739 |
|
|
Color savedColor = g.getColor();
|
740 |
|
|
Shape savedClip = g.getClip();
|
741 |
|
|
FontMetrics fm = g.getFontMetrics(progressBar.getFont());
|
742 |
|
|
int full = getAmountFull(b, width, height);
|
743 |
|
|
String str = progressBar.getString();
|
744 |
|
|
|
745 |
|
|
// We draw this string two times with different clips so that the text
|
746 |
|
|
// over the filled area is painted with selectionForeground and over
|
747 |
|
|
// the clear area with selectionBackground.
|
748 |
|
|
g.setColor(getSelectionForeground());
|
749 |
|
|
g.setClip(0, 0, full + b.left, height);
|
750 |
|
|
g.drawString(str, placement.x, placement.y + fm.getAscent());
|
751 |
|
|
g.setColor(getSelectionBackground());
|
752 |
|
|
g.setClip(full + b.left, 0, width - full, height);
|
753 |
|
|
g.drawString(str, placement.x, placement.y + fm.getAscent());
|
754 |
|
|
g.setClip(savedClip);
|
755 |
|
|
g.setColor(savedColor);
|
756 |
|
|
}
|
757 |
|
|
|
758 |
|
|
/**
|
759 |
|
|
* This method sets the current animation index. If the index
|
760 |
|
|
* is greater than the number of frames, it resets to 0.
|
761 |
|
|
*
|
762 |
|
|
* @param newValue The new animation index.
|
763 |
|
|
*/
|
764 |
|
|
protected void setAnimationIndex(int newValue)
|
765 |
|
|
{
|
766 |
|
|
animationIndex = (newValue <= numFrames) ? newValue : 0;
|
767 |
|
|
progressBar.repaint();
|
768 |
|
|
}
|
769 |
|
|
|
770 |
|
|
/**
|
771 |
|
|
* This method sets the cell length.
|
772 |
|
|
*
|
773 |
|
|
* @param cellLen The cell length.
|
774 |
|
|
*/
|
775 |
|
|
protected void setCellLength(int cellLen)
|
776 |
|
|
{
|
777 |
|
|
cellLength = cellLen;
|
778 |
|
|
}
|
779 |
|
|
|
780 |
|
|
/**
|
781 |
|
|
* This method sets the cell spacing.
|
782 |
|
|
*
|
783 |
|
|
* @param cellSpace The cell spacing.
|
784 |
|
|
*/
|
785 |
|
|
protected void setCellSpacing(int cellSpace)
|
786 |
|
|
{
|
787 |
|
|
cellSpacing = cellSpace;
|
788 |
|
|
}
|
789 |
|
|
|
790 |
|
|
/**
|
791 |
|
|
* This method starts the animation timer. It is called
|
792 |
|
|
* when the propertyChangeListener detects that the progressBar
|
793 |
|
|
* has changed to indeterminate mode.
|
794 |
|
|
*
|
795 |
|
|
* @since 1.4
|
796 |
|
|
*/
|
797 |
|
|
protected void startAnimationTimer()
|
798 |
|
|
{
|
799 |
|
|
if (animationTimer != null)
|
800 |
|
|
animationTimer.start();
|
801 |
|
|
}
|
802 |
|
|
|
803 |
|
|
/**
|
804 |
|
|
* This method stops the animation timer. It is called when
|
805 |
|
|
* the propertyChangeListener detects that the progressBar
|
806 |
|
|
* has changed to determinate mode.
|
807 |
|
|
*
|
808 |
|
|
* @since 1.4
|
809 |
|
|
*/
|
810 |
|
|
protected void stopAnimationTimer()
|
811 |
|
|
{
|
812 |
|
|
if (animationTimer != null)
|
813 |
|
|
animationTimer.stop();
|
814 |
|
|
setAnimationIndex(0);
|
815 |
|
|
}
|
816 |
|
|
|
817 |
|
|
/**
|
818 |
|
|
* This method changes the settings for the progressBar to
|
819 |
|
|
* the defaults provided by the current Look and Feel.
|
820 |
|
|
*/
|
821 |
|
|
protected void installDefaults()
|
822 |
|
|
{
|
823 |
|
|
LookAndFeel.installColorsAndFont(progressBar, "ProgressBar.background",
|
824 |
|
|
"ProgressBar.foreground",
|
825 |
|
|
"ProgressBar.font");
|
826 |
|
|
LookAndFeel.installBorder(progressBar, "ProgressBar.border");
|
827 |
|
|
progressBar.setOpaque(true);
|
828 |
|
|
|
829 |
|
|
selectionForeground = UIManager.getColor("ProgressBar.selectionForeground");
|
830 |
|
|
selectionBackground = UIManager.getColor("ProgressBar.selectionBackground");
|
831 |
|
|
cellLength = UIManager.getInt("ProgressBar.cellLength");
|
832 |
|
|
cellSpacing = UIManager.getInt("ProgressBar.cellSpacing");
|
833 |
|
|
|
834 |
|
|
int repaintInterval = UIManager.getInt("ProgressBar.repaintInterval");
|
835 |
|
|
int cycleTime = UIManager.getInt("ProgressBar.cycleTime");
|
836 |
|
|
|
837 |
|
|
if (cycleTime % repaintInterval != 0
|
838 |
|
|
&& (cycleTime / repaintInterval) % 2 != 0)
|
839 |
|
|
{
|
840 |
|
|
int div = (cycleTime / repaintInterval) + 2;
|
841 |
|
|
div /= 2;
|
842 |
|
|
div *= 2;
|
843 |
|
|
cycleTime = div * repaintInterval;
|
844 |
|
|
}
|
845 |
|
|
setAnimationIndex(0);
|
846 |
|
|
numFrames = cycleTime / repaintInterval;
|
847 |
|
|
animationTimer.setDelay(repaintInterval);
|
848 |
|
|
}
|
849 |
|
|
|
850 |
|
|
/**
|
851 |
|
|
* The method uninstalls any defaults that were
|
852 |
|
|
* set by the current Look and Feel.
|
853 |
|
|
*/
|
854 |
|
|
protected void uninstallDefaults()
|
855 |
|
|
{
|
856 |
|
|
progressBar.setFont(null);
|
857 |
|
|
progressBar.setForeground(null);
|
858 |
|
|
progressBar.setBackground(null);
|
859 |
|
|
|
860 |
|
|
selectionForeground = null;
|
861 |
|
|
selectionBackground = null;
|
862 |
|
|
}
|
863 |
|
|
|
864 |
|
|
/**
|
865 |
|
|
* This method registers listeners to all the
|
866 |
|
|
* components that this UI delegate needs to listen to.
|
867 |
|
|
*/
|
868 |
|
|
protected void installListeners()
|
869 |
|
|
{
|
870 |
|
|
changeListener = new ChangeHandler();
|
871 |
|
|
propertyListener = new PropertyChangeHandler();
|
872 |
|
|
animation = new Animator();
|
873 |
|
|
|
874 |
|
|
progressBar.addChangeListener(changeListener);
|
875 |
|
|
progressBar.addPropertyChangeListener(propertyListener);
|
876 |
|
|
animationTimer.addActionListener(animation);
|
877 |
|
|
|
878 |
|
|
ancestorListener = new AncestorHandler();
|
879 |
|
|
progressBar.addAncestorListener(ancestorListener);
|
880 |
|
|
|
881 |
|
|
componentListener = new ComponentHandler();
|
882 |
|
|
progressBar.addComponentListener(componentListener);
|
883 |
|
|
}
|
884 |
|
|
|
885 |
|
|
/**
|
886 |
|
|
* This method unregisters listeners to all the
|
887 |
|
|
* components that were listened to.
|
888 |
|
|
*/
|
889 |
|
|
protected void uninstallListeners()
|
890 |
|
|
{
|
891 |
|
|
progressBar.removeChangeListener(changeListener);
|
892 |
|
|
progressBar.removePropertyChangeListener(propertyListener);
|
893 |
|
|
animationTimer.removeActionListener(animation);
|
894 |
|
|
|
895 |
|
|
changeListener = null;
|
896 |
|
|
propertyListener = null;
|
897 |
|
|
animation = null;
|
898 |
|
|
|
899 |
|
|
if (ancestorListener != null)
|
900 |
|
|
progressBar.removeAncestorListener(ancestorListener);
|
901 |
|
|
ancestorListener = null;
|
902 |
|
|
|
903 |
|
|
if (componentListener != null)
|
904 |
|
|
progressBar.removeComponentListener(componentListener);
|
905 |
|
|
componentListener = null;
|
906 |
|
|
}
|
907 |
|
|
|
908 |
|
|
/**
|
909 |
|
|
* This method installs the UI for the given JComponent.
|
910 |
|
|
* This includes setting up defaults and listeners as
|
911 |
|
|
* well as initializing any values or objects that
|
912 |
|
|
* the UI may need.
|
913 |
|
|
*
|
914 |
|
|
* @param c The JComponent that is having this UI installed.
|
915 |
|
|
*/
|
916 |
|
|
public void installUI(JComponent c)
|
917 |
|
|
{
|
918 |
|
|
super.installUI(c);
|
919 |
|
|
if (c instanceof JProgressBar)
|
920 |
|
|
{
|
921 |
|
|
progressBar = (JProgressBar) c;
|
922 |
|
|
|
923 |
|
|
animationTimer = new Timer(200, null);
|
924 |
|
|
animationTimer.setRepeats(true);
|
925 |
|
|
|
926 |
|
|
installDefaults();
|
927 |
|
|
installListeners();
|
928 |
|
|
}
|
929 |
|
|
if (progressBar.isIndeterminate())
|
930 |
|
|
startAnimationTimer();
|
931 |
|
|
}
|
932 |
|
|
|
933 |
|
|
/**
|
934 |
|
|
* This method removes the UI for the given JComponent.
|
935 |
|
|
* This includes removing any listeners or defaults
|
936 |
|
|
* that the installUI may have set up.
|
937 |
|
|
*
|
938 |
|
|
* @param c The JComponent that is having this UI uninstalled.
|
939 |
|
|
*/
|
940 |
|
|
public void uninstallUI(JComponent c)
|
941 |
|
|
{
|
942 |
|
|
super.uninstallUI(c);
|
943 |
|
|
uninstallListeners();
|
944 |
|
|
uninstallDefaults();
|
945 |
|
|
|
946 |
|
|
animationTimer = null;
|
947 |
|
|
progressBar = null;
|
948 |
|
|
}
|
949 |
|
|
|
950 |
|
|
}
|