Blame view

src/main/kotlin/application/controller/DataPanelController.kt 12.8 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
23
24
  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?>
e220e082b   lsagona   graph binded to s...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      private val latitude: ArrayList<MessageData?> = arrayListOf()
      private val longitude: ArrayList<MessageData?> = arrayListOf()
      private val speedOverGround: ArrayList<MessageData?> = arrayListOf()
      private val courseOverGround: ArrayList<MessageData?> = arrayListOf()
      private val heading: ArrayList<MessageData?> = arrayListOf()
      private val vesselName: ArrayList<MessageData?> = arrayListOf()
      private val imo: ArrayList<MessageData?> = arrayListOf()
      private val callSign: ArrayList<MessageData?> = arrayListOf()
      private val vesselType: ArrayList<MessageData?> = arrayListOf()
      private val status: ArrayList<MessageData?> = arrayListOf()
      private val length: ArrayList<MessageData?> = arrayListOf()
      private val width: ArrayList<MessageData?> = arrayListOf()
      private val draft: ArrayList<MessageData?> = arrayListOf()
      private val cargo: ArrayList<MessageData?> = arrayListOf()
  
      private var selectedItem: Pair<String, ArrayList<MessageData?>>? = null
513c0341c   lsagona   add chart
41
42
  
      @FXML
f15a58907   lsagona   add better chart
43
      var dataListView = ListView<Pair<String, ArrayList<MessageData?>>>()
513c0341c   lsagona   add chart
44

513c0341c   lsagona   add chart
45
      @FXML
f15a58907   lsagona   add better chart
46
      var dataViewer = JavaFxDataViewer()
e220e082b   lsagona   graph binded to s...
47
48
      private val plotData = PlotData()
      private val config = DataViewerConfiguration()
513c0341c   lsagona   add chart
49
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
59
60
61
62
63
64
65
66
67
68
69
70
71
          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 ->
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()
f39d90e60   lsagona   Select/deselect MMSI
84

e220e082b   lsagona   graph binded to s...
85
      }
f15a58907   lsagona   add better chart
86

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

e220e082b   lsagona   graph binded to s...
95
96
97
98
99
100
      private fun plot(data: Pair<String, ArrayList<MessageData?>>?) {
          if (data == null) {
              plotData.allTraces.clear()
              config.setxAxisTitle("")
              config.setyAxisTitle("")
              dataViewer.updateConfiguration(config)
513c0341c   lsagona   add chart
101

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

e220e082b   lsagona   graph binded to s...
104
              return
867a0df46   lsagona   populate plot whe...
105
          }else if (data.second.size < timeData.size) return
513c0341c   lsagona   add chart
106

e220e082b   lsagona   graph binded to s...
107
108
          val getValueVisitorX = GetValueVisitor()
          val getValueVisitorY = GetValueVisitor()
513c0341c   lsagona   add chart
109

e220e082b   lsagona   graph binded to s...
110
111
112
113
          val arrayListStringX = arrayListOf<String>()
  //        val arrayListDoubleX = arrayListOf<Double>()
          val arrayListStringY = arrayListOf<String>()
          val arrayListDoubleY = arrayListOf<Double>()
f15a58907   lsagona   add better chart
114

e220e082b   lsagona   graph binded to s...
115
116
117
          for (x in 0 until timeData.size) {
              timeData[x]?.accept(getValueVisitorX)
              data.second[x]?.accept(getValueVisitorY)
f15a58907   lsagona   add better chart
118
119
  
              if (getValueVisitorY.value.toDoubleOrNull() == null) {
e220e082b   lsagona   graph binded to s...
120
121
                  arrayListStringX.add(getValueVisitorX.value)
                  arrayListStringY.add(getValueVisitorY.value)
f15a58907   lsagona   add better chart
122
              } else {
e220e082b   lsagona   graph binded to s...
123
124
                  arrayListStringX.add(getValueVisitorX.value)
                  arrayListDoubleY.add(getValueVisitorY.value.toDouble())
513c0341c   lsagona   add chart
125
              }
e220e082b   lsagona   graph binded to s...
126
          }
513c0341c   lsagona   add chart
127

e220e082b   lsagona   graph binded to s...
128
129
130
          val scatterTrace = ScatterTrace<Any>()
          scatterTrace.traceColour = TraceColour.RED
          scatterTrace.traceVisibility = TraceVisibility.TRUE
f15a58907   lsagona   add better chart
131

e220e082b   lsagona   graph binded to s...
132
133
134
135
136
137
138
139
140
141
142
          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)
513c0341c   lsagona   add chart
143
          }
8108656dd   lsagona   remove plot title
144
          config.plotTitle = ""
e220e082b   lsagona   graph binded to s...
145
146
147
148
149
          config.setxAxisTitle("Time (s)")
          config.setyAxisTitle(data.first)
          dataViewer.resetPlot()
          plotData.allTraces.clear()
          plotData.addTrace(scatterTrace)
f15a58907   lsagona   add better chart
150
151
          dataViewer.updateConfiguration(config)
          dataViewer.updatePlot(plotData)
513c0341c   lsagona   add chart
152
      }
e220e082b   lsagona   graph binded to s...
153

513c0341c   lsagona   add chart
154
155
156
157
158
      private fun setObservableSelectedVesselListener() {
          observableSelectedVessel.listeners.add(this)
      }
  
      private fun populateTime(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
159
160
161
162
163
          return if (observableIsReplayState.value) {
              vessel.getAllTimeBeforeSelectedTime()
          } else {
              vessel.getAllTime()
          }
513c0341c   lsagona   add chart
164
      }
513c0341c   lsagona   add chart
165

e220e082b   lsagona   graph binded to s...
166
167
168
169
170
171
      private fun populateLatitude(vessel: Vessel): ArrayList<MessageData?> {
          return if (observableIsReplayState.value) {
              vessel.getAllLatitudeBeforeSelectedTime()
          } else {
              vessel.getAllLatitude()
          }
513c0341c   lsagona   add chart
172
173
174
      }
  
      private fun populateLongitude(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
175
176
177
178
179
          return if (observableIsReplayState.value) {
              vessel.getAllLongitudeBeforeSelectedTime()
          } else {
              vessel.getAllLongitude()
          }
513c0341c   lsagona   add chart
180
181
182
      }
  
      private fun populateSpeedOverGround(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
183
184
185
186
187
          return if (observableIsReplayState.value) {
              vessel.getAllSpeedOverGroundBeforeSelectedTime()
          } else {
              vessel.getAllSpeedOverGround()
          }
513c0341c   lsagona   add chart
188
189
190
      }
  
      private fun populateCourseOverGround(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
191
192
193
194
195
          return if (observableIsReplayState.value) {
              vessel.getAllCourseOverGroundBeforeSelectedTime()
          } else {
              vessel.getAllCourseOverGround()
          }
513c0341c   lsagona   add chart
196
197
198
      }
  
      private fun populateHeading(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
199
200
201
202
203
          return if (observableIsReplayState.value) {
              vessel.getAllHeadingBeforeSelectedTime()
          } else {
              vessel.getAllHeading()
          }
513c0341c   lsagona   add chart
204
      }
f15a58907   lsagona   add better chart
205
      private fun populateVesselName(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
206
207
208
209
210
          return if (observableIsReplayState.value) {
              vessel.getAllVesselNameBeforeSelectedTime()
          } else {
              vessel.getAllVesselName()
          }
513c0341c   lsagona   add chart
211
      }
f15a58907   lsagona   add better chart
212
      private fun populateIMO(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
213
214
215
216
217
          return if (observableIsReplayState.value) {
              vessel.getAllIMOBeforeSelectedTime()
          } else {
              vessel.getAllIMO()
          }
513c0341c   lsagona   add chart
218
      }
f15a58907   lsagona   add better chart
219
      private fun populateCallSign(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
220
221
222
223
224
          return if (observableIsReplayState.value) {
              vessel.getAllCallSignBeforeSelectedTime()
          } else {
              vessel.getAllCallSign()
          }
513c0341c   lsagona   add chart
225
      }
f15a58907   lsagona   add better chart
226
      private fun populateVesselType(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
227
228
229
230
231
          return if (observableIsReplayState.value) {
              vessel.getAllVesselTypeBeforeSelectedTime()
          } else {
              vessel.getAllVesselType()
          }
513c0341c   lsagona   add chart
232
      }
f15a58907   lsagona   add better chart
233
      private fun populateStatus(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
234
235
236
237
238
          return if (observableIsReplayState.value) {
              vessel.getAllStatusBeforeSelectedTime()
          } else {
              vessel.getAllStatus()
          }
513c0341c   lsagona   add chart
239
      }
f15a58907   lsagona   add better chart
240
      private fun populateLength(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
241
242
243
244
245
          return if (observableIsReplayState.value) {
              vessel.getAllLengthBeforeSelectedTime()
          } else {
              vessel.getAllLength()
          }
513c0341c   lsagona   add chart
246
      }
f15a58907   lsagona   add better chart
247
      private fun populateWidth(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
248
249
250
251
252
          return if (observableIsReplayState.value) {
              vessel.getAllWidthBeforeSelectedTime()
          } else {
              vessel.getAllWidth()
          }
513c0341c   lsagona   add chart
253
      }
f15a58907   lsagona   add better chart
254
      private fun populateDraft(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
255
256
257
258
259
          return if (observableIsReplayState.value) {
              vessel.getAllDraftBeforeSelectedTime()
          } else {
              vessel.getAllDraft()
          }
513c0341c   lsagona   add chart
260
      }
f15a58907   lsagona   add better chart
261
      private fun populateCargo(vessel: Vessel): ArrayList<MessageData?> {
e220e082b   lsagona   graph binded to s...
262
263
264
265
266
          return if (observableIsReplayState.value) {
              vessel.getAllCargoBeforeSelectedTime()
          } else {
              vessel.getAllCargo()
          }
513c0341c   lsagona   add chart
267
      }
e220e082b   lsagona   graph binded to s...
268
      private fun initDataList() {
513c0341c   lsagona   add chart
269
          val data = arrayListOf<Pair<String, ArrayList<MessageData?>>>()
e220e082b   lsagona   graph binded to s...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
          data.add(Pair("Latitude", latitude))
          data.add(Pair("Longitude", longitude))
          data.add(Pair("Speed Over Ground", speedOverGround))
          data.add(Pair("Course Over Ground", courseOverGround))
          data.add(Pair("Heading", heading))
          data.add(Pair("Vessel Name", vesselName))
          data.add(Pair("IMO", imo))
          data.add(Pair("Call Sign", callSign))
          data.add(Pair("Vessel Type", vesselType))
          data.add(Pair("Status", status))
          data.add(Pair("Length", length))
          data.add(Pair("Width", width))
          data.add(Pair("Draft", draft))
          data.add(Pair("Cargo", cargo))
  
          dataList.addAll(data)
      }
  
      private fun updateDataList(vessel: Vessel) {
513c0341c   lsagona   add chart
289
          timeData = populateTime(vessel)
867a0df46   lsagona   populate plot whe...
290
          if(dataListView.selectionModel.selectedItem == null) return
513c0341c   lsagona   add chart
291

867a0df46   lsagona   populate plot whe...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
          when (dataListView.selectionModel.selectedItem.first) {
              "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
350
      }
e220e082b   lsagona   graph binded to s...
351
352
353
354
355
356
357
358
      private fun setObservableCurrentTimeListener() {
          observableCurrentTime.listeners.add(object : CurrentTime {
              override fun onValueChanged(newValue: Int) {
                  updateDataList(observableSelectedVessel.value)
                  plot()
              }
          })
      }
513c0341c   lsagona   add chart
359

e220e082b   lsagona   graph binded to s...
360
361
362
      override fun onValueChanged(newValue: Vessel) {
          updateDataList(newValue)
          plot()
513c0341c   lsagona   add chart
363
364
365
      }
  
  }