Init mit Aufgabe10

This commit is contained in:
2023-12-18 10:24:38 +01:00
commit 22efa7c18f
22 changed files with 3188 additions and 0 deletions
@@ -0,0 +1,56 @@
/*
* 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;
import ChatProgramm.controller.commands.CommandConnect;
import ChatProgramm.controller.commands.CommandInvoker;
import ChatProgramm.controller.commands.CommandSend;
import ChatProgramm.view.ChatView;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author ahren
*/
public class CommandController implements ActionListener{
private ChatView view;
private CommandInvoker invoker;
public CommandController(ChatView view){
this.view = view;
this.invoker = new CommandInvoker();
}
public void registerEvents(){
view.getBtnConnect().addActionListener(this);
view.getTfNachricht().addActionListener(this);
}
public void registerCommands(){
CommandSend commandSend = new CommandSend(view);
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend));
invoker.addCommand(view.getTfNachricht(), commandSend);
}
/**
* Ausführen des jeweiligen Kommandos
* @param e Referenz auf das Event
*/
@Override
public void actionPerformed(ActionEvent e) {
Component key = (Component)e.getSource();
invoker.executeCommand(key);
// if(key == view.getBtnOpen()|| key==view.getMiOpen())
// invoker.deleteStack();
// }
}
}
@@ -0,0 +1,76 @@
/*
* 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 ChatProgramm.model.Client;
import ChatProgramm.model.Server;
import ChatProgramm.util.OhmLogger;
import ChatProgramm.view.ChatView;
import java.io.IOException;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JRadioButton;
/**
*
* @author ahren
*/
public class CommandConnect implements CommandInterface
{
private JRadioButton rBtnServer;
private JRadioButton rBtnClient;
private JDialog dialogFenster;
private static Logger lg = OhmLogger.getLogger();
private CommandSend commandSend;
private ChatView view;
public CommandConnect(ChatView view, CommandInterface value)
{
rBtnServer = view.getBtnServer();
rBtnClient = view.getBtnClient();
dialogFenster = view.getjDialog1();
commandSend = (CommandSend) value;
this.view = view;
}
@Override
public void execute()
{
if(rBtnServer.isSelected()){
lg.info("Server ausgewählt");
try {
commandSend.transmitterInterface = new Server(view);
} catch (IOException ex) {
lg.info("Die Verbindung zum Server ist Fehlgeschlagen");
}
}
if(rBtnClient.isSelected()){
lg.info("Client ausgewählt");
try {
commandSend.transmitterInterface = new Client(view);
} catch (IOException ex) {
lg.info("Die Verbindung zum Client ist Fehlgeschlagen");
}
}
dialogFenster.setVisible(false);
}
@Override
public boolean isUndoable()
{
return false;
}
@Override
public void undo()
{
}
}
@@ -0,0 +1,17 @@
/*
* 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;
/**
*
* @author ahren
*/
public interface CommandInterface
{
public void execute();
public void undo();
public boolean isUndoable();
}
@@ -0,0 +1,69 @@
/*
* 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();
}
}
@@ -0,0 +1,61 @@
/*
* 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 ChatProgramm.model.Client;
import ChatProgramm.model.Server;
import ChatProgramm.model.TransmitterInterface;
import ChatProgramm.util.OhmLogger;
import ChatProgramm.view.ChatView;
import java.util.logging.Logger;
import javax.swing.JTextField;
import ChatProgramm.model.Nachricht;
/**
*
* @author ahren
*/
public class CommandSend implements CommandInterface
{
private static Logger lg = OhmLogger.getLogger();
private JTextField eingabeFeld;
private String nachricht;
private ChatView view;
public TransmitterInterface transmitterInterface;
public Server server;
public Client client;
public CommandSend(ChatView view)
{
this.view = view;
this.eingabeFeld = view.getTfNachricht();
transmitterInterface = null;
}
@Override
public void execute()
{
if(transmitterInterface != null && !eingabeFeld.getText().isEmpty()){
transmitterInterface.send(eingabeFeld.getText());
eingabeFeld.setText("");
}
}
@Override
public boolean isUndoable()
{
return false;
}
@Override
public void undo()
{
}
}