From 11fdf8715e972f3d6e6fb64c80c83904154d9b29 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sat, 16 Apr 2022 20:27:29 +0200 Subject: [PATCH] Solved #48 make it impossible to send messages if not connected in ChatWindow. --- .../multichat/client/ChatWindowController.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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 c09d7ae..2bf0951 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 @@ -46,7 +46,6 @@ public class ChatWindowController { @FXML private Button sendButton; - /** * Takes a message object and stores it used as Model and also starts message Listener via messageListener method. * @@ -67,6 +66,7 @@ public class ChatWindowController { startConnectionHandlerListener(); serverAddressField.setText(connectionHandler.getServerAddressProperty().get()); serverPortField.setText(String.valueOf(connectionHandler.getServerPortProperty().get())); + refreshConnectionState(connectionHandler.getStateProperty().get()); } /** @@ -170,12 +170,20 @@ public class ChatWindowController { * * @param newState is the state that it should be set to. */ - public void stateChanged(State newState) { + public void refreshConnectionState(State newState) { // update UI (need to be run in UI thread: see Platform.runLater()) Platform.runLater(new Runnable() { @Override public void run() { - connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect"); + if(newState == CONNECTED || newState == CONFIRM_DISCONNECT){ + connectButton.setText("Disconnect"); + messageField.setDisable(false); + sendButton.setDisable(false); + } else { + connectButton.setText("Connect"); + messageField.setDisable(true); + sendButton.setDisable(true); + } } }); if (newState == DISCONNECTED) { @@ -255,7 +263,7 @@ public class ChatWindowController { connectionHandler.getStateProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, State oldValue, State newValue) { - stateChanged(newValue); + refreshConnectionState(newValue); } });