[Editor] Add support for api classes selection in SelectJavaClassView

This commit is contained in:
2021-03-01 11:59:16 +01:00
parent a270414a28
commit 2fb2183849
6 changed files with 68 additions and 32 deletions

View File

@@ -15,7 +15,11 @@ class APIProvider {
val apiNode: FileNode by lazy { loadNode() }
private fun loadNode() = ResourceFileNode.root("api").apply {
private fun loadNode() = ResourceFileNode.root(API_DIR).apply {
apiIndex.inputStream.bufferedReader().use(BufferedReader::readLines).forEach(this::createNode)
}
companion object {
val API_DIR = "api"
}
}

View File

@@ -1,17 +1,24 @@
package com.bartlomiejpluta.base.editor.code.view.select
import com.bartlomiejpluta.base.editor.file.model.FileSystemNode
import com.bartlomiejpluta.base.editor.file.model.FileType
import com.bartlomiejpluta.base.editor.code.api.APIProvider
import com.bartlomiejpluta.base.editor.file.model.*
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
import javafx.beans.binding.Bindings.createBooleanBinding
import javafx.beans.property.SimpleObjectProperty
import tornadofx.*
import java.io.File
import java.nio.file.Path
class SelectJavaClassFragment : Fragment("Select Java Class") {
private val projectContext: ProjectContext by di()
private val rootNode = projectContext.project!!.codeFSNode
private val selection = SimpleObjectProperty<FileSystemNode>()
private val apiProvider: APIProvider by di()
private val codeFSNode = projectContext.project!!.codeFSNode
private val rootNode = PseudoFileNode.emptyRoot().apply {
children += apiProvider.apiNode
children += codeFSNode
}
private val selection = SimpleObjectProperty<FileNode>()
private val selectJavaClassView = find<SelectJavaClassView>(
SelectJavaClassView::rootNode to rootNode,
@@ -35,7 +42,7 @@ class SelectJavaClassFragment : Fragment("Select Java Class") {
action {
selection.value?.let { node ->
onCompleteConsumer?.let { consumer ->
consumer(formatClassName(node.file))
consumer(formatClassName(node))
close()
}
}
@@ -50,10 +57,9 @@ class SelectJavaClassFragment : Fragment("Select Java Class") {
}
}
private fun formatClassName(file: File) = file
.relativeTo(rootNode.file)
.toPath()
.normalize()
.joinToString(".")
.substringBeforeLast(".")
private fun formatClassName(node: FileNode) = when (node) {
is FileSystemNode -> node.file.relativeTo(codeFSNode.file).toPath()
is ResourceFileNode -> Path.of(node.absolutePath.substringAfter("/${APIProvider.API_DIR}"))
else -> throw IllegalStateException("Unsupported file node type")
}.normalize().joinToString(".").substringBeforeLast(".")
}

View File

@@ -1,27 +1,29 @@
package com.bartlomiejpluta.base.editor.code.view.select
import com.bartlomiejpluta.base.editor.file.model.FileSystemNode
import com.bartlomiejpluta.base.editor.file.model.FileNode
import com.bartlomiejpluta.base.editor.file.model.FileType
import javafx.beans.property.Property
import javafx.scene.control.TreeItem
import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.*
class SelectJavaClassView : View() {
val rootNode: FileSystemNode by param()
val selection: Property<FileSystemNode> by param()
val rootNode: FileNode by param()
val selection: Property<FileNode> by param()
private val treeView = treeview<FileSystemNode> {
private val treeView = treeview<FileNode> {
root = TreeItem(rootNode)
isShowRoot = false
populate {
it.value?.children
}
cellFormat {
text = it.file.nameWithoutExtension
graphic = when {
it.file.isFile -> FontIcon("fa-cube")
else -> FontIcon("fa-folder")
text = it.name
graphic = when (it.type) {
FileType.FILE -> FontIcon("fa-cube")
FileType.DIRECTORY -> FontIcon("fa-folder")
}
}

View File

@@ -26,13 +26,13 @@ interface FileNode {
val lastModifiedProperty: ObservableLongValue
val lastModified: Long
fun delete()
fun rename(name: String)
fun refresh()
fun createNode(path: String): FileNode
fun delete(): Unit = throw UnsupportedOperationException()
fun rename(name: String): Unit = throw UnsupportedOperationException()
fun refresh(): Unit = throw UnsupportedOperationException()
fun createNode(path: String): FileNode = throw UnsupportedOperationException()
fun inputStream(): InputStream
fun outputStream(): OutputStream
fun inputStream(): InputStream = throw UnsupportedOperationException()
fun outputStream(): OutputStream = throw UnsupportedOperationException()
fun readText(charset: Charset = Charsets.UTF_8) = inputStream().reader(charset).readText()
fun writeText(text: String, charset: Charset = Charsets.UTF_8) = writeBytes(text.toByteArray(charset))

View File

@@ -0,0 +1,29 @@
package com.bartlomiejpluta.base.editor.file.model
import javafx.beans.property.SimpleLongProperty
import tornadofx.getValue
import tornadofx.observableListOf
import tornadofx.toProperty
class PseudoFileNode(name: String, absolutePath: String, override val type: FileType) : FileNode {
override val nameProperty = name.toProperty()
override val name by nameProperty
override val extensionProperty = "".toProperty()
override val extension by extensionProperty
override val absolutePathProperty = absolutePath.toProperty()
override val absolutePath by absolutePathProperty
override val lastModifiedProperty = SimpleLongProperty(0L)
override val lastModified by lastModifiedProperty
override val parent = null
override val children = observableListOf<FileNode>()
companion object {
fun emptyRoot() = PseudoFileNode("", "", FileType.DIRECTORY)
}
}

View File

@@ -48,11 +48,6 @@ class ResourceFileNode private constructor(
?.inputStream
?: throw IllegalStateException("Attempt to open input stream of resource directory")
override fun delete() = throw UnsupportedOperationException()
override fun rename(name: String) = throw UnsupportedOperationException()
override fun refresh() = throw UnsupportedOperationException()
override fun outputStream() = throw UnsupportedOperationException()
companion object {
fun resource(resource: Resource, parent: ResourceFileNode) = ResourceFileNode(resource, parent)
fun directory(name: String, parent: ResourceFileNode) = ResourceFileNode(null, parent, FileType.DIRECTORY, name)