GNU Classpath (0.17) | ||
Frames | No Frames |
1: /* JScrollBar.java -- 2: Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.swing; 40: 41: import java.awt.Adjustable; 42: import java.awt.Dimension; 43: import java.awt.event.AdjustmentEvent; 44: import java.awt.event.AdjustmentListener; 45: 46: import javax.accessibility.Accessible; 47: import javax.accessibility.AccessibleContext; 48: import javax.accessibility.AccessibleRole; 49: import javax.accessibility.AccessibleStateSet; 50: import javax.accessibility.AccessibleValue; 51: import javax.swing.plaf.ScrollBarUI; 52: 53: /** 54: * The JScrollBar. Two buttons control how the values that the 55: * scroll bar can take. You can also drag the thumb or click the track 56: * to move the scroll bar. Typically, the JScrollBar is used with 57: * other components to translate the value of the bar to the viewable 58: * contents of the other components. 59: */ 60: public class JScrollBar extends JComponent implements Adjustable, Accessible 61: { 62: /** 63: * DOCUMENT ME! 64: */ 65: protected class AccessibleJScrollBar extends JComponent.AccessibleJComponent 66: implements AccessibleValue 67: { 68: private static final long serialVersionUID = -7758162392045586663L; 69: 70: /** 71: * Creates a new AccessibleJSlider object. 72: * 73: * @param value0 DOCUMENT ME! 74: */ 75: protected AccessibleJScrollBar() 76: { 77: super(); 78: } 79: 80: /** 81: * DOCUMENT ME! 82: * 83: * @return DOCUMENT ME! 84: */ 85: public AccessibleStateSet getAccessibleStateSet() 86: { 87: return null; 88: } 89: 90: /** 91: * DOCUMENT ME! 92: * 93: * @return DOCUMENT ME! 94: */ 95: public AccessibleRole getAccessibleRole() 96: { 97: return null; 98: } 99: 100: /** 101: * DOCUMENT ME! 102: * 103: * @return DOCUMENT ME! 104: */ 105: public AccessibleValue getAccessibleValue() 106: { 107: return null; 108: } 109: 110: /** 111: * DOCUMENT ME! 112: * 113: * @return DOCUMENT ME! 114: */ 115: public Number getCurrentAccessibleValue() 116: { 117: return null; 118: } 119: 120: /** 121: * setCurrentAccessibleValue 122: * 123: * @param value0 TODO 124: * 125: * @return boolean 126: */ 127: public boolean setCurrentAccessibleValue(Number value0) 128: { 129: return false; 130: } 131: 132: /** 133: * getMinimumAccessibleValue 134: * 135: * @return Number 136: */ 137: public Number getMinimumAccessibleValue() 138: { 139: return null; 140: } 141: 142: /** 143: * getMaximumAccessibleValue 144: * 145: * @return Number 146: */ 147: public Number getMaximumAccessibleValue() 148: { 149: return null; 150: } 151: } 152: 153: private static final long serialVersionUID = -8195169869225066566L; 154: 155: /** How much the thumb moves when moving in a block. */ 156: protected int blockIncrement = 10; 157: 158: /** The model that holds the scroll bar's data. */ 159: protected BoundedRangeModel model; 160: 161: /** The orientation of the scroll bar. */ 162: protected int orientation = SwingConstants.VERTICAL; 163: 164: /** How much the thumb moves when moving in a unit. */ 165: protected int unitIncrement = 1; 166: 167: /** 168: * Creates a new horizontal JScrollBar object with a minimum 169: * of 0, a maxmium of 100, a value of 0 and an extent of 10. 170: */ 171: public JScrollBar() 172: { 173: this(SwingConstants.VERTICAL, 0, 10, 0, 100); 174: } 175: 176: /** 177: * Creates a new JScrollBar object with a minimum of 0, a 178: * maximum of 100, a value of 0, an extent of 10 and the given 179: * orientation. 180: * 181: * @param orientation The orientation of the JScrollBar. 182: */ 183: public JScrollBar(int orientation) 184: { 185: this(orientation, 0, 10, 0, 100); 186: } 187: 188: /** 189: * Creates a new JScrollBar object with the given orientation, 190: * value, min, max, and extent. 191: * 192: * @param orientation The orientation to use. 193: * @param value The value to use. 194: * @param extent The extent to use. 195: * @param min The minimum value of the scrollbar. 196: * @param max The maximum value of the scrollbar. 197: */ 198: public JScrollBar(int orientation, int value, int extent, int min, int max) 199: { 200: model = new DefaultBoundedRangeModel(value, extent, min, max); 201: if (orientation != SwingConstants.HORIZONTAL 202: && orientation != SwingConstants.VERTICAL) 203: throw new IllegalArgumentException(orientation 204: + " is not a legal orientation"); 205: this.orientation = orientation; 206: updateUI(); 207: } 208: 209: /** 210: * This method sets the UI of this scrollbar to 211: * the given UI. 212: * 213: * @param ui The UI to use with this scrollbar. 214: */ 215: public void setUI(ScrollBarUI ui) 216: { 217: super.setUI(ui); 218: } 219: 220: /** 221: * This method returns the UI that is being used 222: * with this scrollbar. 223: * 224: * @return The scrollbar's current UI. 225: */ 226: public ScrollBarUI getUI() 227: { 228: return (ScrollBarUI) ui; 229: } 230: 231: /** 232: * This method changes the UI to be the 233: * default for the current look and feel. 234: */ 235: public void updateUI() 236: { 237: setUI((ScrollBarUI) UIManager.getUI(this)); 238: invalidate(); 239: repaint(); 240: } 241: 242: /** 243: * This method returns an identifier to 244: * choose the correct UI delegate for the 245: * scrollbar. 246: * 247: * @return The identifer to choose the UI delegate; "ScrollBarUI" 248: */ 249: public String getUIClassID() 250: { 251: return "ScrollBarUI"; 252: } 253: 254: /** 255: * This method returns the orientation of the scrollbar. 256: * 257: * @return The orientation of the scrollbar. 258: */ 259: public int getOrientation() 260: { 261: return orientation; 262: } 263: 264: /** 265: * This method sets the orientation of the scrollbar. 266: * 267: * @param orientation The orientation of the scrollbar. 268: */ 269: public void setOrientation(int orientation) 270: { 271: if (orientation != SwingConstants.HORIZONTAL 272: && orientation != SwingConstants.VERTICAL) 273: throw new IllegalArgumentException("orientation must be one of HORIZONTAL or VERTICAL"); 274: if (orientation != this.orientation) 275: { 276: int oldOrientation = this.orientation; 277: this.orientation = orientation; 278: firePropertyChange("orientation", oldOrientation, 279: this.orientation); 280: } 281: } 282: 283: /** 284: * This method returns the model being used with 285: * the scrollbar. 286: * 287: * @return The scrollbar's model. 288: */ 289: public BoundedRangeModel getModel() 290: { 291: return model; 292: } 293: 294: /** 295: * This method sets the model to use with 296: * the scrollbar. 297: * 298: * @param newModel The new model to use with the scrollbar. 299: */ 300: public void setModel(BoundedRangeModel newModel) 301: { 302: if (model != newModel) 303: { 304: BoundedRangeModel oldModel = model; 305: model = newModel; 306: firePropertyChange("model", oldModel, model); 307: } 308: } 309: 310: /** 311: * This method returns how much the scrollbar's value 312: * should change for a unit increment depending on the 313: * given direction. 314: * 315: * @param direction The direction to scroll in. 316: * 317: * @return The amount the scrollbar's value will change given the direction. 318: */ 319: public int getUnitIncrement(int direction) 320: { 321: return direction * unitIncrement; 322: } 323: 324: /** 325: * This method sets the unitIncrement property. 326: * 327: * @param unitIncrement The new unitIncrement. 328: */ 329: public void setUnitIncrement(int unitIncrement) 330: { 331: if (unitIncrement != this.unitIncrement) 332: { 333: int oldInc = this.unitIncrement; 334: this.unitIncrement = unitIncrement; 335: firePropertyChange("unitIncrement", oldInc, 336: this.unitIncrement); 337: } 338: } 339: 340: /** 341: * The method returns how much the scrollbar's value 342: * should change for a block increment depending on 343: * the given direction. 344: * 345: * @param direction The direction to scroll in. 346: * 347: * @return The amount the scrollbar's value will change given the direction. 348: */ 349: public int getBlockIncrement(int direction) 350: { 351: return direction * blockIncrement; 352: } 353: 354: /** 355: * This method sets the blockIncrement property. 356: * 357: * @param blockIncrement The new blockIncrement. 358: */ 359: public void setBlockIncrement(int blockIncrement) 360: { 361: if (blockIncrement != this.blockIncrement) 362: { 363: int oldInc = this.blockIncrement; 364: this.blockIncrement = blockIncrement; 365: firePropertyChange("blockIncrement", oldInc, 366: this.blockIncrement); 367: } 368: } 369: 370: /** 371: * This method returns the unitIncrement. 372: * 373: * @return The unitIncrement. 374: */ 375: public int getUnitIncrement() 376: { 377: return unitIncrement; 378: } 379: 380: /** 381: * This method returns the blockIncrement. 382: * 383: * @return The blockIncrement. 384: */ 385: public int getBlockIncrement() 386: { 387: return blockIncrement; 388: } 389: 390: /** 391: * This method returns the value of the scrollbar. 392: * 393: * @return The value of the scrollbar. 394: */ 395: public int getValue() 396: { 397: return model.getValue(); 398: } 399: 400: /** 401: * This method changes the value of the scrollbar. 402: * 403: * @param value The new value of the scrollbar. 404: */ 405: public void setValue(int value) 406: { 407: if (isEnabled() && value != getValue()) 408: { 409: model.setValue(value); 410: fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 411: AdjustmentEvent.TRACK, value); 412: } 413: } 414: 415: /** 416: * This method returns the visible amount (AKA extent). 417: * The visible amount can be used by UI delegates to 418: * determine the size of the thumb. 419: * 420: * @return The visible amount (AKA extent). 421: */ 422: public int getVisibleAmount() 423: { 424: return model.getExtent(); 425: } 426: 427: /** 428: * This method sets the visible amount (AKA extent). 429: * 430: * @param extent The visible amount (AKA extent). 431: */ 432: public void setVisibleAmount(int extent) 433: { 434: if (extent != getVisibleAmount()) 435: { 436: model.setExtent(extent); 437: fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 438: AdjustmentEvent.TRACK, extent); 439: } 440: } 441: 442: /** 443: * This method returns the minimum value of the scrollbar. 444: * 445: * @return The minimum value of the scrollbar. 446: */ 447: public int getMinimum() 448: { 449: return model.getMinimum(); 450: } 451: 452: /** 453: * This method sets the minimum value of the scrollbar. 454: * 455: * @param minimum The minimum value of the scrollbar. 456: */ 457: public void setMinimum(int minimum) 458: { 459: if (minimum != getMinimum()) 460: { 461: model.setMinimum(minimum); 462: fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 463: AdjustmentEvent.TRACK, minimum); 464: } 465: } 466: 467: /** 468: * This method returns the maximum value of the scrollbar. 469: * 470: * @return The maximum value of the scrollbar. 471: */ 472: public int getMaximum() 473: { 474: return model.getMaximum(); 475: } 476: 477: /** 478: * This method sets the maximum value of the scrollbar. 479: * 480: * @param maximum The maximum value of the scrollbar. 481: */ 482: public void setMaximum(int maximum) 483: { 484: if (maximum != getMaximum()) 485: { 486: model.setMaximum(maximum); 487: fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 488: AdjustmentEvent.TRACK, maximum); 489: } 490: } 491: 492: /** 493: * This method returns the model's isAjusting value. 494: * 495: * @return The model's isAdjusting value. 496: */ 497: public boolean getValueIsAdjusting() 498: { 499: return model.getValueIsAdjusting(); 500: } 501: 502: /** 503: * This method sets the model's isAdjusting value. 504: * 505: * @param b The new isAdjusting value. 506: */ 507: public void setValueIsAdjusting(boolean b) 508: { 509: model.setValueIsAdjusting(b); 510: } 511: 512: /** 513: * This method sets the value, extent, minimum and 514: * maximum. 515: * 516: * @param newValue The new value. 517: * @param newExtent The new extent. 518: * @param newMin The new minimum. 519: * @param newMax The new maximum. 520: */ 521: public void setValues(int newValue, int newExtent, int newMin, int newMax) 522: { 523: if (!isEnabled()) 524: newValue = model.getValue(); 525: // It seems to be that on any change the value is fired. 526: if (newValue != getValue() || newExtent != getVisibleAmount() || 527: newMin != getMinimum() || newMax != getMaximum()) 528: { 529: model.setRangeProperties(newValue, newExtent, newMin, newMax, 530: model.getValueIsAdjusting()); 531: fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 532: AdjustmentEvent.TRACK, newValue); 533: } 534: } 535: 536: /** 537: * This method adds an AdjustmentListener to the scroll bar. 538: * 539: * @param listener The listener to add. 540: */ 541: public void addAdjustmentListener(AdjustmentListener listener) 542: { 543: listenerList.add(AdjustmentListener.class, listener); 544: } 545: 546: /** 547: * This method removes an AdjustmentListener from the scroll bar. 548: * 549: * @param listener The listener to remove. 550: */ 551: public void removeAdjustmentListener(AdjustmentListener listener) 552: { 553: listenerList.remove(AdjustmentListener.class, listener); 554: } 555: 556: /** 557: * This method returns an arry of all AdjustmentListeners listening to 558: * this scroll bar. 559: * 560: * @return An array of AdjustmentListeners listening to this scroll bar. 561: */ 562: public AdjustmentListener[] getAdjustmentListeners() 563: { 564: return (AdjustmentListener[]) listenerList.getListeners(AdjustmentListener.class); 565: } 566: 567: /** 568: * This method is called to fired AdjustmentEvents to the listeners 569: * of this scroll bar. All AdjustmentEvents that are fired 570: * will have an ID of ADJUSTMENT_VALUE_CHANGED and a type of 571: * TRACK. 572: * 573: * @param id The ID of the adjustment event. 574: * @param type The Type of change. 575: * @param value The new value for the property that was changed.. 576: */ 577: protected void fireAdjustmentValueChanged(int id, int type, int value) 578: { 579: Object[] adjustmentListeners = listenerList.getListenerList(); 580: AdjustmentEvent adjustmentEvent = new AdjustmentEvent(this, 581: AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, 582: AdjustmentEvent.TRACK, 583: value); 584: for (int i = adjustmentListeners.length - 2; i >= 0; i -= 2) 585: { 586: if (adjustmentListeners[i] == AdjustmentListener.class) 587: ((AdjustmentListener) adjustmentListeners[i + 1]).adjustmentValueChanged(adjustmentEvent); 588: } 589: } 590: 591: /** 592: * This method returns the minimum size for this scroll bar. 593: * 594: * @return The minimum size. 595: */ 596: public Dimension getMinimumSize() 597: { 598: return ui.getMinimumSize(this); 599: } 600: 601: /** 602: * This method returns the maximum size for this scroll bar. 603: * 604: * @return The maximum size. 605: */ 606: public Dimension getMaximumSize() 607: { 608: return ui.getMaximumSize(this); 609: } 610: 611: /** 612: * This method overrides the setEnabled in JComponent. 613: * When the scroll bar is disabled, the knob cannot 614: * be moved. 615: * 616: * @param x Whether the scrollbar is enabled. 617: */ 618: public void setEnabled(boolean x) 619: { 620: // nothing special needs to be done here since we 621: // just check the enabled setting before changing the value. 622: super.setEnabled(x); 623: } 624: 625: /** 626: * A string that describes this JScrollBar. Normally only used 627: * for debugging. 628: * 629: * @return A string describing this JScrollBar. 630: */ 631: protected String paramString() 632: { 633: return "JScrollBar"; 634: } 635: 636: /** 637: * DOCUMENT ME! 638: * 639: * @return DOCUMENT ME! 640: */ 641: public AccessibleContext getAccessibleContext() 642: { 643: if (accessibleContext == null) 644: accessibleContext = new AccessibleJScrollBar(); 645: return accessibleContext; 646: } 647: }
GNU Classpath (0.17) |