Blame view

src/main/kotlin/application/controller/DataPanelController.kt 11.7 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

f15a58907   lsagona   add better chart
54
55
          config.showLegend(true)
          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
73
74
75
76
77
78
79
80
81
82
              selectedItem = newValue
              plot(newValue)
          }
  
          plotData.allTraces.clear()
          config.setxAxisTitle("")
          config.setyAxisTitle("")
          config.plotTitle = ""
          dataViewer.updateConfiguration(config)
          dataViewer.updatePlot(plotData)
          initDataList()
f39d90e60   lsagona   Select/deselect MMSI
83

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

e220e082b   lsagona   graph binded to s...
86
87
88
89
      private fun plot() {
          if (selectedItem != null) {
              GlobalScope.launch {
                  plot(selectedItem)
513c0341c   lsagona   add chart
90
              }
e220e082b   lsagona   graph binded to s...
91
92
          }
      }
513c0341c   lsagona   add chart
93

e220e082b   lsagona   graph binded to s...
94
95
96
97
98
99
      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
100

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

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

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

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

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

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

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

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

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

e220e082b   lsagona   graph binded to s...
292
293
          longitude.clear()
          longitude.addAll(populateLongitude(vessel))
513c0341c   lsagona   add chart
294

e220e082b   lsagona   graph binded to s...
295
296
          speedOverGround.clear()
          speedOverGround.addAll(populateSpeedOverGround(vessel))
513c0341c   lsagona   add chart
297

e220e082b   lsagona   graph binded to s...
298
299
          courseOverGround.clear()
          courseOverGround.addAll(populateCourseOverGround(vessel))
513c0341c   lsagona   add chart
300

e220e082b   lsagona   graph binded to s...
301
302
          heading.clear()
          heading.addAll(populateHeading(vessel))
513c0341c   lsagona   add chart
303

e220e082b   lsagona   graph binded to s...
304
305
          vesselName.clear()
          vesselName.addAll(populateVesselName(vessel))
513c0341c   lsagona   add chart
306

e220e082b   lsagona   graph binded to s...
307
308
          imo.clear()
          imo.addAll(populateIMO(vessel))
513c0341c   lsagona   add chart
309

e220e082b   lsagona   graph binded to s...
310
311
          callSign.clear()
          callSign.addAll(populateCallSign(vessel))
513c0341c   lsagona   add chart
312

e220e082b   lsagona   graph binded to s...
313
314
          vesselType.clear()
          vesselType.addAll(populateVesselType(vessel))
513c0341c   lsagona   add chart
315

e220e082b   lsagona   graph binded to s...
316
317
          status.clear()
          status.addAll(populateStatus(vessel))
513c0341c   lsagona   add chart
318

e220e082b   lsagona   graph binded to s...
319
320
          length.clear()
          length.addAll(populateLength(vessel))
513c0341c   lsagona   add chart
321

e220e082b   lsagona   graph binded to s...
322
323
          width.clear()
          width.addAll(populateWidth(vessel))
513c0341c   lsagona   add chart
324

e220e082b   lsagona   graph binded to s...
325
326
          draft.clear()
          draft.addAll(populateDraft(vessel))
513c0341c   lsagona   add chart
327

e220e082b   lsagona   graph binded to s...
328
329
          cargo.clear()
          cargo.addAll(populateCargo(vessel))
513c0341c   lsagona   add chart
330

513c0341c   lsagona   add chart
331
      }
e220e082b   lsagona   graph binded to s...
332
333
334
335
336
337
338
339
      private fun setObservableCurrentTimeListener() {
          observableCurrentTime.listeners.add(object : CurrentTime {
              override fun onValueChanged(newValue: Int) {
                  updateDataList(observableSelectedVessel.value)
                  plot()
              }
          })
      }
513c0341c   lsagona   add chart
340

e220e082b   lsagona   graph binded to s...
341
342
343
      override fun onValueChanged(newValue: Vessel) {
          updateDataList(newValue)
          plot()
513c0341c   lsagona   add chart
344
345
346
      }
  
  }