53 lines
1.2 KiB
Java
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();
|
|
}
|
|
}
|
|
}
|