Now saves the most recently used config folder path and reuses that

This commit is contained in:
ceikry 2021-08-02 11:19:50 -05:00
parent 62988a7f4f
commit d59e8decc5
2 changed files with 45 additions and 1 deletions

View file

@ -0,0 +1,39 @@
import org.json.simple.JSONObject
import org.json.simple.parser.JSONParser
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.lang.Exception
object DataStore {
val savePath = File(System.getProperty("user.home") + File.separator + ".thanos_tool")
var LastConfigPath: String = System.getProperty("user.home")
fun parse() {
if(!savePath.exists()){
savePath.mkdirs()
return
}
try {
val reader = FileReader(savePath.toString() + File.separator + "settings.json")
val parser = JSONParser().parse(reader) as JSONObject
if (parser.containsKey("lastConfigPath")) {
LastConfigPath = parser["lastConfigPath"].toString()
}
} catch (ignored: Exception) {}
}
fun save() {
val obj = JSONObject()
obj["lastConfigPath"] = LastConfigPath
Logger.logInfo("Saving user preferences...")
FileWriter(savePath.toString() + File.separator + "settings.json").use {
it.write(obj.toJSONString())
it.flush()
it.close()
Logger.logInfo("User preferences saved!")
}
}
}

View file

@ -9,7 +9,7 @@ import javax.swing.*
import javax.swing.table.DefaultTableModel
object MainScreen : JFrame("RS09 Thanos Tool") {
val dirChooser = object : JFileChooser(){
val dirChooser = object : JFileChooser(DataStore.LastConfigPath){
override fun updateUI() {
EditorConstants.updateTheme()
super.updateUI()
@ -53,6 +53,7 @@ object MainScreen : JFrame("RS09 Thanos Tool") {
showLoaded()
if(loadedModel.rowCount > 0) {
selectDir.isEnabled = false
DataStore.LastConfigPath = EditorConstants.CONFIG_PATH
}
}
}
@ -142,5 +143,9 @@ fun showLoaded(){
}
fun main() {
DataStore.parse()
Runtime.getRuntime().addShutdownHook(Thread {
DataStore.save()
})
MainScreen.isVisible = true
}