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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 kontakte.controller;
  7. import java.awt.Component;
  8. import java.util.HashMap;
  9. import java.util.Stack;
  10. /**
  11. *
  12. * @author nobody
  13. */
  14. public class CommandInvoker
  15. {
  16. private HashMap<Component, CommandInterface> commands;
  17. private Stack<CommandInterface> undoStack;
  18. public CommandInvoker()
  19. {
  20. commands = new HashMap<>();
  21. undoStack = new Stack<CommandInterface>();
  22. }
  23. public void addCommand(Component key, CommandInterface value)
  24. {
  25. commands.put(key, value);
  26. }
  27. public void executeCommand(Component key)
  28. {
  29. commands.get(key).execute();
  30. undoStack.add(commands.get(key));
  31. }
  32. public void undoCommand()
  33. {
  34. if (!undoStack.empty())
  35. {
  36. undoStack.pop().undo();
  37. }
  38. }
  39. }