I work a lot with Swing so I created a solution very much like Swing's
layout managers for use with Piccolo.
I did the following
1) Created a PLayoutManager interface, mine contained the following
methods
public void removeLayoutComponent(PNode child);
public void addLayoutComponent(PNode child, PLayoutConstraints const
raints);
public void layoutContainer(PNode target);
2) Extended PNode (I called mine a LayoutNode) so can set a layout
manager on it
setLayout(PLayoutManager layout)
then, and so you can add a child with constraints, I added these 2
addChild methods.
public void addChild(final PNode child, PLayoutConstraints
constraints)
public void addChild(final int index, final PNode child,
PLayoutConstraints constraints)
The above 2 methods just call the equivalent super's addChild methods
to add the child to the node and then call addLayoutComponent on the
layout manager to indicate this child will be positioned by the layout
manager.
Similarly override
public PNode removeChild(final int index)
to call the layout manager's removeLayoutComponent method after the
super's emoveChild method.
then override layoutChildren, mine looks like this
protected void layoutChildren() {
if (layout !=null) {
layout.layoutContainer(this);
}
}
3) Write a class that implements your desired layout and pass an
instance of it to the node whose children you want to layout with the
setLayout() method. The node with the layout manager will now
automatically position its children.
I did think while I was implementing my layout manager that maybe
the layout manager interface I added ; and
PNode's setLayout method I added ; and
PNode's extra addChild methods ; and
PNode's layoutChildren I overrode (to use the layout manager if
set)
could / should all be part of piccolo, then all you would have to do
to use a layout would be to implement one and call setLayout on the
PNode.
Nigel