diff --git a/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java b/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java index be9424b..daffcfd 100644 --- a/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java @@ -26,7 +26,7 @@ public interface ChangeManager { * @return the change that was applied last * @since 0.0.1 */ - Optional getLastChange(); + Optional getLastChange(); /** * Undoes the current change. diff --git a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java index b353a7d..4e64120 100644 --- a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java @@ -23,7 +23,7 @@ public final class UnlimitedChangeManager implements ChangeMan } @Override - public Optional getLastChange() { + public Optional getLastChange() { return index == -1 ? Optional.empty() : Optional.of(changes.get(index)); } diff --git a/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java b/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java new file mode 100644 index 0000000..85029b5 --- /dev/null +++ b/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java @@ -0,0 +1,118 @@ +package dev.kske.undoredo.javafx; + +import java.util.List; + +import javafx.beans.property.*; + +import dev.kske.undoredo.core.*; + +/** + * Wraps an ordinary change manager into an observable change manager, providing the required + * properties for concrete implementations. + * + * @param the change type to store in this change manager + * @param the type of change manager to wrap + * @author Kai S. K. Engelbart + * @since 0.0.1 + */ +public class ChangeManagerWrapper> + implements ObservableChangeManager { + + protected ReadOnlyObjectWrapper lastChange = + new ReadOnlyObjectWrapper<>(this, "lastChange"); + protected ReadOnlyBooleanWrapper atMarkedIndex = + new ReadOnlyBooleanWrapper(this, "atMarkedIndex"); + protected ReadOnlyBooleanWrapper undoAvailable = + new ReadOnlyBooleanWrapper(this, "undoAvailable"); + protected ReadOnlyBooleanWrapper redoAvailable = + new ReadOnlyBooleanWrapper(this, "redoAvailable"); + + protected final M manager; + + protected ChangeManagerWrapper(M manager) { + this.manager = manager; + } + + @Override + public void addChange(C change) { + manager.addChange(change); + updateProperties(); + } + + @Override + public boolean undo() { + if (manager.undo()) { + updateProperties(); + return true; + } + return false; + } + + @Override + public boolean redo() { + if (manager.redo()) { + updateProperties(); + return true; + } + return false; + } + + @Override + public void mark() { + manager.mark(); + setAtMarkedIndex(manager.isAtMarkedIndex()); + } + + /** + * Sets the values of all properties to those present in the wrapped change manager. + * + * @since 0.0.1 + */ + private void updateProperties() { + setLastChange(manager.getLastChange().orElse(null)); + setAtMarkedIndex(manager.isAtMarkedIndex()); + setUndoAvailable(manager.isUndoAvailable()); + setRedoAvailable(manager.isRedoAvailable()); + } + + @Override + public final ReadOnlyObjectProperty lastChangeProperty() { + return lastChange.getReadOnlyProperty(); + } + + protected final void setLastChange(C lastChange) { + this.lastChange.set(lastChange); + } + + @Override + public final ReadOnlyBooleanProperty atMarkedIndexProperty() { + return atMarkedIndex.getReadOnlyProperty(); + } + + protected final void setAtMarkedIndex(boolean atMarkedIndex) { + this.atMarkedIndex.set(atMarkedIndex); + } + + @Override + public final ReadOnlyBooleanProperty undoAvailableProperty() { + return undoAvailable.getReadOnlyProperty(); + } + + protected final void setUndoAvailable(boolean undoAvailable) { + this.undoAvailable.set(undoAvailable); + } + + @Override + public final ReadOnlyBooleanProperty redoAvailableProperty() { + return redoAvailable.getReadOnlyProperty(); + } + + protected final void setRedoAvailable(boolean redoAvailable) { + this.redoAvailable.set(redoAvailable); + } + + @Override + public List getChanges() { + return manager.getChanges(); + } +} diff --git a/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java b/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java new file mode 100644 index 0000000..09697af --- /dev/null +++ b/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java @@ -0,0 +1,43 @@ +package dev.kske.undoredo.javafx; + +import java.util.Optional; + +import javafx.beans.property.*; + +import dev.kske.undoredo.core.*; + +/** + * @param the change type to store in this change manager + * @author Kai S. K. Engelbart + * @since 0.0.1 + */ +public interface ObservableChangeManager extends ChangeManager { + + ReadOnlyObjectProperty lastChangeProperty(); + + @Override + default Optional getLastChange() { + return Optional.of(lastChangeProperty().get()); + } + + ReadOnlyBooleanProperty atMarkedIndexProperty(); + + @Override + default boolean isAtMarkedIndex() { + return atMarkedIndexProperty().get(); + } + + ReadOnlyBooleanProperty undoAvailableProperty(); + + @Override + default boolean isUndoAvailable() { + return undoAvailableProperty().get(); + } + + ReadOnlyBooleanProperty redoAvailableProperty(); + + @Override + default boolean isRedoAvailable() { + return redoAvailableProperty().get(); + } +} diff --git a/javafx/src/main/java/module-info.java b/javafx/src/main/java/module-info.java index 654bed2..aa7f939 100644 --- a/javafx/src/main/java/module-info.java +++ b/javafx/src/main/java/module-info.java @@ -9,5 +9,5 @@ module dev.kske.undoredo.javafx { exports dev.kske.undoredo.javafx; requires dev.kske.undoredo.core; - requires javafx.base; + requires transitive javafx.base; }