Class UpdateListStrategy<S,D>
- Type Parameters:
S- the type of the elements on the source side (i.e. the model side if this is a model-to-target update and the target side if this is a target-to-model update)D- the type of the elements on the destination side (i.e. the target side if this is a model-to-target update and the model side if this is a target-to-model update)
Binding between two observable
lists. The following behaviors can be customized via the strategy:
- Conversion
- Automatic processing
Conversion:
When elements are added they can be converted to the
destination element type.
Automatic processing:
The processing to perform when the source observable changes. This behavior
is configured via policies provided on construction of the strategy (e.g.
POLICY_NEVER, POLICY_ON_REQUEST, POLICY_UPDATE).
- Since:
- 1.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected IConverter<? super S, ? extends D> static intPolicy constant denoting that the source observable's state should not be tracked and that the destination observable's state should never be updated.static intPolicy constant denoting that the source observable's state should not be tracked, but that conversion and updating the destination observable's state should be performed when explicitly requested.static intPolicy constant denoting that the source observable's state should be tracked, and that conversion and updating the destination observable's state should be performed automatically on every change of the source observable state.protected boolean -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new update list strategy for automatically updating the destination observable list whenever the source observable list changes.UpdateListStrategy(boolean provideDefaults, int updatePolicy) Creates a new update list strategy with a configurable update policy.UpdateListStrategy(int updatePolicy) Creates a new update list strategy with a configurable update policy. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidcheckAssignable(Object toType, Object fromType, String errorString) Converts the value from the source type to the destination type.static <S,D> UpdateListStrategy <S, D> create(IConverter<S, D> converter) Convenience method that creates anUpdateValueStrategywith the given converter.protected IConverter<?, ?> createConverter(Object fromType, Object toType) Tries to create a converter that can convert from values of type fromType.protected IStatusdoAdd(IObservableList<? super D> observableList, D element, int index) Adds the given element at the given index to the given observable list.protected IStatusdoMove(IObservableList<? super D> observableList, int oldIndex, int newIndex) Moves the element in the observable list located at the given old index to the given new index.protected IStatusdoRemove(IObservableList<? super D> observableList, int index) Removes the element at the given index from the given observable list.protected IStatusdoReplace(IObservableList<? super D> observableList, int index, D element) Replaces the element in the observable list located at the given index to with the given element.protected voidfillDefaults(IObservableList<? extends S> source, IObservableList<? super D> destination) intprotected BooleanisAssignableFromTo(Object fromType, Object toType) ReturnsBoolean.TRUEif the from type is assignable to the to type, orBoolean.FALSEif it not, ornullif unknown.protected IStatusstatic <S,D> UpdateListStrategy <S, D> never()Convenience method that creates an update strategy that never updates its observables, usingPOLICY_NEVERand no defaults.setConverter(IConverter<? super S, ? extends D> converter) Sets the converter to be invoked when converting added elements from the source element type to the destination element type.protected booleanReturns whether ListBinding should calldoMove(IObservableList, int, int)anddoReplace(IObservableList, int, Object)instead of paired calls todoRemove(IObservableList, int)anddoAdd(IObservableList, Object, int).
-
Field Details
-
POLICY_NEVER
public static int POLICY_NEVERPolicy constant denoting that the source observable's state should not be tracked and that the destination observable's state should never be updated. -
POLICY_ON_REQUEST
public static int POLICY_ON_REQUESTPolicy constant denoting that the source observable's state should not be tracked, but that conversion and updating the destination observable's state should be performed when explicitly requested. -
POLICY_UPDATE
public static int POLICY_UPDATEPolicy constant denoting that the source observable's state should be tracked, and that conversion and updating the destination observable's state should be performed automatically on every change of the source observable state. -
provideDefaults
protected boolean provideDefaults -
converter
-
-
Constructor Details
-
UpdateListStrategy
public UpdateListStrategy()Creates a new update list strategy for automatically updating the destination observable list whenever the source observable list changes. A default converter will be provided. The defaults can be changed by calling one of the setter methods. -
UpdateListStrategy
public UpdateListStrategy(int updatePolicy) Creates a new update list strategy with a configurable update policy. A default converter will be provided. The defaults can be changed by calling one of the setter methods.- Parameters:
updatePolicy- one ofPOLICY_NEVER,POLICY_ON_REQUEST, orPOLICY_UPDATE
-
UpdateListStrategy
public UpdateListStrategy(boolean provideDefaults, int updatePolicy) Creates a new update list strategy with a configurable update policy. A default converter will be provided ifprovideDefaultsistrue, seeDataBindingContext. The defaults can be changed by calling one of the setter methods.- Parameters:
provideDefaults- iftrue, default validators and a default converter will be provided based on the observable list's type, seeDataBindingContextupdatePolicy- one ofPOLICY_NEVER,POLICY_ON_REQUEST, orPOLICY_UPDATE
-
-
Method Details
-
fillDefaults
protected void fillDefaults(IObservableList<? extends S> source, IObservableList<? super D> destination) - Parameters:
source- source observable, to be used for its typedestination- destination observable, to be used for its type
-
getUpdatePolicy
public int getUpdatePolicy()- Returns:
- the update policy
-
setConverter
Sets the converter to be invoked when converting added elements from the source element type to the destination element type.If the converter throws any exceptions they are reported as validation errors, using the exception message.
- Parameters:
converter- the new converter- Returns:
- the receiver, to enable method call chaining
-
doAdd
Adds the given element at the given index to the given observable list. Clients may extend but must call the super implementation.- Parameters:
observableList- the list to add toelement- element to addindex- insertion index- Returns:
- a status
-
doRemove
Removes the element at the given index from the given observable list. Clients may extend but must call the super implementation.- Parameters:
observableList- the list to remove fromindex- element index to remove- Returns:
- a status
-
useMoveAndReplace
protected boolean useMoveAndReplace()Returns whether ListBinding should calldoMove(IObservableList, int, int)anddoReplace(IObservableList, int, Object)instead of paired calls todoRemove(IObservableList, int)anddoAdd(IObservableList, Object, int). The default implementation returns true for this class and false for subclasses.This method is provided for the benefit of subclasses which extend the
doAdd(IObservableList, Object, int)anddoRemove(IObservableList, int)methods. ThedoMove(IObservableList, int, int)anddoReplace(IObservableList, int, Object)methods were added in version 1.2 so that logically related remove() and add() operations could be combined into a single method call. However to ensure that custom behavior is not lost in existing code,ListBindingis advised by this method whether these new methods may be used.To enable use of these methods in subclasses, override this method to return true.
- Returns:
- whether ListBinding should call doMove() and doReplace() instead of paired calls of doRemove() and doAdd()
- Since:
- 1.2
- Restriction:
- This method is not intended to be referenced by clients.
-
doMove
Moves the element in the observable list located at the given old index to the given new index.- Parameters:
observableList- the list to manipulateoldIndex- current element indexnewIndex- new element index- Returns:
- a status
- Since:
- 1.2
-
doReplace
Replaces the element in the observable list located at the given index to with the given element.- Parameters:
observableList- the list to manipulateindex- index of the existing elementelement- new element for the given index- Returns:
- a status
- Since:
- 1.2
-
create
Convenience method that creates anUpdateValueStrategywith the given converter. It usesPOLICY_UPDATE.- Parameters:
converter- the converter- Returns:
- the update strategy
- Since:
- 1.8
-
never
Convenience method that creates an update strategy that never updates its observables, usingPOLICY_NEVERand no defaults.- Returns:
- the update strategy
- Since:
- 1.8
-
checkAssignable
-
createConverter
Tries to create a converter that can convert from values of type fromType. Returnsnullif no converter could be created. Either toType or modelDescription can benull, but not both.- Parameters:
fromType- source typetoType- target type- Returns:
- an IConverter, or
nullif unsuccessful
-
isAssignableFromTo
ReturnsBoolean.TRUEif the from type is assignable to the to type, orBoolean.FALSEif it not, ornullif unknown.- Parameters:
fromType- source type to assigntoType- target type to check assignability against- Returns:
- whether fromType is assignable to toType, or
nullif unknown
-
logErrorWhileSettingValue
- Parameters:
ex- the exception, that was caught- Returns:
- the validation status
-
convert
Converts the value from the source type to the destination type.Default implementation will use the setConverter(IConverter), if one exists. If no converter exists no conversion occurs.
- Parameters:
value- source value to convert- Returns:
- the converted value
-