package com.example.schedulerapp; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Popup; import java.time.format.DateTimeFormatter; import java.util.ArrayList; public class ScheduleView extends GridPane implements ModelSubscriber{ Model model; ArrayList<Label> times; Popup dropShift; DateTimeFormatter dateFormatView; DateTimeFormatter timeFormat; // must be implemented by the child class public void draw(){} public ScheduleView(){ //list of time labels 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; } Label jLabel = new Label(j); Label kLabel = new Label(k); times.add(jLabel); times.add(kLabel); } for (Label label : times){ label.setPrefHeight(20); //label.setStyle(" -fx-border-color: black"); } dateFormatView = DateTimeFormatter.ofPattern("MMM dd, yyyy"); timeFormat = DateTimeFormatter.ofPattern("kk:mm"); } public void setModel(Model newModel) { this.model = newModel; //now can draw for first time draw(); } public void drawPopUp(Shift shift){ dropShift = new Popup(); dropShift.setHeight(300); dropShift.setWidth(300); VBox details = new VBox(); details.setStyle("-fx-background-color: papayawhip; -fx-border-color: black"); details.setPadding(new Insets(20)); details.setSpacing(20); Label name = new Label("Name: " + shift.getEmployeeID()); //todo: display name instead Label date = new Label("Date: " + shift.getStart().format(dateFormatView)); Label start = new Label("Start: " + shift.getStart().format(timeFormat)); Label end = new Label("End: " + shift.getEnd().format(timeFormat)); HBox buttons = new HBox(); buttons.setSpacing(10); //todo: only display buttons if this is your shift Button cancel = new Button("Cancel"); cancel.setCancelButton(true); cancel.setOnAction(cancelDrop); //debug Button drop = new Button("Drop Shift"); drop.setDefaultButton(true); drop.setOnAction(dropShiftConfirm); //debug buttons.getChildren().addAll(cancel, drop); details.getChildren().addAll(name, date, start, end, buttons); dropShift.getContent().add(details); dropShift.setAutoHide(true); } public void modelChanged() { draw(); } public String formatShiftTimeLabel(Shift shift){ String text = shift.getStart().format(timeFormat); text += "\n - \n"; text += shift.getEnd().format(timeFormat); return text; } public ShiftButton formatShiftButton(Shift shift){ ShiftButton shiftRep = new ShiftButton(formatShiftTimeLabel(shift), shift); shiftRep.setMaxHeight(700); //so it always fills its region shiftRep.setPrefWidth(60); GridPane.setFillHeight(shiftRep, true); GridPane.setFillWidth(shiftRep, true); //css styling //todo: different colour for current user shiftRep.setStyle("-fx-background-color: skyblue"); shiftRep.setOnAction(dropShiftPopUp); return shiftRep; } public int shiftGetRow(Shift shift){ //convert from start time to offset, granularity in 30 min increments int row = (shift.getStart().getHour() - 8 ) * 2 + 1; if (shift.getStart().getMinute() == 30) row++; return row; } public int shiftGetLength(Shift shift){ //calculate length int length = (shift.getEnd().getHour() - shift.getStart().getHour()) * 2; if (shift.getStart().getMinute() == 30) length--; if (shift.getEnd().getMinute() == 30) length++; return length; } //event handlers EventHandler<ActionEvent> dropShiftPopUp = new EventHandler<>() { @Override public void handle(ActionEvent e) { drawPopUp(((ShiftButton) e.getSource()).getShift()); dropShift.show(((Node) e.getSource()).getScene().getWindow()); } }; EventHandler<ActionEvent> dropShiftConfirm = new EventHandler<>() { @Override public void handle(ActionEvent e) { System.out.println("todo: drop shift :)"); dropShift.hide(); } }; EventHandler<ActionEvent> cancelDrop = new EventHandler<>() { @Override public void handle(ActionEvent actionEvent) { System.out.println("Shift drop cancelled."); dropShift.hide(); } }; }