org.springframework.binding.form.support
Class FormModelMediatingValueModel

java.lang.Object
  extended by org.springframework.binding.value.support.AbstractValueModelWrapper
      extended by org.springframework.binding.form.support.FormModelMediatingValueModel
All Implemented Interfaces:
PropertyChangeListener, EventListener, PropertyChangePublisher, DirtyTrackingValueModel, ValueModelWrapper, ValueModel

public class FormModelMediatingValueModel
extends AbstractValueModelWrapper
implements DirtyTrackingValueModel, PropertyChangeListener

A value model wrapper that mediates between the (wrapped) data value model and the derived view value model. Allows for value change event delivery to be disabled.

Use the provided method setDeliverValueChangeEvents(boolean) to enable/disable the event mechanism. This makes it possible to change all valueModels and only then fire all events (as it is used in AbstractFormModel). Events are handled internally by the dirtyChangeListeners and the mediatedValueHolder.

As this is also a DirtyTrackingValueModel, the implementation allows reverting to the original value by using revertToOriginal(), which uses the value stored in originalValue to reset the wrappedValueModel. This originalValue is updated to hold the wrappedValue when using clearDirty().

Small sketch to illustrate the positioning and usage:

 <setup>
 External actor -> FormModelMediatingValueModel -> wrappedValueModel
                   holds originalValue = ori       wrappedValue = ori
                   events = enabled
 
 <use case>
 events disabled -> events = disabled            -> wrappedValue = ori
 write value A   -> delagates to wrappedModel    -> wrappedValue = A
                    originalValue = ori
                    update dirty state
 events enabled  -> events = enabled             -> wrappedValue = A
                    sends events (dirty...)
 clearDirty      -> originalValue = A            -> wrappedValue = A
 OR
 revertToOriginal-> set originalValue on wrapped -> wrappedValue = ori
                    update dirty state                  
 

Author:
Oliver Hutchison

Field Summary
 
Fields inherited from interface org.springframework.binding.value.support.DirtyTrackingValueModel
DIRTY_PROPERTY
 
Fields inherited from interface org.springframework.binding.value.ValueModel
VALUE_PROPERTY
 
Constructor Summary
FormModelMediatingValueModel(ValueModel propertyValueModel)
          Constructor which defaults trackDirty=true.
FormModelMediatingValueModel(ValueModel propertyValueModel, boolean trackDirty)
          Constructor.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener listener)
          Register a listener to all properties of this publisher.
 void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
          Register a listener to a specific property.
 void addValueChangeListener(PropertyChangeListener listener)
          Registers the given PropertyChangeListener with this ValueModel.
 void clearDirty()
          Resets the dirty state of this model to false.
protected  void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
          Handles the dirty event firing.
protected  ValueChangeDetector getValueChangeDetector()
           
 boolean isDirty()
          Returns true if value held by this model has changed since the last call to reset or the last time a value came up from the inner model.
 void propertyChange(PropertyChangeEvent evt)
           
 void removePropertyChangeListener(PropertyChangeListener listener)
          Remove the listener from all properties of this publisher.
 void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
          Remove the listener from a specific property.
 void removeValueChangeListener(PropertyChangeListener listener)
          Deregisters the given PropertyChangeListener from this ValueModel.
 void revertToOriginal()
          Reverts the value held by this model to the original value at the last call to reset or the last time a value came up from the inner model.
 void setDeliverValueChangeEvents(boolean deliverValueChangeEvents)
           Enable/disable the event mechanism.
 void setValueChangeDetector(ValueChangeDetector valueChangeDetector)
           
 void setValueSilently(Object value, PropertyChangeListener listenerToSkip)
          Sets a new value and if the value has changed notifies all registered value change listeners except for the specified listener to skip.
protected  void updateDirtyState()
          Check the dirty state and fire events if needed.
 
Methods inherited from class org.springframework.binding.value.support.AbstractValueModelWrapper
getInnerMostValue, getInnerMostWrappedValueModel, getValue, getWrappedValueModel, setValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.binding.value.ValueModel
getValue, setValue
 

Constructor Detail

FormModelMediatingValueModel

public FormModelMediatingValueModel(ValueModel propertyValueModel)
Constructor which defaults trackDirty=true.

Parameters:
propertyValueModel - the ValueModel to mediate.

FormModelMediatingValueModel

public FormModelMediatingValueModel(ValueModel propertyValueModel,
                                    boolean trackDirty)
Constructor.

Parameters:
propertyValueModel - the valueModel to mediate.
trackDirty - disable/enable dirty tracking.
Method Detail

setValueSilently

public void setValueSilently(Object value,
                             PropertyChangeListener listenerToSkip)
Description copied from interface: ValueModel
Sets a new value and if the value has changed notifies all registered value change listeners except for the specified listener to skip.

Specified by:
setValueSilently in interface ValueModel
Overrides:
setValueSilently in class AbstractValueModelWrapper
Parameters:
value - the value to be set
listenerToSkip - the PropertyChangeListener that should not be notified of this change (may be null).

propertyChange

public void propertyChange(PropertyChangeEvent evt)
Specified by:
propertyChange in interface PropertyChangeListener

setDeliverValueChangeEvents

public void setDeliverValueChangeEvents(boolean deliverValueChangeEvents)

Enable/disable the event mechanism. Makes it possible to control the timing of event firing (delay the events).

When disabling, no dirty events will be fired and the mediating valueHolder will not set it's value. The latter results in not firing other events like valueChangedEvents.

When enabling, original (stored) value is compared to the newer value in the wrapped model and the necessary events are fired (dirty/valueChanged).

Parameters:
deliverValueChangeEvents - boolean to enable/disable event mechanism.

isDirty

public boolean isDirty()
Description copied from interface: DirtyTrackingValueModel
Returns true if value held by this model has changed since the last call to reset or the last time a value came up from the inner model.

Specified by:
isDirty in interface DirtyTrackingValueModel

clearDirty

public void clearDirty()
Description copied from interface: DirtyTrackingValueModel
Resets the dirty state of this model to false.

Specified by:
clearDirty in interface DirtyTrackingValueModel

revertToOriginal

public void revertToOriginal()
Description copied from interface: DirtyTrackingValueModel
Reverts the value held by this model to the original value at the last call to reset or the last time a value came up from the inner model.

Specified by:
revertToOriginal in interface DirtyTrackingValueModel

updateDirtyState

protected void updateDirtyState()
Check the dirty state and fire events if needed.


setValueChangeDetector

public void setValueChangeDetector(ValueChangeDetector valueChangeDetector)
Parameters:
valueChangeDetector - set the ValueChangeDetector to use when checking the dirty state.

getValueChangeDetector

protected ValueChangeDetector getValueChangeDetector()
Returns:
a ValueChangeDetector to use when checking the dirty state.

addValueChangeListener

public void addValueChangeListener(PropertyChangeListener listener)
Description copied from interface: ValueModel
Registers the given PropertyChangeListener with this ValueModel. The listener will be notified if the value has changed. The PropertyChangeEvents delivered to the listener must have the name set to "value". The latter ensures that all ValueModel implementers behave like the AbstractValueModel subclasses.

To comply with the above specification implementers can use the PropertyChangeSupport's #addPropertyChangeListener method that accepts a property name, so that listeners will be invoked only if that specific property has changed.

Specified by:
addValueChangeListener in interface ValueModel
Overrides:
addValueChangeListener in class AbstractValueModelWrapper
Parameters:
listener - the listener to be added
See Also:
AbstractValueModel.addValueChangeListener(PropertyChangeListener)

removeValueChangeListener

public void removeValueChangeListener(PropertyChangeListener listener)
Description copied from interface: ValueModel
Deregisters the given PropertyChangeListener from this ValueModel.

Specified by:
removeValueChangeListener in interface ValueModel
Overrides:
removeValueChangeListener in class AbstractValueModelWrapper
Parameters:
listener - the listener to be removed

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Description copied from interface: PropertyChangePublisher
Register a listener to all properties of this publisher.

Specified by:
addPropertyChangeListener in interface PropertyChangePublisher
Parameters:
listener - the PropertyChangeListener to register.

addPropertyChangeListener

public void addPropertyChangeListener(String propertyName,
                                      PropertyChangeListener listener)
Description copied from interface: PropertyChangePublisher
Register a listener to a specific property.

Specified by:
addPropertyChangeListener in interface PropertyChangePublisher
Parameters:
propertyName - the property to monitor.
listener - the PropertyChangeListener to register.

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Description copied from interface: PropertyChangePublisher
Remove the listener from all properties of this publisher.

Specified by:
removePropertyChangeListener in interface PropertyChangePublisher
Parameters:
listener - the PropertyChangeListener to remove.

removePropertyChangeListener

public void removePropertyChangeListener(String propertyName,
                                         PropertyChangeListener listener)
Description copied from interface: PropertyChangePublisher
Remove the listener from a specific property.

Specified by:
removePropertyChangeListener in interface PropertyChangePublisher
Parameters:
propertyName - the property that was being monitored.
listener - the PropertyChangeListener to remove.

firePropertyChange

protected final void firePropertyChange(String propertyName,
                                        boolean oldValue,
                                        boolean newValue)
Handles the dirty event firing.

Parameters:
propertyName - implementation only handles DIRTY_PROPERTY.
oldValue -
newValue -


Copyright © 2004-2008 The Spring Framework. All Rights Reserved.