From 815c6f50af53f0290a7733aaceb844de4a533aa0 Mon Sep 17 00:00:00 2001
From: inseo <i32kim@uwaterloo.ca>
Date: Thu, 16 Feb 2023 21:20:02 -0500
Subject: [PATCH] Load, Save, Delete event handlers have been implemented. All
 features thoroughly tested

---
 .../kotlin/notes/multi/console/Console.kt     |  8 +--
 console/test/hello.txt                        |  1 +
 .../notes/multi/utilities/Filemanager.kt      |  5 +-
 .../notes/multi/utilities/NoteTextWindow.kt   | 58 ++++++++++++++++++-
 4 files changed, 63 insertions(+), 9 deletions(-)
 create mode 100644 console/test/hello.txt

diff --git a/console/src/main/kotlin/notes/multi/console/Console.kt b/console/src/main/kotlin/notes/multi/console/Console.kt
index 9ae4ea5..3412f38 100644
--- a/console/src/main/kotlin/notes/multi/console/Console.kt
+++ b/console/src/main/kotlin/notes/multi/console/Console.kt
@@ -8,14 +8,14 @@ import notes.multi.utilities.Filemanager
 import notes.multi.utilities.Note
 import notes.multi.utilities.TextWindow
 import javafx.application.Application
+import javafx.stage.Stage
 
 fun main() {
     // thesea re test functions feel free to use
     val n = Note()
-    val noteTitle = n.title
-    val noteContent = n.text.toString()
-    println(noteContent)
-    Application.launch(TextWindow()::class.java, "--title=${noteTitle}", "--text=${noteContent}")
+    val noteTitle = "testver2.txt"
+    val path = "${System.getProperty("user.dir")}/test/"
+    Application.launch(TextWindow()::class.java, "--title=${noteTitle}", "--text=${path}")
 
     // val f = Filemanager("${System.getProperty("user.dir")}/test/", "hello.txt")
     // f.writefile("hello world")
diff --git a/console/test/hello.txt b/console/test/hello.txt
new file mode 100644
index 0000000..beb9354
--- /dev/null
+++ b/console/test/hello.txt
@@ -0,0 +1 @@
+"dude this is annoying"
\ No newline at end of file
diff --git a/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt b/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt
index 2551ace..ffff0db 100644
--- a/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt
+++ b/utilities/src/main/kotlin/notes/multi/utilities/Filemanager.kt
@@ -3,7 +3,7 @@ package notes.multi.utilities
 import java.io.File
 import java.io.InputStream
 
-class Filemanager(private val dir: String, private val name: String) {
+class Filemanager(private val dir: String, val name: String) {
     private val directory = File(dir)
     private val filepath = File("$dir/$name")
     private val listfiles = mutableListOf<File>()
@@ -41,9 +41,6 @@ class Filemanager(private val dir: String, private val name: String) {
 
     fun deletefile():Boolean {
         listfiles.remove(filepath)
-        for (i in listfiles) {
-            println("Not Deleted!")
-        }
         return filepath.delete()
     }
 
diff --git a/utilities/src/main/kotlin/notes/multi/utilities/NoteTextWindow.kt b/utilities/src/main/kotlin/notes/multi/utilities/NoteTextWindow.kt
index e8fafe2..4c2b14a 100644
--- a/utilities/src/main/kotlin/notes/multi/utilities/NoteTextWindow.kt
+++ b/utilities/src/main/kotlin/notes/multi/utilities/NoteTextWindow.kt
@@ -1,4 +1,5 @@
 package notes.multi.utilities
+import notes.multi.utilities.Filemanager
 
 import javafx.application.Application
 import javafx.stage.Stage
@@ -8,10 +9,17 @@ import javafx.scene.control.TextArea
 import javafx.scene.layout.VBox
 import javafx.scene.layout.AnchorPane
 import javafx.application.Application.Parameters
+import javafx.application.Platform
+import javafx.scene.control.Alert
+import javafx.scene.control.ButtonType
+import javafx.scene.input.KeyCode
 import javafx.scene.layout.Priority
 
 class TextWindow(): Application() {
     var paramsMap = mutableMapOf<String, String>()
+
+
+    var controlpressed = false
     override fun init() {
         super.init()
         val params = getParameters()
@@ -19,9 +27,12 @@ class TextWindow(): Application() {
     }
 
     override fun start(stage: Stage) {
+        val path = paramsMap["text"]!!
+        val filecontroller = Filemanager(path, paramsMap["title"]!!)
         stage.setTitle(paramsMap["title"])
         val textarea = TextArea()
-        textarea.setText(paramsMap["text"])
+        //textarea.setText(paramsMap["text"])
+        textarea.setText(filecontroller.openfile())
         textarea.setWrapText(true)
         val scroll = ScrollPane()
         val anchor = AnchorPane(textarea)
@@ -45,6 +56,51 @@ class TextWindow(): Application() {
         VBox.setVgrow(anchor, Priority.ALWAYS)
 
         stage.scene = Scene(box, 300.0, 300.0)
+
+
+        stage.scene.setOnKeyPressed { event->
+            if (event.code == KeyCode.CONTROL) {
+                controlpressed = true
+            } else if (event.code == KeyCode.S && controlpressed) {
+                val warning = Alert(Alert.AlertType.CONFIRMATION)
+                warning.title = "SAVE"
+                warning.contentText = "Do you want to save this file?"
+                val result = warning.showAndWait()
+                if (result.isPresent) {
+                    when (result.get()) {
+                        ButtonType.OK -> filecontroller.writefile(textarea.getText())
+                    }
+                }
+            } else if (event.code == KeyCode.D && controlpressed) {
+                val warning = Alert(Alert.AlertType.CONFIRMATION)
+                warning.title = "DELETE"
+                warning.contentText = "Do you delete this file?"
+                val result = warning.showAndWait()
+                if (result.isPresent) {
+                    when (result.get()) {
+                        ButtonType.OK -> {filecontroller.deletefile()
+                            Platform.exit()}
+                    }
+                }
+            } else if (event.code == KeyCode.W && controlpressed) {
+                val warning = Alert(Alert.AlertType.CONFIRMATION)
+                warning.title = "WARNING"
+                warning.contentText = "The current work will not be saved. Are you sure you want to quit?"
+                val result = warning.showAndWait()
+                if (result.isPresent) {
+                    when (result.get()) {
+                        ButtonType.OK -> {
+                            Platform.exit()
+                        }
+                    }
+                }
+            }
+        }
+
+        stage.scene.setOnKeyReleased { event->
+            if (controlpressed) {controlpressed = false}
+        }
+
         stage.show()
     }
 }
\ No newline at end of file
-- 
GitLab