1 /* FixedHeightLayoutCache.java -- Fixed cell height tree layout cache
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. */
38 package javax
.swing
.tree
;
40 import gnu
.javax
.swing
.tree
.GnuPath
;
42 import java
.awt
.Rectangle
;
43 import java
.util
.Enumeration
;
44 import java
.util
.HashSet
;
45 import java
.util
.Hashtable
;
46 import java
.util
.LinkedList
;
48 import java
.util
.Vector
;
50 import javax
.swing
.event
.TreeModelEvent
;
54 * The fixed height tree layout. This class assumes that all cells in the tree
55 * have the same fixed height. This may be not the case, for instance, if leaves
56 * and branches have different height, of if the tree rows may have arbitrary
57 * variable height. This class will also work if the NodeDimensions are not
60 * @author Audrius Meskauskas
61 * @author Andrew Selkirk
63 public class FixedHeightLayoutCache
64 extends VariableHeightLayoutCache
67 * The cached node record.
71 NodeRecord(int aRow
, int aDepth
, Object aNode
, Object aParent
)
78 isExpanded
= expanded
.contains(aNode
);
82 * The row, where the tree node is displayed.
92 * The parent of the given node, null for the root node.
102 * True for the expanded nodes. The value is calculated in constructor.
103 * Using this field saves one hashtable access operation.
105 final boolean isExpanded
;
108 * The cached bounds of the tree row.
113 * The path from the tree top to the given node (computed under first
116 private TreePath path
;
119 * Get the path for this node. The derived class is returned,
120 * making check for the last child of some parent easier.
126 boolean lastChild
= false;
129 int nc
= treeModel
.getChildCount(parent
);
132 int n
= treeModel
.getIndexOfChild(parent
, node
);
138 LinkedList lpath
= new LinkedList();
139 NodeRecord rp
= this;
142 lpath
.addFirst(rp
.node
);
143 if (rp
.parent
!= null)
145 Object parent
= rp
.parent
;
146 rp
= (NodeRecord
) nodes
.get(parent
);
147 // Add the root node, even if it is not visible.
149 lpath
.addFirst(parent
);
154 path
= new GnuPath(lpath
.toArray(), lastChild
);
160 * Get the rectangle bounds (compute, if required).
162 Rectangle
getBounds()
164 // This method may be called in the context when the tree rectangle is
165 // not known. To work around this, it is assumed near infinitely large.
167 bounds
= getNodeDimensions(node
, row
, depth
, isExpanded
,
174 * The set of all expanded tree nodes.
176 Set expanded
= new HashSet();
179 * Maps nodes to the row numbers.
181 Hashtable nodes
= new Hashtable();
184 * Maps row numbers to nodes.
186 Hashtable row2node
= new Hashtable();
189 * If true, the row map must be recomputed before using.
194 * The cumulative height of all rows.
204 * Creates the unitialised instance. Before using the class, the row height
205 * must be set with the {@link #setRowHeight(int)} and the model must be set
206 * with {@link #setModel(TreeModel)}. The node dimensions may not be set.
208 public FixedHeightLayoutCache()
210 // Nothing to do here.
214 * Get the total number of rows in the tree. Every displayed node occupies the
215 * single row. The root node row is included if the root node is set as
216 * visible (false by default).
218 * @return int the number of the displayed rows.
220 public int getRowCount()
223 return row2node
.size();
227 * Refresh the row map.
229 private final void update()
234 totalHeight
= maximalWidth
= 0;
236 Object root
= treeModel
.getRoot();
240 countRows(root
, null, 0);
244 int sc
= treeModel
.getChildCount(root
);
245 for (int i
= 0; i
< sc
; i
++)
247 Object child
= treeModel
.getChild(root
, i
);
248 countRows(child
, root
, 0);
255 * Recursively counts all rows in the tree.
257 private final void countRows(Object node
, Object parent
, int depth
)
259 Integer n
= new Integer(row2node
.size());
260 row2node
.put(n
, node
);
262 NodeRecord nr
= new NodeRecord(n
.intValue(), depth
, node
, parent
);
265 // For expanded nodes and for the root node.
266 if (expanded
.contains(node
))
268 int sc
= treeModel
.getChildCount(node
);
269 int deeper
= depth
+1;
270 for (int i
= 0; i
< sc
; i
++)
272 Object child
= treeModel
.getChild(node
, i
);
273 countRows(child
, node
, deeper
);
279 * Discard the bound information for the given path.
281 * @param path the path, for that the bound information must be recomputed.
283 public void invalidatePathBounds(TreePath path
)
285 NodeRecord r
= (NodeRecord
) nodes
.get(path
.getLastPathComponent());
291 * Mark all cached information as invalid.
293 public void invalidateSizes()
299 * Set the expanded state of the given path. The expansion states must be
300 * always updated when expanding and colapsing the tree nodes. Otherwise
301 * other methods will not work correctly after the nodes are collapsed or
304 * @param path the tree path, for that the state is being set.
305 * @param isExpanded the expanded state of the given path.
307 public void setExpandedState(TreePath path
, boolean isExpanded
)
310 expanded
.add(path
.getLastPathComponent());
312 expanded
.remove(path
.getLastPathComponent());
318 * Get the expanded state for the given tree path.
320 * @return true if the given path is expanded, false otherwise.
322 public boolean isExpanded(TreePath path
)
324 return expanded
.contains(path
.getLastPathComponent());
328 * Get bounds for the given tree path.
330 * @param path the tree path
331 * @param rect the rectangle that will be reused to return the result.
332 * @return Rectangle the bounds of the last line, defined by the given path.
334 public Rectangle
getBounds(TreePath path
, Rectangle rect
)
340 Object last
= path
.getLastPathComponent();
341 NodeRecord r
= (NodeRecord
) nodes
.get(last
);
343 // This node is not visible.
345 rect
.x
= rect
.y
= rect
.width
= rect
.height
= 0;
349 if (r
.bounds
== null)
351 Rectangle dim
= getNodeDimensions(last
, r
.row
, r
.depth
,
356 rect
.setRect(r
.bounds
);
362 * Get the path, the last element of that is displayed in the given row.
365 * @return TreePath the path
367 public TreePath
getPathForRow(int row
)
371 Object last
= row2node
.get(new Integer(row
));
376 NodeRecord r
= (NodeRecord
) nodes
.get(last
);
382 * Get the row, displaying the last node of the given path.
384 * @param path the path
385 * @return int the row number or -1 if the end of the path is not visible.
387 public int getRowForPath(TreePath path
)
394 NodeRecord r
= (NodeRecord
) nodes
.get(path
.getLastPathComponent());
402 * Get the path, closest to the given point.
404 * @param x the point x coordinate
405 * @param y the point y coordinate
406 * @return the tree path, closest to the the given point
408 public TreePath
getPathClosestTo(int x
, int y
)
413 // As the rows have arbitrary height, we need to iterate.
414 NodeRecord best
= null;
416 Enumeration en
= nodes
.elements();
418 int dist
= Integer
.MAX_VALUE
;
420 while (en
.hasMoreElements() && dist
> 0)
422 r
= (NodeRecord
) en
.nextElement();
426 dist
= distance(r
.getBounds(), x
, y
);
430 int rr
= distance(r
.getBounds(), x
, y
);
442 return best
.getPath();
446 * Get the closest distance from this point till the given rectangle. Only
447 * vertical distance is taken into consideration.
449 int distance(Rectangle r
, int x
, int y
)
453 else if (y
> r
.y
+ r
.height
)
454 return y
- (r
.y
+ r
.height
);
460 * Get the number of the visible childs for the given tree path. If the node
461 * is not expanded, 0 is returned. Otherwise, the number of children is
462 * obtained from the model as the number of children for the last path
465 * @param path the tree path
466 * @return int the number of the visible childs (for row).
468 public int getVisibleChildCount(TreePath path
)
470 if (isExpanded(path
))
473 return treeModel
.getChildCount(path
.getLastPathComponent());
477 * Get the enumeration over all visible pathes that start from the given
480 * @param parentPath the parent path
481 * @return the enumeration over pathes
483 public Enumeration
getVisiblePathsFrom(TreePath parentPath
)
487 Vector p
= new Vector(parentPath
.getPathCount());
491 for (int i
= 0; i
< parentPath
.getPathCount(); i
++)
493 node
= parentPath
.getPathComponent(i
);
494 nr
= (NodeRecord
) nodes
.get(node
);
502 * Return the expansion state of the given tree path. The expansion state
503 * must be previously set with the
504 * {@link #setExpandedState(TreePath, boolean)}
506 * @param path the path being checked
507 * @return true if the last node of the path is expanded, false otherwise.
509 public boolean getExpandedState(TreePath path
)
511 return expanded
.contains(path
.getLastPathComponent());
515 * The listener method, called when the tree nodes are changed.
517 * @param event the change event
519 public void treeNodesChanged(TreeModelEvent event
)
525 * The listener method, called when the tree nodes are inserted.
527 * @param event the change event
529 public void treeNodesInserted(TreeModelEvent event
)
535 * The listener method, called when the tree nodes are removed.
537 * @param event the change event
539 public void treeNodesRemoved(TreeModelEvent event
)
545 * Called when the tree structure has been changed.
547 * @param event the change event
549 public void treeStructureChanged(TreeModelEvent event
)
555 * Set the tree model that will provide the data.
557 public void setModel(TreeModel newModel
)
559 treeModel
= newModel
;
560 // The root node is expanded by default.
561 expanded
.add(treeModel
.getRoot());
566 * Inform the instance if the tree root node is visible. If this method
567 * is not called, it is assumed that the tree root node is not visible.
569 * @param visible true if the tree root node is visible, false
572 public void setRootVisible(boolean visible
)
574 rootVisible
= visible
;
579 * Get the sum of heights for all rows.
581 public int getPreferredHeight()
586 Enumeration en
= nodes
.elements();
587 while (en
.hasMoreElements())
589 NodeRecord nr
= (NodeRecord
) en
.nextElement();
590 Rectangle r
= nr
.getBounds();
591 totalHeight
+= r
.height
;
597 * Get the maximal width.
599 public int getPreferredWidth(Rectangle value
)
605 Enumeration en
= nodes
.elements();
606 while (en
.hasMoreElements())
608 NodeRecord nr
= (NodeRecord
) en
.nextElement();
609 Rectangle r
= nr
.getBounds();
610 if (r
.x
+ r
.width
> maximalWidth
)
611 maximalWidth
= r
.x
+ r
.width
;
617 * Returns true if this layout supposes that all rows have the fixed
620 * @return boolean true if all rows in the tree must have the fixed
621 * height (true by default).
623 protected boolean isFixedRowHeight()