From 0fc35777501529fed1d4e3b173adef319a36b730 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 23 Dec 2021 10:00:48 +0200 Subject: [PATCH] Add change manager unmarking --- .../dev/kske/undoredo/core/ChangeManager.java | 9 +++++- .../undoredo/core/UnlimitedChangeManager.java | 7 ++++- .../kske/undoredo/core/ChangeManagerTest.java | 24 ++++++++++++--- .../undoredo/javafx/ChangeManagerWrapper.java | 30 +++++++++++-------- .../javafx/ObservableChangeManager.java | 6 ++-- 5 files changed, 55 insertions(+), 21 deletions(-) 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 5410cfc..3c01003 100644 --- a/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java @@ -55,11 +55,18 @@ public interface ChangeManager { */ void mark(); + /** + * Resets the marked index so that no change is marked. + * + * @since 0.0.1 + */ + void unmark(); + /** * @return whether the current change is marked * @since 0.0.1 */ - boolean isAtMarkedIndex(); + boolean isAtMarkedChange(); /** * @return whether a change is present that can be undone 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 4b0d13b..ef06c84 100644 --- a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java @@ -53,7 +53,12 @@ public final class UnlimitedChangeManager implements ChangeMan } @Override - public boolean isAtMarkedIndex() { + public void unmark() { + markedIndex = -1; + } + + @Override + public boolean isAtMarkedChange() { return markedIndex == index; } diff --git a/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java b/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java index 51ac613..bbfe7d5 100644 --- a/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java +++ b/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java @@ -108,12 +108,28 @@ class ChangeManagerTest { @Test @Order(60) void testMark() { - assertTrue(manager.isAtMarkedIndex()); + assertTrue(manager.isAtMarkedChange()); manager.addChange(change); - assertFalse(manager.isAtMarkedIndex()); + assertFalse(manager.isAtMarkedChange()); manager.mark(); - assertTrue(manager.isAtMarkedIndex()); + assertTrue(manager.isAtMarkedChange()); manager.undo(); - assertFalse(manager.isAtMarkedIndex()); + assertFalse(manager.isAtMarkedChange()); + } + + /** + * Tests unmarking a change. + * + * @since 0.0.1 + */ + @Test + @Order(70) + void testUnmark() { + manager.addChange(change); + manager.mark(); + manager.unmark(); + assertFalse(manager.isAtMarkedChange()); + manager.undo(); + assertTrue(manager.isAtMarkedChange()); } } diff --git a/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java b/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java index 0c16cb6..b21e17a 100644 --- a/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java +++ b/javafx/src/main/java/dev/kske/undoredo/javafx/ChangeManagerWrapper.java @@ -23,15 +23,15 @@ import dev.kske.undoredo.core.*; public class ChangeManagerWrapper> implements ObservableChangeManager { - public static final String LAST_CHANGE = "lastChange"; - public static final String AT_MARKED_INDEX = "atMarkedIndex"; - public static final String UNDO_AVAILABLE = "undoAvailable"; - public static final String REDO_AVAILABLE = "redoAvailable"; + public static final String LAST_CHANGE = "lastChange"; + public static final String AT_MARKED_CHANGE = "atMarkedChange"; + public static final String UNDO_AVAILABLE = "undoAvailable"; + public static final String REDO_AVAILABLE = "redoAvailable"; protected ReadOnlyObjectWrapper lastChange = new ReadOnlyObjectWrapper<>(this, LAST_CHANGE); - protected ReadOnlyBooleanWrapper atMarkedIndex = - new ReadOnlyBooleanWrapper(this, AT_MARKED_INDEX); + protected ReadOnlyBooleanWrapper atMarkedChange = + new ReadOnlyBooleanWrapper(this, AT_MARKED_CHANGE); protected ReadOnlyBooleanWrapper undoAvailable = new ReadOnlyBooleanWrapper(this, UNDO_AVAILABLE); protected ReadOnlyBooleanWrapper redoAvailable = @@ -76,7 +76,13 @@ public class ChangeManagerWrapper> @Override public void mark() { manager.mark(); - setAtMarkedIndex(manager.isAtMarkedIndex()); + setAtMarkedChange(manager.isAtMarkedChange()); + } + + @Override + public void unmark() { + manager.unmark(); + setAtMarkedChange(manager.isAtMarkedChange()); } /** @@ -86,7 +92,7 @@ public class ChangeManagerWrapper> */ private void updateProperties() { setLastChange(manager.getLastChange().orElse(null)); - setAtMarkedIndex(manager.isAtMarkedIndex()); + setAtMarkedChange(manager.isAtMarkedChange()); setUndoAvailable(manager.isUndoAvailable()); setRedoAvailable(manager.isRedoAvailable()); } @@ -101,12 +107,12 @@ public class ChangeManagerWrapper> } @Override - public final ReadOnlyBooleanProperty atMarkedIndexProperty() { - return atMarkedIndex.getReadOnlyProperty(); + public final ReadOnlyBooleanProperty atMarkedChangeProperty() { + return atMarkedChange.getReadOnlyProperty(); } - protected final void setAtMarkedIndex(boolean atMarkedIndex) { - this.atMarkedIndex.set(atMarkedIndex); + protected final void setAtMarkedChange(boolean atMarkedChange) { + this.atMarkedChange.set(atMarkedChange); } @Override diff --git a/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java b/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java index 4dad0e4..2908589 100644 --- a/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java +++ b/javafx/src/main/java/dev/kske/undoredo/javafx/ObservableChangeManager.java @@ -24,11 +24,11 @@ public interface ObservableChangeManager extends ChangeManager return Optional.of(lastChangeProperty().get()); } - ReadOnlyBooleanProperty atMarkedIndexProperty(); + ReadOnlyBooleanProperty atMarkedChangeProperty(); @Override - default boolean isAtMarkedIndex() { - return atMarkedIndexProperty().get(); + default boolean isAtMarkedChange() { + return atMarkedChangeProperty().get(); } ReadOnlyBooleanProperty undoAvailableProperty(); -- 2.45.2