Skip to content

Commit

Permalink
Expose nextToUndo, nextToRedo in public API. Requires to make UndoMan…
Browse files Browse the repository at this point in the history
…ager generic.
  • Loading branch information
TomasMikula committed Apr 28, 2017
1 parent be4d813 commit 2eedd39
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public boolean equals(Object other) {
private final Button redoBtn = new Button("Redo");
private final Button saveBtn = new Button("Save");
private final EventStream<CircleChange<?>> changes;
private final UndoManager undoManager;
private final UndoManager<CircleChange<?>> undoManager;

{
circle.fillProperty().bind(colorPicker.valueProperty());
Expand Down
14 changes: 13 additions & 1 deletion undofx/src/main/java/org/fxmisc/undo/UndoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.reactfx.value.Val;

public interface UndoManager {
public interface UndoManager<C> {

/**
* Represents a position in UndoManager's history.
Expand Down Expand Up @@ -48,6 +48,18 @@ interface UndoPosition {
Val<Boolean> undoAvailableProperty();
boolean isUndoAvailable();

/**
* Gives a peek at the change that will be undone by {@link #undo()}.
*/
Val<C> nextToUndoProperty();
default C getNextToUndo() { return nextToUndoProperty().getValue(); }

/**
* Gives a peek at the change that will be redone by {@link #redo()}.
*/
Val<C> nextToRedoProperty();
default C getNextToRedo() { return nextToRedoProperty().getValue(); }

/**
* Indicates whether there is a change that can be redone.
*/
Expand Down
26 changes: 13 additions & 13 deletions undofx/src/main/java/org/fxmisc/undo/UndoManagerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface UndoManagerFactory {
* describes an action to be performed. Calling {@code apply.accept(c)}
* <em>must</em> cause {@code c} to be emitted from {@code changeStream}.
*/
default <C> UndoManager create(
default <C> UndoManager<C> create(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply) {
Expand All @@ -46,7 +46,7 @@ default <C> UndoManager create(
* @param merge Used to merge two subsequent changes into one.
* Returns an empty {@linkplain Optional} when the changes cannot or should not be merged.
*/
default <C> UndoManager create(
default <C> UndoManager<C> create(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -72,7 +72,7 @@ default <C> UndoManager create(
* @param isIdentity returns true for changes whose application would have no effect, thereby equivalent
* to an identity function ({@link Function#identity()}) on the underlying model.
*/
<C> UndoManager create(
<C> UndoManager<C> create(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -84,7 +84,7 @@ <C> UndoManager create(
*
* For description of parameters, see {@link #create(EventStream, Function, Consumer)}.
*/
public static <C> UndoManager unlimitedHistoryUndoManager(
public static <C> UndoManager<C> unlimitedHistoryUndoManager(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply) {
Expand All @@ -96,7 +96,7 @@ public static <C> UndoManager unlimitedHistoryUndoManager(
*
* For description of parameters, see {@link #create(EventStream, Function, Consumer, BiFunction)}.
*/
public static <C> UndoManager unlimitedHistoryUndoManager(
public static <C> UndoManager<C> unlimitedHistoryUndoManager(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -108,7 +108,7 @@ public static <C> UndoManager unlimitedHistoryUndoManager(
*
* For description of parameters, see {@link #create(EventStream, Function, Consumer, BiFunction, Predicate)}.
*/
public static <C> UndoManager unlimitedHistoryUndoManager(
public static <C> UndoManager<C> unlimitedHistoryUndoManager(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -124,7 +124,7 @@ public static <C> UndoManager unlimitedHistoryUndoManager(
public static UndoManagerFactory unlimitedHistoryFactory() {
return new UndoManagerFactory() {
@Override
public <C> UndoManager create(
public <C> UndoManager<C> create(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -143,7 +143,7 @@ public <C> UndoManager create(
*
* @param capacity maximum number of changes the returned UndoManager can store
*/
public static <C> UndoManager fixedSizeHistoryUndoManager(
public static <C> UndoManager<C> fixedSizeHistoryUndoManager(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -159,7 +159,7 @@ public static <C> UndoManager fixedSizeHistoryUndoManager(
*
* @param capacity maximum number of changes the returned UndoManager can store
*/
public static <C> UndoManager fixedSizeHistoryUndoManager(
public static <C> UndoManager<C> fixedSizeHistoryUndoManager(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -176,7 +176,7 @@ public static <C> UndoManager fixedSizeHistoryUndoManager(
*
* @param capacity maximum number of changes the returned UndoManager can store
*/
public static <C> UndoManager fixedSizeHistoryUndoManager(
public static <C> UndoManager<C> fixedSizeHistoryUndoManager(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -194,7 +194,7 @@ public static <C> UndoManager fixedSizeHistoryUndoManager(
public static UndoManagerFactory fixedSizeHistoryFactory(int capacity) {
return new UndoManagerFactory() {
@Override
public <C> UndoManager create(
public <C> UndoManager<C> create(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand All @@ -213,7 +213,7 @@ public <C> UndoManager create(
* {@link UndoManager#atMarkedPositionProperty()} to determine whether any change has occurred since the last
* {@link UndoManager#mark()} (e.g. since the last save).
*/
public static <C> UndoManager zeroHistoryUndoManager(EventStream<C> changeStream) {
public static <C> UndoManager<C> zeroHistoryUndoManager(EventStream<C> changeStream) {
ChangeQueue<C> queue = new ZeroSizeChangeQueue<>();
return new UndoManagerImpl<>(queue, c -> c, c -> {}, (c1, c2) -> Optional.empty(), c -> false, changeStream);
}
Expand All @@ -226,7 +226,7 @@ public static <C> UndoManager zeroHistoryUndoManager(EventStream<C> changeStream
public static UndoManagerFactory zeroHistoryFactory() {
return new UndoManagerFactory() {
@Override
public <C> UndoManager create(
public <C> UndoManager<C> create(
EventStream<C> changeStream,
Function<? super C, ? extends C> invert,
Consumer<C> apply,
Expand Down
12 changes: 11 additions & 1 deletion undofx/src/main/java/org/fxmisc/undo/impl/UndoManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.reactfx.value.Val;
import org.reactfx.value.ValBase;

public class UndoManagerImpl<C> implements UndoManager {
public class UndoManagerImpl<C> implements UndoManager<C> {

private class UndoPositionImpl implements UndoPosition {
private final QueuePosition queuePos;
Expand Down Expand Up @@ -118,6 +118,16 @@ public boolean redo() {
}
}

@Override
public Val<C> nextToUndoProperty() {
return nextToUndo;
}

@Override
public Val<C> nextToRedoProperty() {
return nextToRedo;
}

@Override
public boolean isUndoAvailable() {
return nextToUndo.isPresent();
Expand Down
24 changes: 12 additions & 12 deletions undofx/src/test/java/org/fxmisc/undo/impl/UndoManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UndoManagerTest {
public void testUndoInvertsTheChange() {
EventSource<Integer> changes = new EventSource<>();
Var<Integer> lastAction = Var.newSimpleVar(null);
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes, i -> -i, i -> { lastAction.setValue(i); changes.push(i); });

changes.push(3);
Expand All @@ -43,7 +43,7 @@ public void testUndoInvertsTheChange() {
@Test
public void testMark() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.fixedSizeHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.fixedSizeHistoryUndoManager(
changes, c -> c, changes::push, 4);

assertTrue(um.atMarkedPositionProperty().get());
Expand All @@ -68,7 +68,7 @@ public void testMark() {
@Test
public void testPositionValidAfterAddingAChange() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(changes, c -> c, changes::push);
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(changes, c -> c, changes::push);

changes.push(1);
UndoPosition pos = um.getCurrentPosition();
Expand All @@ -79,7 +79,7 @@ public void testPositionValidAfterAddingAChange() {
@Test
public void testPositionInvalidAfterMerge() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes, c -> -c, changes::push, (c1, c2) -> Optional.of(c1 + c2));

changes.push(1);
Expand All @@ -91,7 +91,7 @@ public void testPositionInvalidAfterMerge() {
@Test
public void testRedoUnavailableAfterAnnihilation() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes, c -> -c, changes::push, (c1, c2) -> Optional.of(c1 + c2), c -> c == 0);

changes.push(1);
Expand All @@ -102,7 +102,7 @@ public void testRedoUnavailableAfterAnnihilation() {
@Test
public void zeroHistoryUndoManagerMark() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.zeroHistoryUndoManager(changes);
UndoManager<?> um = UndoManagerFactory.zeroHistoryUndoManager(changes);

assertTrue(um.atMarkedPositionProperty().get());
changes.push(1);
Expand All @@ -122,7 +122,7 @@ public void zeroHistoryUndoManagerMark() {
@Test
public void testAtMarkedPositionRevalidation() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.zeroHistoryUndoManager(changes);
UndoManager<?> um = UndoManagerFactory.zeroHistoryUndoManager(changes);

um.atMarkedPositionProperty().get(); // atMarkedPositionProperty is now valid

Expand All @@ -142,7 +142,7 @@ public void testAtMarkedPositionRevalidation() {
@Test(expected = IllegalStateException.class)
public void testFailFastWhenExpectedChangeNotReceived() {
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes, i -> -i, i -> {});

changes.push(1);
Expand All @@ -156,7 +156,7 @@ public void testFailFastWhenExpectedChangeNotReceived() {
public void testPushedNonIdentityChangeIsStored() {
SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes,
i -> -i, // invert
i -> { lastAppliedValue.set(i); changes.push(i); }, // apply change and re-emit value so expected change is received
Expand All @@ -174,7 +174,7 @@ public void testPushedNonIdentityChangeIsStored() {
public void testPushedIdentityChangeIsNotStored() {
SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes,
i -> -i, // invert
i -> { lastAppliedValue.set(i); changes.push(i); }, // apply change and re-emit value so expected change is received
Expand All @@ -195,7 +195,7 @@ public void testPushedIdentityChangeIsNotStored() {
public void testMergeResultingInIdentityChangeAnnihilatesBothAndPreventsNextMerge() {
SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes,
i -> -i, // invert
i -> { lastAppliedValue.set(i); changes.push(i); }, // apply change and re-emit value so expected change is received
Expand Down Expand Up @@ -229,7 +229,7 @@ public void testMergeResultingInIdentityChangeAnnihilatesBothAndPreventsNextMerg
public void testMergeResultingInNonIdentityChangeStoresMergeAndPreventsNextMerge() {
SimpleIntegerProperty lastAppliedValue = new SimpleIntegerProperty(0);
EventSource<Integer> changes = new EventSource<>();
UndoManager um = UndoManagerFactory.unlimitedHistoryUndoManager(
UndoManager<?> um = UndoManagerFactory.unlimitedHistoryUndoManager(
changes,
i -> -i, // invert
i -> { lastAppliedValue.set(i); changes.push(i); }, // apply change and re-emit value so expected change is received
Expand Down

0 comments on commit 2eedd39

Please sign in to comment.