NOTE: This is outdated information that applies only to Tapestry 4. |
A simple example for using a property selection
The first step is to design an HTML template. In this template you must include a Tapestry tag like this:
.... <span jwcid="@PropertySelection" value="ognl:propertyName" model="ognl:collectionName"></span> ....
Next, you must define a class. This class must implement the IPropertySelectionModel Interface:
import java.io.Serializable;
import org.apache.tapestry.form.IPropertySelectionModel;
public class SelectionObject implements IPropertySelectionModel, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Object[] core;
private String methodName;
private String getMethodName() {
return methodName;
}
private void setMethodName(String methodName) {
this.methodName = methodName;
}
private Object[] getCore() {
return core;
}
private void setCore(Object[] core) {
this.core = core;
}
public SelectionObject(Object[] data,String method) {
this.setCore(data);
this.setMethodName(method);
}
public SelectionObject(Object[] data) {
this.setCore(data);
this.setMethodName("toString");
}
public int getOptionCount() {
return this.getCore().length;
}
public Object getOption(int arg0) {
return this.getCore()[arg0];
}
public String getLabel(int arg0) {
try {
return this.getCore()[arg0].getClass().getMethod(this.getMethodName(),new Class[]{}).invoke(this.getCore()[arg0],new Object[]{}).toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getValue(int arg0) {
return Integer.toString(arg0);
}
public Object translateValue(String arg0) {
int index;
index = Integer.parseInt(arg0);
return this.getCore()[index];
}
}This class can contain any object's cain. Please see the constructor with 2 parameters. The first parameter is an array of objects. These objects will be assigned to the property. The second parameter is a String with the method name for invoking and obtaining the representation for the user in the combo box.
And then in your Page Implementation you must include code like this:
public abstract class YourPage implements PageBeginRenderListener{
...
private IPropertySelectionModel collectionName;
public void pageBeginRender(PageEvent event){
.....
this.setCollectionName(new SelectionObject(youObjectArray[],"youMethod"));
....
}
.....
public IPropertySelectionModel getCollectionName() {
return collectionName;
}
public void setCollectionName(IPropertySelectionModel collection) {
this.collectionName = collection;
}
@Perstist
public abstract youObject getPropertyName();
public abstract void setPropertyName(youObject);
......
}When submitted, the property PropertyName will be set with the object selected in the combo box for the user.