MapPanelController.kt 2.45 KB
package application.controller

import application.model.*
import application.model.State.*
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.layout.StackPane
import map.*
import java.net.URL
import java.util.*

class MapPanelController : Initializable {

    @FXML
    private lateinit var map: StackPane

    private val mapView = LeafletMapView()


    override fun initialize(location: URL?, resources: ResourceBundle?) {
        mapView.displayMap(MapConfig())
        setObservableVesselListener()
        setObservableSelectedVesselListener()
        setStateListener()
        /*val completeFutureMap: CompletableFuture<Worker.State> = mapView.displayMap(MapConfig())
        completeFutureMap.whenComplete{
            workerState, _ ->
            if (workerState == Worker.State.SUCCEEDED) {
            }
        }*/
        map.children.add(mapView)
        map.children
    }

    private fun setStateListener() {
        observableState.listeners.add(object : StateListener {
            override fun onValueChanged(newValue: State) {
                if (observableSelectedVessel.vessel.mmsi != null) {
                    updateMap(observableSelectedVessel.vessel.mmsi!!)
                } else {
                    updateMap()
                }
            }
        })
    }

    private fun updateMap() {
        when (observableState.state) {
            ALL_MESSAGES -> displayAllMessageOnMap(mapView)
            CLUSTERED_MESSAGES -> displayClusterMessageOnMap(mapView)
            HEAT_MAP -> displayHeatMapOnMap(mapView)
        }
    }

    private fun updateMap(selectedMMSI: Int) {
        when (observableState.state) {
            ALL_MESSAGES -> displayAllMessageOnMap(mapView, selectedMMSI)
            CLUSTERED_MESSAGES -> displayClusterMessageOnMap(mapView, selectedMMSI)
            HEAT_MAP -> displayHeatMapOnMap(mapView, selectedMMSI)
        }
    }

    private fun setObservableVesselListener() {
        observableVessel.listeners.add(object : MessageListener {
            override fun onValueChanged(newValue: MutableMap<Int?, Vessel>) {
                updateMap()
            }
        })
    }

    private fun setObservableSelectedVesselListener() {
        observableSelectedVessel.listeners.add(object : SelectedVesselListener {
            override fun onValueChanged(newValue: Vessel) {
                if (newValue.mmsi != null){
                    updateMap(newValue.mmsi)
                }
            }
        })
    }


}