Commit 6b2760a4588ded162dbfedd718cb2af05df72b94

Authored by lsagona
1 parent ea7eacbe63
Exists in master and in 1 other branch dev

add warning message if the file contain more than 50 000 messages

Showing 1 changed file with 26 additions and 2 deletions Inline Diff

src/main/kotlin/application/controller/MenuBarController.kt View file @ 6b2760a
package application.controller 1 1 package application.controller
2 2
import application.model.MapState.* 3 3 import application.model.MapState.*
import application.model.createVesselCollection 4 4 import application.model.createVesselCollection
import application.model.observableIsReplayState 5 5 import application.model.observableIsReplayState
import application.model.observableMapState 6 6 import application.model.observableMapState
import application.model.observableVessel 7 7 import application.model.observableVessel
import javafx.event.EventHandler 8 8 import javafx.event.EventHandler
import javafx.fxml.FXML 9 9 import javafx.fxml.FXML
import javafx.fxml.Initializable 10 10 import javafx.fxml.Initializable
import javafx.scene.control.* 11 11 import javafx.scene.control.*
import javafx.stage.FileChooser 12 12 import javafx.stage.FileChooser
13 import java.io.*
import java.net.URL 13 14 import java.net.URL
import java.util.* 14 15 import java.util.*
15 16
17
class MenuBarController : Initializable { 16 18 class MenuBarController : Initializable {
17 19
@FXML 18 20 @FXML
var menuBar: MenuBar = MenuBar() 19 21 var menuBar: MenuBar = MenuBar()
20 22
@FXML 21 23 @FXML
var import: MenuItem = MenuItem() 22 24 var import: MenuItem = MenuItem()
23 25
@FXML 24 26 @FXML
var allMessages: CheckMenuItem = CheckMenuItem() 25 27 var allMessages: CheckMenuItem = CheckMenuItem()
26 28
@FXML 27 29 @FXML
var clusteredMessage: CheckMenuItem = CheckMenuItem() 28 30 var clusteredMessage: CheckMenuItem = CheckMenuItem()
29 31
@FXML 30 32 @FXML
var heatMap: CheckMenuItem = CheckMenuItem() 31 33 var heatMap: CheckMenuItem = CheckMenuItem()
32 34
@FXML 33 35 @FXML
var activateReplayButton: RadioMenuItem = RadioMenuItem() 34 36 var activateReplayButton: RadioMenuItem = RadioMenuItem()
35 37
override fun initialize(location: URL?, resources: ResourceBundle?) { 36 38 override fun initialize(location: URL?, resources: ResourceBundle?) {
37 39
setOnActionImportButton() 38 40 setOnActionImportButton()
39 41
setOnActionAllMessageButton() 40 42 setOnActionAllMessageButton()
setOnActionClusteredMessageButton() 41 43 setOnActionClusteredMessageButton()
setOnActionHeatMapButton() 42 44 setOnActionHeatMapButton()
setOnActionActivateReplayButton() 43 45 setOnActionActivateReplayButton()
observableMapState.state = CLUSTERED_MESSAGES 44 46 observableMapState.state = CLUSTERED_MESSAGES
allMessages.isSelected = false 45 47 allMessages.isSelected = false
clusteredMessage.isSelected = true 46 48 clusteredMessage.isSelected = true
heatMap.isSelected = false 47 49 heatMap.isSelected = false
48 50
} 49 51 }
50 52
private fun setOnActionActivateReplayButton() { 51 53 private fun setOnActionActivateReplayButton() {
activateReplayButton.onAction = EventHandler { 52 54 activateReplayButton.onAction = EventHandler {
observableIsReplayState.value = activateReplayButton.isSelected 53 55 observableIsReplayState.value = activateReplayButton.isSelected
} 54 56 }
} 55 57 }
56 58
private fun setOnActionImportButton() { 57 59 private fun setOnActionImportButton() {
import.onAction = EventHandler { 58 60 import.onAction = EventHandler {
val fileChooser = FileChooser() 59 61 val fileChooser = FileChooser()
fileChooser.title = "Choose a file to import" 60 62 fileChooser.title = "Choose a file to import"
val window = menuBar.scene.window 61 63 val window = menuBar.scene.window
val file = fileChooser.showOpenDialog(window) 62 64 val file = fileChooser.showOpenDialog(window)
65
try { 63 66 try {
if (file.extension != "csv") { 64 67 if (file.extension != "csv") {
val alert = Alert(Alert.AlertType.WARNING) 65 68 val alert = Alert(Alert.AlertType.WARNING)
alert.title = "Warning Alert" 66 69 alert.title = "Warning Alert"
alert.headerText = "Wrong file format." 67 70 alert.headerText = "Wrong file format."
alert.contentText = "Please choose à .csv file." 68 71 alert.contentText = "Please choose à .csv file."
alert.showAndWait() 69 72 alert.showAndWait()
} 70 73 }
val vessels = createVesselCollection(file) 71
observableVessel.vessels.clear() 72 74 observableVessel.vessels.clear()
observableVessel.vessels = vessels 73 75 if(toMuchVessel(file)){
76 observableVessel.vessels = mutableMapOf()
77 }else {
78 val vessels = createVesselCollection(file)
79 observableVessel.vessels = vessels
80 }
} catch (ignore: IllegalStateException) { 74 81 } catch (ignore: IllegalStateException) {
75 82
} 76 83 }
} 77 84 }
85 }
86
87 private fun toMuchVessel(file: File): Boolean {
88 val nbLine = file.readLines().size
89 if (nbLine > 50000) {
90 val alert = Alert(Alert.AlertType.CONFIRMATION)
91 alert.title = "Warning!!"
92 alert.headerText = "Warning: This file contain a lot of messages."
93 alert.contentText = "Are you sure you want to continue."
94 val buttonTypeYes = ButtonType("Yes")
95 val buttonTypeNo = ButtonType("No ")
96 alert.buttonTypes.setAll(buttonTypeYes, buttonTypeNo)
97 val result = alert.showAndWait()
98
99 return result.get() != buttonTypeYes
100 }
101 return false
} 78 102 }
79 103
private fun setOnActionAllMessageButton() { 80 104 private fun setOnActionAllMessageButton() {
allMessages.onAction = EventHandler { 81 105 allMessages.onAction = EventHandler {
observableMapState.state = ALL_MESSAGES 82 106 observableMapState.state = ALL_MESSAGES
allMessages.isSelected = true 83 107 allMessages.isSelected = true
clusteredMessage.isSelected = false 84 108 clusteredMessage.isSelected = false
heatMap.isSelected = false 85 109 heatMap.isSelected = false
} 86 110 }
} 87 111 }
88 112
private fun setOnActionClusteredMessageButton() { 89 113 private fun setOnActionClusteredMessageButton() {