Skip to content

Commit

Permalink
Add nextToUndo, nextToRedo properties to UndoManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasMikula committed Apr 27, 2017
1 parent 6f9b5ad commit f32c6cb
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Optional;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
Expand Down Expand Up @@ -194,8 +193,8 @@ public boolean equals(Object other) {
c -> c.redo(), // function to undo a change
(c1, c2) -> c1.mergeWith(c2)); // function to merge two changes

undoBtn.disableProperty().bind(Bindings.not(undoManager.undoAvailableProperty()));
redoBtn.disableProperty().bind(Bindings.not(undoManager.redoAvailableProperty()));
undoBtn.disableProperty().bind(undoManager.undoAvailableProperty().map(x -> !x));
redoBtn.disableProperty().bind(undoManager.redoAvailableProperty().map(x -> !x));
undoBtn.setOnAction(evt -> undoManager.undo());
redoBtn.setOnAction(evt -> undoManager.redo());
saveBtn.disableProperty().bind(undoManager.atMarkedPositionProperty());
Expand Down
6 changes: 4 additions & 2 deletions undofx/src/main/java/org/fxmisc/undo/UndoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import javafx.beans.value.ObservableBooleanValue;

import org.reactfx.value.Val;

public interface UndoManager {

/**
Expand Down Expand Up @@ -43,13 +45,13 @@ interface UndoPosition {
/**
* Indicates whether there is a change that can be undone.
*/
ObservableBooleanValue undoAvailableProperty();
Val<Boolean> undoAvailableProperty();
boolean isUndoAvailable();

/**
* Indicates whether there is a change that can be redone.
*/
ObservableBooleanValue redoAvailableProperty();
Val<Boolean> redoAvailableProperty();
boolean isRedoAvailable();

/**
Expand Down
8 changes: 8 additions & 0 deletions undofx/src/main/java/org/fxmisc/undo/impl/ChangeQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ interface QueuePosition {

boolean hasPrev();

/** Returns the next item. Current position stays unchanged. */
C peekNext();

/** Returns the previous item. Current position stays unchanged. */
C peekPrev();

/** Returns the next item and increases the current position by 1. */
C next();

/** Returns the previous item and decreases the current position by 1. */
C prev();

@SuppressWarnings({"unchecked"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,37 @@ public boolean hasPrev() {
}

@Override
public C next() {
public C peekNext() {
if(currentPosition < size) {
return fetch(currentPosition++).getChange();
return fetch(currentPosition).getChange();
} else {
throw new NoSuchElementException();
}
}

@Override
public C prev() {
public C next() {
C c = peekNext();
currentPosition += 1;
return c;
}

@Override
public C peekPrev() {
if(currentPosition > 0) {
return fetch(--currentPosition).getChange();
return fetch(currentPosition - 1).getChange();
} else {
throw new NoSuchElementException();
}
}

@Override
public C prev() {
C c = peekPrev();
currentPosition -= 1;
return c;
}

@Override
public void forgetHistory() {
zeroPositionRevision = fetchRevisionForPosition(currentPosition);
Expand Down
53 changes: 27 additions & 26 deletions undofx/src/main/java/org/fxmisc/undo/impl/UndoManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

import org.fxmisc.undo.UndoManager;
import org.fxmisc.undo.impl.ChangeQueue.QueuePosition;
import org.reactfx.EventSource;
import org.reactfx.EventStream;
import org.reactfx.Subscription;
import org.reactfx.SuspendableNo;
import org.reactfx.value.Val;
import org.reactfx.value.ValBase;

public class UndoManagerImpl<C> implements UndoManager {

Expand Down Expand Up @@ -45,21 +48,21 @@ public boolean isValid() {
private final Subscription subscription;
private final SuspendableNo performingAction = new SuspendableNo();

private final BooleanBinding undoAvailable = new BooleanBinding() {
@Override
protected boolean computeValue() {
return queue.hasPrev();
}
private final EventSource<Void> invalidationRequests = new EventSource<Void>();

private final Val<C> nextToUndo = new ValBase<C>() {
@Override protected Subscription connect() { return invalidationRequests.subscribe(x -> invalidate()); }
@Override protected C computeValue() { return queue.hasPrev() ? queue.peekPrev() : null; }
};

private final BooleanBinding redoAvailable = new BooleanBinding() {
@Override
protected boolean computeValue() {
return queue.hasNext();
}
private final Val<C> nextToRedo = new ValBase<C>() {
@Override protected Subscription connect() { return invalidationRequests.subscribe(x -> invalidate()); }
@Override protected C computeValue() { return queue.hasNext() ? queue.peekNext() : null; }
};

private final BooleanBinding atMarkedPosition = new BooleanBinding() {
{ invalidationRequests.addObserver(x -> this.invalidate()); }

@Override
protected boolean computeValue() {
return mark.equals(queue.getCurrentPosition());
Expand Down Expand Up @@ -96,9 +99,7 @@ public boolean undo() {
if(isUndoAvailable()) {
canMerge = false;
performChange(invert.apply(queue.prev()));
undoAvailable.invalidate();
redoAvailable.invalidate();
atMarkedPosition.invalidate();
invalidateProperties();
return true;
} else {
return false;
Expand All @@ -110,9 +111,7 @@ public boolean redo() {
if(isRedoAvailable()) {
canMerge = false;
performChange(queue.next());
undoAvailable.invalidate();
redoAvailable.invalidate();
atMarkedPosition.invalidate();
invalidateProperties();
return true;
} else {
return false;
Expand All @@ -121,22 +120,22 @@ public boolean redo() {

@Override
public boolean isUndoAvailable() {
return undoAvailable.get();
return nextToUndo.isPresent();
}

@Override
public ObservableBooleanValue undoAvailableProperty() {
return undoAvailable;
public Val<Boolean> undoAvailableProperty() {
return nextToUndo.map(c -> true).orElseConst(false);
}

@Override
public boolean isRedoAvailable() {
return redoAvailable.get();
return nextToRedo.isPresent();
}

@Override
public ObservableBooleanValue redoAvailableProperty() {
return redoAvailable;
public Val<Boolean> redoAvailableProperty() {
return nextToRedo.map(c -> true).orElseConst(false);
}

@Override
Expand Down Expand Up @@ -172,7 +171,7 @@ public void preventMerge() {
@Override
public void forgetHistory() {
queue.forgetHistory();
undoAvailable.invalidate();
invalidateProperties();
}

private void performChange(C change) {
Expand Down Expand Up @@ -220,8 +219,10 @@ private void addChange(C change) {
queue.push(change);
canMerge = true;
}
undoAvailable.invalidate();
redoAvailable.invalidate();
atMarkedPosition.invalidate();
invalidateProperties();
}

private void invalidateProperties() {
invalidationRequests.push(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ public final boolean hasPrev() {
return currentPosition > 0;
}

@Override
public final C peekNext() {
return changes.get(currentPosition).getChange();
}

@Override
public final C next() {
return changes.get(currentPosition++).getChange();
}

@Override
public final C peekPrev() {
return changes.get(currentPosition - 1).getChange();
}

@Override
public final C prev() {
return changes.get(--currentPosition).getChange();
Expand Down
10 changes: 10 additions & 0 deletions undofx/src/main/java/org/fxmisc/undo/impl/ZeroSizeChangeQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ public boolean hasPrev() {
return false;
}

@Override
public C peekNext() {
throw new NoSuchElementException();
}

@Override
public C next() {
throw new NoSuchElementException();
}

@Override
public C peekPrev() {
throw new NoSuchElementException();
}

@Override
public C prev() {
throw new NoSuchElementException();
Expand Down

0 comments on commit f32c6cb

Please sign in to comment.