Skip to content
Snippets Groups Projects
Main.java 18 KiB
Newer Older
import com.application.DB.HelpingFunctions;
import com.application.GUI.InputPopUpWindow;
import com.application.GUI.OutputPopUpWindow;
import com.application.GUI.ProgressBar.RingProgressIndicator;
import com.application.GUI.LineChartFunctionality;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.chart.LineChart;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Logger;

import static com.application.DB.Constants.*;
import static com.application.DB.HelpingFunctions.*;
import static com.application.DB.HelpingFunctions.isLoadedData;
import static com.application.GUI.LineChartFunctionality.getDataPointsXAxis;
import static com.application.GUI.LineChartFunctionality.getLiveData;
import static java.util.logging.Level.SEVERE;
/**
 * This class launches the application
 *
 * @author Eilert Tunheim, Karin Pettersen, Mads Arnesen
 * @version 1.0.0
 */
public class Main extends Application {
Eilert Tunheim's avatar
Eilert Tunheim committed
    private LineChartFunctionality lineChartFunctionality;

    private BorderPane topBar;
    private HBox logoBar;
    private VBox sideBar;
    private static TextField treeSpeciesText;
    private static TextField dimensionsText;
    private static TextField sawsetText;
    private static TextField moistureGoalText;
    private static TextField timeLeftText;
    private static CheckMenuItem menuViewLiveData;
    private static CheckMenuItem menuViewRegression;
    private static CheckMenuItem menuViewRegressionShadow;
    private static CheckMenuItem menuViewPreviousData;

    private static CheckBox liveDataBox;
    private static CheckBox regressionBox;
    private static CheckBox regressionConfidenceIntervalBox;
    private static CheckBox previousBox;

    /**
     * Starts the application
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
    public static void exitApplication(){
        Platform.exit();
        System.exit(0);
    }

    /**
     * Initializes the application with all the different panes.
     *
     * @throws Exception Exception in super.
     */
    @Override
    public void init() throws Exception {
        super.init();

        // Initializing variables
Eilert Tunheim's avatar
Eilert Tunheim committed
        this.lineChartFunctionality = new LineChartFunctionality();
        this.topBar = new BorderPane();
        this.logoBar = new HBox();
        this.sideBar = new VBox();
        this.bottomBar = new HBox();
     * Sets the primaryStage and sets the scene for the window.
Eilert Tunheim's avatar
Eilert Tunheim committed
     *
     * @param primaryStage
     * @throws Exception
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        // Create panes for root
        BorderPane root = new BorderPane();
        this.menuBar = createMenuBar();
        LineChart<String, Number> lineChart = LineChartFunctionality.getLineChart();
        //Set id's to connect to css stylesheet
        root.setId("root");
        this.logoBar.setId("logoBar");
        this.menuBar.setId("menuBar");
        this.sideBar.setId("sideBar");
        this.bottomBar = createBottomBar();
        this.bottomBar.setId("bottomBar");
        lineChart.setId("lineChart");
        // Sett the menubar in a vbox inorder to stretch over the whole screen
        VBox vBox = new VBox(this.menuBar);

        this.topBar.setCenter(logoBar);

        // Sets alignment for the panes to the parent root
        root.setTop(this.topBar);
        root.setLeft(this.sideBar);
        root.setBottom(this.bottomBar);
        VBox.setVgrow(this.logoBar, Priority.ALWAYS);

        // Sets the scene and defines boundaries
        Scene scene = new Scene(root, 1200, 600);
        scene.getStylesheets().add(getClass().getResource("/com.application/CSS/styleSheet.css").toExternalForm());

        primaryStage.setTitle("Moelven Drying Application");
        primaryStage.setMaximized(true);
        primaryStage.setMinHeight(600);
        primaryStage.setMinWidth(1200);
        primaryStage.setScene(scene);
        primaryStage.show();

        // Displays the input parameters popup window
        InputPopUpWindow.display();

    /**
     * Creates the menubar with buttons.
     * Defines each action when button is clicked.
     *
     * @return MenuBar as a HBox
     */
    private MenuBar createMenuBar() {

        // Creating a menubar
        MenuBar menuBar = new MenuBar();

        // Defining the individual menus
        Menu menuFile = new Menu("File");
        Menu menuView = new Menu("View");
        MenuItem menuFileExit = new MenuItem("Exit");
        menuFileExit.setOnAction(event -> exitApplication());

        setMenuViewLiveData(new CheckMenuItem ("Live Data"));
        setMenuViewRegression(new CheckMenuItem ("Regression"));
        setMenuViewRegressionShadow(new CheckMenuItem ("Regression Shadow"));
        setMenuViewPreviousData(new CheckMenuItem ("Previous Data"));

        getMenuViewLiveData().setOnAction(event -> {
            if(getMenuViewLiveData().isSelected()){
                LineChartFunctionality.setPrintLiveData(true);
                LineChartFunctionality.printGraphs();
            } else {
                LineChartFunctionality.setPrintLiveData(false);
                LineChartFunctionality.printGraphs();
            }
        });
        getMenuViewRegression().setOnAction(event -> {
            if(getMenuViewRegression().isSelected()){
                LineChartFunctionality.setPrintRegression(true);
                LineChartFunctionality.printGraphs();
            } else {
                LineChartFunctionality.setPrintRegression(false);
                LineChartFunctionality.printGraphs();
            }
        });
        getMenuViewRegressionShadow().setOnAction(event -> {
            if(getMenuViewRegressionShadow().isSelected()){
                LineChartFunctionality.setPrintRegressionConfidenceInterval(true);
                LineChartFunctionality.printGraphs();
            } else {
                LineChartFunctionality.setPrintRegressionConfidenceInterval(false);
                LineChartFunctionality.printGraphs();
            }
        });
        getMenuViewPreviousData().setOnAction(event -> {
            if(getMenuViewPreviousData().isSelected()){
                LineChartFunctionality.setPrintPreviousData(true);
                LineChartFunctionality.printGraphs();
            } else {
                LineChartFunctionality.setPrintPreviousData(false);
                LineChartFunctionality.printGraphs();
            }
        });



        menuFile.getItems().addAll(menuFileExit);
        menuView.getItems().addAll(getMenuViewLiveData(), getMenuViewRegression(), getMenuViewRegressionShadow(), getMenuViewPreviousData());
        // Adding the menus to the menubar
        menuBar.getMenus().addAll(menuFile, menuView, menuHelp);

    private VBox createSideBar(){

        // Creating a vbox
        VBox sideBarVBox = new VBox();

        Label treeSpeciesLabel = new Label("Tree Species");
        treeSpeciesLabel.setId("sideBarLabelText");
        treeSpeciesText.setId("sideBarLabelText");
        treeSpeciesText.setPromptText("No Input");
        treeSpeciesText.setText(HelpingFunctions.TREE_SPECIES);

        Label dimensionsLabel = new Label("Width x Height");
        dimensionsLabel.setId("sideBarLabelText");
        dimensionsText.setId("sideBarLabelText");
        dimensionsText.setPromptText("No Input");
        dimensionsText.setText(HelpingFunctions.DIMENSIONS);

        Label sawsetLabel = new Label("Sawset");
        sawsetLabel.setId("sideBarLabelText");
        sawsetText.setId("sideBarLabelText");
        sawsetText.setPromptText("No Input");
        sawsetText.setText(HelpingFunctions.SAWSET);

        Label moistureGoalLabel = new Label("Moisture Goal");
        moistureGoalLabel.setId("sideBarLabelText");
        moistureGoalText.setId("sideBarLabelText");
        moistureGoalText.setPromptText("No Input");
        moistureGoalText.setText(HelpingFunctions.MOISTURE_GOAL);
        Label timeLeftLabel = new Label("Time Left");
        timeLeftLabel.setId("sideBarLabelText");
        timeLeftText = new TextField();
        timeLeftText.setId("sideBarLabelText");
        timeLeftText.setPromptText("Calculating...");
        timeLeftText.setText(TIME_LEFT);
        timeLeftText.setEditable(false);

        Button inputParametersButton = new Button("Input Parameters");
        inputParametersButton.setId("sideBarButtonInputParameters");
        inputParametersButton.setOnAction(e -> InputPopUpWindow.display());

        Button finishButton = new Button("Finish");
        finishButton.setId("sideBarButtonFinish");
        finishButton.setOnAction(e -> OutputPopUpWindow.displayOutputWindow());
        Button exitButton = new Button("Exit");
        exitButton.setId("sideBarButtonExit");
        exitButton.setOnAction(e -> exitApplication());
        // Creating the circular progressbar
        RingProgressIndicator ringProgressIndicator = new RingProgressIndicator();
        ringProgressIndicator.setRingWidth(100);
        ringProgressIndicator.makeIndeterminate();

        class WorkerThread extends Thread{
            RingProgressIndicator rpi;
            int progress = 0;

            public WorkerThread(RingProgressIndicator rpi){
                this.rpi = rpi;
            }

            @Override
            public void run() {
                while (!IS_FINISHED) {
                        ringProgressIndicator.makeIndeterminate();
                        Thread.sleep(500L * NUMBER_OF_SECONDS_LIVE_DATA);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    while (isLoadedData()) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            Logger.getLogger(getClass().getName()).log(SEVERE, null, e);
                        }
                        Platform.runLater(() -> {
                            rpi.setProgress(progress);
                        });

Eilert Tunheim's avatar
Eilert Tunheim committed
                        progress = getLiveData().size()/ getDataPointsXAxis()*100;

                        if (progress > 100) {
                            break;
                        }
        sideBarVBox.getChildren().addAll(ringProgressIndicator, treeSpeciesLabel, treeSpeciesText, dimensionsLabel, dimensionsText,
                sawsetLabel, sawsetText, moistureGoalLabel, moistureGoalText, timeLeftLabel, timeLeftText, inputParametersButton, finishButton, exitButton);

        VBox.setVgrow(sideBarVBox, Priority.ALWAYS);

        return sideBarVBox;
    }


    /**
     * This function imports the logos and defines the alignments
Eilert Tunheim's avatar
Eilert Tunheim committed
     *
     * @return a logoBar containing the logos in proper alignments
     * @throws FileNotFoundException
     */
    private HBox createLogoBar() throws FileNotFoundException {
        Image moelvenLogoM = new Image(new FileInputStream("src/main/resources/com.application/GUI/moelven_logo_m.png"));
        Image moelvenLogoTitle = new Image(new FileInputStream("src/main/resources/com.application/GUI/moelven_logo_title.png"));
        ImageView imageViewM = new ImageView(moelvenLogoM);
        ImageView imageViewTitle = new ImageView(moelvenLogoTitle);

        // Defining resolution and aspect ratio
        imageViewM.setFitHeight(100);
        imageViewM.setPreserveRatio(true);
        imageViewTitle.setFitHeight(100);
        imageViewTitle.setPreserveRatio(true);

        Region region1 = new Region();
        HBox.setHgrow(region1, Priority.ALWAYS);

        Region region2 = new Region();
        HBox.setHgrow(region2, Priority.ALWAYS);

        return new HBox(imageViewM, region1, imageViewTitle, region2);
    }
    private HBox createBottomBar(){
        HBox hBox = new HBox();

        Label liveDataText = new Label("View Live Data");
        setLiveDataBox(new CheckBox());
        getLiveDataBox().setSelected(true);
        getLiveDataBox().setOnAction(event -> {
            if(getLiveDataBox().isSelected()){
                LineChartFunctionality.setPrintLiveData(true);
                LineChartFunctionality.printGraphs();
                LineChartFunctionality.setPrintLiveData(false);
                LineChartFunctionality.printGraphs();
        });

        Label regressionText = new Label("View Regression");
        setRegressionBox(new CheckBox());
        getRegressionBox().setSelected(true);
        getRegressionBox().setOnAction(event -> {
            if(getRegressionBox().isSelected()){
                LineChartFunctionality.setPrintRegression(true);
                LineChartFunctionality.printGraphs();
                LineChartFunctionality.setPrintRegression(false);
                LineChartFunctionality.printGraphs();
        Label regressionConfidenceIntervalText = new Label("View Regression Shadow");
        setRegressionConfidenceIntervalBox(new CheckBox());
        getRegressionConfidenceIntervalBox().setSelected(false);
        getRegressionConfidenceIntervalBox().setOnAction(event -> {
            if(getRegressionConfidenceIntervalBox().isSelected()){
                LineChartFunctionality.setPrintRegressionConfidenceInterval(true);
                LineChartFunctionality.printGraphs();
            } else {
                LineChartFunctionality.setPrintRegressionConfidenceInterval(false);
                LineChartFunctionality.printGraphs();
            }
        });

        Label previousText = new Label("View Previous Data");
        setPreviousBox(new CheckBox());
        getPreviousBox().setSelected(true);
        getPreviousBox().setOnAction(event -> {
            if(getPreviousBox().isSelected()){
                LineChartFunctionality.setPrintPreviousData(true);
                LineChartFunctionality.printGraphs();
            } else {
                LineChartFunctionality.setPrintPreviousData(false);
                LineChartFunctionality.printGraphs();
            }
        hBox.getChildren().addAll(liveDataText, liveDataBox, regressionText, regressionBox, regressionConfidenceIntervalText, regressionConfidenceIntervalBox, previousText, previousBox);
        hBox.setAlignment(Pos.CENTER_RIGHT);
        hBox.setSpacing(5);




    public static void setTreeSpeciesText(String treeSpeciesText) {
        Main.treeSpeciesText.setText(treeSpeciesText);
    }

    public static void setDimensionsText(String dimensionsText)  {
        Main.dimensionsText.setText(dimensionsText);
    }

    public static void setSawsetText(String sawsetText) {
        Main.sawsetText.setText(sawsetText);
    }

    public static void setMoistureGoalText(String moistureGoalText) {
        Main.moistureGoalText.setText(moistureGoalText);
    }

    public static void setTimeLeftText(String moistureGoalText) {
        Main.timeLeftText.setText(moistureGoalText);
    }

    public LineChartFunctionality getLineChartFunctionality() {
        return lineChartFunctionality;
    }

    public static CheckMenuItem getMenuViewLiveData() {
        return menuViewLiveData;
    }

    public static void setMenuViewLiveData(CheckMenuItem menuViewLiveData) {
        Main.menuViewLiveData = menuViewLiveData;
    }

    public static CheckMenuItem getMenuViewRegression() {
        return menuViewRegression;
    }

    public void setMenuViewRegression(CheckMenuItem menuViewRegression) {
        Main.menuViewRegression = menuViewRegression;
    }

    public static CheckMenuItem getMenuViewRegressionShadow() {
        return menuViewRegressionShadow;
    }

    public void setMenuViewRegressionShadow(CheckMenuItem menuViewRegressionShadow) {
        this.menuViewRegressionShadow = menuViewRegressionShadow;
    }

    public static CheckMenuItem getMenuViewPreviousData() {
        return menuViewPreviousData;
    }

    public void setMenuViewPreviousData(CheckMenuItem menuViewPreviousData) {
        Main.menuViewPreviousData = menuViewPreviousData;
    }

    public static CheckBox getLiveDataBox() {
        return liveDataBox;
    }

    public void setLiveDataBox(CheckBox liveDataBox) {
        Main.liveDataBox = liveDataBox;
    }

    public static CheckBox getRegressionBox() {
        return regressionBox;
    }

    public void setRegressionBox(CheckBox regressionBox) {
        Main.regressionBox = regressionBox;
    }

    public static CheckBox getRegressionConfidenceIntervalBox() {
        return regressionConfidenceIntervalBox;
    }

    public void setRegressionConfidenceIntervalBox(CheckBox regressionConfidenceIntervalBox) {
        Main.regressionConfidenceIntervalBox = regressionConfidenceIntervalBox;
    }

    public static CheckBox getPreviousBox() {
        return previousBox;
    }

    public void setPreviousBox(CheckBox previousBox) {
        Main.previousBox = previousBox;
    }