Feat/chat tests #317

Merged
m.ginkel merged 14 commits from feat/chat-tests into main 2026-04-27 00:52:31 +02:00
2 changed files with 60 additions and 13 deletions
Showing only changes of commit 1b18affd57 - Show all commits
@@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui; package ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController.ChatKey;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message; import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
@@ -167,9 +168,19 @@ public class ChatBoxController {
ChatType whisper = ChatType.WHISPER; ChatType whisper = ChatType.WHISPER;
chatController chatController
.getChatModelMap() .getChatModelMap()
.put(new ChatController.ChatKey(whisper, target), chatModel); .put(new ChatKey(whisper, target), chatModel);
addChatTab(target, chatModel, whisper); ChatViewController chatViewController = addChatTab(target, chatModel, whisper);
Tab chatTab = chatViewController.getChatTab();
chatTab.setOnCloseRequest(_ -> {
chatViewController.tabIsOpen = false;
chatTabPane.getTabs().remove(chatTab);
});
} else { } else {
ChatViewController chatViewController = chatController.activeChatControllers.get(new ChatKey(ChatType.WHISPER, target));
if (!chatViewController.tabIsOpen) {
reopenChatTab(chatViewController.getChatTab());
chatViewController.tabIsOpen = true;
}
chatTabPane.getSelectionModel().select(usernameTabMap.get(target)); chatTabPane.getSelectionModel().select(usernameTabMap.get(target));
} }
} }
@@ -183,18 +194,25 @@ public class ChatBoxController {
* @param chatModel The {@link ChatModel} containing the data and logic for this specific chat. * @param chatModel The {@link ChatModel} containing the data and logic for this specific chat.
* @throws RuntimeException If the FXML resource for the chat tab cannot be loaded. * @throws RuntimeException If the FXML resource for the chat tab cannot be loaded.
*/ */
public void addChatTab(String title, ChatModel chatModel, ChatType chatType) { public ChatViewController addChatTab(String title, ChatModel chatModel, ChatType chatType) {
String ressource = "/ui-structure/components/chatui/chattab.fxml"; String ressource = "/ui-structure/components/chatui/chattab.fxml";
URL resource = getClass().getResource(ressource); URL resource = getClass().getResource(ressource);
FXMLLoader fxmlLoader = new FXMLLoader(resource); FXMLLoader fxmlLoader = new FXMLLoader(resource);
runOnPlatformSynchronized( return runOnPlatformSynchronized(
() -> { () -> {
try { try {
ChatViewController chatViewController = ChatViewController chatViewController =
new ChatViewController( new ChatViewController(
this.chatController, chatModel, this.username); this.chatController, chatModel, this.username, this);
ChatKey chatKey;
if (chatType.equals(ChatType.WHISPER)) {
chatKey = new ChatKey(chatType, title);
} else {
chatKey = new ChatKey(chatType);
}
chatController.activeChatControllers.put( chatController.activeChatControllers.put(
new ChatController.ChatKey(chatType), chatViewController); chatKey, chatViewController);
fxmlLoader.setController(chatViewController); fxmlLoader.setController(chatViewController);
Node load = fxmlLoader.load(); Node load = fxmlLoader.load();
VBox.setVgrow(load, Priority.ALWAYS); VBox.setVgrow(load, Priority.ALWAYS);
@@ -204,9 +222,13 @@ public class ChatBoxController {
chatModel.addListener((msg) -> chatViewController.showMessage(msg)); chatModel.addListener((msg) -> chatViewController.showMessage(msg));
Tab newChat = new Tab(title, vbox); Tab newChat = new Tab(title, vbox);
usernameTabMap.put(title, newChat); usernameTabMap.put(title, newChat);
this.chatTabPane.getTabs().add(newChat); chatTabPane.getTabs().add(newChat);
this.chatTabPane.getSelectionModel().select(newChat); this.chatTabPane.getSelectionModel().select(newChat);
return newChat; chatViewController.setChatTab(newChat);
if (!chatType.equals(ChatType.WHISPER)) {
newChat.setClosable(false);
}
return chatViewController;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -267,4 +289,11 @@ public class ChatBoxController {
} }
} }
} }
public void reopenChatTab(Tab chatTab) {
Platform.runLater(() -> {
chatTabPane.getTabs().add(chatTab);
chatTabPane.getSelectionModel().select(chatTab);
});
}
} }
@@ -2,22 +2,22 @@ package ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message; import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
import java.net.URL; import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
/** Responsible for the presentation of the ChatModel to the Client */ /** Responsible for the presentation of the ChatModel to the Client */
public class ChatViewController implements Initializable { public class ChatViewController implements Initializable {
private final ChatBoxController chatBoxController;
@FXML private Button sendButton; @FXML private Button sendButton;
@FXML private TextField inputField; @FXML private TextField inputField;
@@ -30,6 +30,18 @@ public class ChatViewController implements Initializable {
@FXML private VBox chat; @FXML private VBox chat;
public Tab getChatTab() {
return chatTab;
}
public void setChatTab(Tab chatTab) {
this.chatTab = chatTab;
}
@FXML private Tab chatTab;
public Boolean tabIsOpen;
private final ChatModel chatModel; private final ChatModel chatModel;
private final String username; private final String username;
@@ -38,10 +50,12 @@ public class ChatViewController implements Initializable {
private static final int CHAT_PADDING = 20; private static final int CHAT_PADDING = 20;
public ChatViewController(ChatController chatController, ChatModel chatModel, String username) { public ChatViewController(ChatController chatController, ChatModel chatModel, String username, ChatBoxController chatBoxController) {
this.controller = chatController; this.controller = chatController;
this.username = username; this.username = username;
this.chatModel = chatModel; this.chatModel = chatModel;
this.chatBoxController = chatBoxController;
tabIsOpen = true;
} }
/** /**
@@ -88,6 +102,10 @@ public class ChatViewController implements Initializable {
* @param msg The {@link Message} object containing the content and metadata to display. * @param msg The {@link Message} object containing the content and metadata to display.
*/ */
public void showMessage(Message msg) { public void showMessage(Message msg) {
if (!tabIsOpen) {
tabIsOpen = true;
chatBoxController.reopenChatTab(chatTab);
}
Platform.runLater( Platform.runLater(
() -> { () -> {
String msgText = String msgText =