Skip to content

Commit

Permalink
Closes #463
Browse files Browse the repository at this point in the history
Also tidied things up a little bit
  • Loading branch information
matthewhorridge committed Aug 4, 2016
1 parent 33dd6d1 commit 1c52b00
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.semanticweb.owlapi.model.OWLOntologyChange;

import javax.annotation.Nonnull;
import java.util.List;


Expand All @@ -16,27 +17,61 @@
*/
public interface HistoryManager {

public void logChanges(List<? extends OWLOntologyChange> changes);


public boolean canUndo();


public void undo();


public boolean canRedo();


public void redo();


public void addUndoManagerListener(UndoManagerListener listener);


public void removeUndoManagerListener(UndoManagerListener listener);


public List<List<OWLOntologyChange>> getLoggedChanges();
/**
* Logs the specified list of changes so that they can be undone at a later point in time.
* @param changes The changes to be logged.
*/
void logChanges(@Nonnull List<? extends OWLOntologyChange> changes);

/**
* Determines whether or not it is possible to perform an undo operation.
* @return {@code true} if it is possible to perform an undo operation, otherwise {@code false}.
*/
boolean canUndo();


/**
* Performs an undo operation, undoing the last set of changes. If it is not possible to perform an undo
* then nothing will happen.
*/
void undo();


/**
* Determines whether or not it is possible to perform a redo operation.
* @return {@code true} if it is possible to perform a redo operation, otherwise {@code false}.
*/
boolean canRedo();

/**
* Performs a redo operation, redoing the last set of undone changes. If it is not possible to perfom a redo
* then nothing will happen.
*/
void redo();

/**
* Clears the history. Warning: Clearing the history will prevent the user from undoing any of their previous
* changes.
*/
void clear();

/**
* Gets a list of the lists of changes that are currently in the undo stack.
* @return The changes logged in the undo stack.
*/
@Nonnull
List<List<OWLOntologyChange>> getLoggedChanges();

/**
* Adds a listener to this manager.
* @param listener The listener.
*/
void addUndoManagerListener(@Nonnull UndoManagerListener listener);

/**
* Removes a previously added listener
* @param listener The listener.
*/
void removeUndoManagerListener(@Nonnull UndoManagerListener listener);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import static com.google.common.base.Preconditions.checkNotNull;


/**
* Author: Matthew Horridge<br>
Expand All @@ -28,9 +31,9 @@ private enum ChangeType {

private ChangeType typeOfChangeInProgress = ChangeType.NORMAL;

private Logger logger = LoggerFactory.getLogger(HistoryManager.class);
private static final Logger logger = LoggerFactory.getLogger(HistoryManager.class);

private OWLOntologyManager manager;
private final OWLOntologyManager manager;


/**
Expand All @@ -39,17 +42,17 @@ private enum ChangeType {
* if the list contain an "add superclass" history, then the
* required undo history is a "remove superclass" history.
*/
private Stack<List<OWLOntologyChange>> undoStack;
private final Stack<List<OWLOntologyChange>> undoStack = new Stack<>();

/**
* Holds a list of sets of changes that can be redone. These
* are changes that result from an undo operation.
* These are a list of "forward" changes rather that the
* "undo changes".
*/
private Stack<List<OWLOntologyChange>> redoStack;
private final Stack<List<OWLOntologyChange>> redoStack = new Stack<>();

private List<UndoManagerListener> listeners;
private final List<UndoManagerListener> listeners = new ArrayList<>();


public HistoryManagerImpl(OWLModelManager owlModelManager) {
Expand All @@ -59,9 +62,6 @@ public HistoryManagerImpl(OWLModelManager owlModelManager) {

public HistoryManagerImpl(OWLOntologyManager manager) {
this.manager = manager;
undoStack = new Stack<>();
redoStack = new Stack<>();
listeners = new ArrayList<>();
typeOfChangeInProgress = ChangeType.NORMAL;
}

Expand All @@ -76,7 +76,7 @@ public boolean canUndo() {
}


public void logChanges(List<? extends OWLOntologyChange> changes) {
public void logChanges(@Nonnull List<? extends OWLOntologyChange> changes) {
switch (typeOfChangeInProgress) {
case NORMAL:
// Clear the redo stack, because we can
Expand Down Expand Up @@ -140,17 +140,23 @@ public void undo() {
}
}

@Override
public void clear() {
redoStack.clear();
undoStack.clear();
fireStateChanged();
}

public void addUndoManagerListener(UndoManagerListener listener) {
listeners.add(listener);
public void addUndoManagerListener(@Nonnull UndoManagerListener listener) {
listeners.add(checkNotNull(listener));
}


public void removeUndoManagerListener(UndoManagerListener listener) {
public void removeUndoManagerListener(@Nonnull UndoManagerListener listener) {
listeners.remove(listener);
}


@Nonnull
public List<List<OWLOntologyChange>> getLoggedChanges() {
List<List<OWLOntologyChange>> copyOfLog = new ArrayList<>();
for (List<OWLOntologyChange> changes : undoStack){
Expand Down

0 comments on commit 1c52b00

Please sign in to comment.