Source for javax.swing.JScrollPane

   1: /* JScrollPane.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.Component;
  42: import java.awt.ComponentOrientation;
  43: import java.awt.Dimension;
  44: import java.awt.Insets;
  45: import java.awt.LayoutManager;
  46: import java.awt.Point;
  47: import java.awt.Rectangle;
  48: 
  49: import javax.accessibility.Accessible;
  50: import javax.swing.border.Border;
  51: import javax.swing.event.ChangeEvent;
  52: import javax.swing.event.ChangeListener;
  53: import javax.swing.plaf.ScrollPaneUI;
  54: import javax.swing.plaf.UIResource;
  55: 
  56: /**
  57:  * A component that embeds another component and enables it to be scrolled
  58:  * both in horizontal and vertical direction.
  59:  *
  60:  * <table>
  61:  * <tr><th>Property                    </th><th>Stored in       </th><th>Bound?</th></tr>
  62:  * <tr><td>columnHeader                </td><td>scrollPane      </td><td>yes   </td></tr>
  63:  * <tr><td>columnHeaderView            </td><td>columnHeader    </td><td>no    </td></tr>
  64:  * <tr><td>componentOrientation        </td><td>scrollPane      </td><td>yes   </td></tr>
  65:  * <tr><td>horizontalScrollBar         </td><td>scrollPane      </td><td>yes   </td></tr>
  66:  * <tr><td>horizontalScrollBarPolicy   </td><td>scrollPane      </td><td>yes   </td></tr>
  67:  * <tr><td>layout                      </td><td>scrollPane      </td><td>yes   </td></tr>
  68:  * <tr><td>rowHeader                   </td><td>scrollPane      </td><td>yes   </td></tr>
  69:  * <tr><td>rowHeaderView               </td><td>rowHeader       </td><td>no    </td></tr>
  70:  * <tr><td>validateRoot                </td><td>scrollPane      </td><td>no    </td></tr>
  71:  * <tr><td>verticalScrollBar           </td><td>scrollPane      </td><td>yes   </td></tr>
  72:  * <tr><td>verticalScrollBarPolicy     </td><td>scrollPane      </td><td>yes   </td></tr>
  73:  * <tr><td>viewport                    </td><td>scrollPane      </td><td>yes   </td></tr>
  74:  * <tr><td>viewportBorder              </td><td>scrollPane      </td><td>yes   </td></tr>
  75:  * <tr><td>viewportBorderBounds        </td><td>scrollPane      </td><td>no    </td></tr>
  76:  * <tr><td>viewportView                </td><td>viewport        </td><td>no    </td></tr>
  77:  * <tr><td>wheelScrollingEnabled       </td><td>scrollPane      </td><td>yes   </td></tr>
  78:  * </table>
  79:  */
  80: public class JScrollPane 
  81:   extends JComponent 
  82:   implements Accessible, ScrollPaneConstants
  83: {
  84:   private static final long serialVersionUID = 5203525440012340014L;
  85:   
  86:   protected JViewport columnHeader;
  87:   protected JViewport rowHeader;
  88: 
  89:   protected Component lowerLeft;
  90:   protected Component lowerRight;
  91:   protected Component upperLeft;
  92:   protected Component upperRight;
  93: 
  94:   protected JScrollBar horizontalScrollBar;
  95:   protected int horizontalScrollBarPolicy;
  96:   protected JScrollBar verticalScrollBar;
  97:   protected int verticalScrollBarPolicy;
  98: 
  99:   protected JViewport viewport;
 100:   
 101:   Border viewportBorder;
 102:   boolean wheelScrollingEnabled;
 103:   ChangeListener scrollListener;  
 104: 
 105:   public JViewport getColumnHeader()
 106:   {
 107:     return columnHeader;
 108:   }
 109: 
 110:   public Component getCorner(String key) {
 111:     if (getComponentOrientation() 
 112:         == ComponentOrientation.LEFT_TO_RIGHT)
 113:       {
 114:         if (key == LOWER_LEADING_CORNER)
 115:           key = LOWER_LEFT_CORNER;
 116:         else if (key == LOWER_TRAILING_CORNER)
 117:           key = LOWER_RIGHT_CORNER;
 118:         else if (key == UPPER_LEADING_CORNER)
 119:           key = UPPER_LEFT_CORNER;
 120:         else if (key == UPPER_TRAILING_CORNER)
 121:           key = UPPER_RIGHT_CORNER;
 122:       }
 123:     else if (getComponentOrientation() 
 124:              == ComponentOrientation.RIGHT_TO_LEFT)
 125:       {
 126:         if (key == LOWER_LEADING_CORNER)
 127:           key = LOWER_RIGHT_CORNER;
 128:         else if (key == LOWER_TRAILING_CORNER)
 129:           key = LOWER_LEFT_CORNER;
 130:         else if (key == UPPER_LEADING_CORNER)
 131:           key = UPPER_RIGHT_CORNER;
 132:         else if (key == UPPER_TRAILING_CORNER)
 133:           key = UPPER_LEFT_CORNER;
 134:       }
 135: 
 136:     if (key == LOWER_RIGHT_CORNER)
 137:       return lowerRight;
 138:     else if (key == UPPER_RIGHT_CORNER)
 139:       return upperRight;
 140:     else if (key == LOWER_LEFT_CORNER)
 141:       return lowerLeft;
 142:     else if (key == UPPER_LEFT_CORNER)
 143:       return upperLeft;
 144:     return null;
 145:   }
 146: 
 147:   public JScrollBar getHorizontalScrollBar()
 148:   {
 149:     return horizontalScrollBar;
 150:   }
 151: 
 152:   public int getHorizontalScrollBarPolicy()
 153:   {
 154:     return horizontalScrollBarPolicy;
 155:   }
 156: 
 157:   public JViewport getRowHeader()
 158:   {
 159:     return rowHeader;
 160:   }
 161: 
 162:   public JScrollBar getVerticalScrollBar()
 163:   {
 164:     return verticalScrollBar;
 165:   }
 166: 
 167:   public int getVerticalScrollBarPolicy()
 168:   {
 169:     return verticalScrollBarPolicy;
 170:   }
 171: 
 172:   public JViewport getViewport()
 173:   {
 174:     return viewport;
 175:   }
 176: 
 177:   public Border getViewportBorder()
 178:   {
 179:     return viewportBorder;
 180:   }
 181: 
 182:   public Rectangle getViewportBorderBounds()
 183:   {
 184:     if (viewportBorder == null)
 185:       {
 186:         if (getViewport() == null)
 187:           return new Rectangle(0,0,0,0);
 188:         else
 189:           return getViewport().getBounds();
 190:       }
 191:     else
 192:       {
 193:         Insets i = viewportBorder.getBorderInsets(getViewport());
 194:         if (getViewport() == null)
 195:           return new Rectangle(0,0,
 196:                                i.left+i.right, i.top+i.bottom);
 197:         else
 198:           {
 199:             Rectangle b = getViewport().getBounds();
 200:             return new Rectangle(b.x - i.left, 
 201:                                  b.y - i.top,
 202:                                  b.width + i.left + i.right, 
 203:                                  b.height + i.top + i.bottom);
 204:           }
 205:       }
 206:   }
 207:   
 208:   public boolean isWheelScrollingEnabled()
 209:   {
 210:     return wheelScrollingEnabled;
 211:   }
 212: 
 213: 
 214: 
 215:   private void sync()
 216:   {
 217:     LayoutManager m = super.getLayout();
 218:     if (m != null && m instanceof ScrollPaneLayout)
 219:       {
 220:         ScrollPaneLayout sl = (ScrollPaneLayout) m;
 221:         sl.syncWithScrollPane(this);
 222:       }
 223:   }
 224: 
 225:   private void removeNonNull(Component c)
 226:   {
 227:     if (c != null)
 228:       remove(c);
 229:   }
 230: 
 231:   private void addNonNull(Component c)
 232:   {
 233:     if (c != null)
 234:       add(c);
 235:   }
 236: 
 237:   public void setComponentOrientation(ComponentOrientation co)
 238:   {
 239:     ComponentOrientation old = super.getComponentOrientation();
 240:     super.setComponentOrientation(co);
 241:     firePropertyChange("componentOrientation", old, co);
 242:     sync();
 243:   }
 244: 
 245:   public void setColumnHeader(JViewport h)
 246:   {
 247:     if (columnHeader == h)
 248:       return;
 249:     
 250:     JViewport old = columnHeader;
 251:     removeNonNull(old);
 252:     columnHeader = h;
 253:     addNonNull(h);
 254:     firePropertyChange("columnHeader", old, h);
 255:     sync();
 256:   }
 257: 
 258:   public void setColumnHeaderView(Component c)
 259:   {
 260:     if (columnHeader == null)
 261:       setColumnHeader(createViewport());
 262:     columnHeader.setView(c);
 263:     sync();
 264:   }
 265: 
 266:   public void setCorner(String key, Component c)
 267:   {
 268:     if (getComponentOrientation() 
 269:         == ComponentOrientation.LEFT_TO_RIGHT)
 270:       {
 271:         if (key == LOWER_LEADING_CORNER)
 272:           key = LOWER_LEFT_CORNER;
 273:         else if (key == LOWER_TRAILING_CORNER)
 274:           key = LOWER_RIGHT_CORNER;
 275:         else if (key == UPPER_LEADING_CORNER)
 276:           key = UPPER_LEFT_CORNER;
 277:         else if (key == UPPER_TRAILING_CORNER)
 278:           key = UPPER_RIGHT_CORNER;
 279:       }
 280:     else if (getComponentOrientation() 
 281:              == ComponentOrientation.RIGHT_TO_LEFT)
 282:       {
 283:         if (key == LOWER_LEADING_CORNER)
 284:           key = LOWER_RIGHT_CORNER;
 285:         else if (key == LOWER_TRAILING_CORNER)
 286:           key = LOWER_LEFT_CORNER;
 287:         else if (key == UPPER_LEADING_CORNER)
 288:           key = UPPER_RIGHT_CORNER;
 289:         else if (key == UPPER_TRAILING_CORNER)
 290:           key = UPPER_LEFT_CORNER;
 291:       }
 292: 
 293:     if (key == LOWER_RIGHT_CORNER)
 294:       {
 295:         removeNonNull(lowerRight);
 296:         lowerRight = c;
 297:         addNonNull(c);
 298:       }
 299:     else if (key == UPPER_RIGHT_CORNER)
 300:       {
 301:         removeNonNull(upperRight);
 302:         upperRight = c;
 303:         addNonNull(c);
 304:       }
 305:     else if (key == LOWER_LEFT_CORNER)
 306:       {
 307:         removeNonNull(lowerLeft);
 308:         lowerLeft = c;
 309:         addNonNull(c);
 310:       }
 311:     else if (key == UPPER_LEFT_CORNER)
 312:       {
 313:         removeNonNull(upperLeft);
 314:         upperLeft = c;
 315:         addNonNull(c);
 316:       }
 317:     else
 318:       throw new IllegalArgumentException("unknown corner " + key);
 319:     sync();
 320:   }
 321: 
 322:   public void setHorizontalScrollBar(JScrollBar h)
 323:   {
 324:     if (horizontalScrollBar == h)
 325:       return;
 326: 
 327:     JScrollBar old = horizontalScrollBar;
 328:     removeNonNull(old);
 329:     horizontalScrollBar = h;
 330:     addNonNull(h);
 331:     firePropertyChange("horizontalScrollBar", old, h);
 332:     sync();
 333: 
 334:     if (old != null)
 335:       {
 336:         BoundedRangeModel model = old.getModel();
 337:         if (model != null)
 338:           model.removeChangeListener(scrollListener);
 339:       }
 340:     if (h != null)
 341:       {
 342:         BoundedRangeModel model = h.getModel();
 343:         if (model != null)
 344:           model.addChangeListener(scrollListener);
 345:       }
 346:   }
 347: 
 348:   public void setHorizontalScrollBarPolicy(int h)
 349:   {
 350:     if (horizontalScrollBarPolicy == h)
 351:       return;
 352:     
 353:     if (h != HORIZONTAL_SCROLLBAR_AS_NEEDED
 354:         && h != HORIZONTAL_SCROLLBAR_NEVER
 355:         && h != HORIZONTAL_SCROLLBAR_ALWAYS)
 356:       throw new IllegalArgumentException("unknown horizontal scrollbar policy");    
 357: 
 358:     int old = horizontalScrollBarPolicy;
 359:     horizontalScrollBarPolicy = h;
 360:     firePropertyChange("horizontalScrollBarPolicy", old, h);
 361:     sync();
 362:   }
 363: 
 364:   public void setLayout(LayoutManager l)
 365:   {
 366:     LayoutManager old = super.getLayout();
 367:     ScrollPaneLayout tmp = (ScrollPaneLayout) l;
 368:     super.setLayout(l);
 369:     tmp.syncWithScrollPane(this);
 370:     firePropertyChange("layout", old, l);
 371:     sync();
 372:   }
 373: 
 374:   public void setRowHeader(JViewport v)
 375:   {
 376:     if (rowHeader == v)
 377:       return;
 378:     
 379:     JViewport old = rowHeader;
 380:     removeNonNull(old);
 381:     rowHeader = v;
 382:     addNonNull(v);
 383:     firePropertyChange("rowHeader", old, v);
 384:     sync();
 385:   }
 386: 
 387:   public void setRowHeaderView(Component c)
 388:   {
 389:     if (rowHeader == null)
 390:       setRowHeader(createViewport());
 391:     rowHeader.setView(c);
 392:     sync();
 393:   }
 394: 
 395:   public void setVerticalScrollBar(JScrollBar v)
 396:   {
 397:     if (verticalScrollBar == v)
 398:       return;
 399:     
 400:     JScrollBar old = verticalScrollBar;
 401:     removeNonNull(old);
 402:     verticalScrollBar = v;
 403:     addNonNull(v);
 404:     firePropertyChange("verticalScrollBar", old, v);
 405:     sync();
 406: 
 407:     if (old != null)
 408:       {
 409:         BoundedRangeModel model = old.getModel();
 410:         if (model != null)
 411:           model.removeChangeListener(scrollListener);
 412:       }
 413:     if (v != null)
 414:       {
 415:         BoundedRangeModel model = v.getModel();
 416:         if (model != null)
 417:           model.addChangeListener(scrollListener);
 418:       }
 419:   }
 420: 
 421:   public void setVerticalScrollBarPolicy(int v)
 422:   {
 423:     if (verticalScrollBarPolicy == v)
 424:       return;
 425:     
 426:     if (v != VERTICAL_SCROLLBAR_AS_NEEDED
 427:         && v != VERTICAL_SCROLLBAR_NEVER
 428:         && v != VERTICAL_SCROLLBAR_ALWAYS)
 429:       throw new IllegalArgumentException("unknown vertical scrollbar policy");    
 430:     
 431:     int old = verticalScrollBarPolicy;
 432:     verticalScrollBarPolicy = v;
 433:     firePropertyChange("verticalScrollBarPolicy", old, v);
 434:     sync();
 435:   }
 436: 
 437:   public void setWheelScrollingEnabled(boolean b)
 438:   {
 439:     if (wheelScrollingEnabled == b)
 440:       return;
 441:     
 442:     boolean old = wheelScrollingEnabled;
 443:     wheelScrollingEnabled = b;
 444:     firePropertyChange("wheelScrollingEnabled", old, b);
 445:     sync();
 446:   }
 447: 
 448:   public void setViewport(JViewport v)
 449:   {
 450:     if (viewport == v)
 451:       return;
 452:     
 453:     JViewport old = viewport;
 454:     removeNonNull(old);
 455:     if (old != null)
 456:       old.removeChangeListener(scrollListener);
 457:     viewport = v;
 458:     if (v != null)
 459:       v.addChangeListener(scrollListener);
 460:     addNonNull(v);
 461:     revalidate();
 462:     repaint();
 463:     firePropertyChange("viewport", old, v);
 464:     sync();
 465:   }
 466: 
 467:   public void setViewportBorder(Border b)
 468:   {
 469:     if (viewportBorder == b)
 470:       return;
 471:     
 472:     Border old = viewportBorder;
 473:     viewportBorder = b;
 474:     firePropertyChange("viewportBorder", old, b);
 475:     sync();
 476:   }
 477:     
 478:   public void setViewportView(Component view)
 479:   {
 480:     if (getViewport() == null)
 481:       {
 482:         setViewport(createViewport());
 483:       }
 484:     
 485:     if (view != null)
 486:       {
 487:         getViewport().setView(view);
 488:       }
 489:     sync();
 490:   }
 491: 
 492:   public boolean isValidateRoot()
 493:   {
 494:     return true;
 495:   }
 496: 
 497:   ChangeListener createScrollListener()
 498:   {
 499:     return new ChangeListener() 
 500:       {
 501:         
 502:         public void stateChanged(ChangeEvent event)
 503:         {
 504:           JScrollBar vsb = JScrollPane.this.getVerticalScrollBar();
 505:           JScrollBar hsb = JScrollPane.this.getHorizontalScrollBar();
 506:           JViewport vp = JScrollPane.this.getViewport();
 507: 
 508:           if (vp != null && event.getSource() == vp)
 509:             {
 510:               // if the viewport changed, we should update the VSB / HSB
 511:               // models according to the new vertical and horizontal sizes
 512: 
 513:               Rectangle vr = vp.getViewRect();
 514:               Dimension vs = vp.getViewSize();
 515:               if (vsb != null
 516:                   && (vsb.getMinimum() != 0
 517:                       || vsb.getMaximum() != vs.height
 518:                       || vsb.getValue() != vr.y
 519:                       || vsb.getVisibleAmount() != vr.height))
 520:                 vsb.setValues(vr.y, vr.height, 0, vs.height);
 521: 
 522:               if (hsb != null
 523:                   && (hsb.getMinimum() != 0
 524:                       || hsb.getMaximum() != vs.width
 525:                       || hsb.getValue() != vr.width
 526:                       || hsb.getVisibleAmount() != vr.height))
 527:                 hsb.setValues(vr.x, vr.width, 0, vs.width);
 528:             }
 529:           else
 530:             {
 531:               // otherwise we got a change update from either the VSB or
 532:               // HSB model, and we need to update the viewport positions of
 533:               // both the main viewport and any row or column headers to
 534:               // match.
 535: 
 536:               int xpos = 0;
 537:               int ypos = 0;
 538:               
 539:               if (vsb != null)
 540:                 ypos = vsb.getValue();
 541:               
 542:               if (hsb != null)
 543:                 xpos = hsb.getValue();
 544: 
 545:               Point pt = new Point(xpos, ypos);
 546: 
 547:               if (vp != null
 548:                   && vp.getViewPosition() != pt)
 549:                 vp.setViewPosition(pt);
 550: 
 551:               pt.x = 0;
 552: 
 553:               if (rowHeader != null 
 554:                   && rowHeader.getViewPosition() != pt)
 555:                 rowHeader.setViewPosition(pt);
 556:               
 557:               pt.x = xpos;
 558:               pt.y = 0;
 559: 
 560:               if (columnHeader != null 
 561:                   && columnHeader.getViewPosition() != pt)
 562:                 columnHeader.setViewPosition(pt);
 563: 
 564:             }
 565:         }
 566:       };
 567:   }
 568: 
 569: 
 570:   /**
 571:    * Creates a new <code>JScrollPane</code> without a view. The scrollbar
 572:    * policy is set to {@link #VERTICAL_SCROLLBAR_AS_NEEDED} and
 573:    * {@link #HORIZONTAL_SCROLLBAR_AS_NEEDED}.
 574:    *
 575:    * @param view the component that is embedded inside the JScrollPane
 576:    */
 577:   public JScrollPane() 
 578:   {
 579:     this(null);
 580:   }
 581: 
 582:   /**
 583:    * Creates a new <code>JScrollPane</code> that embeds the specified
 584:    * <code>view</code> component, displaying vertical and horizontal scrollbars
 585:    * as needed.
 586:    *
 587:    * @param view the component that is embedded inside the JScrollPane
 588:    */
 589:   public JScrollPane(Component view) 
 590:   {
 591:     this(view, 
 592:          VERTICAL_SCROLLBAR_AS_NEEDED, 
 593:          HORIZONTAL_SCROLLBAR_AS_NEEDED);
 594:   }
 595: 
 596:   /**
 597:    * Creates a new <code>JScrollPane</code> without a view; The scrollbar
 598:    * policies are set to <code>vsbPolicy</code> and <code>hsbPolicy</code>.
 599:    *
 600:    * @param vsbPolicy the vertical scrollbar policy to set
 601:    * @param hsbPolicy the vertical scrollbar policy to set
 602:    *
 603:    * @see {@link ScrollPaneConstants#HORIZONTAL_SCROLLBAR_ALWAYS}
 604:    * @see {@link ScrollPaneConstants#HORIZONTAL_SCROLLBAR_AS_NEEDED}
 605:    * @see {@link ScrollPaneConstants#HORIZONTAL_SCROLLBAR_NEVER}
 606:    * @see {@link ScrollPaneConstants#VERTICAL_SCROLLBAR_ALWAYS}
 607:    * @see {@link ScrollPaneConstants#VERTICAL_SCROLLBAR_AS_NEEDED}
 608:    * @see {@link ScrollPaneConstants#VERTICAL_SCROLLBAR_NEVER}
 609:    */
 610:   public JScrollPane(int vsbPolicy, int hsbPolicy) 
 611:   {
 612:     this(null, vsbPolicy, hsbPolicy);
 613:   }
 614: 
 615:   /**
 616:    * Creates a new <code>JScrollPane</code> that embeds the specified
 617:    * <code>view</code> component; The scrollbar
 618:    * policies are set to <code>vsbPolicy</code> and <code>hsbPolicy</code>.
 619:    *
 620:    * @param vsbPolicy the vertical scrollbar policy to set
 621:    * @param hsbPolicy the vertical scrollbar policy to set
 622:    *
 623:    * @see {@link ScrollPaneConstants#HORIZONTAL_SCROLLBAR_ALWAYS}
 624:    * @see {@link ScrollPaneConstants#HORIZONTAL_SCROLLBAR_AS_NEEDED}
 625:    * @see {@link ScrollPaneConstants#HORIZONTAL_SCROLLBAR_NEVER}
 626:    * @see {@link ScrollPaneConstants#VERTICAL_SCROLLBAR_ALWAYS}
 627:    * @see {@link ScrollPaneConstants#VERTICAL_SCROLLBAR_AS_NEEDED}
 628:    * @see {@link ScrollPaneConstants#VERTICAL_SCROLLBAR_NEVER}
 629:    */
 630:   public JScrollPane(Component view, int vsbPolicy, int hsbPolicy) 
 631:   {
 632:     scrollListener = createScrollListener();
 633:     setVerticalScrollBarPolicy(vsbPolicy);
 634:     setVerticalScrollBar(createVerticalScrollBar());
 635:     setHorizontalScrollBarPolicy(hsbPolicy);
 636:     setHorizontalScrollBar(createHorizontalScrollBar());
 637:     viewport = createViewport();
 638:     if (view != null)
 639:       getViewport().setView(view);
 640:     viewport.addChangeListener(scrollListener);
 641:     add(viewport,0);
 642:     setLayout(new ScrollPaneLayout());
 643:     setOpaque(false);
 644:     updateUI();
 645:   }
 646: 
 647:   
 648:   public JScrollBar createHorizontalScrollBar()
 649:   {
 650:     return new ScrollBar(SwingConstants.HORIZONTAL);
 651:   }
 652: 
 653:   public JScrollBar createVerticalScrollBar()
 654:   {
 655:     return new ScrollBar(SwingConstants.VERTICAL);
 656:   }
 657:     
 658:   protected JViewport createViewport()
 659:   {
 660:     return new JViewport();
 661:   }
 662: 
 663:   public String getUIClassID()
 664:   {
 665:     return "ScrollPaneUI";
 666:   }
 667:   
 668:   public void updateUI()
 669:   {
 670:     ScrollPaneUI b = (ScrollPaneUI)UIManager.getUI(this);
 671:     setUI(b);
 672:   }  
 673: 
 674:   /**
 675:    * This method returns the scrollpane's UI delegate.
 676:    *
 677:    * @return The scrollpane's UI delegate.
 678:    */
 679:   public ScrollPaneUI getUI()
 680:   {
 681:     return (ScrollPaneUI) ui;
 682:   }
 683: 
 684:   /**
 685:    * This method sets the scrollpane's UI delegate.
 686:    *
 687:    * @param ui The scrollpane's UI delegate.
 688:    */
 689:   public void setUI(ScrollPaneUI ui)
 690:   {
 691:     super.setUI(ui);
 692:   }
 693: 
 694:   protected class ScrollBar 
 695:     extends JScrollBar
 696:     implements UIResource
 697:   {
 698:     private static final long serialVersionUID = -42032395320987283L;
 699: 
 700:     public ScrollBar(int orientation)
 701:     {
 702:       super(orientation);
 703:     }
 704: 
 705:     public int getBlockIncrement(int direction)
 706:     {
 707:       Component view = JScrollPane.this.getViewport().getView();
 708:       if (view == null || (! (view instanceof Scrollable)))
 709:         return super.getBlockIncrement(direction);
 710:       else
 711:         {
 712:           Scrollable s = (Scrollable) view;
 713:           return s.getScrollableBlockIncrement(JScrollPane.this.getViewport().getViewRect(), 
 714:                                                this.getOrientation(),
 715:                                                direction);
 716:         }
 717:     }
 718: 
 719:     public int getUnitIncrement(int direction)
 720:     {
 721:       Component view = JScrollPane.this.getViewport().getView();
 722:       if (view == null || (! (view instanceof Scrollable)))
 723:         return super.getUnitIncrement(direction);
 724:       else
 725:         {
 726:           Scrollable s = (Scrollable) view;
 727:           return s.getScrollableUnitIncrement(JScrollPane.this.getViewport().getViewRect(), 
 728:                                               this.getOrientation(),
 729:                                               direction);
 730:         }
 731:     }
 732:   }
 733: }