Guest User

Untitled

a guest
Dec 18th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import tornadofx.*
  2.  
  3. // Example of opening a normal window or a selection window based on command line args
  4. // If there is an argument, it is the ID of the model to open a normal window for.
  5. // If there is no argument, select an ID and open a window for it.
  6.  
  7. // model - also used outside of GUI
  8. class MyModel(val id:Int) {
  9. fun doStuff() {
  10. println( "My id is ${id}" )
  11. }
  12. }
  13.  
  14. // the normal application window
  15. // There is no NormalApplication because I cannot create the NormalScope ahead of time for it
  16.  
  17. class NormalScope(val model:MyModel):Scope()
  18.  
  19. class NormalController: Controller() {
  20. override val scope:NormalScope = super.scope as NormalScope
  21. }
  22. class NormalView:View() {
  23. val controller:NormalController by inject()
  24. override val root = vbox {
  25. button( "open new window" ) {
  26. action {
  27. find(SelectorView::class).openWindow(owner=null)
  28. }
  29. }
  30. label( "my ID is ${controller.scope.model.id}" )
  31. button( "do stuff" ) {
  32. action {
  33. controller.scope.model.doStuff()
  34. }
  35. }
  36. }
  37. }
  38.  
  39. // launcher window/app - gets the model based on params and launches the normal application window
  40.  
  41. // map of javafx raw param -> model
  42. val map:MutableMap<String,MyModel> = mutableMapOf()
  43.  
  44. class LauncherApp:App(LauncherView::class)
  45.  
  46. class LauncherView:View() {
  47. override val root = label("nobody should see this")
  48. override fun onBeforeShow() {
  49. super.onBeforeShow()
  50. val key = app.parameters.raw.first()
  51. val model = map[key]!!
  52. val scope = NormalScope(model)
  53. // launch NormalView and close this view
  54. find(NormalView::class,scope).openWindow(owner=null)
  55. javafx.application.Platform.runLater { close() }
  56. }
  57. }
  58.  
  59. // the model selector window
  60. // can use an App because it does not need a custom scope
  61. class SelectorApp:App(SelectorView::class)
  62.  
  63. class SelectorView:View() {
  64. override val root = vbox {
  65. (1 until 5).map { id ->
  66. button("open ${id}") {
  67. action {
  68. val model = MyModel(id)
  69. val scope = NormalScope(model)
  70. find(NormalView::class,scope).openWindow(owner=null)
  71. close()
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78. object Main {
  79. @JvmStatic
  80. fun main(args:Array<String>) {
  81. if ( args.size == 1 ) {
  82. // arg is model ID. Launch normal window
  83. val id = args[0].toInt()
  84. val model = MyModel(id)
  85. // store the model in map so we can retrieve it by the param
  86. val key = java.util.UUID.randomUUID().toString()
  87. map[key] = model
  88. launch<LauncherApp>(key)
  89. } else {
  90. // launch selector window
  91. launch<SelectorApp>()
  92. }
  93. }
  94. }
Add Comment
Please, Sign In to add comment