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 b48a249..9cb8888 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; @@ -40,7 +42,6 @@ public class ChatWindowController { public void initialize() { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); - stateChanged(NEW); messages = new ClientMessageList(this); } @@ -50,7 +51,7 @@ public class ChatWindowController { @FXML private void toggleConnection () { - if (connectionHandler == null || connectionHandler.getState() != CONNECTED) { + if (connectionHandler == null || connectionHandler.getStateObjectProperty().get() != CONNECTED) { connect(); } else { disconnect(); @@ -117,6 +118,9 @@ public class ChatWindowController { // register window close handler rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); + + //register changelistener + startChangeListener(); } private void terminateConnectionHandler() { @@ -141,6 +145,16 @@ public class ChatWindowController { } } + private void startChangeListener() { + connectionHandler.getStateObjectProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, State oldValue, State newValue) { + stateChanged(newValue); + } + }); + + } + public void setUserName(String userName) { Platform.runLater(new Runnable() { @Override @@ -211,8 +225,4 @@ public class ChatWindowController { } } - - - - } 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 156010d..049c5bf 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,8 @@ 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.SimpleObjectProperty; import java.io.EOFException; import java.io.IOException; @@ -25,7 +27,7 @@ public class ClientConnectionHandler implements Runnable { public static final String USER_ALL = "*"; private String userName = USER_NONE; - private State state = NEW; + private ObjectProperty stateObjectProperty; enum State { NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED; @@ -37,15 +39,16 @@ public class ClientConnectionHandler implements Runnable { this.connection = connection; this.userName = (userName == null || userName.isBlank())? USER_NONE : userName; this.controller = controller; + this.stateObjectProperty = new SimpleObjectProperty<>(NEW); } - public State getState() { - return this.state; + + public ObjectProperty getStateObjectProperty() { + return stateObjectProperty; } public void setState (State newState) { - this.state = newState; - controller.stateChanged(newState); + this.stateObjectProperty.set(newState); } public void run () { @@ -119,7 +122,7 @@ 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) { + if (stateObjectProperty.get() == CONFIRM_CONNECT) { this.userName = reciever; controller.setUserName(userName); controller.setServerPort(connection.getRemotePort()); @@ -127,7 +130,7 @@ public class ClientConnectionHandler implements Runnable { controller.addInfo(payload); System.out.println("CONFIRM: " + payload); this.setState(CONNECTED); - } else if (state == CONFIRM_DISCONNECT) { + } else if (stateObjectProperty.get() == CONFIRM_DISCONNECT) { controller.addInfo(payload); System.out.println("CONFIRM: " + payload); this.setState(DISCONNECTED); @@ -135,7 +138,7 @@ public class ClientConnectionHandler implements Runnable { System.err.println("Got unexpected confirm message: " + payload); } } else if (type.equals(DATA_TYPE_DISCONNECT)) { - if (state == DISCONNECTED) { + if (stateObjectProperty.get() == DISCONNECTED) { System.out.println("DISCONNECT: Already in disconnected: " + payload); return; } @@ -143,8 +146,8 @@ public class ClientConnectionHandler implements Runnable { System.out.println("DISCONNECT: " + payload); this.setState(DISCONNECTED); } else if (type.equals(DATA_TYPE_MESSAGE)) { - if (state != CONNECTED) { - System.out.println("MESSAGE: Illegal state " + state + " for message: " + payload); + if (stateObjectProperty.get() != CONNECTED) { + System.out.println("MESSAGE: Illegal state " + stateObjectProperty.get() + " for message: " + payload); return; } controller.addMessage(sender, reciever, payload); @@ -183,19 +186,19 @@ public class ClientConnectionHandler implements Runnable { } public void connect() throws ChatProtocolException { - if (state != NEW) throw new ChatProtocolException("Illegal state for connect: " + state); + if (stateObjectProperty.get() != NEW) throw new ChatProtocolException("Illegal state for connect: " + stateObjectProperty.get()); this.sendData(userName, 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); + 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.setState(CONFIRM_DISCONNECT); } public void message(String receiver, String message) throws ChatProtocolException { - if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state); + if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get()); this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message); }