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

ArktikHunter's avatar
ArktikHunter committed
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;
ArktikHunter's avatar
ArktikHunter committed
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class ScheduleView extends ScrollPane implements ModelSubscriber{
ArktikHunter's avatar
ArktikHunter committed
    Model model;
    ArrayList<Label> times;
    Popup dropShift;
ArktikHunter's avatar
ArktikHunter committed
    DateTimeFormatter dateFormatView;
ArktikHunter's avatar
ArktikHunter committed
    DateTimeFormatter timeFormat;
ArktikHunter's avatar
ArktikHunter committed

ArktikHunter's avatar
ArktikHunter committed

ArktikHunter's avatar
ArktikHunter committed
    // must be implemented by the child class
    public void draw(){}

    public ScheduleView(){
        contents = new GridPane();
        this.setContent(contents);

ArktikHunter's avatar
ArktikHunter committed
        //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){
ArktikHunter's avatar
ArktikHunter committed
            //label.setStyle(" -fx-border-color: black");
        }
ArktikHunter's avatar
ArktikHunter committed
        dateFormatView = DateTimeFormatter.ofPattern("MMM dd, yyyy");
ArktikHunter's avatar
ArktikHunter committed
        timeFormat = DateTimeFormatter.ofPattern("kk:mm");
ArktikHunter's avatar
ArktikHunter committed

ArktikHunter's avatar
ArktikHunter committed
    }

    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
ArktikHunter's avatar
ArktikHunter committed
        Label date = new Label("Date: " + shift.getStart().format(dateFormatView));
ArktikHunter's avatar
ArktikHunter committed
        Label start = new Label("Start: " + shift.getStart().format(timeFormat));
        Label end = new Label("End: " + shift.getEnd().format(timeFormat));

        GridPane buttons = new GridPane();
        buttons.setHgap(10);
        buttons.setVgap(10);
ArktikHunter's avatar
ArktikHunter committed

ArktikHunter's avatar
ArktikHunter committed
        Button cancel = new Button("Cancel");
        cancel.setCancelButton(true);
        cancel.setOnAction(cancelDrop);   //debug

        Button drop = new Button("Drop Shift");
        drop.setOnAction(dropShiftConfirm);      //debug

        //Manager Buttons
        Button remove = new Button("Remove Shift");
        remove.setOnAction(removeShift);

        Button edit = new Button("Edit Shift");
        edit.setOnAction(editShift);

        buttons.add(cancel, 0, 0);
        if (model.getThisEmployee() == shift.getEmployeeID()){
            buttons.add(drop, 1, 0);
        }
        if (model.getIsManager()) {
            buttons.add(edit, 0, 1);
            buttons.add(remove, 1, 1);
        }
        for (Node button : buttons.getChildren()){
            ((Button) button).setMaxWidth(200);
        }

ArktikHunter's avatar
ArktikHunter committed
        details.getChildren().addAll(name, date, start, end, buttons);
        dropShift.getContent().add(details);
        dropShift.setAutoHide(true);
    }

eyan_'s avatar
eyan_ committed

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

    EventHandler<ActionEvent> removeShift =
            new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    System.out.println("todo: delete shift");
                    dropShift.hide();
                }
            };

    EventHandler<ActionEvent> editShift =
            new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    System.out.println("todo: edit shift");
                    dropShift.hide();
                }
            };

ArktikHunter's avatar
ArktikHunter committed

eyan_'s avatar
eyan_ committed
}