Skip to content

Commit

Permalink
solved some renaming related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
justADeni committed Mar 25, 2023
1 parent dcc5be7 commit 00dd3a0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 60 deletions.
16 changes: 14 additions & 2 deletions src/main/kotlin/actions/ButtonActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -97,6 +106,9 @@ class ButtonActions : ActionListener {

Inst.cEditor.reset()
Inst.sEditor.reset()

//println("4")

/*
Saver.reloadall()
Expand Down
111 changes: 57 additions & 54 deletions src/main/kotlin/actions/ListActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class ListActions : ListSelectionListener {
Inst.cEditor.reset()
}
current = new

loadRecipe()
}
} else {
justRemoved = false
Expand All @@ -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()
}
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/actions/RecipeRename.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
}

}
}
}
24 changes: 21 additions & 3 deletions src/main/kotlin/misc/Saver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()
}
}

0 comments on commit 00dd3a0

Please sign in to comment.