Source for javax.swing.JScrollBar

   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:     protected AccessibleJScrollBar()
  74:     {
  75:       super();
  76:     }
  77: 
  78:     /**
  79:      * DOCUMENT ME!
  80:      *
  81:      * @return DOCUMENT ME!
  82:      */
  83:     public AccessibleStateSet getAccessibleStateSet()
  84:     {
  85:       return null;
  86:     }
  87: 
  88:     /**
  89:      * DOCUMENT ME!
  90:      *
  91:      * @return DOCUMENT ME!
  92:      */
  93:     public AccessibleRole getAccessibleRole()
  94:     {
  95:       return null;
  96:     }
  97: 
  98:     /**
  99:      * DOCUMENT ME!
 100:      *
 101:      * @return DOCUMENT ME!
 102:      */
 103:     public AccessibleValue getAccessibleValue()
 104:     {
 105:       return null;
 106:     }
 107: 
 108:     /**
 109:      * DOCUMENT ME!
 110:      *
 111:      * @return DOCUMENT ME!
 112:      */
 113:     public Number getCurrentAccessibleValue()
 114:     {
 115:       return null;
 116:     }
 117: 
 118:     /**
 119:      * setCurrentAccessibleValue
 120:      *
 121:      * @param value0 TODO
 122:      *
 123:      * @return boolean
 124:      */
 125:     public boolean setCurrentAccessibleValue(Number value0)
 126:     {
 127:       return false;
 128:     }
 129: 
 130:     /**
 131:      * getMinimumAccessibleValue
 132:      *
 133:      * @return Number
 134:      */
 135:     public Number getMinimumAccessibleValue()
 136:     {
 137:       return null;
 138:     }
 139: 
 140:     /**
 141:      * getMaximumAccessibleValue
 142:      *
 143:      * @return Number
 144:      */
 145:     public Number getMaximumAccessibleValue()
 146:     {
 147:       return null;
 148:     }
 149:   }
 150: 
 151:   private static final long serialVersionUID = -8195169869225066566L;
 152:   
 153:   /** How much the thumb moves when moving in a block. */
 154:   protected int blockIncrement = 10;
 155: 
 156:   /** The model that holds the scroll bar's data. */
 157:   protected BoundedRangeModel model;
 158: 
 159:   /** The orientation of the scroll bar. */
 160:   protected int orientation = SwingConstants.VERTICAL;
 161: 
 162:   /** How much the thumb moves when moving in a unit. */
 163:   protected int unitIncrement = 1;
 164: 
 165:   /** 
 166:    * Creates a new horizontal JScrollBar object with a minimum
 167:    * of 0, a maxmium of 100, a value of 0 and an extent of 10.
 168:    */
 169:   public JScrollBar()
 170:   {
 171:     this(SwingConstants.VERTICAL, 0, 10, 0, 100);
 172:   }
 173: 
 174:   /**
 175:    * Creates a new JScrollBar object with a minimum of 0, a 
 176:    * maximum of 100, a value of 0, an extent of 10 and the given
 177:    * orientation.
 178:    *
 179:    * @param orientation The orientation of the JScrollBar.
 180:    */
 181:   public JScrollBar(int orientation)
 182:   {
 183:     this(orientation, 0, 10, 0, 100);
 184:   }
 185: 
 186:   /**
 187:    * Creates a new JScrollBar object with the given orientation, 
 188:    * value, min, max, and extent.
 189:    *
 190:    * @param orientation The orientation to use.
 191:    * @param value The value to use.
 192:    * @param extent The extent to use.
 193:    * @param min The minimum value of the scrollbar.
 194:    * @param max The maximum value of the scrollbar.
 195:    */
 196:   public JScrollBar(int orientation, int value, int extent, int min, int max)
 197:   {
 198:     model = new DefaultBoundedRangeModel(value, extent, min, max);
 199:     if (orientation != SwingConstants.HORIZONTAL
 200:         && orientation != SwingConstants.VERTICAL)
 201:       throw new IllegalArgumentException(orientation
 202:                                          + " is not a legal orientation");
 203:     this.orientation = orientation;
 204:     updateUI();
 205:   }
 206: 
 207:   /**
 208:    * This method sets the UI of this scrollbar to
 209:    * the given UI.
 210:    *
 211:    * @param ui The UI to use with this scrollbar.
 212:    */
 213:   public void setUI(ScrollBarUI ui)
 214:   {
 215:     super.setUI(ui);
 216:   }
 217: 
 218:   /**
 219:    * This method returns the UI that is being used
 220:    * with this scrollbar.
 221:    *
 222:    * @return The scrollbar's current UI.
 223:    */
 224:   public ScrollBarUI getUI()
 225:   {
 226:     return (ScrollBarUI) ui;
 227:   }
 228: 
 229:   /**
 230:    * This method changes the UI to be the
 231:    * default for the current look and feel.
 232:    */
 233:   public void updateUI()
 234:   {
 235:     setUI((ScrollBarUI) UIManager.getUI(this));
 236:     invalidate();
 237:     repaint();
 238:   }
 239: 
 240:   /**
 241:    * This method returns an identifier to 
 242:    * choose the correct UI delegate for the
 243:    * scrollbar.
 244:    *
 245:    * @return The identifer to choose the UI delegate; "ScrollBarUI"
 246:    */
 247:   public String getUIClassID()
 248:   {
 249:     return "ScrollBarUI";
 250:   }
 251: 
 252:   /**
 253:    * This method returns the orientation of the scrollbar.
 254:    *
 255:    * @return The orientation of the scrollbar.
 256:    */
 257:   public int getOrientation()
 258:   {
 259:     return orientation;
 260:   }
 261: 
 262:   /**
 263:    * This method sets the orientation of the scrollbar.
 264:    *
 265:    * @param orientation The orientation of the scrollbar.
 266:    */
 267:   public void setOrientation(int orientation)
 268:   {
 269:     if (orientation != SwingConstants.HORIZONTAL
 270:         && orientation != SwingConstants.VERTICAL)
 271:       throw new IllegalArgumentException("orientation must be one of HORIZONTAL or VERTICAL");
 272:     if (orientation != this.orientation)
 273:       {
 274:     int oldOrientation = this.orientation;
 275:     this.orientation = orientation;
 276:     firePropertyChange("orientation", oldOrientation,
 277:                        this.orientation);
 278:       }
 279:   }
 280: 
 281:   /**
 282:    * This method returns the model being used with 
 283:    * the scrollbar.
 284:    *
 285:    * @return The scrollbar's model.
 286:    */
 287:   public BoundedRangeModel getModel()
 288:   {
 289:     return model;
 290:   }
 291: 
 292:   /**
 293:    * This method sets the model to use with
 294:    * the scrollbar.
 295:    *
 296:    * @param newModel The new model to use with the scrollbar.
 297:    */
 298:   public void setModel(BoundedRangeModel newModel)
 299:   {
 300:     if (model != newModel)
 301:       {
 302:     BoundedRangeModel oldModel = model;
 303:     model = newModel;
 304:     firePropertyChange("model", oldModel, model);
 305:       }
 306:   }
 307: 
 308:   /**
 309:    * This method returns how much the scrollbar's value
 310:    * should change for a unit increment depending on the 
 311:    * given direction.
 312:    *
 313:    * @param direction The direction to scroll in.
 314:    *
 315:    * @return The amount the scrollbar's value will change given the direction.
 316:    */
 317:   public int getUnitIncrement(int direction)
 318:   {
 319:     return direction * unitIncrement;
 320:   }
 321: 
 322:   /**
 323:    * This method sets the unitIncrement property.
 324:    *
 325:    * @param unitIncrement The new unitIncrement.
 326:    */
 327:   public void setUnitIncrement(int unitIncrement)
 328:   {
 329:     if (unitIncrement != this.unitIncrement)
 330:       {
 331:     int oldInc = this.unitIncrement;
 332:     this.unitIncrement = unitIncrement;
 333:     firePropertyChange("unitIncrement", oldInc,
 334:                        this.unitIncrement);
 335:       }
 336:   }
 337: 
 338:   /**
 339:    * The method returns how much the scrollbar's value
 340:    * should change for a block increment depending on
 341:    * the given direction.
 342:    *
 343:    * @param direction The direction to scroll in.
 344:    *
 345:    * @return The amount the scrollbar's value will change given the direction.
 346:    */
 347:   public int getBlockIncrement(int direction)
 348:   {
 349:     return direction * blockIncrement;
 350:   }
 351: 
 352:   /**
 353:    * This method sets the blockIncrement property.
 354:    *
 355:    * @param blockIncrement The new blockIncrement.
 356:    */
 357:   public void setBlockIncrement(int blockIncrement)
 358:   {
 359:     if (blockIncrement != this.blockIncrement)
 360:       {
 361:     int oldInc = this.blockIncrement;
 362:     this.blockIncrement = blockIncrement;
 363:     firePropertyChange("blockIncrement", oldInc,
 364:                        this.blockIncrement);
 365:       }
 366:   }
 367: 
 368:   /**
 369:    * This method returns the unitIncrement.
 370:    *
 371:    * @return The unitIncrement.
 372:    */
 373:   public int getUnitIncrement()
 374:   {
 375:     return unitIncrement;
 376:   }
 377: 
 378:   /**
 379:    * This method returns the blockIncrement.
 380:    *
 381:    * @return The blockIncrement.
 382:    */
 383:   public int getBlockIncrement()
 384:   {
 385:     return blockIncrement;
 386:   }
 387: 
 388:   /**
 389:    * This method returns the value of the scrollbar.
 390:    *
 391:    * @return The value of the scrollbar.
 392:    */
 393:   public int getValue()
 394:   {
 395:     return model.getValue();
 396:   }
 397: 
 398:   /**
 399:    * This method changes the value of the scrollbar.
 400:    *
 401:    * @param value The new value of the scrollbar.
 402:    */
 403:   public void setValue(int value)
 404:   {
 405:     if (isEnabled() && value != getValue())
 406:     {
 407:       model.setValue(value);
 408:       fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
 409:                                  AdjustmentEvent.TRACK, value);
 410:     }
 411:   }
 412: 
 413:   /**
 414:    * This method returns the visible amount (AKA extent). 
 415:    * The visible amount can be used by UI delegates to 
 416:    * determine the size of the thumb.
 417:    *
 418:    * @return The visible amount (AKA extent).
 419:    */
 420:   public int getVisibleAmount()
 421:   {
 422:     return model.getExtent();
 423:   }
 424: 
 425:   /**
 426:    * This method sets the visible amount (AKA extent).
 427:    *
 428:    * @param extent The visible amount (AKA extent).
 429:    */
 430:   public void setVisibleAmount(int extent)
 431:   {
 432:     if (extent != getVisibleAmount())
 433:     {
 434:       model.setExtent(extent);
 435:       fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
 436:                                  AdjustmentEvent.TRACK, extent);
 437:     }
 438:   }
 439: 
 440:   /**
 441:    * This method returns the minimum value of the scrollbar.
 442:    *
 443:    * @return The minimum value of the scrollbar.
 444:    */
 445:   public int getMinimum()
 446:   {
 447:     return model.getMinimum();
 448:   }
 449: 
 450:   /**
 451:    * This method sets the minimum value of the scrollbar.
 452:    *
 453:    * @param minimum The minimum value of the scrollbar.
 454:    */
 455:   public void setMinimum(int minimum)
 456:   {
 457:     if (minimum != getMinimum())
 458:     {
 459:       model.setMinimum(minimum);
 460:       fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
 461:                                  AdjustmentEvent.TRACK, minimum);
 462:     }
 463:   }
 464: 
 465:   /**
 466:    * This method returns the maximum value of the scrollbar.
 467:    *
 468:    * @return The maximum value of the scrollbar.
 469:    */
 470:   public int getMaximum()
 471:   {
 472:     return model.getMaximum();
 473:   }
 474: 
 475:   /**
 476:    * This method sets the maximum value of the scrollbar.
 477:    *
 478:    * @param maximum The maximum value of the scrollbar.
 479:    */
 480:   public void setMaximum(int maximum)
 481:   {
 482:     if (maximum != getMaximum())
 483:     {
 484:       model.setMaximum(maximum);
 485:       fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
 486:                                  AdjustmentEvent.TRACK, maximum);
 487:     }
 488:   }
 489: 
 490:   /**
 491:    * This method returns the model's isAjusting value.
 492:    *
 493:    * @return The model's isAdjusting value.
 494:    */
 495:   public boolean getValueIsAdjusting()
 496:   {
 497:     return model.getValueIsAdjusting();
 498:   }
 499: 
 500:   /**
 501:    * This method sets the model's isAdjusting value.
 502:    *
 503:    * @param b The new isAdjusting value.
 504:    */
 505:   public void setValueIsAdjusting(boolean b)
 506:   {
 507:     model.setValueIsAdjusting(b);
 508:   }
 509: 
 510:   /**
 511:    * This method sets the value, extent, minimum and 
 512:    * maximum.
 513:    *
 514:    * @param newValue The new value.
 515:    * @param newExtent The new extent.
 516:    * @param newMin The new minimum.
 517:    * @param newMax The new maximum.
 518:    */
 519:   public void setValues(int newValue, int newExtent, int newMin, int newMax)
 520:   {
 521:     if (!isEnabled())
 522:       newValue = model.getValue();
 523:     // It seems to be that on any change the value is fired.
 524:     if (newValue != getValue() || newExtent != getVisibleAmount() ||
 525:         newMin != getMinimum() || newMax != getMaximum())
 526:     {
 527:       model.setRangeProperties(newValue, newExtent, newMin, newMax,
 528:                                model.getValueIsAdjusting());
 529:       fireAdjustmentValueChanged(AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
 530:                                  AdjustmentEvent.TRACK, newValue);
 531:     }
 532:   }
 533: 
 534:   /**
 535:    * This method adds an AdjustmentListener to the scroll bar.
 536:    *
 537:    * @param listener The listener to add.
 538:    */
 539:   public void addAdjustmentListener(AdjustmentListener listener)
 540:   {
 541:     listenerList.add(AdjustmentListener.class, listener);
 542:   }
 543: 
 544:   /**
 545:    * This method removes an AdjustmentListener from the scroll bar. 
 546:    *
 547:    * @param listener The listener to remove.
 548:    */
 549:   public void removeAdjustmentListener(AdjustmentListener listener)
 550:   {
 551:     listenerList.remove(AdjustmentListener.class, listener);
 552:   }
 553: 
 554:   /**
 555:    * This method returns an arry of all AdjustmentListeners listening to 
 556:    * this scroll bar.
 557:    *
 558:    * @return An array of AdjustmentListeners listening to this scroll bar.
 559:    */
 560:   public AdjustmentListener[] getAdjustmentListeners()
 561:   {
 562:     return (AdjustmentListener[]) listenerList.getListeners(AdjustmentListener.class);
 563:   }
 564: 
 565:   /**
 566:    * This method is called to fired AdjustmentEvents to the listeners
 567:    * of this scroll bar. All AdjustmentEvents that are fired
 568:    * will have an ID of ADJUSTMENT_VALUE_CHANGED and a type of
 569:    * TRACK. 
 570:    *
 571:    * @param id The ID of the adjustment event.
 572:    * @param type The Type of change.
 573:    * @param value The new value for the property that was changed..
 574:    */
 575:   protected void fireAdjustmentValueChanged(int id, int type, int value)
 576:   {
 577:     Object[] adjustmentListeners = listenerList.getListenerList();
 578:     AdjustmentEvent adjustmentEvent = new AdjustmentEvent(this, 
 579:                                             AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED,
 580:                         AdjustmentEvent.TRACK,
 581:                         value);
 582:     for (int i = adjustmentListeners.length - 2; i >= 0; i -= 2)
 583:       {
 584:     if (adjustmentListeners[i] == AdjustmentListener.class)
 585:       ((AdjustmentListener) adjustmentListeners[i + 1]).adjustmentValueChanged(adjustmentEvent);
 586:       }
 587:   }
 588: 
 589:   /**
 590:    * This method returns the minimum size for this scroll bar.
 591:    *
 592:    * @return The minimum size.
 593:    */
 594:   public Dimension getMinimumSize()
 595:   {
 596:     return ui.getMinimumSize(this);
 597:   }
 598: 
 599:   /**
 600:    * This method returns the maximum size for this scroll bar.
 601:    *
 602:    * @return The maximum size.
 603:    */
 604:   public Dimension getMaximumSize()
 605:   {
 606:     return ui.getMaximumSize(this);
 607:   }
 608: 
 609:   /**
 610:    * This method overrides the setEnabled in JComponent.
 611:    * When the scroll bar is disabled, the knob cannot
 612:    * be moved.
 613:    *
 614:    * @param x Whether the scrollbar is enabled.
 615:    */
 616:   public void setEnabled(boolean x)
 617:   {
 618:     // nothing special needs to be done here since we 
 619:     // just check the enabled setting before changing the value.
 620:     super.setEnabled(x);
 621:   }
 622: 
 623:   /**
 624:    * A string that describes this JScrollBar. Normally only used
 625:    * for debugging.
 626:    *
 627:    * @return A string describing this JScrollBar.
 628:    */
 629:   protected String paramString()
 630:   {
 631:     return "JScrollBar";
 632:   }
 633: 
 634:   /**
 635:    * DOCUMENT ME!
 636:    *
 637:    * @return DOCUMENT ME!
 638:    */
 639:   public AccessibleContext getAccessibleContext()
 640:   {
 641:     if (accessibleContext == null)
 642:       accessibleContext = new AccessibleJScrollBar();
 643:     return accessibleContext;
 644:   }
 645: }