TreeLayoutpublic class TreeLayout extends Object implements LayoutManagerSimple layout manager that arranges its children in a tree.
The tree always expands to fill the available area, and the
internal components are resized to fit the area proportional
to their preferred size and the actual available size.
This layout manager requires several method calls beyond the
normal layout manager. Please notice the following:
* Components must be added using the add(String, Component) method.
The strings don't have to be unique, but must be present.
* Each instance must have exactly one root object,
which must be add()ed, then setRoot()ed.
* Each component after the root must first be added and then must
be connected into the tree using setParent(child, parent).
* If you want lines between parents and children,
you must call paintLines() from your
applet's paint() method. |
Fields Summary |
---|
TreeNode | root | Hashtable | nodes |
Constructors Summary |
---|
public TreeLayout() nodes = new Hashtable();
|
Methods Summary |
---|
public void | addLayoutComponent(java.lang.String name, java.awt.Component comp)
TreeNode tn = new TreeNode(comp);
nodes.put(comp, tn);
| public void | layoutContainer(java.awt.Container target)
Insets insets = target.getInsets();
Dimension d = target.getSize();
Dimension root_pref = root.getPreferredSize();
double xscale =
((double)(d.width-insets.left-insets.right)/(double)(root_pref.width));
double yscale =
((double)(d.height-insets.top-insets.bottom)/(double)(root_pref.height));
root.doLayout(xscale, yscale, insets.left, insets.top);
| public java.awt.Dimension | minimumLayoutSize(java.awt.Container target)
Dimension d = root.getMinimumSize();
Insets insets = target.getInsets();
d.width += insets.left + insets.right;
d.height += insets.top + insets.bottom;
return d;
| public void | paintLines(java.awt.Graphics g, java.awt.Color bg)This piece of hackery is needed since one cant really draw things
from a layout manager. Call this if you want to draw lines between
components.
Color dk = bg.darker();
Color br = bg.brighter();
root.paintLines(g, dk, br);
| public java.awt.Dimension | preferredLayoutSize(java.awt.Container target)
Dimension d = root.getPreferredSize();
Insets insets = target.getInsets();
d.width += insets.left + insets.right;
d.height += insets.top + insets.bottom;
return d;
| public void | removeLayoutComponent(java.awt.Component comp) nodes.remove(comp);
| public void | setParent(java.awt.Component child, java.awt.Component parent)Sets the tree parent of a child. The components must
have been previously added. If either component has not
previously been added, this injunction is silently ignored.
Cycles are not checked.
TreeNode p = (TreeNode) nodes.get(parent);
TreeNode c = (TreeNode) nodes.get(child);
if ((p != null) && (c != null)) p.addChild(c);
| public void | setRoot(java.awt.Component c)You must make this call, otherwise none of the
components will be layed out. root = (TreeNode) nodes.get(c);
|
|