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 9eac20a..1a8a9c6 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 @@ -4,6 +4,8 @@ import ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import javafx.application.Platform; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -39,11 +41,11 @@ public class ChatWindowController { public void initialize() { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); - stateChanged(NEW); } public void setMessages(ClientMessageList messages) { this.messages = messages; + messageListener(); } private void applicationClose() { @@ -52,7 +54,7 @@ public class ChatWindowController { @FXML private void toggleConnection () { - if (connectionHandler == null || connectionHandler.getState() != CONNECTED) { + if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) { connect(); } else { disconnect(); @@ -62,7 +64,6 @@ public class ChatWindowController { private void connect() { try { messages.clear(); // clear message list - redrawMessageList(); startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { @@ -109,9 +110,12 @@ public class ChatWindowController { int serverPort = Integer.parseInt(serverPortField.getText()); connectionHandler = new ClientConnectionHandler( NetworkHandler.openConnection(serverAddress, serverPort), userName, - this); + messages); new Thread(connectionHandler).start(); + //register Listener + startListener(); + // register window close handler rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); } @@ -165,19 +169,8 @@ public class ChatWindowController { }); } - public void addMessage(String sender, String receiver, String message) { - messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, message)); - this.redrawMessageList(); - } - - public void addInfo(String message) { - messages.addMessage(new Message(Message.MessageType.INFO, null, null, message)); - this.redrawMessageList(); - } - public void addError(String message) { messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message)); - this.redrawMessageList(); } private void redrawMessageList() { @@ -192,4 +185,43 @@ public class ChatWindowController { } + public void startListener() { + connectionHandler.getStateProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, State oldValue, State newValue) { + stateChanged(newValue); + } + }); + + connectionHandler.getUserNameProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + setUserName(newValue); + } + }); + + connectionHandler.getServerAddressProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + setServerAddress(newValue); + } + }); + + connectionHandler.getServerPortProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Number oldValue, Number newValue) { + setServerPort(newValue.intValue()); + } + }); + } + + private void messageListener() { + messages.getChangedProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { + redrawMessageList(); + } + }); + } + } 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 22e4ce2..9da83da 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 @@ -2,6 +2,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.SimpleIntegerProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; import java.io.EOFException; import java.io.IOException; @@ -14,7 +17,6 @@ import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*; public class ClientConnectionHandler implements Runnable { private final NetworkHandler.NetworkConnection connection; - private final ChatWindowController controller; // Data types used for the Chat Protocol private static final String DATA_TYPE_CONNECT = "CONNECT"; @@ -28,8 +30,11 @@ public class ClientConnectionHandler implements Runnable { private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" ); - private String userName = USER_NONE; - private State state = NEW; + private SimpleStringProperty userName; + private SimpleObjectProperty state; + private ClientMessageList messages; + private SimpleStringProperty serverAddress; + private SimpleIntegerProperty serverPort; enum State { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; @@ -37,19 +42,28 @@ public class ClientConnectionHandler implements Runnable { public ClientConnectionHandler(NetworkHandler.NetworkConnection connection, String userName, - ChatWindowController controller) { + ClientMessageList messages) { this.connection = connection; - this.userName = (userName == null || userName.isBlank())? USER_NONE : userName; - this.controller = controller; + this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName); + this.messages = messages; + state = new SimpleObjectProperty<>(NEW); + serverAddress = new SimpleStringProperty(); + serverPort = new SimpleIntegerProperty(); } - public State getState() { + public SimpleStringProperty getServerAddressProperty() { return serverAddress; } + + public SimpleIntegerProperty getServerPortProperty() { return serverPort; } + + public SimpleObjectProperty getStateProperty() { return this.state; } + public SimpleStringProperty getUserNameProperty() { return userName; } + public void setState (State newState) { - this.state = newState; - controller.stateChanged(newState); + state.set(newState); + } public void run () { @@ -123,45 +137,44 @@ public class ClientConnectionHandler implements Runnable { if (type.equals(DATA_TYPE_CONNECT)) { System.err.println("Illegal connect request from server"); } else if (type.equals(DATA_TYPE_CONFIRM)) { - if (state == CONFIRM_CONNECT) { - this.userName = reciever; - controller.setUserName(userName); - controller.setServerPort(connection.getRemotePort()); - controller.setServerAddress(connection.getRemoteHost()); - controller.addInfo(payload); + if (state.get() == CONFIRM_CONNECT) { + this.userName.set(reciever); + this.serverPort.set(connection.getRemotePort()); + this.serverAddress.set(connection.getRemoteHost()); + messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload)); System.out.println("CONFIRM: " + payload); this.setState(CONNECTED); - } else if (state == CONFIRM_DISCONNECT) { - controller.addInfo(payload); + } else if (state.get() == CONFIRM_DISCONNECT) { + messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload)); System.out.println("CONFIRM: " + payload); this.setState(DISCONNECTED); } else { System.err.println("Got unexpected confirm message: " + payload); } } else if (type.equals(DATA_TYPE_DISCONNECT)) { - if (state == DISCONNECTED) { + if (state.get() == DISCONNECTED) { System.out.println("DISCONNECT: Already in disconnected: " + payload); return; } - controller.addInfo(payload); + messages.addMessage(new Message(Message.MessageType.INFO,sender,reciever,payload)); System.out.println("DISCONNECT: " + payload); this.setState(DISCONNECTED); } else if (type.equals(DATA_TYPE_MESSAGE)) { - if (state != CONNECTED) { + if (state.get() != CONNECTED) { System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload); return; } - controller.addMessage(sender, reciever, payload); + messages.addMessage(new Message(Message.MessageType.MESSAGE,sender,reciever,payload)); System.out.println("MESSAGE: From " + sender + " to " + reciever + ": "+ payload); } else if (type.equals(DATA_TYPE_ERROR)) { - controller.addError(payload); + messages.addMessage(new Message(Message.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 +200,19 @@ public class ClientConnectionHandler implements Runnable { } public void connect() throws ChatProtocolException { - if (state != NEW) throw new ChatProtocolException("Illegal state for connect: " + state); - this.sendData(userName, USER_NONE, DATA_TYPE_CONNECT,null); + if (state.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + state); + this.sendData(userName.get(), USER_NONE, DATA_TYPE_CONNECT,null); this.setState(CONFIRM_CONNECT); } public void disconnect() throws ChatProtocolException { - if (state != NEW && state != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state); - this.sendData(userName, USER_NONE, DATA_TYPE_DISCONNECT,null); + if (state.get() != NEW && state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for disconnect: " + state); + this.sendData(userName.get(), USER_NONE, DATA_TYPE_DISCONNECT,null); this.setState(CONFIRM_DISCONNECT); } public boolean message(String messageString) throws ChatProtocolException { - if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); + if (state.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); Matcher matcher = messagePattern.matcher(messageString); if (matcher.find()) { @@ -209,7 +222,7 @@ public class ClientConnectionHandler implements Runnable { return false; } if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL; - this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); + this.sendData(userName.get(), receiver, DATA_TYPE_MESSAGE,message); return true; } else { return false; 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 a811406..7105c5b 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,14 +1,17 @@ package ch.zhaw.pm2.multichat.client; +import javafx.beans.property.SimpleBooleanProperty; + import java.util.ArrayList; import java.util.List; public class ClientMessageList { private List messages = new ArrayList<>(); - + private SimpleBooleanProperty changed = new SimpleBooleanProperty(false); public void addMessage(Message message) { messages.add(message); + changed.set(!changed.get()); } public String getFilteredMessages(String filter) { @@ -30,6 +33,9 @@ public class ClientMessageList { public void clear() { messages = new ArrayList<>(); + changed.set(!changed.get()); } + public SimpleBooleanProperty getChangedProperty() { return changed; } + }