Blame view

src/main/kotlin/application/controller/DataPanelController.kt 12.6 KB
513c0341c   lsagona   add chart
1
2
3
4
5
6
7
  package application.controller
  
  import application.model.*
  import javafx.collections.FXCollections
  import javafx.collections.ObservableList
  import javafx.fxml.FXML
  import javafx.fxml.Initializable
513c0341c   lsagona   add chart
8
9
  import javafx.scene.control.ListCell
  import javafx.scene.control.ListView
e220e082b   lsagona   graph binded to s...
10
11
  import kotlinx.coroutines.GlobalScope
  import kotlinx.coroutines.launch
f15a58907   lsagona   add better chart
12
13
  import org.charts.dataviewer.api.config.DataViewerConfiguration
  import org.charts.dataviewer.api.data.PlotData
f15a58907   lsagona   add better chart
14
15
16
  import org.charts.dataviewer.api.trace.ScatterTrace
  import org.charts.dataviewer.javafx.JavaFxDataViewer
  import org.charts.dataviewer.utils.TraceColour
f15a58907   lsagona   add better chart
17
  import org.charts.dataviewer.utils.TraceVisibility
513c0341c   lsagona   add chart
18
19
20
21
22
  import java.net.URL
  import java.util.*
  
  
  class DataPanelController : Initializable, SelectedVesselListener {
df68a9fdc   lsagona   refactor graph mi...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
      private var dataList: ObservableList<String> = FXCollections.observableArrayList()
      private lateinit var timeData: ArrayList<String>
  
      private val latitude: ArrayList<Double> = arrayListOf()
      private val longitude: ArrayList<Double> = arrayListOf()
      private val speedOverGround: ArrayList<Double> = arrayListOf()
      private val courseOverGround: ArrayList<Double> = arrayListOf()
      private val heading: ArrayList<Double> = arrayListOf()
      private val vesselName: ArrayList<String> = arrayListOf()
      private val imo: ArrayList<String> = arrayListOf()
      private val callSign: ArrayList<String> = arrayListOf()
      private val vesselType: ArrayList<Double> = arrayListOf()
      private val status: ArrayList<String> = arrayListOf()
      private val length: ArrayList<Double> = arrayListOf()
      private val width: ArrayList<Double> = arrayListOf()
      private val draft: ArrayList<Double> = arrayListOf()
      private val cargo: ArrayList<Double> = arrayListOf()
  
      private var selectedItem: String? = null
513c0341c   lsagona   add chart
42
43
  
      @FXML
df68a9fdc   lsagona   refactor graph mi...
44
      var dataListView = ListView<String>()
513c0341c   lsagona   add chart
45

513c0341c   lsagona   add chart
46
      @FXML
f15a58907   lsagona   add better chart
47
      var dataViewer = JavaFxDataViewer()
e220e082b   lsagona   graph binded to s...
48
49
      private val plotData = PlotData()
      private val config = DataViewerConfiguration()
513c0341c   lsagona   add chart
50
      override fun initialize(location: URL?, resources: ResourceBundle?) {
513c0341c   lsagona   add chart
51
52
          setObservableSelectedVesselListener()
          dataListView.items = dataList
e220e082b   lsagona   graph binded to s...
53

867a0df46   lsagona   populate plot whe...
54
          config.showLegend(false)
f15a58907   lsagona   add better chart
55
          config.setLegendInsidePlot(false)
513c0341c   lsagona   add chart
56

e220e082b   lsagona   graph binded to s...
57
          setObservableCurrentTimeListener()
513c0341c   lsagona   add chart
58
          dataListView.setCellFactory {
df68a9fdc   lsagona   refactor graph mi...
59
60
              object : ListCell<String?>() {
                  override fun updateItem(item: String?, empty: Boolean) {
513c0341c   lsagona   add chart
61
62
63
64
                      super.updateItem(item, empty)
                      text = if (empty) {
                          null
                      } else {
df68a9fdc   lsagona   refactor graph mi...
65
                          item
513c0341c   lsagona   add chart
66
67
68
69
70
71
                      }
                  }
              }
          }
  
          dataListView.selectionModel.selectedItemProperty().addListener { _, _, newValue ->
e220e082b   lsagona   graph binded to s...
72
              selectedItem = newValue
867a0df46   lsagona   populate plot whe...
73
              updateDataList(observableSelectedVessel.value)
e220e082b   lsagona   graph binded to s...
74
75
76
77
78
79
80
81
82
83
              plot(newValue)
          }
  
          plotData.allTraces.clear()
          config.setxAxisTitle("")
          config.setyAxisTitle("")
          config.plotTitle = ""
          dataViewer.updateConfiguration(config)
          dataViewer.updatePlot(plotData)
          initDataList()
e220e082b   lsagona   graph binded to s...
84
      }
f15a58907   lsagona   add better chart
85

e220e082b   lsagona   graph binded to s...
86
      private fun plot() {
93534e693   lsagona   fix exception
87
          if (selectedItem != null && observableSelectedVessel.value.messages.size != 0) {
e220e082b   lsagona   graph binded to s...
88
89
              GlobalScope.launch {
                  plot(selectedItem)
513c0341c   lsagona   add chart
90
              }
e220e082b   lsagona   graph binded to s...
91
92
          }
      }
513c0341c   lsagona   add chart
93

df68a9fdc   lsagona   refactor graph mi...
94
      private fun plot(data: String?) {
e220e082b   lsagona   graph binded to s...
95
96
97
98
99
          if (data == null) {
              plotData.allTraces.clear()
              config.setxAxisTitle("")
              config.setyAxisTitle("")
              dataViewer.updateConfiguration(config)
513c0341c   lsagona   add chart
100

e220e082b   lsagona   graph binded to s...
101
              dataViewer.resetPlot()
513c0341c   lsagona   add chart
102

e220e082b   lsagona   graph binded to s...
103
              return
e220e082b   lsagona   graph binded to s...
104
          }
513c0341c   lsagona   add chart
105

e220e082b   lsagona   graph binded to s...
106
107
108
          val scatterTrace = ScatterTrace<Any>()
          scatterTrace.traceColour = TraceColour.RED
          scatterTrace.traceVisibility = TraceVisibility.TRUE
f15a58907   lsagona   add better chart
109

df68a9fdc   lsagona   refactor graph mi...
110
          val serieStringX: Array<String> = timeData.toTypedArray()
e220e082b   lsagona   graph binded to s...
111
  //            val serieDoubleX: Array<Double> = arrayListDoubleX.toTypedArray()
df68a9fdc   lsagona   refactor graph mi...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
          var serieStringY: Array<String> = arrayOf()
          var serieDoubleY: Array<Double> = arrayOf()
          when (data) {
              "Latitude" -> {
                  serieDoubleY = latitude.toTypedArray()
              }
              "Longitude" -> {
                  serieDoubleY = longitude.toTypedArray()
              }
              "Speed Over Ground" -> {
                  serieDoubleY = speedOverGround.toTypedArray()
              }
              "Course Over Ground" -> {
                  serieDoubleY = courseOverGround.toTypedArray()
              }
              "Heading" -> {
                  serieDoubleY = heading.toTypedArray()
              }
              "Vessel Name" -> {
                  serieStringY = vesselName.toTypedArray()
              }
              "IMO" -> {
                  serieStringY = imo.toTypedArray()
              }
              "Call Sign" -> {
                  serieStringY = callSign.toTypedArray()
              }
              "Vessel Type" -> {
                  serieDoubleY = vesselType.toTypedArray()
              }
              "Status" -> {
                  serieStringY = status.toTypedArray()
              }
              "Length" -> {
                  serieDoubleY = length.toTypedArray()
              }
              "Width" -> {
                  serieDoubleY = width.toTypedArray()
              }
              "Draft" -> {
                  serieDoubleY = draft.toTypedArray()
              }
              "Cargo" -> {
                  serieDoubleY = cargo.toTypedArray()
              }
          }
e220e082b   lsagona   graph binded to s...
158

df68a9fdc   lsagona   refactor graph mi...
159
          if (serieStringY.isNotEmpty()) {
e220e082b   lsagona   graph binded to s...
160
161
162
163
164
              scatterTrace.setxArray(serieStringX)
              scatterTrace.setyArray(serieStringY)
          } else {
              scatterTrace.setxArray(serieStringX)
              scatterTrace.setyArray(serieDoubleY)
513c0341c   lsagona   add chart
165
          }
8108656dd   lsagona   remove plot title
166
          config.plotTitle = ""
e220e082b   lsagona   graph binded to s...
167
          config.setxAxisTitle("Time (s)")
df68a9fdc   lsagona   refactor graph mi...
168
          config.setyAxisTitle(data)
e220e082b   lsagona   graph binded to s...
169
170
171
          dataViewer.resetPlot()
          plotData.allTraces.clear()
          plotData.addTrace(scatterTrace)
f15a58907   lsagona   add better chart
172
173
          dataViewer.updateConfiguration(config)
          dataViewer.updatePlot(plotData)
513c0341c   lsagona   add chart
174
175
176
177
178
      }
  
      private fun setObservableSelectedVesselListener() {
          observableSelectedVessel.listeners.add(this)
      }
df68a9fdc   lsagona   refactor graph mi...
179
      private fun populateTime(vessel: Vessel): ArrayList<String> {
e220e082b   lsagona   graph binded to s...
180
181
182
183
184
          return if (observableIsReplayState.value) {
              vessel.getAllTimeBeforeSelectedTime()
          } else {
              vessel.getAllTime()
          }
513c0341c   lsagona   add chart
185
      }
df68a9fdc   lsagona   refactor graph mi...
186
      private fun populateLatitude(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
187
188
189
190
191
          return if (observableIsReplayState.value) {
              vessel.getAllLatitudeBeforeSelectedTime()
          } else {
              vessel.getAllLatitude()
          }
513c0341c   lsagona   add chart
192
      }
df68a9fdc   lsagona   refactor graph mi...
193
      private fun populateLongitude(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
194
195
196
197
198
          return if (observableIsReplayState.value) {
              vessel.getAllLongitudeBeforeSelectedTime()
          } else {
              vessel.getAllLongitude()
          }
513c0341c   lsagona   add chart
199
      }
df68a9fdc   lsagona   refactor graph mi...
200
      private fun populateSpeedOverGround(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
201
202
203
204
205
          return if (observableIsReplayState.value) {
              vessel.getAllSpeedOverGroundBeforeSelectedTime()
          } else {
              vessel.getAllSpeedOverGround()
          }
513c0341c   lsagona   add chart
206
      }
df68a9fdc   lsagona   refactor graph mi...
207
      private fun populateCourseOverGround(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
208
209
210
211
212
          return if (observableIsReplayState.value) {
              vessel.getAllCourseOverGroundBeforeSelectedTime()
          } else {
              vessel.getAllCourseOverGround()
          }
513c0341c   lsagona   add chart
213
      }
df68a9fdc   lsagona   refactor graph mi...
214
      private fun populateHeading(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
215
216
217
218
219
          return if (observableIsReplayState.value) {
              vessel.getAllHeadingBeforeSelectedTime()
          } else {
              vessel.getAllHeading()
          }
513c0341c   lsagona   add chart
220
      }
df68a9fdc   lsagona   refactor graph mi...
221
      private fun populateVesselName(vessel: Vessel): ArrayList<String> {
e220e082b   lsagona   graph binded to s...
222
223
224
225
226
          return if (observableIsReplayState.value) {
              vessel.getAllVesselNameBeforeSelectedTime()
          } else {
              vessel.getAllVesselName()
          }
513c0341c   lsagona   add chart
227
      }
df68a9fdc   lsagona   refactor graph mi...
228
      private fun populateIMO(vessel: Vessel): ArrayList<String> {
e220e082b   lsagona   graph binded to s...
229
230
231
232
233
          return if (observableIsReplayState.value) {
              vessel.getAllIMOBeforeSelectedTime()
          } else {
              vessel.getAllIMO()
          }
513c0341c   lsagona   add chart
234
      }
df68a9fdc   lsagona   refactor graph mi...
235
      private fun populateCallSign(vessel: Vessel): ArrayList<String> {
e220e082b   lsagona   graph binded to s...
236
237
238
239
240
          return if (observableIsReplayState.value) {
              vessel.getAllCallSignBeforeSelectedTime()
          } else {
              vessel.getAllCallSign()
          }
513c0341c   lsagona   add chart
241
      }
df68a9fdc   lsagona   refactor graph mi...
242
      private fun populateVesselType(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
243
244
245
246
247
          return if (observableIsReplayState.value) {
              vessel.getAllVesselTypeBeforeSelectedTime()
          } else {
              vessel.getAllVesselType()
          }
513c0341c   lsagona   add chart
248
      }
df68a9fdc   lsagona   refactor graph mi...
249
      private fun populateStatus(vessel: Vessel): ArrayList<String> {
e220e082b   lsagona   graph binded to s...
250
251
252
253
254
          return if (observableIsReplayState.value) {
              vessel.getAllStatusBeforeSelectedTime()
          } else {
              vessel.getAllStatus()
          }
513c0341c   lsagona   add chart
255
      }
df68a9fdc   lsagona   refactor graph mi...
256
      private fun populateLength(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
257
258
259
260
261
          return if (observableIsReplayState.value) {
              vessel.getAllLengthBeforeSelectedTime()
          } else {
              vessel.getAllLength()
          }
513c0341c   lsagona   add chart
262
      }
df68a9fdc   lsagona   refactor graph mi...
263
      private fun populateWidth(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
264
265
266
267
268
          return if (observableIsReplayState.value) {
              vessel.getAllWidthBeforeSelectedTime()
          } else {
              vessel.getAllWidth()
          }
513c0341c   lsagona   add chart
269
      }
df68a9fdc   lsagona   refactor graph mi...
270
      private fun populateDraft(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
271
272
273
274
275
          return if (observableIsReplayState.value) {
              vessel.getAllDraftBeforeSelectedTime()
          } else {
              vessel.getAllDraft()
          }
513c0341c   lsagona   add chart
276
      }
df68a9fdc   lsagona   refactor graph mi...
277
      private fun populateCargo(vessel: Vessel): ArrayList<Double> {
e220e082b   lsagona   graph binded to s...
278
279
280
281
282
          return if (observableIsReplayState.value) {
              vessel.getAllCargoBeforeSelectedTime()
          } else {
              vessel.getAllCargo()
          }
513c0341c   lsagona   add chart
283
      }
e220e082b   lsagona   graph binded to s...
284
      private fun initDataList() {
df68a9fdc   lsagona   refactor graph mi...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
          val data = arrayListOf<String>()
  
          data.add("Latitude")
          data.add("Longitude")
          data.add("Speed Over Ground")
          data.add("Course Over Ground")
          data.add("Heading")
          data.add("Vessel Name")
          data.add("IMO")
          data.add("Call Sign")
          data.add("Vessel Type")
          data.add("Status")
          data.add("Length")
          data.add("Width")
          data.add("Draft")
          data.add("Cargo")
e220e082b   lsagona   graph binded to s...
301
302
303
304
305
  
          dataList.addAll(data)
      }
  
      private fun updateDataList(vessel: Vessel) {
513c0341c   lsagona   add chart
306
          timeData = populateTime(vessel)
df68a9fdc   lsagona   refactor graph mi...
307
          if (dataListView.selectionModel.selectedItem == null) return
a6a8df6ff   lsagona   add the possibili...
308

df68a9fdc   lsagona   refactor graph mi...
309
          when (dataListView.selectionModel.selectedItem) {
867a0df46   lsagona   populate plot whe...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
              "Latitude" -> {
                  latitude.clear()
                  latitude.addAll(populateLatitude(vessel))
              }
              "Longitude" -> {
                  longitude.clear()
                  longitude.addAll(populateLongitude(vessel))
              }
              "Speed Over Ground" -> {
                  speedOverGround.clear()
                  speedOverGround.addAll(populateSpeedOverGround(vessel))
              }
              "Course Over Ground" -> {
                  courseOverGround.clear()
                  courseOverGround.addAll(populateCourseOverGround(vessel))
              }
              "Heading" -> {
                  heading.clear()
                  heading.addAll(populateHeading(vessel))
              }
              "Vessel Name" -> {
                  vesselName.clear()
                  vesselName.addAll(populateVesselName(vessel))
              }
              "IMO" -> {
                  imo.clear()
                  imo.addAll(populateIMO(vessel))
              }
              "Call Sign" -> {
                  callSign.clear()
                  callSign.addAll(populateCallSign(vessel))
              }
              "Vessel Type" -> {
                  vesselType.clear()
                  vesselType.addAll(populateVesselType(vessel))
              }
              "Status" -> {
                  status.clear()
                  status.addAll(populateStatus(vessel))
              }
              "Length" -> {
                  length.clear()
                  length.addAll(populateLength(vessel))
              }
              "Width" -> {
                  width.clear()
                  width.addAll(populateWidth(vessel))
              }
              "Draft" -> {
                  draft.clear()
                  draft.addAll(populateDraft(vessel))
              }
              "Cargo" -> {
                  cargo.clear()
                  cargo.addAll(populateCargo(vessel))
              }
          }
513c0341c   lsagona   add chart
367
      }
e220e082b   lsagona   graph binded to s...
368
369
370
371
372
373
374
375
      private fun setObservableCurrentTimeListener() {
          observableCurrentTime.listeners.add(object : CurrentTime {
              override fun onValueChanged(newValue: Int) {
                  updateDataList(observableSelectedVessel.value)
                  plot()
              }
          })
      }
513c0341c   lsagona   add chart
376

e220e082b   lsagona   graph binded to s...
377
378
379
      override fun onValueChanged(newValue: Vessel) {
          updateDataList(newValue)
          plot()
513c0341c   lsagona   add chart
380
381
382
      }
  
  }