From dbd7a02d71a0a4ee5e2ee064cd494d7fa9de4795 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:55:19 +0300 Subject: Add observable views --- .../cotton/gui/widget/data/ObservableProperty.java | 48 ++++++++++++++-------- .../cotton/gui/widget/data/ObservableView.java | 38 +++++++++++++++++ 2 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java (limited to 'src/main/java') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java index 7111b25..39a078b 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java @@ -1,7 +1,5 @@ package io.github.cottonmc.cotton.gui.widget.data; -import org.jetbrains.annotations.Nullable; - import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -18,7 +16,7 @@ import java.util.function.Supplier; * @param the contained value type * @since 4.2.0 */ -public final class ObservableProperty implements Supplier { +public final class ObservableProperty implements ObservableView { private Supplier value; private final List> listeners = new ArrayList<>(); private boolean allowNull = true; @@ -112,6 +110,33 @@ public final class ObservableProperty implements Supplier { return this; } + /** + * Returns a read-only view of this property. + * The result is not an instance of {@link ObservableProperty}, + * and thus can't be mutated. + * + * @return an observable view of this property + */ + public ObservableView readOnly() { + // Missing delegates from Kotlin... :( + return new ObservableView<>() { + @Override + public T get() { + return ObservableProperty.this.get(); + } + + @Override + public void addListener(ChangeListener listener) { + ObservableProperty.this.addListener(listener); + } + + @Override + public void removeListener(ChangeListener listener) { + ObservableProperty.this.removeListener(listener); + } + }; + } + /** * {@return the name of this property} */ @@ -130,28 +155,15 @@ public final class ObservableProperty implements Supplier { return this; } - /** - * Adds a change listener to this observable property. - * - * @param listener the added listener - */ + @Override public void addListener(ChangeListener listener) { Objects.requireNonNull(listener); listeners.add(listener); } - /** - * Removes a change listener from this observable property if present. - * - * @param listener the removed listener - */ + @Override public void removeListener(ChangeListener listener) { Objects.requireNonNull(listener); listeners.remove(listener); } - - @FunctionalInterface - public interface ChangeListener { - void onPropertyChange(ObservableProperty property, @Nullable T from, @Nullable T to); - } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java new file mode 100644 index 0000000..c3113b9 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java @@ -0,0 +1,38 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +import org.jetbrains.annotations.Nullable; + +import java.util.function.Supplier; + +public interface ObservableView extends Supplier { + /** + * Adds a change listener to this property view. + * + * @param listener the added listener + */ + void addListener(ChangeListener listener); + + /** + * Removes a change listener from this property view if present. + * + * @param listener the removed listener + */ + void removeListener(ChangeListener listener); + + /** + * A listener for changes in observable views and properties. + * + * @param the value type listened to + */ + @FunctionalInterface + interface ChangeListener { + /** + * Handles a change in an observable property. + * + * @param property + * @param from + * @param to + */ + void onPropertyChange(ObservableView property, @Nullable T from, @Nullable T to); + } +} -- cgit