From eb0e6d0dbc71110ee1ffb3a5b1e749226e99ea75 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 08:43:19 +0200 Subject: [PATCH] Refactoring creating ClientMessageList Object in ClientUI --- .../client/ChatWindowController.java | 10 ++++----- .../multichat/client/ClientMessageList.java | 21 +++++++++++-------- .../zhaw/pm2/multichat/client/ClientUI.java | 5 +++++ 3 files changed, 22 insertions(+), 14 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 93b36de..51bc48f 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 @@ -41,7 +41,10 @@ public class ChatWindowController { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); stateChanged(NEW); - messages = new ClientMessageList(); + } + + public void setMessages(ClientMessageList messages) { + this.messages = messages; } private void applicationClose() { @@ -59,7 +62,7 @@ public class ChatWindowController { private void connect() { try { - messages = new ClientMessageList(); // clear message list + messages.clear(); // clear message list startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { @@ -196,7 +199,4 @@ public class ChatWindowController { } - - - } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java index 62d8ca0..402d19d 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java @@ -2,10 +2,9 @@ package ch.zhaw.pm2.multichat.client; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class ClientMessageList { - private final List messages = new ArrayList<>(); + private List messages = new ArrayList<>(); public void addMessage(Message message) { @@ -13,20 +12,24 @@ public class ClientMessageList { } public String getFilteredMessages(String filter) { - String result = null; + StringBuilder result = null; boolean showAll = filter == null || filter.isBlank(); for(Message message : messages) { if(showAll || message.matchesFilter(filter)) { - switch (message.getType()) { - case MESSAGE: result = result + String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()); break; - case ERROR: result = result + String.format("[ERROR] %s\n", message.getText()); break; - case INFO: result = result + String.format("[INFO] %s\n", message.getText()); break; - default: result = result + String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()); + switch (message.getType()) { + case MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText())); + case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText())); + case INFO -> result.append(String.format("[INFO] %s\n", message.getText())); + default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType())); } } } - return result; + return result.toString(); } + public void clear() { + messages = new ArrayList<>(); + } + } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java index 5f27276..2141237 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/ClientUI.java @@ -7,6 +7,7 @@ import javafx.scene.layout.Pane; import javafx.stage.Stage; public class ClientUI extends Application { + private ClientMessageList clientMessageList = new ClientMessageList(); @Override public void start(Stage primaryStage) { @@ -17,6 +18,10 @@ public class ClientUI extends Application { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml")); Pane rootPane = loader.load(); + + ChatWindowController chatWindowController = loader.getController(); + chatWindowController.setMessages(clientMessageList); + // fill in scene and stage setup Scene scene = new Scene(rootPane); //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());