Solved #48 make it impossible to send messages if not connected in ChatWindow.

This commit is contained in:
schrom01 2022-04-16 20:27:29 +02:00
parent b67b9c84aa
commit 11fdf8715e
1 changed files with 12 additions and 4 deletions

View File

@ -46,7 +46,6 @@ public class ChatWindowController {
@FXML @FXML
private Button sendButton; private Button sendButton;
/** /**
* Takes a message object and stores it used as Model and also starts message Listener via messageListener method. * 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(); startConnectionHandlerListener();
serverAddressField.setText(connectionHandler.getServerAddressProperty().get()); serverAddressField.setText(connectionHandler.getServerAddressProperty().get());
serverPortField.setText(String.valueOf(connectionHandler.getServerPortProperty().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. * @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()) // update UI (need to be run in UI thread: see Platform.runLater())
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
public void run() { 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) { if (newState == DISCONNECTED) {
@ -255,7 +263,7 @@ public class ChatWindowController {
connectionHandler.getStateProperty().addListener(new ChangeListener<State>() { connectionHandler.getStateProperty().addListener(new ChangeListener<State>() {
@Override @Override
public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) { public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
stateChanged(newValue); refreshConnectionState(newValue);
} }
}); });