Nice implementation! :) But shouldn't this be better writen a mixin?

On Wed, 29 Jan 2014 19:57:19 -0200, Bob Harner <bobhar...@gmail.com> wrote:

Maybe this handy little component will help:

/**
* A Tapestry component that rearranges the HTML items in its body into multiple * columns. It accomplishes this by DOM manipulation after the body is rendered
 * to the DOM.
 * <p>
* Note: For this to work properly, the items that you wrap this component * around must each consist of a single HTML element (possibly with nested * elements). For example, the following works, because each item is contained
 * within a single &lt;li&gt; element:
 *
 * <pre>
 * &lt;t:columns number="2"&gt;
 *     &lt;t:loop source="myList" value="oneItem"&gt;
 *         &lt;li&gt;&lt;b&gt;Name:&lt;/b&gt;{$oneItem.name}&lt;/li&gt;
 *     &lt;/t:loop&gt;
 * &lt;/t:columns&gt;
 * </pre>
 *
* But the following fails to lay out property, because there are two HTML
 * elements -- a span and a br -- per item:
 *
 * <pre>
 * &lt;t:columns number="2"&gt;
 *     &lt;t:loop source="myList" value="oneItem"&gt;
 *         &lt;span&gt;${oneItem.name}&lt;/span&gt;&lt;br /&gt;
 *     &lt;/t:loop&gt;
 * &lt;/t:columns&gt;
 * </pre>
 */
@SupportsInformalParameters
public class Columns {
    /**
     * The number of columns to divide the items into
     */
    @Property
    @Parameter(value="5", defaultPrefix="literal")
    private int number;

    @Property
    @Parameter(value="literal:t-column")
    private String columnClass;

    @Inject
    private ComponentResources componentResources;

    /**
* Manipulate the Tapestry-rendered DOM to insert as many DIV elements as * needed to break up the list of items in our "body" into the requested
     * number of columns. This method is only called after the "body" is
     * rendered into the DOM.
     *
     * @param writer
     */
    @AfterRenderBody
    void manipulateTheDom(MarkupWriter writer)
    {
        Element container = writer.getElement();

        // figure out how many items should go into each column

        List<Node> children = container.getChildren();
        int numChildren = children.size();
        int itemsPerColumn = numChildren / number;
        int remainder = numChildren % number;
        int itemNum = 0;

        // render informal parameters into the container element
        componentResources.renderInformalParameters(writer);

        for (int columnNumber = 0; columnNumber < number; columnNumber++)
        {
            int extras = (remainder > 0) ? 1 : 0;
            remainder--;
            int numItemsthisColumn = itemsPerColumn + extras;

// Make a per-column wrapper element and move it to the bottom
            Element columnElement = container.element("div", "class",
columnClass);
            columnElement.moveToBottom(container);

            // move some children into the new column wrapper element
            for (int i = 0; i < numItemsthisColumn; i++)
            {
                children.get(itemNum).moveToBottom(columnElement);
                itemNum++;
            }
        }
    }
}

On Wed, Jan 29, 2014 at 3:28 PM, Thiago H de Paula Figueiredo
<thiag...@gmail.com> wrote:
On Wed, 29 Jan 2014 17:58:08 -0200, Dimitris Zenios
<dimitris.zen...@gmail.com> wrote:

I am not sure whether i will be able to cast to collection.Maybe tapestry
is doing some magic with BindParameter.


You don't need to cast it to a Collection, because Iterable is exactly the Java interface implemented by any class which provides an Iterator. Just use
Iterable and its iterator() method directly.


--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tapestry.apache.org
For additional commands, e-mail: dev-h...@tapestry.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tapestry.apache.org
For additional commands, e-mail: dev-h...@tapestry.apache.org



--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tapestry.apache.org
For additional commands, e-mail: dev-h...@tapestry.apache.org

Reply via email to