diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java index dfccf24..3c46ca6 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/chat/ChatController.java @@ -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; diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatBoxController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatBoxController.java index 1493e33..c4fe68f 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatBoxController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatBoxController.java @@ -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 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); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatViewController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatViewController.java index b3b4966..145a43e 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatViewController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/casono/client/ui/chatui/ChatViewController.java @@ -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() { diff --git a/src/main/resources/ui-structure/components/chatui/chatbox.fxml b/src/main/resources/ui-structure/components/chatui/chatbox.fxml index ff9c451..1d54aed 100644 --- a/src/main/resources/ui-structure/components/chatui/chatbox.fxml +++ b/src/main/resources/ui-structure/components/chatui/chatbox.fxml @@ -4,8 +4,9 @@ - + +