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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3. * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
  4. */
  5. package ChatProgramm.controller.commands;
  6. import java.awt.Component;
  7. import java.util.HashMap;
  8. import java.util.Stack;
  9. /**
  10. *
  11. * @author ahren
  12. */
  13. public class CommandInvoker {
  14. private HashMap<Component, CommandInterface> commands;
  15. private Stack <CommandInterface> undoStack;
  16. public CommandInvoker(){
  17. commands = new HashMap<>();
  18. undoStack = new Stack();
  19. }
  20. /**
  21. * Fügt ein Kommando zur Kommando-"Datenbank" = HashMap hinzu
  22. * @param key Quelle des Events
  23. * @param value Referenz auf das zugehörige Kommando-Objekt
  24. */
  25. public void addCommand(Component key, CommandInterface value){
  26. commands.put(key, value);
  27. }
  28. /**
  29. * Führt Kommando der Eventquelle "key" aus und legt die Referenz
  30. * des Kommando in den Undo-Stack
  31. * @param key Referenz auf die Eventquelle
  32. */
  33. public void executeCommand(Component key){
  34. CommandInterface command = commands.get(key);
  35. command.execute();
  36. if (command.isUndoable())
  37. {
  38. undoStack.push(command);
  39. }
  40. }
  41. /**
  42. * Falls der Stack Elemente enthält, wird das oberste Element geholt
  43. * und die Methode "undo" des Commands aufgerufen
  44. */
  45. public void undoCommand()
  46. {
  47. if (!undoStack.isEmpty())
  48. {
  49. undoStack.pop().undo();
  50. }
  51. }
  52. /**
  53. * Löscht bei Öffnen einer neuen Datei den Stack
  54. */
  55. public void deleteStack()
  56. {
  57. while(!undoStack.isEmpty())
  58. undoStack.pop();
  59. }
  60. }