You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2008/03/03 22:02:49 UTC
[Tapestry Wiki] Update of "Tapestry5HowToAddBindingPrefixCycle" by DavorHrg
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:
http://wiki.apache.org/tapestry/Tapestry5HowToAddBindingPrefixCycle
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">
...
</t:grid>
}}}
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:
{{{#!java
public static void contributeBindingSource(
MappedConfiguration<String, BindingFactory> configuration,
BindingSource bindingSource
)
{
configuration.add("cycle",new CycleBindingFactory(bindingSource));
}
}}}
And you'll need the implementation:
{{{#!java
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 component,
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);
delegates.add(binding);
}
CycleBinding cycleBinding = new CycleBinding(delegates);
container.addPageLifecycleListener(cycleBinding);
return cycleBinding;
}
}
}}}
and this also:
{{{#!java
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;
}
@Override
public boolean isInvariant() {
return false;
}
@Override
public Class<Object> getBindingType() {
return Object.class;
}
public void containingPageDidDetach() {
index=0;
}
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