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 MarceloLotif: http://wiki.apache.org/tapestry/Tapestry5SelectObject The comment on the change is: The BeanUtils dependency was removed (thanks to DavorHrg) ------------------------------------------------------------------------------ * ["Tapestry5DisplayableSelectionModel"] * ["Tapestry5HowtoSelectWithObjects"] - First, you have to add a dependency to your pom.xml. This will add the Apache BeanUtils to your project: - {{{ - <dependency> - <groupId>commons-beanutils</groupId> - <artifactId>commons-beanutils-core</artifactId> - <version>1.7.0</version> - </dependency> - }}} - == GenericSelectionModel.java == This is the model that the component will use to render your List. Add it to any package visible to the components package. - {{{ - import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; - import org.apache.commons.beanutils.BeanUtils; import org.apache.tapestry.OptionGroupModel; import org.apache.tapestry.OptionModel; import org.apache.tapestry.internal.OptionModelImpl; + import org.apache.tapestry.ioc.services.PropertyAccess; import org.apache.tapestry.util.AbstractSelectModel; /** * @author jued - * + * * @param <T> */ public class GenericSelectionModel<T> extends AbstractSelectModel { - private String labelField; + private String labelField; - private List<T> list; + private List<T> list; + private final PropertyAccess adapter; + - public GenericSelectionModel(List<T> list, String labelField) { + public GenericSelectionModel(List<T> list, String labelField, PropertyAccess adapter) { - this.labelField = labelField; + this.labelField = labelField; - this.list = list; - } + this.list = list; + this.adapter = adapter; + } - public List<OptionGroupModel> getOptionGroups() { + public List<OptionGroupModel> getOptionGroups() { - return null; - } + return null; + } - public List<OptionModel> getOptions() { + public List<OptionModel> getOptions() { - List<OptionModel> optionModelList = new ArrayList<OptionModel>(); + List<OptionModel> optionModelList = new ArrayList<OptionModel>(); + for (T obj : list) { + if (labelField == null) { - try { - for (T obj : list) { - if (labelField == null) { - optionModelList.add(new OptionModelImpl(obj + "", false, obj, new String[0])); + optionModelList.add(new OptionModelImpl(obj + "", false, obj, new String[0])); + } else { + optionModelList.add(new OptionModelImpl(adapter.get(obj, labelField) + +"", false, obj, new String[0])); + } + } + return optionModelList; + } - } else { - optionModelList.add(new OptionModelImpl( - BeanUtils.getProperty(obj, labelField), false, obj, new String[0])); - } - } - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return optionModelList; - } } }}} - == GenericValueEncoder.java == This is the encoder that the component will use to get your selection. Add it to the same package of the GenericSelectionModel. {{{ - import java.lang.reflect.InvocationTargetException; import java.util.List; - import org.apache.commons.beanutils.BeanUtils; import org.apache.tapestry.ValueEncoder; + import org.apache.tapestry.ioc.services.PropertyAccess; public class GenericValueEncoder<T> implements ValueEncoder<T> { - private String labelField; - - private List<T> list; + private List<T> list; + private final PropertyAccess access; + private final String fieldName; - public GenericValueEncoder(List<T> list, String labelField) { - this.list = list; - this.labelField = labelField; - } + public GenericValueEncoder(List<T> list, String fieldName, PropertyAccess propertyAccess) { + this.list = list; + this.fieldName = fieldName; + this.access = propertyAccess; + } - public String toClient(T obj) { + public String toClient(T obj) { + if (fieldName == null) { + return obj + ""; + } else { + return access.get(obj,fieldName)+""; + } + } - try { - if (labelField == null) { - return obj + ""; - } else { - return BeanUtils.getProperty(obj, labelField); - } - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } - public T toValue(String string) { + public T toValue(String string) { + for (T obj : list) { + if (fieldName == null) { + if ((obj + "").equals(string)) { + return obj; + } + } else { + if (access.get(obj, fieldName).equals(string)) { + return obj; + } + } + } + return null; + } - try { - for (T obj : list) { - if (labelField == null) { - if ((obj + "").equals(string)) { - return obj; - } - } else { - if (BeanUtils.getProperty(obj, labelField).equals(string)) { - return obj; - } - } - } - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } } }}} == SelectObject.java == This is the Select (org.apache.tapestry.corelib.components) with some simple modifications to use those models above and ommit part of the custom parameters. {{{ + import java.util.List; + import java.util.Locale; import org.apache.tapestry.Binding; import org.apache.tapestry.ComponentResources; @@ -166, +125 @@ import org.apache.tapestry.annotations.Parameter; import org.apache.tapestry.corelib.base.AbstractField; import org.apache.tapestry.internal.util.SelectModelRenderer; + import org.apache.tapestry.ioc.services.PropertyAccess; import org.apache.tapestry.services.FieldValidatorDefaultSource; import org.apache.tapestry.services.FormSupport; import org.apache.tapestry.services.Request; import org.apache.tapestry.services.ValueEncoderFactory; import org.apache.tapestry.services.ValueEncoderSource; import org.apache.tapestry.util.EnumSelectModel; - import org.exemplo.selectComponent.models.GenericSelectionModel; + import org.exemplo.testeModels.selectModel.GenericSelectionModel; - import org.exemplo.selectComponent.models.GenericValueEncoder; + import org.exemplo.testeModels.selectModel.GenericValueEncoder; /** - * Select an item from a list of values, using an [X]HTML <select> element on the client side. + * Select an item from a list of values, using an [X]HTML <select> + element on the client side. - * An validation decorations will go around the entire <select> element. + * An validation decorations will go around the entire <select> + element. * <p> - * A core part of this component is the [EMAIL PROTECTED] ValueEncoder} (the encoder parameter) that is used to + * A core part of this component is the [EMAIL PROTECTED] ValueEncoder} (the encoder + parameter) that is used to - * convert between server-side values and client-side strings. In many cases, a [EMAIL PROTECTED] ValueEncoder} + * convert between server-side values and client-side strings. In many + cases, a [EMAIL PROTECTED] ValueEncoder} * can be generated automatically from the type of the value parameter. The - * [EMAIL PROTECTED] ValueEncoderSource} service provides an encoder in these situations; it can be overriden + * [EMAIL PROTECTED] ValueEncoderSource} service provides an encoder in these + situations; it can be overriden - * by binding the encoder parameter, or extended by contributing a [EMAIL PROTECTED] ValueEncoderFactory} into + * by binding the encoder parameter, or extended by contributing a [EMAIL PROTECTED] + ValueEncoderFactory} into * the service's configuration. */ public final class SelectObject extends AbstractField { - private class Renderer extends SelectModelRenderer + private class Renderer extends SelectModelRenderer - { + { - public Renderer(MarkupWriter writer) + public Renderer(MarkupWriter writer) - { + { - super(writer, _encoder); + super(writer, _encoder); - } + } - @Override + @Override - protected boolean isOptionSelected(OptionModel optionModel) + protected boolean isOptionSelected(OptionModel optionModel) - { + { - Object value = optionModel.getValue(); + Object value = optionModel.getValue(); - return value == _value || (value != null && value.equals(_value)); + return value == _value || (value != null && value.equals(_value)); - } + } - } + } - + - @Inject + @Inject - private FieldValidatorDefaultSource _fieldValidatorDefaultSource; + private FieldValidatorDefaultSource _fieldValidatorDefaultSource; - @Inject + @Inject - private Locale _locale; + private Locale _locale; + @Inject + private PropertyAccess propertyAccess; + - @Parameter(required = true) + @Parameter(required = true) - private List<Object> _list; + private List<Object> _list; - + - @Parameter + @Parameter - private String _labelField = null; + private String _labelField = null; - + - // Maybe this should default to property "<componentId>Model"? + // Maybe this should default to property "<componentId>Model"? - /** + /** - * The model used to identify the option groups and options to be presented to the user. This + * The model used to identify the option groups and options to be + presented to the user. This - * can be generated automatically for Enum types. + * can be generated automatically for Enum types. - */ + */ // @Parameter(required = true) // private SelectModel _model; - + - private GenericSelectionModel<Object> _model; + private GenericSelectionModel<Object> _model; - /** + /** - * Allows a specific implementation of [EMAIL PROTECTED] ValueEncoder} to be supplied. This is used to + * Allows a specific implementation of [EMAIL PROTECTED] ValueEncoder} to be + supplied. This is used to - * create client-side string values for the different options. + * create client-side string values for the different options. - * + * - * @see ValueEncoderSource + * @see ValueEncoderSource - */ + */ // @Parameter // private ValueEncoder _encoder; - private GenericValueEncoder<Object> _encoder; + private GenericValueEncoder<Object> _encoder; - + - @Inject + @Inject - private Request _request; + private Request _request; - @Inject + @Inject - private ComponentResources _resources; + private ComponentResources _resources; - @Environmental + @Environmental - private ValidationTracker _tracker; + private ValidationTracker _tracker; - /** Performs input validation on the value supplied by the user in the form submission. */ + /** Performs input validation on the value supplied by the user in the + form submission. */ - @Parameter(defaultPrefix = "validate") + @Parameter(defaultPrefix = "validate") - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") - private FieldValidator<Object> _validate = NOOP_VALIDATOR; + private FieldValidator<Object> _validate = NOOP_VALIDATOR; - /** The value to read or update. */ + /** The value to read or update. */ - @Parameter(required = true, principal = true) + @Parameter(required = true, principal = true) - private Object _value; + private Object _value; - @Inject + @Inject - private ValueEncoderSource _valueEncoderSource; + private ValueEncoderSource _valueEncoderSource; - @Override + @Override - protected void processSubmission(FormSupport formSupport, String elementName) + protected void processSubmission(FormSupport formSupport, String elementName) - { + { - _encoder = new GenericValueEncoder<Object>(_list, _labelField); + _encoder = new GenericValueEncoder<Object>(_list, _labelField, propertyAccess); - + - String primaryKey = _request.getParameter(elementName); + String primaryKey = _request.getParameter(elementName); - + - Object selectedValue = _encoder.toValue(primaryKey); + Object selectedValue = _encoder.toValue(primaryKey); - try + try - { + { - _validate.validate(selectedValue); + _validate.validate(selectedValue); - _value = selectedValue; + _value = selectedValue; - } + } - catch (ValidationException ex) + catch (ValidationException ex) - { + { - _tracker.recordError(this, ex.getMessage()); + _tracker.recordError(this, ex.getMessage()); - return; + return; - } + } - } + } - void afterRender(MarkupWriter writer) + void afterRender(MarkupWriter writer) - { + { - writer.end(); + writer.end(); - } + } - void beginRender(MarkupWriter writer) + void beginRender(MarkupWriter writer) - { + { - writer.element("select", "name", getElementName(), "id", getClientId()); + writer.element("select", "name", getElementName(), "id", getClientId()); - _encoder = new GenericValueEncoder<Object>(_list, _labelField); + _encoder = new GenericValueEncoder<Object>(_list, _labelField, propertyAccess); - _model = new GenericSelectionModel<Object>(_list, _labelField); + _model = new GenericSelectionModel<Object>(_list, _labelField, propertyAccess); - // Disabled, informals via mixins + // Disabled, informals via mixins - } + } - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") - ValueEncoder defaultEncoder() + ValueEncoder defaultEncoder() - { + { - return _valueEncoderSource.createEncoder("value", _resources); + return _valueEncoderSource.createEncoder("value", _resources); - } + } - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") - SelectModel defaultModel() + SelectModel defaultModel() - { + { - Class valueType = _resources.getBoundType("value"); + Class valueType = _resources.getBoundType("value"); - if (valueType == null) return null; + if (valueType == null) return null; - if (Enum.class.isAssignableFrom(valueType)) + if (Enum.class.isAssignableFrom(valueType)) - return new EnumSelectModel(valueType, _resources.getContainerMessages()); + return new EnumSelectModel(valueType, _resources.getContainerMessages()); - return null; + return null; - } + } - /** + /** - * Computes a default value for the "validate" parameter using + * Computes a default value for the "validate" parameter using - * [EMAIL PROTECTED] FieldValidatorDefaultSource}. + * [EMAIL PROTECTED] FieldValidatorDefaultSource}. - */ + */ - FieldValidator defaultValidate() + FieldValidator defaultValidate() - { + { - Class type = _resources.getBoundType("value"); + Class type = _resources.getBoundType("value"); - if (type == null) return null; + if (type == null) return null; - return _fieldValidatorDefaultSource.createDefaultValidator( + return _fieldValidatorDefaultSource.createDefaultValidator( - this, + this, - _resources.getId(), + _resources.getId(), - _resources.getContainerMessages(), + _resources.getContainerMessages(), - _locale, + _locale, - type, + type, - _resources.getAnnotationProvider("value")); + _resources.getAnnotationProvider("value")); - } + } - Binding defaultValue() + Binding defaultValue() - { + { - return createDefaultParameterBinding("value"); + return createDefaultParameterBinding("value"); - } + } - @BeforeRenderTemplate + @BeforeRenderTemplate - void options(MarkupWriter writer) + void options(MarkupWriter writer) - { + { - SelectModelVisitor renderer = new Renderer(writer); + SelectModelVisitor renderer = new Renderer(writer); - _model.visit(renderer); + _model.visit(renderer); - } + } - // For testing. + // For testing. - void setModel(GenericSelectionModel model) + void setModel(GenericSelectionModel model) - { + { - _model = model; + _model = model; - } + } - void setValue(Object value) + void setValue(Object value) - { + { - _value = value; + _value = value; - } + } - void setValueEncoder(GenericValueEncoder encoder) + void setValueEncoder(GenericValueEncoder encoder) - { + { - _encoder = encoder; + _encoder = encoder; - } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
