[Editor] Enable binding between file name and code editor tab title
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
package com.bartlomiejpluta.base.editor.code.model
|
||||
|
||||
import javafx.beans.property.Property
|
||||
import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
import tornadofx.toProperty
|
||||
import java.io.File
|
||||
|
||||
class Code(file: File, type: CodeType, code: String) {
|
||||
val fileProperty = file.toProperty()
|
||||
class Code(val fileProperty: Property<File>, val typeProperty: Property<CodeType>, code: String) {
|
||||
val file by fileProperty
|
||||
|
||||
val typeProperty = type.toProperty()
|
||||
val type by typeProperty
|
||||
|
||||
val codeProperty = code.toProperty()
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.bartlomiejpluta.base.editor.code.model
|
||||
|
||||
import tornadofx.getValue
|
||||
import tornadofx.observableListOf
|
||||
import tornadofx.setValue
|
||||
import tornadofx.toProperty
|
||||
import java.io.File
|
||||
|
||||
class FileSystemNode(file: File, val parent: FileSystemNode? = null) {
|
||||
var file = file
|
||||
val fileProperty = file.toProperty()
|
||||
var file by fileProperty
|
||||
private set
|
||||
|
||||
val isFile = file.isFile
|
||||
|
||||
@@ -33,7 +33,7 @@ class CodeStructureView : View() {
|
||||
if (event.button == MouseButton.PRIMARY && event.clickCount == 2) {
|
||||
selectionModel?.selectedItem?.value
|
||||
.takeIf { it?.isFile ?: false }
|
||||
?.let { mainController.openScript(it.file) }
|
||||
?.let { mainController.openScript(it) }
|
||||
|
||||
event.consume()
|
||||
}
|
||||
|
||||
@@ -6,8 +6,11 @@ import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
|
||||
class CodeVM(code: Code) : ItemViewModel<Code>(code) {
|
||||
val fileProperty = bind(Code::fileProperty)
|
||||
val file by fileProperty
|
||||
|
||||
val typeProperty = bind(Code::typeProperty)
|
||||
var type by typeProperty
|
||||
val type by typeProperty
|
||||
|
||||
val codeProperty = bind(Code::codeProperty)
|
||||
var code by codeProperty
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.editor.main.controller
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import com.bartlomiejpluta.base.editor.code.model.Code
|
||||
import com.bartlomiejpluta.base.editor.code.model.FileSystemNode
|
||||
import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
|
||||
import com.bartlomiejpluta.base.editor.command.context.UndoableScope
|
||||
import com.bartlomiejpluta.base.editor.image.view.importing.ImportImageFragment
|
||||
@@ -20,7 +21,6 @@ import com.bartlomiejpluta.base.editor.tileset.viewmodel.TileSetAssetDataVM
|
||||
import javafx.stage.FileChooser
|
||||
import org.springframework.stereotype.Component
|
||||
import tornadofx.*
|
||||
import java.io.File
|
||||
import kotlin.collections.set
|
||||
|
||||
@Component
|
||||
@@ -85,9 +85,9 @@ class MainController : Controller() {
|
||||
}
|
||||
}
|
||||
|
||||
fun openScript(file: File) {
|
||||
if (openItems.count { (_, item) -> item is Code && item.file.absolutePath == file.absolutePath } == 0) {
|
||||
val code = projectContext.loadScript(file)
|
||||
fun openScript(fsNode: FileSystemNode) {
|
||||
if (openItems.count { (_, item) -> item is Code && item.file.absolutePath == fsNode.file.absolutePath } == 0) {
|
||||
val code = projectContext.loadScript(fsNode.fileProperty)
|
||||
val vm = CodeVM(code)
|
||||
val scope = UndoableScope()
|
||||
setInScope(vm, scope)
|
||||
|
||||
@@ -16,7 +16,9 @@ import com.bartlomiejpluta.base.editor.tileset.asset.TileSetAsset
|
||||
import com.bartlomiejpluta.base.editor.tileset.asset.TileSetAssetData
|
||||
import com.bartlomiejpluta.base.editor.tileset.model.TileSet
|
||||
import com.bartlomiejpluta.base.editor.util.uid.UID
|
||||
import javafx.beans.binding.Bindings.createObjectBinding
|
||||
import javafx.beans.property.ObjectProperty
|
||||
import javafx.beans.property.Property
|
||||
import javafx.beans.property.SimpleObjectProperty
|
||||
import javafx.scene.image.Image
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -157,15 +159,20 @@ class DefaultProjectContext : ProjectContext {
|
||||
} ?: throw IllegalStateException("There is no open project in the context")
|
||||
}
|
||||
|
||||
override fun loadScript(file: File): Code {
|
||||
val type = when (file.extension.toLowerCase()) {
|
||||
"java" -> CodeType.JAVA
|
||||
else -> throw IllegalStateException("Unsupported script type")
|
||||
override fun loadScript(fileProperty: Property<File>): Code {
|
||||
val typeProperty = SimpleObjectProperty<CodeType>().apply {
|
||||
bind(createObjectBinding({
|
||||
when (fileProperty.value.extension.toLowerCase()) {
|
||||
"java" -> CodeType.JAVA
|
||||
else -> throw IllegalStateException("Unsupported script type")
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
val code = file.readText()
|
||||
|
||||
return Code(file, type, code)
|
||||
val code = fileProperty.value.readText()
|
||||
|
||||
return Code(fileProperty, typeProperty, code)
|
||||
}
|
||||
|
||||
override fun saveScript(code: Code) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.bartlomiejpluta.base.editor.project.model.Project
|
||||
import com.bartlomiejpluta.base.editor.tileset.asset.TileSetAssetData
|
||||
import com.bartlomiejpluta.base.editor.tileset.model.TileSet
|
||||
import javafx.beans.property.ObjectProperty
|
||||
import javafx.beans.property.Property
|
||||
import javafx.scene.image.Image
|
||||
import java.io.File
|
||||
|
||||
@@ -32,6 +33,6 @@ interface ProjectContext {
|
||||
|
||||
fun deleteAsset(asset: Asset)
|
||||
|
||||
fun loadScript(file: File): Code
|
||||
fun loadScript(fileProperty: Property<File>): Code
|
||||
fun saveScript(code: Code)
|
||||
}
|
||||
Reference in New Issue
Block a user