1 7 8 package javax.swing; 9 10 import java.awt.Color ; 11 import java.awt.Graphics ; 12 13 import java.text.Format ; 14 import java.text.NumberFormat ; 15 16 import java.io.Serializable ; 17 import java.io.ObjectOutputStream ; 18 import java.io.ObjectInputStream ; 19 import java.io.IOException ; 20 21 import javax.swing.event.*; 22 import javax.accessibility.*; 23 import javax.swing.plaf.ProgressBarUI ; 24 25 26 101 public class JProgressBar extends JComponent implements SwingConstants , Accessible 102 { 103 106 private static final String uiClassID = "ProgressBarUI"; 107 108 114 protected int orientation; 115 116 122 protected boolean paintBorder; 123 124 129 protected BoundedRangeModel model; 130 131 138 protected String progressString; 139 140 152 protected boolean paintString; 153 154 157 static final private int defaultMinimum = 0; 158 161 static final private int defaultMaximum = 100; 162 165 static final private int defaultOrientation = HORIZONTAL; 166 167 172 protected transient ChangeEvent changeEvent = null; 173 174 182 protected ChangeListener changeListener = null; 183 184 187 private transient Format format; 188 189 196 private boolean indeterminate; 197 198 199 211 public JProgressBar() 212 { 213 this(defaultOrientation); 214 } 215 216 233 public JProgressBar(int orient) 234 { 235 this(orient, defaultMinimum, defaultMaximum); 236 } 237 238 239 258 public JProgressBar(int min, int max) 259 { 260 this(defaultOrientation, min, max); 261 } 262 263 264 284 public JProgressBar(int orient, int min, int max) 285 { 286 setModel(new DefaultBoundedRangeModel (min, 0, min, max)); 290 updateUI(); 291 292 setOrientation(orient); setBorderPainted(true); setStringPainted(false); setString(null); setIndeterminate(false); } 298 299 300 314 public JProgressBar(BoundedRangeModel newModel) 315 { 316 setModel(newModel); 317 updateUI(); 318 319 setOrientation(defaultOrientation); setBorderPainted(true); setStringPainted(false); setString(null); setIndeterminate(false); } 325 326 327 336 public int getOrientation() { 337 return orientation; 338 } 339 340 341 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 (newOrientation + 379 " is not a legal orientation"); 380 } 381 revalidate(); 382 } 383 } 384 385 386 393 public boolean isStringPainted() { 394 return paintString; 395 } 396 397 398 415 public void setStringPainted(boolean b) { 416 boolean oldValue = paintString; 419 paintString = b; 420 firePropertyChange("stringPainted", oldValue, paintString); 421 if (paintString != oldValue) { 422 revalidate(); 423 repaint(); 424 } 425 } 426 427 428 438 public String 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 (getPercentComplete())); 446 } 447 } 448 449 470 public void setString(String s){ 471 String 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 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 500 public boolean isBorderPainted() { 501 return paintBorder; 502 } 503 504 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 539 protected void paintBorder(Graphics g) { 540 if (isBorderPainted()) { 541 super.paintBorder(g); 542 } 543 } 544 545 546 551 public ProgressBarUI getUI() { 552 return (ProgressBarUI )ui; 553 } 554 555 566 public void setUI(ProgressBarUI ui) { 567 super.setUI(ui); 568 } 569 570 571 576 public void updateUI() { 577 setUI((ProgressBarUI )UIManager.getUI(this)); 578 } 579 580 581 591 public String getUIClassID() { 592 return uiClassID; 593 } 594 595 596 608 private class ModelListener implements ChangeListener, Serializable { 609 public void stateChanged(ChangeEvent e) { 610 fireStateChanged(); 611 } 612 } 613 614 624 protected ChangeListener createChangeListener() { 625 return new ModelListener(); 626 } 627 628 633 public void addChangeListener(ChangeListener l) { 634 listenerList.add(ChangeListener.class, l); 635 } 636 637 642 public void removeChangeListener(ChangeListener l) { 643 listenerList.remove(ChangeListener.class, l); 644 } 645 646 654 public ChangeListener[] getChangeListeners() { 655 return (ChangeListener[])listenerList.getListeners( 656 ChangeListener.class); 657 } 658 659 667 protected void fireStateChanged() { 668 Object [] listeners = listenerList.getListenerList(); 670 for (int i = listeners.length-2; i>=0; i-=2) { 673 if (listeners[i]==ChangeListener.class) { 674 if (changeEvent == null) 676 changeEvent = new ChangeEvent(this); 677 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 678 } 679 } 680 } 681 682 688 public BoundedRangeModel getModel() { 689 return model; 690 } 691 692 701 public void setModel(BoundedRangeModel newModel) { 702 BoundedRangeModel 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 (oldModel.getValue())), 723 (newModel== null 724 ? null : new Integer (newModel.getValue()))); 725 } 726 727 if (model != null) { 728 model.setExtent(0); 729 } 730 repaint(); 731 } 732 } 733 734 735 736 737 748 public int getValue() { return getModel().getValue(); } 749 750 759 public int getMinimum() { return getModel().getMinimum(); } 760 761 770 public int getMaximum() { return getModel().getMaximum(); } 771 772 789 public void setValue(int n) { 790 BoundedRangeModel 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 (oldValue), 798 new Integer (brm.getValue())); 799 } 800 } 801 802 820 public void setMinimum(int n) { getModel().setMinimum(n); } 821 822 839 public void setMaximum(int n) { getModel().setMaximum(n); } 840 841 872 public void setIndeterminate(boolean newValue) { 873 boolean oldValue = indeterminate; 874 indeterminate = newValue; 875 firePropertyChange("indeterminate", oldValue, indeterminate); 876 } 877 878 890 public boolean isIndeterminate() { 891 return indeterminate; 892 } 893 894 895 899 private void writeObject(ObjectOutputStream s) throws IOException { 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 920 protected String paramString() { 921 String orientationString = (orientation == HORIZONTAL ? 922 "HORIZONTAL" : "VERTICAL"); 923 String paintBorderString = (paintBorder ? 924 "true" : "false"); 925 String progressStringString = (progressString != null ? 926 progressString : ""); 927 String paintStringString = (paintString ? 928 "true" : "false"); 929 String 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 944 957 public AccessibleContext getAccessibleContext() { 958 if (accessibleContext == null) { 959 accessibleContext = new AccessibleJProgressBar(); 960 } 961 return accessibleContext; 962 } 963 964 979 protected class AccessibleJProgressBar extends AccessibleJComponent 980 implements AccessibleValue { 981 982 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 1008 public AccessibleRole getAccessibleRole() { 1009 return AccessibleRole.PROGRESS_BAR; 1010 } 1011 1012 1020 public AccessibleValue getAccessibleValue() { 1021 return this; 1022 } 1023 1024 1029 public Number getCurrentAccessibleValue() { 1030 return new Integer (getValue()); 1031 } 1032 1033 1038 public boolean setCurrentAccessibleValue(Number n) { 1039 if (n == null) { 1041 return false; 1042 } 1043 setValue(n.intValue()); 1044 return true; 1045 } 1046 1047 1052 public Number getMinimumAccessibleValue() { 1053 return new Integer (getMinimum()); 1054 } 1055 1056 1061 public Number getMaximumAccessibleValue() { 1062 return new Integer (model.getMaximum() - model.getExtent()); 1064 } 1065 1066 } } 1068 | Popular Tags |