KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JProgressBar


1 /*
2  * @(#)JProgressBar.java 1.93 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;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Graphics JavaDoc;
12
13 import java.text.Format JavaDoc;
14 import java.text.NumberFormat JavaDoc;
15
16 import java.io.Serializable JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import java.io.ObjectInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import javax.swing.event.*;
22 import javax.accessibility.*;
23 import javax.swing.plaf.ProgressBarUI JavaDoc;
24
25
26 /**
27  * A component that, by default, displays an integer value within a bounded
28  * interval. A progress bar typically communicates the progress of some
29  * work by displaying its percentage of completion and possibly a textual
30  * display of this percentage.
31  *
32  * <p>
33  *
34  * To indicate that a task of unknown length is executing,
35  * you can put a progress bar into indeterminate mode.
36  * While the bar is in indeterminate mode,
37  * it animates constantly to show that work is occurring.
38  * As soon as you can determine the task's length and amount of progress,
39  * you should update the progress bar's value
40  * and switch it back to determinate mode.
41  *
42  * <p>
43  *
44  * Here is an example of creating a progress bar,
45  * where <code>task</code> is an object
46  * that returns information about the progress of some work:
47  *
48  *<pre>
49  *progressBar = new JProgressBar(0, task.getLengthOfTask());
50  *progressBar.setValue(0);
51  *progressBar.setStringPainted(true);
52  *</pre>
53  *
54  * Here is an example of updating the value of the progress bar:
55  *
56  *<pre>
57  *progressBar.setValue(task.getCurrent());
58  *</pre>
59  *
60  * Here is an example of putting a progress bar into
61  * indeterminate mode,
62  * and then switching back to determinate mode
63  * once the length of the task is known:
64  *
65  *<pre>
66  *progressBar = new JProgressBar();
67  *<em>...//when the task of (initially) unknown length begins:</em>
68  *progressBar.setIndeterminate(true);
69  *<em>...//do some work; get length of task...</em>
70  *progressBar.setMaximum(newLength);
71  *progressBar.setValue(newValue);
72  *progressBar.setIndeterminate(false);
73  *</pre>
74  *
75  * <p>
76  *
77  * For complete examples and further documentation see
78  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html" target="_top">How to Monitor Progress</a>,
79  * a section in <em>The Java Tutorial.</em>
80  *
81  * <p>
82  * <strong>Warning:</strong>
83  * Serialized objects of this class will not be compatible with
84  * future Swing releases. The current serialization support is
85  * appropriate for short term storage or RMI between applications running
86  * the same version of Swing. As of 1.4, support for long term storage
87  * of all JavaBeans<sup><font size="-2">TM</font></sup>
88  * has been added to the <code>java.beans</code> package.
89  * Please see {@link java.beans.XMLEncoder}.
90  *
91  * @see javax.swing.plaf.basic.BasicProgressBarUI
92  *
93  * @beaninfo
94  * attribute: isContainer false
95  * description: A component that displays an integer value.
96  *
97  * @version 1.93 12/19/03
98  * @author Michael C. Albers
99  * @author Kathy Walrath
100  */

101 public class JProgressBar extends JComponent JavaDoc implements SwingConstants JavaDoc, Accessible
102 {
103     /**
104      * @see #getUIClassID
105      */

106     private static final String JavaDoc uiClassID = "ProgressBarUI";
107
108     /**
109      * Whether the progress bar is horizontal or vertical.
110      * The default is <code>HORIZONTAL</code>.
111      *
112      * @see #setOrientation
113      */

114     protected int orientation;
115
116     /**
117      * Whether to display a border around the progress bar.
118      * The default is <code>true</code>.
119      *
120      * @see #setBorderPainted
121      */

122     protected boolean paintBorder;
123
124     /**
125      * The object that holds the data for the progress bar.
126      *
127      * @see #setModel
128      */

129     protected BoundedRangeModel JavaDoc model;
130
131     /**
132      * An optional string that can be displayed on the progress bar.
133      * The default is <code>null</code>. Setting this to a non-<code>null</code>
134      * value does not imply that the string will be displayed.
135      *
136      * @see #setString
137      */

138     protected String JavaDoc progressString;
139
140     /**
141      * Whether to textually display a string on the progress bar.
142      * The default is <code>false</code>.
143      * Setting this to <code>true</code> causes a textual
144      * display of the progress to be rendered on the progress bar. If
145      * the <code>progressString</code> is <code>null</code>,
146      * the percentage of completion is displayed on the progress bar.
147      * Otherwise, the <code>progressString</code> is
148      * rendered on the progress bar.
149      *
150      * @see #setStringPainted
151      */

152     protected boolean paintString;
153
154     /**
155      * The default minimum for a progress bar is 0.
156      */

157     static final private int defaultMinimum = 0;
158     /**
159      * The default maximum for a progress bar is 100.
160      */

161     static final private int defaultMaximum = 100;
162     /**
163      * The default orientation for a progress bar is <code>HORIZONTAL</code>.
164      */

165     static final private int defaultOrientation = HORIZONTAL;
166
167     /**
168      * Only one <code>ChangeEvent</code> is needed per instance since the
169      * event's only interesting property is the immutable source, which
170      * is the progress bar.
171      */

172     protected transient ChangeEvent changeEvent = null;
173
174     /**
175      * Listens for change events sent by the progress bar's model,
176      * redispatching them
177      * to change-event listeners registered upon
178      * this progress bar.
179      *
180      * @see #createChangeListener
181      */

182     protected ChangeListener changeListener = null;
183
184     /**
185      * Format used when displaying percent complete.
186      */

187     private transient Format JavaDoc format;
188
189     /**
190      * Whether the progress bar is indeterminate (<code>true</code>) or
191      * normal (<code>false</code>); the default is <code>false</code>.
192      *
193      * @see #setIndeterminate
194      * @since 1.4
195      */

196     private boolean indeterminate;
197
198
199    /**
200      * Creates a horizontal progress bar
201      * that displays a border but no progress string.
202      * The initial and minimum values are 0,
203      * and the maximum is 100.
204      *
205      * @see #setOrientation
206      * @see #setBorderPainted
207      * @see #setStringPainted
208      * @see #setString
209      * @see #setIndeterminate
210      */

211     public JProgressBar()
212     {
213     this(defaultOrientation);
214     }
215
216    /**
217      * Creates a progress bar with the specified orientation,
218      * which can be
219      * either <code>JProgressBar.VERTICAL</code> or
220      * <code>JProgressBar.HORIZONTAL</code>.
221      * By default, a border is painted but a progress string is not.
222      * The initial and minimum values are 0,
223      * and the maximum is 100.
224      *
225      * @param orient the desired orientation of the progress bar
226      *
227      * @see #setOrientation
228      * @see #setBorderPainted
229      * @see #setStringPainted
230      * @see #setString
231      * @see #setIndeterminate
232      */

233     public JProgressBar(int orient)
234     {
235     this(orient, defaultMinimum, defaultMaximum);
236     }
237
238
239     /**
240      * Creates a horizontal progress bar
241      * with the specified minimum and maximum.
242      * Sets the initial value of the progress bar to the specified minimum.
243      * By default, a border is painted but a progress string is not.
244      * The <code>BoundedRangeModel</code> that holds the progress bar's data
245      * handles any issues that may arise from improperly setting the
246      * minimum, initial, and maximum values on the progress bar.
247      *
248      * @param min the minimum value of the progress bar
249      * @param max the maximum value of the progress bar
250      *
251      * @see BoundedRangeModel
252      * @see #setOrientation
253      * @see #setBorderPainted
254      * @see #setStringPainted
255      * @see #setString
256      * @see #setIndeterminate
257      */

258     public JProgressBar(int min, int max)
259     {
260     this(defaultOrientation, min, max);
261     }
262
263
264     /**
265      * Creates a progress bar using the specified orientation,
266      * minimum, and maximum.
267      * By default, a border is painted but a progress string is not.
268      * Sets the initial value of the progress bar to the specified minimum.
269      * The <code>BoundedRangeModel</code> that holds the progress bar's data
270      * handles any issues that may arise from improperly setting the
271      * minimum, initial, and maximum values on the progress bar.
272      *
273      * @param orient the desired orientation of the progress bar
274      * @param min the minimum value of the progress bar
275      * @param max the maximum value of the progress bar
276      *
277      * @see BoundedRangeModel
278      * @see #setOrientation
279      * @see #setBorderPainted
280      * @see #setStringPainted
281      * @see #setString
282      * @see #setIndeterminate
283      */

284     public JProgressBar(int orient, int min, int max)
285     {
286     // Creating the model this way is a bit simplistic, but
287
// I believe that it is the the most common usage of this
288
// component - it's what people will expect.
289
setModel(new DefaultBoundedRangeModel JavaDoc(min, 0, min, max));
290         updateUI();
291
292         setOrientation(orient); // documented with set/getOrientation()
293
setBorderPainted(true); // documented with is/setBorderPainted()
294
setStringPainted(false); // see setStringPainted
295
setString(null); // see getString
296
setIndeterminate(false); // see setIndeterminate
297
}
298
299
300     /**
301      * Creates a horizontal progress bar
302      * that uses the specified model
303      * to hold the progress bar's data.
304      * By default, a border is painted but a progress string is not.
305      *
306      * @param newModel the data model for the progress bar
307      *
308      * @see #setOrientation
309      * @see #setBorderPainted
310      * @see #setStringPainted
311      * @see #setString
312      * @see #setIndeterminate
313      */

314     public JProgressBar(BoundedRangeModel JavaDoc newModel)
315     {
316         setModel(newModel);
317         updateUI();
318
319         setOrientation(defaultOrientation); // see setOrientation()
320
setBorderPainted(true); // see setBorderPainted()
321
setStringPainted(false); // see setStringPainted
322
setString(null); // see getString
323
setIndeterminate(false); // see setIndeterminate
324
}
325
326
327     /**
328      * Returns <code>JProgressBar.VERTICAL</code> or
329      * <code>JProgressBar.HORIZONTAL</code>, depending on the orientation
330      * of the progress bar. The default orientation is
331      * <code>HORIZONTAL</code>.
332      *
333      * @return <code>HORIZONTAL</code> or <code>VERTICAL</code>
334      * @see #setOrientation
335      */

336     public int getOrientation() {
337         return orientation;
338     }
339
340
341    /**
342      * Sets the progress bar's orientation to <code>newOrientation</code>,
343      * which must be <code>JProgressBar.VERTICAL</code> or
344      * <code>JProgressBar.HORIZONTAL</code>. The default orientation
345      * is <code>HORIZONTAL</code>.
346      *
347      * @param newOrientation <code>HORIZONTAL</code> or <code>VERTICAL</code>
348      * @exception IllegalArgumentException if <code>newOrientation</code>
349      * is an illegal value
350      * @see #getOrientation
351      *
352      * @beaninfo
353      * preferred: true
354      * bound: true
355      * attribute: visualUpdate true
356      * description: Set the progress bar's orientation.
357      */

358     public void setOrientation(int newOrientation) {
359         if (orientation != newOrientation) {
360             switch (newOrientation) {
361             case VERTICAL:
362             case HORIZONTAL:
363                 int oldOrientation = orientation;
364                 orientation = newOrientation;
365                 firePropertyChange("orientation", oldOrientation, newOrientation);
366                 if (accessibleContext != null) {
367                     accessibleContext.firePropertyChange(
368                 AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
369                             ((oldOrientation == VERTICAL)
370                  ? AccessibleState.VERTICAL
371                  : AccessibleState.HORIZONTAL),
372                             ((orientation == VERTICAL)
373                  ? AccessibleState.VERTICAL
374                  : AccessibleState.HORIZONTAL));
375             }
376                 break;
377             default:
378                 throw new IllegalArgumentException JavaDoc(newOrientation +
379                                              " is not a legal orientation");
380             }
381             revalidate();
382         }
383     }
384
385
386     /**
387      * Returns the value of the <code>stringPainted</code> property.
388      *
389      * @return the value of the <code>stringPainted</code> property
390      * @see #setStringPainted
391      * @see #setString
392      */

393     public boolean isStringPainted() {
394         return paintString;
395     }
396
397
398     /**
399      * Sets the value of the <code>stringPainted</code> property,
400      * which determines whether the progress bar
401      * should render a progress string.
402      * The default is <code>false</code>:
403      * no string is painted.
404      * Some look and feels might not support progress strings
405      * or might support them only when the progress bar is in determinate mode.
406      *
407      * @param b <code>true</code> if the progress bar should render a string
408      * @see #isStringPainted
409      * @see #setString
410      * @beaninfo
411      * bound: true
412      * attribute: visualUpdate true
413      * description: Whether the progress bar should render a string.
414      */

415     public void setStringPainted(boolean b) {
416         //PENDING: specify that string not painted when in indeterminate mode?
417
// or just leave that to the L&F?
418
boolean oldValue = paintString;
419         paintString = b;
420         firePropertyChange("stringPainted", oldValue, paintString);
421         if (paintString != oldValue) {
422         revalidate();
423             repaint();
424         }
425     }
426
427
428     /**
429      * Returns the current value of the progress string.
430      * If you are providing a custom progress string
431      * by overriding this method,
432      * make sure your implementation calls <code>setString</code> before
433      * calling <code>super.getString</code>.
434      *
435      * @return the value of the percent string
436      * @see #setString
437      */

438     public String JavaDoc getString(){
439     if (progressString != null) {
440         return progressString;
441     } else {
442             if (format == null) {
443                 format = NumberFormat.getPercentInstance();
444             }
445             return format.format(new Double JavaDoc(getPercentComplete()));
446         }
447     }
448
449     /**
450      * Sets the value of the progress string. By default,
451      * this string is <code>null</code>.
452      * If you have provided a custom progress string and want to revert to
453      * the built-in behavior, set the string back to <code>null</code>.
454      * If you are providing a custom progress string
455      * by overriding this method,
456      * make sure that you call <code>setString</code> before
457      * calling <code>getString</code>.
458      * The progress string is painted only if
459      * the <code>isStringPainted</code> method returns <code>true</code>.
460      *
461      * @param s the value of the percent string
462      * @see #getString
463      * @see #setStringPainted
464      * @see #isStringPainted
465      * @beaninfo
466      * bound: true
467      * attribute: visualUpdate true
468      * description: Specifies the progress string to paint
469      */

470     public void setString(String JavaDoc s){
471         String JavaDoc oldValue = progressString;
472     progressString = s;
473         firePropertyChange("string", oldValue, progressString);
474         if (progressString == null || oldValue == null || !progressString.equals(oldValue)) {
475         repaint();
476         }
477     }
478
479     /**
480      * Returns the percent complete for the progress bar.
481      * Note that this number is between 0.0 and 1.0.
482      *
483      * @return the percent complete for this progress bar
484      */

485     public double getPercentComplete() {
486     long span = model.getMaximum() - model.getMinimum();
487     double currentValue = model.getValue();
488     double pc = (currentValue - model.getMinimum()) / span;
489     return pc;
490     }
491
492     /**
493      * Returns the <code>borderPainted</code> property.
494      *
495      * @return the value of the <code>borderPainted</code> property
496      * @see #setBorderPainted
497      * @beaninfo
498      * description: Does the progress bar paint its border
499      */

500     public boolean isBorderPainted() {
501         return paintBorder;
502     }
503
504     /**
505      * Sets the <code>borderPainted</code> property, which is
506      * <code>true</code> if the progress bar should paint its border.
507      * The default value for this property is <code>true</code>.
508      * Some look and feels might not implement painted borders;
509      * they will ignore this property.
510      *
511      * @param b <code>true</code> if the progress bar
512      * should paint its border;
513      * otherwise, <code>false</code>
514      * @see #isBorderPainted
515      * @beaninfo
516      * bound: true
517      * attribute: visualUpdate true
518      * description: Whether the progress bar should paint its border.
519      */

520     public void setBorderPainted(boolean b) {
521         boolean oldValue = paintBorder;
522         paintBorder = b;
523         firePropertyChange("borderPainted", oldValue, paintBorder);
524         if (paintBorder != oldValue) {
525             repaint();
526         }
527     }
528
529     /**
530      * Paints the progress bar's border if the <code>borderPainted</code>
531      * property is <code>true</code>.
532      *
533      * @param g the <code>Graphics</code> context within which to paint the border
534      * @see #paint
535      * @see #setBorder
536      * @see #isBorderPainted
537      * @see #setBorderPainted
538      */

539     protected void paintBorder(Graphics JavaDoc g) {
540         if (isBorderPainted()) {
541             super.paintBorder(g);
542         }
543     }
544
545
546     /**
547      * Returns the look-and-feel object that renders this component.
548      *
549      * @return the <code>ProgressBarUI</code> object that renders this component
550      */

551     public ProgressBarUI JavaDoc getUI() {
552         return (ProgressBarUI JavaDoc)ui;
553     }
554
555     /**
556      * Sets the look-and-feel object that renders this component.
557      *
558      * @param ui a <code>ProgressBarUI</code> object
559      * @see UIDefaults#getUI
560      * @beaninfo
561      * bound: true
562      * hidden: true
563      * attribute: visualUpdate true
564      * description: The UI object that implements the Component's LookAndFeel.
565      */

566     public void setUI(ProgressBarUI JavaDoc ui) {
567         super.setUI(ui);
568     }
569
570
571     /**
572      * Resets the UI property to a value from the current look and feel.
573      *
574      * @see JComponent#updateUI
575      */

576     public void updateUI() {
577         setUI((ProgressBarUI JavaDoc)UIManager.getUI(this));
578     }
579
580
581     /**
582      * Returns the name of the look-and-feel class that renders this component.
583      *
584      * @return the string "ProgressBarUI"
585      * @see JComponent#getUIClassID
586      * @see UIDefaults#getUI
587      * @beaninfo
588      * expert: true
589      * description: A string that specifies the name of the look-and-feel class.
590      */

591     public String JavaDoc getUIClassID() {
592         return uiClassID;
593     }
594
595
596     /* We pass each Change event to the listeners with the
597      * the progress bar as the event source.
598      * <p>
599      * <strong>Warning:</strong>
600      * Serialized objects of this class will not be compatible with
601      * future Swing releases. The current serialization support is
602      * appropriate for short term storage or RMI between applications running
603      * the same version of Swing. As of 1.4, support for long term storage
604      * of all JavaBeans<sup><font size="-2">TM</font></sup>
605      * has been added to the <code>java.beans</code> package.
606      * Please see {@link java.beans.XMLEncoder}.
607      */

608     private class ModelListener implements ChangeListener, Serializable JavaDoc {
609         public void stateChanged(ChangeEvent e) {
610             fireStateChanged();
611         }
612     }
613
614     /**
615      * Subclasses that want to handle change events
616      * from the model differently
617      * can override this to return
618      * an instance of a custom <code>ChangeListener</code> implementation.
619      *
620      * @see #changeListener
621      * @see javax.swing.event.ChangeListener
622      * @see javax.swing.BoundedRangeModel
623      */

624     protected ChangeListener createChangeListener() {
625         return new ModelListener();
626     }
627
628     /**
629      * Adds the specified <code>ChangeListener</code> to the progress bar.
630      *
631      * @param l the <code>ChangeListener</code> to add
632      */

633     public void addChangeListener(ChangeListener l) {
634         listenerList.add(ChangeListener.class, l);
635     }
636     
637     /**
638      * Removes a <code>ChangeListener</code> from the progress bar.
639      *
640      * @param l the <code>ChangeListener</code> to remove
641      */

642     public void removeChangeListener(ChangeListener l) {
643         listenerList.remove(ChangeListener.class, l);
644     }
645         
646     /**
647      * Returns an array of all the <code>ChangeListener</code>s added
648      * to this progress bar with <code>addChangeListener</code>.
649      *
650      * @return all of the <code>ChangeListener</code>s added or an empty
651      * array if no listeners have been added
652      * @since 1.4
653      */

654     public ChangeListener[] getChangeListeners() {
655         return (ChangeListener[])listenerList.getListeners(
656                 ChangeListener.class);
657     }
658
659     /**
660      * Notifies all listeners that have registered interest in
661      * <code>ChangeEvent</code>s.
662      * The event instance
663      * is created if necessary.
664      *
665      * @see EventListenerList
666      */

667     protected void fireStateChanged() {
668         // Guaranteed to return a non-null array
669
Object JavaDoc[] listeners = listenerList.getListenerList();
670         // Process the listeners last to first, notifying
671
// those that are interested in this event
672
for (int i = listeners.length-2; i>=0; i-=2) {
673             if (listeners[i]==ChangeListener.class) {
674                 // Lazily create the event:
675
if (changeEvent == null)
676                     changeEvent = new ChangeEvent(this);
677                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
678             }
679         }
680     }
681       
682     /**
683      * Returns the data model used by this progress bar.
684      *
685      * @return the <code>BoundedRangeModel</code> currently in use
686      * @see BoundedRangeModel
687      */

688     public BoundedRangeModel JavaDoc getModel() {
689         return model;
690     }
691
692     /**
693      * Sets the data model used by the <code>JProgressBar</code>.
694      *
695      * @param newModel the <code>BoundedRangeModel</code> to use
696      *
697      * @beaninfo
698      * expert: true
699      * description: The data model used by the JProgressBar.
700      */

701     public void setModel(BoundedRangeModel JavaDoc newModel) {
702         // PENDING(???) setting the same model to multiple bars is broken; listeners
703
BoundedRangeModel JavaDoc oldModel = getModel();
704
705         if (newModel != oldModel) {
706             if (oldModel != null) {
707                 oldModel.removeChangeListener(changeListener);
708                 changeListener = null;
709             }
710
711             model = newModel;
712
713             if (newModel != null) {
714                 changeListener = createChangeListener();
715                 newModel.addChangeListener(changeListener);
716             }
717
718         if (accessibleContext != null) {
719                 accessibleContext.firePropertyChange(
720                 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
721                         (oldModel== null
722              ? null : new Integer JavaDoc(oldModel.getValue())),
723                         (newModel== null
724              ? null : new Integer JavaDoc(newModel.getValue())));
725         }
726
727         if (model != null) {
728         model.setExtent(0);
729         }
730             repaint();
731         }
732     }
733
734
735     /* All of the model methods are implemented by delegation. */
736
737     /**
738      * Returns the progress bar's current value,
739      * which is stored in the progress bar's <code>BoundedRangeModel</code>.
740      * The value is always between the
741      * minimum and maximum values, inclusive. By default, the
742      * value is initialized to be equal to the minimum value.
743      *
744      * @return the current value of the progress bar
745      * @see #setValue
746      * @see BoundedRangeModel#getValue
747      */

748     public int getValue() { return getModel().getValue(); }
749
750     /**
751      * Returns the progress bar's minimum value,
752      * which is stored in the progress bar's <code>BoundedRangeModel</code>.
753      * By default, the minimum value is <code>0</code>.
754      *
755      * @return the progress bar's minimum value
756      * @see #setMinimum
757      * @see BoundedRangeModel#getMinimum
758      */

759     public int getMinimum() { return getModel().getMinimum(); }
760
761     /**
762      * Returns the progress bar's maximum value,
763      * which is stored in the progress bar's <code>BoundedRangeModel</code>.
764      * By default, the maximum value is <code>100</code>.
765      *
766      * @return the progress bar's maximum value
767      * @see #setMaximum
768      * @see BoundedRangeModel#getMaximum
769      */

770     public int getMaximum() { return getModel().getMaximum(); }
771
772     /**
773      * Sets the progress bar's current value
774      * (stored in the progress bar's data model) to <code>n</code>.
775      * The data model (a <code>BoundedRangeModel</code> instance)
776      * handles any mathematical
777      * issues arising from assigning faulty values.
778      * <p>
779      * If the new value is different from the previous value,
780      * all change listeners are notified.
781      *
782      * @param n the new value
783      * @see #getValue
784      * @see BoundedRangeModel#setValue
785      * @beaninfo
786      * preferred: true
787      * description: The progress bar's current value.
788      */

789     public void setValue(int n) {
790         BoundedRangeModel JavaDoc brm = getModel();
791     int oldValue = brm.getValue();
792     brm.setValue(n);
793  
794     if (accessibleContext != null) {
795             accessibleContext.firePropertyChange(
796             AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
797                     new Integer JavaDoc(oldValue),
798                     new Integer JavaDoc(brm.getValue()));
799     }
800     }
801
802     /**
803      * Sets the progress bar's minimum value
804      * (stored in the progress bar's data model) to <code>n</code>.
805      * The data model (a <code>BoundedRangeModel</code> instance)
806      * handles any mathematical
807      * issues arising from assigning faulty values.
808      * <p>
809      * If the minimum value is different from the previous minimum,
810      * all change listeners are notified.
811      *
812      * @param n the new minimum
813      * @see #getMinimum
814      * @see #addChangeListener
815      * @see BoundedRangeModel#setMinimum
816      * @beaninfo
817      * preferred: true
818      * description: The progress bar's minimum value.
819      */

820     public void setMinimum(int n) { getModel().setMinimum(n); }
821
822     /**
823      * Sets the progress bar's maximum value
824      * (stored in the progress bar's data model) to <code>n</code>.
825      * The underlying <code>BoundedRangeModel</code> handles any mathematical
826      * issues arising from assigning faulty values.
827      * <p>
828      * If the maximum value is different from the previous maximum,
829      * all change listeners are notified.
830      *
831      * @param n the new maximum
832      * @see #getMaximum
833      * @see #addChangeListener
834      * @see BoundedRangeModel#setMaximum
835      * @beaninfo
836      * preferred: true
837      * description: The progress bar's maximum value.
838      */

839     public void setMaximum(int n) { getModel().setMaximum(n); }
840
841     /**
842      * Sets the <code>indeterminate</code> property of the progress bar,
843      * which determines whether the progress bar is in determinate
844      * or indeterminate mode.
845      * An indeterminate progress bar continuously displays animation
846      * indicating that an operation of unknown length is occurring.
847      * By default, this property is <code>false</code>.
848      * Some look and feels might not support indeterminate progress bars;
849      * they will ignore this property.
850      *
851      * <p>
852      *
853      * See
854      * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html" target="_top">How to Monitor Progress</a>
855      * for examples of using indeterminate progress bars.
856      *
857      * @param newValue <code>true</code> if the progress bar
858      * should change to indeterminate mode;
859      * <code>false</code> if it should revert to normal.
860      *
861      * @see #isIndeterminate
862      * @see javax.swing.plaf.basic.BasicProgressBarUI
863      *
864      * @since 1.4
865      *
866      * @beaninfo
867      * bound: true
868      * attribute: visualUpdate true
869      * description: Set whether the progress bar is indeterminate (true)
870      * or normal (false).
871      */

872     public void setIndeterminate(boolean newValue) {
873     boolean oldValue = indeterminate;
874     indeterminate = newValue;
875     firePropertyChange("indeterminate", oldValue, indeterminate);
876     }
877
878     /**
879      * Returns the value of the <code>indeterminate</code> property.
880      *
881      * @return the value of the <code>indeterminate</code> property
882      * @see #setIndeterminate
883      *
884      * @since 1.4
885      *
886      * @beaninfo
887      * description: Is the progress bar indeterminate (true)
888      * or normal (false)?
889      */

890     public boolean isIndeterminate() {
891         return indeterminate;
892     }
893
894
895     /**
896      * See readObject() and writeObject() in JComponent for more
897      * information about serialization in Swing.
898      */

899     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
900         s.defaultWriteObject();
901         if (getUIClassID().equals(uiClassID)) {
902             byte count = JComponent.getWriteObjCounter(this);
903             JComponent.setWriteObjCounter(this, --count);
904             if (count == 0 && ui != null) {
905                 ui.installUI(this);
906             }
907         }
908     }
909
910
911     /**
912      * Returns a string representation of this <code>JProgressBar</code>.
913      * This method is intended to be used only for debugging purposes. The
914      * content and format of the returned string may vary between
915      * implementations. The returned string may be empty but may not
916      * be <code>null</code>.
917      *
918      * @return a string representation of this <code>JProgressBar</code>
919      */

920     protected String JavaDoc paramString() {
921     String JavaDoc orientationString = (orientation == HORIZONTAL ?
922                     "HORIZONTAL" : "VERTICAL");
923     String JavaDoc paintBorderString = (paintBorder ?
924                     "true" : "false");
925     String JavaDoc progressStringString = (progressString != null ?
926                        progressString : "");
927     String JavaDoc paintStringString = (paintString ?
928                     "true" : "false");
929     String JavaDoc indeterminateString = (indeterminate ?
930                     "true" : "false");
931
932     return super.paramString() +
933     ",orientation=" + orientationString +
934     ",paintBorder=" + paintBorderString +
935     ",paintString=" + paintStringString +
936     ",progressString=" + progressStringString +
937     ",indeterminateString=" + indeterminateString;
938     }
939
940 /////////////////
941
// Accessibility support
942
////////////////
943

944     /**
945      * Gets the <code>AccessibleContext</code> associated with this
946      * <code>JProgressBar</code>. For progress bars, the
947      * <code>AccessibleContext</code> takes the form of an
948      * <code>AccessibleJProgressBar</code>.
949      * A new <code>AccessibleJProgressBar</code> instance is created if necessary.
950      *
951      * @return an <code>AccessibleJProgressBar</code> that serves as the
952      * <code>AccessibleContext</code> of this <code>JProgressBar</code>
953      * @beaninfo
954      * expert: true
955      * description: The AccessibleContext associated with this ProgressBar.
956      */

957     public AccessibleContext getAccessibleContext() {
958         if (accessibleContext == null) {
959             accessibleContext = new AccessibleJProgressBar();
960         }
961         return accessibleContext;
962     }
963
964     /**
965      * This class implements accessibility support for the
966      * <code>JProgressBar</code> class. It provides an implementation of the
967      * Java Accessibility API appropriate to progress bar user-interface
968      * elements.
969      * <p>
970      * <strong>Warning:</strong>
971      * Serialized objects of this class will not be compatible with
972      * future Swing releases. The current serialization support is
973      * appropriate for short term storage or RMI between applications running
974      * the same version of Swing. As of 1.4, support for long term storage
975      * of all JavaBeans<sup><font size="-2">TM</font></sup>
976      * has been added to the <code>java.beans</code> package.
977      * Please see {@link java.beans.XMLEncoder}.
978      */

979     protected class AccessibleJProgressBar extends AccessibleJComponent
980         implements AccessibleValue {
981
982         /**
983          * Gets the state set of this object.
984          *
985          * @return an instance of AccessibleState containing the current state
986          * of the object
987          * @see AccessibleState
988          */

989         public AccessibleStateSet getAccessibleStateSet() {
990             AccessibleStateSet states = super.getAccessibleStateSet();
991             if (getModel().getValueIsAdjusting()) {
992                 states.add(AccessibleState.BUSY);
993             }
994             if (getOrientation() == VERTICAL) {
995                 states.add(AccessibleState.VERTICAL);
996             } else {
997                 states.add(AccessibleState.HORIZONTAL);
998             }
999             return states;
1000        }
1001
1002        /**
1003         * Gets the role of this object.
1004         *
1005         * @return an instance of AccessibleRole describing the role of the
1006         * object
1007         */

1008        public AccessibleRole getAccessibleRole() {
1009            return AccessibleRole.PROGRESS_BAR;
1010        }
1011
1012        /**
1013         * Gets the <code>AccessibleValue</code> associated with this object. In the
1014         * implementation of the Java Accessibility API for this class,
1015     * returns this object, which is responsible for implementing the
1016         * <code>AccessibleValue</code> interface on behalf of itself.
1017     *
1018     * @return this object
1019         */

1020        public AccessibleValue getAccessibleValue() {
1021            return this;
1022        }
1023
1024        /**
1025         * Gets the accessible value of this object.
1026         *
1027         * @return the current value of this object
1028         */

1029        public Number JavaDoc getCurrentAccessibleValue() {
1030            return new Integer JavaDoc(getValue());
1031        }
1032
1033        /**
1034         * Sets the value of this object as a <code>Number</code>.
1035         *
1036         * @return <code>true</code> if the value was set
1037         */

1038        public boolean setCurrentAccessibleValue(Number JavaDoc n) {
1039        // TIGER- 4422535
1040
if (n == null) {
1041        return false;
1042        }
1043        setValue(n.intValue());
1044        return true;
1045        }
1046
1047        /**
1048         * Gets the minimum accessible value of this object.
1049         *
1050         * @return the minimum value of this object
1051         */

1052        public Number JavaDoc getMinimumAccessibleValue() {
1053            return new Integer JavaDoc(getMinimum());
1054        }
1055
1056        /**
1057         * Gets the maximum accessible value of this object.
1058         *
1059         * @return the maximum value of this object
1060         */

1061        public Number JavaDoc getMaximumAccessibleValue() {
1062        // TIGER - 4422362
1063
return new Integer JavaDoc(model.getMaximum() - model.getExtent());
1064        }
1065
1066    } // AccessibleJProgressBar
1067
}
1068
Popular Tags