diff --git a/saveMessage.txt b/saveMessage.txt new file mode 100644 index 0000000..bc9d32d --- /dev/null +++ b/saveMessage.txt @@ -0,0 +1,6 @@ +Me: 1 +Me: 2 +Me: 3 +Me: 4 +Me: 5 +; diff --git a/src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java b/src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java index ef637c3..16bb676 100644 --- a/src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java +++ b/src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java @@ -12,6 +12,7 @@ import org.jxmpp.jid.impl.JidCreate; import team.elrant.bubbles.xmpp.ConnectedUser; +import java.io.File; import java.util.Objects; /** @@ -67,7 +68,6 @@ public void start(@NotNull Stage stage) throws Exception { throw new RuntimeException(e); } }); - AnchorPane root = fxmlLoader.load(); @NotNull Scene scene = new Scene(root, 800, 700); scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("styling/fluent-light.css")).toExternalForm()); @@ -76,9 +76,18 @@ public void start(@NotNull Stage stage) throws Exception { stage.centerOnScreen(); stage.setResizable(false); stage.show(); + stage.setOnCloseRequest(windowEvent -> { + try { + ChatViewController chatViewController = fxmlLoader.getController(); + chatViewController.saveMessage(); + } + catch (Exception e){ + logger.error("Error: {}",e.getMessage()); + } + }); } catch (Exception e) { logger.error("Error starting ChatViewApplication: {}", e.getMessage()); throw e; } } -} +} \ No newline at end of file diff --git a/src/main/java/team/elrant/bubbles/gui/ChatViewController.java b/src/main/java/team/elrant/bubbles/gui/ChatViewController.java index bf58ddb..dfef953 100644 --- a/src/main/java/team/elrant/bubbles/gui/ChatViewController.java +++ b/src/main/java/team/elrant/bubbles/gui/ChatViewController.java @@ -13,7 +13,9 @@ import org.jxmpp.jid.BareJid; import team.elrant.bubbles.xmpp.ConnectedUser; +import java.io.*; import java.security.Key; +import java.util.StringTokenizer; /** * The ChatViewController class controls the chat view functionality in the GUI. @@ -51,6 +53,9 @@ public ChatViewController(@NotNull ConnectedUser connectedUser, @NotNull BareJid protected void initialize() { chatTextArea.setStyle("-fx-background-color: Black;"); messageTextArea.setStyle("-fx-background-color: Black;"); + if(verifyFile() != 0){ + readFromFile(); + } connectedUser.addIncomingMessageListener(bareContactJid, this::updateChatDisplay); messageTextArea.setOnKeyPressed(event ->{ if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call sendMessage @@ -64,8 +69,45 @@ protected void initialize() { messageTextArea.setWrapText(true); chatTextArea.setEditable(false); chatTextArea.setWrapText(true); + + } + + protected int verifyFile(){ + File file = new File("saveMessage.txt"); + return (int)file.length(); } + protected void saveMessage(){ + try { + FileWriter f = new FileWriter("saveMessage.txt",false); + PrintWriter file = new PrintWriter(f); + file.println(chatTextArea.getText() + ";"); + f.flush(); + f.close(); + } + catch (IOException e){ + logger.error("Error To Save Message"); + } + } + protected void readFromFile(){ //Read from file and add to chatAreaText + try { + String line; + FileReader f = new FileReader("saveMessage.txt"); + BufferedReader file = new BufferedReader(f); + StringTokenizer tokenizer; + String string = file.readLine(); + while (string != null){ + tokenizer = new StringTokenizer(string,";"); + line = "\n" + tokenizer.nextToken(); + chatTextArea.appendText(string); + string = file.readLine(); + } + f.close(); + } + catch (IOException e){ + logger.error("Error To Read From File"); + } + } /** * Sends a message to the contact and updates the chat display. */