You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandInvoker.java 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package command;
  7. import command.CommandInterface;
  8. import java.util.HashMap;
  9. import java.util.Stack;
  10. /**
  11. *
  12. * @author PC
  13. */
  14. public class CommandInvoker {
  15. private HashMap<Object, CommandInterface> commands;
  16. private Stack<CommandInterface> undoStack;
  17. public CommandInvoker()
  18. {
  19. commands = new HashMap<>();
  20. undoStack = new Stack<>();
  21. }
  22. public void addCommand(Object key, CommandInterface value)
  23. {
  24. commands.put(key, value);
  25. }
  26. public void executeCommand(Object key)
  27. {
  28. CommandInterface cmd = commands.get(key);
  29. cmd.execute();
  30. if(cmd.isUndoable())
  31. {
  32. undoStack.push(cmd);
  33. }
  34. }
  35. public void undoCommand()
  36. {
  37. if(!undoStack.empty())
  38. {
  39. undoStack.pop().undo();
  40. }
  41. }
  42. }