M - the type of the model class.@Beta public class ModelWrapper<M> extends Object
A typical workflow would be:
Additional requirements:
These requirements are quite common but there is a lot of code needed to copy between the model and the viewModel. Additionally we have a tight coupling because every time the structure of the model changes (for example a field is removed) we have several places in the viewModel that need to be adjusted.
This component can be used to simplify use cases like the described one and minimize the coupling between the model
and the viewModel. See the following code example. First without and afterwards with the ModelWrapper.
The model class:
public class Person {
private String name;
private String familyName;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Without ModelWrapper:
public class PersonViewModel implements ViewModel {
private StringProperty name = new SimpleStringProperty();
private StringProperty familyName = new SimpleStringProperty();
private IntegerProperty age = new SimpleIntegerProperty();
private Person person;
public void init(Person person) {
this.person = person;
reloadFromModel();
}
public void reset() {
this.name.setValue("");
this.familyName.setValue("");
this.age.setValue(0);
}
public void reloadFromModel() {
this.name.setValue(person.getName());
this.familyName.setValue(person.getFamilyName());
this.age.setValue(person.getAge());
}
public void save() {
if (someValidation() && person != null) {
person.setName(name.getValue());
person.setFamilyName(familyName.getValue());
person.setAge(age.getValue());
}
}
public StringProperty nameProperty() {
return name;
}
public StringProperty familyNameProperty() {
return familyName;
}
public IntegerProperty ageProperty() {
return age;
}
}
With ModelWrapper:
public class PersonViewModel implements ViewModel {
private ModelWrapper wrapper = new ModelWrapper();
public void init(Person person) {
wrapper.set(person);
wrapper.reload();
}
public void reset() {
wrapper.reset();
}
public void reloadFromModel(){
wrapper.reload();
}
public void save() {
if (someValidation()) {
wrapper.commit();
}
}
public StringProperty nameProperty(){
return wrapper.field("name", Person::getName, Person::setName, "");
}
public StringProperty familyNameProperty(){
return wrapper.field("familyName", Person::getFamilyName, Person::setFamilyName, "");
}
public IntegerProperty ageProperty() {
return wrapper.field("age", Person::getAge, Person::setAge, 0);
}
}
In the first example without the ModelWrapper we have several lines of code that are specific for each field
of the model. If we would add a new field to the model (for example "email") then we would have to update several
pieces of code in the ViewModel.
On the other hand in the example with the ModelWrapper there is only the definition of the Property accessors
in the bottom of the class that is specific to the fields of the Model. For each field we have only one place in the
ViewModel that would need an update when the structure of the model changes.
| Type | Property and Description |
|---|---|
javafx.beans.property.ReadOnlyBooleanProperty |
different
This boolean flag indicates whether there is a difference of the data between the wrapped model object and the
properties provided by this wrapper.
|
javafx.beans.property.ReadOnlyBooleanProperty |
dirty
This boolean flag indicates whether there was a change to at least one wrapped property.
|
javafx.beans.property.ObjectProperty<M> |
model |
| Constructor and Description |
|---|
ModelWrapper()
Create a new instance of
ModelWrapper that is empty at the moment. |
ModelWrapper(M model)
Create a new instance of
ModelWrapper that wraps the given instance of the Model class. |
ModelWrapper(javafx.beans.property.ObjectProperty<M> model)
Create a new instance of
ModelWrapper that wraps the instance of the Model class wrapped by the property. |
| Modifier and Type | Method and Description |
|---|---|
void |
commit()
Take the current value of each property field and write it into the wrapped model element.
|
void |
copyValuesTo(M model)
This method can be used to copy all values of this
ModelWrapper instance
to the model instance provided as argument. |
javafx.beans.property.ReadOnlyBooleanProperty |
differentProperty()
This boolean flag indicates whether there is a difference of the data between the wrapped model object and the
properties provided by this wrapper.
|
javafx.beans.property.ReadOnlyBooleanProperty |
dirtyProperty()
This boolean flag indicates whether there was a change to at least one wrapped property.
|
javafx.beans.property.BooleanProperty |
field(BooleanGetter<M> getter,
BooleanSetter<M> setter) |
javafx.beans.property.BooleanProperty |
field(BooleanGetter<M> getter,
BooleanSetter<M> setter,
boolean defaultValue) |
javafx.beans.property.BooleanProperty |
field(BooleanPropertyAccessor<M> accessor) |
javafx.beans.property.BooleanProperty |
field(BooleanPropertyAccessor<M> accessor,
boolean defaultValue) |
javafx.beans.property.DoubleProperty |
field(DoubleGetter<M> getter,
DoubleSetter<M> setter) |
javafx.beans.property.DoubleProperty |
field(DoubleGetter<M> getter,
DoubleSetter<M> setter,
double defaultValue) |
javafx.beans.property.DoubleProperty |
field(DoublePropertyAccessor<M> accessor) |
javafx.beans.property.DoubleProperty |
field(DoublePropertyAccessor<M> accessor,
double defaultValue) |
javafx.beans.property.FloatProperty |
field(FloatGetter<M> getter,
FloatSetter<M> setter) |
javafx.beans.property.FloatProperty |
field(FloatGetter<M> getter,
FloatSetter<M> setter,
float defaultValue) |
javafx.beans.property.FloatProperty |
field(FloatPropertyAccessor<M> accessor) |
javafx.beans.property.FloatProperty |
field(FloatPropertyAccessor<M> accessor,
float defaultValue) |
javafx.beans.property.IntegerProperty |
field(IntGetter<M> getter,
IntSetter<M> setter) |
javafx.beans.property.IntegerProperty |
field(IntGetter<M> getter,
IntSetter<M> setter,
int defaultValue) |
javafx.beans.property.IntegerProperty |
field(IntPropertyAccessor<M> accessor) |
javafx.beans.property.IntegerProperty |
field(IntPropertyAccessor<M> accessor,
int defaultValue) |
<E> javafx.beans.property.ListProperty<E> |
field(ListGetter<M,E> getter,
ListSetter<M,E> setter) |
<E> javafx.beans.property.ListProperty<E> |
field(ListGetter<M,E> getter,
ListSetter<M,E> setter,
List<E> defaultValue) |
<E> javafx.beans.property.ListProperty<E> |
field(ListPropertyAccessor<M,E> accessor) |
<E> javafx.beans.property.ListProperty<E> |
field(ListPropertyAccessor<M,E> accessor,
List<E> defaultValue) |
javafx.beans.property.LongProperty |
field(LongGetter<M> getter,
LongSetter<M> setter) |
javafx.beans.property.LongProperty |
field(LongGetter<M> getter,
LongSetter<M> setter,
long defaultValue) |
javafx.beans.property.LongProperty |
field(LongPropertyAccessor<M> accessor) |
javafx.beans.property.LongProperty |
field(LongPropertyAccessor<M> accessor,
long defaultValue) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(MapGetter<M,K,V> getter,
MapSetter<M,K,V> setter) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(MapGetter<M,K,V> getter,
MapSetter<M,K,V> setter,
Map<K,V> defaultValue) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(MapPropertyAccessor<M,K,V> accessor) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(MapPropertyAccessor<M,K,V> accessor,
Map<K,V> defaultValue) |
<T> javafx.beans.property.ObjectProperty<T> |
field(ObjectGetter<M,T> getter,
ObjectSetter<M,T> setter) |
<T> javafx.beans.property.ObjectProperty<T> |
field(ObjectGetter<M,T> getter,
ObjectSetter<M,T> setter,
T defaultValue) |
<T> javafx.beans.property.ObjectProperty<T> |
field(ObjectPropertyAccessor<M,T> accessor) |
<T> javafx.beans.property.ObjectProperty<T> |
field(ObjectPropertyAccessor<M,T> accessor,
T defaultValue) |
<E> javafx.beans.property.SetProperty<E> |
field(SetGetter<M,E> getter,
SetSetter<M,E> setter) |
<E> javafx.beans.property.SetProperty<E> |
field(SetGetter<M,E> getter,
SetSetter<M,E> setter,
Set<E> defaultValue) |
<E> javafx.beans.property.SetProperty<E> |
field(SetPropertyAccessor<M,E> accessor) |
<E> javafx.beans.property.SetProperty<E> |
field(SetPropertyAccessor<M,E> accessor,
Set<E> defaultValue) |
javafx.beans.property.BooleanProperty |
field(String identifier,
BooleanGetter<M> getter,
BooleanSetter<M> setter) |
javafx.beans.property.BooleanProperty |
field(String identifier,
BooleanGetter<M> getter,
BooleanSetter<M> setter,
boolean defaultValue) |
javafx.beans.property.BooleanProperty |
field(String identifier,
BooleanPropertyAccessor<M> accessor) |
javafx.beans.property.BooleanProperty |
field(String identifier,
BooleanPropertyAccessor<M> accessor,
boolean defaultValue) |
javafx.beans.property.DoubleProperty |
field(String identifier,
DoubleGetter<M> getter,
DoubleSetter<M> setter) |
javafx.beans.property.DoubleProperty |
field(String identifier,
DoubleGetter<M> getter,
DoubleSetter<M> setter,
double defaultValue) |
javafx.beans.property.DoubleProperty |
field(String identifier,
DoublePropertyAccessor<M> accessor) |
javafx.beans.property.DoubleProperty |
field(String identifier,
DoublePropertyAccessor<M> accessor,
double defaultValue) |
javafx.beans.property.FloatProperty |
field(String identifier,
FloatGetter<M> getter,
FloatSetter<M> setter) |
javafx.beans.property.FloatProperty |
field(String identifier,
FloatGetter<M> getter,
FloatSetter<M> setter,
float defaultValue) |
javafx.beans.property.FloatProperty |
field(String identifier,
FloatPropertyAccessor<M> accessor) |
javafx.beans.property.FloatProperty |
field(String identifier,
FloatPropertyAccessor<M> accessor,
float defaultValue) |
javafx.beans.property.StringProperty |
field(StringGetter<M> getter,
StringSetter<M> setter)
Add a new field of type String to this instance of the wrapper.
|
javafx.beans.property.StringProperty |
field(StringGetter<M> getter,
StringSetter<M> setter,
String defaultValue)
Add a new field of type String to this instance of the wrapper.
|
javafx.beans.property.IntegerProperty |
field(String identifier,
IntGetter<M> getter,
IntSetter<M> setter) |
javafx.beans.property.IntegerProperty |
field(String identifier,
IntGetter<M> getter,
IntSetter<M> setter,
int defaultValue) |
javafx.beans.property.IntegerProperty |
field(String identifier,
IntPropertyAccessor<M> accessor) |
javafx.beans.property.IntegerProperty |
field(String identifier,
IntPropertyAccessor<M> accessor,
int defaultValue) |
<E> javafx.beans.property.ListProperty<E> |
field(String identifier,
ListGetter<M,E> getter,
ListSetter<M,E> setter) |
<E> javafx.beans.property.ListProperty<E> |
field(String identifier,
ListGetter<M,E> getter,
ListSetter<M,E> setter,
List<E> defaultValue) |
<E> javafx.beans.property.ListProperty<E> |
field(String identifier,
ListPropertyAccessor<M,E> accessor) |
<E> javafx.beans.property.ListProperty<E> |
field(String identifier,
ListPropertyAccessor<M,E> accessor,
List<E> defaultValue) |
javafx.beans.property.LongProperty |
field(String identifier,
LongGetter<M> getter,
LongSetter<M> setter) |
javafx.beans.property.LongProperty |
field(String identifier,
LongGetter<M> getter,
LongSetter<M> setter,
long defaultValue) |
javafx.beans.property.LongProperty |
field(String identifier,
LongPropertyAccessor<M> accessor) |
javafx.beans.property.LongProperty |
field(String identifier,
LongPropertyAccessor<M> accessor,
long defaultValue) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(String identifier,
MapGetter<M,K,V> getter,
MapSetter<M,K,V> setter) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(String identifier,
MapGetter<M,K,V> getter,
MapSetter<M,K,V> setter,
Map<K,V> defaultValue) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(String identifier,
MapPropertyAccessor<M,K,V> accessor) |
<K,V> javafx.beans.property.MapProperty<K,V> |
field(String identifier,
MapPropertyAccessor<M,K,V> accessor,
Map<K,V> defaultValue) |
<T> javafx.beans.property.ObjectProperty<T> |
field(String identifier,
ObjectGetter<M,T> getter,
ObjectSetter<M,T> setter) |
<T> javafx.beans.property.ObjectProperty<T> |
field(String identifier,
ObjectGetter<M,T> getter,
ObjectSetter<M,T> setter,
T defaultValue) |
<T> javafx.beans.property.ObjectProperty<T> |
field(String identifier,
ObjectPropertyAccessor<M,T> accessor) |
<T> javafx.beans.property.ObjectProperty<T> |
field(String identifier,
ObjectPropertyAccessor<M,T> accessor,
T defaultValue) |
javafx.beans.property.StringProperty |
field(StringPropertyAccessor<M> accessor)
Add a new field of type
String to this instance of the wrapper. |
javafx.beans.property.StringProperty |
field(StringPropertyAccessor<M> accessor,
String defaultValue)
Add a new field of type String to this instance of the wrapper.
|
<E> javafx.beans.property.SetProperty<E> |
field(String identifier,
SetGetter<M,E> getter,
SetSetter<M,E> setter) |
<E> javafx.beans.property.SetProperty<E> |
field(String identifier,
SetGetter<M,E> getter,
SetSetter<M,E> setter,
Set<E> defaultValue) |
<E> javafx.beans.property.SetProperty<E> |
field(String identifier,
SetPropertyAccessor<M,E> accessor) |
<E> javafx.beans.property.SetProperty<E> |
field(String identifier,
SetPropertyAccessor<M,E> accessor,
Set<E> defaultValue) |
javafx.beans.property.StringProperty |
field(String identifier,
StringGetter<M> getter,
StringSetter<M> setter)
Add a new field of type String to this instance of the wrapper.
|
javafx.beans.property.StringProperty |
field(String identifier,
StringGetter<M> getter,
StringSetter<M> setter,
String defaultValue) |
javafx.beans.property.StringProperty |
field(String identifier,
StringPropertyAccessor<M> accessor)
Add a new field of type String to this instance of the wrapper.
|
javafx.beans.property.StringProperty |
field(String identifier,
StringPropertyAccessor<M> accessor,
String defaultValue) |
M |
get() |
javafx.beans.property.BooleanProperty |
immutableField(BooleanGetter<M> getter,
BooleanImmutableSetter<M> immutableSetter) |
javafx.beans.property.BooleanProperty |
immutableField(BooleanGetter<M> getter,
BooleanImmutableSetter<M> immutableSetter,
boolean defaultValue) |
javafx.beans.property.DoubleProperty |
immutableField(DoubleGetter<M> getter,
DoubleImmutableSetter<M> immutableSetter) |
javafx.beans.property.DoubleProperty |
immutableField(DoubleGetter<M> getter,
DoubleImmutableSetter<M> immutableSetter,
double defaultValue) |
javafx.beans.property.FloatProperty |
immutableField(FloatGetter<M> getter,
FloatImmutableSetter<M> immutableSetter) |
javafx.beans.property.FloatProperty |
immutableField(FloatGetter<M> getter,
FloatImmutableSetter<M> immutableSetter,
float defaultValue) |
javafx.beans.property.IntegerProperty |
immutableField(IntGetter<M> getter,
IntImmutableSetter<M> immutableSetter) |
javafx.beans.property.IntegerProperty |
immutableField(IntGetter<M> getter,
IntImmutableSetter<M> immutableSetter,
int defaultValue) |
<E> javafx.beans.property.ListProperty<E> |
immutableField(ListGetter<M,E> getter,
ListImmutableSetter<M,E> immutableSetter) |
<E> javafx.beans.property.ListProperty<E> |
immutableField(ListGetter<M,E> getter,
ListImmutableSetter<M,E> immutableSetter,
List<E> defaultValue) |
javafx.beans.property.LongProperty |
immutableField(LongGetter<M> getter,
LongImmutableSetter<M> immutableSetter) |
javafx.beans.property.LongProperty |
immutableField(LongGetter<M> getter,
LongImmutableSetter<M> immutableSetter,
long defaultValue) |
<K,V> javafx.beans.property.MapProperty<K,V> |
immutableField(MapGetter<M,K,V> getter,
MapImmutableSetter<M,K,V> immutableSetter) |
<K,V> javafx.beans.property.MapProperty<K,V> |
immutableField(MapGetter<M,K,V> getter,
MapImmutableSetter<M,K,V> immutableSetter,
Map<K,V> defaultValue) |
<T> javafx.beans.property.ObjectProperty<T> |
immutableField(ObjectGetter<M,T> getter,
ObjectImmutableSetter<M,T> immutableSetter) |
<T> javafx.beans.property.ObjectProperty<T> |
immutableField(ObjectGetter<M,T> getter,
ObjectImmutableSetter<M,T> immutableSetter,
T defaultValue) |
<E> javafx.beans.property.SetProperty<E> |
immutableField(SetGetter<M,E> getter,
SetImmutableSetter<M,E> immutableSetter) |
<E> javafx.beans.property.SetProperty<E> |
immutableField(SetGetter<M,E> getter,
SetImmutableSetter<M,E> immutableSetter,
Set<E> defaultValue) |
javafx.beans.property.BooleanProperty |
immutableField(String identifier,
BooleanGetter<M> getter,
BooleanImmutableSetter<M> immutableSetter) |
javafx.beans.property.BooleanProperty |
immutableField(String identifier,
BooleanGetter<M> getter,
BooleanImmutableSetter<M> immutableSetter,
boolean defaultValue) |
javafx.beans.property.DoubleProperty |
immutableField(String identifier,
DoubleGetter<M> getter,
DoubleImmutableSetter<M> immutableSetter) |
javafx.beans.property.DoubleProperty |
immutableField(String identifier,
DoubleGetter<M> getter,
DoubleImmutableSetter<M> immutableSetter,
double defaultValue) |
javafx.beans.property.FloatProperty |
immutableField(String identifier,
FloatGetter<M> getter,
FloatImmutableSetter<M> immutableSetter) |
javafx.beans.property.FloatProperty |
immutableField(String identifier,
FloatGetter<M> getter,
FloatImmutableSetter<M> immutableSetter,
float defaultValue) |
javafx.beans.property.StringProperty |
immutableField(StringGetter<M> getter,
StringImmutableSetter<M> immutableSetter)
Add a new immutable field of type String to this instance of the wrapper.
|
javafx.beans.property.StringProperty |
immutableField(StringGetter<M> getter,
StringImmutableSetter<M> immutableSetter,
String defaultValue)
Ad a new immutable field of type String to this instance of the wrapper.
|
javafx.beans.property.IntegerProperty |
immutableField(String identifier,
IntGetter<M> getter,
IntImmutableSetter<M> immutableSetter) |
javafx.beans.property.IntegerProperty |
immutableField(String identifier,
IntGetter<M> getter,
IntImmutableSetter<M> immutableSetter,
int defaultValue) |
<E> javafx.beans.property.ListProperty<E> |
immutableField(String identifier,
ListGetter<M,E> getter,
ListImmutableSetter<M,E> immutableSetter) |
<E> javafx.beans.property.ListProperty<E> |
immutableField(String identifier,
ListGetter<M,E> getter,
ListImmutableSetter<M,E> immutableSetter,
List<E> defaultValue) |
javafx.beans.property.LongProperty |
immutableField(String identifier,
LongGetter<M> getter,
LongImmutableSetter<M> immutableSetter) |
javafx.beans.property.LongProperty |
immutableField(String identifier,
LongGetter<M> getter,
LongImmutableSetter<M> immutableSetter,
long defaultValue) |
<K,V> javafx.beans.property.MapProperty<K,V> |
immutableField(String identifier,
MapGetter<M,K,V> getter,
MapImmutableSetter<M,K,V> immutableSetter) |
<K,V> javafx.beans.property.MapProperty<K,V> |
immutableField(String identifier,
MapGetter<M,K,V> getter,
MapImmutableSetter<M,K,V> immutableSetter,
Map<K,V> defaultValue) |
<T> javafx.beans.property.ObjectProperty<T> |
immutableField(String identifier,
ObjectGetter<M,T> getter,
ObjectImmutableSetter<M,T> immutableSetter) |
<T> javafx.beans.property.ObjectProperty<T> |
immutableField(String identifier,
ObjectGetter<M,T> getter,
ObjectImmutableSetter<M,T> immutableSetter,
T defaultValue) |
<E> javafx.beans.property.SetProperty<E> |
immutableField(String identifier,
SetGetter<M,E> getter,
SetImmutableSetter<M,E> immutableSetter) |
<E> javafx.beans.property.SetProperty<E> |
immutableField(String identifier,
SetGetter<M,E> getter,
SetImmutableSetter<M,E> immutableSetter,
Set<E> defaultValue) |
javafx.beans.property.StringProperty |
immutableField(String identifier,
StringGetter<M> getter,
StringImmutableSetter<M> immutableSetter)
Add a new immutable field of type String to this instance of the wrapper.
|
javafx.beans.property.StringProperty |
immutableField(String identifier,
StringGetter<M> getter,
StringImmutableSetter<M> immutableSetter,
String defaultValue) |
boolean |
isDifferent()
See
differentProperty(). |
boolean |
isDirty()
See
dirtyProperty(). |
javafx.beans.property.ObjectProperty<M> |
modelProperty() |
void |
reload()
Take the current values from the wrapped model element and put them in the corresponding property fields.
|
void |
reset()
Resets all defined fields to their default values.
|
void |
set(M model)
Define the model element that will be wrapped by this
ModelWrapper instance. |
void |
useCurrentValuesAsDefaults()
Use all values that are currently present in the wrapped model object as new default values for respective fields.
|
public javafx.beans.property.ObjectProperty<M> modelProperty
public javafx.beans.property.ReadOnlyBooleanProperty differentProperty
Note the difference to dirtyProperty(): This property will be true if the data of the
wrapped model is different to the properties of this wrapper. If you change the data back to the initial state so
that the data is equal again, this property will change back to false while the
dirtyProperty() will still be true.
Simply speaking: This property indicates whether there is a difference in data between the model and the wrapper.
The dirtyProperty() indicates whether there was a change done.
Note: Only those changes are observed that are done through the wrapped property fields of this wrapper. If you
change the data of the model instance directly, this property won't turn to true.
isDifferent()public javafx.beans.property.ReadOnlyBooleanProperty dirtyProperty
Note the difference to differentProperty(): This property will turn to true when the value
of one of the wrapped properties is changed. It will only change back to false when either the
commit() or reload() method is called. This property will stay true even if
afterwards another change is done so that the data is equal again. In this case the differentProperty()
will switch back to false.
differentProperty() indicates whether there is a difference in data at the moment.isDirty()public ModelWrapper(javafx.beans.property.ObjectProperty<M> model)
ModelWrapper that wraps the instance of the Model class wrapped by the property.
Updates all data when the model instance changes.model - the property of the model element that will be wrapped.public ModelWrapper(M model)
ModelWrapper that wraps the given instance of the Model class.model - the element of the model that will be wrapped.public ModelWrapper()
ModelWrapper that is empty at the moment. You have to define the model element
that should be wrapped afterwards with the set(Object) method.public void set(M model)
ModelWrapper instance.model - the element of the model that will be wrapped.public M get()
null.public javafx.beans.property.ObjectProperty<M> modelProperty()
public void reset()
Default values can be defined as last argument of the overloaded "field" methods
(see field(StringGetter, StringSetter, String))
or by using the useCurrentValuesAsDefaults() method.
If no special default value was defined for a field the default value of the actual Property type will be used
(e.g. 0 for IntegerProperty, null for StringProperty and ObjectProperty ...).
Note: This method has no effects on the wrapped model element but will only change the values of the defined property fields.
public void useCurrentValuesAsDefaults()
Subsequent calls to reset() will reset the values to this new default values.
Usage example:
ModelWrapper wrapper = new ModelWrapper();
wrapper.field(Person::getName, Person::setName, "oldDefault");
Person p = new Person();
wrapper.set(p);
p.setName("Luise");
wrapper.useCurrentValuesAsDefaults(); // now "Luise" is the default value for the name field.
name.set("Hugo");
wrapper.commit();
name.get(); // Hugo
p.getName(); // Hugo
wrapper.reset(); // reset to the new defaults
name.get(); // Luise
wrapper.commit(); // put values from properties to the wrapped model object
p.getName(); // Luise
If no model instance is set to be wrapped by the ModelWrapper, nothing will happen when this method is invoked.
Instead the old default values will still be available.public void commit()
If no model element is defined then nothing will happen.
Note: This method has no effects on the values of the defined property fields but will only change the state of the wrapped model element.
public void reload()
If no model element is defined then nothing will happen.
Note: This method has no effects on the wrapped model element but will only change the values of the defined property fields.
public void copyValuesTo(M model)
ModelWrapper instance
to the model instance provided as argument.
Existing values in the provided model instance will be overwritten.
This method doesn't change the state of this modelWrapper or the wrapped model instance.
model - a non-null instance of a model.public javafx.beans.property.ReadOnlyBooleanProperty differentProperty()
Note the difference to dirtyProperty(): This property will be true if the data of the
wrapped model is different to the properties of this wrapper. If you change the data back to the initial state so
that the data is equal again, this property will change back to false while the
dirtyProperty() will still be true.
Simply speaking: This property indicates whether there is a difference in data between the model and the wrapper.
The dirtyProperty() indicates whether there was a change done.
Note: Only those changes are observed that are done through the wrapped property fields of this wrapper. If you
change the data of the model instance directly, this property won't turn to true.
isDifferent()public boolean isDifferent()
differentProperty().public javafx.beans.property.ReadOnlyBooleanProperty dirtyProperty()
Note the difference to differentProperty(): This property will turn to true when the value
of one of the wrapped properties is changed. It will only change back to false when either the
commit() or reload() method is called. This property will stay true even if
afterwards another change is done so that the data is equal again. In this case the differentProperty()
will switch back to false.
differentProperty() indicates whether there is a difference in data at the moment.isDirty()public boolean isDirty()
dirtyProperty().public javafx.beans.property.StringProperty field(StringGetter<M> getter, StringSetter<M> setter)
Example:
ModelWrapper personWrapper = new ModelWrapper();
StringProperty wrappedNameProperty = personWrapper.field(person -> person.getName(), (person, value)
-> person.setName(value));
// or with a method reference
StringProperty wrappedNameProperty = personWrapper.field(Person::getName, Person::setName);
getter - a function that returns the current value of the field for a given model element. Typically you will
use a method reference to the getter method of the model element.setter - a function that sets the given value to the given model element. Typically you will use a method
reference to the setter method of the model element.public javafx.beans.property.StringProperty immutableField(StringGetter<M> getter, StringImmutableSetter<M> immutableSetter)
Example:
ModelWrapper personWrapper = new ModelWrapper();
StringProperty wrappedNameProperty = personWrapper.field(person -> person.getName(), (person, value)
-> {
Person clone = person.withName(value);
return clone;
});
// or with a method reference
StringProperty wrappedNameProperty = personWrapper.field(Person::getName, Person::withName);
getter - a function that returns the current value of the field for a given model element. Typically you will
use a method reference to the getter method of the model element.immutableSetter - a function that returns a clone of this the given model element that has the given value set. Typically you will use a method
reference to the immutable setter method of the model element.public javafx.beans.property.StringProperty field(StringGetter<M> getter, StringSetter<M> setter, String defaultValue)
field(StringGetter, StringSetter).
This method additionally has a parameter to define the default value that is used when the reset()
method is used.getter - a function that returns the current value of the field for a given model element. Typically you will
use a method reference to the getter method of the model element.setter - a function that sets the given value to the given model element. Typically you will use a method
reference to the setter method of the model element.defaultValue - the default value that is used when reset() is invoked.public javafx.beans.property.StringProperty immutableField(StringGetter<M> getter, StringImmutableSetter<M> immutableSetter, String defaultValue)
immutableField(StringGetter, StringImmutableSetter).
This method additionally has a parameter to define the default value that is used when the reset()
method is used.getter - a function that returns the current value of the field for a given model element. Typically you will
use a method reference to the getter method of the model element.immutableSetter - a function that returns a clone of this the given model element that has the given value set. Typically you will use a method
reference to the immutable setter method of the model element.defaultValue - the default value that is used when reset() is invoked.public javafx.beans.property.StringProperty field(StringPropertyAccessor<M> accessor)
String to this instance of the wrapper. This method is used for model elements
that are following the enhanced JavaFX-Beans-standard i.e. the model fields are available as JavaFX Properties.
Example:
ModelWrapper personWrapper = new ModelWrapper(); StringProperty wrappedNameProperty = personWrapper.field(person -> person.nameProperty()); // or with a method reference StringProperty wrappedNameProperty = personWrapper.field(Person::nameProperty);
accessor - a function that returns the property for a given model instance. Typically you will use a method
reference to the javafx-property accessor method.public javafx.beans.property.StringProperty field(StringPropertyAccessor<M> accessor, String defaultValue)
field(StringGetter, StringSetter).
This method additionally has a parameter to define the default value that is used when the reset()
method is used.accessor - a function that returns the property for a given model instance. Typically you will use a method
reference to the javafx-property accessor method.defaultValue - the default value that is used when reset() is invoked.public javafx.beans.property.StringProperty field(String identifier, StringGetter<M> getter, StringSetter<M> setter)
field(StringGetter, StringSetter).
This method additionally takes a string identifier as first parameter.
This identifier is used to return the same property instance even when the method is invoked multiple times.identifier - an identifier for the field.getter - a function that returns the current value of the field for a given model element. Typically you will
use a method reference to the getter method of the model element.setter - a function that sets the given value to the given model element. Typically you will use a method
reference to the setter method of the model element.public javafx.beans.property.StringProperty field(String identifier, StringGetter<M> getter, StringSetter<M> setter, String defaultValue)
public javafx.beans.property.StringProperty immutableField(String identifier, StringGetter<M> getter, StringImmutableSetter<M> immutableSetter)
#immutableField(StringGetter, StringImmutableSetter).
This method additionally takes a string identifier as first parameter.
This identifier is used to return the same property instance even when the method is invoked multiple times.identifier - an identifier for the field.getter - a function that returns the current value of the field for a given model element. Typically you will
use a method reference to the getter method of the model element.immutableSetter - a function that returns a clone of this the given model element that has the given value set. Typically you will use a method
reference to the immutable setter method of the model element.public javafx.beans.property.StringProperty immutableField(String identifier, StringGetter<M> getter, StringImmutableSetter<M> immutableSetter, String defaultValue)
public javafx.beans.property.StringProperty field(String identifier, StringPropertyAccessor<M> accessor)
field(StringPropertyAccessor). This
method additionally takes a string identifier as first parameter.
This identifier is used to return the same property instance even when the method is invoked multiple times.identifier - an identifier for the field.accessor - a function that returns the property for a given model instance. Typically you will use a method
reference to the javafx-property accessor method.public javafx.beans.property.StringProperty field(String identifier, StringPropertyAccessor<M> accessor, String defaultValue)
public javafx.beans.property.BooleanProperty field(BooleanGetter<M> getter, BooleanSetter<M> setter)
public javafx.beans.property.BooleanProperty immutableField(BooleanGetter<M> getter, BooleanImmutableSetter<M> immutableSetter)
public javafx.beans.property.BooleanProperty field(BooleanGetter<M> getter, BooleanSetter<M> setter, boolean defaultValue)
public javafx.beans.property.BooleanProperty immutableField(BooleanGetter<M> getter, BooleanImmutableSetter<M> immutableSetter, boolean defaultValue)
public javafx.beans.property.BooleanProperty field(BooleanPropertyAccessor<M> accessor)
public javafx.beans.property.BooleanProperty field(BooleanPropertyAccessor<M> accessor, boolean defaultValue)
public javafx.beans.property.BooleanProperty field(String identifier, BooleanGetter<M> getter, BooleanSetter<M> setter)
public javafx.beans.property.BooleanProperty field(String identifier, BooleanGetter<M> getter, BooleanSetter<M> setter, boolean defaultValue)
public javafx.beans.property.BooleanProperty immutableField(String identifier, BooleanGetter<M> getter, BooleanImmutableSetter<M> immutableSetter)
public javafx.beans.property.BooleanProperty immutableField(String identifier, BooleanGetter<M> getter, BooleanImmutableSetter<M> immutableSetter, boolean defaultValue)
public javafx.beans.property.BooleanProperty field(String identifier, BooleanPropertyAccessor<M> accessor)
public javafx.beans.property.BooleanProperty field(String identifier, BooleanPropertyAccessor<M> accessor, boolean defaultValue)
public javafx.beans.property.DoubleProperty field(DoubleGetter<M> getter, DoubleSetter<M> setter)
public javafx.beans.property.DoubleProperty immutableField(DoubleGetter<M> getter, DoubleImmutableSetter<M> immutableSetter)
public javafx.beans.property.DoubleProperty field(DoubleGetter<M> getter, DoubleSetter<M> setter, double defaultValue)
public javafx.beans.property.DoubleProperty immutableField(DoubleGetter<M> getter, DoubleImmutableSetter<M> immutableSetter, double defaultValue)
public javafx.beans.property.DoubleProperty field(DoublePropertyAccessor<M> accessor)
public javafx.beans.property.DoubleProperty field(DoublePropertyAccessor<M> accessor, double defaultValue)
public javafx.beans.property.DoubleProperty field(String identifier, DoubleGetter<M> getter, DoubleSetter<M> setter)
public javafx.beans.property.DoubleProperty field(String identifier, DoubleGetter<M> getter, DoubleSetter<M> setter, double defaultValue)
public javafx.beans.property.DoubleProperty immutableField(String identifier, DoubleGetter<M> getter, DoubleImmutableSetter<M> immutableSetter)
public javafx.beans.property.DoubleProperty immutableField(String identifier, DoubleGetter<M> getter, DoubleImmutableSetter<M> immutableSetter, double defaultValue)
public javafx.beans.property.DoubleProperty field(String identifier, DoublePropertyAccessor<M> accessor)
public javafx.beans.property.DoubleProperty field(String identifier, DoublePropertyAccessor<M> accessor, double defaultValue)
public javafx.beans.property.FloatProperty field(FloatGetter<M> getter, FloatSetter<M> setter)
public javafx.beans.property.FloatProperty immutableField(FloatGetter<M> getter, FloatImmutableSetter<M> immutableSetter)
public javafx.beans.property.FloatProperty field(FloatGetter<M> getter, FloatSetter<M> setter, float defaultValue)
public javafx.beans.property.FloatProperty immutableField(FloatGetter<M> getter, FloatImmutableSetter<M> immutableSetter, float defaultValue)
public javafx.beans.property.FloatProperty field(FloatPropertyAccessor<M> accessor)
public javafx.beans.property.FloatProperty field(FloatPropertyAccessor<M> accessor, float defaultValue)
public javafx.beans.property.FloatProperty field(String identifier, FloatGetter<M> getter, FloatSetter<M> setter)
public javafx.beans.property.FloatProperty field(String identifier, FloatGetter<M> getter, FloatSetter<M> setter, float defaultValue)
public javafx.beans.property.FloatProperty immutableField(String identifier, FloatGetter<M> getter, FloatImmutableSetter<M> immutableSetter)
public javafx.beans.property.FloatProperty immutableField(String identifier, FloatGetter<M> getter, FloatImmutableSetter<M> immutableSetter, float defaultValue)
public javafx.beans.property.FloatProperty field(String identifier, FloatPropertyAccessor<M> accessor)
public javafx.beans.property.FloatProperty field(String identifier, FloatPropertyAccessor<M> accessor, float defaultValue)
public javafx.beans.property.IntegerProperty immutableField(IntGetter<M> getter, IntImmutableSetter<M> immutableSetter)
public javafx.beans.property.IntegerProperty field(IntGetter<M> getter, IntSetter<M> setter, int defaultValue)
public javafx.beans.property.IntegerProperty immutableField(IntGetter<M> getter, IntImmutableSetter<M> immutableSetter, int defaultValue)
public javafx.beans.property.IntegerProperty field(IntPropertyAccessor<M> accessor)
public javafx.beans.property.IntegerProperty field(IntPropertyAccessor<M> accessor, int defaultValue)
public javafx.beans.property.IntegerProperty field(String identifier, IntGetter<M> getter, IntSetter<M> setter)
public javafx.beans.property.IntegerProperty field(String identifier, IntGetter<M> getter, IntSetter<M> setter, int defaultValue)
public javafx.beans.property.IntegerProperty immutableField(String identifier, IntGetter<M> getter, IntImmutableSetter<M> immutableSetter)
public javafx.beans.property.IntegerProperty immutableField(String identifier, IntGetter<M> getter, IntImmutableSetter<M> immutableSetter, int defaultValue)
public javafx.beans.property.IntegerProperty field(String identifier, IntPropertyAccessor<M> accessor)
public javafx.beans.property.IntegerProperty field(String identifier, IntPropertyAccessor<M> accessor, int defaultValue)
public javafx.beans.property.LongProperty field(LongGetter<M> getter, LongSetter<M> setter)
public javafx.beans.property.LongProperty immutableField(LongGetter<M> getter, LongImmutableSetter<M> immutableSetter)
public javafx.beans.property.LongProperty field(LongGetter<M> getter, LongSetter<M> setter, long defaultValue)
public javafx.beans.property.LongProperty immutableField(LongGetter<M> getter, LongImmutableSetter<M> immutableSetter, long defaultValue)
public javafx.beans.property.LongProperty field(LongPropertyAccessor<M> accessor)
public javafx.beans.property.LongProperty field(LongPropertyAccessor<M> accessor, long defaultValue)
public javafx.beans.property.LongProperty field(String identifier, LongGetter<M> getter, LongSetter<M> setter)
public javafx.beans.property.LongProperty field(String identifier, LongGetter<M> getter, LongSetter<M> setter, long defaultValue)
public javafx.beans.property.LongProperty immutableField(String identifier, LongGetter<M> getter, LongImmutableSetter<M> immutableSetter)
public javafx.beans.property.LongProperty immutableField(String identifier, LongGetter<M> getter, LongImmutableSetter<M> immutableSetter, long defaultValue)
public javafx.beans.property.LongProperty field(String identifier, LongPropertyAccessor<M> accessor)
public javafx.beans.property.LongProperty field(String identifier, LongPropertyAccessor<M> accessor, long defaultValue)
public <T> javafx.beans.property.ObjectProperty<T> field(ObjectGetter<M,T> getter, ObjectSetter<M,T> setter)
public <T> javafx.beans.property.ObjectProperty<T> immutableField(ObjectGetter<M,T> getter, ObjectImmutableSetter<M,T> immutableSetter)
public <T> javafx.beans.property.ObjectProperty<T> field(ObjectGetter<M,T> getter, ObjectSetter<M,T> setter, T defaultValue)
public <T> javafx.beans.property.ObjectProperty<T> immutableField(ObjectGetter<M,T> getter, ObjectImmutableSetter<M,T> immutableSetter, T defaultValue)
public <T> javafx.beans.property.ObjectProperty<T> field(ObjectPropertyAccessor<M,T> accessor)
public <T> javafx.beans.property.ObjectProperty<T> field(ObjectPropertyAccessor<M,T> accessor, T defaultValue)
public <T> javafx.beans.property.ObjectProperty<T> field(String identifier, ObjectGetter<M,T> getter, ObjectSetter<M,T> setter)
public <T> javafx.beans.property.ObjectProperty<T> field(String identifier, ObjectGetter<M,T> getter, ObjectSetter<M,T> setter, T defaultValue)
public <T> javafx.beans.property.ObjectProperty<T> immutableField(String identifier, ObjectGetter<M,T> getter, ObjectImmutableSetter<M,T> immutableSetter)
public <T> javafx.beans.property.ObjectProperty<T> immutableField(String identifier, ObjectGetter<M,T> getter, ObjectImmutableSetter<M,T> immutableSetter, T defaultValue)
public <T> javafx.beans.property.ObjectProperty<T> field(String identifier, ObjectPropertyAccessor<M,T> accessor)
public <T> javafx.beans.property.ObjectProperty<T> field(String identifier, ObjectPropertyAccessor<M,T> accessor, T defaultValue)
public <E> javafx.beans.property.ListProperty<E> field(ListGetter<M,E> getter, ListSetter<M,E> setter)
public <E> javafx.beans.property.ListProperty<E> immutableField(ListGetter<M,E> getter, ListImmutableSetter<M,E> immutableSetter)
public <E> javafx.beans.property.ListProperty<E> field(ListGetter<M,E> getter, ListSetter<M,E> setter, List<E> defaultValue)
public <E> javafx.beans.property.ListProperty<E> immutableField(ListGetter<M,E> getter, ListImmutableSetter<M,E> immutableSetter, List<E> defaultValue)
public <E> javafx.beans.property.ListProperty<E> field(ListPropertyAccessor<M,E> accessor)
public <E> javafx.beans.property.ListProperty<E> field(ListPropertyAccessor<M,E> accessor, List<E> defaultValue)
public <E> javafx.beans.property.ListProperty<E> field(String identifier, ListGetter<M,E> getter, ListSetter<M,E> setter)
public <E> javafx.beans.property.ListProperty<E> field(String identifier, ListGetter<M,E> getter, ListSetter<M,E> setter, List<E> defaultValue)
public <E> javafx.beans.property.ListProperty<E> immutableField(String identifier, ListGetter<M,E> getter, ListImmutableSetter<M,E> immutableSetter)
public <E> javafx.beans.property.ListProperty<E> immutableField(String identifier, ListGetter<M,E> getter, ListImmutableSetter<M,E> immutableSetter, List<E> defaultValue)
public <E> javafx.beans.property.ListProperty<E> field(String identifier, ListPropertyAccessor<M,E> accessor)
public <E> javafx.beans.property.ListProperty<E> field(String identifier, ListPropertyAccessor<M,E> accessor, List<E> defaultValue)
public <E> javafx.beans.property.SetProperty<E> field(SetGetter<M,E> getter, SetSetter<M,E> setter)
public <E> javafx.beans.property.SetProperty<E> immutableField(SetGetter<M,E> getter, SetImmutableSetter<M,E> immutableSetter)
public <E> javafx.beans.property.SetProperty<E> field(SetGetter<M,E> getter, SetSetter<M,E> setter, Set<E> defaultValue)
public <E> javafx.beans.property.SetProperty<E> immutableField(SetGetter<M,E> getter, SetImmutableSetter<M,E> immutableSetter, Set<E> defaultValue)
public <E> javafx.beans.property.SetProperty<E> field(SetPropertyAccessor<M,E> accessor)
public <E> javafx.beans.property.SetProperty<E> field(SetPropertyAccessor<M,E> accessor, Set<E> defaultValue)
public <E> javafx.beans.property.SetProperty<E> field(String identifier, SetGetter<M,E> getter, SetSetter<M,E> setter)
public <E> javafx.beans.property.SetProperty<E> field(String identifier, SetGetter<M,E> getter, SetSetter<M,E> setter, Set<E> defaultValue)
public <E> javafx.beans.property.SetProperty<E> immutableField(String identifier, SetGetter<M,E> getter, SetImmutableSetter<M,E> immutableSetter)
public <E> javafx.beans.property.SetProperty<E> immutableField(String identifier, SetGetter<M,E> getter, SetImmutableSetter<M,E> immutableSetter, Set<E> defaultValue)
public <E> javafx.beans.property.SetProperty<E> field(String identifier, SetPropertyAccessor<M,E> accessor)
public <E> javafx.beans.property.SetProperty<E> field(String identifier, SetPropertyAccessor<M,E> accessor, Set<E> defaultValue)
public <K,V> javafx.beans.property.MapProperty<K,V> field(MapGetter<M,K,V> getter, MapSetter<M,K,V> setter)
public <K,V> javafx.beans.property.MapProperty<K,V> immutableField(MapGetter<M,K,V> getter, MapImmutableSetter<M,K,V> immutableSetter)
public <K,V> javafx.beans.property.MapProperty<K,V> field(MapGetter<M,K,V> getter, MapSetter<M,K,V> setter, Map<K,V> defaultValue)
public <K,V> javafx.beans.property.MapProperty<K,V> immutableField(MapGetter<M,K,V> getter, MapImmutableSetter<M,K,V> immutableSetter, Map<K,V> defaultValue)
public <K,V> javafx.beans.property.MapProperty<K,V> field(MapPropertyAccessor<M,K,V> accessor)
public <K,V> javafx.beans.property.MapProperty<K,V> field(MapPropertyAccessor<M,K,V> accessor, Map<K,V> defaultValue)
public <K,V> javafx.beans.property.MapProperty<K,V> field(String identifier, MapGetter<M,K,V> getter, MapSetter<M,K,V> setter)
public <K,V> javafx.beans.property.MapProperty<K,V> field(String identifier, MapGetter<M,K,V> getter, MapSetter<M,K,V> setter, Map<K,V> defaultValue)
public <K,V> javafx.beans.property.MapProperty<K,V> immutableField(String identifier, MapGetter<M,K,V> getter, MapImmutableSetter<M,K,V> immutableSetter)
public <K,V> javafx.beans.property.MapProperty<K,V> immutableField(String identifier, MapGetter<M,K,V> getter, MapImmutableSetter<M,K,V> immutableSetter, Map<K,V> defaultValue)
public <K,V> javafx.beans.property.MapProperty<K,V> field(String identifier, MapPropertyAccessor<M,K,V> accessor)
public <K,V> javafx.beans.property.MapProperty<K,V> field(String identifier, MapPropertyAccessor<M,K,V> accessor, Map<K,V> defaultValue)
Copyright © 2019 Saxonia Systems AG. All rights reserved.