1 7 8 package javax.swing; 9 10 import java.io.Serializable ; 11 import java.awt.Component ; 12 import java.awt.Adjustable ; 13 import java.awt.Dimension ; 14 import java.awt.event.AdjustmentListener ; 15 import java.awt.event.AdjustmentEvent ; 16 import java.awt.Graphics ; 17 18 import javax.swing.event.*; 19 import javax.swing.plaf.*; 20 import javax.accessibility.*; 21 22 import java.io.ObjectOutputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.IOException ; 25 26 27 28 59 public class JScrollBar extends JComponent implements Adjustable , Accessible 60 { 61 65 private static final String uiClassID = "ScrollBarUI"; 66 67 71 private ChangeListener fwdAdjustmentEvents = new ModelListener(); 72 73 74 79 protected BoundedRangeModel model; 80 81 82 85 protected int orientation; 86 87 88 91 protected int unitIncrement; 92 93 94 97 protected int blockIncrement; 98 99 100 private void checkOrientation(int orientation) { 101 switch (orientation) { 102 case VERTICAL: 103 case HORIZONTAL: 104 break; 105 default: 106 throw new IllegalArgumentException ("orientation must be one of: VERTICAL, HORIZONTAL"); 107 } 108 } 109 110 111 130 public JScrollBar(int orientation, int value, int extent, int min, int max) 131 { 132 checkOrientation(orientation); 133 this.unitIncrement = 1; 134 this.blockIncrement = (extent == 0) ? 1 : extent; 135 this.orientation = orientation; 136 this.model = new DefaultBoundedRangeModel (value, extent, min, max); 137 this.model.addChangeListener(fwdAdjustmentEvents); 138 setRequestFocusEnabled(false); 139 updateUI(); 140 } 141 142 143 153 public JScrollBar(int orientation) { 154 this(orientation, 0, 10, 0, 100); 155 } 156 157 158 167 public JScrollBar() { 168 this(VERTICAL); 169 } 170 171 172 184 public void setUI(ScrollBarUI ui) { 185 super.setUI(ui); 186 } 187 188 189 195 public ScrollBarUI getUI() { 196 return (ScrollBarUI)ui; 197 } 198 199 200 204 public void updateUI() { 205 setUI((ScrollBarUI)UIManager.getUI(this)); 206 } 207 208 209 216 public String getUIClassID() { 217 return uiClassID; 218 } 219 220 221 222 229 public int getOrientation() { 230 return orientation; 231 } 232 233 234 248 public void setOrientation(int orientation) 249 { 250 checkOrientation(orientation); 251 int oldValue = this.orientation; 252 this.orientation = orientation; 253 firePropertyChange("orientation", oldValue, orientation); 254 255 if ((oldValue != orientation) && (accessibleContext != null)) { 256 accessibleContext.firePropertyChange( 257 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 258 ((oldValue == VERTICAL) 259 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL), 260 ((orientation == VERTICAL) 261 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL)); 262 } 263 if (orientation != oldValue) { 264 revalidate(); 265 } 266 } 267 268 269 275 public BoundedRangeModel getModel() { 276 return model; 277 } 278 279 280 290 public void setModel(BoundedRangeModel newModel) { 291 Integer oldValue = null; 292 BoundedRangeModel oldModel = model; 293 if (model != null) { 294 model.removeChangeListener(fwdAdjustmentEvents); 295 oldValue = new Integer (model.getValue()); 296 } 297 model = newModel; 298 if (model != null) { 299 model.addChangeListener(fwdAdjustmentEvents); 300 } 301 302 firePropertyChange("model", oldModel, model); 303 304 if (accessibleContext != null) { 305 accessibleContext.firePropertyChange( 306 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 307 oldValue, new Integer (model.getValue())); 308 } 309 } 310 311 312 332 public int getUnitIncrement(int direction) { 333 return unitIncrement; 334 } 335 336 337 348 public void setUnitIncrement(int unitIncrement) { 349 int oldValue = this.unitIncrement; 350 this.unitIncrement = unitIncrement; 351 firePropertyChange("unitIncrement", oldValue, unitIncrement); 352 } 353 354 355 375 public int getBlockIncrement(int direction) { 376 return blockIncrement; 377 } 378 379 380 391 public void setBlockIncrement(int blockIncrement) { 392 int oldValue = this.blockIncrement; 393 this.blockIncrement = blockIncrement; 394 firePropertyChange("blockIncrement", oldValue, blockIncrement); 395 } 396 397 398 403 public int getUnitIncrement() { 404 return unitIncrement; 405 } 406 407 408 413 public int getBlockIncrement() { 414 return blockIncrement; 415 } 416 417 418 423 public int getValue() { 424 return getModel().getValue(); 425 } 426 427 428 438 public void setValue(int value) { 439 BoundedRangeModel m = getModel(); 440 int oldValue = m.getValue(); 441 m.setValue(value); 442 443 if (accessibleContext != null) { 444 accessibleContext.firePropertyChange( 445 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 446 new Integer (oldValue), 447 new Integer (m.getValue())); 448 } 449 } 450 451 452 460 public int getVisibleAmount() { 461 return getModel().getExtent(); 462 } 463 464 465 474 public void setVisibleAmount(int extent) { 475 getModel().setExtent(extent); 476 } 477 478 479 486 public int getMinimum() { 487 return getModel().getMinimum(); 488 } 489 490 491 500 public void setMinimum(int minimum) { 501 getModel().setMinimum(minimum); 502 } 503 504 505 511 public int getMaximum() { 512 return getModel().getMaximum(); 513 } 514 515 516 526 public void setMaximum(int maximum) { 527 getModel().setMaximum(maximum); 528 } 529 530 531 537 public boolean getValueIsAdjusting() { 538 return getModel().getValueIsAdjusting(); 539 } 540 541 542 555 public void setValueIsAdjusting(boolean b) { 556 BoundedRangeModel m = getModel(); 557 boolean oldValue = m.getValueIsAdjusting(); 558 m.setValueIsAdjusting(b); 559 560 if ((oldValue != b) && (accessibleContext != null)) { 561 accessibleContext.firePropertyChange( 562 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 563 ((oldValue) ? AccessibleState.BUSY : null), 564 ((b) ? AccessibleState.BUSY : null)); 565 } 566 } 567 568 569 583 public void setValues(int newValue, int newExtent, int newMin, int newMax) 584 { 585 BoundedRangeModel m = getModel(); 586 int oldValue = m.getValue(); 587 m.setRangeProperties(newValue, newExtent, newMin, newMax, m.getValueIsAdjusting()); 588 589 if (accessibleContext != null) { 590 accessibleContext.firePropertyChange( 591 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 592 new Integer (oldValue), 593 new Integer (m.getValue())); 594 } 595 } 596 597 598 615 public void addAdjustmentListener(AdjustmentListener l) { 616 listenerList.add(AdjustmentListener .class, l); 617 } 618 619 620 626 public void removeAdjustmentListener(AdjustmentListener l) { 627 listenerList.remove(AdjustmentListener .class, l); 628 } 629 630 631 639 public AdjustmentListener [] getAdjustmentListeners() { 640 return (AdjustmentListener [])listenerList.getListeners( 641 AdjustmentListener .class); 642 } 643 644 645 651 protected void fireAdjustmentValueChanged(int id, int type, int value) { 652 fireAdjustmentValueChanged(id, type, value, getValueIsAdjusting()); 653 } 654 655 661 private void fireAdjustmentValueChanged(int id, int type, int value, 662 boolean isAdjusting) { 663 Object [] listeners = listenerList.getListenerList(); 664 AdjustmentEvent e = null; 665 for (int i = listeners.length - 2; i >= 0; i -= 2) { 666 if (listeners[i]==AdjustmentListener .class) { 667 if (e == null) { 668 e = new AdjustmentEvent (this, id, type, value, isAdjusting); 669 } 670 ((AdjustmentListener )listeners[i+1]).adjustmentValueChanged(e); 671 } 672 } 673 } 674 675 676 683 private class ModelListener implements ChangeListener, Serializable { 684 public void stateChanged(ChangeEvent e) { 685 Object obj = e.getSource(); 686 if (obj instanceof BoundedRangeModel ) { 687 int id = AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED; 688 int type = AdjustmentEvent.TRACK; 689 BoundedRangeModel model = (BoundedRangeModel )obj; 690 int value = model.getValue(); 691 boolean isAdjusting = model.getValueIsAdjusting(); 692 fireAdjustmentValueChanged(id, type, value, isAdjusting); 693 } 694 } 695 } 696 697 699 703 public Dimension getMinimumSize() { 704 Dimension pref = getPreferredSize(); 705 if (orientation == VERTICAL) { 706 return new Dimension (pref.width, 5); 707 } else { 708 return new Dimension (5, pref.height); 709 } 710 } 711 712 716 public Dimension getMaximumSize() { 717 Dimension pref = getPreferredSize(); 718 if (getOrientation() == VERTICAL) { 719 return new Dimension (pref.width, Short.MAX_VALUE); 720 } else { 721 return new Dimension (Short.MAX_VALUE, pref.height); 722 } 723 } 724 725 732 public void setEnabled(boolean x) { 733 super.setEnabled(x); 734 Component [] children = getComponents(); 735 for(int i = 0; i < children.length; i++) { 736 children[i].setEnabled(x); 737 } 738 } 739 740 744 private void writeObject(ObjectOutputStream s) throws IOException { 745 s.defaultWriteObject(); 746 if (getUIClassID().equals(uiClassID)) { 747 byte count = JComponent.getWriteObjCounter(this); 748 JComponent.setWriteObjCounter(this, --count); 749 if (count == 0 && ui != null) { 750 ui.installUI(this); 751 } 752 } 753 } 754 755 756 765 protected String paramString() { 766 String orientationString = (orientation == HORIZONTAL ? 767 "HORIZONTAL" : "VERTICAL"); 768 769 return super.paramString() + 770 ",blockIncrement=" + blockIncrement + 771 ",orientation=" + orientationString + 772 ",unitIncrement=" + unitIncrement; 773 } 774 775 779 788 public AccessibleContext getAccessibleContext() { 789 if (accessibleContext == null) { 790 accessibleContext = new AccessibleJScrollBar(); 791 } 792 return accessibleContext; 793 } 794 795 810 protected class AccessibleJScrollBar extends AccessibleJComponent 811 implements AccessibleValue { 812 813 820 public AccessibleStateSet getAccessibleStateSet() { 821 AccessibleStateSet states = super.getAccessibleStateSet(); 822 if (getValueIsAdjusting()) { 823 states.add(AccessibleState.BUSY); 824 } 825 if (getOrientation() == VERTICAL) { 826 states.add(AccessibleState.VERTICAL); 827 } else { 828 states.add(AccessibleState.HORIZONTAL); 829 } 830 return states; 831 } 832 833 839 public AccessibleRole getAccessibleRole() { 840 return AccessibleRole.SCROLL_BAR; 841 } 842 843 851 public AccessibleValue getAccessibleValue() { 852 return this; 853 } 854 855 860 public Number getCurrentAccessibleValue() { 861 return new Integer (getValue()); 862 } 863 864 869 public boolean setCurrentAccessibleValue(Number n) { 870 if (n == null) { 872 return false; 873 } 874 setValue(n.intValue()); 875 return true; 876 } 877 878 883 public Number getMinimumAccessibleValue() { 884 return new Integer (getMinimum()); 885 } 886 887 892 public Number getMaximumAccessibleValue() { 893 return new Integer (model.getMaximum() - model.getExtent()); 895 } 896 897 } } 899 | Popular Tags |