Refactoring ClientMessageList

This commit is contained in:
schrom01 2022-04-12 08:25:21 +02:00
parent e11fb136cf
commit 0896257fad
3 changed files with 24 additions and 41 deletions

View File

@ -41,7 +41,7 @@ public class ChatWindowController {
serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName()); serverAddressField.setText(NetworkHandler.DEFAULT_ADDRESS.getCanonicalHostName());
serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT)); serverPortField.setText(String.valueOf(NetworkHandler.DEFAULT_PORT));
stateChanged(NEW); stateChanged(NEW);
messages = new ClientMessageList(this); messages = new ClientMessageList();
} }
private void applicationClose() { private void applicationClose() {
@ -59,30 +59,30 @@ public class ChatWindowController {
private void connect() { private void connect() {
try { try {
messages = new ClientMessageList(this); // clear message list messages = new ClientMessageList(); // clear message list
startConnectionHandler(); startConnectionHandler();
connectionHandler.connect(); connectionHandler.connect();
} catch(ChatProtocolException | IOException e) { } catch(ChatProtocolException | IOException e) {
writeError(e.getMessage()); addError(e.getMessage());
} }
} }
private void disconnect() { private void disconnect() {
if (connectionHandler == null) { if (connectionHandler == null) {
writeError("No connection handler"); addError("No connection handler");
return; return;
} }
try { try {
connectionHandler.disconnect(); connectionHandler.disconnect();
} catch (ChatProtocolException e) { } catch (ChatProtocolException e) {
writeError(e.getMessage()); addError(e.getMessage());
} }
} }
@FXML @FXML
private void message() { private void message() {
if (connectionHandler == null) { if (connectionHandler == null) {
writeError("No connection handler"); addError("No connection handler");
return; return;
} }
String messageString = messageField.getText().strip(); String messageString = messageField.getText().strip();
@ -95,10 +95,10 @@ public class ChatWindowController {
try { try {
connectionHandler.message(receiver, message); connectionHandler.message(receiver, message);
} catch (ChatProtocolException e) { } catch (ChatProtocolException e) {
writeError(e.getMessage()); addError(e.getMessage());
} }
} else { } else {
writeError("Not a valid message format."); addError("Not a valid message format.");
} }
} }
@ -184,28 +184,11 @@ public class ChatWindowController {
this.redrawMessageList(); this.redrawMessageList();
} }
public void clearMessageArea() {
this.messageArea.clear();
}
private void redrawMessageList() { 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<WindowEvent> { class WindowCloseHandler implements EventHandler<WindowEvent> {
public void handle(WindowEvent event) { public void handle(WindowEvent event) {
applicationClose(); applicationClose();

View File

@ -6,33 +6,27 @@ import java.util.Objects;
public class ClientMessageList { public class ClientMessageList {
private final List<Message> messages = new ArrayList<>(); private final List<Message> messages = new ArrayList<>();
private final ChatWindowController gui;
public ClientMessageList(ChatWindowController gui) {
this.gui = gui;
}
public void addMessage(Message message) { public void addMessage(Message message) {
messages.add(message); messages.add(message);
} }
public void writeFilteredMessages(String filter) { public String getFilteredMessages(String filter) {
String result = null;
boolean showAll = filter == null || filter.isBlank(); boolean showAll = filter == null || filter.isBlank();
gui.clearMessageArea();
for(Message message : messages) { for(Message message : messages) {
if(showAll || if(showAll || message.matchesFilter(filter))
message.getSender().contains(filter) ||
message.getReceiver().contains(filter) ||
message.getText().contains(filter))
{ {
switch (message.getType()) { switch (message.getType()) {
case MESSAGE: gui.writeMessage(message.getSender(), message.getReceiver(), message.getText()); break; case MESSAGE: result = result + String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()); break;
case ERROR: gui.writeError(message.getText()); break; case ERROR: result = result + String.format("[ERROR] %s\n", message.getText()); break;
case INFO: gui.writeInfo(message.getText()); break; case INFO: result = result + String.format("[INFO] %s\n", message.getText()); break;
default: gui.writeError("Unexpected message type: " + message.getType()); default: result = result + String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType());
} }
} }
} }
return result;
} }
} }

View File

@ -13,6 +13,12 @@ public class Message {
this.text = text; this.text = text;
} }
public boolean matchesFilter(String filter){
return sender.contains(filter) ||
receiver.contains(filter) ||
text.contains(filter);
}
public MessageType getType() { public MessageType getType() {
return type; return type;
} }