Skip to content
Snippets Groups Projects
Commit 1331d412 authored by Terence-ChanLY's avatar Terence-ChanLY
Browse files

Added User Config File, with color theme and default file open location

parent ff573e11
No related branches found
No related tags found
No related merge requests found
Pipeline #87243 failed
...@@ -8,7 +8,6 @@ import javafx.scene.layout.BorderPane ...@@ -8,7 +8,6 @@ import javafx.scene.layout.BorderPane
import javafx.scene.layout.HBox import javafx.scene.layout.HBox
import javafx.scene.layout.VBox import javafx.scene.layout.VBox
import javafx.scene.text.Font import javafx.scene.text.Font
import javafx.scene.text.Text
import javafx.stage.FileChooser import javafx.stage.FileChooser
import javafx.stage.Stage import javafx.stage.Stage
import java.io.File import java.io.File
...@@ -16,15 +15,20 @@ import java.io.FileNotFoundException ...@@ -16,15 +15,20 @@ import java.io.FileNotFoundException
import java.io.PrintWriter import java.io.PrintWriter
import java.util.* import java.util.*
class Main : Application() { class Main : Application() {
override fun start(stage: Stage) { override fun start(stage: Stage) {
//Config, setting up themeColor and default file location
var userConfig = initConfig()
val bold = Button("B") val bold = Button("B")
val italics = Button("I") val italics = Button("I")
val heading = Button("H") val heading = Button("H")
val strikethrough = Button("S") val strikethrough = Button("S")
val compileMd = Button("Compile") val compileMd = Button("Compile")
val toolbar = ToolBar( val toolbar = ToolBar(
bold, bold,
...@@ -117,10 +121,10 @@ class Main : Application() { ...@@ -117,10 +121,10 @@ class Main : Application() {
val file = Menu("File") val file = Menu("File")
val openFile = MenuItem("Open File") val openFile = MenuItem("Open File")
val new = MenuItem("New") val newFile = MenuItem("New")
val saveFile = MenuItem("Save") val saveFile = MenuItem("Save")
val exitApp = MenuItem("Exit") val exitApp = MenuItem("Exit")
file.items.addAll(openFile, new, saveFile, exitApp) file.items.addAll(openFile, newFile, saveFile, exitApp)
val edit = Menu("Edit") val edit = Menu("Edit")
val cut = MenuItem("Cut") val cut = MenuItem("Cut")
...@@ -128,6 +132,7 @@ class Main : Application() { ...@@ -128,6 +132,7 @@ class Main : Application() {
val paste = MenuItem("Paste") val paste = MenuItem("Paste")
edit.items.addAll(cut, copy, paste) edit.items.addAll(cut, copy, paste)
//Create SubMenu Help. //Create SubMenu Help.
//Create SubMenu Help. //Create SubMenu Help.
val view = Menu("View") val view = Menu("View")
...@@ -139,11 +144,22 @@ class Main : Application() { ...@@ -139,11 +144,22 @@ class Main : Application() {
topContainer.getChildren().add(mainMenu); topContainer.getChildren().add(mainMenu);
topContainer.getChildren().add(toolbar); topContainer.getChildren().add(toolbar);
//NewFile
newFile.onAction = EventHandler {
text.clear();
}
//OpenFile function //OpenFile function
openFile.onAction = EventHandler { openFile.onAction = EventHandler {
val filechooser = FileChooser(); val filechooser = FileChooser();
filechooser.setTitle("Open my file"); filechooser.setTitle("Open my file");
filechooser.setInitialDirectory(File(System.getProperty("user.home")));
if (userConfig.defaultFileLocation == "user.home") {
filechooser.setInitialDirectory(File(System.getProperty(userConfig.defaultFileLocation)))
} else {
filechooser.setInitialDirectory(File(userConfig.defaultFileLocation))
}
val selectedFile = filechooser.showOpenDialog(stage); val selectedFile = filechooser.showOpenDialog(stage);
try { try {
val scanner = Scanner(selectedFile); val scanner = Scanner(selectedFile);
...@@ -155,11 +171,20 @@ class Main : Application() { ...@@ -155,11 +171,20 @@ class Main : Application() {
e.printStackTrace(); e.printStackTrace();
} }
border.left = FolderView().build(selectedFile.parentFile.absolutePath) border.left = FolderView().build(selectedFile.parentFile.absolutePath)
userConfig = updateFileLocationConfig(userConfig, selectedFile.parentFile.absolutePath)
} }
//SaveFile function //SaveFile function
saveFile.onAction = EventHandler { saveFile.onAction = EventHandler {
val file = FileChooser().showSaveDialog(Stage()); val savefilechooser = FileChooser();
if (userConfig.defaultFileLocation == "user.home") {
savefilechooser.setInitialDirectory(File(System.getProperty(userConfig.defaultFileLocation)))
} else {
savefilechooser.setInitialDirectory(File(userConfig.defaultFileLocation))
}
val file = savefilechooser.showSaveDialog(Stage());
if (file != null) { if (file != null) {
try { try {
val printWriter = PrintWriter(file); val printWriter = PrintWriter(file);
......
package net.codebot.application
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
import java.io.IOException
import java.io.PrintWriter
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
@Serializable
data class UserSetting(
var theme: String,
var defaultFileLocation: String
)
//Check config file -> return user setting data class
fun initConfig(): UserSetting {
var currSetting: UserSetting = UserSetting("", "")
val configFile = File(getConfigPath())
//Check if Config File exists, if not, create a new default one
if (!configFile.exists()) {
println("Config file not found")
val configFolder = configFile.parentFile
if (!configFolder.exists()) {
println("Config folder not found")
if (!configFolder.mkdirs()) {
throw IllegalStateException("Couldn't create dir: " + configFolder)
} else {
println("Config folder created");
}
} else {
println("Config folder exists")
}
println("Writing Default Config")
//Default Config
updateConfig(UserSetting("light", "user.home"))
}
try {
println("Reading Config from" + configFile.toString())
val config = String(Files.readAllBytes(configFile.toPath()))
println("Parsing Config into Json")
val json = Json {prettyPrint = true}
currSetting = json.decodeFromString<UserSetting>(config)
} catch (e: IOException) {
e.printStackTrace()
}
if ( currSetting.theme == "" ||currSetting.defaultFileLocation == "") {
throw IllegalStateException("Couldn't load User Config")
}
return currSetting
}
public fun updateFileLocationConfig
(userSetting: UserSetting, newlocation: String) : UserSetting {
val newUserSetting = UserSetting(userSetting.theme, newlocation)
updateConfig(newUserSetting)
return newUserSetting
}
public fun updateColorThemeConfig
(userSetting: UserSetting, newTheme: String) : UserSetting {
val newUserSetting = UserSetting(newTheme, userSetting.defaultFileLocation)
updateConfig(newUserSetting)
return newUserSetting
}
private fun updateConfig(userSetting: UserSetting) {
val configFile = File(getConfigPath())
println("Attempting to Update Config File: " + configFile.toString())
try {
println("Converting Properties to Json")
val json = Json {prettyPrint = true}
val jsonString = json.encodeToString(userSetting)
println("Writing Config")
println(jsonString)
val file = File(getConfigPath())
val printWriter = PrintWriter(file) //Get where to write
printWriter.write(jsonString)
printWriter.close();
println("Done Writing Config")
} catch (e: IOException) {
e.printStackTrace()
}
}
private fun getConfigPath(): String? {
val rootPath = Paths.get(System.getProperty("user.home"))
val partialPath = Paths.get(".MarkDown/config.txt")
val resolvedPath: Path = rootPath.resolve(partialPath)
return resolvedPath.toString()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment