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());
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<WindowEvent> {
public void handle(WindowEvent event) {
applicationClose();

View File

@ -6,33 +6,27 @@ import java.util.Objects;
public class ClientMessageList {
private final List<Message> 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;
}
}

View File

@ -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;
}