From 129a26e1a96acfa2ffb7ef7e741a7f223836cbe1 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Mon, 28 Nov 2022 02:49:55 +0100 Subject: [PATCH] ui display task schedule --- .../MyScheduleController.java | 157 ++++++++++-------- .../ch/zhaw/gartenverwaltung/MySchedule.fxml | 54 ++---- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 2 +- 3 files changed, 101 insertions(+), 112 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java index e895480..f73feb2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -9,11 +9,14 @@ import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Task; +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.collections.FXCollections; import javafx.fxml.FXML; -import javafx.scene.control.Label; -import javafx.scene.control.ListCell; -import javafx.scene.control.ListView; +import javafx.scene.control.*; +import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; +import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import java.io.IOException; @@ -24,6 +27,7 @@ import java.util.logging.Logger; public class MyScheduleController { private static final Logger LOG = Logger.getLogger(MyScheduleController.class.getName()); + private final ListProperty> taskListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private Crop selectedCrop = null; @@ -35,46 +39,7 @@ public class MyScheduleController { private PlantList plantList; @FXML - private Label day1_label; - - @FXML - private Pane day1_pane; - - @FXML - private Label day2_label; - - @FXML - private Pane day2_pane; - - @FXML - private Label day3_label; - - @FXML - private Pane day3_pane; - - @FXML - private Label day4_label; - - @FXML - private Pane day4_pane; - - @FXML - private Label day5_label; - - @FXML - private Pane day5_pane; - - @FXML - private Label day6_label; - - @FXML - private Pane day6_pane; - - @FXML - private Label day7_label; - - @FXML - private Pane day7_pane; + private ListView> week_listView; @FXML private Label information_label; @@ -85,10 +50,11 @@ public class MyScheduleController { @AfterInject @SuppressWarnings("unused") public void init() { - setCellFactoryListView(); + setCellFactoryCropListView(); + setCellFactoryTaskListView(); scheduledPlants_listview.itemsProperty().bind(garden.getPlantedCrops()); + week_listView.itemsProperty().bind(taskListProperty); lookForSelectedListEntries(); - setDayLabels(); information_label.setText(""); try { loadTaskList(); @@ -108,18 +74,7 @@ public class MyScheduleController { }); } - private void setDayLabels() { - LocalDate today = LocalDate.now(); - day1_label.setText(today.getDayOfWeek().toString()); - day2_label.setText(today.plusDays(1).getDayOfWeek().toString()); - day3_label.setText(today.plusDays(2).getDayOfWeek().toString()); - day4_label.setText(today.plusDays(3).getDayOfWeek().toString()); - day5_label.setText(today.plusDays(4).getDayOfWeek().toString()); - day6_label.setText(today.plusDays(5).getDayOfWeek().toString()); - day7_label.setText(today.plusDays(6).getDayOfWeek().toString()); - } - - private void setCellFactoryListView() { + private void setCellFactoryCropListView() { scheduledPlants_listview.setCellFactory(param -> new ListCell<>() { @Override protected void updateItem(Crop crop, boolean empty) { @@ -141,6 +96,23 @@ public class MyScheduleController { }); } + private void setCellFactoryTaskListView() { + week_listView.setCellFactory(param -> new ListCell<>() { + @Override + protected void updateItem(List taskList, boolean empty) { + super.updateItem(taskList, empty); + + if (empty || taskList == null) { + setGraphic(null); + setText(null); + } else { + setText(""); + setGraphic(weekTaskVBox(taskList, this.getIndex())); + } + } + }); + } + private void loadTaskList() throws IOException { List> taskLists; if (selectedCrop != null) { @@ -148,25 +120,66 @@ public class MyScheduleController { } else { taskLists = gardenSchedule.getTasksUpcomingWeek(); } - if (!taskLists.isEmpty()) { - viewTaskListOfDay(day1_pane, taskLists.get(0)); - viewTaskListOfDay(day2_pane, taskLists.get(1)); - viewTaskListOfDay(day3_pane, taskLists.get(2)); - viewTaskListOfDay(day4_pane, taskLists.get(3)); - viewTaskListOfDay(day5_pane, taskLists.get(4)); - viewTaskListOfDay(day6_pane, taskLists.get(5)); - viewTaskListOfDay(day7_pane, taskLists.get(6)); - } + taskListProperty.clear(); + taskListProperty.addAll(taskLists); } - private void viewTaskListOfDay(Pane pane, List tasks) { - //ToDo update pane with task list - VBox vBox = new VBox(); + private VBox weekTaskVBox(List tasks, int dayIndex) { + VBox vBox = new VBox(10); + LocalDate today = LocalDate.now(); + Label weekDay = new Label(today.plusDays(dayIndex).getDayOfWeek().toString()); + weekDay.setStyle("-fx-font-weight: bold; -fx-underline: true"); + vBox.getChildren().add(weekDay); for (Task task : tasks) { - Label label = new Label(task.getDescription()); - vBox.getChildren().add(label); + HBox hBox = new HBox(10); + Label taskName = new Label(task.getName() + ":"); + taskName.setStyle("-fx-font-weight: bold"); + taskName.setMinWidth(100); + taskName.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); + hBox.getChildren().addAll(taskName); + + HBox hBoxDescription = new HBox(); + Label taskDescription = new Label(task.getDescription()); + taskDescription.setWrapText(true); + taskDescription.setMaxSize(600, Double.MAX_VALUE); + Pane puffer = new Pane(); + HBox.setHgrow(puffer, Priority.ALWAYS); + CheckBox checkBox = new CheckBox("Task completed?"); + checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> { + if (newValue) { + showConfirmation(task, checkBox); + } + }); + HBox.setHgrow(checkBox, Priority.NEVER); + hBoxDescription.getChildren().addAll(taskDescription, puffer, checkBox); + vBox.getChildren().addAll(hBox, hBoxDescription); } - pane.getChildren().add(vBox); + return vBox; + } + + /** + * Alert to confirm that task has been completed. + * @param task {@link Task} which is selected + */ + private void showConfirmation(Task task, CheckBox checkBox) { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Task Completed?"); + alert.setHeaderText("Are you sure you have completed this task?"); + alert.setContentText("Confirming that you have completed the task will remove it from the schedule."); + + alert.showAndWait() + .ifPresent(buttonType -> { + if (buttonType == ButtonType.OK) { + task.done(); + try { + loadTaskList(); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + checkBox.setSelected(false); + } + }); } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/MySchedule.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/MySchedule.fxml index b778d50..d09db93 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/MySchedule.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/MySchedule.fxml @@ -1,19 +1,15 @@ + - - - - - + - + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index ec76a31..6fc508f 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -300,7 +300,7 @@ "relativeStartDate": 0, "relativeEndDate": 10, "description": "When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.", - "interval": 3 + "interval": 1 } ] },