diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java index 9cb8888..29afc08 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java @@ -42,7 +42,14 @@ public class ChatWindowController { public void initialize() { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); - messages = new ClientMessageList(this); + } + + public void chatWindowController(ClientMessageList messages,ClientConnectionHandler connectionHandler) { + this.connectionHandler = connectionHandler; + this.messages = messages; + + //register changelistener + startChangeListener(); } private void applicationClose() { @@ -111,16 +118,12 @@ public class ChatWindowController { String userName = userNameField.getText(); String serverAddress = serverAddressField.getText(); int serverPort = Integer.parseInt(serverPortField.getText()); - connectionHandler = new ClientConnectionHandler( - NetworkHandler.openConnection(serverAddress, serverPort), userName, - this); + connectionHandler.openConnection(serverAddress,serverPort,userName); new Thread(connectionHandler).start(); // register window close handler rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); - //register changelistener - startChangeListener(); } private void terminateConnectionHandler() { @@ -146,6 +149,7 @@ public class ChatWindowController { } private void startChangeListener() { + //Listener for State connectionHandler.getStateObjectProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, State oldValue, State newValue) { @@ -153,6 +157,29 @@ public class ChatWindowController { } }); + //Listener for Username + connectionHandler.getUserNameProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + setUserName(newValue); + } + }); + + //Listener for Address + connectionHandler.getServerAddressProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + setServerAddress(newValue); + } + }); + + //Listener for Port + connectionHandler.getServerPortProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Number oldValue, Number newValue) { + setServerPort(newValue.intValue()); + } + }); } public void setUserName(String userName) { diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java index 049c5bf..6c8663c 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java @@ -3,7 +3,9 @@ package ch.zhaw.pm2.multichat.client; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; import java.io.EOFException; import java.io.IOException; @@ -13,8 +15,7 @@ import java.util.Scanner; import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; public class ClientConnectionHandler implements Runnable { - private final NetworkHandler.NetworkConnection connection; - private final ChatWindowController controller; + private NetworkHandler.NetworkConnection connection; // Data types used for the Chat Protocol private static final String DATA_TYPE_CONNECT = "CONNECT"; @@ -26,27 +27,46 @@ public class ClientConnectionHandler implements Runnable { public static final String USER_NONE = ""; public static final String USER_ALL = "*"; - private String userName = USER_NONE; + private SimpleStringProperty userName; + private SimpleIntegerProperty serverPort; + private SimpleStringProperty serverAddress; private ObjectProperty stateObjectProperty; + private ClientMessageList messageList; enum State { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; } - public ClientConnectionHandler(NetworkHandler.NetworkConnection connection, - String userName, - ChatWindowController controller) { - this.connection = connection; - this.userName = (userName == null || userName.isBlank())? USER_NONE : userName; - this.controller = controller; + public ClientConnectionHandler(ClientMessageList messageList) { this.stateObjectProperty = new SimpleObjectProperty<>(NEW); + connection = null; + userName = new SimpleStringProperty(USER_NONE); + serverPort = new SimpleIntegerProperty(); + serverAddress = new SimpleStringProperty(); + this.messageList = messageList; } + public void openConnection(String serverAddress, int serverPort, String userName) throws IOException{ + connection = NetworkHandler.openConnection(serverAddress,serverPort); + this.userName.set((userName == null || userName.isBlank())? USER_NONE : userName); + } public ObjectProperty getStateObjectProperty() { return stateObjectProperty; } + public SimpleStringProperty getUserNameProperty() { + return userName; + } + + public SimpleStringProperty getServerAddressProperty() { + return serverAddress; + } + + public SimpleIntegerProperty getServerPortProperty() { + return serverPort; + } + public void setState (State newState) { this.stateObjectProperty.set(newState); } @@ -123,15 +143,16 @@ public class ClientConnectionHandler implements Runnable { System.err.println("Illegal connect request from server"); } else if (type.equals(DATA_TYPE_CONFIRM)) { if (stateObjectProperty.get() == CONFIRM_CONNECT) { - this.userName = reciever; - controller.setUserName(userName); - controller.setServerPort(connection.getRemotePort()); - controller.setServerAddress(connection.getRemoteHost()); - controller.addInfo(payload); + this.userName.set(reciever); + controller.addInfo(payload);//TODO: Make Changelistener + messageList.addMessage(ClientMessageList.MessageType.INFO,sender,reciever,payload); + System.out.println("CONFIRM: " + payload); this.setState(CONNECTED); } else if (stateObjectProperty.get() == CONFIRM_DISCONNECT) { - controller.addInfo(payload); + controller.addInfo(payload);//TODO: Make Changelistener + messageList.addMessage(ClientMessageList.MessageType.INFO,sender,reciever,payload); + System.out.println("CONFIRM: " + payload); this.setState(DISCONNECTED); } else { @@ -143,6 +164,8 @@ public class ClientConnectionHandler implements Runnable { return; } controller.addInfo(payload); + messageList.addMessage(ClientMessageList.MessageType.INFO,sender,reciever,payload); + System.out.println("DISCONNECT: " + payload); this.setState(DISCONNECTED); } else if (type.equals(DATA_TYPE_MESSAGE)) { @@ -151,16 +174,20 @@ public class ClientConnectionHandler implements Runnable { return; } controller.addMessage(sender, reciever, payload); + messageList.addMessage(ClientMessageList.MessageType.MESSAGE,sender,reciever,payload); + System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload); } else if (type.equals(DATA_TYPE_ERROR)) { controller.addError(payload); + messageList.addMessage(ClientMessageList.MessageType.ERROR,sender,reciever,payload); + System.out.println("ERROR: " + payload); } else { System.out.println("Unknown data type received: " + type); } } catch (ChatProtocolException e) { System.err.println("Error while processing data: " + e.getMessage()); - sendData(USER_NONE, userName, DATA_TYPE_ERROR, e.getMessage()); + sendData(USER_NONE, userName.get(), DATA_TYPE_ERROR, e.getMessage()); } } @@ -187,19 +214,19 @@ public class ClientConnectionHandler implements Runnable { public void connect() throws ChatProtocolException { if (stateObjectProperty.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + stateObjectProperty.get()); - this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null); + this.sendData(userName.get(), USER_NONE, DATA_TYPE_CONNECT,null); this.setState(CONFIRM_CONNECT); } public void disconnect() throws ChatProtocolException { if (stateObjectProperty.get() != NEW && stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + stateObjectProperty.get()); - this.sendData(userName, USER_NONE, DATA_TYPE_DISCONNECT,null); + this.sendData(userName.get(), USER_NONE, DATA_TYPE_DISCONNECT,null); this.setState(CONFIRM_DISCONNECT); } public void message(String receiver, String message) throws ChatProtocolException { if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get()); - this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); + this.sendData(userName.get(), receiver, DATA_TYPE_MESSAGE,message); } } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 100cb67..7359b9f 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -1,5 +1,7 @@ package ch.zhaw.pm2.multichat.client; +import javafx.beans.property.SimpleListProperty; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -8,9 +10,10 @@ public class ClientMessageList { private final List typeList = new ArrayList<>(); private final List senderList = new ArrayList<>(); private final List receiverList = new ArrayList<>(); - private final List messageList = new ArrayList<>(); + private final SimpleListProperty messageList = new SimpleListProperty<>(); private final ChatWindowController gui; + public ClientMessageList(ChatWindowController gui) { this.gui = gui; } @@ -44,6 +47,10 @@ public class ClientMessageList { } } + public SimpleListProperty getMessageListProperty() { + return messageList; + } + public enum MessageType { INFO, MESSAGE, ERROR; } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java index 5f27276..10d4237 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java @@ -16,6 +16,14 @@ public class ClientUI extends Application { private void chatWindow(Stage primaryStage) { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml")); + + // set up Controller + + ChatWindowController controller = loader.getController(); + ClientMessageList messageList = new ClientMessageList(controller); + ClientConnectionHandler connectionHandler = new ClientConnectionHandler(messageList); + controller.chatWindowController(messageList,connectionHandler); + Pane rootPane = loader.load(); // fill in scene and stage setup Scene scene = new Scene(rootPane);