package com.example.schedulerapp; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.MouseEvent; import javafx.scene.layout.*; import javafx.stage.Stage; import java.io.IOException; import java.time.LocalDate; import java.util.ArrayList; public class Controller { @FXML BorderPane myBorderPane; @FXML VBox leftSideButtons; @FXML public BorderPane rootBorderPane; @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; @FXML ComboBox removeEmployeeBox; @FXML ComboBox addShiftStartTimeBox; @FXML ComboBox addShiftEndTimeBox; @FXML Label currentDateText; @FXML TextField loginField; @FXML Label loggedIn; @FXML Label viewing; @FXML Label loginWarning; Stage popupStage; Scene popupScene; Model model; DailyView dailyView; WeeklyView weeklyView; EmployeeView employeeView; AvailabilityView availabilityView; BorderPane root; Parent header; Parent sideSchedulePanel; Parent bottomPanel; Parent paystubView; Parent editEmployeeView; Parent addEmployeeView; Parent staffPanel; 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); availabilityView = new AvailabilityView(); availabilityView.setModel(model); model.addSubscriber(availabilityView); //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() { root.setCenter(availabilityView); } 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"); } public void prevButtonClicked (MouseEvent event) throws Exception { System.out.println("prev button clicked"); model.datePrev(); updateCurrentDate(event); } public void jumpButtonClicked (MouseEvent event) throws Exception { System.out.println("jump button clicked"); //model.dateJump(); updateCurrentDate(event); } public void nextButtonClicked (MouseEvent event) throws Exception { System.out.println("next button clicked"); System.out.println(isDaily); model.dateNext(); updateCurrentDate(event); } 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(); } public void populateEmployeeBox(MouseEvent event) { ArrayList<String> aList = model.returnFormattedEmployeeNames(); ObservableList<String> list = FXCollections.observableArrayList(); list.addAll(aList); 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(); } public void addShiftSubmitClicked(MouseEvent event){ System.out.println("submit clicked"); String employee = (String) addShiftEmployeeBox.getValue(); int index = addShiftEmployeeBox.getSelectionModel().getSelectedIndex(); String startTime = (String) addShiftStartTimeBox.getValue(); String endTime = (String) addShiftEndTimeBox.getValue(); LocalDate date = addShiftDatePicker.getValue(); 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? } public void removeEmployeeBoxClicked(MouseEvent event){ System.out.println("remove employee box clicked"); populateEmployeeBox(event); } 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); box.setItems(list); } public void updateCurrentDate(MouseEvent event) throws Exception { if(currentDateText == null){ currentDateText = new Label(); } currentDateText.setText("Current Date: " + model.date.toString()); } 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"); } // Header 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(); } // Manager Page // 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 { 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) { //edit employee } // Remove Employee public void removeEmployeeClicked(MouseEvent mouseEvent) { int index = removeEmployeeBox.getSelectionModel().getSelectedIndex(); String id = String.valueOf(model.getIDbyIndex(index)); model.removeEmployee(id); removeEmployeeBox.getSelectionModel().clearSelection(); } // View Employees public void setModel(Model model) { this.model = model; } }