Blame view

src/main/kotlin/application/controller/VesselListPanelController.kt 2.27 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
b350f9dfe   lsagona   import vessels an...
9
10
  import javafx.fxml.FXML
  import javafx.fxml.Initializable
78935bd62   lsagona   slider bind to al...
11
12
13
14
  import javafx.scene.control.ListCell
  import javafx.scene.control.ListView
  import javafx.scene.control.MultipleSelectionModel
  import javafx.scene.control.SelectionMode
f39d90e60   lsagona   Select/deselect MMSI
15
  import javafx.scene.input.MouseEvent
b350f9dfe   lsagona   import vessels an...
16
17
  import java.net.URL
  import java.util.*
f39d90e60   lsagona   Select/deselect MMSI
18

2bbe36a1b   lsagona   addd the possibil...
19
  class VesselListPanelController : Initializable, MessageListener {
b350f9dfe   lsagona   import vessels an...
20
      @FXML
f39d90e60   lsagona   Select/deselect MMSI
21
      var shipListView: ListView<String?> = ListView()
b350f9dfe   lsagona   import vessels an...
22

f39d90e60   lsagona   Select/deselect MMSI
23
      private var shipList: ObservableList<String?> = FXCollections.observableArrayList()
b350f9dfe   lsagona   import vessels an...
24
25
  
      override fun initialize(location: URL?, resources: ResourceBundle?) {
f39d90e60   lsagona   Select/deselect MMSI
26

b350f9dfe   lsagona   import vessels an...
27
          shipListView.items = shipList
2bbe36a1b   lsagona   addd the possibil...
28
29
          observableVessel.listeners.add(this)
          shipListView.selectionModel.selectedItemProperty().addListener { _, _, newValue ->
f39d90e60   lsagona   Select/deselect MMSI
30
31
32
33
34
              if (newValue == null) {
                  observableSelectedVessel.vessel = Vessel(null)
              } else {
                  observableSelectedVessel.vessel = observableVessel.vessels[newValue]!!
              }
2bbe36a1b   lsagona   addd the possibil...
35
          }
f39d90e60   lsagona   Select/deselect MMSI
36
          setCellFactory()
b350f9dfe   lsagona   import vessels an...
37
      }
f39d90e60   lsagona   Select/deselect MMSI
38
      override fun onValueChanged(newValue: MutableMap<String?, Vessel>) {
b350f9dfe   lsagona   import vessels an...
39
40
41
          shipList.clear()
          shipList.addAll(newValue.keys)
      }
f39d90e60   lsagona   Select/deselect MMSI
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      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
          }
      }
  }