Newer
Older
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
committed
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;
ArktikHunter
committed
public class ScheduleView extends ScrollPane implements ModelSubscriber{
Model model;
ArrayList<Label> times;
Popup dropShift;
ArktikHunter
committed
GridPane contents;
// must be implemented by the child class
public void draw(){}
public ScheduleView(){
ArktikHunter
committed
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){
ArktikHunter
committed
label.setPrefHeight(18);
dateFormatView = DateTimeFormatter.ofPattern("MMM dd, yyyy");
}
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));
ArktikHunter
committed
GridPane buttons = new GridPane();
buttons.setHgap(10);
buttons.setVgap(10);
ArktikHunter
committed
//Buttons
Button cancel = new Button("Cancel");
cancel.setCancelButton(true);
cancel.setOnAction(cancelDrop); //debug
Button drop = new Button("Drop Shift");
drop.setOnAction(dropShiftConfirm); //debug
ArktikHunter
committed
//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);
}
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
ArktikHunter
committed
shiftRep.setMaxWidth(100);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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();
}
};
ArktikHunter
committed
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();
}
};