From 2bbe36a1be826b47c824114b471c6a1e5de02983 Mon Sep 17 00:00:00 2001 From: lsagona Date: Tue, 21 Jul 2020 13:24:26 +0200 Subject: [PATCH] addd the possibility to select a ship --- .../controller/ControlPanelController.kt | 33 ------------------ .../application/controller/MapPanelController.kt | 22 +++++++----- .../application/controller/MenuBarController.kt | 6 ++-- .../controller/VesselListPanelController.kt | 35 ++++++++++++++++++++ src/main/kotlin/application/model/Context.kt | 4 ++- .../application/model/ObservableSelectedVessel.kt | 18 ++++++++++ .../application/model/SelectedVesselListener.kt | 5 +++ src/main/kotlin/application/model/Vessel.kt | 2 +- .../kotlin/application/model/VesselGenerator.kt | 2 +- src/main/kotlin/map/CircleMarkerGenerator.kt | 17 ++++++++-- src/main/resources/gui/controlPanel.fxml | 17 ---------- src/main/resources/gui/vesselListPanel.fxml | 17 ++++++++++ src/main/resources/gui/windows.fxml | 2 +- 13 files changed, 112 insertions(+), 68 deletions(-) delete mode 100644 src/main/kotlin/application/controller/ControlPanelController.kt create mode 100644 src/main/kotlin/application/controller/VesselListPanelController.kt create mode 100644 src/main/kotlin/application/model/ObservableSelectedVessel.kt create mode 100644 src/main/kotlin/application/model/SelectedVesselListener.kt delete mode 100644 src/main/resources/gui/controlPanel.fxml create mode 100644 src/main/resources/gui/vesselListPanel.fxml diff --git a/src/main/kotlin/application/controller/ControlPanelController.kt b/src/main/kotlin/application/controller/ControlPanelController.kt deleted file mode 100644 index 4764dd4..0000000 --- a/src/main/kotlin/application/controller/ControlPanelController.kt +++ /dev/null @@ -1,33 +0,0 @@ -package application.controller - -import application.model.MessageListener -import application.model.Vessel -import application.model.observableMessages -import javafx.collections.FXCollections -import javafx.collections.ObservableList -import javafx.event.EventHandler -import javafx.fxml.FXML -import javafx.fxml.Initializable -import javafx.scene.control.Button -import javafx.scene.control.ListView -import java.net.URL -import java.util.* - -class ControlPanelController : Initializable, MessageListener { - @FXML - var shipListView: ListView = ListView() - - - var shipList: ObservableList = FXCollections.observableArrayList() - - override fun initialize(location: URL?, resources: ResourceBundle?) { - shipListView.items = shipList - observableMessages.listeners.add(this) - } - - override fun onValueChanged(newValue: MutableMap) { - shipList.clear() - shipList.addAll(newValue.keys) - } - -} \ No newline at end of file diff --git a/src/main/kotlin/application/controller/MapPanelController.kt b/src/main/kotlin/application/controller/MapPanelController.kt index 742802a..374f70a 100644 --- a/src/main/kotlin/application/controller/MapPanelController.kt +++ b/src/main/kotlin/application/controller/MapPanelController.kt @@ -1,8 +1,6 @@ package application.controller -import application.model.MessageListener -import application.model.Vessel -import application.model.observableMessages +import application.model.* import javafx.concurrent.Worker import javafx.fxml.FXML import javafx.fxml.Initializable @@ -14,7 +12,7 @@ import java.net.URL import java.util.* import java.util.concurrent.CompletableFuture -class MapPanelController : Initializable, MessageListener { +class MapPanelController : Initializable { @FXML private lateinit var map: StackPane @@ -24,7 +22,17 @@ class MapPanelController : Initializable, MessageListener { override fun initialize(location: URL?, resources: ResourceBundle?) { val completeFutureMap: CompletableFuture = mapView.displayMap(MapConfig()) - observableMessages.listeners.add(this) + observableVessel.listeners.add(object : MessageListener { + override fun onValueChanged(newValue: MutableMap) { + displayMessageOnMap(mapView) + } + }) + + observableSelectedVessel.listeners.add(object : SelectedVesselListener { + override fun onValueChanged(newValue: Vessel) { + displayMessageOnMap(mapView, newValue.mmsi!!) + } + }) /*completeFutureMap.whenComplete{ workerState, _ -> @@ -34,9 +42,5 @@ class MapPanelController : Initializable, MessageListener { map.children.add(mapView) } - override fun onValueChanged(newValue: MutableMap) { - displayMessageOnMap(mapView) - } - } \ No newline at end of file diff --git a/src/main/kotlin/application/controller/MenuBarController.kt b/src/main/kotlin/application/controller/MenuBarController.kt index 6c0d69f..1f3f502 100644 --- a/src/main/kotlin/application/controller/MenuBarController.kt +++ b/src/main/kotlin/application/controller/MenuBarController.kt @@ -1,7 +1,7 @@ package application.controller import application.model.createVesselCollection -import application.model.observableMessages +import application.model.observableVessel import javafx.event.EventHandler import javafx.fxml.FXML import javafx.fxml.Initializable @@ -27,8 +27,8 @@ class MenuBarController : Initializable { val window = menuBar.scene.window val file = fileChooser.showOpenDialog(window) val vessels = createVesselCollection(file) - observableMessages.vessels.clear() - observableMessages.vessels = vessels + observableVessel.vessels.clear() + observableVessel.vessels = vessels } } } \ No newline at end of file diff --git a/src/main/kotlin/application/controller/VesselListPanelController.kt b/src/main/kotlin/application/controller/VesselListPanelController.kt new file mode 100644 index 0000000..a74069f --- /dev/null +++ b/src/main/kotlin/application/controller/VesselListPanelController.kt @@ -0,0 +1,35 @@ +package application.controller + +import application.model.MessageListener +import application.model.Vessel +import application.model.observableVessel +import application.model.observableSelectedVessel +import javafx.collections.FXCollections +import javafx.collections.ObservableList +import javafx.fxml.FXML +import javafx.fxml.Initializable +import javafx.scene.control.ListView +import java.net.URL +import java.util.* + +class VesselListPanelController : Initializable, MessageListener { + @FXML + var shipListView: ListView = ListView() + + + var shipList: ObservableList = FXCollections.observableArrayList() + + override fun initialize(location: URL?, resources: ResourceBundle?) { + shipListView.items = shipList + observableVessel.listeners.add(this) + shipListView.selectionModel.selectedItemProperty().addListener { _, _, newValue -> + observableSelectedVessel.vessel = observableVessel.vessels[newValue]!! + } + } + + override fun onValueChanged(newValue: MutableMap) { + shipList.clear() + shipList.addAll(newValue.keys) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/Context.kt b/src/main/kotlin/application/model/Context.kt index 64a624c..34f5277 100644 --- a/src/main/kotlin/application/model/Context.kt +++ b/src/main/kotlin/application/model/Context.kt @@ -1,3 +1,5 @@ package application.model -val observableMessages : ObservableVessel = ObservableVessel() \ No newline at end of file +val observableVessel: ObservableVessel = ObservableVessel() + +val observableSelectedVessel: ObservableSelectedVessel = ObservableSelectedVessel() \ No newline at end of file diff --git a/src/main/kotlin/application/model/ObservableSelectedVessel.kt b/src/main/kotlin/application/model/ObservableSelectedVessel.kt new file mode 100644 index 0000000..6d8fb37 --- /dev/null +++ b/src/main/kotlin/application/model/ObservableSelectedVessel.kt @@ -0,0 +1,18 @@ +package application.model + +import kotlin.properties.Delegates + +class ObservableSelectedVessel { + val listeners: MutableList = mutableListOf() + + var vessel: Vessel by Delegates.observable( + initialValue = Vessel(null), + onChange = { _, _, new -> + run { + listeners.forEach { + it.onValueChanged(new) + } + } + } + ) +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/SelectedVesselListener.kt b/src/main/kotlin/application/model/SelectedVesselListener.kt new file mode 100644 index 0000000..292bbbe --- /dev/null +++ b/src/main/kotlin/application/model/SelectedVesselListener.kt @@ -0,0 +1,5 @@ +package application.model + +interface SelectedVesselListener { + fun onValueChanged(newValue: Vessel) +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/Vessel.kt b/src/main/kotlin/application/model/Vessel.kt index bdd5764..9a1c30a 100644 --- a/src/main/kotlin/application/model/Vessel.kt +++ b/src/main/kotlin/application/model/Vessel.kt @@ -4,6 +4,6 @@ import java.time.LocalDateTime import java.util.* -class Vessel { +class Vessel(val mmsi : Int?) { val messages: SortedMap = sortedMapOf() } \ No newline at end of file diff --git a/src/main/kotlin/application/model/VesselGenerator.kt b/src/main/kotlin/application/model/VesselGenerator.kt index 9b40dbf..7222ef6 100644 --- a/src/main/kotlin/application/model/VesselGenerator.kt +++ b/src/main/kotlin/application/model/VesselGenerator.kt @@ -14,7 +14,7 @@ fun createVesselCollection(file: File) : SortedMap { val message = Message(arrayMessage) messages.add(message) if (!vessels.containsKey(message.mmsi)){ - vessels[message.mmsi] = Vessel() + vessels[message.mmsi] = Vessel(message.mmsi!!) } vessels[message.mmsi]?.messages?.set(message.time, message) } diff --git a/src/main/kotlin/map/CircleMarkerGenerator.kt b/src/main/kotlin/map/CircleMarkerGenerator.kt index cf81c8d..fd678a1 100644 --- a/src/main/kotlin/map/CircleMarkerGenerator.kt +++ b/src/main/kotlin/map/CircleMarkerGenerator.kt @@ -1,6 +1,6 @@ package map -import application.model.observableMessages +import application.model.observableVessel fun clearMapCanvas(map: LeafletMapView) { map.execScript(""" @@ -11,9 +11,22 @@ fun clearMapCanvas(map: LeafletMapView) { fun displayMessageOnMap(map: LeafletMapView) { clearMapCanvas(map) - observableMessages.vessels.forEach { (_, value) -> + observableVessel.vessels.forEach { (_, value) -> value.messages.forEach { (_, message) -> map.execScript("L.circleMarker([${message.latitude}, ${message.longitude}], {renderer: myRenderer, radius: 0.01, color: '#${message.getHexColor()}'}).addTo(myMap)") } } +} + +fun displayMessageOnMap(map: LeafletMapView, selectedMMSI: Int) { + clearMapCanvas(map) + observableVessel.vessels.forEach { (_, value) -> + value.messages.forEach { (_, message) -> + if(selectedMMSI == message.mmsi){ + map.execScript("L.circleMarker([${message.latitude}, ${message.longitude}], {renderer: myRenderer, radius: 4, color: '#ff001e'}).addTo(myMap)") + }else{ + map.execScript("L.circleMarker([${message.latitude}, ${message.longitude}], {renderer: myRenderer, radius: 0.01, color: '#${message.getHexColor()}'}).addTo(myMap)") + } + } + } } \ No newline at end of file diff --git a/src/main/resources/gui/controlPanel.fxml b/src/main/resources/gui/controlPanel.fxml deleted file mode 100644 index e5784da..0000000 --- a/src/main/resources/gui/controlPanel.fxml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - -