1 7 8 package javax.swing.tree; 9 10 import javax.swing.event.TreeModelEvent ; 11 import java.awt.Dimension ; 12 import java.awt.Rectangle ; 13 import java.util.Enumeration ; 14 15 28 29 public abstract class AbstractLayoutCache implements RowMapper { 30 31 protected NodeDimensions nodeDimensions; 32 33 34 protected TreeModel treeModel; 35 36 37 protected TreeSelectionModel treeSelectionModel; 38 39 43 protected boolean rootVisible; 44 45 49 protected int rowHeight; 50 51 52 59 public void setNodeDimensions(NodeDimensions nd) { 60 this.nodeDimensions = nd; 61 } 62 63 69 public NodeDimensions getNodeDimensions() { 70 return nodeDimensions; 71 } 72 73 79 public void setModel(TreeModel newModel) { 80 treeModel = newModel; 81 } 82 83 88 public TreeModel getModel() { 89 return treeModel; 90 } 91 92 103 public void setRootVisible(boolean rootVisible) { 104 this.rootVisible = rootVisible; 105 } 106 107 113 public boolean isRootVisible() { 114 return rootVisible; 115 } 116 117 127 public void setRowHeight(int rowHeight) { 128 this.rowHeight = rowHeight; 129 } 130 131 136 public int getRowHeight() { 137 return rowHeight; 138 } 139 140 146 public void setSelectionModel(TreeSelectionModel newLSM) { 147 if(treeSelectionModel != null) 148 treeSelectionModel.setRowMapper(null); 149 treeSelectionModel = newLSM; 150 if(treeSelectionModel != null) 151 treeSelectionModel.setRowMapper(this); 152 } 153 154 159 public TreeSelectionModel getSelectionModel() { 160 return treeSelectionModel; 161 } 162 163 168 public int getPreferredHeight() { 169 int rowCount = getRowCount(); 171 172 if(rowCount > 0) { 173 Rectangle bounds = getBounds(getPathForRow(rowCount - 1), 174 null); 175 176 if(bounds != null) 177 return bounds.y + bounds.height; 178 } 179 return 0; 180 } 181 182 195 public int getPreferredWidth(Rectangle bounds) { 196 int rowCount = getRowCount(); 197 198 if(rowCount > 0) { 199 TreePath firstPath; 201 int endY; 202 203 if(bounds == null) { 204 firstPath = getPathForRow(0); 205 endY = Integer.MAX_VALUE; 206 } 207 else { 208 firstPath = getPathClosestTo(bounds.x, bounds.y); 209 endY = bounds.height + bounds.y; 210 } 211 212 Enumeration paths = getVisiblePathsFrom(firstPath); 213 214 if(paths != null && paths.hasMoreElements()) { 215 Rectangle pBounds = getBounds((TreePath )paths.nextElement(), 216 null); 217 int width; 218 219 if(pBounds != null) { 220 width = pBounds.x + pBounds.width; 221 if (pBounds.y >= endY) { 222 return width; 223 } 224 } 225 else 226 width = 0; 227 while (pBounds != null && paths.hasMoreElements()) { 228 pBounds = getBounds((TreePath )paths.nextElement(), 229 pBounds); 230 if (pBounds != null && pBounds.y < endY) { 231 width = Math.max(width, pBounds.x + pBounds.width); 232 } 233 else { 234 pBounds = null; 235 } 236 } 237 return width; 238 } 239 } 240 return 0; 241 } 242 243 247 250 public abstract boolean isExpanded(TreePath path); 251 252 260 public abstract Rectangle getBounds(TreePath path, Rectangle placeIn); 261 262 269 public abstract TreePath getPathForRow(int row); 270 271 280 public abstract int getRowForPath(TreePath path); 281 282 294 public abstract TreePath getPathClosestTo(int x, int y); 295 296 307 public abstract Enumeration <TreePath > getVisiblePathsFrom(TreePath path); 308 309 315 public abstract int getVisibleChildCount(TreePath path); 316 317 324 public abstract void setExpandedState(TreePath path, boolean isExpanded); 325 326 332 public abstract boolean getExpandedState(TreePath path); 333 334 339 public abstract int getRowCount(); 340 341 345 public abstract void invalidateSizes(); 346 347 353 public abstract void invalidatePathBounds(TreePath path); 354 355 361 376 public abstract void treeNodesChanged(TreeModelEvent e); 377 378 387 public abstract void treeNodesInserted(TreeModelEvent e); 388 389 401 public abstract void treeNodesRemoved(TreeModelEvent e); 402 403 415 public abstract void treeStructureChanged(TreeModelEvent e); 416 417 421 435 public int[] getRowsForPaths(TreePath [] paths) { 436 if(paths == null) 437 return null; 438 439 int numPaths = paths.length; 440 int[] rows = new int[numPaths]; 441 442 for(int counter = 0; counter < numPaths; counter++) 443 rows[counter] = getRowForPath(paths[counter]); 444 return rows; 445 } 446 447 452 469 protected Rectangle getNodeDimensions(Object value, int row, int depth, 470 boolean expanded, 471 Rectangle placeIn) { 472 NodeDimensions nd = getNodeDimensions(); 473 474 if(nd != null) { 475 return nd.getNodeDimensions(value, row, depth, expanded, placeIn); 476 } 477 return null; 478 } 479 480 483 protected boolean isFixedRowHeight() { 484 return (rowHeight > 0); 485 } 486 487 488 492 static public abstract class NodeDimensions { 493 509 public abstract Rectangle getNodeDimensions(Object value, int row, 510 int depth, 511 boolean expanded, 512 Rectangle bounds); 513 } 514 } 515 | Popular Tags |