Advertisement
Guest User

Untitled

a guest
Jul 31st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.71 KB | None | 0 0
  1. package space.smena.app.controller
  2.  
  3. import javafx.scene.control.Button
  4. import javafx.scene.control.ComboBox
  5. import javafx.scene.control.TableCell
  6. import javafx.scene.image.ImageView
  7. import javafx.scene.layout.BorderPane
  8. import space.smena.app.model.Printer
  9. import tornadofx.*
  10.  
  11. class HomeView : View("Home") {
  12.     private val commonController: CommonController by inject()
  13.     private val homeController: HomeController by inject()
  14.  
  15.     override val root = BorderPane()
  16.  
  17.     private val printers = homeController.loadPrintersAndUpdateStatus().observable()
  18.     private val devices = homeController.loadDevices()
  19.  
  20.     var printingIsRunning = false
  21.     var updateStatusIsRunning = false
  22.  
  23.     val model = PrinterModel(Printer())
  24.  
  25.     init {
  26.         with(root) {
  27.             top {}
  28.             center {
  29.                 tableview(printers) {
  30.                     column("Printer", Printer::deviceNameProperty).setCellFactory {
  31.                         object : TableCell<Printer, String>() {
  32.                             val cmb = ComboBox<String>(devices)
  33.  
  34.                             override fun updateItem(item: String?, empty: Boolean) {
  35.                                 super.updateItem(item, empty)
  36.  
  37.                                 if (item == null) {
  38.                                     graphic = null
  39.                                     return
  40.                                 }
  41.  
  42.                                 graphic = cmb
  43.                                 cmb.value = item
  44.                             }
  45.                         }
  46.                     }
  47.  
  48.  
  49.                     column("Account", Printer::printerNameProperty)
  50.  
  51.                     column("Connection", Printer::isOnlineProperty).cellFormat {
  52.                         graphic = if (rowItem.connectStatus) {
  53.                             ImageView("connection-ok.png")
  54.                         } else {
  55.                             ImageView("connection-off.png")
  56.                         }
  57.                         graphic.resize(22.5, 22.5)
  58.                     }
  59.  
  60.                     column("Status", Printer::genericStatusProperty).apply {
  61.                         remainingWidth()
  62.                         cellFormat {
  63.                             graphic = if (rowItem.genericStatus) {
  64.                                 ImageView("connection-ok.png")
  65.                             } else {
  66.                                 ImageView("connection-off.png")
  67.                             }
  68.                             graphic.resize(20.5, 22.5)
  69.                         }
  70.                     }
  71.                         column("Start print", Printer::printingStatusProperty).apply {
  72.                             setCellFactory {
  73.                                 object : TableCell<Printer, Boolean>() {
  74.                                     val btn = Button()
  75.  
  76.                                     override fun updateItem(item: Boolean?, empty: Boolean) {
  77.                                         super.updateItem(item, empty)
  78.  
  79.                                         if (item == null) {
  80.                                             graphic = null
  81.                                             return
  82.                                         }
  83.  
  84.                                         graphic = if (rowItem.printingStatus) {
  85.                                             btn.apply {
  86.                                                 graphic = ImageView("pause.png")
  87.                                             }
  88.                                         } else {
  89.                                             btn.apply {
  90.                                                 graphic = ImageView("play.png")
  91.                                             }
  92.                                         }
  93.  
  94.                                         graphic = btn
  95.  
  96.                                         btn.setOnAction {
  97.                                             // todo: event на остановку/включенеи
  98.                                             event ->
  99.                                             println("Проверка печати у ${rowItem.deviceName}")
  100.                                         }
  101.                                     }
  102.                                 }
  103.                             }
  104.                         }
  105.  
  106.                         column("Test print", Printer::deviceNameProperty).setCellFactory {
  107.                             object : TableCell<Printer, String>() {
  108.                                 val btn = Button("Тест")
  109.  
  110.                                 override fun updateItem(item: String?, empty: Boolean) {
  111.                                     super.updateItem(item, empty)
  112.  
  113.                                     if (item == null) {
  114.                                         graphic = null
  115.                                         return
  116.                                     }
  117.  
  118.                                     graphic = btn
  119.                                     btn.setOnAction {
  120.                                         // todo: event на проверку
  121.                                         event -> println("Проверка печати у $item ($event)")
  122.                                     }
  123.                                 }
  124.                             }
  125.                         }
  126.  
  127.                         model.rebindOnChange(this) { selectedPrinter ->
  128.                             printer = selectedPrinter ?: Printer()
  129.                         }
  130.  
  131.                         smartResize()
  132.                     }
  133.                 }
  134.  
  135.             bottom {}
  136.         }
  137.     }
  138. }
  139.  
  140. class PrinterModel(var printer: Printer) : ViewModel() {
  141.     val printerId = bind { printer.idProperty }
  142.     val device    = bind { printer.deviceNameProperty }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement