Browse Source

new presentation model methods; ability to surpress commit of dirty value in PMB

pull/23217/head
Keith Donald 17 years ago
parent
commit
f0ca94d5f3
  1. 19
      org.springframework.context/src/main/java/org/springframework/model/ui/PresentationModel.java
  2. 14
      org.springframework.context/src/main/java/org/springframework/model/ui/support/DefaultPresentationModel.java
  3. 28
      org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java
  4. 1
      org.springframework.context/src/test/java/org/springframework/model/ui/support/PresentationModelBinderTests.java
  5. 3
      org.springframework.context/src/test/java/org/springframework/model/ui/support/WebBinderTests.java

19
org.springframework.context/src/main/java/org/springframework/model/ui/PresentationModel.java

@ -15,7 +15,6 @@
*/ */
package org.springframework.model.ui; package org.springframework.model.ui;
/** /**
* Represents the state and behavior of a presentation independently of the GUI controls used in the interface. * Represents the state and behavior of a presentation independently of the GUI controls used in the interface.
* Pulls the state and behavior of a view out into a model class that is part of the presentation. * Pulls the state and behavior of a view out into a model class that is part of the presentation.
@ -31,5 +30,23 @@ public interface PresentationModel {
* @throws FieldNotFoundException if no such field exists * @throws FieldNotFoundException if no such field exists
*/ */
FieldModel getFieldModel(String fieldName); FieldModel getFieldModel(String fieldName);
/**
* If errors are present on this PresentationModel.
* Returns true if at least one FieldModel has {@link BindingStatus#INVALID_SUBMITTED_VALUE invalid submitted values} or is {@link ValidationStatus#INVALID invalid}.
*/
boolean hasErrors();
/**
* Commit any {@link BindingStatus#DIRTY dirty} fields.
* @throws IllegalStateException if there are field models that have {@link BindingStatus#INVALID_SUBMITTED_VALUE invalid submitted values} or are {@link ValidationStatus#INVALID invalid}.
*/
void commit();
/**
* Validate all fields.
* Skips any fields with {@link BindingStatus#INVALID_SUBMITTED_VALUE invalid submitted values}.
*/
void validate();
} }

14
org.springframework.context/src/main/java/org/springframework/model/ui/support/DefaultPresentationModel.java

@ -143,6 +143,20 @@ public class DefaultPresentationModel implements PresentationModel {
return field; return field;
} }
public boolean hasErrors() {
return false;
}
public void commit() {
}
public void validate() {
}
// internal helpers
private PropertyFieldModelRule getRule(String fieldName) { private PropertyFieldModelRule getRule(String fieldName) {
PropertyFieldModelRule rule = fieldModelRules.get(fieldName); PropertyFieldModelRule rule = fieldModelRules.get(fieldName);
if (rule == null) { if (rule == null) {

28
org.springframework.context/src/main/java/org/springframework/model/ui/support/PresentationModelBinder.java

@ -35,28 +35,36 @@ import org.springframework.model.ui.PresentationModel;
* @since 3.0 * @since 3.0
* @see #setMessageSource(MessageSource) * @see #setMessageSource(MessageSource)
* @see #setRequiredFields(String[]) * @see #setRequiredFields(String[])
* @see #setCommitDirtyValue(boolean)
* @see #bind(Map, PresentationModel) * @see #bind(Map, PresentationModel)
*/ */
public class PresentationModelBinder extends AbstractBinder<PresentationModel> { public class PresentationModelBinder extends AbstractBinder<PresentationModel> {
// subclassing hooks private boolean commitDirtyValue;
/**
* Configures if this PresentationModelBinder should eagerly commit the dirty value after a successful field binding.
* Default is false.
*/
public void setCommitDirtyValue(boolean commitDirtyValue) {
this.commitDirtyValue = commitDirtyValue;
}
// subclass hooks
@Override @Override
protected FieldBinder createFieldBinder(PresentationModel model) { protected FieldBinder createFieldBinder(PresentationModel model) {
return new FieldModelBinder(model, getMessageSource()); return new FieldModelBinder(model);
} }
// internal helpers // internal helpers
private static class FieldModelBinder implements FieldBinder { private class FieldModelBinder implements FieldBinder {
private PresentationModel presentationModel; private PresentationModel presentationModel;
private MessageSource messageSource; public FieldModelBinder(PresentationModel presentationModel) {
public FieldModelBinder(PresentationModel presentationModel, MessageSource messageSource) {
this.presentationModel = presentationModel; this.presentationModel = presentationModel;
this.messageSource = messageSource;
} }
public BindingResult bind(String fieldName, Object value) { public BindingResult bind(String fieldName, Object value) {
@ -64,13 +72,13 @@ public class PresentationModelBinder extends AbstractBinder<PresentationModel> {
try { try {
field = presentationModel.getFieldModel(fieldName); field = presentationModel.getFieldModel(fieldName);
} catch (FieldNotFoundException e) { } catch (FieldNotFoundException e) {
return new FieldNotFoundResult(fieldName, value, messageSource); return new FieldNotFoundResult(fieldName, value, getMessageSource());
} }
if (!field.isEditable()) { if (!field.isEditable()) {
return new FieldNotEditableResult(fieldName, value, messageSource); return new FieldNotEditableResult(fieldName, value, getMessageSource());
} else { } else {
field.applySubmittedValue(value); field.applySubmittedValue(value);
if (field.getBindingStatus() == BindingStatus.DIRTY) { if (field.getBindingStatus() == BindingStatus.DIRTY && commitDirtyValue) {
field.commit(); field.commit();
} }
return new AlertBindingResult(fieldName, value, field.getStatusAlert()); return new AlertBindingResult(fieldName, value, field.getStatusAlert());

1
org.springframework.context/src/test/java/org/springframework/model/ui/support/PresentationModelBinderTests.java

@ -48,6 +48,7 @@ public class PresentationModelBinderTests {
bean = new TestBean(); bean = new TestBean();
presentationModel = new DefaultPresentationModel(bean); presentationModel = new DefaultPresentationModel(bean);
binder = new PresentationModelBinder(); binder = new PresentationModelBinder();
binder.setCommitDirtyValue(true);
LocaleContextHolder.setLocale(Locale.US); LocaleContextHolder.setLocale(Locale.US);
} }

3
org.springframework.context/src/test/java/org/springframework/model/ui/support/WebBinderTests.java

@ -34,7 +34,8 @@ public class WebBinderTests {
public void setUp() { public void setUp() {
LocaleContextHolder.setLocale(Locale.US); LocaleContextHolder.setLocale(Locale.US);
presentationModel = new DefaultPresentationModel(bean); presentationModel = new DefaultPresentationModel(bean);
binder = new WebBinder(); binder = new WebBinder();
binder.setCommitDirtyValue(true);
} }
@After @After

Loading…
Cancel
Save