Refactoring creating ClientMessageList Object in ClientUI
This commit is contained in:
parent
0896257fad
commit
eb0e6d0dbc
|
@ -41,7 +41,10 @@ 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();
|
}
|
||||||
|
|
||||||
|
public void setMessages(ClientMessageList messages) {
|
||||||
|
this.messages = messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applicationClose() {
|
private void applicationClose() {
|
||||||
|
@ -59,7 +62,7 @@ public class ChatWindowController {
|
||||||
|
|
||||||
private void connect() {
|
private void connect() {
|
||||||
try {
|
try {
|
||||||
messages = new ClientMessageList(); // clear message list
|
messages.clear(); // clear message list
|
||||||
startConnectionHandler();
|
startConnectionHandler();
|
||||||
connectionHandler.connect();
|
connectionHandler.connect();
|
||||||
} catch(ChatProtocolException | IOException e) {
|
} catch(ChatProtocolException | IOException e) {
|
||||||
|
@ -196,7 +199,4 @@ public class ChatWindowController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,9 @@ package ch.zhaw.pm2.multichat.client;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class ClientMessageList {
|
public class ClientMessageList {
|
||||||
private final List<Message> messages = new ArrayList<>();
|
private List<Message> messages = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
public void addMessage(Message message) {
|
public void addMessage(Message message) {
|
||||||
|
@ -13,20 +12,24 @@ public class ClientMessageList {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFilteredMessages(String filter) {
|
public String getFilteredMessages(String filter) {
|
||||||
String result = null;
|
StringBuilder result = null;
|
||||||
boolean showAll = filter == null || filter.isBlank();
|
boolean showAll = filter == null || filter.isBlank();
|
||||||
for(Message message : messages) {
|
for(Message message : messages) {
|
||||||
if(showAll || message.matchesFilter(filter))
|
if(showAll || message.matchesFilter(filter))
|
||||||
{
|
{
|
||||||
switch (message.getType()) {
|
switch (message.getType()) {
|
||||||
case MESSAGE: result = result + String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()); break;
|
case MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
|
||||||
case ERROR: result = result + String.format("[ERROR] %s\n", message.getText()); break;
|
case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText()));
|
||||||
case INFO: result = result + String.format("[INFO] %s\n", message.getText()); break;
|
case INFO -> result.append(String.format("[INFO] %s\n", message.getText()));
|
||||||
default: result = result + String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType());
|
default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
messages = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import javafx.scene.layout.Pane;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
public class ClientUI extends Application {
|
public class ClientUI extends Application {
|
||||||
|
private ClientMessageList clientMessageList = new ClientMessageList();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) {
|
public void start(Stage primaryStage) {
|
||||||
|
@ -17,6 +18,10 @@ public class ClientUI extends Application {
|
||||||
try {
|
try {
|
||||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml"));
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatWindow.fxml"));
|
||||||
Pane rootPane = loader.load();
|
Pane rootPane = loader.load();
|
||||||
|
|
||||||
|
ChatWindowController chatWindowController = loader.getController();
|
||||||
|
chatWindowController.setMessages(clientMessageList);
|
||||||
|
|
||||||
// fill in scene and stage setup
|
// fill in scene and stage setup
|
||||||
Scene scene = new Scene(rootPane);
|
Scene scene = new Scene(rootPane);
|
||||||
//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
|
//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
|
||||||
|
|
Loading…
Reference in New Issue