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.chart.CategoryAxis import javafx.scene.chart.NumberAxis import javafx.scene.chart.ScatterChart import javafx.scene.chart.XYChart.Data import javafx.scene.chart.XYChart.Series import javafx.scene.control.ListCell import javafx.scene.control.ListView import java.net.URL import java.util.* class DataPanelController : Initializable, SelectedVesselListener { private var dataList: ObservableList>> = FXCollections.observableArrayList() private lateinit var timeData: ArrayList @FXML var dataListView = ListView>>() @FXML var xaxisNumber: CategoryAxis = CategoryAxis() @FXML var yaxisNumber: NumberAxis = NumberAxis() @FXML lateinit var scatterChartNumber: ScatterChart @FXML var xaxisCategory: CategoryAxis = CategoryAxis() @FXML var yaxisCategory: CategoryAxis = CategoryAxis() @FXML lateinit var scatterChartCategory: ScatterChart override fun initialize(location: URL?, resources: ResourceBundle?) { xaxisNumber.animated = false yaxisNumber.animated = false xaxisCategory.animated = false yaxisCategory.animated = false setObservableSelectedVesselListener() dataListView.items = dataList dataListView.setCellFactory { object : ListCell>?>() { override fun updateItem(item: Pair>?, empty: Boolean) { super.updateItem(item, empty) text = if (empty) { null } else { item?.first } } } } dataListView.selectionModel.selectedItemProperty().addListener { _, _, newValue -> if (newValue == null){ scatterChartCategory.data.clear() scatterChartNumber.data.clear() return@addListener } val serieNumber = Series() val serieString = Series() val getValueVisitorX = GetValueVisitor() val getValueVisitorY = GetValueVisitor() for (x in 0 until newValue?.second?.size!!) { timeData[x]?.accept(getValueVisitorX) newValue.second[x]?.accept(getValueVisitorY) if (getValueVisitorY.value.toDoubleOrNull() == null){ serieString.data.add(Data(getValueVisitorX.value, getValueVisitorY.value)) } else{ serieNumber.data.add(Data(getValueVisitorX.value, getValueVisitorY.value.toDouble())) } } scatterChartNumber.data.clear() scatterChartCategory.data.clear() if (getValueVisitorY.value.toDoubleOrNull() == null){ serieString.data.add(Data(getValueVisitorX.value, getValueVisitorY.value)) scatterChartCategory.data.addAll(serieString) setChartCategoryVisible() xaxisCategory.label = "Date" yaxisCategory.label = newValue.first } else{ serieNumber.data.add(Data(getValueVisitorX.value, getValueVisitorY.value.toDouble())) scatterChartNumber.data.addAll(serieNumber) setChartNumberVisible() xaxisNumber.label = "Date" yaxisNumber.label = newValue.first } } } 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 { val allTime: ArrayList = vessel.getAllTime() allTime.sortBy { (it as Time).value } return allTime } private fun populateLatitude(vessel: Vessel): ArrayList { val allLatitude: ArrayList = vessel.getAllLatitude() allLatitude.sortBy { (it as Latitude).value } return allLatitude } private fun populateLongitude(vessel: Vessel): ArrayList { val allLongitude: ArrayList = vessel.getAllLongitude() allLongitude.sortBy { (it as Longitude).value } return allLongitude } private fun populateSpeedOverGround(vessel: Vessel): ArrayList { val allSpeedOverGround: ArrayList = vessel.getAllSpeedOverGround() allSpeedOverGround.sortBy { (it as SpeedOverGround).value } return allSpeedOverGround } private fun populateCourseOverGround(vessel: Vessel): ArrayList { val allCourseOverGround: ArrayList = vessel.getAllCourseOverGround() allCourseOverGround.sortBy { (it as CourseOverGround).value } return allCourseOverGround } private fun populateHeading(vessel: Vessel): ArrayList { val allHeading: ArrayList = vessel.getAllHeading() allHeading.sortBy { (it as Heading).value } return allHeading } private fun populatVesselName(vessel: Vessel): ArrayList { val allVesselName: ArrayList = vessel.getAllVesselName() allVesselName.sortBy { (it as VesselName).value } return allVesselName } private fun populatIMO(vessel: Vessel): ArrayList { val allIMO: ArrayList = vessel.getAllIMO() allIMO.sortBy { (it as IMO).value } return allIMO } private fun populatCallSign(vessel: Vessel): ArrayList { val allCallSign: ArrayList = vessel.getAllCallSign() allCallSign.sortBy { (it as CallSign).value } return allCallSign } private fun populatVesselType(vessel: Vessel): ArrayList { val allVesselType: ArrayList = vessel.getAllVesselType() allVesselType.sortBy { (it as VesselType).value } return allVesselType } private fun populatStatus(vessel: Vessel): ArrayList { val allStatus: ArrayList = vessel.getAllStatus() allStatus.sortBy { (it as Status).value } return allStatus } private fun populatLength(vessel: Vessel): ArrayList { val allLength: ArrayList = vessel.getAllLength() allLength.sortBy { (it as Length).value } return allLength } private fun populatWidth(vessel: Vessel): ArrayList { val allWidth: ArrayList = vessel.getAllWidth() allWidth.sortBy { (it as Width).value } return allWidth } private fun populatDraft(vessel: Vessel): ArrayList { val allDraft: ArrayList = vessel.getAllDraft() allDraft.sortBy { (it as Draft).value } return allDraft } private fun populatCargo(vessel: Vessel): ArrayList { val allCargo: ArrayList = vessel.getAllCargo() allCargo.sortBy { (it as Cargo).value } return allCargo } private fun populateDataList(vessel: Vessel) { val data = arrayListOf>>() 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", populatVesselName(vessel))) data.add(Pair("IMO", populatIMO(vessel))) data.add(Pair("Call Sign", populatCallSign(vessel))) data.add(Pair("Vessel Type", populatVesselType(vessel))) data.add(Pair("Status", populatStatus(vessel))) data.add(Pair("Length", populatLength(vessel))) data.add(Pair("Width", populatWidth(vessel))) data.add(Pair("Draft", populatDraft(vessel))) data.add(Pair("Cargo", populatCargo(vessel))) dataList.addAll(data) } override fun onValueChanged(newValue: Vessel) { dataList.clear() populateDataList(newValue) } }