RainfallAnalyser / src / main / java / project / rainanalyser / betarelease / RainfallAPI.java
RainfallAPI.java
Raw
package project.rainanalyser.betarelease;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class RainfallAPI extends Application {
    private RainfallData rainfallData;

    private static final int CHART_WIDTH = 800;
    private static final int CHART_HEIGHT = 600;

    @Override
    public void start(Stage primaryStage) throws IOException {
        // Create a toolbar with buttons
        ToolBar toolbar = createToolbar(primaryStage);

        // Create a VBox to hold the graph
        VBox vbox = new VBox();

        // Create a BorderPane to hold the toolbar and VBox
        BorderPane root = new BorderPane();
        root.setTop(toolbar);
        root.setCenter(vbox);

        // Create a Scene
        Scene scene = new Scene(root, 800, 600);

        // Set the title of the Stage
        primaryStage.setTitle("Rainfall Data");

        // Set the Scene of the Stage
        primaryStage.setScene(scene);

        // Show the Stage
        primaryStage.show();
    }

    private ToolBar createToolbar(Stage primaryStage) {
        // Create buttons for the toolbar
        Button chooseFileButton = new Button("Choose CSV file");
        Button visualizeButton = new Button("Visualize Data");

        // Add event handlers to the buttons
        chooseFileButton.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Open CSV file");

            // Add a file extension filter for CSV files
            FileChooser.ExtensionFilter csvFilter = new FileChooser.ExtensionFilter("CSV Files (*.csv)", "*.csv");
            fileChooser.getExtensionFilters().add(csvFilter);

            File file = fileChooser.showOpenDialog(primaryStage);
            if (file != null) {
                rainfallData = new RainfallData(file);

                // Set the selected file name as the button text
                chooseFileButton.setText("Selected File: " + file.getName());

                visualizeButton.setDisable(false);
            }
        });

        visualizeButton.setDisable(true);
        visualizeButton.setOnAction(event -> {
            try {
                showData(primaryStage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        // Create a toolbar and add the buttons
        ToolBar toolbar = new ToolBar();
        toolbar.getItems().addAll(chooseFileButton, visualizeButton);

        return toolbar;
    }

    private void showData(Stage primaryStage) throws IOException {
        // Create a VBox to hold the graph
        VBox vbox = new VBox();

        // Create a button to go back to the file chooser
        Button backButton = new Button("Back");
        backButton.setOnAction(event -> {
            try {
                start(primaryStage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        // Create a bar chart to display the data
        BorderPane chartPane = new BorderPane();
        RainfallChart rainfallChart = new RainfallChart(rainfallData.getRecords());
        rainfallChart.show(primaryStage, chartPane);

        vbox.getChildren().addAll(backButton, chartPane);

        // Create a Scene
        Scene scene = new Scene(vbox, CHART_WIDTH, CHART_HEIGHT);


        // Set the title of the Stage
        primaryStage.setTitle("Rainfall Data");

        // Set the Scene of the Stage
        primaryStage.setScene(scene);
    }

    public static void main(String[] args) {
        launch(args);
    }
}