jimm.datavision.gui.cmd
Class CommandHistory

java.lang.Object
  extended by jimm.datavision.gui.cmd.CommandHistory

public class CommandHistory
extends java.lang.Object

A command history holds comands and manages undo and redo behavior. To use a new command for the first time, pass it to perform. You can then call undo and redo to walk the command history chain.

Author:
Jim Menard, jimm@io.com

Field Summary
protected  int baselineIndex
          If the command index is different than the baseline index then something has changed, and isChanged will return true.
protected  int commandIndex
           
protected  java.util.ArrayList commands
           
protected  javax.swing.JMenuItem redoMenuItem
           
protected  javax.swing.JMenuItem undoMenuItem
           
 
Constructor Summary
CommandHistory()
           
CommandHistory(javax.swing.JMenuItem undoMenuItem, javax.swing.JMenuItem redoMenuItem)
           
 
Method Summary
 void add(Command command)
          Add a command to the history list without performing it.
 boolean canRedo()
          Answers the question, "Is there anything to redo?"
 boolean canUndo()
          Answers the question, "Is there anything to undo?"
 java.lang.String getCommandName(int index)
          Return the name of the command at index.
 java.lang.String getRedoName()
          Return the name of the command that would be redone were one to call redo().
 java.lang.String getUndoName()
          Return the name of the command that would be undone were one to call undo().
 boolean isChanged()
          Answers the question, "Has anything changed?"
 void perform(Command command)
          Perform a command and add it to the history list.
 void redo()
          Redo the command under the the history cursor.
 void setBaseline()
          Resets the baseline index.
 void setMenuItems(javax.swing.JMenuItem undoMenuItem, javax.swing.JMenuItem redoMenuItem)
           
 void undo()
          Undo the command at the history cursor.
protected  void updateMenu()
           
protected  void updateMenuItem(javax.swing.JMenuItem item, java.lang.String verb, java.lang.String cmdName, boolean canDo)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

commands

protected java.util.ArrayList commands

commandIndex

protected int commandIndex

baselineIndex

protected int baselineIndex
If the command index is different than the baseline index then something has changed, and isChanged will return true.


undoMenuItem

protected javax.swing.JMenuItem undoMenuItem

redoMenuItem

protected javax.swing.JMenuItem redoMenuItem
Constructor Detail

CommandHistory

public CommandHistory()

CommandHistory

public CommandHistory(javax.swing.JMenuItem undoMenuItem,
                      javax.swing.JMenuItem redoMenuItem)
Method Detail

setMenuItems

public void setMenuItems(javax.swing.JMenuItem undoMenuItem,
                         javax.swing.JMenuItem redoMenuItem)

canUndo

public boolean canUndo()
Answers the question, "Is there anything to undo?"


canRedo

public boolean canRedo()
Answers the question, "Is there anything to redo?"


setBaseline

public void setBaseline()
Resets the baseline index. The method isChanged returns false only when the baseline index is equal to the current command index.


isChanged

public boolean isChanged()
Answers the question, "Has anything changed?"


add

public void add(Command command)
Add a command to the history list without performing it. If the history cursor is not at the top of the stack, first erase all of the commands after the cursor.

Parameters:
command - the command to add

perform

public void perform(Command command)
Perform a command and add it to the history list. If the history cursor is not at the top of the stack, first erase all of the commands after the cursor.

Parameters:
command - the command to perform

undo

public void undo()
Undo the command at the history cursor.


redo

public void redo()
Redo the command under the the history cursor.


getUndoName

public java.lang.String getUndoName()
Return the name of the command that would be undone were one to call undo(). Returns null if there is no such command.


getRedoName

public java.lang.String getRedoName()
Return the name of the command that would be redone were one to call redo(). Returns null if there is no such command.


getCommandName

public java.lang.String getCommandName(int index)
Return the name of the command at index. Returns null if there is no such command.


updateMenu

protected void updateMenu()

updateMenuItem

protected void updateMenuItem(javax.swing.JMenuItem item,
                              java.lang.String verb,
                              java.lang.String cmdName,
                              boolean canDo)