Observetree is a library that extends the classic Observable pattern by integrating it in a tree structure. Change events are propagated through the Observable tree. Listeners can be assigned a priority to control the order at which they are called.
Javadoc also available at sgora.dev/observetree/
For detailed technical description see Observable class
<dependency>
<groupId>dev.sgora</groupId>
<artifactId>observetree</artifactId>
<version>${observetree-version}</version>
</dependency>
(Earlier versions released under io.github.stasgora
groupId)
SNAPSHOT versions are not synchronized to Central. If you want to use a snapshot version you need to add the https://oss.sonatype.org/content/repositories/snapshots/ repository to your pom.xml.
See the release page for jar downloads. Follow your IDE instructions for detailed steps on adding external library dependencies to your project.
- Create model class extending
Observable
:
class Point extends Observable {
public int x, y;
}
- Internally call
onValueChanged()
:
private void set(int x, int y) {
this.x = x;
this.y = y;
onValueChanged();
}
- Create instance and register a listener:
Point p = new Point();
p.addListener(() -> {...});
- Change model object and allow it to call it's listeners:
p.set(1, 1);
p.notifyListeners();
- Build model structure:
class Model extends Observable {
public Point point;
...
public Model() {
addSubObservable(point);
...
}
}
- You can subscribe to changes of objects on different levels:
model.addListener(...); // Changes to model and all it's sub-observables
model.point.addListener(...);
- After changes to potentially many objects notify listeners of all changed objects in tree:
model.notifyListeners();
(For detailed behaviour description see the documentation)
p.addListener(() -> {...}, ListenerPriority.HIGH);
- Declare:
SettableProperty<Integer> size = new SettableProperty<>(1);
- Get notified when the value is set:
size.set(2);
- Declare:
SettableObservable<Point> point = new SettableObservable<>(new Point());
- Register static listener:
point.addStaticListener(() -> {...});
- After replacing the object the listeners will remain attached:
point.set(new Point());
Stanisław Góra