From 290e142797c52e821c8f402b51385f01fe564d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 25 Feb 2021 13:15:48 +0100 Subject: [PATCH] [Editor] Create styles for Compilation Logs pane --- .../editor/code/component/CompilationLogs.kt | 71 +++++++++++++++++++ .../code/stylesheet/CompilerLogsStylesheet.kt | 18 +++++ .../base/editor/code/view/CompilerLogsView.kt | 48 +++---------- 3 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/component/CompilationLogs.kt create mode 100644 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/stylesheet/CompilerLogsStylesheet.kt diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/component/CompilationLogs.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/component/CompilationLogs.kt new file mode 100644 index 00000000..c976d0bc --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/component/CompilationLogs.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/stylesheet/CompilerLogsStylesheet.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/stylesheet/CompilerLogsStylesheet.kt new file mode 100644 index 00000000..4f6be211 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/stylesheet/CompilerLogsStylesheet.kt @@ -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 + } + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/CompilerLogsView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/CompilerLogsView.kt index 13e9fc28..877ce645 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/CompilerLogsView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/CompilerLogsView.kt @@ -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 { 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 } } \ No newline at end of file