VesselListPanelController.kt
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package application.controller
import application.model.MessageListener
import application.model.Vessel
import application.model.observableSelectedVessel
import application.model.observableVessel
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.collections.transformation.FilteredList
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.*
import javafx.scene.input.MouseEvent
import java.net.URL
import java.util.*
class VesselListPanelController : Initializable, MessageListener {
@FXML
var shipListView: ListView<String?> = ListView()
@FXML
var filterInput: TextField = TextField()
private var shipList: ObservableList<String?> = FXCollections.observableArrayList()
private val filterMMSI = FilteredList(shipList)
override fun initialize(location: URL?, resources: ResourceBundle?) {
shipListView.items = filterMMSI
observableVessel.listeners.add(this)
shipListView.selectionModel.selectedItemProperty().addListener { _, _, newValue ->
if (newValue == null) {
observableSelectedVessel.value = Vessel(null)
} else {
observableSelectedVessel.value = observableVessel.vessels[newValue]!!
}
}
setCellFactory()
setFilterTextListener()
}
override fun onValueChanged(newValue: MutableMap<String?, Vessel>) {
shipList.clear()
shipList.addAll(newValue.keys)
}
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) }
}
}
}
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
}
}
}