-
ArktikHunter authoredArktikHunter authored
ScheduleView.java 7.09 KiB
package com.example.schedulerapp;
import javafx.application.Platform;
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.control.ScrollPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class ScheduleView extends ScrollPane implements ModelSubscriber{
Model model;
ArrayList<Label> times;
Popup dropShift;
DateTimeFormatter dateFormatView;
DateTimeFormatter timeFormat;
GridPane contents;
// must be implemented by the child class
public void draw(){}
public ScheduleView(){
contents = new GridPane();
this.setContent(contents);
//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(18);
//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
Timer drawTimer = new Timer(true);
drawTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> {
draw();
});
}
}, 1000, 1000);
}
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: " + model.getEmployeeByID(shift.getEmployeeID()));
Label position = new Label("Position: " + shift.getPosition());
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));
GridPane buttons = new GridPane();
buttons.setHgap(10);
buttons.setVgap(10);
//Buttons
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(e -> removeShiftTest(shift.getShiftID()));
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);
}
details.getChildren().addAll(name, position, 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.setMaxWidth(100);
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");
//model.removeShift();
dropShift.hide();
}
};
private void removeShiftTest(int shiftID){
model.removeShift(shiftID);
}
EventHandler<ActionEvent> editShift =
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
System.out.println("todo: edit shift");
dropShift.hide();
}
};
private class DrawMe extends TimerTask {
/**
* The action to be performed by this timer task.
*/
@Override
public void run() {
draw();
}
}
}