1 /* JProgressBar.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import java
.awt
.Graphics
;
43 import javax
.accessibility
.Accessible
;
44 import javax
.accessibility
.AccessibleContext
;
45 import javax
.accessibility
.AccessibleRole
;
46 import javax
.accessibility
.AccessibleStateSet
;
47 import javax
.accessibility
.AccessibleValue
;
48 import javax
.swing
.event
.ChangeEvent
;
49 import javax
.swing
.event
.ChangeListener
;
50 import javax
.swing
.plaf
.ProgressBarUI
;
53 * The ProgressBar is a widget that displays in two modes. In
54 * determinate mode, it displays fills a percentage of its bar
55 * based on its current value. In indeterminate mode, it creates
56 * box and bounces it between its bounds.
59 * JProgressBars have the following properties:
63 * <tr><th> Property </th><th> Stored in </th><th> Bound? </th></tr>
64 * <tr><td> borderPainted </td><td> progressBar </td><td> yes </td></tr>
65 * <tr><td> changeListeners </td><td> progressBar </td><td> no </td></tr>
66 * <tr><td> indeterminate </td><td> progressBar </td><td> yes </td></tr>
67 * <tr><td> maximum </td><td> model </td><td> no </td></tr>
68 * <tr><td> minimum </td><td> model </td><td> no </td></tr>
69 * <tr><td> model </td><td> progressBar </td><td> no </td></tr>
70 * <tr><td> orientation </td><td> progressBar </td><td> yes </td></tr>
71 * <tr><td> percentComplete </td><td> progressBar </td><td> no </td></tr>
72 * <tr><td> string </td><td> progressBar </td><td> yes </td></tr>
73 * <tr><td> stringPainted </td><td> progressBar </td><td> yes </td></tr>
74 * <tr><td> value </td><td> model </td><td> no </td></tr>
77 public class JProgressBar
extends JComponent
implements SwingConstants
,
81 * AccessibleJProgressBar
83 protected class AccessibleJProgressBar
extends AccessibleJComponent
84 implements AccessibleValue
86 private static final long serialVersionUID
= -2938130009392721813L;
89 * Constructor AccessibleJProgressBar
91 * @param component TODO
93 protected AccessibleJProgressBar()
98 * getAccessibleStateSet
100 * @return AccessibleStateSet
102 public AccessibleStateSet
getAccessibleStateSet()
110 * @return AccessibleRole
112 public AccessibleRole
getAccessibleRole()
114 return AccessibleRole
.PROGRESS_BAR
;
120 * @return AccessibleValue
122 public AccessibleValue
getAccessibleValue()
128 * getCurrentAccessibleValue
132 public Number
getCurrentAccessibleValue()
138 * setCurrentAccessibleValue
144 public boolean setCurrentAccessibleValue(Number value0
)
150 * getMinimumAccessibleValue
154 public Number
getMinimumAccessibleValue()
160 * getMaximumAccessibleValue
164 public Number
getMaximumAccessibleValue()
170 private static final long serialVersionUID
= 1980046021813598781L;
172 /** Whether the ProgressBar is determinate. */
173 private transient boolean indeterminate
= false;
175 /** The orientation of the ProgressBar */
176 protected int orientation
= HORIZONTAL
;
178 /** Whether borders should be painted. */
179 protected boolean paintBorder
= true;
181 /** The model describing this ProgressBar. */
182 protected BoundedRangeModel model
;
184 /** The string that is displayed by the ProgressBar. */
185 protected String progressString
;
187 /** Whether the string should be painted. */
188 protected boolean paintString
= false;
190 /** The static changeEvent passed to all ChangeListeners. */
191 protected transient ChangeEvent changeEvent
;
193 /** The ChangeListener that listens to the model. */
194 protected ChangeListener changeListener
;
197 * Creates a new horizontally oriented JProgressBar object
198 * with a minimum of 0 and a maximum of 100.
200 public JProgressBar()
202 this(0, 100, HORIZONTAL
);
206 * Creates a new JProgressBar object with a minimum of 0,
207 * a maximum of 100, and the given orientation.
209 * @param orientation The orientation of the JProgressBar.
211 public JProgressBar(int orientation
)
213 this(0, 100, orientation
);
217 * Creates a new horizontally oriented JProgressBar object
218 * with the given minimum and maximum.
220 * @param minimum The minimum of the JProgressBar.
221 * @param maximum The maximum of the JProgressBar.
223 public JProgressBar(int minimum
, int maximum
)
225 this(minimum
, maximum
, HORIZONTAL
);
229 * Creates a new JProgressBar object with the given minimum,
230 * maximum, and orientation.
232 * @param minimum The minimum of the JProgressBar.
233 * @param maximum The maximum of the JProgressBar.
234 * @param orientation The orientation of the JProgressBar.
236 public JProgressBar(int minimum
, int maximum
, int orientation
)
238 model
= new DefaultBoundedRangeModel(minimum
, 0, minimum
, maximum
);
239 if (orientation
!= HORIZONTAL
&& orientation
!= VERTICAL
)
240 throw new IllegalArgumentException(orientation
+ " is not a legal orientation");
241 this.orientation
= orientation
;
242 changeListener
= createChangeListener();
243 model
.addChangeListener(changeListener
);
248 * Creates a new horizontally oriented JProgressBar object
249 * with the given model.
251 * @param model The model to be used with the JProgressBar.
253 public JProgressBar(BoundedRangeModel model
)
256 changeListener
= createChangeListener();
257 model
.addChangeListener(changeListener
);
262 * This method returns the current value of the JProgressBar.
264 * @return The current value of the JProgressBar.
266 public int getValue()
268 return model
.getValue();
272 * This method sets the value of the JProgressBar.
274 * @param value The value of the JProgressBar.
276 public void setValue(int value
)
278 model
.setValue(value
);
282 * This method paints the border of the JProgressBar
284 * @param graphics The graphics object to paint with.
286 protected void paintBorder(Graphics graphics
)
288 getBorder().paintBorder(this, graphics
, 0, 0,
294 * This method returns the orientation of the JProgressBar.
296 * @return The orientation of the JProgressBar.
298 public int getOrientation()
304 * This method changes the orientation property. The orientation of the
305 * JProgressBar can be either horizontal or vertical.
307 * @param orientation The orientation of the JProgressBar.
309 public void setOrientation(int orientation
)
311 if (orientation
!= VERTICAL
&& orientation
!= HORIZONTAL
)
312 throw new IllegalArgumentException("orientation must be one of VERTICAL or HORIZONTAL");
313 if (this.orientation
!= orientation
)
315 int oldOrientation
= this.orientation
;
316 this.orientation
= orientation
;
317 firePropertyChange("orientation", oldOrientation
,
323 * This method returns whether the progressString will be painted.
325 * @return Whether the string is painted.
327 public boolean isStringPainted()
333 * This method changes the stringPainted property.
335 * @param painted Whether the string is painted.
337 public void setStringPainted(boolean painted
)
339 if (paintString
!= painted
)
341 boolean oldPainted
= paintString
;
342 paintString
= painted
;
343 firePropertyChange("stringPainted", oldPainted
,
349 * This method returns the string that is painted if the
350 * stringPainted property is set to true. If there is no
351 * string set, it will return a string containing the
352 * JProgressBar's value as a percent.
354 * @return The string that is painted.
356 public String
getString()
358 if (progressString
!= null)
359 return progressString
;
361 return (int) (getPercentComplete() * 100) + "%";
365 * This method changes the string property. The string
366 * given will be the one painted. If you want to
367 * revert to the default string given, set the
370 * @param string The string to be painted.
372 public void setString(String string
)
374 if (((string
== null || progressString
== null) &&
375 string
!= progressString
) || (string
!= null &&
376 ! string
.equals(progressString
)))
378 String oldString
= progressString
;
379 progressString
= string
;
380 firePropertyChange("string", oldString
, progressString
);
385 * This method returns the percent of the bar
386 * that is "complete". (This is the amount value / (max - min)).
388 * @return DOCUMENT ME!
390 public double getPercentComplete()
392 if (getMaximum() == getMinimum())
395 return (double) (model
.getValue() - model
.getMinimum()) / (model
397 - model
.getMinimum());
401 * This method returns whether the border is painted.
403 * @return Whether the border is painted.
405 public boolean isBorderPainted()
411 * This method changes the borderPainted property.
413 * @param painted Whether the border is painted.
415 public void setBorderPainted(boolean painted
)
417 if (painted
!= paintBorder
)
419 boolean oldPainted
= paintBorder
;
420 paintBorder
= painted
;
421 firePropertyChange("borderPainted", oldPainted
,
427 * This method returns the JProgressBar's UI delegate.
429 * @return This JProgressBar's UI delegate.
431 public ProgressBarUI
getUI()
433 return (ProgressBarUI
) ui
;
437 * This method changes the UI property for this JProgressBar.
439 * @param ui The new UI delegate.
441 public void setUI(ProgressBarUI ui
)
447 * This method reverts the UI delegate for this JProgressBar
448 * to the default for this Look and Feel.
450 public void updateUI()
452 setUI((ProgressBarUI
) UIManager
.getUI(this));
457 * This method returns the identifier to allow the UIManager
458 * to pick the correct class to act as the UI for
461 * @return The UIClassID: "ProgressBarUI".
463 public String
getUIClassID()
465 return "ProgressBarUI";
469 * This method returns a ChangeListener that gets registered
470 * model. By default, the ChangeListener, propagates the
471 * ChangeEvents to the ChangeListeners of the JProgressBar.
473 * @return A new ChangeListener.
475 protected ChangeListener
createChangeListener()
477 return new ChangeListener()
479 public void stateChanged(ChangeEvent ce
)
487 * This method adds a ChangeListener to this JProgressBar.
489 * @param listener The ChangeListener to add to this JProgressBar.
491 public void addChangeListener(ChangeListener listener
)
493 listenerList
.add(ChangeListener
.class, listener
);
497 * This method removes a ChangeListener from this JProgressBar.
499 * @param listener The ChangeListener to remove from this JProgressBar.
501 public void removeChangeListener(ChangeListener listener
)
503 listenerList
.remove(ChangeListener
.class, listener
);
507 * This method returns an array of all ChangeListeners listening to this
510 * @return An array of ChangeListeners listening to this progress bar.
512 public ChangeListener
[] getChangeListeners()
514 return (ChangeListener
[]) listenerList
.getListeners(ChangeListener
.class);
518 * This method is called when the JProgressBar receives a ChangeEvent
519 * from its model. This simply propagates the event (changing the source
520 * to the JProgressBar) to the JProgressBar's listeners.
522 protected void fireStateChanged()
524 Object
[] changeListeners
= listenerList
.getListenerList();
525 if (changeEvent
== null)
526 changeEvent
= new ChangeEvent(this);
527 for (int i
= changeListeners
.length
- 2; i
>= 0; i
-= 2)
529 if (changeListeners
[i
] == ChangeListener
.class)
530 ((ChangeListener
) changeListeners
[i
+ 1]).stateChanged(changeEvent
);
535 * This method returns the model used with this JProgressBar.
537 * @return The model used with this JProgressBar.
539 public BoundedRangeModel
getModel()
545 * This method changes the model property for this JProgressBar.
547 * @param model The model to use with this JProgressBar.
549 public void setModel(BoundedRangeModel model
)
551 if (model
!= this.model
)
553 this.model
.removeChangeListener(changeListener
);
555 this.model
.addChangeListener(changeListener
);
561 * This method returns the minimum value of this JProgressBar.
563 * @return The minimum value of this JProgressBar.
565 public int getMinimum()
567 return model
.getMinimum();
571 * This method sets the minimum value of this JProgressBar.
573 * @param minimum The minimum value of this JProgressBar.
575 public void setMinimum(int minimum
)
577 model
.setMinimum(minimum
);
581 * This method returns the maximum value of this JProgressBar.
583 * @return The maximum value of this JProgressBar.
585 public int getMaximum()
587 return model
.getMaximum();
591 * This method sets the maximum value of this JProgressBar.
593 * @param maximum The maximum value of this JProgressBar.
595 public void setMaximum(int maximum
)
597 model
.setMaximum(maximum
);
601 * This method returns a string that can be used to
602 * describe this JProgressBar. This method is usually
603 * only used for debugging purposes.
605 * @return A string that describes this JProgressBar.
607 protected String
paramString()
609 return "JProgressBar";
613 * This method changes the indeterminate property. If the
614 * JProgressBar is determinate, it paints a percentage
615 * of the bar described by its value. If it is indeterminate,
616 * it simply bounces a box between the ends of the bar; the
617 * value of the JProgressBar is ignored.
619 * @param newValue Whether the JProgressBar is indeterminate.
621 public void setIndeterminate(boolean newValue
)
623 if (indeterminate
!= newValue
)
625 boolean olddeter
= indeterminate
;
626 indeterminate
= newValue
;
627 firePropertyChange("indeterminate", olddeter
,
633 * This method returns whether the JProgressBar is indeterminate.
635 * @return Whether this JProgressBar is indeterminate.
637 public boolean isIndeterminate()
639 return indeterminate
;
645 * @return DOCUMENT ME!
647 public AccessibleContext
getAccessibleContext()
649 if (accessibleContext
== null)
650 accessibleContext
= new AccessibleJProgressBar();
652 return accessibleContext
;