Skip to content

Decorator

Brian edited this page Jul 3, 2018 · 1 revision

EventBus version

import com.jfoenix.controls.JFXButton
import javafx.geometry.Pos
import javafx.scene.paint.Color
import javafx.scene.text.FontWeight
import tornadofx.*

class JFXDecoratorEventBusTestApp : App(Main::class, MyStyles::class) {

    class Main: View() {
        override val root = jfxdecorator(First::class) {
            subscribe<ReplaceContentEvent<UIComponent>> {
                setContent(find(it.uiComponent, it.findScope).root) // receive component and what scope to find it in
            }
        }
    }

    class First: View() {
        override val root = vbox {
            addClass(MyStyles.box)
            label("First").addClass(MyStyles.title)
            jfxbutton("Switch") {
                addClass(MyStyles.switchBtn)
                action { fire(ReplaceContentEvent(Second::class)) } // fire event to replace decorators content
            }
        }
    }

    class Second: View() {
        override val root = vbox {
            addClass(MyStyles.box)
            label("Second").addClass(MyStyles.title)
            jfxbutton("Switch", btnType = JFXButton.ButtonType.RAISED) {
                addClass(MyStyles.switchBtn)
                action { fire(ReplaceContentEvent(First::class)) } // fire event to replace decorators content
            }
        }
    }

    class MyStyles: JFXStylesheet() {
        companion object {
            val box by cssclass()
            val switchBtn by cssclass()
            val title by cssclass()

            val defaultColor = Color.web("#4059a9")
        }

        init {
            box {
                alignment = Pos.TOP_CENTER
                spacing = 10.px
            }

            jfxDecoratorContentContainer {
                prefHeight = 300.px
                prefWidth = 400.px
            }

            switchBtn {
                backgroundColor += defaultColor
                textFill = Color.WHITE
            }

            title {
                fontSize = 18.px
                fontWeight = FontWeight.BOLD
            }
        }
    }
}

Controller version

import javafx.geometry.Pos
import javafx.scene.paint.Color
import tornadofx.*

class JFXDecoratorTestApp: App(Main::class, MyStyles::class) {

    class Main: View() {
        override val root = jfxdecorator(First::class)
    }

    class MainController: Controller() {

        private val main by inject<Main>()
        private val first by inject<First>()
        private val second by inject<Second>()

        fun switchToFirst() {
            main.root.setContent(first.root)
        }

        fun switchToSecond() {
            main.root.setContent(second.root)
        }
    }


    class First: View() {
        private val ctrl by inject<MainController>()
        override val root = vbox {
            addClass(MyStyles.box)
            label("First")
            jfxbutton("Switch") {
                addClass(MyStyles.switchBtn)
                action { ctrl.switchToSecond() }
            }
        }
    }

    class Second: View() {
        private val ctrl by inject<MainController>()
        override val root = vbox {
            addClass(MyStyles.box)
            label("Second")
            jfxbutton("Switch") {
                addClass(MyStyles.switchBtn)
                action { ctrl.switchToFirst() }
            }
        }
    }

    class MyStyles: JFXStylesheet() {
        companion object {
            val box by cssclass()
            val switchBtn by cssclass()

            val defaultColor = Color.web("#4059a9")
        }

        init {
            box {
                alignment = Pos.TOP_CENTER
            }

            jfxDecoratorContentContainer {
                prefHeight = 300.px
                prefWidth = 400.px
            }

            switchBtn {
                backgroundColor += defaultColor
                textFill = Color.WHITE
            }
        }
    }
}
Clone this wiki locally