Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / tree / AbstractLayoutCache.java
blob8dbdd2f5e58b23a2640dc17cc61ea4428c02416a
1 /* AbstractLayoutCache.java --
2 Copyright (C) 2002, 2004 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)
9 any later version.
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
19 02110-1301 USA.
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.
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 gnu.classpath.NotImplementedException;
43 import java.awt.Rectangle;
44 import java.util.Enumeration;
46 import javax.swing.event.TreeModelEvent;
47 import javax.swing.tree.VariableHeightLayoutCache.NodeRecord;
49 /**
50 * class AbstractLayoutCache
52 * @author Andrew Selkirk
54 public abstract class AbstractLayoutCache
55 implements RowMapper
57 /**
58 * class NodeDimensions
60 public abstract static class NodeDimensions
62 /**
63 * Creates <code>NodeDimensions</code> object.
65 public NodeDimensions()
67 // Do nothing here.
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.
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
82 public abstract Rectangle getNodeDimensions(Object value, int row,
83 int depth, boolean expanded,
84 Rectangle bounds);
87 /**
88 * nodeDimensions
90 protected NodeDimensions nodeDimensions;
92 /**
93 * treeModel
95 protected TreeModel treeModel;
97 /**
98 * treeSelectionModel
100 protected TreeSelectionModel treeSelectionModel;
103 * rootVisible
105 protected boolean rootVisible;
108 * rowHeight
110 protected int rowHeight;
113 * Constructor AbstractLayoutCache
115 public AbstractLayoutCache()
117 // Do nothing here.
121 * setNodeDimensions
123 * @param dimensions TODO
125 public void setNodeDimensions(NodeDimensions dimensions)
127 nodeDimensions = dimensions;
131 * getNodeDimensions
133 * @return NodeDimensions
135 public NodeDimensions getNodeDimensions()
137 return nodeDimensions;
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.
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
152 protected Rectangle getNodeDimensions(Object value, int row, int depth,
153 boolean expanded, Rectangle bounds)
155 if (nodeDimensions == null)
156 throw new InternalError("The NodeDimensions are not set");
157 return nodeDimensions.getNodeDimensions(value, row, depth, expanded, bounds);
161 * Sets the model that provides the tree data.
163 * @param model the model
165 public void setModel(TreeModel model)
167 treeModel = model;
171 * Returns the model that provides the tree data.
173 * @return the model
175 public TreeModel getModel()
177 return treeModel;
181 * setRootVisible
183 * @param visible <code>true</code> if root should be visible,
184 * <code>false</code> otherwise
186 public void setRootVisible(boolean visible)
188 rootVisible = visible;
192 * isRootVisible
194 * @return <code>true</code> if root is visible,
195 * <code>false</code> otherwise
197 public boolean isRootVisible()
199 return rootVisible;
203 * setRowHeight
205 * @param height the row height
207 public void setRowHeight(int height)
209 rowHeight = height;
210 invalidateSizes();
214 * getRowHeight
216 * @return the row height
218 public int getRowHeight()
220 return rowHeight;
224 * setSelectionModel
226 * @param model the model
228 public void setSelectionModel(TreeSelectionModel model)
230 treeSelectionModel = model;
234 * getSelectionModel
236 * @return the model
238 public TreeSelectionModel getSelectionModel()
240 return treeSelectionModel;
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.
249 public int getPreferredHeight()
251 int height = 0;
252 int n = getRowCount();
253 Rectangle r = new Rectangle();
254 for (int i = 0; i < n; i++)
256 TreePath path = getPathForRow(i);
257 height += getBounds(path, r).height;
259 return height;
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.
268 * @param rect the rectangle that is used during the method work
270 public int getPreferredWidth(Rectangle rect)
272 int maximalWidth = 0;
273 Rectangle r = new Rectangle();
274 int n = getRowCount();
275 for (int i = 0; i < n; i++)
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);
287 return maximalWidth;
290 * isExpanded
292 * @param value0 TODO
294 * @return boolean
296 public abstract boolean isExpanded(TreePath value0);
299 * getBounds
301 * @param value0 TODO
302 * @param value1 TODO
304 * @return Rectangle
306 public abstract Rectangle getBounds(TreePath value0, Rectangle value1);
309 * getPathForRow
311 * @param row the row
313 * @return the tree path
315 public abstract TreePath getPathForRow(int row);
318 * getRowForPath
320 * @param path the tree path
322 * @return the row
324 public abstract int getRowForPath(TreePath path);
327 * getPathClosestTo
329 * @param value0 TODO
330 * @param value1 TODO
332 * @return the tree path
334 public abstract TreePath getPathClosestTo(int value0, int value1);
337 * getVisiblePathsFrom
339 * @param path the tree path
341 * @return Enumeration
343 public abstract Enumeration getVisiblePathsFrom(TreePath path);
346 * getVisibleChildCount
348 * @param path the tree path
350 * @return int
352 public abstract int getVisibleChildCount(TreePath path);
355 * setExpandedState
357 * @param value0 TODO
359 * @param value1 TODO
361 public abstract void setExpandedState(TreePath value0, boolean value1);
364 * getExpandedState
366 * @param path the tree path
368 * @return boolean
370 public abstract boolean getExpandedState(TreePath path);
373 * getRowCount
375 * @return the number of rows
377 public abstract int getRowCount();
380 * invalidateSizes
382 public abstract void invalidateSizes();
385 * invalidatePathBounds
387 * @param path the tree path
389 public abstract void invalidatePathBounds(TreePath path);
392 * treeNodesChanged
394 * @param event the event to send
396 public abstract void treeNodesChanged(TreeModelEvent event);
399 * treeNodesInserted
401 * @param event the event to send
403 public abstract void treeNodesInserted(TreeModelEvent event);
406 * treeNodesRemoved
408 * @param event the event to send
410 public abstract void treeNodesRemoved(TreeModelEvent event);
413 * treeStructureChanged
415 * @param event the event to send
417 public abstract void treeStructureChanged(TreeModelEvent event);
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)}.
426 * @param paths the tree paths the array of the tree pathes.
427 * @return the array of the matching tree rows.
429 public int[] getRowsForPaths(TreePath[] paths)
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;
438 * Returns true if this layout supposes that all rows have the fixed
439 * height.
441 * @return boolean true if all rows in the tree must have the fixed
442 * height (false by default).
444 protected boolean isFixedRowHeight()
446 return false;