Source for javax.swing.tree.AbstractLayoutCache

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