Add: First implementation of Button, to create Whisper Chat

This commit is contained in:
Mathis Ginkel
2026-04-09 18:02:13 +02:00
parent c0c9225d42
commit e8401a976a
8 changed files with 72 additions and 257 deletions
@@ -3,6 +3,7 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ChatClient;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui.ChatBoxController;
import javafx.application.Platform;
import org.jspecify.annotations.Nullable;
import java.util.Timer;
@@ -55,8 +56,10 @@ public class ChatController {
new TimerTask() {
@Override
public void run() {
clientService.ping();
receiveMessage();
Platform.runLater(() -> {
clientService.ping();
receiveMessage();
});
}
},
0,
@@ -67,6 +70,7 @@ public class ChatController {
this.lobbyId = lobbyId;
ChatModel lobbyChatModel = new ChatModel(ChatType.LOBBY, username, lobbyId, null);
chatModelMap.put(new ChatKey(ChatType.LOBBY), lobbyChatModel);
this.chatBoxController.addChatTab("Lobby", lobbyChatModel);
}
/** method to get all messages from the server */
@@ -89,9 +93,7 @@ public class ChatController {
if (chatModelMap.containsKey(new ChatKey(ChatType.WHISPER, msg.sender))) {
chatModelMap.get(new ChatKey(ChatType.WHISPER, msg.sender)).addMessage(msg);
} else {
ChatModel value = new ChatModel(ChatType.WHISPER, username, -1, msg.sender);
chatModelMap.put(new ChatKey(ChatType.WHISPER, msg.sender), value);
chatBoxController.addWhisperChat(msg.sender, value);
chatBoxController.addWhisperChat(msg.sender);
}
}
break;
@@ -5,12 +5,16 @@ import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class ChatBoxController {
@@ -22,8 +26,12 @@ public class ChatBoxController {
@FXML private TabPane ChatTabPane;
@FXML private MenuButton addWhisperChatButton;
private FXMLLoader fxmlLoader;
@FXML private List<MenuItem> whisperUsers;
private String ressource = "/ui-structure/components/chatui/chattab.fxml";
@@ -41,7 +49,16 @@ public class ChatBoxController {
}
public void addWhisperChat(String target, ChatModel chatModel) {
public void addWhisperUser(String targetUserName) {
MenuItem menuItem = new MenuItem(targetUserName);
whisperUsers.add(menuItem);
addWhisperChatButton.getItems().add(menuItem);
menuItem.setOnAction(event -> addWhisperChat(targetUserName));
}
public void addWhisperChat(String target) {
ChatModel chatModel = new ChatModel(ChatType.WHISPER, username, -1, target);
chatController.getChatModelMap().put(new ChatController.ChatKey(ChatType.WHISPER, target), chatModel);
addChatTab(target, chatModel);
}
@@ -50,11 +67,12 @@ public class ChatBoxController {
public void addChatTab(String title, ChatModel chatModel) {
URL resource = getClass().getResource(ressource);
FXMLLoader fxmlLoader = new FXMLLoader(resource);
ChatViewController chatViewController = new ChatViewController(username, chatModel, chatController);
chatModel.addListener((msg)-> chatViewController.showMessage(msg));
try {
ChatViewController chatViewController = new ChatViewController(this.chatController, chatModel, this.username);
fxmlLoader.setController(chatViewController);
Tab newChat = new Tab(title, fxmlLoader.load());
Node load = fxmlLoader.load();
chatModel.addListener((msg)-> chatViewController.showMessage(msg));
Tab newChat = new Tab(title, load);
this.ChatTabPane.getTabs().add(newChat);
} catch (IOException e) {
throw new RuntimeException(e);
@@ -38,23 +38,17 @@ public class ChatViewController implements Initializable {
private static final int CHAT_PADDING = 20;
public ChatViewController() {
this(null, null, null);
}
public ChatViewController(
String username, ChatModel chatModel, ChatController controller) {
public ChatViewController(ChatController chatController, ChatModel chatModel, String username){
this.controller = chatController;
this.username = username;
this.controller = controller;
this.chatModel = chatModel;
}
@Override
public void initialize(URL location, ResourceBundle resourceBundle) {
inputField.setOnAction(event -> sendMessage());
sendButton.setOnAction(event -> sendMessage());
scrollPane.vvalueProperty().bind(chatInterfaceVBox.heightProperty());
scrollPane.vvalueProperty().bind(chat.heightProperty());
}
public void sendMessage() {