Source for javax.swing.ScrollPaneLayout

   1: /* ScrollPaneLayout.java --
   2:    Copyright (C) 2002, 2004  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.Container;
  43: import java.awt.Dimension;
  44: import java.awt.Insets;
  45: import java.awt.LayoutManager;
  46: import java.awt.Rectangle;
  47: import java.io.Serializable;
  48: 
  49: /**
  50:  * ScrollPaneLayout
  51:  * @author    Andrew Selkirk
  52:  * @version    1.0
  53:  */
  54: public class ScrollPaneLayout
  55:   implements LayoutManager, ScrollPaneConstants, Serializable
  56: {
  57:   private static final long serialVersionUID = -4480022884523193743L;
  58: 
  59:   public static class UIResource extends ScrollPaneLayout 
  60:     implements javax.swing.plaf.UIResource
  61:   {
  62:     public UIResource()
  63:     {
  64:       super();
  65:     }
  66:   }
  67: 
  68:   protected JViewport viewport;
  69:   protected JScrollBar vsb;
  70:   protected JScrollBar hsb;
  71:   protected JViewport rowHead;
  72:   protected JViewport colHead;
  73:   protected Component lowerLeft;
  74:   protected Component lowerRight;
  75:   protected Component upperLeft;
  76:   protected Component upperRight;
  77:   protected int vsbPolicy;
  78:   protected int hsbPolicy;
  79: 
  80:   public ScrollPaneLayout()
  81:   {
  82:     // Nothing to do here.
  83:   }
  84: 
  85:   public void syncWithScrollPane(JScrollPane scrollPane) {
  86:     viewport = scrollPane.getViewport();
  87:     rowHead = scrollPane.getRowHeader();
  88:     colHead = scrollPane.getColumnHeader();
  89:     vsb = scrollPane.getVerticalScrollBar();
  90:     hsb = scrollPane.getHorizontalScrollBar();
  91:     vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
  92:     hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
  93:     lowerLeft = scrollPane.getCorner(LOWER_LEFT_CORNER);
  94:     lowerRight = scrollPane.getCorner(LOWER_RIGHT_CORNER);
  95:     upperLeft = scrollPane.getCorner(UPPER_LEFT_CORNER);
  96:     upperRight = scrollPane.getCorner(UPPER_RIGHT_CORNER);    
  97:   }
  98: 
  99:   /**
 100:    * Removes an existing component.  If oldComponent is not null
 101:    * and is not equal to newComponent, oldComponent must be removed
 102:    * from its parent.
 103:    * @param oldComponent the old Component that may need to be removed.
 104:    * @param newComponent the Component to add.
 105:    * @return the newComponent
 106:    */
 107:   protected Component addSingletonComponent(Component oldComponent,
 108:                                             Component newComponent) 
 109:   {
 110:     if (oldComponent != null && oldComponent != newComponent)
 111:       oldComponent.getParent().remove(oldComponent);
 112:     return newComponent;
 113:   }
 114: 
 115:   /**
 116:    * Add the specified component to the layout. 
 117:    * @param key must be one of VIEWPORT, VERTICAL_SCROLLBAR,
 118:    * HORIZONTAL_SCROLLBAR, ROW_HEADER, COLUMN_HEADER,
 119:    * LOWER_RIGHT_CORNER, LOWER_LEFT_CORNER, UPPER_RIGHT_CORNER,
 120:    * UPPER_LEFT_CORNER.
 121:    * @param component the Component to add
 122:    * @throws IllegalArgumentException if key is not as above
 123:    */
 124:   public void addLayoutComponent(String key, Component component) 
 125:   {
 126:     if (key == VIEWPORT)
 127:       viewport = (JViewport) component;
 128:     else if (key == VERTICAL_SCROLLBAR)
 129:       vsb = (JScrollBar) component;
 130:     else if (key == HORIZONTAL_SCROLLBAR)
 131:       hsb = (JScrollBar) component;
 132:     else if (key == ROW_HEADER)
 133:       rowHead = (JViewport) component;
 134:     else if (key == COLUMN_HEADER)
 135:       colHead = (JViewport) component;
 136:     else if (key == LOWER_RIGHT_CORNER)
 137:       lowerRight = component;
 138:     else if (key == UPPER_RIGHT_CORNER)
 139:       upperRight = component;
 140:     else if (key == LOWER_LEFT_CORNER)
 141:       lowerLeft = component;
 142:     else if (key == UPPER_LEFT_CORNER)
 143:       upperLeft = component;
 144:     else
 145:       throw new IllegalArgumentException();
 146:   }
 147: 
 148:   public void removeLayoutComponent(Component component) {
 149:     if (component == viewport)
 150:       viewport = null;
 151:     else if (component == vsb)
 152:       vsb = null;
 153:     else if (component == hsb)
 154:       hsb = null;
 155:     else if (component == rowHead)
 156:       rowHead = null;
 157:     else if (component == colHead)
 158:       colHead = null;
 159:     else if (component == lowerRight)
 160:       lowerRight = null;
 161:     else if (component == upperRight)
 162:       upperRight = null;
 163:     else if (component == lowerLeft)
 164:       lowerLeft = null;
 165:     else if (component == upperLeft)
 166:       upperLeft = null;
 167:   }
 168: 
 169:   public int getVerticalScrollBarPolicy()
 170:   {
 171:     return vsbPolicy;
 172:   }
 173:   
 174:   /**
 175:    * Sets the vertical scrollbar policy.
 176:    * @param policy must be one of VERTICAL_SCROLLBAR_AS_NEEDED,
 177:    * VERTICAL_SCROLLBAR_NEVER, VERTICAL_SCROLLBAR_ALWAYS.
 178:    * @throws IllegalArgumentException if policy is not one of the valid
 179:    * JScrollBar policies.
 180:    */
 181:   public void setVerticalScrollBarPolicy(int policy)
 182:   {
 183:     if (policy != VERTICAL_SCROLLBAR_AS_NEEDED && 
 184:         policy != VERTICAL_SCROLLBAR_NEVER &&
 185:         policy != VERTICAL_SCROLLBAR_ALWAYS)
 186:       throw new IllegalArgumentException("Illegal Scrollbar Policy");
 187:     vsbPolicy = policy;
 188:   }
 189: 
 190:   public int getHorizontalScrollBarPolicy()
 191:   {
 192:     return hsbPolicy;
 193:   }
 194: 
 195:   /**
 196:    * Sets the horizontal scrollbar policy.
 197:    * @param policy must be one of HORIZONTAL_SCROLLBAR_AS_NEEDED,
 198:    * HORIZONTAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_ALWAYS.
 199:    * @throws IllegalArgumentException if policy is not one of the valid 
 200:    * JScrollbar policies.
 201:    */
 202:   public void setHorizontalScrollBarPolicy(int policy)
 203:   {
 204:     if (policy != HORIZONTAL_SCROLLBAR_AS_NEEDED && 
 205:         policy != HORIZONTAL_SCROLLBAR_NEVER &&
 206:         policy != HORIZONTAL_SCROLLBAR_ALWAYS)
 207:       throw new IllegalArgumentException("Illegal Scrollbar Policy");
 208:     hsbPolicy = policy;
 209:   }
 210: 
 211:   public JViewport getViewport()
 212:   {
 213:     return viewport;
 214:   }
 215: 
 216:   public JScrollBar getHorizontalScrollBar()
 217:   {
 218:     return hsb;
 219:   }
 220: 
 221:   public JScrollBar getVerticalScrollBar()
 222:   {
 223:     return vsb;
 224:   }
 225: 
 226:   public JViewport getRowHeader()
 227:   {
 228:     return rowHead;
 229:   }
 230: 
 231:   public JViewport getColumnHeader()
 232:   {
 233:     return colHead;
 234:   }
 235: 
 236:   /**
 237:    * Returns the Component at the specified corner.
 238:    * @param key the corner.
 239:    * @return the Component at the specified corner, or null if
 240:    * key is not one of the four valid corners.
 241:    */
 242:   public Component getCorner(String key)
 243:   {
 244:     if (key == LOWER_RIGHT_CORNER)
 245:       return lowerRight;
 246:     else if (key == UPPER_RIGHT_CORNER)
 247:       return upperRight;
 248:     else if (key == LOWER_LEFT_CORNER)
 249:       return lowerLeft;
 250:     else if (key == UPPER_LEFT_CORNER)
 251:       return upperLeft;
 252:     return null;
 253:   }
 254: 
 255:   public Dimension preferredLayoutSize(Container parent) 
 256:   {
 257:     // Sun's implementation simply throws a ClassCastException if
 258:     // parent is no JScrollPane, so do we.
 259:     JScrollPane sc = (JScrollPane) parent;
 260:     Dimension viewportSize = viewport.getPreferredSize();
 261:     Dimension viewSize = viewport.getViewSize();
 262:     int width = viewportSize.width;
 263:     int height = viewportSize.height;
 264: 
 265:     // horizontal scrollbar needed if the view's preferred width
 266:     // is larger than the viewport's preferred width
 267:     if (hsb != null && viewSize.width > viewportSize.width)
 268:       height += hsb.getPreferredSize().height;
 269: 
 270:     // vertical scrollbar needed if the view's preferred height
 271:     // is larger than the viewport's preferred height
 272:     if (vsb != null && viewSize.height > viewportSize.height)
 273:       width += vsb.getPreferredSize().width;
 274:     if (rowHead != null && rowHead.isVisible())
 275:       width += rowHead.getPreferredSize().width;
 276:     if (colHead != null && colHead.isVisible())
 277:       height += colHead.getPreferredSize().height;
 278:     Insets i = sc.getInsets();
 279:     return new Dimension(width + i.left + i.right,
 280:                          height + i.left + i.right);
 281:   }
 282: 
 283:   public Dimension minimumLayoutSize(Container parent)
 284:   {
 285:     // Sun's implementation simply throws a ClassCastException if
 286:     // parent is no JScrollPane, so do we.
 287:     JScrollPane sc = (JScrollPane) parent;
 288:     Dimension viewportSize = viewport.getMinimumSize();
 289:     int width = viewportSize.width;
 290:     int height = viewportSize.height;
 291:     if (hsb != null && hsb.isVisible())
 292:       height += hsb.getMinimumSize().height;
 293:     if (vsb != null && vsb.isVisible())
 294:       width += vsb.getMinimumSize().width;
 295:     if (rowHead != null && rowHead.isVisible())
 296:       width += rowHead.getMinimumSize().width;
 297:     if (colHead != null && colHead.isVisible())
 298:       height += colHead.getMinimumSize().height;
 299:     Insets i = sc.getInsets();
 300:     return new Dimension(width + i.left + i.right,
 301:                          height + i.top + i.bottom);
 302:   }
 303: 
 304:   /**
 305:    *
 306:    *     +----+--------------------+----+ y1
 307:    *     | c1 |   column header    | c2 |
 308:    *     +----+--------------------+----+ y2
 309:    *     | r  |                    | v  |
 310:    *     | o  |                    |    |
 311:    *     | w  |                    | s  |
 312:    *     |    |                    | r  |
 313:    *     | h  |                    | o  |
 314:    *     | e  |      viewport      | l  |
 315:    *     | a  |                    | l  |
 316:    *     | d  |                    | b  |
 317:    *     | e  |                    | a  |
 318:    *     | r  |                    | r  |
 319:    *     +----+--------------------+----+ y3
 320:    *     | c3 |    h scrollbar     | c4 |
 321:    *     +----+--------------------+----+ y4
 322:    *    x1   x2                   x3   x4
 323:    *   
 324:    */
 325:   public void layoutContainer(Container parent)
 326:   {
 327:     // Sun's implementation simply throws a ClassCastException if
 328:     // parent is no JScrollPane, so do we.
 329:     JScrollPane sc = (JScrollPane) parent;
 330:     JViewport viewport = sc.getViewport();
 331:     Dimension viewSize = viewport.getViewSize();
 332: 
 333:     int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
 334:     int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
 335:     Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);
 336: 
 337:     x1 = scrollPaneBounds.x;
 338:     y1 = scrollPaneBounds.y;
 339:     x4 = scrollPaneBounds.x + scrollPaneBounds.width;
 340:     y4 = scrollPaneBounds.y + scrollPaneBounds.height;
 341:     if (colHead != null)
 342:       y2 = y1 + colHead.getPreferredSize().height;
 343:     else
 344:       y2 = y1;
 345: 
 346:     if (rowHead != null)
 347:       x2 = x1 + rowHead.getPreferredSize().width;
 348:     else
 349:       x2 = x1;
 350: 
 351:     int vsbPolicy = sc.getVerticalScrollBarPolicy();
 352:     int hsbPolicy = sc.getHorizontalScrollBarPolicy();
 353: 
 354:     boolean showVsb = 
 355:       (vsb != null)
 356:       && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
 357:           || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED 
 358:               && viewSize.height > (y4 - y2)));
 359:     boolean showHsb = 
 360:       (hsb != null)
 361:       && ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
 362:           || (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED 
 363:               && viewSize.width > (x4 - x2)));
 364: 
 365:     if (!showVsb)
 366:       x3 = x4;
 367:     else
 368:       x3 = x4 - vsb.getPreferredSize().width;
 369: 
 370:     if (!showHsb)
 371:       y3 = y4;
 372:     else
 373:       y3 = y4 - hsb.getPreferredSize().height;
 374: 
 375:     // now set the layout
 376:     if (viewport != null)
 377:       viewport.setBounds(new Rectangle(x2, y2, x3 - x2, y3 - y2));
 378: 
 379:     if (colHead != null)
 380:       colHead.setBounds(new Rectangle(x2, y1, x3 - x2, y2 - y1));
 381: 
 382:     if (rowHead != null)
 383:       rowHead.setBounds(new Rectangle(x1, y2, x2 - x1, y3 - y2));
 384: 
 385:     if (showVsb)
 386:       {
 387:         vsb.setVisible(true);
 388:         vsb.setBounds(new Rectangle(x3, y2, x4 - x3, y3 - y2));
 389:       }
 390:     else if (vsb != null)
 391:       vsb.setVisible(false);
 392: 
 393:     if (showHsb)
 394:       {
 395:         hsb.setVisible(true);
 396:         hsb.setBounds(new Rectangle(x2, y3, x3 - x2, y4 - y3));
 397:       }
 398:     else if (hsb != null)
 399:       hsb.setVisible(false);
 400: 
 401:     if (upperLeft != null)
 402:       upperLeft.setBounds(new Rectangle(x1, y1, x2 - x1, y2 - y1));
 403: 
 404:     if (upperRight != null)
 405:       upperRight.setBounds(new Rectangle(x3, y1, x4 - x3, y2 - y1));
 406: 
 407:     if (lowerLeft != null)
 408:       lowerLeft.setBounds(new Rectangle(x1, y3, x2 - x1, y4 - y3));
 409: 
 410:     if (lowerRight != null)
 411:       lowerRight.setBounds(new Rectangle(x3, y3, x4 - x3, y4 - y3));
 412:   }
 413: 
 414:   /**
 415:    * Returns the bounds of the border around a ScrollPane's viewport.
 416:    *
 417:    * @param scrollPane the ScrollPane for which's viewport the border
 418:    *     is requested
 419:    *
 420:    * @deprecated As of Swing 1.1 replaced by
 421:    *     {@link javax.swing.JScrollPane#getViewportBorderBounds}.
 422:    */
 423:   public Rectangle getViewportBorderBounds(JScrollPane scrollPane) {
 424:     return null;
 425:   }
 426: 
 427: 
 428: }