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 26aa545..46bfe4f 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 @@ -1,6 +1,7 @@ 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.ChatKey; 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; @@ -20,7 +21,6 @@ import javafx.scene.control.MenuButton; import javafx.scene.control.MenuItem; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; -import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; @@ -30,16 +30,10 @@ public class ChatBoxController { private final ChatController chatController; - @FXML private VBox chatBox; - @FXML private TabPane chatTabPane; @FXML private MenuButton addWhisperChatButton; - @FXML private HBox menuBox; - - private FXMLLoader fxmlLoader; - private final List activeWhisperChats; @FXML private final Map usernameTabMap; @@ -76,7 +70,7 @@ public class ChatBoxController { ChatModel globalChatModel = new ChatModel(global, username, -1, null); chatController.getChatModelMap().put(new ChatController.ChatKey(global), globalChatModel); addChatTab("GLOBAL", globalChatModel, global); - addWhisperChatButton.setOnAction(event -> addWhisperChatButton.show()); + addWhisperChatButton.setOnAction(_ -> addWhisperChatButton.show()); } /** @@ -96,7 +90,7 @@ public class ChatBoxController { MenuItem menuItem = new MenuItem(targetUserName); addWhisperChatButton.getItems().add(menuItem); menuItem.setOnAction( - event -> + _ -> addWhisperChat( targetUserName, new ChatModel( @@ -130,7 +124,7 @@ public class ChatBoxController { if (oldUsername.equals(item.getText())) { item.setText(newUsername); item.setOnAction( - event -> + _ -> addWhisperChat( newUsername, new ChatModel( @@ -165,11 +159,21 @@ public class ChatBoxController { if (!activeWhisperChats.contains(target)) { activeWhisperChats.add(target); ChatType whisper = ChatType.WHISPER; - chatController - .getChatModelMap() - .put(new ChatController.ChatKey(whisper, target), chatModel); - addChatTab(target, chatModel, whisper); + chatController.getChatModelMap().put(new ChatKey(whisper, target), chatModel); + ChatViewController chatViewController = addChatTab(target, chatModel, whisper); + Tab chatTab = chatViewController.getChatTab(); + chatTab.setOnCloseRequest( + _ -> { + chatViewController.tabIsOpen = false; + chatTabPane.getTabs().remove(chatTab); + }); } 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)); } } @@ -183,30 +187,40 @@ public class ChatBoxController { * @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. */ - 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"; URL resource = getClass().getResource(ressource); FXMLLoader fxmlLoader = new FXMLLoader(resource); - runOnPlatformSynchronized( + return runOnPlatformSynchronized( () -> { try { ChatViewController chatViewController = new ChatViewController( - this.chatController, chatModel, this.username); - chatController.activeChatControllers.put( - new ChatController.ChatKey(chatType), chatViewController); + this.chatController, chatModel, this); + + ChatKey chatKey; + if (chatType.equals(ChatType.WHISPER)) { + chatKey = new ChatKey(chatType, title); + } else { + chatKey = new ChatKey(chatType); + } + chatController.activeChatControllers.put(chatKey, chatViewController); fxmlLoader.setController(chatViewController); Node load = fxmlLoader.load(); VBox.setVgrow(load, Priority.ALWAYS); VBox vbox = new VBox(); VBox.setVgrow(vbox, Priority.ALWAYS); vbox.getChildren().add(load); - chatModel.addListener((msg) -> chatViewController.showMessage(msg)); + chatModel.addListener(chatViewController::showMessage); Tab newChat = new Tab(title, vbox); usernameTabMap.put(title, newChat); - this.chatTabPane.getTabs().add(newChat); + chatTabPane.getTabs().add(newChat); this.chatTabPane.getSelectionModel().select(newChat); - return newChat; + chatViewController.setChatTab(newChat); + if (!chatType.equals(ChatType.WHISPER)) { + newChat.setClosable(false); + } + return chatViewController; } catch (IOException e) { throw new RuntimeException(e); } @@ -267,4 +281,12 @@ public class ChatBoxController { } } } + + public void reopenChatTab(Tab chatTab) { + Platform.runLater( + () -> { + chatTabPane.getTabs().add(chatTab); + chatTabPane.getSelectionModel().select(chatTab); + }); + } } 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 2bad460..fc9bff8 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 @@ -11,37 +11,48 @@ import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; +import javafx.scene.control.Tab; import javafx.scene.control.TextField; -import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; /** Responsible for the presentation of the ChatModel to the Client */ public class ChatViewController implements Initializable { + private final ChatBoxController chatBoxController; @FXML private Button sendButton; @FXML private TextField inputField; - @FXML private VBox chatInterfaceVBox; - - @FXML private HBox controlBar; - @FXML private ScrollPane scrollPane; @FXML private VBox chat; - private final ChatModel chatModel; + public Tab getChatTab() { + return chatTab; + } - private final String username; + public void setChatTab(Tab chatTab) { + this.chatTab = chatTab; + } + + @FXML private Tab chatTab; + + public Boolean tabIsOpen; + + private final ChatModel chatModel; private final ChatController controller; private static final int CHAT_PADDING = 20; - public ChatViewController(ChatController chatController, ChatModel chatModel, String username) { + public ChatViewController( + ChatController chatController, + ChatModel chatModel, + ChatBoxController chatBoxController) { this.controller = chatController; - this.username = username; this.chatModel = chatModel; + this.chatBoxController = chatBoxController; + tabIsOpen = true; } /** @@ -54,8 +65,8 @@ public class ChatViewController implements Initializable { */ @Override public void initialize(URL location, ResourceBundle resourceBundle) { - inputField.setOnAction(event -> sendMessage()); - sendButton.setOnAction(event -> sendMessage()); + inputField.setOnAction(_ -> sendMessage()); + sendButton.setOnAction(_ -> sendMessage()); scrollPane.vvalueProperty().bind(chat.heightProperty()); } @@ -88,6 +99,10 @@ public class ChatViewController implements Initializable { * @param msg The {@link Message} object containing the content and metadata to display. */ public void showMessage(Message msg) { + if (!tabIsOpen) { + tabIsOpen = true; + chatBoxController.reopenChatTab(chatTab); + } Platform.runLater( () -> { String msgText = diff --git a/src/main/resources/ui-structure/components/chatui/chatbox.fxml b/src/main/resources/ui-structure/components/chatui/chatbox.fxml index e88c640..2a6c526 100644 --- a/src/main/resources/ui-structure/components/chatui/chatbox.fxml +++ b/src/main/resources/ui-structure/components/chatui/chatbox.fxml @@ -3,13 +3,14 @@ + - + styleClass="tab-pane"> diff --git a/src/main/resources/ui-structure/components/chatui/chattab.fxml b/src/main/resources/ui-structure/components/chatui/chattab.fxml index ce3bf95..7db08e1 100644 --- a/src/main/resources/ui-structure/components/chatui/chattab.fxml +++ b/src/main/resources/ui-structure/components/chatui/chattab.fxml @@ -8,7 +8,7 @@