From 2b01e92e8d24e5083974257383905d89a4e106b4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 15 Apr 2022 15:34:03 +0200 Subject: [PATCH 1/2] sovled #42 --- .../client/ChatWindowController.java | 22 +++++++------------ .../client/ClientConnectionHandler.java | 9 +++++--- 2 files changed, 14 insertions(+), 17 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 5bde847..2f680ce 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 @@ -2,8 +2,6 @@ package ch.zhaw.pm2.multichat.client; import ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State; import ch.zhaw.pm2.multichat.protocol.ChatProtocolException; -import ch.zhaw.pm2.multichat.protocol.ConnectionHandler; -import ch.zhaw.pm2.multichat.protocol.NetworkHandler; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; @@ -38,8 +36,7 @@ public class ChatWindowController { @FXML public void initialize() { - serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); - serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); + } public void setMessages(ClientMessageList messages) { @@ -49,6 +46,9 @@ public class ChatWindowController { public void setConnectionHandler(ClientConnectionHandler connectionHandler){ this.connectionHandler = connectionHandler; + startConnectionHandlerListener(); + serverAddressField.setText(connectionHandler.getServerAddressProperty().get()); + serverPortField.setText(String.valueOf(connectionHandler.getServerPortProperty().get())); } private void applicationClose() { @@ -104,7 +104,7 @@ public class ChatWindowController { @FXML private void applyFilter( ) { - this.redrawMessageList(); + Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip()))); } private void startConnectionHandler() throws IOException { @@ -116,7 +116,7 @@ public class ChatWindowController { new Thread(connectionHandler).start(); //register Listener - startListener(); + //startConnectionHandlerListener(); // register window close handler rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); @@ -169,19 +169,13 @@ public class ChatWindowController { messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message)); } - private void redrawMessageList() { - this.messageArea.clear(); - Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip()))); - } - class WindowCloseHandler implements EventHandler { public void handle(WindowEvent event) { applicationClose(); } - } - public void startListener() { + public void startConnectionHandlerListener() { connectionHandler.getStateProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, State oldValue, State newValue) { @@ -215,7 +209,7 @@ public class ChatWindowController { messages.getChangedProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { - redrawMessageList(); + Platform.runLater(() -> messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip()))); } }); } 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 983c34c..f67ceb0 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 @@ -29,12 +29,15 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab super(); this.messages = messages; state = new SimpleObjectProperty<>(State.NEW); - serverAddress = new SimpleStringProperty(); - serverPort = new SimpleIntegerProperty(); + serverAddress = new SimpleStringProperty(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); + serverPort = new SimpleIntegerProperty(NetworkHandler.DEFAULT_PORT); + this.userName = new SimpleStringProperty(null); } public void initialize(String serverAddress, int serverPort, String userName) throws IOException { - state = new SimpleObjectProperty<>(NEW); + state.set(NEW); + this.serverAddress.set(serverAddress); + this.serverPort.set(serverPort); setConnection(NetworkHandler.openConnection(serverAddress, serverPort)); this.userName = new SimpleStringProperty((userName == null || userName.isBlank())? USER_NONE : userName); } From 2b70299507e586519bfee6129ce19ed983621efa Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 15 Apr 2022 15:35:04 +0200 Subject: [PATCH 2/2] removed Method initialize because it's not used anymore. --- .../ch/zhaw/pm2/multichat/client/ChatWindowController.java | 5 ----- 1 file changed, 5 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 2f680ce..b780b49 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 @@ -34,11 +34,6 @@ public class ChatWindowController { @FXML private TextField filterValue; - @FXML - public void initialize() { - - } - public void setMessages(ClientMessageList messages) { this.messages = messages; messageListener();