Posted on 2009-03-15 22:20
董锐 阅读(849)
评论(0) 编辑 收藏
FROM :
http://wiki.apache.org/tapestry/Tapestry5HowtoSelectWithObjects
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.tapestry.OptionGroupModel;
import org.apache.tapestry.OptionModel;
import org.apache.tapestry.ValueEncoder;
import org.apache.tapestry.internal.OptionModelImpl;
import org.apache.tapestry5.internal.OptionGroupModelImpl;
import org.apache.tapestry.ioc.services.PropertyAccess;
import org.apache.tapestry.ioc.services.PropertyAdapter;
import org.apache.tapestry.util.AbstractSelectModel;
/** Generic selection model for a list of Objects.
* use:
* <pre>@Inject private PropertyAccess _access;</pre>
* in your page to ge the {@link PropertyAccess} service.<br>
* !Notice: you must set the created instance both as model and encoder parameter for the {@link Select} component.*/
public class GenericSelectModel<T> extends AbstractSelectModel implements ValueEncoder<T> {
private PropertyAdapter labelFieldAdapter;
private PropertyAdapter idFieldAdapter;
private Collection<T> list;
public GenericSelectModel(Collection<T> list, Class<T> clazz, String labelField, String idField, PropertyAccess access) {
this.list = list;
if (idField != null)
this.idFieldAdapter = access.getAdapter(clazz).getPropertyAdapter(idField);
if (labelField != null)
this.labelFieldAdapter = access.getAdapter(clazz).getPropertyAdapter(labelField);
}
public void addOptionGroup(String label, boolean disabled, List<T> options) {
List<OptionModel> optionModels = new ArrayList<OptionModel>();
if (labelFieldAdapter == null) {
for (T obj : options) {
optionModels.add(new OptionModelImpl(nvl(obj), obj));
}
} else {
for (T obj : options) {
optionModels.add(new OptionModelImpl(nvl(labelFieldAdapter.get(obj)), obj));
}
}
if (optionGroups == null) {
optionGroups = new ArrayList<OptionGroupModel>();
}
optionGroups.add(new OptionGroupModelImpl(label, disabled, optionModels, new String[0]));
}
public List<OptionGroupModel> getOptionGroups() {
return null;
}
public List<OptionModel> getOptions() {
List<OptionModel> optionModelList = new ArrayList<OptionModel>();
if (labelFieldAdapter == null) {
for (T obj : list) {
optionModelList.add(new OptionModelImpl(nvl(obj)));
}
} else {
for (T obj : list) {
optionModelList.add(new OptionModelImpl(nvl(labelFieldAdapter.get(obj)), obj));
}
}
return optionModelList;
}
// ValueEncoder functions
public String toClient(T obj) {
if (idFieldAdapter == null) {
return obj + "";
} else {
return idFieldAdapter.get(obj) + "";
}
}
public T toValue(String string) {
if (idFieldAdapter == null) {
for (T obj : list) {
if (nvl(obj).equals(string)) return obj;
}
} else {
for (T obj : list) {
if (nvl(idFieldAdapter.get(obj)).equals(string)) return obj;
}
}
return null;
}
private String nvl(Object o) {
if (o == null)
return "";
else
return o.toString();
}
}