Skip to content
Snippets Groups Projects
Controller.java 17.9 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;
    EmployeeView employeeView;
    Parent sideSchedulePanel;
    Parent paystubView;
    Parent editEmployeeView;
    Parent addEmployeeView;
    Parent staffPanel;
eyan_'s avatar
eyan_ committed
    static boolean isDaily;

    // login methods, initializes views too
    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 {
        //set this employee and manager privileges as appropriate
        int thisEmployee = Integer.parseInt(loginField.getText());
        model.setThisEmployee(thisEmployee);
        model.setSelectedEmployee(thisEmployee);
        model.setIsManager(model.getEmployee(thisEmployee).isManager());

        //load everything
        loadEverything();

        //set up for initial daily schedule view
        root.setTop(header);
        viewSchedule();

        Stage stage = (Stage)((Node)e.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();

        //set variable text
        currentDateText.setText("Current Date: " + model.date);
        loggedIn.setText("Logged In: " + model.getEmployeeByID(model.getThisEmployee()));

        //set manager specific text
        if (model.getIsManager()){
            viewing.setText("Viewing: " + model.getEmployeeByID(model.getSelectedEmployee()));
        }

    // add components here to be loaded once
    public void loadEverything() throws IOException {
        root = new BorderPane();
        dailyView = new DailyView();
        dailyView.setModel(model);
        model.addSubscriber(dailyView);
        weeklyView = new WeeklyView();
        weeklyView.setModel(model);
        model.addSubscriber(weeklyView);
        employeeView = new EmployeeView();
        employeeView.setModel(model);
        model.addSubscriber(employeeView);

        //fxml views
        loadHeader();
        loadScheduleSide();
        loadScheduleBottom();
        loadPaystubView();
        loadEditEmployee();
        loadStaffPanel();
    }

    //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
            //FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("employeeHeader.fxml"));
            FXMLLoader headerLoader = new FXMLLoader(this.getClass().getResource("headerManager.fxml"));    //for now
            headerLoader.setController(this);
            header  = headerLoader.load();

        }
    }

    public void loadPaystubView() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(ScheduleApp.class.getResource("paystubView.fxml"));
        fxmlLoader.setController(this);
        paystubView = fxmlLoader.load();

    // show schedule, default to daily
    public void viewSchedule(){
        root.setLeft(sideSchedulePanel);
        root.setBottom(bottomPanel);
        root.setCenter(dailyView);
    }

    // load weekly view from schedule daily
    public void viewScheduleWeekly(){
        root.setCenter(weeklyView);
    }

    public void viewScheduleDaily(){
        root.setCenter(dailyView);
    }

    // load pickups from schedule todo
    public void viewPickups(){
        //root.setCenter();
    }

    public void loadScheduleSide() throws IOException {
        //same for both
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("schedSidePanel.fxml"));
        sideLoader.setController(this);
        sideSchedulePanel  = sideLoader.load();
    }

    public void loadScheduleBottom() throws IOException {
        if (model.getIsManager()){
            FXMLLoader bottomLoader = new FXMLLoader(this.getClass().getResource("schedBottomPanelManager.fxml"));
            bottomLoader.setController(this);
            bottomPanel  = bottomLoader.load();
        }
        else {
            FXMLLoader bottomLoader = new FXMLLoader(this.getClass().getResource("employeeSchedBottomPanel.fxml"));
            bottomLoader.setController(this);
            bottomPanel  = bottomLoader.load();
        }
    }

    public void loadEditEmployee() throws IOException{
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("editEmployee.fxml"));
        gridLoader.setController(this);
        editEmployeeView = gridLoader.load();
    }

    public void viewPaystub() throws IOException {
        root.setCenter(paystubView);
    }

    public void viewPayroll() {
        System.out.println("todo: display payroll");
    }

    public void viewRequests() {
        System.out.println("todo: display requests");
    }

    public void viewEditEmployee(){
        root.setCenter(editEmployeeView);
    }

    public void viewAvailability() {
        System.out.println("todo viewAvailability");
    }
    public void timeoffButtonClicked () {
        System.out.println("timeoff button clicked");
    }

    // manager clicked on Employee tab
    public void viewStaff(){
        root.setLeft(staffPanel);
        root.setCenter(employeeView);
        root.setBottom(null);


    public void loadStaffPanel() throws IOException {
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("staffManagerSidePanel.fxml"));
        sideLoader.setController(this);
        staffPanel = sideLoader.load();
    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();
        list.addAll(aList);
eyan_'s avatar
eyan_ committed

        ComboBox test = (ComboBox) event.getSource();
        test.setItems(list);
    public void populatePositionBox(MouseEvent event) {
        ArrayList<String> aList = model.returnPositions();
        ObservableList<String> list = FXCollections.observableArrayList();
        list.addAll(aList);

        ComboBox test = (ComboBox) event.getSource();
        test.setItems(list);
    }

    public void cancelClicked(MouseEvent e){
        ((Node)e.getSource()).getScene().getWindow().hide();
    }

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());
        String result = model.addShift(model.getIDbyIndex(index),date.toString(),Integer.parseInt(startTime),Integer.parseInt(endTime));
        System.out.println(result);
        //todo: show result to user?
eyan_'s avatar
eyan_ committed

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());
eyan_'s avatar
eyan_ committed
    }
eyan_'s avatar
eyan_ committed

eyan_'s avatar
eyan_ committed
    public void employeePickupClicked() throws IOException {
        System.out.println("employee pickup clicked");
        FXMLLoader pickupLoader = new FXMLLoader(getClass().getResource("employeePickup.fxml"));
        pickupLoader.setController(this);
        root.setCenter(pickupLoader.load());
        root.setBottom(null);

    }
    public void employeeAvailabilityClicked() throws IOException {
        System.out.println("employee availability clicked");
        FXMLLoader availabilityLoader = new FXMLLoader(getClass().getResource("employeeAvailability.fxml"));
        availabilityLoader.setController(this);

        FXMLLoader availabilityFooterLoader = new FXMLLoader(getClass().getResource("employeeAvailabilityFooter.fxml"));
        availabilityFooterLoader.setController(this);
        root.setCenter(availabilityLoader.load());
        root.setLeft(null);
        root.setBottom(availabilityFooterLoader.load());
    }
    public void employeePaystubClicked() throws IOException {
        System.out.println("employee paystub clicked");
        FXMLLoader paystubLoader = new FXMLLoader(getClass().getResource("employeePaystub.fxml"));
        paystubLoader.setController(this);
        root.setCenter(paystubLoader.load());
        root.setLeft(null);
        root.setBottom(bottomPanel);
    }

    public void employeeNextButtonClicked(){
        System.out.println("employee next clicked");
    }
    public void employeePrevButtonClicked(){
        System.out.println("employee prev clicked");
    }
    public void employeeJumpButtonClicked(){
        System.out.println("employee jump clicked");
eyan_'s avatar
eyan_ committed
    }

    public void logoutClicked(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




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