public class DirtyStateTracker
extends javafx.beans.property.ReadOnlyBooleanWrapper
Track dirty states for a collection of properties, with undo feature to rollback changes.
Create an instance of DirtyStateTracker with a reference to the bean that holds the properties you want to track changes in. All puplic properties are automatically tracked, unless you supply an explicit list of properties to track.
// Track all properties in customer DirtyStateTracker dirtyState = new DirtyStateTracker(customer); // Track only username and password DirtyStateTracker dirtyState = new DirtyStateTracker(customer, customer.usernameProperty(), customer.passwordProperty());
The list of currently dirty properties can be obtained via DirtyStateTracker#getUnmodifiableDirtyProperties()
.
The DirtyStateTracker
itself is a ReadOnlyProperty
you can use to track if any of the properties are dirty. A good
use case for this is for example conditionally enabling of a save button:
// Disable save button until anything is changed saveButton.disableProperty().bind(dirtyState.not())
To roll back any changes, call DirtyStateTracker#reset()
.
To reset dirty state and clear the rollback buffer, call DirtyStateTracker#undo()
.
// Undo changes undoButton.setOnAction(event -> dirtyState.reset()); // Show undo button when changes are performed undoButton.visibleProperty().bind(dirtyState);
It's possible to change the tracked properties after the tracker is created. Let's say you want to listen to all properties but the id property of a customer object:
// Track all but the id property DirtyStateTracker dirtyState = new DirtyStateTracker(customer); dirtyState.getProperties().remove(customer.idProperty());
Constructor and Description |
---|
DirtyStateTracker(Object bean) |
DirtyStateTracker(Object bean,
boolean addDeclaredProperties) |
DirtyStateTracker(Object bean,
List<javafx.beans.property.Property> properties) |
Modifier and Type | Method and Description |
---|---|
javafx.collections.ObservableList<javafx.beans.property.Property> |
getProperties() |
javafx.collections.ObservableList<javafx.beans.property.Property> |
getUnmodifiableDirtyProperties() |
Boolean |
isDirty() |
void |
reset() |
void |
undo() |
addListener, addListener, fireValueChangedEvent, getReadOnlyProperty, removeListener, removeListener
bind, get, invalidated, isBound, set, toString, unbind
bindBidirectional, booleanProperty, setValue, unbindBidirectional
and, asString, booleanExpression, booleanExpression, getValue, isEqualTo, isNotEqualTo, not, or
public DirtyStateTracker(Object bean)
public DirtyStateTracker(Object bean, boolean addDeclaredProperties)
public void reset()
public void undo()
public Boolean isDirty()
public javafx.collections.ObservableList<javafx.beans.property.Property> getProperties()
public javafx.collections.ObservableList<javafx.beans.property.Property> getUnmodifiableDirtyProperties()
Copyright © 2016 SYSE. All rights reserved.