Created
September 2, 2012 04:15
-
-
Save vivekrk/3594798 to your computer and use it in GitHub Desktop.
A simple undo manager implementation using command pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vravindranath.undosample; | |
public interface Command { | |
public static final String UNDO = "undo"; | |
public static final String REDO = "redo"; | |
public void execute(String command); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vravindranath.undosample; | |
import java.util.ArrayList; | |
public class UndoManager { | |
private ArrayList<Command> undoStack = new ArrayList<Command>(); | |
private ArrayList<Command> redoStack = new ArrayList<Command>(); | |
// private MainActivity mContext = null; | |
private static UndoManager mUndoManager = null; | |
// private String TAG = "UndoManager"; | |
private UndoManager() { | |
// mContext = (MainActivity) context; | |
} | |
public static UndoManager getInstance() { | |
if(mUndoManager == null) { | |
mUndoManager = new UndoManager(); | |
} | |
return mUndoManager; | |
} | |
public void undo() { | |
if (undoStack.size() > 0) { | |
Command command = undoStack.remove(undoStack.size() - 1); | |
command.execute(Command.UNDO); | |
} | |
// Log.d(TAG , "undoStack length: " + undoStack.size()); | |
// Log.d(TAG , "redoStack length: " + redoStack.size()); | |
} | |
public void redo() { | |
if (redoStack.size() > 0) { | |
Command command = redoStack.remove(redoStack.size() - 1); | |
command.execute(Command.REDO); | |
} | |
// Log.d(TAG , "undoStack length: " + undoStack.size()); | |
// Log.d(TAG , "redoStack length: " + redoStack.size()); | |
} | |
public void addToUndoStack(Command command) { | |
undoStack.add(command); | |
} | |
public void addToRedoStack(Command command) { | |
redoStack.add(command); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment