Refactoring creating ClientMessageList Object in ClientUI

This commit is contained in:
schrom01 2022-04-12 08:43:19 +02:00
parent 0896257fad
commit eb0e6d0dbc
3 changed files with 22 additions and 14 deletions

View File

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

View File

@ -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<Message> messages = new ArrayList<>();
private List<Message> 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());
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<>();
}
}

View File

@ -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());