Skip to content
Snippets Groups Projects
Controller.java 22 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;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
eyan_'s avatar
eyan_ committed
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
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;
import java.util.Arrays;
import java.util.List;
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;
    Label loggedIn;
    @FXML
    Label viewing;
    @FXML
    Label loginWarning;
    TextField editEmployeeFirstName;
    @FXML
    TextField editEmployeeLastName;
    @FXML
    TextField editEmployeeEmail;
    @FXML
    TextField editEmployeeWage;
    @FXML
    TextField editEmployeePhoneNumber;
    @FXML
    TextField editEmployeeAddress;
    @FXML
    ComboBox editEmployeeBox;
    @FXML
    CheckBox editEmployeeManager;
    ChoiceBox<Employee> selectedEmployee;
    @FXML
    Label currentDateTextAvail;
    @FXML
    ComboBox<String> availabilityDayBox;
    @FXML
    ComboBox<String> editAvailabilityStartBox;
    @FXML
    ComboBox<String> editAvailabilityEndBox;
    @FXML
    Label availabilityResult;
    @FXML
    ComboBox editEmployeePositions;
    @FXML
    ComboBox<String> addShiftPositionBox;
    @FXML
    TextArea timeOffReasonBox;
    @FXML
    DatePicker timeOffDateStart;
    @FXML
    DatePicker timeOffDateEnd;
    @FXML
    Label timeOffLabel;

eyan_'s avatar
eyan_ committed

eyan_'s avatar
eyan_ committed
    Stage popupStage;
    Scene popupScene;
    Stage popupAvailStage;
    Scene popupAvail;
    Stage popupTimeOffStage;
    Scene popupTimeOff;
    EmployeeView employeeView;
ArktikHunter's avatar
ArktikHunter committed
    AvailabilityView availabilityView;
ArktikHunter's avatar
ArktikHunter committed
    //PickupView pickupView;
    Parent sideSchedulePanel;
    Parent paystubView;
    Parent editEmployeeView;
    Parent addEmployeeView;
    Parent staffPanel;
    Parent removeEmployeeView;
    DatePicker jumpDatePicker;

eyan_'s avatar
eyan_ committed

    // login methods, initializes views too
    public void attemptLogin(Event e) throws Exception {
        if (!model.hasEmployee(Integer.parseInt(loginField.getText()))) {
            loginWarning.setText("Invalid id");
        }
        else {
            login(e);
        }
    }
    public void login(Event e) throws Exception {
        //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());
eyan_'s avatar
eyan_ committed

        //load everything
        loadEverything();
        //set up for initial daily schedule view
        Stage stage = (Stage)((Node)e.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600);
        stage.setTitle("Scheduler App");
        stage.setScene(scene);
        stage.show();
        loggedIn.setText("Logged In: " + model.getEmployee(model.getThisEmployee()).toString());
        //set manager specific text
        if (model.getIsManager()){
            selectedEmployee.setItems(FXCollections.observableArrayList(model.getAllEmployees()));
            selectedEmployee.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) ->
                    model.setSelectedEmployee(selectedEmployee.getSelectionModel().getSelectedItem().getEmployeeID()));
            selectedEmployee.getSelectionModel().select(model.getEmployee(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);
ArktikHunter's avatar
ArktikHunter committed
        availabilityView = new AvailabilityView();
        availabilityView.setModel(model);
ArktikHunter's avatar
ArktikHunter committed
        //pickupView = new PickupView();
        //pickupView.setModel(model);
ArktikHunter's avatar
ArktikHunter committed
        model.addSubscriber(availabilityView);

        //fxml views
        loadHeader();
        loadScheduleSide();
        loadScheduleBottom();
        loadPaystubView();
        loadEditEmployee();
        loadStaffPanel();
        loadAddEmployee();
        loadRemoveEmployee();
    /* load functions for individual views / panels
        all need to be called in loadEverything
        emp vs manager distinction made here, later can just load
     */
    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();
eyan_'s avatar
eyan_ committed

    public void loadScheduleSide() throws IOException {
        //same for both
eyan_'s avatar
eyan_ committed
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("schedSidePanel.fxml"));
        sideLoader.setController(this);
        sideSchedulePanel  = sideLoader.load();
eyan_'s avatar
eyan_ committed
    }
    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 loadAvailabilityBottomPanel() throws IOException {
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("availabilityBottomPanel.fxml"));
        sideLoader.setController(this);
        availabilityPanel = sideLoader.load();
eyan_'s avatar
eyan_ committed
    }
    public void loadPaystubView() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(ScheduleApp.class.getResource("paystubView.fxml"));
        fxmlLoader.setController(this);
        paystubView = fxmlLoader.load();
eyan_'s avatar
eyan_ committed
    }
eyan_'s avatar
eyan_ committed

    public void loadEditEmployee() throws IOException{
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("editEmployee.fxml"));
        gridLoader.setController(this);
        editEmployeeView = gridLoader.load();
eyan_'s avatar
eyan_ committed
    }
    public void loadStaffPanel() throws IOException {
        FXMLLoader sideLoader = new FXMLLoader(this.getClass().getResource("staffManagerSidePanel.fxml"));
        sideLoader.setController(this);
        staffPanel = sideLoader.load();
    public void loadAddEmployee() throws IOException {
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("addEmployee.fxml"));
        gridLoader.setController(this);
        addEmployeeView  = gridLoader.load();
    public void loadRemoveEmployee() throws IOException {
        FXMLLoader gridLoader = new FXMLLoader(this.getClass().getResource("removeEmployee.fxml"));
        gridLoader.setController(this);
        removeEmployeeView  = gridLoader.load();
eyan_'s avatar
eyan_ committed
    }
eyan_'s avatar
eyan_ committed

    /* switch to the appropriate view
        same code for emp vs manager, distinction made in load functions
     */
    // schedule tab (default daily view)
    public void viewSchedule(){
        root.setLeft(sideSchedulePanel);
eyan_'s avatar
eyan_ committed
        root.setBottom(bottomPanel);
        root.setCenter(dailyView);
        isDaily = true;
eyan_'s avatar
eyan_ committed
    }
    // schedule -> weekly
    public void viewScheduleWeekly(){
eyan_'s avatar
eyan_ committed
        root.setCenter(weeklyView);
        root.setBottom(bottomPanel);
        isDaily = false;
    // schedule -> daily
    public void viewScheduleDaily(){
        root.setCenter(dailyView);
        root.setBottom(bottomPanel);
        isDaily = true;
    // schedule -> availability
    public void viewAvailability() {
        root.setCenter(availabilityView);
        root.setBottom(availabilityPanel);
        isDaily = false;
    // schedule -> pickups todo
    public void viewPickups(){
ArktikHunter's avatar
ArktikHunter committed
        //root.setCenter(pickupView);
        root.setCenter(null);
eyan_'s avatar
eyan_ committed
        root.setBottom(bottomPanel);
    }

    public void editAvailability() throws IOException {
        FXMLLoader popupLoader = new FXMLLoader(this.getClass().getResource("availabilityPopup.fxml"));
        popupLoader.setController(this);
        popupAvail = new Scene(popupLoader.load(),500,500);
        popupAvailStage = new Stage();
        popupAvailStage.setScene(popupAvail);
        popupAvailStage.show();
    }
    public void requestTimeOff() throws IOException {
        FXMLLoader popupLoader = new FXMLLoader(this.getClass().getResource("timeOffPopup.fxml"));
        popupLoader.setController(this);
        popupTimeOff = new Scene(popupLoader.load(),500,500);
        popupTimeOffStage = new Stage();
        popupTimeOffStage.setScene(popupTimeOff);
        popupTimeOffStage.show();
eyan_'s avatar
eyan_ committed
    }
    //add shift popup (manager only)
    public void addShiftClicked() throws IOException {
        FXMLLoader popupLoader = new FXMLLoader(this.getClass().getResource("addShiftPopup.fxml"));
        popupLoader.setController(this);
        popupScene = new Scene(popupLoader.load(),500,500);
        popupStage = new Stage();
        popupStage.setScene(popupScene);
        popupStage.show();
eyan_'s avatar
eyan_ committed
    }

    // Paystub tab (employee only)
    public void viewPaystub() throws IOException {
        root.setCenter(paystubView);
ArktikHunter's avatar
ArktikHunter committed
        System.out.println("todo: display paystub");
eyan_'s avatar
eyan_ committed
        root.setLeft(null);
ArktikHunter's avatar
ArktikHunter committed
        root.setBottom(null);
eyan_'s avatar
eyan_ committed
    }
    // Payroll tab (manager only)
    public void viewPayroll() {
        root.setCenter(null);   //todo
        System.out.println("todo: display payroll");
eyan_'s avatar
eyan_ committed
        root.setLeft(null);
ArktikHunter's avatar
ArktikHunter committed
        root.setBottom(null);
eyan_'s avatar
eyan_ committed
    }

    // Staff tab (default view staff, manager only)
    public void viewStaff(){
        root.setLeft(staffPanel);
        root.setCenter(employeeView);
        root.setBottom(null);
eyan_'s avatar
eyan_ committed
    }
    // Staff -> edit employee
    public void viewEditEmployee(){
        root.setCenter(editEmployeeView);
eyan_'s avatar
eyan_ committed
    }
    // Staff -> add employee
    public void viewAddEmployee(){
        root.setCenter(addEmployeeView);
eyan_'s avatar
eyan_ committed
    }
    // Staff -> remove employee
    public void viewRemoveEmployee(){
        root.setCenter(removeEmployeeView);
    //Staff -> view employees
    public void viewViewEmployees(){
        root.setCenter(employeeView);
        model.notifySubscribers();
    // Requests tab (manager only)
    public void viewRequests() {
ArktikHunter's avatar
ArktikHunter committed
        root.setCenter(null);
        System.out.println("todo: display requests");
ArktikHunter's avatar
ArktikHunter committed
        root.setLeft(null);
        root.setBottom(null);
    //methods for submitting data
    public void submitTimeOff(MouseEvent event){
ArktikHunter's avatar
ArktikHunter committed
        timeOffLabel.setText(model.addTimeOff(model.getSelectedEmployee(), timeOffDateStart.getValue().toString(),
                timeOffDateEnd.getValue().toString(), timeOffReasonBox.getText()));
    public void addShiftSubmitClicked(MouseEvent event){
        String employee = (String) addShiftEmployeeBox.getValue();
        int index = addShiftEmployeeBox.getSelectionModel().getSelectedIndex();
        String startTime = (String) addShiftStartTimeBox.getValue();
        String endTime = (String) addShiftEndTimeBox.getValue();
        LocalDate date = addShiftDatePicker.getValue();
        String position = addShiftPositionBox.getValue();
        addShiftError.setText(model.addShift(model.getIDbyIndex(index),date.toString(),Integer.parseInt(startTime),
                Integer.parseInt(endTime), position));
    public void submitNewAvailability(MouseEvent e){
        availabilityResult.setText(model.editAvailability(model.getSelectedEmployee(),
                availabilityDayBox.getSelectionModel().getSelectedIndex(),
                Integer.parseInt(editAvailabilityStartBox.getValue()),
                Integer.parseInt(editAvailabilityEndBox.getValue())));
    }
    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 + "!");
        model.notifySubscribers();
    public void editEmployeeClicked(MouseEvent mouseEvent) {
        int id = model.getIDbyIndex(editEmployeeBox.getSelectionModel().getSelectedIndex());
        String newFirstName = editEmployeeFirstName.getText();
        String newLastName = editEmployeeLastName.getText();
        String newEmail = editEmployeeEmail.getText();
        Float newWage = Float.parseFloat(editEmployeeWage.getText());
        String newPhoneNumber = editEmployeePhoneNumber.getText();
        boolean newIsManager = editEmployeeManager.isSelected();
        model.editEmployee(id,newFirstName,newLastName,newIsManager,newEmail,newPhoneNumber,newWage);
        System.out.println("edit employee submit clicked");
    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();
eyan_'s avatar
eyan_ committed
    public void prevButtonClicked (MouseEvent event) throws Exception {
        if (isDaily) model.datePrev();
        else model.weekPrev();
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");
        jumpDatePicker = new DatePicker();
        Scene tempScene = new Scene(jumpDatePicker);
        Stage tempStage = new Stage();
        tempStage.setScene(tempScene);
        tempStage.show();
        jumpDatePicker.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                model.dateJump(jumpDatePicker.getValue());
                try {
                    updateCurrentDate(actionEvent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
eyan_'s avatar
eyan_ committed
        updateCurrentDate(event);
    public void jumpDatePicked(){
        model.dateJump(jumpDatePicker.getValue());
    }
eyan_'s avatar
eyan_ committed
    public void nextButtonClicked (MouseEvent event) throws Exception {
        if (isDaily) model.dateNext();
        else model.weekNext();
eyan_'s avatar
eyan_ committed
        updateCurrentDate(event);
    public void cancelClicked(MouseEvent e){
        ((Node)e.getSource()).getScene().getWindow().hide();
    }
    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();

    // helper methods for comboboxes
    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 temp = (ComboBox) event.getSource();
        if(event.getSource() == editEmployeeBox){
            System.out.println("test");
        }
        temp.setItems(list);
    public void populatePositionBox(MouseEvent event) {
        ComboBox test = (ComboBox) event.getSource();
        test.setItems(FXCollections.observableArrayList(model.returnPositions()));
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);
    }
    public void populateDaysOfWeek(MouseEvent event){
        List<String> times = Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
        ComboBox box = (ComboBox) event.getSource();
        ObservableList<String> list = FXCollections.observableArrayList(times);
        box.setItems(list);
    }
eyan_'s avatar
eyan_ committed
    public void addShiftTimeBoxClicked(MouseEvent event){
        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);
        box.setItems(list);
eyan_'s avatar
eyan_ committed
    }
    public void availabilityTimeBoxClicked(MouseEvent event){
        ArrayList<String> times = new ArrayList<>();
        for (int i = 0; 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);
eyan_'s avatar
eyan_ committed
        }
        ComboBox box = (ComboBox) event.getSource();
        ObservableList<String> list = FXCollections.observableArrayList(times);
        box.setItems(list);
    public void updateCurrentDate(Event event) throws Exception {
        currentDateText.setText("Current Date: " + model.date.toString());
        currentDateTextAvail.setText("Current Date: " + model.date.toString());
    public void setModel(Model model) {
        this.model = model;
    }
    public void fillEditEmployee(){
        int boxIndex = editEmployeeBox.getSelectionModel().getSelectedIndex();
        if(boxIndex >= 0){
            int index = model.getIDbyIndex(boxIndex);
            Employee currentEmployee = model.getEmployee(index);
            editEmployeeFirstName.setText(currentEmployee.getFirstName());
            editEmployeeLastName.setText(currentEmployee.getLastName());
            editEmployeeManager.setSelected(currentEmployee.isManager());
            editEmployeeEmail.setText(currentEmployee.getEmail());
            editEmployeePhoneNumber.setText(currentEmployee.getPhoneNumber());
            System.out.println(currentEmployee.getPositions());
        }
    }
    public void addRemovePosition(){
        VBox tempBox = new VBox();
        tempBox.setPadding(new Insets(20));
        tempBox.setSpacing(20);
        ComboBox tempCombo = new ComboBox();
        Button addPosition = new Button("Add Position");
        Button removePosition = new Button("Remove Position");
        Label result = new Label("");
        tempBox.getChildren().addAll(tempCombo,addPosition,removePosition, result);
        tempCombo.setOnMouseClicked(this::populatePositionBox);

        addPosition.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                VBox tempVBox = new VBox();
                tempVBox.setSpacing(20);
                tempVBox.setPadding(new Insets(30));
                TextField tempPositionText = new TextField("Enter Position Here");
                TextField tempWageText = new TextField("Enter Wage Here");
                Button addPositionSubmit = new Button("Submit");
                tempVBox.getChildren().addAll(tempPositionText,tempWageText,addPositionSubmit);
                Scene tempScene = new Scene(tempVBox);
                Stage tempStage = new Stage();
                tempStage.setScene(tempScene);
                tempStage.show();
                addPositionSubmit.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        System.out.println("add position submit clicked");
                        System.out.println(model.addPosition(tempPositionText.getText(),Float.parseFloat(tempWageText.getText())));
                        tempStage.close();
                    }
                });
            }
        });
        removePosition.setOnMouseClicked(e -> {
            if (tempCombo.getValue() == null) result.setText("No position selected.");
            else result.setText(model.removePosition(tempCombo.getSelectionModel().getSelectedItem().toString()));
                });

eyan_'s avatar
eyan_ committed
}