[Editor] Create styles for Compilation Logs pane

This commit is contained in:
2021-02-25 13:15:48 +01:00
parent c9716d438d
commit 290e142797
3 changed files with 99 additions and 38 deletions

View File

@@ -0,0 +1,71 @@
package com.bartlomiejpluta.base.editor.code.component
import com.bartlomiejpluta.base.editor.code.stylesheet.CompilerLogsStylesheet
import com.bartlomiejpluta.base.editor.event.UpdateCompilationLogEvent
import javafx.scene.Cursor
import javafx.scene.layout.StackPane
import javafx.scene.paint.Color
import javafx.scene.text.Text
import org.codehaus.commons.compiler.Location
import org.fxmisc.richtext.StyledTextArea
import tornadofx.addClass
class CompilationLogs(private val locationClick: (location: Location) -> Unit) : StackPane() {
private val editor = StyledTextArea("compiler-logs",
{ text, style -> text.addClass(style) },
CompilationLogStyle.NO_STYLE,
{ text, style -> style.apply(text) }
).apply { isEditable = false }
init {
children += editor
}
fun setEntry(message: String, severity: UpdateCompilationLogEvent.Severity, location: Location?) {
editor.clear()
val locationRef = CompilationLogStyle(location = location, onClick = locationClick)
editor.insert(editor.length, location?.toString() ?: "", locationRef)
editor.insert(editor.length, message, CompilationLogStyle(severity = severity))
}
fun clear() = editor.clear()
override fun getUserAgentStylesheet(): String = CompilerLogsStylesheet().base64URL.toExternalForm()
class CompilationLogStyle(
private val location: Location? = null,
private val severity: UpdateCompilationLogEvent.Severity? = null,
private val onClick: (Location) -> Unit = {}
) {
fun apply(text: Text) = when {
severity != null -> message(text, severity)
location != null -> location(text, location)
else -> {
}
}
private fun message(text: Text, severity: UpdateCompilationLogEvent.Severity) {
text.fill = when (severity) {
UpdateCompilationLogEvent.Severity.WARNING -> Color.ORANGE
UpdateCompilationLogEvent.Severity.ERROR -> Color.RED
UpdateCompilationLogEvent.Severity.INFO -> text.fill
}
}
private fun location(text: Text, location: Location) {
text.cursor = Cursor.HAND
text.fill = Color.BLUE
text.isUnderline = true
text.setOnMouseClicked {
onClick(location)
}
}
companion object {
val NO_STYLE = CompilationLogStyle()
}
}
}

View File

@@ -0,0 +1,18 @@
package com.bartlomiejpluta.base.editor.code.stylesheet
import javafx.scene.text.FontWeight
import tornadofx.Stylesheet
import tornadofx.cssclass
class CompilerLogsStylesheet : Stylesheet() {
companion object {
val compilerLogs by cssclass()
}
init {
compilerLogs {
fontFamily = "monospace"
fontWeight = FontWeight.MEDIUM
}
}
}

View File

@@ -1,13 +1,10 @@
package com.bartlomiejpluta.base.editor.code.view
import com.bartlomiejpluta.base.editor.code.component.CompilationLogs
import com.bartlomiejpluta.base.editor.event.UpdateCompilationLogEvent
import com.bartlomiejpluta.base.editor.main.controller.MainController
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
import javafx.scene.Cursor
import javafx.scene.paint.Color
import javafx.scene.text.Text
import org.codehaus.commons.compiler.Location
import org.fxmisc.richtext.StyledTextArea
import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.*
import java.io.File
@@ -17,52 +14,27 @@ class CompilerLogsView : View() {
private val projectContext: ProjectContext by di()
private val mainController: MainController by di()
private val editor = StyledTextArea(
null,
{ _, _ -> },
LocationRef.NO_LINK,
{ text, style -> style.apply(text) }
).apply { isEditable = false }
private val compilationLogs = CompilationLogs(this::locationClick)
init {
subscribe<UpdateCompilationLogEvent> { event ->
editor.clear()
compilationLogs.setEntry(event.message, event.severity, event.location)
}
}
val locationRef = LocationRef(event.location) { loc ->
projectContext.project?.codeFSNode?.findByFile(File(loc.fileName))?.let {
mainController.openScript(it, loc.lineNumber, 1)
}
}
editor.insert(editor.length, event.location?.toString() ?: "", locationRef)
editor.insert(editor.length, event.message, LocationRef.NO_LINK)
private fun locationClick(location: Location) {
projectContext.project?.codeFSNode?.findByFile(File(location.fileName))?.let {
mainController.openScript(it, location.lineNumber, 1)
}
}
override val root = borderpane {
left = hbox {
button(graphic = FontIcon("fa-trash")) {
action { editor.clear() }
action { compilationLogs.clear() }
}
}
center = editor
}
class LocationRef(private val location: Location?, private val onClick: (Location) -> Unit = {}) {
private constructor() : this(null)
fun apply(text: Text) = location?.let { loc ->
text.cursor = Cursor.HAND
text.fill = Color.BLUE
text.isUnderline = true
text.setOnMouseClicked {
onClick(loc)
}
}
companion object {
val NO_LINK = LocationRef()
}
center = compilationLogs
}
}