All Downloads are FREE. Search and download functionalities are using the official Maven repository.

undo.UndoRedoStack Maven / Gradle / Ivy

There is a newer version: 0.1.5
Show newest version
package undo;

/*-
 * #%L
 * FOKProjects Common
 * %%
 * Copyright (C) 2016 - 2017 Frederik Kammel
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.util.Stack;

/**
 * A simple implementation of a undo/redo stack
 */
public class UndoRedoStack {
    private Stack undoStack = new Stack<>();
    private Stack redoStack = new Stack<>();

    /**
     * Adds a new command to the undo stack and reinitializes the redo stack. To be called when the user executes something new.
     *
     * @param command The command to be added to the undo stack
     */
    public void push(Command command) {
        redoStack = new Stack<>();
        undoStack.push(command);
    }

    /**
     * Pops the last command from the undo stack, puts it on the redo stack and calls its {@link Command#undo()}-method
     *
     * @return The command that was undone. Its undo method was already called!
     */
    public Command undoLast() {
        Command lastCommand = undoStack.pop();
        redoStack.push(lastCommand);
        lastCommand.undo();
        return lastCommand;
    }

    /**
     * Pops the last command from the redo stack, puts it on the undo stack and calls its {@link Command#redo()}-method. If no commandisfound on the redo stack,the last command from thundo stack is redone.
     *
     * @return The command that was redone. Its redo method was already called!
     */
    public Command redoNext() {
        Command nextCommand;
        if (redoStack.size() > 0) {
            nextCommand = redoStack.pop();
        } else {
            // redo the last command that was done
            nextCommand = undoStack.pop();
            // put it again on the stack
            undoStack.push(nextCommand);
        }
        undoStack.push(nextCommand);
        nextCommand.redo();
        return nextCommand;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy