Blame view

src/main/kotlin/application/controller/VesselListPanelController.kt 2.69 KB
b350f9dfe   lsagona   import vessels an...
1
2
3
4
  package application.controller
  
  import application.model.MessageListener
  import application.model.Vessel
2bbe36a1b   lsagona   addd the possibil...
5
  import application.model.observableSelectedVessel
f39d90e60   lsagona   Select/deselect MMSI
6
  import application.model.observableVessel
b350f9dfe   lsagona   import vessels an...
7
8
  import javafx.collections.FXCollections
  import javafx.collections.ObservableList
a6a8df6ff   lsagona   add the possibili...
9
  import javafx.collections.transformation.FilteredList
b350f9dfe   lsagona   import vessels an...
10
11
  import javafx.fxml.FXML
  import javafx.fxml.Initializable
a6a8df6ff   lsagona   add the possibili...
12
  import javafx.scene.control.*
f39d90e60   lsagona   Select/deselect MMSI
13
  import javafx.scene.input.MouseEvent
b350f9dfe   lsagona   import vessels an...
14
15
  import java.net.URL
  import java.util.*
f39d90e60   lsagona   Select/deselect MMSI
16

2bbe36a1b   lsagona   addd the possibil...
17
  class VesselListPanelController : Initializable, MessageListener {
a6a8df6ff   lsagona   add the possibili...
18

b350f9dfe   lsagona   import vessels an...
19
      @FXML
f39d90e60   lsagona   Select/deselect MMSI
20
      var shipListView: ListView<String?> = ListView()
b350f9dfe   lsagona   import vessels an...
21

a6a8df6ff   lsagona   add the possibili...
22
23
      @FXML
      var filterInput: TextField = TextField()
f39d90e60   lsagona   Select/deselect MMSI
24
      private var shipList: ObservableList<String?> = FXCollections.observableArrayList()
b350f9dfe   lsagona   import vessels an...
25

a6a8df6ff   lsagona   add the possibili...
26
      private val filterMMSI = FilteredList(shipList)
b350f9dfe   lsagona   import vessels an...
27
      override fun initialize(location: URL?, resources: ResourceBundle?) {
f39d90e60   lsagona   Select/deselect MMSI
28

a6a8df6ff   lsagona   add the possibili...
29
          shipListView.items = filterMMSI
2bbe36a1b   lsagona   addd the possibil...
30
31
          observableVessel.listeners.add(this)
          shipListView.selectionModel.selectedItemProperty().addListener { _, _, newValue ->
f39d90e60   lsagona   Select/deselect MMSI
32
              if (newValue == null) {
dd1ce7fe8   lsagona   add button to swi...
33
                  observableSelectedVessel.value = Vessel(null)
f39d90e60   lsagona   Select/deselect MMSI
34
              } else {
dd1ce7fe8   lsagona   add button to swi...
35
                  observableSelectedVessel.value = observableVessel.vessels[newValue]!!
f39d90e60   lsagona   Select/deselect MMSI
36
              }
2bbe36a1b   lsagona   addd the possibil...
37
          }
a6a8df6ff   lsagona   add the possibili...
38

f39d90e60   lsagona   Select/deselect MMSI
39
          setCellFactory()
a6a8df6ff   lsagona   add the possibili...
40
          setFilterTextListener()
b350f9dfe   lsagona   import vessels an...
41
      }
f39d90e60   lsagona   Select/deselect MMSI
42
      override fun onValueChanged(newValue: MutableMap<String?, Vessel>) {
b350f9dfe   lsagona   import vessels an...
43
44
45
          shipList.clear()
          shipList.addAll(newValue.keys)
      }
a6a8df6ff   lsagona   add the possibili...
46
47
48
49
50
51
52
53
54
55
      private fun setFilterTextListener() {
          filterInput.textProperty().addListener { _ ->
              val filter: String = filterInput.text
              if (filter.isEmpty()) {
                  filterMMSI.setPredicate { true }
              } else {
                  filterMMSI.setPredicate { s: String? -> s!!.contains(filter) }
              }
          }
      }
f39d90e60   lsagona   Select/deselect MMSI
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      private fun setCellFactory() {
          val selectionModel: MultipleSelectionModel<String?>? = shipListView.selectionModel
          selectionModel?.selectionMode = SelectionMode.SINGLE
          shipListView.setCellFactory {
              val cell = ListCell<String?>()
              cell.textProperty().bind(cell.itemProperty())
              cell.addEventFilter(MouseEvent.MOUSE_PRESSED) { event: MouseEvent ->
                  shipListView.requestFocus()
                  if (!cell.isEmpty) {
                      val index = cell.index
                      if (selectionModel!!.selectedIndices.contains(index)) {
                          selectionModel.clearSelection()
                      } else {
                          selectionModel.select(index)
                      }
                      event.consume()
                  }
              }
              cell
          }
      }
  }