> <span jwcid="@Insert" value="</tr><tr>" raw="true"/>

I don't think it's ever a good idea to output partial HTML in your
page like this.

I'll throw one more implementation into the ring.

<!-- Usage Example -->
<t:gallery entriesPerRow="4" entry="var:galleryEntry" source="1..21">
    ${var:galleryEntry}
</t:gallery>

<!-- Gallery.tml -->
<div xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd";>
    <table>
        <t:loop source="rows" value="columns">
            <tr>
                <t:loop source="columns" value="entry">
                    <td>
                        <t:body/>
                    </td>
                    <t:if test="needsPadding">
                        <td colspan="${padding}">&#160;</td>
                    </t:if>
                </t:loop>
            </tr>
        </t:loop>
    </table>
</div>


/**
 * Gallery.java
 */
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Gallery {

    @Parameter(required = true)
    private Iterable source;

    @Parameter(required = true)
    @Property
    private Object entry;

    @Parameter(required = true)
    private int entriesPerRow;

    @Parameter
    private int maxRows;

    @Property
    private WrappedIterable rows;

    @Property
    private LimitedIterable columns;

    void setupRender() {
        rows = new WrappedIterable(source);
    }

    public boolean getNeedsPadding() {
        return !rows.iterator.hasNext() && columns.count < entriesPerRow;
    }

    public int getPadding() {
        return entriesPerRow - columns.count;
    }

    public class WrappedIterable implements Iterable<LimitedIterable> {
        private Iterator iterator;
        int rows = 0;

        private WrappedIterable(Iterable iterable) {
            iterator = iterable.iterator();
        }

        public Iterator<LimitedIterable> iterator() {

            return new Iterator<LimitedIterable>() {
                public boolean hasNext() {
                    return (maxRows <= 0 || rows < maxRows) &&
iterator.hasNext();
                }

                public LimitedIterable next() {
                    if (hasNext()) {
                        ++rows;
                        return new LimitedIterable(iterator, entriesPerRow);
                    }

                    throw new NoSuchElementException();
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }

    public class LimitedIterable implements Iterable {
        int count = 0;
        private final Iterator iterator;
        private int limit;

        private LimitedIterable(Iterator iterator, int limit) {
            this.iterator = iterator;
            this.limit = limit;
        }

        public Iterator iterator() {
            return new Iterator<Object>() {
                public boolean hasNext() {
                    return count < limit && iterator.hasNext();
                }

                public Object next() {
                    if (hasNext()) {
                        ++count;
                        return iterator.next();
                    }
                    throw new NoSuchElementException();
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }
}

Josh

On Sat, Sep 17, 2011 at 6:40 PM, Ken in Nashua <kcola...@live.com> wrote:
>
> Rob,
>
> Thanks for the constructive criticism.
>
> I will give this a whirl... like what I see...
>
> I am all for good form... and one of the nice things I am excited about T5 is 
> that it promotes a ton of loose options for achieving good form.
>
> Appreciate the help and will let you know how I make out.
>
> - cheers
>
> KEN
>

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

Reply via email to