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 30cd50c..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,12 +2,9 @@ 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; -import javafx.collections.FXCollections; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -26,33 +23,20 @@ public class ChatWindowController { private final WindowCloseHandler windowCloseHandler = new WindowCloseHandler(); - @FXML - private Pane rootPane; - @FXML - private TextField serverAddressField; - @FXML - private TextField serverPortField; - @FXML - private TextField userNameField; - @FXML - private TextField messageField; - @FXML - private TextArea messageArea; - @FXML - private Button connectButton; - @FXML - private Button sendButton; - @FXML - private TextField filterValue; + @FXML private Pane rootPane; + @FXML private TextField serverAddressField; + @FXML private TextField serverPortField; + @FXML private TextField userNameField; + @FXML private TextField messageField; + @FXML private TextArea messageArea; + @FXML private Button connectButton; + @FXML private Button sendButton; + @FXML private TextField filterValue; - private ChatWindowModel model = new ChatWindowModel(); @FXML public void initialize() { - serverAddressField.setText(model.getServerAddress()); // binding server address to view - model.setServerAddress(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); // sets modell adress - serverPortField.setText(model.getServerPort()); - model.setServerPort(String.valueOf(NetworkHandler.DEFAULT_PORT)); + } public void setMessages(ClientMessageList messages) { @@ -60,8 +44,11 @@ public class ChatWindowController { messageListener(); } - public void setConnectionHandler(ClientConnectionHandler connectionHandler) { + public void setConnectionHandler(ClientConnectionHandler connectionHandler){ this.connectionHandler = connectionHandler; + startConnectionHandlerListener(); + serverAddressField.setText(connectionHandler.getServerAddressProperty().get()); + serverPortField.setText(String.valueOf(connectionHandler.getServerPortProperty().get())); } private void applicationClose() { @@ -69,7 +56,7 @@ public class ChatWindowController { } @FXML - private void toggleConnection() { + private void toggleConnection () { if (connectionHandler == null || connectionHandler.getStateProperty().get() != CONNECTED) { connect(); } else { @@ -82,7 +69,7 @@ public class ChatWindowController { messages.clear(); // clear message list startConnectionHandler(); connectionHandler.connect(); - } catch (ChatProtocolException | IOException e) { + } catch(ChatProtocolException | IOException e) { addError(e.getMessage()); } } @@ -116,20 +103,20 @@ public class ChatWindowController { } @FXML - private void applyFilter() { - this.redrawMessageList(); + private void applyFilter( ) { + Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip()))); } private void startConnectionHandler() throws IOException { String userName = userNameField.getText(); - if (!userName.contains(" ")) { + if(!userName.contains(" ")) { String serverAddress = serverAddressField.getText(); int serverPort = Integer.parseInt(serverPortField.getText()); connectionHandler.initialize(serverAddress, serverPort, userName); new Thread(connectionHandler).start(); //register Listener - startListener(); + //startConnectionHandlerListener(); // register window close handler rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler); @@ -146,7 +133,7 @@ public class ChatWindowController { connectButton.setText((newState == CONNECTED || newState == CONFIRM_DISCONNECT) ? "Disconnect" : "Connect"); } }); - if (newState == DISCONNECTED) { + if(newState == DISCONNECTED){ connectionHandler.stopReceiving(); } } @@ -182,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) { @@ -228,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); }