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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 Jan
  12. * Invoker für alle Commands zur übersichtlichkeit, evtl. undo anruf?
  13. */
  14. public class CommandInvoker
  15. {
  16. private HashMap<Object, CommandInterface> commands;
  17. private Stack<CommandInterface> undoStack;
  18. public CommandInvoker()
  19. {
  20. commands = new HashMap<>();
  21. undoStack = new Stack<>();
  22. }
  23. public void addCommand(Object key,CommandInterface value)
  24. {
  25. commands.put(key,value);
  26. }
  27. public void executeCommand(Object key)
  28. {
  29. commands.get(key).execute();
  30. if(commands.get(key).isundoable()==true)
  31. {
  32. undoStack.push(commands.get(key));
  33. }
  34. }
  35. public void undoCommand(Object key)
  36. {
  37. try
  38. {
  39. undoStack.pop().undo();
  40. }
  41. catch (Exception e)
  42. {
  43. }
  44. }
  45. }