From 0896257fad3a8a13416ae5ab6515775f8ac93c5f Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 12 Apr 2022 08:25:21 +0200 Subject: [PATCH] Refactoring ClientMessageList --- .../client/ChatWindowController.java | 37 +++++-------------- .../multichat/client/ClientMessageList.java | 22 ++++------- .../ch/zhaw/pm2/multichat/client/Message.java | 6 +++ 3 files changed, 24 insertions(+), 41 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 bc7e57c..93b36de 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,7 @@ public class ChatWindowController { serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); stateChanged(NEW); - messages = new ClientMessageList(this); + messages = new ClientMessageList(); } private void applicationClose() { @@ -59,30 +59,30 @@ public class ChatWindowController { private void connect() { try { - messages = new ClientMessageList(this); // clear message list + messages = new ClientMessageList(); // clear message list startConnectionHandler(); connectionHandler.connect(); } catch(ChatProtocolException | IOException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } private void disconnect() { if (connectionHandler == null) { - writeError("No connection handler"); + addError("No connection handler"); return; } try { connectionHandler.disconnect(); } catch (ChatProtocolException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } @FXML private void message() { if (connectionHandler == null) { - writeError("No connection handler"); + addError("No connection handler"); return; } String messageString = messageField.getText().strip(); @@ -95,10 +95,10 @@ public class ChatWindowController { try { connectionHandler.message(receiver, message); } catch (ChatProtocolException e) { - writeError(e.getMessage()); + addError(e.getMessage()); } } else { - writeError("Not a valid message format."); + addError("Not a valid message format."); } } @@ -184,28 +184,11 @@ public class ChatWindowController { this.redrawMessageList(); } - public void clearMessageArea() { - this.messageArea.clear(); - } - - private void redrawMessageList() { - Platform.runLater(() -> messages.writeFilteredMessages(filterValue.getText().strip())); + this.messageArea.clear(); + Platform.runLater(() -> this.messageArea.appendText(messages.getFilteredMessages(filterValue.getText().strip()))); } - public void writeError(String message) { - this.messageArea.appendText(String.format("[ERROR] %s\n", message)); - } - - public void writeInfo(String message) { - this.messageArea.appendText(String.format("[INFO] %s\n", message)); - } - - public void writeMessage(String sender, String reciever, String message) { - this.messageArea.appendText(String.format("[%s -> %s] %s\n", sender, reciever, message)); - } - - class WindowCloseHandler implements EventHandler { public void handle(WindowEvent event) { applicationClose(); 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 55dec4d..62d8ca0 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 @@ -6,33 +6,27 @@ import java.util.Objects; public class ClientMessageList { private final List messages = new ArrayList<>(); - private final ChatWindowController gui; - public ClientMessageList(ChatWindowController gui) { - this.gui = gui; - } public void addMessage(Message message) { messages.add(message); } - public void writeFilteredMessages(String filter) { + public String getFilteredMessages(String filter) { + String result = null; boolean showAll = filter == null || filter.isBlank(); - gui.clearMessageArea(); for(Message message : messages) { - if(showAll || - message.getSender().contains(filter) || - message.getReceiver().contains(filter) || - message.getText().contains(filter)) + if(showAll || message.matchesFilter(filter)) { switch (message.getType()) { - case MESSAGE: gui.writeMessage(message.getSender(), message.getReceiver(), message.getText()); break; - case ERROR: gui.writeError(message.getText()); break; - case INFO: gui.writeInfo(message.getText()); break; - default: gui.writeError("Unexpected message type: " + 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()); } } } + return result; } } diff --git a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java index ea41cd7..c1ed9c6 100644 --- a/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java +++ b/client/src/main/java/ch/zhaw/pm2/multichat/client/Message.java @@ -13,6 +13,12 @@ public class Message { this.text = text; } + public boolean matchesFilter(String filter){ + return sender.contains(filter) || + receiver.contains(filter) || + text.contains(filter); + } + public MessageType getType() { return type; }