Source for javax.swing.tree.AbstractLayoutCache

   1: /* AbstractLayoutCache.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.tree;
  40: 
  41: import gnu.classpath.NotImplementedException;
  42: 
  43: import java.awt.Rectangle;
  44: import java.util.Enumeration;
  45: 
  46: import javax.swing.event.TreeModelEvent;
  47: import javax.swing.tree.VariableHeightLayoutCache.NodeRecord;
  48: 
  49: /**
  50:  * class AbstractLayoutCache
  51:  * 
  52:  * @author Andrew Selkirk
  53:  */
  54: public abstract class AbstractLayoutCache
  55:   implements RowMapper
  56: {
  57:   /**
  58:    * class NodeDimensions
  59:    */
  60:   public abstract static class NodeDimensions
  61:   {
  62:     /**
  63:      * Creates <code>NodeDimensions</code> object.
  64:      */
  65:     public NodeDimensions()
  66:     {
  67:       // Do nothing here.
  68:     }
  69: 
  70:    /**
  71:     * Get the node dimensions. The NodeDimensions property must be set (unless
  72:     * the method is overridden, like if {@link FixedHeightLayoutCache}. If the
  73:     * method is not overridden and the property is not set, the InternalError is
  74:     * thrown.
  75:     * 
  76:     * @param value the last node in the path
  77:     * @param row the node row
  78:     * @param depth the indentation depth
  79:     * @param expanded true if this node is expanded, false otherwise
  80:     * @param bounds the area where the tree is displayed
  81:     */
  82:     public abstract Rectangle getNodeDimensions(Object value, int row,
  83:                                                 int depth, boolean expanded,
  84:                                                 Rectangle bounds);
  85:   }
  86: 
  87:   /**
  88:    * nodeDimensions
  89:    */
  90:   protected NodeDimensions nodeDimensions;
  91: 
  92:   /**
  93:    * treeModel
  94:    */
  95:   protected TreeModel treeModel;
  96: 
  97:   /**
  98:    * treeSelectionModel
  99:    */
 100:   protected TreeSelectionModel treeSelectionModel;
 101: 
 102:   /**
 103:    * rootVisible
 104:    */
 105:   protected boolean rootVisible;
 106: 
 107:   /**
 108:    * rowHeight
 109:    */
 110:   protected int rowHeight;
 111: 
 112:   /**
 113:    * Constructor AbstractLayoutCache
 114:    */
 115:   public AbstractLayoutCache()
 116:   {
 117:     // Do nothing here.
 118:   }
 119: 
 120:   /**
 121:    * setNodeDimensions
 122:    * 
 123:    * @param dimensions TODO
 124:    */
 125:   public void setNodeDimensions(NodeDimensions dimensions)
 126:   {
 127:     nodeDimensions = dimensions;
 128:   }
 129: 
 130:   /**
 131:    * getNodeDimensions
 132:    * 
 133:    * @return NodeDimensions
 134:    */
 135:   public NodeDimensions getNodeDimensions()
 136:   {
 137:     return nodeDimensions;
 138:   }
 139: 
 140:  /**
 141:   * Get the node dimensions. The NodeDimensions property must be set
 142:   * (unless the method is overridden, like if
 143:   * {@link FixedHeightLayoutCache}. If the method is not overridden and
 144:   * the property is not set, the InternalError is thrown.
 145:   * 
 146:   * @param value the last node in the path
 147:   * @param row the node row
 148:   * @param depth the indentation depth
 149:   * @param expanded true if this node is expanded, false otherwise
 150:   * @param bounds the area where the tree is displayed
 151:   */
 152:   protected Rectangle getNodeDimensions(Object value, int row, int depth,
 153:                                         boolean expanded, Rectangle bounds)
 154:   {
 155:     if (nodeDimensions == null)
 156:       throw new InternalError("The NodeDimensions are not set");
 157:     return nodeDimensions.getNodeDimensions(value, row, depth, expanded, bounds);
 158:   }
 159: 
 160:   /**
 161:    * Sets the model that provides the tree data.
 162:    * 
 163:    * @param model the model
 164:    */
 165:   public void setModel(TreeModel model)
 166:   {
 167:     treeModel = model;
 168:   }
 169: 
 170:   /**
 171:    * Returns the model that provides the tree data.
 172:    * 
 173:    * @return the model
 174:    */
 175:   public TreeModel getModel()
 176:   {
 177:     return treeModel;
 178:   }
 179: 
 180:   /**
 181:    * setRootVisible
 182:    * 
 183:    * @param visible <code>true</code> if root should be visible,
 184:    * <code>false</code> otherwise
 185:    */
 186:   public void setRootVisible(boolean visible)
 187:   {
 188:     rootVisible = visible;
 189:   }
 190: 
 191:   /**
 192:    * isRootVisible
 193:    * 
 194:    * @return <code>true</code> if root is visible,
 195:    * <code>false</code> otherwise
 196:    */
 197:   public boolean isRootVisible()
 198:   {
 199:     return rootVisible;
 200:   }
 201: 
 202:   /**
 203:    * setRowHeight
 204:    * 
 205:    * @param height the row height
 206:    */
 207:   public void setRowHeight(int height)
 208:   {
 209:     rowHeight = height;
 210:     invalidateSizes();
 211:   }
 212: 
 213:   /**
 214:    * getRowHeight
 215:    * 
 216:    * @return the row height
 217:    */
 218:   public int getRowHeight()
 219:   {
 220:     return rowHeight;
 221:   }
 222: 
 223:   /**
 224:    * setSelectionModel
 225:    * 
 226:    * @param model the model
 227:    */
 228:   public void setSelectionModel(TreeSelectionModel model)
 229:   {
 230:     treeSelectionModel = model;
 231:   }
 232: 
 233:   /**
 234:    * getSelectionModel
 235:    * 
 236:    * @return the model
 237:    */
 238:   public TreeSelectionModel getSelectionModel()
 239:   {
 240:     return treeSelectionModel;
 241:   }
 242: 
 243:   /**
 244:    * Get the sum of heights for all rows. This class provides a general not
 245:    * optimized implementation that is overridded in derived classes 
 246:    * ({@link VariableHeightLayoutCache}, {@link FixedHeightLayoutCache}) for
 247:    * the better performance.
 248:    */
 249:   public int getPreferredHeight()
 250:   {
 251:     int height = 0;
 252:     int n = getRowCount();
 253:     Rectangle r = new Rectangle();
 254:     for (int i = 0; i < n; i++)
 255:       {
 256:         TreePath path = getPathForRow(i);
 257:         height += getBounds(path, r).height;
 258:       }
 259:     return height;
 260:   }
 261: 
 262:   /**
 263:    * Get the maximal width. This class provides a general not
 264:    * optimized implementation that is overridded in derived classes 
 265:    * ({@link VariableHeightLayoutCache}, {@link FixedHeightLayoutCache}) for
 266:    * the better performance.
 267:    * 
 268:    * @param rect the rectangle that is used during the method work
 269:    */
 270:   public int getPreferredWidth(Rectangle rect)
 271:   {
 272:     int maximalWidth = 0;
 273:     Rectangle r = new Rectangle();
 274:     int n = getRowCount();
 275:     for (int i = 0; i < n; i++)
 276:       {
 277:         TreePath path = getPathForRow(i);
 278:         r.setBounds(0,0,0,0);        
 279:         r = getBounds(path, r);
 280:         if (r.x + r.width > maximalWidth)
 281:           maximalWidth = r.x + r.width;
 282:         // Invalidate the cached value as this may be the very early call
 283:         // before the heigth is properly set (the vertical coordinate may
 284:         // not be correct).
 285:         invalidatePathBounds(path);
 286:       }
 287:     return maximalWidth;
 288:   }
 289:   /**
 290:    * isExpanded
 291:    * 
 292:    * @param value0 TODO
 293:    * 
 294:    * @return boolean
 295:    */
 296:   public abstract boolean isExpanded(TreePath value0);
 297: 
 298:   /**
 299:    * getBounds
 300:    * 
 301:    * @param value0 TODO
 302:    * @param value1 TODO
 303:    * 
 304:    * @return Rectangle
 305:    */
 306:   public abstract Rectangle getBounds(TreePath value0, Rectangle value1);
 307: 
 308:   /**
 309:    * getPathForRow
 310:    * 
 311:    * @param row the row
 312:    * 
 313:    * @return the tree path
 314:    */
 315:   public abstract TreePath getPathForRow(int row);
 316: 
 317:   /**
 318:    * getRowForPath
 319:    * 
 320:    * @param path the tree path
 321:    * 
 322:    * @return the row
 323:    */
 324:   public abstract int getRowForPath(TreePath path);
 325: 
 326:   /**
 327:    * getPathClosestTo
 328:    * 
 329:    * @param value0 TODO
 330:    * @param value1 TODO
 331:    * 
 332:    * @return the tree path
 333:    */
 334:   public abstract TreePath getPathClosestTo(int value0, int value1);
 335: 
 336:   /**
 337:    * getVisiblePathsFrom
 338:    * 
 339:    * @param path the tree path
 340:    * 
 341:    * @return Enumeration
 342:    */
 343:   public abstract Enumeration getVisiblePathsFrom(TreePath path);
 344: 
 345:   /**
 346:    * getVisibleChildCount
 347:    * 
 348:    * @param path the tree path
 349:    * 
 350:    * @return int
 351:    */
 352:   public abstract int getVisibleChildCount(TreePath path);
 353: 
 354:   /**
 355:    * setExpandedState
 356:    * 
 357:    * @param value0 TODO
 358:    * 
 359:    * @param value1 TODO
 360:    */
 361:   public abstract void setExpandedState(TreePath value0, boolean value1);
 362: 
 363:   /**
 364:    * getExpandedState
 365:    * 
 366:    * @param path the tree path
 367:    * 
 368:    * @return boolean
 369:    */
 370:   public abstract boolean getExpandedState(TreePath path);
 371: 
 372:   /**
 373:    * getRowCount
 374:    * 
 375:    * @return the number of rows
 376:    */
 377:   public abstract int getRowCount();
 378: 
 379:   /**
 380:    * invalidateSizes
 381:    */
 382:   public abstract void invalidateSizes();
 383: 
 384:   /**
 385:    * invalidatePathBounds
 386:    * 
 387:    * @param path the tree path
 388:    */
 389:   public abstract void invalidatePathBounds(TreePath path);
 390: 
 391:   /**
 392:    * treeNodesChanged
 393:    * 
 394:    * @param event the event to send
 395:    */
 396:   public abstract void treeNodesChanged(TreeModelEvent event);
 397: 
 398:   /**
 399:    * treeNodesInserted
 400:    * 
 401:    * @param event the event to send
 402:    */
 403:   public abstract void treeNodesInserted(TreeModelEvent event);
 404: 
 405:   /**
 406:    * treeNodesRemoved
 407:    * 
 408:    * @param event the event to send
 409:    */
 410:   public abstract void treeNodesRemoved(TreeModelEvent event);
 411: 
 412:   /**
 413:    * treeStructureChanged
 414:    * 
 415:    * @param event the event to send
 416:    */
 417:   public abstract void treeStructureChanged(TreeModelEvent event);
 418: 
 419:   /**
 420:    * Get the tree row numbers for the given pathes. This method performs
 421:    * the "bulk" conversion that may be faster than mapping pathes one by
 422:    * one. To have the benefit from the bulk conversion, the method must be
 423:    * overridden in the derived classes. The default method delegates work
 424:    * to the {@link #getRowForPath(TreePath)}.
 425:    * 
 426:    * @param paths the tree paths the array of the tree pathes.
 427:    * @return the array of the matching tree rows.
 428:    */
 429:   public int[] getRowsForPaths(TreePath[] paths)
 430:   {
 431:     int[] rows = new int[paths.length];
 432:     for (int i = 0; i < rows.length; i++)
 433:       rows[i] = getRowForPath(paths[i]);
 434:     return rows;
 435:   }
 436: 
 437:   /**
 438:    * Returns true if this layout supposes that all rows have the fixed
 439:    * height.
 440:    * 
 441:    * @return boolean true if all rows in the tree must have the fixed
 442:    * height (false by default).
 443:    */
 444:   protected boolean isFixedRowHeight()
 445:   {
 446:     return false; 
 447:   }
 448: }