tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Tapestry Wiki] Update of "Tapestry5HowToAddBindingPrefixCycle" by DavorHrg
Date Mon, 03 Mar 2008 21:02:49 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The following page has been changed by DavorHrg:

New page:
this is another example like : Tapestry5HowToAddBindingPrefix

This binding prefix will return different value for each call,
and we will use it for zebra effect on grid or any other loop.

First let's see usage example:
<t:grid ....  rowClass="cycle:line1,line2">
that is it, no need for counter, and counter % 2 and stuff..

To make it wor you have to tell tapestry about the "cycle:" binding prefix. So, add following
to your module:
    public static void contributeBindingSource(
            MappedConfiguration<String, BindingFactory> configuration,
            BindingSource bindingSource
        configuration.add("cycle",new CycleBindingFactory(bindingSource));

And you'll need the implementation:
import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.Binding;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.TapestryConstants;
import org.apache.tapestry.ioc.Location;
import org.apache.tapestry.services.BindingFactory;
import org.apache.tapestry.services.BindingSource;

 * Implementation of the cycle: binding prefix -- we parse list of bindings
 * and generate delegate bindings for each element<br>
 * default binding is literal, other bindings can be used by specifying prefix.<br>
 * example: "cycle:prop:name,prop:lastName,sth,sth else"
public class CycleBindingFactory implements BindingFactory {
    private final BindingSource _bindingSource;

    public CycleBindingFactory(BindingSource source){
        this._bindingSource = source;
    public Binding newBinding(String description, ComponentResources container, ComponentResources
            String expression, Location location)
        List<Binding> delegates = new ArrayList<Binding>();
        String[] bindingNames = expression.split(",");

        for (String bindingName : bindingNames){
            String defaultBinding = TapestryConstants.LITERAL_BINDING_PREFIX;

            Binding binding = _bindingSource.newBinding(description, container, component,
defaultBinding, bindingName, location);
        CycleBinding cycleBinding = new CycleBinding(delegates);
        return cycleBinding;

and this also:

import java.util.List;

import org.apache.tapestry.Binding;
import org.apache.tapestry.internal.bindings.AbstractBinding;
import org.apache.tapestry.runtime.PageLifecycleListener;

public class CycleBinding extends AbstractBinding implements PageLifecycleListener{
    private final List<Binding> delegates;
    private int index = 0;

    public CycleBinding(List<Binding> delegates) {
        this.delegates = delegates;

    public Object get() {
        Object ret = delegates.get(index).get();
        index ++; 
        if(index>=delegates.size()) index = 0;
        return ret;
    public boolean isInvariant() {
        return false;
    public Class<Object> getBindingType() {
        return Object.class;

    public void containingPageDidDetach() {

    public void containingPageDidAttach() {/*not interested*/}
    public void containingPageDidLoad() {/*not interested*/}


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

View raw message