Browse Source

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

conversation
Keith Donald 16 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. 1
      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 @@ @@ -15,7 +15,6 @@
*/
package org.springframework.model.ui;
/**
* 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.
@ -32,4 +31,22 @@ public interface PresentationModel { @@ -32,4 +31,22 @@ public interface PresentationModel {
*/
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 { @@ -143,6 +143,20 @@ public class DefaultPresentationModel implements PresentationModel {
return field;
}
public boolean hasErrors() {
return false;
}
public void commit() {
}
public void validate() {
}
// internal helpers
private PropertyFieldModelRule getRule(String fieldName) {
PropertyFieldModelRule rule = fieldModelRules.get(fieldName);
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; @@ -35,28 +35,36 @@ import org.springframework.model.ui.PresentationModel;
* @since 3.0
* @see #setMessageSource(MessageSource)
* @see #setRequiredFields(String[])
* @see #setCommitDirtyValue(boolean)
* @see #bind(Map, 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
protected FieldBinder createFieldBinder(PresentationModel model) {
return new FieldModelBinder(model, getMessageSource());
return new FieldModelBinder(model);
}
// internal helpers
private static class FieldModelBinder implements FieldBinder {
private class FieldModelBinder implements FieldBinder {
private PresentationModel presentationModel;
private MessageSource messageSource;
public FieldModelBinder(PresentationModel presentationModel, MessageSource messageSource) {
public FieldModelBinder(PresentationModel presentationModel) {
this.presentationModel = presentationModel;
this.messageSource = messageSource;
}
public BindingResult bind(String fieldName, Object value) {
@ -64,13 +72,13 @@ public class PresentationModelBinder extends AbstractBinder<PresentationModel> { @@ -64,13 +72,13 @@ public class PresentationModelBinder extends AbstractBinder<PresentationModel> {
try {
field = presentationModel.getFieldModel(fieldName);
} catch (FieldNotFoundException e) {
return new FieldNotFoundResult(fieldName, value, messageSource);
return new FieldNotFoundResult(fieldName, value, getMessageSource());
}
if (!field.isEditable()) {
return new FieldNotEditableResult(fieldName, value, messageSource);
return new FieldNotEditableResult(fieldName, value, getMessageSource());
} else {
field.applySubmittedValue(value);
if (field.getBindingStatus() == BindingStatus.DIRTY) {
if (field.getBindingStatus() == BindingStatus.DIRTY && commitDirtyValue) {
field.commit();
}
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 { @@ -48,6 +48,7 @@ public class PresentationModelBinderTests {
bean = new TestBean();
presentationModel = new DefaultPresentationModel(bean);
binder = new PresentationModelBinder();
binder.setCommitDirtyValue(true);
LocaleContextHolder.setLocale(Locale.US);
}

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

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

Loading…
Cancel
Save