From b350f9dfe4c8f6b30a7b4500ec7b3eeb86644afd Mon Sep 17 00:00:00 2001 From: lsagona Date: Thu, 16 Jul 2020 10:49:00 +0200 Subject: [PATCH] import vessels and display it on a ListView --- build.gradle | 8 ----- src/main/kotlin/application/App.kt | 8 ++--- .../controller/ControlPanelController.kt | 33 +++++++++++++++++ .../application/controller/MenuBarController.kt | 37 ++++++++++++++++++++ src/main/kotlin/application/model/Context.kt | 3 ++ src/main/kotlin/application/model/Message.kt | 23 ++++++++++++ .../kotlin/application/model/MessageListener.kt | 5 +++ .../kotlin/application/model/ObservableVessel.kt | 20 +++++++++++ src/main/kotlin/application/model/Vessel.kt | 8 +++++ .../kotlin/application/model/VesselGenerator.kt | 23 ++++++++++++ src/main/kotlin/application/ui/MenuBar.kt | 11 ------ src/main/kotlin/application/ui/ShipList.kt | 4 --- src/main/resources/gui/controlPanel.fxml | 17 +++++++++ src/main/resources/gui/mainApp.fxml | 37 -------------------- src/main/resources/gui/menuBar.fxml | 9 +++-- src/main/resources/gui/shipList.fxml | 7 ---- src/main/resources/gui/windows.fxml | 36 +++++++++++++++++++ 17 files changed, 211 insertions(+), 78 deletions(-) create mode 100644 src/main/kotlin/application/controller/ControlPanelController.kt create mode 100644 src/main/kotlin/application/controller/MenuBarController.kt create mode 100644 src/main/kotlin/application/model/Context.kt create mode 100644 src/main/kotlin/application/model/Message.kt create mode 100644 src/main/kotlin/application/model/MessageListener.kt create mode 100644 src/main/kotlin/application/model/ObservableVessel.kt create mode 100644 src/main/kotlin/application/model/Vessel.kt create mode 100644 src/main/kotlin/application/model/VesselGenerator.kt delete mode 100644 src/main/kotlin/application/ui/MenuBar.kt delete mode 100644 src/main/kotlin/application/ui/ShipList.kt create mode 100644 src/main/resources/gui/controlPanel.fxml delete mode 100644 src/main/resources/gui/mainApp.fxml delete mode 100644 src/main/resources/gui/shipList.fxml create mode 100644 src/main/resources/gui/windows.fxml diff --git a/build.gradle b/build.gradle index 93073d9..21964f8 100644 --- a/build.gradle +++ b/build.gradle @@ -15,14 +15,6 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' } -sourceSets { - main { - resources { - srcDirs "src/main/resources" - } - } -} - compileKotlin { kotlinOptions.jvmTarget = "1.8" } diff --git a/src/main/kotlin/application/App.kt b/src/main/kotlin/application/App.kt index d407a7f..fef4886 100644 --- a/src/main/kotlin/application/App.kt +++ b/src/main/kotlin/application/App.kt @@ -11,18 +11,14 @@ class App : Application() { override fun start(primaryStage: Stage?) { - val fxmlLoader = FXMLLoader(App::class.java.getResource("/gui/mainApp.fxml")) - val parent: Parent = fxmlLoader.load() - -// val controller: MainAppController = fxmlLoader.getController() + val fxmlLoader = FXMLLoader(App::class.java.getResource("/gui/windows.fxml")) + val parent: Parent = fxmlLoader.load() val scene = Scene(parent) primaryStage!!.scene = scene primaryStage.title = "Maritime Visualisation" - primaryStage.width = 667.0 - primaryStage.height = 375.0 primaryStage.show() } diff --git a/src/main/kotlin/application/controller/ControlPanelController.kt b/src/main/kotlin/application/controller/ControlPanelController.kt new file mode 100644 index 0000000..4764dd4 --- /dev/null +++ b/src/main/kotlin/application/controller/ControlPanelController.kt @@ -0,0 +1,33 @@ +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/MenuBarController.kt b/src/main/kotlin/application/controller/MenuBarController.kt new file mode 100644 index 0000000..9ac032e --- /dev/null +++ b/src/main/kotlin/application/controller/MenuBarController.kt @@ -0,0 +1,37 @@ +package application.controller + +import application.App +import application.model.createVesselCollection +import application.model.observableMessages +import javafx.event.EventHandler +import javafx.fxml.FXML +import javafx.fxml.FXMLLoader +import javafx.fxml.Initializable +import javafx.scene.Parent +import javafx.scene.control.MenuBar +import javafx.scene.control.MenuItem +import javafx.stage.FileChooser +import java.net.URL +import java.util.* + +class MenuBarController : Initializable { + + @FXML + var menuBar: MenuBar = MenuBar() + + @FXML + var import: MenuItem = MenuItem() + + override fun initialize(location: URL?, resources: ResourceBundle?) { + + import.onAction = EventHandler { + val fileChooser = FileChooser() + fileChooser.title = "Choose a file to import" + val window = menuBar.scene.window + val file = fileChooser.showOpenDialog(window) + val vessels = createVesselCollection(file) + observableMessages.vessels.clear() + observableMessages.vessels = vessels + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/Context.kt b/src/main/kotlin/application/model/Context.kt new file mode 100644 index 0000000..64a624c --- /dev/null +++ b/src/main/kotlin/application/model/Context.kt @@ -0,0 +1,3 @@ +package application.model + +val observableMessages : ObservableVessel = ObservableVessel() \ No newline at end of file diff --git a/src/main/kotlin/application/model/Message.kt b/src/main/kotlin/application/model/Message.kt new file mode 100644 index 0000000..32f1209 --- /dev/null +++ b/src/main/kotlin/application/model/Message.kt @@ -0,0 +1,23 @@ +package application.model + +import java.time.LocalDateTime + +class Message(split: List) { + val mmsi: Int? = split[0].toIntOrNull() + val time: LocalDateTime = LocalDateTime.parse(split[1]) + val latitude: Double? = split[2].toDoubleOrNull() + val longitude: Double? = split[3].toDoubleOrNull() + val speedOverGround: Double? = split[4].toDoubleOrNull() + val courseOverGround: Double? = split[5].toDoubleOrNull() + val heading: Int? = split[6].toIntOrNull() + val vesselName: String? = split[7] + val imo: String? = split[8] + val callSign: String? = split[9] + val vesselType: Int? = split[10].toIntOrNull() + val status: String? = split[11] + val length: Double? = split[12].toDoubleOrNull() + val width: Double? = split[13].toDoubleOrNull() + val draft: Double? = split[14].toDoubleOrNull() + val cargo: Int? = split[15].toIntOrNull() + +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/MessageListener.kt b/src/main/kotlin/application/model/MessageListener.kt new file mode 100644 index 0000000..6e05c27 --- /dev/null +++ b/src/main/kotlin/application/model/MessageListener.kt @@ -0,0 +1,5 @@ +package application.model + +interface MessageListener { + fun onValueChanged(newValue: MutableMap) +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/ObservableVessel.kt b/src/main/kotlin/application/model/ObservableVessel.kt new file mode 100644 index 0000000..6116dad --- /dev/null +++ b/src/main/kotlin/application/model/ObservableVessel.kt @@ -0,0 +1,20 @@ +package application.model + +import kotlin.properties.Delegates + +class ObservableVessel { + val listeners: MutableList = mutableListOf() + + var vessels: MutableMap by Delegates.observable( + initialValue = mutableMapOf(), + onChange = { + _, _, new -> + run { + listeners.forEach { + it.onValueChanged(new) + } + } + } + ) + +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/Vessel.kt b/src/main/kotlin/application/model/Vessel.kt new file mode 100644 index 0000000..319c3d9 --- /dev/null +++ b/src/main/kotlin/application/model/Vessel.kt @@ -0,0 +1,8 @@ +package application.model + +import java.time.LocalDateTime + + +class Vessel() { + val messages: MutableMap = mutableMapOf() +} \ No newline at end of file diff --git a/src/main/kotlin/application/model/VesselGenerator.kt b/src/main/kotlin/application/model/VesselGenerator.kt new file mode 100644 index 0000000..fe986bd --- /dev/null +++ b/src/main/kotlin/application/model/VesselGenerator.kt @@ -0,0 +1,23 @@ +package application.model + +import java.io.File + +fun createVesselCollection(file: File) : MutableMap { + val messages : ArrayList = arrayListOf() + val vessels: MutableMap = mutableMapOf() + + file.forEachLine { + val arrayMessage = it.split(",") + if (arrayMessage[0].toIntOrNull() !== null) { + val message = Message(arrayMessage) + messages.add(message) + if (!vessels.containsKey(message.mmsi)){ + vessels[message.mmsi] = Vessel() + } + vessels[message.mmsi]?.messages?.set(message.time, message) + } + + } + + return vessels +} diff --git a/src/main/kotlin/application/ui/MenuBar.kt b/src/main/kotlin/application/ui/MenuBar.kt deleted file mode 100644 index 9d7873a..0000000 --- a/src/main/kotlin/application/ui/MenuBar.kt +++ /dev/null @@ -1,11 +0,0 @@ -package application.ui - -import javafx.fxml.FXML - -class MenuBar { - - @FXML - fun initialize() { - } - -} \ No newline at end of file diff --git a/src/main/kotlin/application/ui/ShipList.kt b/src/main/kotlin/application/ui/ShipList.kt deleted file mode 100644 index edb78e0..0000000 --- a/src/main/kotlin/application/ui/ShipList.kt +++ /dev/null @@ -1,4 +0,0 @@ -package application.ui - -class ShipList { -} \ No newline at end of file diff --git a/src/main/resources/gui/controlPanel.fxml b/src/main/resources/gui/controlPanel.fxml new file mode 100644 index 0000000..e5784da --- /dev/null +++ b/src/main/resources/gui/controlPanel.fxml @@ -0,0 +1,17 @@ + + + + + + + + + + +