- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathJProgressBar.java
1112 lines (1023 loc) · 38.2 KB
/
JProgressBar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing;
importjava.awt.Graphics;
importjava.beans.BeanProperty;
importjava.beans.JavaBean;
importjava.io.IOException;
importjava.io.ObjectOutputStream;
importjava.io.Serial;
importjava.io.Serializable;
importjava.text.Format;
importjava.text.NumberFormat;
importjavax.accessibility.Accessible;
importjavax.accessibility.AccessibleContext;
importjavax.accessibility.AccessibleRole;
importjavax.accessibility.AccessibleState;
importjavax.accessibility.AccessibleStateSet;
importjavax.accessibility.AccessibleValue;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
importjavax.swing.event.EventListenerList;
importjavax.swing.plaf.ProgressBarUI;
/**
* A component that visually displays the progress of some task. As the task
* progresses towards completion, the progress bar displays the
* task's percentage of completion.
* This percentage is typically represented visually by a rectangle which
* starts out empty and gradually becomes filled in as the task progresses.
* In addition, the progress bar can display a textual representation of this
* percentage.
* <p>
* {@code JProgressBar} uses a {@code BoundedRangeModel} as its data model,
* with the {@code value} property representing the "current" state of the task,
* and the {@code minimum} and {@code maximum} properties representing the
* beginning and end points, respectively.
* <p>
* To indicate that a task of unknown length is executing,
* you can put a progress bar into indeterminate mode.
* While the bar is in indeterminate mode,
* it animates constantly to show that work is occurring.
* As soon as you can determine the task's length and amount of progress,
* you should update the progress bar's value
* and switch it back to determinate mode.
*
* <p>
*
* Here is an example of creating a progress bar,
* where <code>task</code> is an object (representing some piece of work)
* which returns information about the progress of the task:
*
*<pre>
*progressBar = new JProgressBar(0, task.getLengthOfTask());
*progressBar.setValue(0);
*progressBar.setStringPainted(true);
*</pre>
*
* Here is an example of querying the current state of the task, and using
* the returned value to update the progress bar:
*
*<pre>
*progressBar.setValue(task.getCurrent());
*</pre>
*
* Here is an example of putting a progress bar into
* indeterminate mode,
* and then switching back to determinate mode
* once the length of the task is known:
*
*<pre>
*progressBar = new JProgressBar();
*<em>...//when the task of (initially) unknown length begins:</em>
*progressBar.setIndeterminate(true);
*<em>...//do some work; get length of task...</em>
*progressBar.setMaximum(newLength);
*progressBar.setValue(newValue);
*progressBar.setIndeterminate(false);
*</pre>
*
* <p>
*
* For complete examples and further documentation see
* <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html" target="_top">How to Monitor Progress</a>,
* a section in <em>The Java Tutorial.</em>
*
* <p>
* <strong>Warning:</strong> Swing is not thread safe. For more
* information see <a
* href="package-summary.html#threading">Swing's Threading
* Policy</a>.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @see javax.swing.plaf.basic.BasicProgressBarUI
* @see javax.swing.BoundedRangeModel
* @see javax.swing.SwingWorker
*
* @author Michael C. Albers
* @author Kathy Walrath
* @since 1.2
*/
@JavaBean(defaultProperty = "UI", description = "A component that displays an integer value.")
@SwingContainer(false)
@SuppressWarnings("serial") // Same-version serialization only
publicclassJProgressBarextendsJComponentimplementsSwingConstants, Accessible
{
/**
* @see #getUIClassID
*/
privatestaticfinalStringuiClassID = "ProgressBarUI";
/**
* Whether the progress bar is horizontal or vertical.
* The default is <code>HORIZONTAL</code>.
*
* @see #setOrientation
*/
protectedintorientation;
/**
* Whether to display a border around the progress bar.
* The default is <code>true</code>.
*
* @see #setBorderPainted
*/
protectedbooleanpaintBorder;
/**
* The object that holds the data for the progress bar.
*
* @see #setModel
*/
protectedBoundedRangeModelmodel;
/**
* An optional string that can be displayed on the progress bar.
* The default is <code>null</code>. Setting this to a non-<code>null</code>
* value does not imply that the string will be displayed.
* To display the string, {@code paintString} must be {@code true}.
*
* @see #setString
* @see #setStringPainted
*/
protectedStringprogressString;
/**
* Whether to display a string of text on the progress bar.
* The default is <code>false</code>.
* Setting this to <code>true</code> causes a textual
* display of the progress to be rendered on the progress bar. If
* the <code>progressString</code> is <code>null</code>,
* the percentage of completion is displayed on the progress bar.
* Otherwise, the <code>progressString</code> is
* rendered on the progress bar.
*
* @see #setStringPainted
* @see #setString
*/
protectedbooleanpaintString;
/**
* The default minimum for a progress bar is 0.
*/
privatestaticfinalintdefaultMinimum = 0;
/**
* The default maximum for a progress bar is 100.
*/
privatestaticfinalintdefaultMaximum = 100;
/**
* The default orientation for a progress bar is <code>HORIZONTAL</code>.
*/
privatestaticfinalintdefaultOrientation = HORIZONTAL;
/**
* Only one <code>ChangeEvent</code> is needed per instance since the
* event's only interesting property is the immutable source, which
* is the progress bar.
* The event is lazily created the first time that an
* event notification is fired.
*
* @see #fireStateChanged
*/
protectedtransientChangeEventchangeEvent = null;
/**
* Listens for change events sent by the progress bar's model,
* redispatching them
* to change-event listeners registered upon
* this progress bar.
*
* @see #createChangeListener
*/
protectedChangeListenerchangeListener = null;
/**
* Format used when displaying percent complete.
*/
privatetransientFormatformat;
/**
* Whether the progress bar is indeterminate (<code>true</code>) or
* normal (<code>false</code>); the default is <code>false</code>.
*
* @see #setIndeterminate
* @since 1.4
*/
privatebooleanindeterminate;
/**
* Creates a horizontal progress bar
* that displays a border but no progress string.
* The initial and minimum values are 0,
* and the maximum is 100.
*
* @see #setOrientation
* @see #setBorderPainted
* @see #setStringPainted
* @see #setString
* @see #setIndeterminate
*/
publicJProgressBar()
{
this(defaultOrientation);
}
/**
* Creates a progress bar with the specified orientation,
* which can be
* either {@code SwingConstants.VERTICAL} or
* {@code SwingConstants.HORIZONTAL}.
* By default, a border is painted but a progress string is not.
* The initial and minimum values are 0,
* and the maximum is 100.
*
* @param orient the desired orientation of the progress bar
* @throws IllegalArgumentException if {@code orient} is an illegal value
*
* @see #setOrientation
* @see #setBorderPainted
* @see #setStringPainted
* @see #setString
* @see #setIndeterminate
*/
publicJProgressBar(intorient)
{
this(orient, defaultMinimum, defaultMaximum);
}
/**
* Creates a horizontal progress bar
* with the specified minimum and maximum.
* Sets the initial value of the progress bar to the specified minimum.
* By default, a border is painted but a progress string is not.
* <p>
* The <code>BoundedRangeModel</code> that holds the progress bar's data
* handles any issues that may arise from improperly setting the
* minimum, initial, and maximum values on the progress bar.
* See the {@code BoundedRangeModel} documentation for details.
*
* @param min the minimum value of the progress bar
* @param max the maximum value of the progress bar
*
* @see BoundedRangeModel
* @see #setOrientation
* @see #setBorderPainted
* @see #setStringPainted
* @see #setString
* @see #setIndeterminate
*/
publicJProgressBar(intmin, intmax)
{
this(defaultOrientation, min, max);
}
/**
* Creates a progress bar using the specified orientation,
* minimum, and maximum.
* By default, a border is painted but a progress string is not.
* Sets the initial value of the progress bar to the specified minimum.
* <p>
* The <code>BoundedRangeModel</code> that holds the progress bar's data
* handles any issues that may arise from improperly setting the
* minimum, initial, and maximum values on the progress bar.
* See the {@code BoundedRangeModel} documentation for details.
*
* @param orient the desired orientation of the progress bar
* @param min the minimum value of the progress bar
* @param max the maximum value of the progress bar
* @throws IllegalArgumentException if {@code orient} is an illegal value
*
* @see BoundedRangeModel
* @see #setOrientation
* @see #setBorderPainted
* @see #setStringPainted
* @see #setString
* @see #setIndeterminate
*/
publicJProgressBar(intorient, intmin, intmax)
{
// Creating the model this way is a bit simplistic, but
// I believe that it is the most common usage of this
// component - it's what people will expect.
setModel(newDefaultBoundedRangeModel(min, 0, min, max));
updateUI();
setOrientation(orient); // documented with set/getOrientation()
setBorderPainted(true); // documented with is/setBorderPainted()
setStringPainted(false); // see setStringPainted
setString(null); // see getString
setIndeterminate(false); // see setIndeterminate
}
/**
* Creates a horizontal progress bar
* that uses the specified model
* to hold the progress bar's data.
* By default, a border is painted but a progress string is not.
*
* @param newModel the data model for the progress bar
*
* @see #setOrientation
* @see #setBorderPainted
* @see #setStringPainted
* @see #setString
* @see #setIndeterminate
*/
publicJProgressBar(BoundedRangeModelnewModel)
{
setModel(newModel);
updateUI();
setOrientation(defaultOrientation); // see setOrientation()
setBorderPainted(true); // see setBorderPainted()
setStringPainted(false); // see setStringPainted
setString(null); // see getString
setIndeterminate(false); // see setIndeterminate
}
/**
* Returns {@code SwingConstants.VERTICAL} or
* {@code SwingConstants.HORIZONTAL}, depending on the orientation
* of the progress bar. The default orientation is
* {@code SwingConstants.HORIZONTAL}.
*
* @return <code>HORIZONTAL</code> or <code>VERTICAL</code>
* @see #setOrientation
*/
publicintgetOrientation() {
returnorientation;
}
/**
* Sets the progress bar's orientation to <code>newOrientation</code>,
* which must be {@code SwingConstants.VERTICAL} or
* {@code SwingConstants.HORIZONTAL}. The default orientation
* is {@code SwingConstants.HORIZONTAL}.
*
* @param newOrientation <code>HORIZONTAL</code> or <code>VERTICAL</code>
* @throws IllegalArgumentException if <code>newOrientation</code>
* is an illegal value
* @see #getOrientation
*/
@BeanProperty(preferred = true, visualUpdate = true, description
= "Set the progress bar's orientation.")
publicvoidsetOrientation(intnewOrientation) {
if (orientation != newOrientation) {
switch (newOrientation) {
caseVERTICAL:
caseHORIZONTAL:
intoldOrientation = orientation;
orientation = newOrientation;
firePropertyChange("orientation", oldOrientation, newOrientation);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
((oldOrientation == VERTICAL)
? AccessibleState.VERTICAL
: AccessibleState.HORIZONTAL),
((orientation == VERTICAL)
? AccessibleState.VERTICAL
: AccessibleState.HORIZONTAL));
}
break;
default:
thrownewIllegalArgumentException(newOrientation +
" is not a legal orientation");
}
revalidate();
}
}
/**
* Returns the value of the <code>stringPainted</code> property.
*
* @return the value of the <code>stringPainted</code> property
* @see #setStringPainted
* @see #setString
*/
publicbooleanisStringPainted() {
returnpaintString;
}
/**
* Sets the value of the <code>stringPainted</code> property,
* which determines whether the progress bar
* should render a progress string.
* The default is <code>false</code>, meaning
* no string is painted.
* Some look and feels might not support progress strings
* or might support them only when the progress bar is in determinate mode.
*
* @param b <code>true</code> if the progress bar should render a string
* @see #isStringPainted
* @see #setString
*/
@BeanProperty(visualUpdate = true, description
= "Whether the progress bar should render a string.")
publicvoidsetStringPainted(booleanb) {
//PENDING: specify that string not painted when in indeterminate mode?
// or just leave that to the L&F?
booleanoldValue = paintString;
paintString = b;
firePropertyChange("stringPainted", oldValue, paintString);
if (paintString != oldValue) {
revalidate();
repaint();
}
}
/**
* Returns a {@code String} representation of the current progress.
* By default, this returns a simple percentage {@code String} based on
* the value returned from {@code getPercentComplete}. An example
* would be the "42%". You can change this by calling {@code setString}.
*
* @return the value of the progress string, or a simple percentage string
* if the progress string is {@code null}
* @see #setString
*/
publicStringgetString(){
if (progressString != null) {
returnprogressString;
} else {
if (format == null) {
format = NumberFormat.getPercentInstance();
}
returnformat.format(Double.valueOf(getPercentComplete()));
}
}
/**
* Sets the value of the progress string. By default,
* this string is <code>null</code>, implying the built-in behavior of
* using a simple percent string.
* If you have provided a custom progress string and want to revert to
* the built-in behavior, set the string back to <code>null</code>.
* <p>
* The progress string is painted only if
* the <code>isStringPainted</code> method returns <code>true</code>.
*
* @param s the value of the progress string
* @see #getString
* @see #setStringPainted
* @see #isStringPainted
*/
@BeanProperty(visualUpdate = true, description
= "Specifies the progress string to paint")
publicvoidsetString(Strings){
StringoldValue = progressString;
progressString = s;
firePropertyChange("string", oldValue, progressString);
if (progressString == null || oldValue == null || !progressString.equals(oldValue)) {
repaint();
}
}
/**
* Returns the percent complete for the progress bar.
* Note that this number is between 0.0 and 1.0.
*
* @return the percent complete for this progress bar
*/
@BeanProperty(bound = false)
publicdoublegetPercentComplete() {
longspan = model.getMaximum() - model.getMinimum();
doublecurrentValue = model.getValue();
doublepc = (currentValue - model.getMinimum()) / span;
returnpc;
}
/**
* Returns the <code>borderPainted</code> property.
*
* @return the value of the <code>borderPainted</code> property
* @see #setBorderPainted
*/
publicbooleanisBorderPainted() {
returnpaintBorder;
}
/**
* Sets the <code>borderPainted</code> property, which is
* <code>true</code> if the progress bar should paint its border.
* The default value for this property is <code>true</code>.
* Some look and feels might not implement painted borders;
* they will ignore this property.
*
* @param b <code>true</code> if the progress bar
* should paint its border;
* otherwise, <code>false</code>
* @see #isBorderPainted
*/
@BeanProperty(visualUpdate = true, description
= "Whether the progress bar should paint its border.")
publicvoidsetBorderPainted(booleanb) {
booleanoldValue = paintBorder;
paintBorder = b;
firePropertyChange("borderPainted", oldValue, paintBorder);
if (paintBorder != oldValue) {
repaint();
}
}
/**
* Paints the progress bar's border if the <code>borderPainted</code>
* property is <code>true</code>.
*
* @param g the <code>Graphics</code> context within which to paint the border
* @see #paint
* @see #setBorder
* @see #isBorderPainted
* @see #setBorderPainted
*/
protectedvoidpaintBorder(Graphicsg) {
if (isBorderPainted()) {
super.paintBorder(g);
}
}
/**
* Returns the look-and-feel object that renders this component.
*
* @return the <code>ProgressBarUI</code> object that renders this component
*/
publicProgressBarUIgetUI() {
return (ProgressBarUI)ui;
}
/**
* Sets the look-and-feel object that renders this component.
*
* @param ui a <code>ProgressBarUI</code> object
* @see UIDefaults#getUI
*/
@BeanProperty(hidden = true, visualUpdate = true, description
= "The UI object that implements the Component's LookAndFeel.")
publicvoidsetUI(ProgressBarUIui) {
super.setUI(ui);
}
/**
* Resets the UI property to a value from the current look and feel.
*
* @see JComponent#updateUI
*/
publicvoidupdateUI() {
setUI((ProgressBarUI)UIManager.getUI(this));
}
/**
* Returns the name of the look-and-feel class that renders this component.
*
* @return the string "ProgressBarUI"
* @see JComponent#getUIClassID
* @see UIDefaults#getUI
*/
@BeanProperty(bound = false, expert = true, description
= "A string that specifies the name of the look-and-feel class.")
publicStringgetUIClassID() {
returnuiClassID;
}
/* We pass each Change event to the listeners with the
* the progress bar as the event source.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
@SuppressWarnings("serial") // Same-version serialization only
privateclassModelListenerimplementsChangeListener, Serializable {
publicvoidstateChanged(ChangeEvente) {
fireStateChanged();
}
}
/**
* Subclasses that want to handle change events
* from the model differently
* can override this to return
* an instance of a custom <code>ChangeListener</code> implementation.
* The default {@code ChangeListener} simply calls the
* {@code fireStateChanged} method to forward {@code ChangeEvent}s
* to the {@code ChangeListener}s that have been added directly to the
* progress bar.
*
* @return the instance of a custom {@code ChangeListener} implementation.
* @see #changeListener
* @see #fireStateChanged
* @see javax.swing.event.ChangeListener
* @see javax.swing.BoundedRangeModel
*/
protectedChangeListenercreateChangeListener() {
returnnewModelListener();
}
/**
* Adds the specified <code>ChangeListener</code> to the progress bar.
*
* @param l the <code>ChangeListener</code> to add
*/
publicvoidaddChangeListener(ChangeListenerl) {
listenerList.add(ChangeListener.class, l);
}
/**
* Removes a <code>ChangeListener</code> from the progress bar.
*
* @param l the <code>ChangeListener</code> to remove
*/
publicvoidremoveChangeListener(ChangeListenerl) {
listenerList.remove(ChangeListener.class, l);
}
/**
* Returns an array of all the <code>ChangeListener</code>s added
* to this progress bar with <code>addChangeListener</code>.
*
* @return all of the <code>ChangeListener</code>s added or an empty
* array if no listeners have been added
* @since 1.4
*/
@BeanProperty(bound = false)
publicChangeListener[] getChangeListeners() {
returnlistenerList.getListeners(ChangeListener.class);
}
/**
* Send a {@code ChangeEvent}, whose source is this {@code JProgressBar}, to
* all {@code ChangeListener}s that have registered interest in
* {@code ChangeEvent}s.
* This method is called each time a {@code ChangeEvent} is received from
* the model.
* <p>
*
* The event instance is created if necessary, and stored in
* {@code changeEvent}.
*
* @see #addChangeListener
* @see EventListenerList
*/
protectedvoidfireStateChanged() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (inti = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ChangeListener.class) {
// Lazily create the event:
if (changeEvent == null)
changeEvent = newChangeEvent(this);
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
}
/**
* Returns the data model used by this progress bar.
*
* @return the <code>BoundedRangeModel</code> currently in use
* @see #setModel
* @see BoundedRangeModel
*/
publicBoundedRangeModelgetModel() {
returnmodel;
}
/**
* Sets the data model used by the <code>JProgressBar</code>.
* Note that the {@code BoundedRangeModel}'s {@code extent} is not used,
* and is set to {@code 0}.
*
* @param newModel the <code>BoundedRangeModel</code> to use
*/
@BeanProperty(bound = false, expert = true, description
= "The data model used by the JProgressBar.")
publicvoidsetModel(BoundedRangeModelnewModel) {
// PENDING(???) setting the same model to multiple bars is broken; listeners
BoundedRangeModeloldModel = getModel();
if (newModel != oldModel) {
if (oldModel != null) {
oldModel.removeChangeListener(changeListener);
changeListener = null;
}
model = newModel;
if (newModel != null) {
changeListener = createChangeListener();
newModel.addChangeListener(changeListener);
}
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
(oldModel== null
? null : Integer.valueOf(oldModel.getValue())),
(newModel== null
? null : Integer.valueOf(newModel.getValue())));
}
if (model != null) {
model.setExtent(0);
}
repaint();
}
}
/* All of the model methods are implemented by delegation. */
/**
* Returns the progress bar's current {@code value}
* from the <code>BoundedRangeModel</code>.
* The value is always between the
* minimum and maximum values, inclusive.
*
* @return the current value of the progress bar
* @see #setValue
* @see BoundedRangeModel#getValue
*/
publicintgetValue() { returngetModel().getValue(); }
/**
* Returns the progress bar's {@code minimum} value
* from the <code>BoundedRangeModel</code>.
*
* @return the progress bar's minimum value
* @see #setMinimum
* @see BoundedRangeModel#getMinimum
*/
publicintgetMinimum() { returngetModel().getMinimum(); }
/**
* Returns the progress bar's {@code maximum} value
* from the <code>BoundedRangeModel</code>.
*
* @return the progress bar's maximum value
* @see #setMaximum
* @see BoundedRangeModel#getMaximum
*/
publicintgetMaximum() { returngetModel().getMaximum(); }
/**
* Sets the progress bar's current value to {@code n}. This method
* forwards the new value to the model.
* <p>
* The data model (an instance of {@code BoundedRangeModel})
* handles any mathematical
* issues arising from assigning faulty values. See the
* {@code BoundedRangeModel} documentation for details.
* <p>
* If the new value is different from the previous value,
* all change listeners are notified.
*
* @param n the new value
* @see #getValue
* @see #addChangeListener
* @see BoundedRangeModel#setValue
*/
@BeanProperty(bound = false, preferred = true, description
= "The progress bar's current value.")
publicvoidsetValue(intn) {
BoundedRangeModelbrm = getModel();
intoldValue = brm.getValue();
brm.setValue(n);
if (accessibleContext != null) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VALUE_PROPERTY,
Integer.valueOf(oldValue),
Integer.valueOf(brm.getValue()));
}
}
/**
* Sets the progress bar's minimum value
* (stored in the progress bar's data model) to <code>n</code>.
* <p>
* The data model (a <code>BoundedRangeModel</code> instance)
* handles any mathematical
* issues arising from assigning faulty values.
* See the {@code BoundedRangeModel} documentation for details.
* <p>
* If the minimum value is different from the previous minimum,
* all change listeners are notified.
*
* @param n the new minimum
* @see #getMinimum
* @see #addChangeListener
* @see BoundedRangeModel#setMinimum
*/
@BeanProperty(bound = false, preferred = true, description
= "The progress bar's minimum value.")
publicvoidsetMinimum(intn) { getModel().setMinimum(n); }
/**
* Sets the progress bar's maximum value
* (stored in the progress bar's data model) to <code>n</code>.
* <p>
* The underlying <code>BoundedRangeModel</code> handles any mathematical
* issues arising from assigning faulty values.
* See the {@code BoundedRangeModel} documentation for details.
* <p>
* If the maximum value is different from the previous maximum,
* all change listeners are notified.
*
* @param n the new maximum
* @see #getMaximum
* @see #addChangeListener
* @see BoundedRangeModel#setMaximum
*/
@BeanProperty(bound = false, preferred = true, description
= "The progress bar's maximum value.")
publicvoidsetMaximum(intn) { getModel().setMaximum(n); }
/**
* Sets the <code>indeterminate</code> property of the progress bar,
* which determines whether the progress bar is in determinate
* or indeterminate mode.
* An indeterminate progress bar continuously displays animation
* indicating that an operation of unknown length is occurring.
* By default, this property is <code>false</code>.
* Some look and feels might not support indeterminate progress bars;
* they will ignore this property.
*
* <p>
*
* See
* <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html" target="_top">How to Monitor Progress</a>
* for examples of using indeterminate progress bars.
*
* @param newValue <code>true</code> if the progress bar
* should change to indeterminate mode;
* <code>false</code> if it should revert to normal.
*
* @see #isIndeterminate
* @see javax.swing.plaf.basic.BasicProgressBarUI
*
* @since 1.4
*/
publicvoidsetIndeterminate(booleannewValue) {
booleanoldValue = indeterminate;
indeterminate = newValue;
firePropertyChange("indeterminate", oldValue, indeterminate);
}
/**
* Returns the value of the <code>indeterminate</code> property.
*
* @return the value of the <code>indeterminate</code> property
* @see #setIndeterminate
*
* @since 1.4
*/
@BeanProperty(bound = false, description
= "Is the progress bar indeterminate (true) or normal (false)?")
publicbooleanisIndeterminate() {
returnindeterminate;
}
/**
* See readObject() and writeObject() in JComponent for more
* information about serialization in Swing.
*/
@Serial
privatevoidwriteObject(ObjectOutputStreams) throwsIOException {
s.defaultWriteObject();
if (getUIClassID().equals(uiClassID)) {
bytecount = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
}
/**
* Returns a string representation of this <code>JProgressBar</code>.
* This method is intended to be used only for debugging purposes. The
* content and format of the returned string may vary between
* implementations. The returned string may be empty but may not
* be <code>null</code>.
*
* @return a string representation of this <code>JProgressBar</code>
*/
protectedStringparamString() {
StringorientationString = (orientation == HORIZONTAL ?
"HORIZONTAL" : "VERTICAL");
StringpaintBorderString = (paintBorder ?
"true" : "false");
StringprogressStringString = (progressString != null ?
progressString : "");
StringpaintStringString = (paintString ?
"true" : "false");
StringindeterminateString = (indeterminate ?
"true" : "false");
returnsuper.paramString() +
",orientation=" + orientationString +
",paintBorder=" + paintBorderString +
",paintString=" + paintStringString +
",progressString=" + progressStringString +
",indeterminateString=" + indeterminateString;
}
/////////////////
// Accessibility support
////////////////
/**
* Gets the <code>AccessibleContext</code> associated with this
* <code>JProgressBar</code>. For progress bars, the
* <code>AccessibleContext</code> takes the form of an
* <code>AccessibleJProgressBar</code>.
* A new <code>AccessibleJProgressBar</code> instance is created if necessary.
*
* @return an <code>AccessibleJProgressBar</code> that serves as the
* <code>AccessibleContext</code> of this <code>JProgressBar</code>
*/
@BeanProperty(bound = false, expert = true, description
= "The AccessibleContext associated with this ProgressBar.")
publicAccessibleContextgetAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = newAccessibleJProgressBar();
}
returnaccessibleContext;