diff --git a/ChatApp/app/src/main/java/client/ClientGuiController.java b/ChatApp/app/src/main/java/client/ClientGuiController.java
index 7154678..6bd72f2 100644
--- a/ChatApp/app/src/main/java/client/ClientGuiController.java
+++ b/ChatApp/app/src/main/java/client/ClientGuiController.java
@@ -16,6 +16,8 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class ClientGuiController implements ClientListener {
@@ -92,15 +94,14 @@ public void initialize() throws SocketException {
String timeString = formatter.format(time);
Text chatterText = new Text(chatter + ": ");
chatterText.setStyle("-fx-fill: black; -fx-font-weight: bold;");
- Text messageText = new Text(content);
- messageText.setStyle("-fx-fill: black;");
+ TextFlow messageText = formatMessage(chatter, content);
TextFlow wholeMessage = new TextFlow(chatterText, messageText);
Text timeText = new Text(" " + timeString);
timeText.setStyle("-fx-fill: gray; -fx-font-size: 10px;");
HBox messageContainer = new HBox(5, wholeMessage, timeText);
messageContainer.setStyle("-fx-background-color: lightblue; -fx-padding: 8; -fx-background-radius: 5;");
messageContainer.setMaxWidth(Double.MAX_VALUE);
- HBox.setHgrow(messageText, javafx.scene.layout.Priority.ALWAYS);
+ HBox.setHgrow(messageText, Priority.ALWAYS);
chatList.getChildren().add(messageContainer);
});
}
@@ -121,8 +122,6 @@ public void initialize() throws SocketException {
}
private void createChatEnv(String roomName) {
-//
-//
Platform.runLater(() -> {
chatList.getChildren().clear();
chatInput.getChildren().clear();
@@ -154,6 +153,43 @@ public void initialize() throws SocketException {
});
}
+ private TextFlow formatMessage(String chatter, String content) {
+ TextFlow textFlow = new TextFlow();
+ Pattern pattern = Pattern.compile("&([rbgyn])([^&]*)");
+ Matcher matcher = pattern.matcher(content);
+ int lastIndex = 0;
+
+ while (matcher.find()) {
+ if (matcher.start() > lastIndex) {
+ textFlow.getChildren().add(new Text(content.substring(lastIndex, matcher.start())));
+ }
+
+ String colorCode = matcher.group(1);
+ String textPart = matcher.group(2);
+
+ String color = switch (colorCode) {
+ case "r" -> "red";
+ case "b" -> "blue";
+ case "g" -> "green";
+ case "y" -> "gray";
+ default -> "black";
+ };
+
+ Text coloredText = new Text(textPart);
+ coloredText.setStyle("-fx-fill: " + color + ";");
+ textFlow.getChildren().add(coloredText);
+
+ lastIndex = matcher.end();
+ }
+
+ // Add the remaining text (works if there's no color code at all)
+ if (lastIndex < content.length()) {
+ textFlow.getChildren().add(new Text(content.substring(lastIndex)));
+ }
+
+ return textFlow;
+ }
+
@Override
public void handleServerResponse(ServerResponsePacket.Response response) {