fixe bug in element type for jeremyg

This commit is contained in:
Keith Donald 2009-07-29 21:30:48 +00:00
parent 17dfc8b0fc
commit db487b6d42
1 changed files with 26 additions and 23 deletions

View File

@ -144,15 +144,15 @@ public class DefaultPresentationModel implements PresentationModel {
} }
public void validate() { public void validate() {
} }
public boolean hasErrors() { public boolean hasErrors() {
return false; return false;
} }
public void commit() { public void commit() {
} }
// internal helpers // internal helpers
@ -192,7 +192,7 @@ public class DefaultPresentationModel implements PresentationModel {
private Map<Integer, FieldModel> listElements; private Map<Integer, FieldModel> listElements;
private Map<Object, FieldModel> mapValues; private Map<Object, FieldModel> mapValues;
public PropertyFieldModelRule(String property, Class domainModelClass) { public PropertyFieldModelRule(String property, Class domainModelClass) {
this.domainModelClass = domainModelClass; this.domainModelClass = domainModelClass;
this.property = findPropertyDescriptor(property); this.property = findPropertyDescriptor(property);
@ -247,7 +247,7 @@ public class DefaultPresentationModel implements PresentationModel {
public String getLabel() { public String getLabel() {
return property.getName(); return property.getName();
} }
public FieldModel getNested(String fieldName) { public FieldModel getNested(String fieldName) {
createValueIfNecessary(); createValueIfNecessary();
return getNestedRule(fieldName, fieldModel.getValueType()).getFieldModel(fieldModel.getValue()); return getNestedRule(fieldName, fieldModel.getValueType()).getFieldModel(fieldModel.getValue());
@ -277,7 +277,8 @@ public class DefaultPresentationModel implements PresentationModel {
FieldModel field = mapValues.get(key); FieldModel field = mapValues.get(key);
if (field == null) { if (field == null) {
FieldModelContext context = new MapValueContext(key, this); FieldModelContext context = new MapValueContext(key, this);
ValueModel valueModel = new MapValueValueModel(key, getElementType(), (Map) fieldModel.getValue(), context); ValueModel valueModel = new MapValueValueModel(key, getElementType(), (Map) fieldModel.getValue(),
context);
field = new DefaultFieldModel(valueModel, context); field = new DefaultFieldModel(valueModel, context);
mapValues.put(key, field); mapValues.put(key, field);
} }
@ -293,8 +294,7 @@ public class DefaultPresentationModel implements PresentationModel {
public FieldModelConfiguration formatElementsWith(Formatter<?> formatter) { public FieldModelConfiguration formatElementsWith(Formatter<?> formatter) {
if (!List.class.isAssignableFrom(domainModelClass) || domainModelClass.isArray()) { if (!List.class.isAssignableFrom(domainModelClass) || domainModelClass.isArray()) {
throw new IllegalStateException( throw new IllegalStateException("Field is not a List or an Array; cannot set a element formatter");
"Field is not a List or an Array; cannot set a element formatter");
} }
elementFormatter = formatter; elementFormatter = formatter;
return this; return this;
@ -324,7 +324,7 @@ public class DefaultPresentationModel implements PresentationModel {
} }
// package private helpers // package private helpers
PropertyFieldModelRule getNestedRule(String propertyName) { PropertyFieldModelRule getNestedRule(String propertyName) {
return getNestedRule(propertyName, this.property.getPropertyType()); return getNestedRule(propertyName, this.property.getPropertyType());
} }
@ -344,10 +344,13 @@ public class DefaultPresentationModel implements PresentationModel {
// internal helpers // internal helpers
private Class<?> getElementType() { private Class<?> getElementType() {
if (Map.class.isAssignableFrom(property.getPropertyType())) { Class<?> propertyType = property.getPropertyType();
return GenericCollectionTypeResolver.getMapValueReturnType(property.getReadMethod()); if (Map.class.isAssignableFrom(propertyType)) {
return GenericCollectionTypeResolver.getMapValueReturnType(property.getReadMethod());
} else if (propertyType.isArray()) {
return property.getPropertyType().getComponentType();
} else { } else {
return GenericCollectionTypeResolver.getCollectionReturnType(property.getReadMethod()); return GenericCollectionTypeResolver.getCollectionReturnType(property.getReadMethod());
} }
} }
@ -396,9 +399,9 @@ public class DefaultPresentationModel implements PresentationModel {
value = newMapValue(fieldModel.getValueType()); value = newMapValue(fieldModel.getValueType());
fieldModel.applySubmittedValue(value); fieldModel.applySubmittedValue(value);
fieldModel.commit(); fieldModel.commit();
} }
} }
private void growListIfNecessary(int index) { private void growListIfNecessary(int index) {
List list = (List) fieldModel.getValue(); List list = (List) fieldModel.getValue();
if (list == null) { if (list == null) {
@ -443,18 +446,18 @@ public class DefaultPresentationModel implements PresentationModel {
} }
private static class ListElementContext implements FieldModelContext { private static class ListElementContext implements FieldModelContext {
private int index; private int index;
private PropertyFieldModelRule listBindingContext; private PropertyFieldModelRule listBindingContext;
final Map<String, FieldModel> nestedBindings = new HashMap<String, FieldModel>(); final Map<String, FieldModel> nestedBindings = new HashMap<String, FieldModel>();
public ListElementContext(int index, PropertyFieldModelRule listBindingContext) { public ListElementContext(int index, PropertyFieldModelRule listBindingContext) {
this.index = index; this.index = index;
this.listBindingContext = listBindingContext; this.listBindingContext = listBindingContext;
} }
public MessageSource getMessageSource() { public MessageSource getMessageSource() {
return listBindingContext.getMessageSource(); return listBindingContext.getMessageSource();
} }
@ -522,20 +525,20 @@ public class DefaultPresentationModel implements PresentationModel {
throw new IllegalArgumentException("Not yet supported"); throw new IllegalArgumentException("Not yet supported");
} }
}; };
private static class MapValueContext implements FieldModelContext { private static class MapValueContext implements FieldModelContext {
private Object key; private Object key;
private PropertyFieldModelRule mapContext; private PropertyFieldModelRule mapContext;
final Map<String, FieldModel> nestedBindings = new HashMap<String, FieldModel>(); final Map<String, FieldModel> nestedBindings = new HashMap<String, FieldModel>();
public MapValueContext(Object key, PropertyFieldModelRule mapContext) { public MapValueContext(Object key, PropertyFieldModelRule mapContext) {
this.key = key; this.key = key;
this.mapContext = mapContext; this.mapContext = mapContext;
} }
public MessageSource getMessageSource() { public MessageSource getMessageSource() {
return mapContext.getMessageSource(); return mapContext.getMessageSource();
} }