Revision: 912 Author: allain.lalonde Date: Wed Dec 2 11:50:15 2009 Log: Modifying SwingLayoutNode so that it accepts a Container during construction. Otherwise, creating a node that uses BoxLayout is impossible without making the node's layout mutable. http://code.google.com/p/piccolo2d/source/detail?r=912
Modified: /piccolo2d.java/trunk/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java /piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java ======================================= --- /piccolo2d.java/trunk/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java Tue Dec 1 14:06:59 2009 +++ /piccolo2d.java/trunk/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java Wed Dec 2 11:50:15 2009 @@ -121,8 +121,9 @@ gridBagLayoutNode.setOffset(400, 250); rootNode.addChild(gridBagLayoutNode); - final SwingLayoutNode boxLayoutNode = new SwingLayoutNode(); - boxLayoutNode.setLayout(new BoxLayout(boxLayoutNode.getContainer(), BoxLayout.Y_AXIS)); + JPanel container = new JPanel(); + container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); + final SwingLayoutNode boxLayoutNode = new SwingLayoutNode(container); boxLayoutNode.addChild(new MyPPath(new Rectangle2D.Double(0, 0, 50, 50), Color.yellow, new BasicStroke(2), Color.red)); boxLayoutNode.addChild(new MyPPath(new Rectangle2D.Double(0, 0, 100, 50), Color.orange, new BasicStroke(2), ======================================= --- /piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java Wed Dec 2 11:29:54 2009 +++ /piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java Wed Dec 2 11:50:15 2009 @@ -38,6 +38,7 @@ import java.util.Collection; import java.util.Iterator; +import javax.swing.BoxLayout; import javax.swing.JComponent; import javax.swing.JPanel; @@ -63,7 +64,7 @@ private static final Anchor DEFAULT_ANCHOR = Anchor.WEST; /** Container for ProxyComponents. */ - private final JPanel container; + private final Container container; private final PropertyChangeListener propertyChangeListener; @@ -85,7 +86,21 @@ * not be null. */ public SwingLayoutNode(final LayoutManager layoutManager) { - container = new JPanel(layoutManager); + this(new JPanel(layoutManager)); + } + + /** + * Constructs a SwingLayoutNode that lays out its children as though they + * were children of the provided Container. + * + * Whatever LayoutManager is being used by the container will be used when + * laying out nodes. + * + * @param container Container in which child nodes will effectively be laid + * out + */ + public SwingLayoutNode(Container container) { + this.container = container; propertyChangeListener = new PropertyChangeListener() { public void propertyChange(final PropertyChangeEvent event) { final String propertyName = event.getPropertyName(); @@ -131,7 +146,7 @@ */ public Container getContainer() { return container; - } + } /** * Adds a child at the specified index. Like Swing, bad things can happen if -- Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en