[Editor] Enable creating and filling class with predefined FTL templates on project and class creation
This commit is contained in:
@@ -47,6 +47,7 @@ dependencies {
|
||||
|
||||
// Spring
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
|
||||
}
|
||||
|
||||
task provideGameEngine(type: Copy) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.bartlomiejpluta.base.editor.code.service
|
||||
|
||||
import com.bartlomiejpluta.base.editor.file.model.FileSystemNode
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import freemarker.template.Configuration
|
||||
import freemarker.template.TemplateExceptionHandler
|
||||
import freemarker.template.Version
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import java.util.*
|
||||
|
||||
@Component
|
||||
class JavaClassService {
|
||||
|
||||
@Autowired
|
||||
private lateinit var projectContext: ProjectContext
|
||||
|
||||
private val config = Configuration(Version(2, 3, 20)).apply {
|
||||
defaultEncoding = "UTF-8"
|
||||
locale = Locale.US
|
||||
templateExceptionHandler = TemplateExceptionHandler.RETHROW_HANDLER
|
||||
setClassForTemplateLoading(this.javaClass, "/java_templates/")
|
||||
}
|
||||
|
||||
fun toPathString(className: String) = toPath(className).toString() + ".java"
|
||||
fun toPath(className: String) = Path.of("", *className.split(".").toTypedArray())
|
||||
|
||||
fun ofPath(path: String): String = ofPath(Path.of(path))
|
||||
fun ofPath(path: Path): String = path.joinToString(".")
|
||||
|
||||
fun createClassFile(name: String, directory: FileSystemNode, classTemplate: String) {
|
||||
projectContext.project?.let { project ->
|
||||
val template = config.getTemplate(classTemplate)
|
||||
|
||||
val className = name.substringAfterLast(".")
|
||||
val classFile = File(directory.file, toPath(name).toString() + ".java")
|
||||
val classPackage = ofPath(
|
||||
File(directory.file, toPath(name).toString()).parentFile.relativeTo(project.codeFSNode.file).toPath()
|
||||
)
|
||||
|
||||
project.codeFSNode.createNode(classFile.toRelativeString(project.codeFSNode.file))
|
||||
|
||||
val model = mapOf("className" to className, "package" to classPackage)
|
||||
template.process(model, classFile.writer())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package com.bartlomiejpluta.base.editor.code.view.list
|
||||
|
||||
import com.bartlomiejpluta.base.editor.code.api.APIProvider
|
||||
import com.bartlomiejpluta.base.editor.code.component.ScriptFileTreeCell
|
||||
import com.bartlomiejpluta.base.editor.code.service.JavaClassService
|
||||
import com.bartlomiejpluta.base.editor.file.model.FileNode
|
||||
import com.bartlomiejpluta.base.editor.file.model.FileSystemNode
|
||||
import com.bartlomiejpluta.base.editor.file.model.FileType
|
||||
import com.bartlomiejpluta.base.editor.file.model.PseudoFileNode
|
||||
import com.bartlomiejpluta.base.editor.main.controller.MainController
|
||||
@@ -15,11 +17,11 @@ import tornadofx.View
|
||||
import tornadofx.expandAll
|
||||
import tornadofx.populate
|
||||
import tornadofx.treeview
|
||||
import java.io.File
|
||||
|
||||
class ScriptFilesView : View() {
|
||||
private val projectContext: ProjectContext by di()
|
||||
private val mainController: MainController by di()
|
||||
private val javaClassService: JavaClassService by di()
|
||||
private val apiProvider: APIProvider by di()
|
||||
|
||||
init {
|
||||
@@ -64,7 +66,11 @@ class ScriptFilesView : View() {
|
||||
title = "New class"
|
||||
}
|
||||
.showAndWait()
|
||||
.map { it.replace(".", File.separator) + ".java" }
|
||||
.map {
|
||||
javaClassService.createClassFile(it, fsNode as FileSystemNode, "empty_class.ftl")
|
||||
it
|
||||
}
|
||||
.map(javaClassService::toPathString)
|
||||
.map { fsNode.createNode(it) }
|
||||
.ifPresent { mainController.openScript(it) }
|
||||
}
|
||||
|
||||
@@ -38,8 +38,7 @@ class MainController : Controller() {
|
||||
find<ProjectSettingsFragment>().apply {
|
||||
onComplete {
|
||||
openItems.clear()
|
||||
projectContext.project = it
|
||||
projectContext.save()
|
||||
projectContext.createNewProject(it)
|
||||
}
|
||||
|
||||
openModal(block = true, resizable = false)
|
||||
|
||||
@@ -64,7 +64,7 @@ class Brush {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTileByMode(tile: Tile) = when (mode) {
|
||||
private fun getTileByMode(tile: Tile) = when (mode!!) {
|
||||
BrushMode.PAINTING_MODE -> tile
|
||||
BrushMode.ERASING_MODE -> null
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.editor.project.context
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import com.bartlomiejpluta.base.editor.code.model.Code
|
||||
import com.bartlomiejpluta.base.editor.code.model.CodeType
|
||||
import com.bartlomiejpluta.base.editor.code.service.JavaClassService
|
||||
import com.bartlomiejpluta.base.editor.file.model.FileNode
|
||||
import com.bartlomiejpluta.base.editor.image.asset.ImageAsset
|
||||
import com.bartlomiejpluta.base.editor.image.asset.ImageAssetData
|
||||
@@ -45,6 +46,9 @@ class DefaultProjectContext : ProjectContext {
|
||||
@Autowired
|
||||
private lateinit var mapDeserializer: MapDeserializer
|
||||
|
||||
@Autowired
|
||||
private lateinit var javaClassService: JavaClassService
|
||||
|
||||
override val projectProperty = SimpleObjectProperty<Project?>() as ObjectProperty<Project?>
|
||||
override var project by projectProperty
|
||||
|
||||
@@ -72,6 +76,13 @@ class DefaultProjectContext : ProjectContext {
|
||||
.let { project = it }
|
||||
}
|
||||
|
||||
override fun createNewProject(project: Project) {
|
||||
this.project = project
|
||||
save()
|
||||
|
||||
javaClassService.createClassFile(project.runner, project.codeFSNode, "game_runner.ftl")
|
||||
}
|
||||
|
||||
override fun importMap(name: String, map: GameMap) {
|
||||
project?.let {
|
||||
UID.next(it.maps.map(Asset::uid)).let { uid ->
|
||||
|
||||
@@ -19,6 +19,7 @@ interface ProjectContext {
|
||||
|
||||
fun save()
|
||||
fun open(file: File)
|
||||
fun createNewProject(project: Project)
|
||||
|
||||
fun importMap(name: String, map: GameMap)
|
||||
fun loadMap(uid: String): GameMap
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
logging:
|
||||
level:
|
||||
com.bartlomiejpluta.base.editor: DEBUG
|
||||
|
||||
spring:
|
||||
freemarker:
|
||||
checkTemplateLocation: false
|
||||
5
editor/src/main/resources/java_templates/empty_class.ftl
Normal file
5
editor/src/main/resources/java_templates/empty_class.ftl
Normal file
@@ -0,0 +1,5 @@
|
||||
package ${package};
|
||||
|
||||
public class ${className} {
|
||||
|
||||
}
|
||||
5
editor/src/main/resources/java_templates/game_runner.ftl
Normal file
5
editor/src/main/resources/java_templates/game_runner.ftl
Normal file
@@ -0,0 +1,5 @@
|
||||
package ${package};
|
||||
|
||||
public class ${className} {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user