Add ObservableChangeManager interface with wrapper implementation
All checks were successful
zdm/undo-redo/pipeline/head This commit looks good

This commit is contained in:
Kai S. K. Engelbart 2021-12-12 08:05:24 +01:00
parent d49772a127
commit 1b6d7f3dde
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
5 changed files with 164 additions and 3 deletions

View File

@ -26,7 +26,7 @@ public interface ChangeManager<C extends Change> {
* @return the change that was applied last * @return the change that was applied last
* @since 0.0.1 * @since 0.0.1
*/ */
Optional<Change> getLastChange(); Optional<C> getLastChange();
/** /**
* Undoes the current change. * Undoes the current change.

View File

@ -23,7 +23,7 @@ public final class UnlimitedChangeManager<C extends Change> implements ChangeMan
} }
@Override @Override
public Optional<Change> getLastChange() { public Optional<C> getLastChange() {
return index == -1 ? Optional.empty() : Optional.of(changes.get(index)); return index == -1 ? Optional.empty() : Optional.of(changes.get(index));
} }

View File

@ -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 <C> the change type to store in this change manager
* @param <M> the type of change manager to wrap
* @author Kai S. K. Engelbart
* @since 0.0.1
*/
public class ChangeManagerWrapper<C extends Change, M extends ChangeManager<C>>
implements ObservableChangeManager<C> {
protected ReadOnlyObjectWrapper<C> 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<C> 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<C> getChanges() {
return manager.getChanges();
}
}

View File

@ -0,0 +1,43 @@
package dev.kske.undoredo.javafx;
import java.util.Optional;
import javafx.beans.property.*;
import dev.kske.undoredo.core.*;
/**
* @param <C> the change type to store in this change manager
* @author Kai S. K. Engelbart
* @since 0.0.1
*/
public interface ObservableChangeManager<C extends Change> extends ChangeManager<C> {
ReadOnlyObjectProperty<C> lastChangeProperty();
@Override
default Optional<C> 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();
}
}

View File

@ -9,5 +9,5 @@ module dev.kske.undoredo.javafx {
exports dev.kske.undoredo.javafx; exports dev.kske.undoredo.javafx;
requires dev.kske.undoredo.core; requires dev.kske.undoredo.core;
requires javafx.base; requires transitive javafx.base;
} }