From 00dd3a0c03d7ad39679eab8eaca5d49169758079 Mon Sep 17 00:00:00 2001 From: justADeni Date: Sat, 25 Mar 2023 19:33:46 +0100 Subject: [PATCH] solved some renaming related issues --- src/main/kotlin/actions/ButtonActions.kt | 16 +++- src/main/kotlin/actions/ListActions.kt | 111 ++++++++++++----------- src/main/kotlin/actions/RecipeRename.kt | 3 +- src/main/kotlin/misc/Saver.kt | 24 ++++- 4 files changed, 94 insertions(+), 60 deletions(-) diff --git a/src/main/kotlin/actions/ButtonActions.kt b/src/main/kotlin/actions/ButtonActions.kt index 2c7fc9a..d700aaa 100644 --- a/src/main/kotlin/actions/ButtonActions.kt +++ b/src/main/kotlin/actions/ButtonActions.kt @@ -64,19 +64,28 @@ class ButtonActions : ActionListener { recipeNum++ Inst.right.demoList.addElement("New Recipe #$recipeNum") - Inst.right.list.selectedIndex = Inst.right.demoList.size() - 1 + + //println("demoList.size() >>> " + Inst.right.demoList.size) + Inst.right.list.selectedIndex = Inst.right.demoList.size - 1 + + Saver.save() } Inst.right.remove -> { removing = true + //println("1") + val listIndex = Inst.right.list.selectedIndex if (listIndex != -1) { + //println("2") val file = File(Inst.loader.recipeFolder.toString() + "/" + Inst.right.demoList[listIndex] + ".chemrecipe") Saver.compoundDeleteCheck(file.toPath()) - if (file.exists()) + if (file.exists()) { + //println("3") file.delete() + } //Inst.right.demoList.remove(listIndex) //Inst.right.list.remove(listIndex) @@ -97,6 +106,9 @@ class ButtonActions : ActionListener { Inst.cEditor.reset() Inst.sEditor.reset() + + //println("4") + /* Saver.reloadall() diff --git a/src/main/kotlin/actions/ListActions.kt b/src/main/kotlin/actions/ListActions.kt index 7a162b5..3bdb773 100644 --- a/src/main/kotlin/actions/ListActions.kt +++ b/src/main/kotlin/actions/ListActions.kt @@ -74,6 +74,8 @@ class ListActions : ListSelectionListener { Inst.cEditor.reset() } current = new + + loadRecipe() } } else { justRemoved = false @@ -84,66 +86,67 @@ class ListActions : ListSelectionListener { //this loads the new recipe //println("file>>> " + Inst.loader.recipeFolder.toString() + "/" + Inst.right.demoList[current] + ".chemrecipe") - val file = File(Inst.loader.recipeFolder.toString() + "/" + Inst.right.demoList[current] + ".chemrecipe") - if (!file.exists()) { - withContext(Dispatchers.IO) { - file.createNewFile() + } + } + } + + fun loadRecipe(){ + val file = File(Inst.loader.recipeFolder.toString() + "/" + Inst.right.demoList[current] + ".chemrecipe") + + if (!file.exists()) { + + file.createNewFile() + return + } + + try { + BufferedReader(FileReader(file)).use { br -> + br.lines().forEach { + + if (it == "sEditor" || it == "cEditor") + return@forEach + + + val splot = it.split(":") + + if (splot.size == 1) + return@forEach + + val label: CustomLabel = Inst.left.getAsset(splot[1])?.copyHandler() ?: return@forEach + + if (splot[0] == "S") { + label.transferHandler = SEditorHandler() + label.addMouseListener(SEditorActions()) + Inst.sEditor.add(label, 10, splot[2].toInt()) + return@forEach + } + if (splot[0] == "C") { + label.transferHandler = CEditorHandler() + label.addMouseListener(CEditorActions()) + Inst.cEditor.add(label, 0, splot[2].toInt(), 0) + return@forEach } - return@launch - } - try { - withContext(Dispatchers.IO) { - BufferedReader(FileReader(file)).use { br -> - br.lines().forEach { - - if (it == "sEditor" || it == "cEditor") - return@forEach - - - val splot = it.split(":") - - if (splot.size == 1) - return@forEach - - val label: CustomLabel = Inst.left.getAsset(splot[1])?.copyHandler() ?: return@forEach - - if (splot[0] == "S") { - label.transferHandler = SEditorHandler() - label.addMouseListener(SEditorActions()) - Inst.sEditor.add(label, 10, splot[2].toInt()) - return@forEach - } - if (splot[0] == "C") { - label.transferHandler = CEditorHandler() - label.addMouseListener(CEditorActions()) - Inst.cEditor.add(label, 0, splot[2].toInt(), 0) - return@forEach - } - - val num = splot[0].toInt() - - if (num <= 9) { - label.transferHandler = SEditorHandler() - label.addMouseListener(SEditorActions()) - Inst.sEditor.add(label, num, splot[2].toInt()) - return@forEach - } else if (num > 9) { - label.transferHandler = CEditorHandler() - label.addMouseListener(CEditorActions()) - Inst.cEditor.add(label, num, splot[2].toInt(), splot[3].toInt()) - return@forEach - } - - } - Inst.refresh() - } + val num = splot[0].toInt() + + if (num <= 9) { + label.transferHandler = SEditorHandler() + label.addMouseListener(SEditorActions()) + Inst.sEditor.add(label, num, splot[2].toInt()) + return@forEach + } else if (num > 9) { + label.transferHandler = CEditorHandler() + label.addMouseListener(CEditorActions()) + Inst.cEditor.add(label, num, splot[2].toInt(), splot[3].toInt()) + return@forEach } - } catch (e: IOException) { - e.printStackTrace() + } + Inst.refresh() } + } catch (e: IOException) { + e.printStackTrace() } } } \ No newline at end of file diff --git a/src/main/kotlin/actions/RecipeRename.kt b/src/main/kotlin/actions/RecipeRename.kt index b7c031a..168858a 100644 --- a/src/main/kotlin/actions/RecipeRename.kt +++ b/src/main/kotlin/actions/RecipeRename.kt @@ -36,10 +36,11 @@ internal class RecipeRename : KeyAdapter() { Saver.save() - Saver.compoundLoadCheck(File(Inst.loader.recipeFolder.toString() + "/" + Inst.right.recipeName.text + ".chemrecipe").toPath()) + //Saver.compoundLoadCheck(File(Inst.loader.recipeFolder.toString() + "/" + Inst.right.recipeName.text + ".chemrecipe").toPath()) Inst.right.recipeName.text = "" } + } } } \ No newline at end of file diff --git a/src/main/kotlin/misc/Saver.kt b/src/main/kotlin/misc/Saver.kt index c07f104..37d3e78 100644 --- a/src/main/kotlin/misc/Saver.kt +++ b/src/main/kotlin/misc/Saver.kt @@ -2,6 +2,8 @@ package main.kotlin.misc import main.kotlin.tooltip.CustomLabel import java.io.File +import java.io.FileInputStream +import java.io.InputStream import java.io.PrintWriter import java.nio.file.Files import java.nio.file.Path @@ -50,6 +52,8 @@ object Saver { val newNum = labelNum + 10 file.appendText("$newNum:" + label.info + ":" + Inst.cEditor.getDecomposeLabelAmt(labelNum) + ":" + Inst.cEditor.getDecomposeLabelPctg(labelNum) + "\n") } + + compoundLoadCheck(file.toPath()) } fun save(listIndex : Int){ @@ -87,6 +91,8 @@ object Saver { val newNum = labelNum + 10 file.appendText("$newNum:" + label.info + ":" + Inst.cEditor.getDecomposeLabelAmt(labelNum) + ":" + Inst.cEditor.getDecomposeLabelPctg(labelNum) + "\n") } + + compoundLoadCheck(file.toPath()) } fun reloadall(){ @@ -136,13 +142,18 @@ object Saver { if (recipeName.contains("New Recipe")) return - for (compound in Inst.left.listchemassets) + for (compound in Inst.left.listchemassets) { + if (compound.toolTipText == recipeName) + compound.toolTipText = "Undefined " + compound.info if (compound.info == parts[1]) compound.toolTipText = recipeName - - for (compound in Inst.left.chemassetpanel.components) + } + for (compound in Inst.left.chemassetpanel.components) { + if ((compound as CustomLabel).toolTipText == recipeName) + compound.toolTipText = "Undefined " + compound.info if ((compound as CustomLabel).info == parts[1]) compound.toolTipText = recipeName + } val clabel = Inst.cEditor.getStartLabel() if (clabel != null) @@ -157,6 +168,9 @@ object Saver { fun compoundDeleteCheck(path: Path){ val recipeName = path.fileName.toString().split(".chemrecipe")[0] + if (isEmptyFile(path)) + return + val cLine : String = Files.lines(path).use { lines -> lines.skip(10).findFirst().get() } if (cLine == "S:") return @@ -176,4 +190,8 @@ object Saver { if ((compound as CustomLabel).info == parts[1]) compound.toolTipText = "Undefined Compound " + parts[1].replace("Compound ", "") } + + private fun isEmptyFile(path: Path): Boolean { + return path.toFile().readLines().isEmpty() + } } \ No newline at end of file