Skip to content
Snippets Groups Projects
Controller.java 16.3 KiB
Newer Older
eyan_'s avatar
eyan_ committed
package com.example.schedulerapp;

eyan_'s avatar
eyan_ committed

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
eyan_'s avatar
eyan_ committed
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
eyan_'s avatar
eyan_ committed
import javafx.scene.Scene;
import javafx.scene.control.*;
eyan_'s avatar
eyan_ committed
import javafx.scene.input.MouseEvent;
eyan_'s avatar
eyan_ committed
import javafx.scene.layout.*;
eyan_'s avatar
eyan_ committed
import javafx.stage.Stage;
eyan_'s avatar
eyan_ committed

import java.io.IOException;
eyan_'s avatar
eyan_ committed
import java.time.LocalDate;
import java.util.ArrayList;
eyan_'s avatar
eyan_ committed

eyan_'s avatar
eyan_ committed
public class Controller {
eyan_'s avatar
eyan_ committed
    @FXML
    BorderPane myBorderPane;
    @FXML
    VBox leftSideButtons;
eyan_'s avatar
eyan_ committed
    @FXML
    public BorderPane rootBorderPane;
eyan_'s avatar
eyan_ committed
    @FXML
    TextArea employees;
    @FXML
    private TextField addFirstName;
    @FXML
    private TextField addLastName;
    @FXML
    private TextField removeID;
    @FXML
    Button addShiftSubmitButton;
    @FXML
    DatePicker addShiftDatePicker;
    @FXML
    TextField addShiftStartTime;
    @FXML
    TextField addShiftEndTime;
    @FXML
    ComboBox addShiftEmployeeBox;
    @FXML
    HBox addShiftEmployeeHBox;
eyan_'s avatar
eyan_ committed
    @FXML
    ComboBox removeEmployeeBox;
    @FXML
    ComboBox addShiftStartTimeBox;
    @FXML
    ComboBox addShiftEndTimeBox;
eyan_'s avatar
eyan_ committed
    @FXML
    Label currentDateText;
ArktikHunter's avatar
ArktikHunter committed
    @FXML
    TextField loginField;
    @FXML
    Label loggedIn;
    @FXML
    Label viewing;
    @FXML
    Label loginWarning;
eyan_'s avatar
eyan_ committed

eyan_'s avatar
eyan_ committed
    Stage popupStage;
    Scene popupScene;
    DailyView dailyView;
    WeeklyView weeklyView;
    BorderPane root;
    Parent header;
    Parent sidePanel;
    Parent bottomPanel;
eyan_'s avatar
eyan_ committed
    static boolean isDaily;

    public void scheduleButtonClicked() throws IOException {
        System.out.println("schedule button clicked");
        dailyScheduleButtonClicked();
        leftSideButtons.setVisible(true);
    }
    public void paystubButtonClicked() throws IOException {
        System.out.println("paystub button clicked");
        FXMLLoader fxmlLoader = new FXMLLoader(ScheduleApp.class.getResource("paystubView.fxml"));
        fxmlLoader.setController(this);
        myBorderPane.setCenter(fxmlLoader.load());
        leftSideButtons.setVisible(false);
    }
    public void availabilityButtonClicked() {
        System.out.println("availability button clicked");
    }
    public void timeoffButtonClicked () {
        System.out.println("timeoff button clicked");
    }
    public void dailyScheduleButtonClicked() throws IOException {
        System.out.println("daily schedule button clicked");
        root.setTop(header);
        root.setLeft(sidePanel);
        root.setBottom(bottomPanel);
        root.setCenter(dailyView);
    public void weeklyScheduleButtonClicked() throws Exception {
        System.out.println("weekly schedule button clicked");
        root.setTop(header);
        root.setLeft(sidePanel);
        root.setBottom(bottomPanel);
        root.setCenter(weeklyView);
    }
    public void pickupsButtonClicked () {
        System.out.println("pickups button clicked");
    }
eyan_'s avatar
eyan_ committed
    public void prevButtonClicked (MouseEvent event) throws Exception {
        System.out.println("prev button clicked");
eyan_'s avatar
eyan_ committed
        updateCurrentDate(event);
eyan_'s avatar
eyan_ committed
    public void jumpButtonClicked (MouseEvent event) throws Exception {
        System.out.println("jump button clicked");
eyan_'s avatar
eyan_ committed
        updateCurrentDate(event);
eyan_'s avatar
eyan_ committed
    public void nextButtonClicked (MouseEvent event) throws Exception {
        System.out.println("next button clicked");
eyan_'s avatar
eyan_ committed
        System.out.println(isDaily);
eyan_'s avatar
eyan_ committed
        updateCurrentDate(event);

    public void addShiftClicked() throws IOException {
        FXMLLoader popupLoader = new FXMLLoader(this.getClass().getResource("addShiftPopup.fxml"));
        popupLoader.setController(this);
eyan_'s avatar
eyan_ committed
        popupScene = new Scene(popupLoader.load(),500,500);
        popupStage = new Stage();
        popupStage.setScene(popupScene);
        popupStage.show();

    public void populateEmployeeBox(MouseEvent event) {

eyan_'s avatar
eyan_ committed
        ArrayList<String> aList = model.returnFormattedEmployeeNames();
        ObservableList<String> list = FXCollections.observableArrayList();
eyan_'s avatar
eyan_ committed
        aList.forEach(employee-> list.add(employee));

        ComboBox test = (ComboBox) event.getSource();
        test.setItems(list);
eyan_'s avatar
eyan_ committed
    public void addShiftSubmitClicked(MouseEvent event){
        System.out.println("submit clicked");
eyan_'s avatar
eyan_ committed
        String employee = (String) addShiftEmployeeBox.getValue();
eyan_'s avatar
eyan_ committed
        int index = addShiftEmployeeBox.getSelectionModel().getSelectedIndex();
        String startTime = (String) addShiftStartTimeBox.getValue();
        String endTime = (String) addShiftEndTimeBox.getValue();
eyan_'s avatar
eyan_ committed
        LocalDate date = addShiftDatePicker.getValue();
eyan_'s avatar
eyan_ committed
        System.out.println(date.toString());
eyan_'s avatar
eyan_ committed
        model.addShift(model.getIDbyIndex(index),date.toString(),Integer.parseInt(startTime),Integer.parseInt(endTime));
        System.out.println(model.getIDbyIndex(index) + "20210101" + Integer.parseInt(startTime) + Integer.parseInt(endTime));
eyan_'s avatar
eyan_ committed

        //System.out.println(employee + startTime + endTime + date);
eyan_'s avatar
eyan_ committed
    public void managerScheduleClicked(MouseEvent event) throws IOException {
        System.out.println("manager schedule clicked");
eyan_'s avatar
eyan_ committed
        isDaily = false;
eyan_'s avatar
eyan_ committed
        managerLoginClicked(event);
    }
    public void managerDailyScheduleClicked(MouseEvent mouseEvent) throws IOException{
eyan_'s avatar
eyan_ committed
        System.out.println("manager daily clicked");
eyan_'s avatar
eyan_ committed
        isDaily = true;
        System.out.println(isDaily);
eyan_'s avatar
eyan_ committed
        FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
        headerLoader.setController(this);
eyan_'s avatar
eyan_ committed
        header = headerLoader.load();
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("schedSidePanel.fxml"));
        sideLoader.setController(this);
eyan_'s avatar
eyan_ committed
        sidePanel = sideLoader.load();
        FXMLLoader bottomLoader = new FXMLLoader(this.getClass().getResource("schedBottomPanelManager.fxml"));
        bottomLoader.setController(this);
eyan_'s avatar
eyan_ committed
        bottomPanel = bottomLoader.load();

        root.setTop(header);;
        root.setLeft(sidePanel);
        root.setBottom(bottomPanel);
        root.setCenter(dailyView);
eyan_'s avatar
eyan_ committed

        HBox box = (HBox) bottomPanel.getChildrenUnmodifiable().get(1);
        Label currentDateText = (Label) box.getChildren().get(1);
        currentDateText.setText("Current Date: " + model.date);


        System.out.println(isDaily);
eyan_'s avatar
eyan_ committed
    }

    public void removeEmployeeBoxClicked(MouseEvent event){
eyan_'s avatar
eyan_ committed
        System.out.println("remove employee box clicked");
eyan_'s avatar
eyan_ committed
        populateEmployeeBox(event);
    }
eyan_'s avatar
eyan_ committed
    public void addShiftTimeBoxClicked(MouseEvent event){
        System.out.println("add shift time box clicked");
        ArrayList<String> times = new ArrayList<>();
        for (int i = 8; i < 24; i++){
            String j = i + "00";
            String k = i + "30";
            if (i < 10) {
                j = "0" + j;
                k = "0" + k;
            }
            times.add(j);
            times.add(k);
        }
        ComboBox box = (ComboBox) event.getSource();
        ObservableList<String> list = FXCollections.observableArrayList(times);
        System.out.println(list);
        box.setItems(list);
eyan_'s avatar
eyan_ committed
    }
eyan_'s avatar
eyan_ committed

eyan_'s avatar
eyan_ committed
    public void updateCurrentDate(MouseEvent event) throws Exception {
        if(currentDateText == null){
            currentDateText = new Label();
        }
        currentDateText.setText("Current Date: " + model.date.toString());
        System.out.println(isDaily);
        updateScheduleViews(event);
    }
    public void updateScheduleViews(MouseEvent event) throws Exception {
        if(isDaily){
            managerDailyScheduleClicked(event);
        }
        else{
            managerScheduleClicked(event);
        }
eyan_'s avatar
eyan_ committed

    }

    public void managerLoginClicked(MouseEvent mouseEvent) throws IOException {

        model.setIsManager(true);
        model.setThisEmployee(Integer.parseInt(loginField.getText()));  //todo:need guard to allow only #

        loadHeader();
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("schedSidePanel.fxml"));
        sideLoader.setController(this);
        FXMLLoader bottomLoader = new FXMLLoader(this.getClass().getResource("schedBottomPanelManager.fxml"));
        bottomLoader.setController(this);
ArktikHunter's avatar
ArktikHunter committed
        dailyView.setModel(model);
ArktikHunter's avatar
ArktikHunter committed
        model.addSubscriber(dailyView);
ArktikHunter's avatar
ArktikHunter committed
        weeklyView.setModel(model);
ArktikHunter's avatar
ArktikHunter committed
        model.addSubscriber(weeklyView);
ArktikHunter's avatar
ArktikHunter committed

        root.setTop(header);
        root.setLeft(sidePanel);
        root.setBottom(bottomPanel);
        root.setCenter(dailyView);
        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
eyan_'s avatar
eyan_ committed

        HBox box = (HBox) bottomPanel.getChildrenUnmodifiable().get(1);
        Label currentDateText = (Label) box.getChildren().get(1);
        currentDateText.setText("Current Date: " + model.date);

    public void employeeLoginClicked(MouseEvent mouseEvent) throws IOException {
        FXMLLoader employeeLoader = new FXMLLoader(this.getClass().getResource("mainView.fxml"));
        employeeLoader.setController(this);
        Parent root = employeeLoader.load();
        dailyView = new DailyView();
        dailyView.setModel(model);
        weeklyView = new WeeklyView();
        weeklyView.setModel(model);

        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
    public void mangerLogoutClicked(MouseEvent mouseEvent) throws IOException {
        FXMLLoader loginLoader = new FXMLLoader(this.getClass().getResource("login.fxml"));
        loginLoader.setController(this);
        Parent root = loginLoader.load();
        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
    // Staff Section
    // Switch to Staff Page
    public void staffManagerClicked(MouseEvent mouseEvent) throws IOException {
        BorderPane root = new BorderPane();
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("addEmployee.fxml"));
        gridLoader.setController(this);
        Parent grid  = gridLoader.load();
        FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
        headerLoader.setController(this);
        Parent header  = headerLoader.load();
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("staffManagerSidePanel.fxml"));
        sideLoader.setController(this);
        Parent sidePanel  = sideLoader.load();
        root.setTop(header);
        root.setLeft(sidePanel);
        root.setCenter(grid);
        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
    // Side Panel
    // Switch to Edit Employee
    public void editStaffSidePanelClicked(MouseEvent mouseEvent) throws IOException {
        BorderPane root = new BorderPane();
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("editEmployee.fxml"));
        gridLoader.setController(this);
        Parent grid  = gridLoader.load();
        FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
        headerLoader.setController(this);
        Parent header  = headerLoader.load();
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("staffManagerSidePanel.fxml"));
        sideLoader.setController(this);
        Parent sidePanel  = sideLoader.load();
        root.setTop(header);
        root.setLeft(sidePanel);
        root.setCenter(grid);
        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
    }
    // Switch to Remove Employee
    public void removeStaffSidePanelClicked(MouseEvent mouseEvent) throws IOException {
eyan_'s avatar
eyan_ committed
        System.out.println("remove staff side panel clicked");
        BorderPane root = new BorderPane();
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("removeEmployee.fxml"));
        gridLoader.setController(this);
        Parent grid  = gridLoader.load();
        FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
        headerLoader.setController(this);
        Parent header  = headerLoader.load();
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("staffManagerSidePanel.fxml"));
        sideLoader.setController(this);
        Parent sidePanel  = sideLoader.load();
        root.setTop(header);
        root.setLeft(sidePanel);
        root.setCenter(grid);
        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
    }
    // Switch to View Employees

    public void viewStaffSidePanelClicked(MouseEvent mouseEvent) throws IOException {
        BorderPane root = new BorderPane();
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("viewEmployees.fxml"));
        gridLoader.setController(this);
        Parent grid  = gridLoader.load();
        FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
        headerLoader.setController(this);
        Parent header  = headerLoader.load();
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("staffManagerSidePanel.fxml"));
        sideLoader.setController(this);
        Parent sidePanel  = sideLoader.load();
        root.setTop(header);
        root.setLeft(sidePanel);
        root.setCenter(grid);
        Stage stage = (Stage)((Node)mouseEvent.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
    }
    // Add, Edit, Remove Functions
    // Add Employee

    public void addEmployeeClicked(MouseEvent mouseEvent) {
        String firstName = addFirstName.getText();
        String lastName = addLastName.getText();
        model.addEmployee(firstName, lastName);
        System.out.println("Employee " + firstName + " " + lastName + " added to Staff. Welcome "
                + firstName + "!");
    }
    // Edit employee
    public void editEmployeeClicked(MouseEvent mouseEvent) {
    // Remove Employee
    public void removeEmployeeClicked(MouseEvent mouseEvent) {
eyan_'s avatar
eyan_ committed
        int index = removeEmployeeBox.getSelectionModel().getSelectedIndex();
        String id = String.valueOf(model.getIDbyIndex(index));
        model.removeEmployee(id);
eyan_'s avatar
eyan_ committed

        removeEmployeeBox.getSelectionModel().clearSelection();
    // View Employees

    //load header
    public void loadHeader() throws IOException {
        if (model.getIsManager()){      //load manager header
            FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
            headerLoader.setController(this);
            header  = headerLoader.load();
        }
        else {                          //load employee header
            //todo: load employee header instead
            FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));
            headerLoader.setController(this);
            header  = headerLoader.load();
        }
    }

    public void attemptLogin(MouseEvent e) throws IOException {
        if (!model.hasEmployee(Integer.parseInt(loginField.getText()))) {
            loginWarning.setText("Invalid id");
        }
        else {
            login(e);
        }
    }

    public void login(MouseEvent e) throws IOException {
        managerLoginClicked(e);
    }

    public void setModel(Model model) {
        this.model = model;
    }
eyan_'s avatar
eyan_ committed
}