DataPanelController.kt 11.1 KB
package application.controller

import application.model.*
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.ListCell
import javafx.scene.control.ListView
import javafx.scene.layout.StackPane
import org.charts.dataviewer.api.config.DataViewerConfiguration
import org.charts.dataviewer.api.data.PlotData
import org.charts.dataviewer.api.trace.LineTrace
import org.charts.dataviewer.api.trace.ScatterTrace
import org.charts.dataviewer.javafx.JavaFxDataViewer
import org.charts.dataviewer.utils.TraceColour
import org.charts.dataviewer.utils.TraceMode
import org.charts.dataviewer.utils.TraceVisibility
import java.net.URL
import java.util.*


class DataPanelController : Initializable, SelectedVesselListener {
    private var dataList: ObservableList<Pair<String, ArrayList<MessageData?>>> = FXCollections.observableArrayList()
    private lateinit var timeData: ArrayList<MessageData?>


    @FXML
    var dataListView = ListView<Pair<String, ArrayList<MessageData?>>>()

//    @FXML
//    var stackPanePlot = StackPane()

    @FXML
    var dataViewer = JavaFxDataViewer()

//    @FXML
//    var xaxisNumber: CategoryAxis = CategoryAxis()
//
//    @FXML
//    var yaxisNumber: NumberAxis = NumberAxis()
//
//    @FXML
//    lateinit var scatterChartNumber: ScatterChart<String, Number>
//
//    @FXML
//    var xaxisCategory: CategoryAxis = CategoryAxis()
//
//    @FXML
//    var yaxisCategory: CategoryAxis = CategoryAxis()
//
//    @FXML
//    lateinit var scatterChartCategory: ScatterChart<String, String>

    override fun initialize(location: URL?, resources: ResourceBundle?) {
//        xaxisNumber.animated = false
//        yaxisNumber.animated = false
//        xaxisCategory.animated = false
//        yaxisCategory.animated = false
        setObservableSelectedVesselListener()
        dataListView.items = dataList


        val plotData = PlotData()
        plotData.addTrace(createScatterTrace())
        val config = DataViewerConfiguration()
        config.showLegend(true)
        config.setLegendInsidePlot(false)

        dataListView.setCellFactory {
            object : ListCell<Pair<String, ArrayList<MessageData?>>?>() {
                override fun updateItem(item: Pair<String, ArrayList<MessageData?>>?, empty: Boolean) {
                    super.updateItem(item, empty)
                    text = if (empty) {
                        null
                    } else {
                        item?.first
                    }
                }
            }
        }

        dataListView.selectionModel.selectedItemProperty().addListener { _, _, newValue ->
            if (newValue == null) {
                plotData.allTraces.clear()
                config.setxAxisTitle("")
                config.setyAxisTitle("")
                dataViewer.resetPlot()

                return@addListener
            }

            val getValueVisitorX = GetValueVisitor()
            val getValueVisitorY = GetValueVisitor()

            val arrayListStringX = arrayListOf<String>()
            val arrayListDoubleX = arrayListOf<Double>()
            val arrayListStringY = arrayListOf<String>()
            val arrayListDoubleY = arrayListOf<Double>()

            for (x in 0 until newValue.second.size) {
                timeData[x]?.accept(getValueVisitorX)
                newValue.second[x]?.accept(getValueVisitorY)

                if (getValueVisitorY.value.toDoubleOrNull() == null) {
                    arrayListStringX.add(getValueVisitorX.value)
                    arrayListStringY.add(getValueVisitorY.value)
                } else {
                    arrayListStringX.add(getValueVisitorX.value)
                    arrayListDoubleY.add(getValueVisitorY.value.toDouble())
                }
            }

            val scatterTrace = ScatterTrace<Any>()
            scatterTrace.traceColour = TraceColour.RED
            scatterTrace.traceVisibility = TraceVisibility.TRUE

            val serieStringX: Array<String> = arrayListStringX.toTypedArray()
//            val serieDoubleX: Array<Double> = arrayListDoubleX.toTypedArray()
            val serieStringY: Array<String> = arrayListStringY.toTypedArray()
            val serieDoubleY: Array<Double> = arrayListDoubleY.toTypedArray()

            if (getValueVisitorY.value.toDoubleOrNull() == null) {
                scatterTrace.setxArray(serieStringX)
                scatterTrace.setyArray(serieStringY)
            } else {
                scatterTrace.setxArray(serieStringX)
                scatterTrace.setyArray(serieDoubleY)
            }

            config.setxAxisTitle("Date")
            config.setyAxisTitle(newValue.first)
            dataViewer.resetPlot()
            plotData.allTraces.clear()
            plotData.addTrace(scatterTrace)
            dataViewer.updateConfiguration(config)
            dataViewer.updatePlot(plotData)

        }

        plotData.allTraces.clear()
//        plotData.addTrace(createScatterTrace())
        config.setxAxisTitle("")
        config.setyAxisTitle("")
        config.plotTitle = "No data selected"
        dataViewer.updateConfiguration(config)
        dataViewer.updatePlot(plotData)

//        stackPanePlot.children.add(dataViewer)

    }

    private fun createScatterTrace(): ScatterTrace<*>? {
        val scatterTrace = ScatterTrace<Any>()
        scatterTrace.setxArray(arrayOf("asdf", "fdsa", "asdfffe", "asdfe", "asfee3"))
        scatterTrace.setyArray(arrayOf("pppp", "koojoi", "pp", "ii", "rty", "ert"))
        scatterTrace.traceName = "MyScatterTrace"
        scatterTrace.traceColour = TraceColour.PURPLE
        scatterTrace.traceMode = TraceMode.MARKERS
        return scatterTrace
    }

    fun createLineTrace(): LineTrace<*>? {
        val lineTrace = LineTrace<Any>()
        lineTrace.setxArray(arrayOf("asdf", "fdsa", "asdfffe", "asdfe", "asfee3"))
        lineTrace.setyArray(arrayOf(0.0, 1.0, 2.0, 3.0, 4.0, 5.0))
        lineTrace.traceName = "MyLineTrace"
        lineTrace.traceColour = TraceColour.PURPLE
        return lineTrace
    }

//    private fun setChartCategoryVisible() {
//        scatterChartCategory.isVisible = true
//        scatterChartNumber.isVisible = false
//    }
//
//    private fun setChartNumberVisible() {
//        scatterChartCategory.isVisible = false
//        scatterChartNumber.isVisible = true
//    }

    private fun setObservableSelectedVesselListener() {
        observableSelectedVessel.listeners.add(this)
    }

    private fun populateTime(vessel: Vessel): ArrayList<MessageData?> {
        val allTime: ArrayList<MessageData?> = vessel.getAllTime()
        allTime.sortBy { (it as Time).value }

        return allTime
    }

    private fun populateLatitude(vessel: Vessel): ArrayList<MessageData?> {
        val allLatitude: ArrayList<MessageData?> = vessel.getAllLatitude()
        allLatitude.sortBy { (it as Latitude).value }

        return allLatitude
    }

    private fun populateLongitude(vessel: Vessel): ArrayList<MessageData?> {
        val allLongitude: ArrayList<MessageData?> = vessel.getAllLongitude()
        allLongitude.sortBy { (it as Longitude).value }

        return allLongitude
    }

    private fun populateSpeedOverGround(vessel: Vessel): ArrayList<MessageData?> {
        val allSpeedOverGround: ArrayList<MessageData?> = vessel.getAllSpeedOverGround()
        allSpeedOverGround.sortBy { (it as SpeedOverGround).value }

        return allSpeedOverGround
    }

    private fun populateCourseOverGround(vessel: Vessel): ArrayList<MessageData?> {
        val allCourseOverGround: ArrayList<MessageData?> = vessel.getAllCourseOverGround()
        allCourseOverGround.sortBy { (it as CourseOverGround).value }

        return allCourseOverGround
    }

    private fun populateHeading(vessel: Vessel): ArrayList<MessageData?> {
        val allHeading: ArrayList<MessageData?> = vessel.getAllHeading()
        allHeading.sortBy { (it as Heading).value }

        return allHeading
    }

    private fun populateVesselName(vessel: Vessel): ArrayList<MessageData?> {
        val allVesselName: ArrayList<MessageData?> = vessel.getAllVesselName()
        allVesselName.sortBy { (it as VesselName).value }

        return allVesselName
    }

    private fun populateIMO(vessel: Vessel): ArrayList<MessageData?> {
        val allIMO: ArrayList<MessageData?> = vessel.getAllIMO()
        allIMO.sortBy { (it as IMO).value }

        return allIMO
    }

    private fun populateCallSign(vessel: Vessel): ArrayList<MessageData?> {
        val allCallSign: ArrayList<MessageData?> = vessel.getAllCallSign()
        allCallSign.sortBy { (it as CallSign).value }

        return allCallSign
    }

    private fun populateVesselType(vessel: Vessel): ArrayList<MessageData?> {
        val allVesselType: ArrayList<MessageData?> = vessel.getAllVesselType()
        allVesselType.sortBy { (it as VesselType).value }

        return allVesselType
    }

    private fun populateStatus(vessel: Vessel): ArrayList<MessageData?> {
        val allStatus: ArrayList<MessageData?> = vessel.getAllStatus()
        allStatus.sortBy { (it as Status).value }

        return allStatus
    }

    private fun populateLength(vessel: Vessel): ArrayList<MessageData?> {
        val allLength: ArrayList<MessageData?> = vessel.getAllLength()
        allLength.sortBy { (it as Length).value }

        return allLength
    }

    private fun populateWidth(vessel: Vessel): ArrayList<MessageData?> {
        val allWidth: ArrayList<MessageData?> = vessel.getAllWidth()
        allWidth.sortBy { (it as Width).value }

        return allWidth
    }

    private fun populateDraft(vessel: Vessel): ArrayList<MessageData?> {
        val allDraft: ArrayList<MessageData?> = vessel.getAllDraft()
        allDraft.sortBy { (it as Draft).value }

        return allDraft
    }

    private fun populateCargo(vessel: Vessel): ArrayList<MessageData?> {
        val allCargo: ArrayList<MessageData?> = vessel.getAllCargo()
        allCargo.sortBy { (it as Cargo).value }

        return allCargo
    }

    private fun populateDataList(vessel: Vessel) {
        val data = arrayListOf<Pair<String, ArrayList<MessageData?>>>()

        timeData = populateTime(vessel)

        data.add(Pair("Latitude", populateLatitude(vessel)))

        data.add(Pair("Longitude", populateLongitude(vessel)))

        data.add(Pair("Speed Over Ground", populateSpeedOverGround(vessel)))

        data.add(Pair("Course Over Ground", populateCourseOverGround(vessel)))

        data.add(Pair("Heading", populateHeading(vessel)))

        data.add(Pair("Vessel Name", populateVesselName(vessel)))

        data.add(Pair("IMO", populateIMO(vessel)))

        data.add(Pair("Call Sign", populateCallSign(vessel)))

        data.add(Pair("Vessel Type", populateVesselType(vessel)))

        data.add(Pair("Status", populateStatus(vessel)))

        data.add(Pair("Length", populateLength(vessel)))

        data.add(Pair("Width", populateWidth(vessel)))

        data.add(Pair("Draft", populateDraft(vessel)))

        data.add(Pair("Cargo", populateCargo(vessel)))

        dataList.addAll(data)
    }

    override fun onValueChanged(newValue: Vessel) {
        dataList.clear()
        populateDataList(newValue)

    }

}