1 7 package java.awt; 8 9 import java.awt.peer.ScrollbarPeer; 10 import java.awt.event.*; 11 import java.util.EventListener ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 import javax.accessibility.*; 16 17 18 149 public class Scrollbar extends Component implements Adjustable , Accessible { 150 151 154 public static final int HORIZONTAL = 0; 155 156 159 public static final int VERTICAL = 1; 160 161 171 int value; 172 173 182 int maximum; 183 184 193 int minimum; 194 195 205 int visibleAmount; 206 207 218 int orientation; 219 220 229 int lineIncrement = 1; 230 231 240 int pageIncrement = 10; 241 242 251 transient boolean isAdjusting; 252 253 transient AdjustmentListener adjustmentListener; 254 255 private static final String base = "scrollbar"; 256 private static int nameCounter = 0; 257 258 261 private static final long serialVersionUID = 8451667562882310543L; 262 263 266 private static native void initIDs(); 267 268 static { 269 270 Toolkit.loadLibraries(); 271 if (!GraphicsEnvironment.isHeadless()) { 272 initIDs(); 273 } 274 } 275 276 338 public Scrollbar() throws HeadlessException { 339 this(VERTICAL, 0, 10, 0, 100); 340 } 341 342 357 public Scrollbar(int orientation) throws HeadlessException { 358 this(orientation, 0, 10, 0, 100); 359 } 360 361 386 public Scrollbar(int orientation, int value, int visible, int minimum, 387 int maximum) throws HeadlessException { 388 GraphicsEnvironment.checkHeadless(); 389 switch (orientation) { 390 case HORIZONTAL: 391 case VERTICAL: 392 this.orientation = orientation; 393 break; 394 default: 395 throw new IllegalArgumentException ("illegal scrollbar orientation"); 396 } 397 setValues(value, visible, minimum, maximum); 398 } 399 400 404 String constructComponentName() { 405 synchronized (getClass()) { 406 return base + nameCounter++; 407 } 408 } 409 410 415 public void addNotify() { 416 synchronized (getTreeLock()) { 417 if (peer == null) 418 peer = getToolkit().createScrollbar(this); 419 super.addNotify(); 420 } 421 } 422 423 431 public int getOrientation() { 432 return orientation; 433 } 434 435 447 public void setOrientation(int orientation) { 448 synchronized (getTreeLock()) { 449 if (orientation == this.orientation) { 450 return; 451 } 452 switch (orientation) { 453 case HORIZONTAL: 454 case VERTICAL: 455 this.orientation = orientation; 456 break; 457 default: 458 throw new IllegalArgumentException ("illegal scrollbar orientation"); 459 } 460 461 if (peer != null) { 462 removeNotify(); 463 addNotify(); 464 invalidate(); 465 } 466 } 467 if (accessibleContext != null) { 468 accessibleContext.firePropertyChange( 469 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 470 ((orientation == VERTICAL) 471 ? AccessibleState.HORIZONTAL : AccessibleState.VERTICAL), 472 ((orientation == VERTICAL) 473 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL)); 474 } 475 } 476 477 484 public int getValue() { 485 return value; 486 } 487 488 512 public void setValue(int newValue) { 513 setValues(newValue, visibleAmount, minimum, maximum); 516 } 517 518 525 public int getMinimum() { 526 return minimum; 527 } 528 529 553 public void setMinimum(int newMinimum) { 554 557 setValues(value, visibleAmount, newMinimum, maximum); 560 } 561 562 569 public int getMaximum() { 570 return maximum; 571 } 572 573 598 public void setMaximum(int newMaximum) { 599 if (newMaximum == Integer.MIN_VALUE) { 602 newMaximum = Integer.MIN_VALUE + 1; 603 } 604 605 if (minimum >= newMaximum) { 606 minimum = newMaximum - 1; 607 } 608 609 setValues(value, visibleAmount, minimum, newMaximum); 612 } 613 614 634 public int getVisibleAmount() { 635 return getVisible(); 636 } 637 638 642 @Deprecated 643 public int getVisible() { 644 return visibleAmount; 645 } 646 647 680 public void setVisibleAmount(int newAmount) { 681 setValues(value, newAmount, minimum, maximum); 684 } 685 686 702 public void setUnitIncrement(int v) { 703 setLineIncrement(v); 704 } 705 706 710 @Deprecated 711 public synchronized void setLineIncrement(int v) { 712 int tmp = (v < 1) ? 1 : v; 713 714 if (lineIncrement == tmp) { 715 return; 716 } 717 lineIncrement = tmp; 718 719 ScrollbarPeer peer = (ScrollbarPeer)this.peer; 720 if (peer != null) { 721 peer.setLineIncrement(lineIncrement); 722 } 723 } 724 725 738 public int getUnitIncrement() { 739 return getLineIncrement(); 740 } 741 742 746 @Deprecated 747 public int getLineIncrement() { 748 return lineIncrement; 749 } 750 751 767 public void setBlockIncrement(int v) { 768 setPageIncrement(v); 769 } 770 771 775 @Deprecated 776 public synchronized void setPageIncrement(int v) { 777 int tmp = (v < 1) ? 1 : v; 778 779 if (pageIncrement == tmp) { 780 return; 781 } 782 pageIncrement = tmp; 783 784 ScrollbarPeer peer = (ScrollbarPeer)this.peer; 785 if (peer != null) { 786 peer.setPageIncrement(pageIncrement); 787 } 788 } 789 790 803 public int getBlockIncrement() { 804 return getPageIncrement(); 805 } 806 807 811 @Deprecated 812 public int getPageIncrement() { 813 return pageIncrement; 814 } 815 816 849 public void setValues(int value, int visible, int minimum, int maximum) { 850 int oldValue; 851 synchronized (this) { 852 if (minimum == Integer.MAX_VALUE) { 853 minimum = Integer.MAX_VALUE - 1; 854 } 855 if (maximum <= minimum) { 856 maximum = minimum + 1; 857 } 858 859 long maxMinusMin = (long) maximum - (long) minimum; 860 if (maxMinusMin > Integer.MAX_VALUE) { 861 maxMinusMin = Integer.MAX_VALUE; 862 maximum = minimum + (int) maxMinusMin; 863 } 864 if (visible > (int) maxMinusMin) { 865 visible = (int) maxMinusMin; 866 } 867 if (visible < 1) { 868 visible = 1; 869 } 870 871 if (value < minimum) { 872 value = minimum; 873 } 874 if (value > maximum - visible) { 875 value = maximum - visible; 876 } 877 878 oldValue = this.value; 879 this.value = value; 880 this.visibleAmount = visible; 881 this.minimum = minimum; 882 this.maximum = maximum; 883 ScrollbarPeer peer = (ScrollbarPeer)this.peer; 884 if (peer != null) { 885 peer.setValues(value, visibleAmount, minimum, maximum); 886 } 887 } 888 889 if ((oldValue != value) && (accessibleContext != null)) { 890 accessibleContext.firePropertyChange( 891 AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, 892 new Integer (oldValue), 893 new Integer (value)); 894 } 895 } 896 897 905 public boolean getValueIsAdjusting() { 906 return isAdjusting; 907 } 908 909 916 public void setValueIsAdjusting(boolean b) { 917 boolean oldValue; 918 919 synchronized (this) { 920 oldValue = isAdjusting; 921 isAdjusting = b; 922 } 923 924 if ((oldValue != b) && (accessibleContext != null)) { 925 accessibleContext.firePropertyChange( 926 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 927 ((oldValue) ? AccessibleState.BUSY : null), 928 ((b) ? AccessibleState.BUSY : null)); 929 } 930 } 931 932 933 934 947 public synchronized void addAdjustmentListener(AdjustmentListener l) { 948 if (l == null) { 949 return; 950 } 951 adjustmentListener = AWTEventMulticaster.add(adjustmentListener, l); 952 newEventsOnly = true; 953 } 954 955 968 public synchronized void removeAdjustmentListener(AdjustmentListener l) { 969 if (l == null) { 970 return; 971 } 972 adjustmentListener = AWTEventMulticaster.remove(adjustmentListener, l); 973 } 974 975 988 public synchronized AdjustmentListener[] getAdjustmentListeners() { 989 return (AdjustmentListener[])(getListeners(AdjustmentListener.class)); 990 } 991 992 1022 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 1023 EventListener l = null; 1024 if (listenerType == AdjustmentListener.class) { 1025 l = adjustmentListener; 1026 } else { 1027 return super.getListeners(listenerType); 1028 } 1029 return AWTEventMulticaster.getListeners(l, listenerType); 1030 } 1031 1032 boolean eventEnabled(AWTEvent e) { 1034 if (e.id == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) { 1035 if ((eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 || 1036 adjustmentListener != null) { 1037 return true; 1038 } 1039 return false; 1040 } 1041 return super.eventEnabled(e); 1042 } 1043 1044 1059 protected void processEvent(AWTEvent e) { 1060 if (e instanceof AdjustmentEvent) { 1061 processAdjustmentEvent((AdjustmentEvent)e); 1062 return; 1063 } 1064 super.processEvent(e); 1065 } 1066 1067 1091 protected void processAdjustmentEvent(AdjustmentEvent e) { 1092 AdjustmentListener listener = adjustmentListener; 1093 if (listener != null) { 1094 listener.adjustmentValueChanged(e); 1095 } 1096 } 1097 1098 1107 protected String paramString() { 1108 return super.paramString() + 1109 ",val=" + value + 1110 ",vis=" + visibleAmount + 1111 ",min=" + minimum + 1112 ",max=" + maximum + 1113 ((orientation == VERTICAL) ? ",vert" : ",horz") + 1114 ",isAdjusting=" + isAdjusting; 1115 } 1116 1117 1118 1120 1121 1126 private int scrollbarSerializedDataVersion = 1; 1127 1128 1146 private void writeObject(ObjectOutputStream s) 1147 throws IOException 1148 { 1149 s.defaultWriteObject(); 1150 1151 AWTEventMulticaster.save(s, adjustmentListenerK, adjustmentListener); 1152 s.writeObject(null); 1153 } 1154 1155 1169 private void readObject(ObjectInputStream s) 1170 throws ClassNotFoundException , IOException , HeadlessException 1171 { 1172 GraphicsEnvironment.checkHeadless(); 1173 s.defaultReadObject(); 1174 1175 Object keyOrNull; 1176 while(null != (keyOrNull = s.readObject())) { 1177 String key = ((String )keyOrNull).intern(); 1178 1179 if (adjustmentListenerK == key) 1180 addAdjustmentListener((AdjustmentListener)(s.readObject())); 1181 1182 else s.readObject(); 1184 } 1185 } 1186 1187 1188 1192 1202 public AccessibleContext getAccessibleContext() { 1203 if (accessibleContext == null) { 1204 accessibleContext = new AccessibleAWTScrollBar(); 1205 } 1206 return accessibleContext; 1207 } 1208 1209 1215 protected class AccessibleAWTScrollBar extends AccessibleAWTComponent 1216 implements AccessibleValue 1217 { 1218 1221 private static final long serialVersionUID = -344337268523697807L; 1222 1223 1230 public AccessibleStateSet getAccessibleStateSet() { 1231 AccessibleStateSet states = super.getAccessibleStateSet(); 1232 if (getValueIsAdjusting()) { 1233 states.add(AccessibleState.BUSY); 1234 } 1235 if (getOrientation() == VERTICAL) { 1236 states.add(AccessibleState.VERTICAL); 1237 } else { 1238 states.add(AccessibleState.HORIZONTAL); 1239 } 1240 return states; 1241 } 1242 1243 1249 public AccessibleRole getAccessibleRole() { 1250 return AccessibleRole.SCROLL_BAR; 1251 } 1252 1253 1262 public AccessibleValue getAccessibleValue() { 1263 return this; 1264 } 1265 1266 1271 public Number getCurrentAccessibleValue() { 1272 return new Integer (getValue()); 1273 } 1274 1275 1280 public boolean setCurrentAccessibleValue(Number n) { 1281 if (n instanceof Integer ) { 1282 setValue(n.intValue()); 1283 return true; 1284 } else { 1285 return false; 1286 } 1287 } 1288 1289 1294 public Number getMinimumAccessibleValue() { 1295 return new Integer (getMinimum()); 1296 } 1297 1298 1303 public Number getMaximumAccessibleValue() { 1304 return new Integer (getMaximum()); 1305 } 1306 1307 } 1309} 1310 1311 | Popular Tags |