diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-09-10 15:55:19 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-09-10 15:55:19 +0300 |
commit | dbd7a02d71a0a4ee5e2ee064cd494d7fa9de4795 (patch) | |
tree | 95cc9776438e72880091c00dd141d4a90c693025 | |
parent | 7da9984fc611a07e948d0d80d61471d12646488c (diff) | |
download | LibGui-dbd7a02d71a0a4ee5e2ee064cd494d7fa9de4795.tar.gz LibGui-dbd7a02d71a0a4ee5e2ee064cd494d7fa9de4795.tar.bz2 LibGui-dbd7a02d71a0a4ee5e2ee064cd494d7fa9de4795.zip |
Add observable views
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java | 48 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableView.java | 38 |
2 files changed, 68 insertions, 18 deletions
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 <T> the contained value type * @since 4.2.0 */ -public final class ObservableProperty<T> implements Supplier<T> { +public final class ObservableProperty<T> implements ObservableView<T> { private Supplier<? extends T> value; private final List<ChangeListener<? super T>> listeners = new ArrayList<>(); private boolean allowNull = true; @@ -113,6 +111,33 @@ public final class ObservableProperty<T> implements Supplier<T> { } /** + * 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<T> readOnly() { + // Missing delegates from Kotlin... :( + return new ObservableView<>() { + @Override + public T get() { + return ObservableProperty.this.get(); + } + + @Override + public void addListener(ChangeListener<? super T> listener) { + ObservableProperty.this.addListener(listener); + } + + @Override + public void removeListener(ChangeListener<? super T> listener) { + ObservableProperty.this.removeListener(listener); + } + }; + } + + /** * {@return the name of this property} */ public String getName() { @@ -130,28 +155,15 @@ public final class ObservableProperty<T> implements Supplier<T> { return this; } - /** - * Adds a change listener to this observable property. - * - * @param listener the added listener - */ + @Override public void addListener(ChangeListener<? super T> 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<? super T> listener) { Objects.requireNonNull(listener); listeners.remove(listener); } - - @FunctionalInterface - public interface ChangeListener<T> { - void onPropertyChange(ObservableProperty<? extends T> 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<T> extends Supplier<T> { + /** + * Adds a change listener to this property view. + * + * @param listener the added listener + */ + void addListener(ChangeListener<? super T> listener); + + /** + * Removes a change listener from this property view if present. + * + * @param listener the removed listener + */ + void removeListener(ChangeListener<? super T> listener); + + /** + * A listener for changes in observable views and properties. + * + * @param <T> the value type listened to + */ + @FunctionalInterface + interface ChangeListener<T> { + /** + * Handles a change in an observable property. + * + * @param property + * @param from + * @param to + */ + void onPropertyChange(ObservableView<? extends T> property, @Nullable T from, @Nullable T to); + } +} |