TornadoFX:标签未更新

时间:2018-09-20 11:50:02

标签: tornadofx

我有一个应用程序,使用XSL转换(XSLT)转换XML文档。这是使用TornadoFX构建的(可以在here中找到源代码)。我正在尝试使用转换状态更新状态标签,该状态标签是在扩展Controller的类内完成的。但是由于某种原因,状态标签什么也没显示。

我的变压器班的源代码:

class Transformer : Controller() {
    private val statusProperty = SimpleStringProperty("")
    var status by statusProperty

    fun transform(xml: File, xslt: File, result: StreamResult) {
        runLater { status = "" }

        // create the DOM Source
        val factory = DocumentBuilderFactory.newInstance()
        factory.isNamespaceAware = true
        val builder = factory.newDocumentBuilder()
        val bbcDoc = builder.parse(xml)
        val source = DOMSource(bbcDoc)

        // Create an instance of the TransformerFactory
        val transfomerFactory = TransformerFactory.newInstance()
        val transformer = transfomerFactory.newTransformer(StreamSource(xslt))
                .apply {
                    setOutputProperty(OutputKeys.INDENT, "yes")
                    setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")
                }
        runLater {
            try {
                transformer.transform(source, result)
                status = "Completed successful"
            } catch (e: Exception) {
                status = e.message
            }
        }
    }
}

主屏幕类:

class MainScreen : View("XSLT Transformer") {

    val status: TaskStatus by inject()
    val model: TransformerModel by inject()
    val transformer: Transformer by inject()

    private val xmlFilter = arrayOf(FileChooser.ExtensionFilter("XML Filer (*.xml)", "*.xml"))

    private val xsltFilter = arrayOf(FileChooser.ExtensionFilter("XSLT Filer (*.xslt)", "*.xslt"))
    private lateinit var xmlInput: TextField
    private lateinit var xsltInput: TextField

    override val root = form {
        fieldset(labelPosition = Orientation.VERTICAL) {
            field("XSLT fil") {
                ...
            }
            field("XML Input Fil") {
                ...
            }
            button("Konverter") {
                enableWhen(model.valid)
                isDefaultButton = true
                useMaxWidth = true
                action {
                    // An object to hold the results. It can be a file.
//                    val writer = System.out
                    val output = StreamResult(StringWriter())
                    runAsyncWithProgress {
                        try {
                            transformer.status = "Konverterer xml fil..."
                            transformer.transform(File(xmlInput.text), File(xsltInput.text), output)
                            transformer.status = "Færdig"
                        } catch (e: Exception) {
                            transformer.status = e.message
                        }
                    } ui {
                        showDialogResult(output)
                        transformer.status = "Completed"
                    }
                }
            }
        }
        label(transformer.status) {
            style {
                paddingTop = 10
                textFill = Color.RED
                fontWeight = FontWeight.BOLD
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须绑定到transformer.statusProperty,而不是transformer.statusstatus属性只是一个getter / setter方法,无法更新标签。 statusProperty是可观察的,因此对其所做的更改将反映在标签中。

还必须确保不要在UI线程上执行长时间运行的任务。 runLater计划在UI线程上完成工作。您要做的是在runAsync块中执行长时间运行的任务,并添加一个ui块来接收结果。在ui块中,您可以更新UI。