This commit is contained in:
Keith Donald
2009-06-09 18:59:44 +00:00
parent 04115a4ff4
commit 78b4254d39
4 changed files with 61 additions and 62 deletions

View File

@@ -74,7 +74,6 @@ public class Binder<T> {
private boolean strict = false;
private static Formatter defaultFormatter = new Formatter() {
public String format(Object object, Locale locale) {
if (object == null) {
return "";
@@ -182,14 +181,8 @@ public class Binder<T> {
public List<BindingResult> bind(List<UserValue> userValues) {
List<BindingResult> results = new ArrayList<BindingResult>(userValues.size());
for (UserValue value : userValues) {
Binding binding = getBinding(value.getProperty());
if (value.isString()) {
results.add(binding.setValue((String) value.getValue()));
} else if (value.isStringArray()) {
results.add(binding.setValues((String[]) value.getValue()));
} else {
throw new IllegalArgumentException("Illegal argument " + value);
}
BindingImpl binding = (BindingImpl) getBinding(value.getProperty());
results.add(binding.setValue(value.getValue()));
}
return results;
}
@@ -205,6 +198,8 @@ public class Binder<T> {
formatter = config.getFormatter();
}
// implementing Binding
public String getValue() {
Object value;
try {
@@ -215,24 +210,16 @@ public class Binder<T> {
return format(value);
}
public BindingResult setValue(String formatted) {
Formatter formatter;
try {
formatter = getFormatter();
} catch (EvaluationException e) {
// could occur the property was not found or is not readable
// TODO probably should not handle all EL failures, only type conversion & property not found?
return new ExpressionEvaluationErrorResult(property.getExpressionString(), formatted, e);
public BindingResult setValue(Object value) {
if (value instanceof String) {
return setStringValue((String) value);
} else if (value instanceof String[]) {
return setStringValues((String[]) value);
} else {
return setObjectValue(value);
}
Object parsed;
try {
parsed = formatter.parse(formatted, LocaleContextHolder.getLocale());
} catch (ParseException e) {
return new InvalidFormatResult(property.getExpressionString(), formatted, e);
}
return setValue(parsed, formatted);
}
public String format(Object selectableValue) {
Formatter formatter;
try {
@@ -256,7 +243,7 @@ public class Binder<T> {
return typeDesc.isCollection() || typeDesc.isArray();
}
public String[] getValues() {
public String[] getCollectionValues() {
Object multiValue;
try {
multiValue = property.getValue(createEvaluationContext());
@@ -281,7 +268,27 @@ public class Binder<T> {
return formattedValues;
}
public BindingResult setValues(String[] formatted) {
// internal helpers
private BindingResult setStringValue(String formatted) {
Formatter formatter;
try {
formatter = getFormatter();
} catch (EvaluationException e) {
// could occur the property was not found or is not readable
// TODO probably should not handle all EL failures, only type conversion & property not found?
return new ExpressionEvaluationErrorResult(property.getExpressionString(), formatted, e);
}
Object parsed;
try {
parsed = formatter.parse(formatted, LocaleContextHolder.getLocale());
} catch (ParseException e) {
return new InvalidFormatResult(property.getExpressionString(), formatted, e);
}
return setValue(parsed, formatted);
}
private BindingResult setStringValues(String[] formatted) {
Formatter formatter;
try {
formatter = getFormatter();
@@ -306,8 +313,10 @@ public class Binder<T> {
}
return setValue(parsed, formatted);
}
// internal helpers
private BindingResult setObjectValue(Object value) {
return setValue(value, value);
}
private Formatter getFormatter() throws EvaluationException {
if (formatter != null) {
@@ -346,12 +355,12 @@ public class Binder<T> {
}
}
private BindingResult setValue(Object parsed, Object formatted) {
private BindingResult setValue(Object parsedValue, Object userValue) {
try {
property.setValue(createEvaluationContext(), parsed);
return new SuccessResult(property.getExpressionString(), formatted);
property.setValue(createEvaluationContext(), parsedValue);
return new SuccessResult(property.getExpressionString(), userValue);
} catch (EvaluationException e) {
return new ExpressionEvaluationErrorResult(property.getExpressionString(), formatted, e);
return new ExpressionEvaluationErrorResult(property.getExpressionString(), userValue, e);
}
}

View File

@@ -17,24 +17,22 @@ package org.springframework.ui.binding;
/**
* A binding between a user interface element and a model property.
* TODO - consider having setValue accept Object to allow for binding special objects like arrays & multi-part files
* @author Keith Donald
*/
public interface Binding {
// single-value properties
/**
* The formatted value to display in the user interface.
*/
String getValue();
/**
* Sets the model property value a from user-entered value.
* @param formatted the value entered by the user
* Set the property associated with this binding to the value provided.
* The value may be a formatted String, a formatted String[] if a collection binding, or an Object of a type that can be coersed to the underlying property type.
* @param value the new value to bind
*/
BindingResult setValue(String formatted);
BindingResult setValue(Object value);
/**
* Formats a candidate model property value for display in the user interface.
* @param selectableValue a possible value
@@ -42,11 +40,9 @@ public interface Binding {
*/
String format(Object selectableValue);
// multi-value properties
/**
* Is this binding associated with a collection or array property?
* If so, a client should call {@link #getValues()} to display property values in the user interface.
* If so, a client should call {@link #getCollectionValues()} to display property values in the user interface.
* A client should call {@link #setValues(String[])} to set model property values from user-entered/selected values.
*/
boolean isCollection();
@@ -54,12 +50,6 @@ public interface Binding {
/**
* When a collection binding, the formatted values to display in the user interface.
*/
String[] getValues();
/**
* When a collection binding, sets the model property values a from user-entered/selected values.
* @param formattedValues the values entered by the user
*/
BindingResult setValues(String[] formatted);
String[] getCollectionValues();
}

View File

@@ -73,7 +73,7 @@ public class UserValue {
* @param value the actual user-entered value
* @return the singleton user value list
*/
public static List<UserValue> singleton(String property, Object value) {
public static List<UserValue> single(String property, Object value) {
List<UserValue> values = new ArrayList<UserValue>(1);
values.add(new UserValue(property, value));
return values;