Created
December 18, 2018 21:26
-
-
Save stephenbmfj/c1b39ec703aafbfb210e98e4437d1bc9 to your computer and use it in GitHub Desktop.
TornadoFX example of opening a normal window or a selection window based on command line args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornadofx.* | |
// Example of opening a normal window or a selection window based on command line args | |
// If there is an argument, it is the ID of the model to open a normal window for. | |
// If there is no argument, select an ID and open a window for it. | |
// model - also used outside of GUI | |
class MyModel(val id:Int) { | |
fun doStuff() { | |
println( "My id is ${id}" ) | |
} | |
} | |
// the normal application window | |
// There is no NormalApplication because I cannot create the NormalScope ahead of time for it | |
class NormalScope(val model:MyModel):Scope() | |
class NormalController: Controller() { | |
override val scope:NormalScope = super.scope as NormalScope | |
} | |
class NormalView:View() { | |
val controller:NormalController by inject() | |
override val root = vbox { | |
button( "open new window" ) { | |
action { | |
find(SelectorView::class).openWindow(owner=null) | |
} | |
} | |
label( "my ID is ${controller.scope.model.id}" ) | |
button( "do stuff" ) { | |
action { | |
controller.scope.model.doStuff() | |
} | |
} | |
} | |
} | |
// launcher window/app - gets the model based on params and launches the normal application window | |
// map of javafx raw param -> model | |
val map:MutableMap<String,MyModel> = mutableMapOf() | |
class LauncherApp:App(LauncherView::class) | |
class LauncherView:View() { | |
override val root = label("nobody should see this") | |
override fun onBeforeShow() { | |
super.onBeforeShow() | |
val key = app.parameters.raw.first() | |
val model = map[key]!! | |
val scope = NormalScope(model) | |
// launch NormalView and close this view | |
find(NormalView::class,scope).openWindow(owner=null) | |
javafx.application.Platform.runLater { close() } | |
} | |
} | |
// the model selector window | |
// can use an App because it does not need a custom scope | |
class SelectorApp:App(SelectorView::class) | |
class SelectorView:View() { | |
override val root = vbox { | |
(1 until 5).map { id -> | |
button("open ${id}") { | |
action { | |
val model = MyModel(id) | |
val scope = NormalScope(model) | |
find(NormalView::class,scope).openWindow(owner=null) | |
close() | |
} | |
} | |
} | |
} | |
} | |
object Main { | |
@JvmStatic | |
fun main(args:Array<String>) { | |
if ( args.size == 1 ) { | |
// arg is model ID. Launch normal window | |
val id = args[0].toInt() | |
val model = MyModel(id) | |
// store the model in map so we can retrieve it by the param | |
val key = java.util.UUID.randomUUID().toString() | |
map[key] = model | |
launch<LauncherApp>(key) | |
} else { | |
// launch selector window | |
launch<SelectorApp>() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment