weberni69795 588c1be66e Version 1.0
fertige Aufgabe
2019-11-10 10:39:39 +01:00

53 lines
1.2 KiB
Java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commands;
import java.awt.Component;
import java.util.HashMap;
import java.util.Stack;
/**
* Command Invoker
* ordnet den verschiedenen Events die Kommandos zu
* speichert die letzten Kommandos im Stack
* @author matthias
*/
public class CommandInvoker {
private HashMap<Component, CommandInterface> commands;
private Stack<CommandInterface> undoStack;
public CommandInvoker()
{
this.commands = new HashMap<>();
this.undoStack = new Stack<>();
}
public void addCommand(Component key, CommandInterface val)
{
this.commands.put(key, val);
}
/**
* führt das zugehörige Kommand zum Key(Object) aus
* @param key (Objekt)
*/
public void executeCommand(Component key)
{
this.commands.get(key).execute();
this.undoStack.push(this.commands.get(key));
}
/**
* führt das zugehörige undo vom letzten Kommand aus
*/
public void undoCommand()
{
if (!this.undoStack.isEmpty())
{
this.undoStack.pop().undo();
}
}
}