1 /* AbstractLayoutCache.java --
2 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package javax.swing.tree;
41 import java.awt.Rectangle;
42 import java.util.Enumeration;
44 import javax.swing.event.TreeModelEvent;
47 * class AbstractLayoutCache
49 * @author Andrew Selkirk
51 public abstract class AbstractLayoutCache
55 * class NodeDimensions
57 public abstract static class NodeDimensions
60 * Creates <code>NodeDimensions</code> object.
62 public NodeDimensions()
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
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
79 public abstract Rectangle getNodeDimensions(Object value, int row,
80 int depth, boolean expanded,
87 protected NodeDimensions nodeDimensions;
92 protected TreeModel treeModel;
97 protected TreeSelectionModel treeSelectionModel;
102 protected boolean rootVisible;
107 protected int rowHeight;
110 * Constructor AbstractLayoutCache
112 public AbstractLayoutCache()
120 * @param dimensions TODO
122 public void setNodeDimensions(NodeDimensions dimensions)
124 nodeDimensions = dimensions;
130 * @return NodeDimensions
132 public NodeDimensions getNodeDimensions()
134 return nodeDimensions;
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.
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
149 protected Rectangle getNodeDimensions(Object value, int row, int depth,
150 boolean expanded, Rectangle bounds)
153 if (nodeDimensions != null)
154 d = nodeDimensions.getNodeDimensions(value, row, depth, expanded,
160 * Sets the model that provides the tree data.
162 * @param model the model
164 public void setModel(TreeModel model)
170 * Returns the model that provides the tree data.
174 public TreeModel getModel()
182 * @param visible <code>true</code> if root should be visible,
183 * <code>false</code> otherwise
185 public void setRootVisible(boolean visible)
187 rootVisible = visible;
193 * @return <code>true</code> if root is visible,
194 * <code>false</code> otherwise
196 public boolean isRootVisible()
204 * @param height the row height
206 public void setRowHeight(int height)
215 * @return the row height
217 public int getRowHeight()
225 * @param model the model
227 public void setSelectionModel(TreeSelectionModel model)
229 if (treeSelectionModel != null)
230 treeSelectionModel.setRowMapper(null);
231 treeSelectionModel = model;
232 if (treeSelectionModel != null)
233 treeSelectionModel.setRowMapper(this);
242 public TreeSelectionModel getSelectionModel()
244 return treeSelectionModel;
248 * Get the sum of heights for all rows. This class provides a general not
249 * optimized implementation that is overridded in derived classes
250 * ({@link VariableHeightLayoutCache}, {@link FixedHeightLayoutCache}) for
251 * the better performance.
253 public int getPreferredHeight()
256 int n = getRowCount();
257 Rectangle r = new Rectangle();
258 for (int i = 0; i < n; i++)
260 TreePath path = getPathForRow(i);
261 height += getBounds(path, r).height;
267 * Get the maximal width. This class provides a general not
268 * optimized implementation that is overridded in derived classes
269 * ({@link VariableHeightLayoutCache}, {@link FixedHeightLayoutCache}) for
270 * the better performance.
272 * @param rect the rectangle that is used during the method work
274 public int getPreferredWidth(Rectangle rect)
276 int maximalWidth = 0;
277 Rectangle r = new Rectangle();
278 int n = getRowCount();
279 for (int i = 0; i < n; i++)
281 TreePath path = getPathForRow(i);
282 r.setBounds(0, 0, 0, 0);
283 r = getBounds(path, r);
284 if (r.x + r.width > maximalWidth)
285 maximalWidth = r.x + r.width;
286 // Invalidate the cached value as this may be the very early call
287 // before the heigth is properly set (the vertical coordinate may
289 invalidatePathBounds(path);
300 public abstract boolean isExpanded(TreePath value0);
310 public abstract Rectangle getBounds(TreePath value0, Rectangle value1);
317 * @return the tree path
319 public abstract TreePath getPathForRow(int row);
324 * @param path the tree path
328 public abstract int getRowForPath(TreePath path);
336 * @return the tree path
338 public abstract TreePath getPathClosestTo(int value0, int value1);
341 * getVisiblePathsFrom
343 * @param path the tree path
345 * @return Enumeration
347 public abstract Enumeration<TreePath> getVisiblePathsFrom(TreePath path);
350 * getVisibleChildCount
352 * @param path the tree path
356 public abstract int getVisibleChildCount(TreePath path);
365 public abstract void setExpandedState(TreePath value0, boolean value1);
370 * @param path the tree path
374 public abstract boolean getExpandedState(TreePath path);
379 * @return the number of rows
381 public abstract int getRowCount();
386 public abstract void invalidateSizes();
389 * invalidatePathBounds
391 * @param path the tree path
393 public abstract void invalidatePathBounds(TreePath path);
398 * @param event the event to send
400 public abstract void treeNodesChanged(TreeModelEvent event);
405 * @param event the event to send
407 public abstract void treeNodesInserted(TreeModelEvent event);
412 * @param event the event to send
414 public abstract void treeNodesRemoved(TreeModelEvent event);
417 * treeStructureChanged
419 * @param event the event to send
421 public abstract void treeStructureChanged(TreeModelEvent event);
424 * Get the tree row numbers for the given pathes. This method performs
425 * the "bulk" conversion that may be faster than mapping pathes one by
426 * one. To have the benefit from the bulk conversion, the method must be
427 * overridden in the derived classes. The default method delegates work
428 * to the {@link #getRowForPath(TreePath)}.
430 * @param paths the tree paths the array of the tree pathes.
431 * @return the array of the matching tree rows.
433 public int[] getRowsForPaths(TreePath[] paths)
438 rows = new int[paths.length];
439 for (int i = 0; i < rows.length; i++)
440 rows[i] = getRowForPath(paths[i]);
446 * Returns true if this layout supposes that all rows have the fixed
449 * @return boolean true if all rows in the tree must have the fixed
450 * height (false by default).
452 protected boolean isFixedRowHeight()
454 return rowHeight > 0;