1 7 8 package javax.swing; 9 10 import javax.swing.border.*; 11 import javax.swing.event.*; 12 import javax.swing.plaf.*; 13 import javax.accessibility.*; 14 15 import java.io.Serializable ; 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectInputStream ; 18 import java.io.IOException ; 19 20 import java.util.*; 21 import java.beans.*; 22 23 24 52 public class JSlider extends JComponent implements SwingConstants , Accessible { 53 57 private static final String uiClassID = "SliderUI"; 58 59 private boolean paintTicks = false; 60 private boolean paintTrack = true; 61 private boolean paintLabels = false; 62 private boolean isInverted = false; 63 64 68 protected BoundedRangeModel sliderModel; 69 70 74 protected int majorTickSpacing; 75 76 81 protected int minorTickSpacing; 82 83 89 protected boolean snapToTicks = false; 90 91 96 boolean snapToValue = true; 97 98 101 protected int orientation; 102 103 104 106 private Dictionary labelTable; 107 108 109 117 protected ChangeListener changeListener = createChangeListener(); 118 119 120 128 protected transient ChangeEvent changeEvent = null; 129 130 131 private void checkOrientation(int orientation) { 132 switch (orientation) { 133 case VERTICAL: 134 case HORIZONTAL: 135 break; 136 default: 137 throw new IllegalArgumentException ("orientation must be one of: VERTICAL, HORIZONTAL"); 138 } 139 } 140 141 142 146 public JSlider() { 147 this(HORIZONTAL, 0, 100, 50); 148 } 149 150 151 155 public JSlider(int orientation) { 156 this(orientation, 0, 100, 50); 157 } 158 159 160 164 public JSlider(int min, int max) { 165 this(HORIZONTAL, min, max, (min + max) / 2); 166 } 167 168 169 172 public JSlider(int min, int max, int value) { 173 this(HORIZONTAL, min, max, value); 174 } 175 176 177 188 public JSlider(int orientation, int min, int max, int value) 189 { 190 checkOrientation(orientation); 191 this.orientation = orientation; 192 sliderModel = new DefaultBoundedRangeModel (value, 0, min, max); 193 sliderModel.addChangeListener(changeListener); 194 updateUI(); 195 } 196 197 198 202 public JSlider(BoundedRangeModel brm) 203 { 204 this.orientation = JSlider.HORIZONTAL; 205 setModel(brm); 206 sliderModel.addChangeListener(changeListener); 207 updateUI(); 208 } 209 210 211 216 public SliderUI getUI() { 217 return(SliderUI)ui; 218 } 219 220 221 232 public void setUI(SliderUI ui) { 233 super.setUI(ui); 234 } 235 236 237 242 public void updateUI() { 243 updateLabelUIs(); 244 setUI((SliderUI)UIManager.getUI(this)); 245 } 246 247 248 255 public String getUIClassID() { 256 return uiClassID; 257 } 258 259 260 264 private class ModelListener implements ChangeListener, Serializable { 265 public void stateChanged(ChangeEvent e) { 266 fireStateChanged(); 267 } 268 } 269 270 271 279 protected ChangeListener createChangeListener() { 280 return new ModelListener(); 281 } 282 283 284 291 public void addChangeListener(ChangeListener l) { 292 listenerList.add(ChangeListener.class, l); 293 } 294 295 296 304 public void removeChangeListener(ChangeListener l) { 305 listenerList.remove(ChangeListener.class, l); 306 } 307 308 309 317 public ChangeListener[] getChangeListeners() { 318 return (ChangeListener[])listenerList.getListeners( 319 ChangeListener.class); 320 } 321 322 323 331 protected void fireStateChanged() { 332 Object [] listeners = listenerList.getListenerList(); 333 for (int i = listeners.length - 2; i >= 0; i -= 2) { 334 if (listeners[i]==ChangeListener.class) { 335 if (changeEvent == null) { 336 changeEvent = new ChangeEvent(this); 337 } 338 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 339 } 340 } 341 } 342 343 344 350 public BoundedRangeModel getModel() { 351 return sliderModel; 352 } 353 354 355 364 public void setModel(BoundedRangeModel newModel) 365 { 366 BoundedRangeModel oldModel = getModel(); 367 368 if (oldModel != null) { 369 oldModel.removeChangeListener(changeListener); 370 } 371 372 sliderModel = newModel; 373 374 if (newModel != null) { 375 newModel.addChangeListener(changeListener); 376 377 if (accessibleContext != null) { 378 accessibleContext.firePropertyChange( 379 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 380 (oldModel == null 381 ? null : new Integer (oldModel.getValue())), 382 (newModel == null 383 ? null : new Integer (newModel.getValue()))); 384 } 385 } 386 387 firePropertyChange("model", oldModel, sliderModel); 388 } 389 390 391 396 public int getValue() { 397 return getModel().getValue(); 398 } 399 400 401 410 public void setValue(int n) { 411 BoundedRangeModel m = getModel(); 412 int oldValue = m.getValue(); 413 if (oldValue == n) { 414 return; 415 } 416 m.setValue(n); 417 418 if (accessibleContext != null) { 419 accessibleContext.firePropertyChange( 420 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 421 new Integer (oldValue), 422 new Integer (m.getValue())); 423 } 424 } 425 426 427 433 public int getMinimum() { 434 return getModel().getMinimum(); 435 } 436 437 438 448 public void setMinimum(int minimum) { 449 int oldMin = getModel().getMinimum(); 450 getModel().setMinimum(minimum); 451 firePropertyChange( "minimum", new Integer ( oldMin ), new Integer ( minimum ) ); 452 } 453 454 455 461 public int getMaximum() { 462 return getModel().getMaximum(); 463 } 464 465 466 476 public void setMaximum(int maximum) { 477 int oldMax = getModel().getMaximum(); 478 getModel().setMaximum(maximum); 479 firePropertyChange( "maximum", new Integer ( oldMax ), new Integer ( maximum ) ); 480 } 481 482 483 489 public boolean getValueIsAdjusting() { 490 return getModel().getValueIsAdjusting(); 491 } 492 493 494 507 public void setValueIsAdjusting(boolean b) { 508 BoundedRangeModel m = getModel(); 509 boolean oldValue = m.getValueIsAdjusting(); 510 m.setValueIsAdjusting(b); 511 512 if ((oldValue != b) && (accessibleContext != null)) { 513 accessibleContext.firePropertyChange( 514 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 515 ((oldValue) ? AccessibleState.BUSY : null), 516 ((b) ? AccessibleState.BUSY : null)); 517 } 518 } 519 520 521 527 public int getExtent() { 528 return getModel().getExtent(); 529 } 530 531 532 543 public void setExtent(int extent) { 544 getModel().setExtent(extent); 545 } 546 547 548 553 public int getOrientation() { 554 return orientation; 555 } 556 557 558 572 public void setOrientation(int orientation) 573 { 574 checkOrientation(orientation); 575 int oldValue = this.orientation; 576 this.orientation = orientation; 577 firePropertyChange("orientation", oldValue, orientation); 578 579 if ((oldValue != orientation) && (accessibleContext != null)) { 580 accessibleContext.firePropertyChange( 581 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 582 ((oldValue == VERTICAL) 583 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL), 584 ((orientation == VERTICAL) 585 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL)); 586 } 587 if (orientation != oldValue) { 588 revalidate(); 589 } 590 } 591 592 593 594 600 public Dictionary getLabelTable() { 601 606 return labelTable; 607 } 608 609 610 623 public void setLabelTable( Dictionary labels ) { 624 Dictionary oldTable = labelTable; 625 labelTable = labels; 626 updateLabelUIs(); 627 firePropertyChange("labelTable", oldTable, labelTable ); 628 if (labels != oldTable) { 629 revalidate(); 630 repaint(); 631 } 632 } 633 634 635 640 protected void updateLabelUIs() { 641 if ( getLabelTable() == null ) { 642 return; 643 } 644 Enumeration labels = getLabelTable().keys(); 645 while ( labels.hasMoreElements() ) { 646 Object value = getLabelTable().get( labels.nextElement() ); 647 if ( value instanceof JComponent ) { 648 JComponent component = (JComponent )value; 649 component.updateUI(); 650 component.setSize( component.getPreferredSize() ); 651 } 652 } 653 } 654 655 656 664 public Hashtable createStandardLabels( int increment ) { 665 return createStandardLabels( increment, getMinimum() ); 666 } 667 668 669 680 public Hashtable createStandardLabels( int increment, int start ) { 681 if ( start > getMaximum() || start < getMinimum() ) { 682 throw new IllegalArgumentException ( "Slider label start point out of range." ); 683 } 684 685 if ( increment <= 0 ) { 686 throw new IllegalArgumentException ( "Label incremement must be > 0" ); 687 } 688 689 class SmartHashtable extends Hashtable implements PropertyChangeListener { 690 int increment = 0; 691 int start = 0; 692 boolean startAtMin = false; 693 694 class LabelUIResource extends JLabel implements UIResource { 695 public LabelUIResource( String text, int alignment ) { 696 super( text, alignment ); 697 setName("Slider.label"); 698 } 699 } 700 701 public SmartHashtable( int increment, int start ) { 702 super(); 703 this.increment = increment; 704 this.start = start; 705 startAtMin = start == getMinimum(); 706 createLabels(); 707 } 708 709 public void propertyChange( PropertyChangeEvent e ) { 710 if ( e.getPropertyName().equals( "minimum" ) && startAtMin ) { 711 start = getMinimum(); 712 } 713 714 if ( e.getPropertyName().equals( "minimum" ) || 715 e.getPropertyName().equals( "maximum" ) ) { 716 717 Enumeration keys = getLabelTable().keys(); 718 Object key = null; 719 Hashtable hashtable = new Hashtable(); 720 721 while ( keys.hasMoreElements() ) { 723 key = keys.nextElement(); 724 Object value = getLabelTable().get( key ); 725 if ( !(value instanceof LabelUIResource) ) { 726 hashtable.put( key, value ); 727 } 728 } 729 730 clear(); 731 createLabels(); 732 733 keys = hashtable.keys(); 735 while ( keys.hasMoreElements() ) { 736 key = keys.nextElement(); 737 put( key, hashtable.get( key ) ); 738 } 739 740 ((JSlider )e.getSource()).setLabelTable( this ); 741 } 742 } 743 744 void createLabels() { 745 for ( int labelIndex = start; labelIndex <= getMaximum(); labelIndex += increment ) { 746 put( new Integer ( labelIndex ), new LabelUIResource( ""+labelIndex, JLabel.CENTER ) ); 747 } 748 } 749 } 750 751 SmartHashtable table = new SmartHashtable( increment, start ); 752 753 if ( getLabelTable() != null && (getLabelTable() instanceof PropertyChangeListener) ) { 754 removePropertyChangeListener( (PropertyChangeListener)getLabelTable() ); 755 } 756 757 addPropertyChangeListener( table ); 758 759 return table; 760 } 761 762 763 769 public boolean getInverted() { 770 return isInverted; 771 } 772 773 774 792 public void setInverted( boolean b ) { 793 boolean oldValue = isInverted; 794 isInverted = b; 795 firePropertyChange("inverted", oldValue, isInverted); 796 if (b != oldValue) { 797 repaint(); 798 } 799 } 800 801 802 812 public int getMajorTickSpacing() { 813 return majorTickSpacing; 814 } 815 816 817 831 public void setMajorTickSpacing(int n) { 832 int oldValue = majorTickSpacing; 833 majorTickSpacing = n; 834 if ( labelTable == null && getMajorTickSpacing() > 0 && getPaintLabels() ) { 835 setLabelTable( createStandardLabels( getMajorTickSpacing() ) ); 836 } 837 firePropertyChange("majorTickSpacing", oldValue, majorTickSpacing); 838 if (majorTickSpacing != oldValue && getPaintTicks()) { 839 repaint(); 840 } 841 } 842 843 844 845 855 public int getMinorTickSpacing() { 856 return minorTickSpacing; 857 } 858 859 860 873 public void setMinorTickSpacing(int n) { 874 int oldValue = minorTickSpacing; 875 minorTickSpacing = n; 876 firePropertyChange("minorTickSpacing", oldValue, minorTickSpacing); 877 if (minorTickSpacing != oldValue && getPaintTicks()) { 878 repaint(); 879 } 880 } 881 882 883 891 public boolean getSnapToTicks() { 892 return snapToTicks; 893 } 894 895 896 903 boolean getSnapToValue() { 904 return snapToValue; 905 } 906 907 908 919 public void setSnapToTicks(boolean b) { 920 boolean oldValue = snapToTicks; 921 snapToTicks = b; 922 firePropertyChange("snapToTicks", oldValue, snapToTicks); 923 } 924 925 926 939 void setSnapToValue(boolean b) { 940 boolean oldValue = snapToValue; 941 snapToValue = b; 942 firePropertyChange("snapToValue", oldValue, snapToValue); 943 } 944 945 946 951 public boolean getPaintTicks() { 952 return paintTicks; 953 } 954 955 956 964 public void setPaintTicks(boolean b) { 965 boolean oldValue = paintTicks; 966 paintTicks = b; 967 firePropertyChange("paintTicks", oldValue, paintTicks); 968 if (paintTicks != oldValue) { 969 revalidate(); 970 repaint(); 971 } 972 } 973 974 979 public boolean getPaintTrack() { 980 return paintTrack; 981 } 982 983 984 992 public void setPaintTrack(boolean b) { 993 boolean oldValue = paintTrack; 994 paintTrack = b; 995 firePropertyChange("paintTrack", oldValue, paintTrack); 996 if (paintTrack != oldValue) { 997 repaint(); 998 } 999 } 1000 1001 1002 1007 public boolean getPaintLabels() { 1008 return paintLabels; 1009 } 1010 1011 1012 1020 public void setPaintLabels(boolean b) { 1021 boolean oldValue = paintLabels; 1022 paintLabels = b; 1023 if ( labelTable == null && getMajorTickSpacing() > 0 ) { 1024 setLabelTable( createStandardLabels( getMajorTickSpacing() ) ); 1025 } 1026 firePropertyChange("paintLabels", oldValue, paintLabels); 1027 if (paintLabels != oldValue) { 1028 revalidate(); 1029 repaint(); 1030 } 1031 } 1032 1033 1034 1038 private void writeObject(ObjectOutputStream s) throws IOException { 1039 s.defaultWriteObject(); 1040 if (getUIClassID().equals(uiClassID)) { 1041 byte count = JComponent.getWriteObjCounter(this); 1042 JComponent.setWriteObjCounter(this, --count); 1043 if (count == 0 && ui != null) { 1044 ui.installUI(this); 1045 } 1046 } 1047 } 1048 1049 1050 1059 protected String paramString() { 1060 String paintTicksString = (paintTicks ? 1061 "true" : "false"); 1062 String paintTrackString = (paintTrack ? 1063 "true" : "false"); 1064 String paintLabelsString = (paintLabels ? 1065 "true" : "false"); 1066 String isInvertedString = (isInverted ? 1067 "true" : "false"); 1068 String snapToTicksString = (snapToTicks ? 1069 "true" : "false"); 1070 String snapToValueString = (snapToValue ? 1071 "true" : "false"); 1072 String orientationString = (orientation == HORIZONTAL ? 1073 "HORIZONTAL" : "VERTICAL"); 1074 1075 return super.paramString() + 1076 ",isInverted=" + isInvertedString + 1077 ",majorTickSpacing=" + majorTickSpacing + 1078 ",minorTickSpacing=" + minorTickSpacing + 1079 ",orientation=" + orientationString + 1080 ",paintLabels=" + paintLabelsString + 1081 ",paintTicks=" + paintTicksString + 1082 ",paintTrack=" + paintTrackString + 1083 ",snapToTicks=" + snapToTicksString + 1084 ",snapToValue=" + snapToValueString; 1085 } 1086 1087 1088 1092 1101 public AccessibleContext getAccessibleContext() { 1102 if (accessibleContext == null) { 1103 accessibleContext = new AccessibleJSlider(); 1104 } 1105 return accessibleContext; 1106 } 1107 1108 1122 protected class AccessibleJSlider extends AccessibleJComponent 1123 implements AccessibleValue { 1124 1125 1132 public AccessibleStateSet getAccessibleStateSet() { 1133 AccessibleStateSet states = super.getAccessibleStateSet(); 1134 if (getValueIsAdjusting()) { 1135 states.add(AccessibleState.BUSY); 1136 } 1137 if (getOrientation() == VERTICAL) { 1138 states.add(AccessibleState.VERTICAL); 1139 } 1140 else { 1141 states.add(AccessibleState.HORIZONTAL); 1142 } 1143 return states; 1144 } 1145 1146 1151 public AccessibleRole getAccessibleRole() { 1152 return AccessibleRole.SLIDER; 1153 } 1154 1155 1163 public AccessibleValue getAccessibleValue() { 1164 return this; 1165 } 1166 1167 1172 public Number getCurrentAccessibleValue() { 1173 return new Integer (getValue()); 1174 } 1175 1176 1181 public boolean setCurrentAccessibleValue(Number n) { 1182 if (n == null) { 1184 return false; 1185 } 1186 setValue(n.intValue()); 1187 return true; 1188 } 1189 1190 1195 public Number getMinimumAccessibleValue() { 1196 return new Integer (getMinimum()); 1197 } 1198 1199 1204 public Number getMaximumAccessibleValue() { 1205 BoundedRangeModel model = JSlider.this.getModel(); 1207 return new Integer (model.getMaximum() - model.getExtent()); 1208 } 1209 } } 1211 | Popular Tags |