MenuBarController.kt 2.95 KB
package application.controller

import application.model.State.*
import application.model.createVesselCollection
import application.model.observableState
import application.model.observableVessel
import javafx.event.EventHandler
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.Alert
import javafx.scene.control.CheckMenuItem
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()

    @FXML
    var allMessages: CheckMenuItem = CheckMenuItem()

    @FXML
    var clusteredMessage: CheckMenuItem = CheckMenuItem()

    @FXML
    var heatMap: CheckMenuItem = CheckMenuItem()

    override fun initialize(location: URL?, resources: ResourceBundle?) {

        setOnActionImportButton()

        setOnActionAllMessageButton()
        setOnActionClusteredMessageButton()
        setOnActionHeatMapButton()
        observableState.state = CLUSTERED_MESSAGES
        allMessages.isSelected = false
        clusteredMessage.isSelected = true
        heatMap.isSelected = false

    }

    private fun setOnActionImportButton() {
        import.onAction = EventHandler {
            val fileChooser = FileChooser()
            fileChooser.title = "Choose a file to import"
            val window = menuBar.scene.window
            val file = fileChooser.showOpenDialog(window)
            try {
                if (file.extension != "csv") {
                    val alert = Alert(Alert.AlertType.WARNING)
                    alert.title = "Warning Alert"
                    alert.headerText = "Wrong file format."
                    alert.contentText = "Please choose à .csv file."
                    alert.showAndWait()
                }
                val vessels = createVesselCollection(file)
                observableVessel.vessels.clear()
                observableVessel.vessels = vessels
            } catch (ignore: IllegalStateException) {

            }
        }
    }

    private fun setOnActionAllMessageButton() {
        allMessages.onAction = EventHandler {
            observableState.state = ALL_MESSAGES
            allMessages.isSelected = true
            clusteredMessage.isSelected = false
            heatMap.isSelected = false
        }
    }

    private fun setOnActionClusteredMessageButton() {
        clusteredMessage.onAction = EventHandler {
            observableState.state = CLUSTERED_MESSAGES
            heatMap.isSelected = false
            allMessages.isSelected = false
            clusteredMessage.isSelected = true
        }
    }

    private fun setOnActionHeatMapButton() {
        heatMap.onAction = EventHandler {
            observableState.state = HEAT_MAP
            heatMap.isSelected = true
            clusteredMessage.isSelected = false
            allMessages.isSelected = false
        }
    }


}