VesselListPanelController.kt 2.17 KB
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.event.EventHandler
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()

    private var shipList: ObservableList<String?> = FXCollections.observableArrayList()

    override fun initialize(location: URL?, resources: ResourceBundle?) {


        shipListView.items = shipList
        observableVessel.listeners.add(this)
        shipListView.selectionModel.selectedItemProperty().addListener { _, _, newValue ->
            if (newValue == null) {
                observableSelectedVessel.vessel = Vessel(null)
            } else {
                observableSelectedVessel.vessel = observableVessel.vessels[newValue]!!
            }
        }
        setCellFactory()
    }

    override fun onValueChanged(newValue: MutableMap<String?, Vessel>) {
        shipList.clear()
        shipList.addAll(newValue.keys)
    }

    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
        }
    }
}