binding rule impl
This commit is contained in:
@@ -42,7 +42,7 @@ public interface Binding {
|
||||
|
||||
/**
|
||||
* If this Binding is enabled.
|
||||
* Used to determine if the user can interact with the field.
|
||||
* Used to determine if the user can interact with the field at all.
|
||||
* A Binding that is not enabled cannot have source values applied and cannot be committed.
|
||||
*/
|
||||
boolean isEnabled();
|
||||
@@ -59,7 +59,7 @@ public interface Binding {
|
||||
* Sets to {@link BindingStatus#DIRTY} if succeeds.
|
||||
* Sets to {@link BindingStatus#INVALID_SOURCE_VALUE} if fails.
|
||||
* @param sourceValue
|
||||
* @throws IllegalStateException if {@link #isEditable()}
|
||||
* @throws IllegalStateException if not editable or not enabled
|
||||
*/
|
||||
void applySourceValue(Object sourceValue);
|
||||
|
||||
@@ -87,7 +87,7 @@ public interface Binding {
|
||||
* Commit the buffered value to the model.
|
||||
* Sets to {@link BindingStatus#COMMITTED} if succeeds.
|
||||
* Sets to {@link BindingStatus#COMMIT_FAILURE} if fails.
|
||||
* @throws IllegalStateException if not {@link BindingStatus#DIRTY} or {@link #isEditable()}
|
||||
* @throws IllegalStateException if not editable, not enabled, or not dirty
|
||||
*/
|
||||
void commit();
|
||||
|
||||
@@ -104,10 +104,11 @@ public interface Binding {
|
||||
|
||||
/**
|
||||
* Get a Binding to a nested property value.
|
||||
* @param nestedProperty the nested property name, such as "foo"; should not be a property path like "foo.bar"
|
||||
* @param property the nested property name, such as "foo"; should not be a property path like "foo.bar"
|
||||
* @return the binding to the nested property
|
||||
* @throws IllegalStateException if not a bean
|
||||
*/
|
||||
Binding getBinding(String nestedProperty);
|
||||
Binding getBinding(String property);
|
||||
|
||||
/**
|
||||
* If bound to an indexable Collection, either a {@link java.util.List} or an array.
|
||||
@@ -118,6 +119,7 @@ public interface Binding {
|
||||
* If a List, get a Binding to a element in the List.
|
||||
* @param index the element index
|
||||
* @return the indexed binding
|
||||
* @throws IllegalStateException if not a list
|
||||
*/
|
||||
Binding getListElementBinding(int index);
|
||||
|
||||
@@ -130,6 +132,7 @@ public interface Binding {
|
||||
* If a Map, get a Binding to a value in the Map.
|
||||
* @param key the map key
|
||||
* @return the keyed binding
|
||||
* @throws IllegalStateException if not a map
|
||||
*/
|
||||
Binding getMapValueBinding(Object key);
|
||||
|
||||
@@ -160,7 +163,6 @@ public interface Binding {
|
||||
|
||||
/**
|
||||
* Set the model value.
|
||||
* @throws IllegalStateException if this binding is read only
|
||||
*/
|
||||
void setValue(Object value);
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package org.springframework.ui.binding.config;
|
||||
|
||||
import org.springframework.ui.format.Formatter;
|
||||
|
||||
public interface BindingRule {
|
||||
|
||||
String getPropertyPath();
|
||||
|
||||
Formatter<?> getFormatter();
|
||||
|
||||
boolean isRequired();
|
||||
|
||||
boolean isCollectionBinding();
|
||||
|
||||
Formatter<?> getKeyFormatter();
|
||||
|
||||
Formatter<?> getValueFormatter();
|
||||
|
||||
}
|
||||
@@ -25,27 +25,31 @@ public interface BindingRuleConfiguration {
|
||||
|
||||
/**
|
||||
* Set the Formatter to use to format bound property values.
|
||||
* If a collection property, this formatter is used to format the Collection as a String.
|
||||
* Default is null.
|
||||
*/
|
||||
BindingRuleConfiguration formatWith(Formatter<?> formatter);
|
||||
|
||||
/**
|
||||
* If a indexable map property, set the Formatter to use to format map key indexes.
|
||||
* Default is null.
|
||||
* If a map property, set the Formatter to use to format map keys.
|
||||
*/
|
||||
BindingRuleConfiguration formatKeysWith(Formatter<?> formatter);
|
||||
|
||||
/**
|
||||
* If an indexable list or map property, set the Formatter to use to format indexed elements.
|
||||
* Default is null.
|
||||
* If an list or map property, set the Formatter to use to format indexed elements.
|
||||
*/
|
||||
BindingRuleConfiguration formatElementsWith(Formatter<?> formatter);
|
||||
|
||||
/**
|
||||
* Mark the binding as read only.
|
||||
* A read-only binding cannot have source values applied and cannot be committed.
|
||||
*/
|
||||
BindingRuleConfiguration readOnly();
|
||||
|
||||
/**
|
||||
* Set when the binding is editable.
|
||||
*/
|
||||
BindingRuleConfiguration editableWhen(Condition condition);
|
||||
|
||||
/**
|
||||
* Set when the binding is enabled.
|
||||
*/
|
||||
BindingRuleConfiguration enabledWhen(Condition condition);
|
||||
|
||||
/**
|
||||
* Set when the binding is visible.
|
||||
*/
|
||||
BindingRuleConfiguration visibleWhen(Condition condition);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package org.springframework.ui.binding.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BindingRules extends List<BindingRule> {
|
||||
|
||||
Class<?> getModelType();
|
||||
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
package org.springframework.ui.binding.config;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.GenericCollectionTypeResolver;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Builder for constructing the rules for binding to a model.
|
||||
* @author Keith Donald
|
||||
* @param <M> the model type
|
||||
*/
|
||||
public class BindingRulesBuilder {
|
||||
|
||||
private BindingRules bindingRules;
|
||||
|
||||
/**
|
||||
* Creates a new BindingRuleBuilder.
|
||||
* @param modelType the type of model to build binding rules against
|
||||
*/
|
||||
public BindingRulesBuilder(Class<?> modelType) {
|
||||
Assert.notNull(modelType, "The model type is required");
|
||||
bindingRules = new ArrayListBindingRules(modelType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a rule for binding to the model property.
|
||||
* @param propertyPath the model property path
|
||||
* @return allows additional binding configuration options to be specified fluently
|
||||
* @throws IllegalArgumentException if the property path is invalid given the modelType
|
||||
*/
|
||||
public BindingRuleConfiguration bind(String propertyPath) {
|
||||
boolean collectionBinding = validate(propertyPath);
|
||||
ConfigurableBindingRule rule = new ConfigurableBindingRule(propertyPath);
|
||||
if (collectionBinding) {
|
||||
rule.markCollectionBinding();
|
||||
}
|
||||
bindingRules.add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* The built list of binding rules.
|
||||
* Call after recording {@link #bind(String)} instructions.
|
||||
*/
|
||||
public BindingRules getBindingRules() {
|
||||
return bindingRules;
|
||||
}
|
||||
|
||||
private boolean validate(String propertyPath) {
|
||||
boolean collectionBinding = false;
|
||||
String[] props = propertyPath.split("\\.");
|
||||
if (props.length == 0) {
|
||||
props = new String[] { propertyPath };
|
||||
}
|
||||
Class<?> modelType = bindingRules.getModelType();
|
||||
for (int i = 0; i < props.length; i ++) {
|
||||
String prop = props[i];
|
||||
PropertyDescriptor[] propDescs = getBeanInfo(modelType).getPropertyDescriptors();
|
||||
boolean found = false;
|
||||
for (PropertyDescriptor propDesc : propDescs) {
|
||||
if (prop.equals(propDesc.getName())) {
|
||||
found = true;
|
||||
Class<?> propertyType = propDesc.getPropertyType();
|
||||
if (Collection.class.isAssignableFrom(propertyType)) {
|
||||
modelType = GenericCollectionTypeResolver.getCollectionReturnType(propDesc.getReadMethod());
|
||||
if (i == (props.length - 1)) {
|
||||
collectionBinding = true;
|
||||
}
|
||||
} else if (Map.class.isAssignableFrom(propertyType)) {
|
||||
modelType = GenericCollectionTypeResolver.getMapValueReturnType(propDesc.getReadMethod());
|
||||
if (i == (props.length - 1)) {
|
||||
collectionBinding = true;
|
||||
}
|
||||
} else {
|
||||
modelType = propertyType;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
if (props.length > 1) {
|
||||
throw new IllegalArgumentException("No property named '" + prop + "' found on model class [" + modelType.getName() + "] as part of property path '" + propertyPath + "'");
|
||||
} else {
|
||||
throw new IllegalArgumentException("No property named '" + prop + "' found on model class [" + modelType.getName() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
return collectionBinding;
|
||||
}
|
||||
|
||||
private BeanInfo getBeanInfo(Class<?> clazz) {
|
||||
try {
|
||||
return Introspector.getBeanInfo(clazz);
|
||||
} catch (IntrospectionException e) {
|
||||
throw new IllegalStateException("Unable to introspect model type " + clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
static class ArrayListBindingRules extends ArrayList<BindingRule> implements BindingRules {
|
||||
|
||||
private Class<?> modelType;
|
||||
|
||||
public ArrayListBindingRules(Class<?> modelType) {
|
||||
this.modelType = modelType;
|
||||
}
|
||||
|
||||
public Class<?> getModelType() {
|
||||
return modelType;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.springframework.ui.binding.config;
|
||||
|
||||
public interface Condition {
|
||||
|
||||
boolean isTrue();
|
||||
|
||||
static final Condition ALWAYS_TRUE = new Condition() {
|
||||
public boolean isTrue() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
static final Condition ALWAYS_FALSE = new Condition() {
|
||||
public boolean isTrue() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package org.springframework.ui.binding.config;
|
||||
|
||||
import org.springframework.ui.format.Formatter;
|
||||
|
||||
public class ConfigurableBindingRule implements BindingRuleConfiguration, BindingRule {
|
||||
|
||||
private String propertyPath;
|
||||
|
||||
private Formatter<?> formatter;
|
||||
|
||||
private boolean required;
|
||||
|
||||
private boolean collectionBinding;
|
||||
|
||||
private Formatter<?> elementFormatter;
|
||||
|
||||
public ConfigurableBindingRule(String propertyPath) {
|
||||
this.propertyPath = propertyPath;
|
||||
}
|
||||
|
||||
// implementing BindingRuleConfiguration
|
||||
|
||||
public BindingRuleConfiguration formatWith(Formatter<?> formatter) {
|
||||
this.formatter = formatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration required() {
|
||||
this.required = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration formatElementsWith(Formatter<?> formatter) {
|
||||
this.elementFormatter = formatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
// implementing BindingRule
|
||||
|
||||
public String getPropertyPath() {
|
||||
return propertyPath;
|
||||
}
|
||||
|
||||
public Formatter<?> getFormatter() {
|
||||
return formatter;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public boolean isCollectionBinding() {
|
||||
return collectionBinding;
|
||||
}
|
||||
|
||||
public void markCollectionBinding() {
|
||||
this.collectionBinding = true;
|
||||
}
|
||||
|
||||
public Formatter<?> getValueFormatter() {
|
||||
return elementFormatter;
|
||||
}
|
||||
|
||||
public Formatter<?> getKeyFormatter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
<p>
|
||||
Binding configuration SPI.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.springframework.ui.binding.support;
|
||||
|
||||
import org.springframework.ui.binding.Binding;
|
||||
import org.springframework.ui.binding.config.Condition;
|
||||
import org.springframework.ui.format.Formatter;
|
||||
|
||||
public interface BindingRule {
|
||||
|
||||
Formatter<?> getFormatter();
|
||||
|
||||
Formatter<?> getKeyFormatter();
|
||||
|
||||
Formatter<?> getElementFormatter();
|
||||
|
||||
Condition getEditableCondition();
|
||||
|
||||
Condition getEnabledCondition();
|
||||
|
||||
Condition getVisibleCondition();
|
||||
|
||||
// TODO - does this belong here?
|
||||
Binding getBinding(String property, Object model);
|
||||
|
||||
}
|
||||
@@ -27,6 +27,8 @@ import org.springframework.ui.binding.BindingResult;
|
||||
import org.springframework.ui.binding.BindingResults;
|
||||
import org.springframework.ui.binding.Binding.BindingStatus;
|
||||
import org.springframework.ui.binding.config.BindingRuleConfiguration;
|
||||
import org.springframework.ui.binding.config.Condition;
|
||||
import org.springframework.ui.format.Formatter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -41,8 +43,8 @@ public class GenericBinder implements Binder {
|
||||
|
||||
private Object model;
|
||||
|
||||
private Map<String, Binding> bindings;
|
||||
|
||||
private Map<String, GenericBindingRule> bindingRules;
|
||||
|
||||
private TypeConverter typeConverter;
|
||||
|
||||
private MessageSource messageSource;
|
||||
@@ -54,7 +56,7 @@ public class GenericBinder implements Binder {
|
||||
public GenericBinder(Object model) {
|
||||
Assert.notNull(model, "The model to bind to is required");
|
||||
this.model = model;
|
||||
bindings = new HashMap<String, Binding>();
|
||||
bindingRules = new HashMap<String, GenericBindingRule>();
|
||||
typeConverter = new DefaultTypeConverter();
|
||||
}
|
||||
|
||||
@@ -80,11 +82,16 @@ public class GenericBinder implements Binder {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param propertyPath binding rule property path in format prop.nestedListProp[].nestedMapProp[].nestedProp
|
||||
* @param propertyPath binding rule property path in format prop.nestedProp
|
||||
* @return
|
||||
*/
|
||||
public BindingRuleConfiguration bindingRule(String propertyPath) {
|
||||
return null;
|
||||
PropertyPath path = new PropertyPath(propertyPath);
|
||||
GenericBindingRule rule = getBindingRule(path.getFirstElement().getValue());
|
||||
for (PropertyPathElement element : path.getNestedElements()) {
|
||||
rule = rule.getBindingRule(element.getValue());
|
||||
}
|
||||
return rule;
|
||||
}
|
||||
|
||||
// implementing Binder
|
||||
@@ -95,11 +102,7 @@ public class GenericBinder implements Binder {
|
||||
|
||||
public Binding getBinding(String property) {
|
||||
PropertyPath path = new PropertyPath(property);
|
||||
Binding binding = bindings.get(path.getFirstElement().getValue());
|
||||
if (binding == null) {
|
||||
binding = new PropertyBinding(path.getFirstElement().getValue(), model, typeConverter);
|
||||
bindings.put(path.getFirstElement().getValue(), binding);
|
||||
}
|
||||
Binding binding = getBindingRule(path.getFirstElement().getValue()).getBinding(model);
|
||||
for (PropertyPathElement element : path.getNestedElements()) {
|
||||
if (element.isIndex()) {
|
||||
if (binding.isMap()) {
|
||||
@@ -142,11 +145,20 @@ public class GenericBinder implements Binder {
|
||||
|
||||
// internal helpers
|
||||
|
||||
private GenericBindingRule getBindingRule(String property) {
|
||||
GenericBindingRule rule = bindingRules.get(property);
|
||||
if (rule == null) {
|
||||
rule = new GenericBindingRule(property);
|
||||
bindingRules.put(property, rule);
|
||||
}
|
||||
return rule;
|
||||
}
|
||||
|
||||
private BindingResult bind(Map.Entry<String, ? extends Object> sourceValue, Binding binding) {
|
||||
String property = sourceValue.getKey();
|
||||
Object value = sourceValue.getValue();
|
||||
if (binding.isEditable()) {
|
||||
return new PropertyNotWriteableResult(property, value, messageSource);
|
||||
if (!binding.isEditable()) {
|
||||
return new PropertyNotEditableResult(property, value, messageSource);
|
||||
} else {
|
||||
binding.applySourceValue(value);
|
||||
if (binding.getStatus() == BindingStatus.DIRTY) {
|
||||
@@ -156,4 +168,109 @@ public class GenericBinder implements Binder {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
class GenericBindingRule implements BindingRule, BindingRuleConfiguration {
|
||||
|
||||
private String property;
|
||||
|
||||
private Formatter formatter = DefaultFormatter.INSTANCE;
|
||||
|
||||
private Formatter elementFormatter = DefaultFormatter.INSTANCE;
|
||||
|
||||
private Formatter keyFormatter = DefaultFormatter.INSTANCE;
|
||||
|
||||
private Condition editableCondition = Condition.ALWAYS_TRUE;
|
||||
|
||||
private Condition enabledCondition = Condition.ALWAYS_TRUE;
|
||||
|
||||
private Condition visibleCondition = Condition.ALWAYS_TRUE;
|
||||
|
||||
private Map<String, GenericBindingRule> nestedBindingRules;
|
||||
|
||||
private Binding binding;
|
||||
|
||||
public GenericBindingRule(String property) {
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
// implementing BindingRule
|
||||
|
||||
public Binding getBinding(String property, Object model) {
|
||||
return getBindingRule(property).getBinding(model);
|
||||
}
|
||||
|
||||
public Formatter<?> getFormatter() {
|
||||
return formatter;
|
||||
}
|
||||
|
||||
public Formatter<?> getElementFormatter() {
|
||||
return elementFormatter;
|
||||
}
|
||||
|
||||
public Formatter<?> getKeyFormatter() {
|
||||
return keyFormatter;
|
||||
}
|
||||
|
||||
public Condition getEnabledCondition() {
|
||||
return enabledCondition;
|
||||
}
|
||||
|
||||
public Condition getEditableCondition() {
|
||||
return editableCondition;
|
||||
}
|
||||
|
||||
public Condition getVisibleCondition() {
|
||||
return visibleCondition;
|
||||
}
|
||||
|
||||
// implementing BindingRuleConfiguration
|
||||
|
||||
public BindingRuleConfiguration formatWith(Formatter<?> formatter) {
|
||||
this.formatter = formatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration formatElementsWith(Formatter<?> formatter) {
|
||||
elementFormatter = formatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration formatKeysWith(Formatter<?> formatter) {
|
||||
keyFormatter = formatter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration editableWhen(Condition condition) {
|
||||
editableCondition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration enabledWhen(Condition condition) {
|
||||
enabledCondition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindingRuleConfiguration visibleWhen(Condition condition) {
|
||||
visibleCondition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
GenericBindingRule getBindingRule(String property) {
|
||||
GenericBindingRule rule = nestedBindingRules.get(property);
|
||||
if (rule == null) {
|
||||
rule = new GenericBindingRule(property);
|
||||
nestedBindingRules.put(property, rule);
|
||||
}
|
||||
return rule;
|
||||
}
|
||||
|
||||
Binding getBinding(Object model) {
|
||||
if (binding == null) {
|
||||
binding = new PropertyBinding(property, model, typeConverter, this);
|
||||
}
|
||||
return binding;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,16 +36,12 @@ public class PropertyBinding implements Binding {
|
||||
|
||||
private Object model;
|
||||
|
||||
private Formatter valueFormatter = DefaultFormatter.INSTANCE;
|
||||
|
||||
private Formatter mapKeyFormatter = DefaultFormatter.INSTANCE;
|
||||
|
||||
private Formatter indexedValueFormatter = DefaultFormatter.INSTANCE;
|
||||
|
||||
private PropertyDescriptor propertyDescriptor;
|
||||
|
||||
private TypeConverter typeConverter;
|
||||
|
||||
private BindingRule bindingRule;
|
||||
|
||||
private PropertyDescriptor propertyDescriptor;
|
||||
|
||||
private Object sourceValue;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@@ -55,11 +51,11 @@ public class PropertyBinding implements Binding {
|
||||
|
||||
private BindingStatus status;
|
||||
|
||||
public PropertyBinding(String property, Object model, TypeConverter typeConverter) {
|
||||
this.propertyDescriptor = findPropertyDescriptor(property, model);
|
||||
this.property = property;
|
||||
public PropertyBinding(String property, Object model, TypeConverter typeConverter, BindingRule bindingRule) {
|
||||
initProperty(property, model);
|
||||
this.model = model;
|
||||
this.typeConverter = typeConverter;
|
||||
this.bindingRule = bindingRule;
|
||||
this.buffer = new ValueBuffer(getModel());
|
||||
status = BindingStatus.CLEAN;
|
||||
}
|
||||
@@ -75,15 +71,15 @@ public class PropertyBinding implements Binding {
|
||||
}
|
||||
|
||||
public boolean isEditable() {
|
||||
return propertyDescriptor.getWriteMethod() != null || !markedNotEditable();
|
||||
return isWriteableProperty() && bindingRule.getEditableCondition().isTrue();
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
return bindingRule.getEnabledCondition().isTrue();
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return true;
|
||||
return bindingRule.getVisibleCondition().isTrue();
|
||||
}
|
||||
|
||||
public void applySourceValue(Object sourceValue) {
|
||||
@@ -91,7 +87,7 @@ public class PropertyBinding implements Binding {
|
||||
assertEnabled();
|
||||
if (sourceValue instanceof String) {
|
||||
try {
|
||||
buffer.setValue(valueFormatter.parse((String) sourceValue, getLocale()));
|
||||
buffer.setValue(bindingRule.getFormatter().parse((String) sourceValue, getLocale()));
|
||||
sourceValue = null;
|
||||
status = BindingStatus.DIRTY;
|
||||
} catch (ParseException e) {
|
||||
@@ -101,7 +97,7 @@ public class PropertyBinding implements Binding {
|
||||
}
|
||||
} else if (sourceValue instanceof String[]) {
|
||||
String[] sourceValues = (String[]) sourceValue;
|
||||
Class<?> parsedType = getFormattedObjectType(indexedValueFormatter.getClass());
|
||||
Class<?> parsedType = getFormattedObjectType(bindingRule.getElementFormatter().getClass());
|
||||
if (parsedType == null) {
|
||||
parsedType = String.class;
|
||||
}
|
||||
@@ -109,7 +105,7 @@ public class PropertyBinding implements Binding {
|
||||
for (int i = 0; i < sourceValues.length; i++) {
|
||||
Object parsedValue;
|
||||
try {
|
||||
parsedValue = indexedValueFormatter.parse(sourceValues[i], LocaleContextHolder.getLocale());
|
||||
parsedValue = bindingRule.getElementFormatter().parse(sourceValues[i], LocaleContextHolder.getLocale());
|
||||
Array.set(parsed, i, parsedValue);
|
||||
} catch (ParseException e) {
|
||||
this.sourceValue = sourceValue;
|
||||
@@ -242,12 +238,12 @@ public class PropertyBinding implements Binding {
|
||||
};
|
||||
}
|
||||
|
||||
public Binding getBinding(String nestedProperty) {
|
||||
public Binding getBinding(String property) {
|
||||
assertScalarProperty();
|
||||
if (getValue() == null) {
|
||||
createValue();
|
||||
}
|
||||
return new PropertyBinding(nestedProperty, getValue(), typeConverter);
|
||||
return bindingRule.getBinding(property, getValue());
|
||||
}
|
||||
|
||||
public boolean isList() {
|
||||
@@ -268,7 +264,7 @@ public class PropertyBinding implements Binding {
|
||||
assertMapProperty();
|
||||
if (key instanceof String) {
|
||||
try {
|
||||
key = mapKeyFormatter.parse((String) key, getLocale());
|
||||
key = bindingRule.getKeyFormatter().parse((String) key, getLocale());
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Invald key", e);
|
||||
}
|
||||
@@ -280,9 +276,9 @@ public class PropertyBinding implements Binding {
|
||||
public String formatValue(Object value) {
|
||||
Formatter formatter;
|
||||
if (isList() || isMap()) {
|
||||
formatter = indexedValueFormatter;
|
||||
formatter = bindingRule.getElementFormatter();
|
||||
} else {
|
||||
formatter = valueFormatter;
|
||||
formatter = bindingRule.getFormatter();
|
||||
}
|
||||
Class<?> formattedType = getFormattedObjectType(formatter.getClass());
|
||||
value = typeConverter.convert(value, formattedType);
|
||||
@@ -291,6 +287,11 @@ public class PropertyBinding implements Binding {
|
||||
|
||||
// internal helpers
|
||||
|
||||
private void initProperty(String property, Object model) {
|
||||
this.propertyDescriptor = findPropertyDescriptor(property, model);
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
private PropertyDescriptor findPropertyDescriptor(String property, Object model) {
|
||||
PropertyDescriptor[] propDescs = getBeanInfo(model.getClass()).getPropertyDescriptors();
|
||||
for (PropertyDescriptor propDesc : propDescs) {
|
||||
@@ -395,7 +396,7 @@ public class PropertyBinding implements Binding {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean markedNotEditable() {
|
||||
return false;
|
||||
private boolean isWriteableProperty() {
|
||||
return propertyDescriptor.getWriteMethod() != null;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import org.springframework.ui.binding.BindingResult;
|
||||
import org.springframework.ui.message.MessageBuilder;
|
||||
import org.springframework.ui.message.ResolvableArgument;
|
||||
|
||||
class PropertyNotWriteableResult implements BindingResult {
|
||||
class PropertyNotEditableResult implements BindingResult {
|
||||
|
||||
private String property;
|
||||
|
||||
@@ -19,7 +19,7 @@ class PropertyNotWriteableResult implements BindingResult {
|
||||
|
||||
private MessageSource messageSource;
|
||||
|
||||
public PropertyNotWriteableResult(String property, Object sourceValue, MessageSource messageSource) {
|
||||
public PropertyNotEditableResult(String property, Object sourceValue, MessageSource messageSource) {
|
||||
this.property = property;
|
||||
this.sourceValue = sourceValue;
|
||||
this.messageSource = messageSource;
|
||||
@@ -5,10 +5,10 @@ package org.springframework.ui.binding.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class PropertyPath {
|
||||
public class PropertyPath implements Iterable<PropertyPathElement> {
|
||||
|
||||
private List<PropertyPathElement> elements = new ArrayList<PropertyPathElement>();
|
||||
|
||||
@@ -41,4 +41,8 @@ public class PropertyPath {
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<PropertyPathElement> iterator() {
|
||||
return elements.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user