wenn du das Programm über Start.java startest, erscheint die View + Dialog zum Auswählen, es passiert aber noch nix
70 lines
1.7 KiB
Java
70 lines
1.7 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
*/
|
|
|
|
package ChatProgramm.controller.commands;
|
|
|
|
import java.awt.Component;
|
|
import java.util.HashMap;
|
|
import java.util.Stack;
|
|
|
|
/**
|
|
*
|
|
* @author ahren
|
|
*/
|
|
public class CommandInvoker {
|
|
|
|
private HashMap<Component, CommandInterface> commands;
|
|
private Stack <CommandInterface> undoStack;
|
|
|
|
public CommandInvoker(){
|
|
commands = new HashMap<>();
|
|
undoStack = new Stack();
|
|
}
|
|
|
|
/**
|
|
* Fügt ein Kommando zur Kommando-"Datenbank" = HashMap hinzu
|
|
* @param key Quelle des Events
|
|
* @param value Referenz auf das zugehörige Kommando-Objekt
|
|
*/
|
|
public void addCommand(Component key, CommandInterface value){
|
|
commands.put(key, value);
|
|
}
|
|
|
|
/**
|
|
* Führt Kommando der Eventquelle "key" aus und legt die Referenz
|
|
* des Kommando in den Undo-Stack
|
|
* @param key Referenz auf die Eventquelle
|
|
*/
|
|
public void executeCommand(Component key){
|
|
CommandInterface command = commands.get(key);
|
|
command.execute();
|
|
if (command.isUndoable())
|
|
{
|
|
undoStack.push(command);
|
|
}
|
|
}
|
|
/**
|
|
* Falls der Stack Elemente enthält, wird das oberste Element geholt
|
|
* und die Methode "undo" des Commands aufgerufen
|
|
*/
|
|
public void undoCommand()
|
|
{
|
|
if (!undoStack.isEmpty())
|
|
{
|
|
undoStack.pop().undo();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Löscht bei Öffnen einer neuen Datei den Stack
|
|
*/
|
|
public void deleteStack()
|
|
{
|
|
while(!undoStack.isEmpty())
|
|
undoStack.pop();
|
|
}
|
|
|
|
}
|