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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 commands;
  7. import java.awt.Component;
  8. import java.util.HashMap;
  9. import java.util.Stack;
  10. /**
  11. * Command Invoker
  12. * ordnet den verschiedenen Events die Kommandos zu
  13. * speichert die letzten Kommandos im Stack
  14. * @author matthias
  15. */
  16. public class CommandInvoker {
  17. private HashMap<Component, CommandInterface> commands;
  18. private Stack<CommandInterface> undoStack;
  19. public CommandInvoker()
  20. {
  21. this.commands = new HashMap<>();
  22. this.undoStack = new Stack<>();
  23. }
  24. public void addCommand(Component key, CommandInterface val)
  25. {
  26. this.commands.put(key, val);
  27. }
  28. /**
  29. * führt das zugehörige Kommand zum Key(Object) aus
  30. * @param key (Objekt)
  31. */
  32. public void executeCommand(Component key)
  33. {
  34. this.commands.get(key).execute();
  35. this.undoStack.push(this.commands.get(key));
  36. }
  37. /**
  38. * führt das zugehörige undo vom letzten Kommand aus
  39. */
  40. public void undoCommand()
  41. {
  42. if (!this.undoStack.isEmpty())
  43. {
  44. this.undoStack.pop().undo();
  45. }
  46. }
  47. }