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 886B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 controller;
  7. import java.util.HashMap;
  8. import java.util.Stack;
  9. /**
  10. *
  11. * @author nobody
  12. */
  13. public class CommandInvoker
  14. {
  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. commands.get(key).execute();
  29. if (false)
  30. {
  31. undoStack.push(commands.get(key));
  32. }
  33. }
  34. public void undoCommand()
  35. {
  36. if(!undoStack.isEmpty())
  37. {
  38. undoStack.pop().undo();
  39. }
  40. }
  41. }