From df19edb25a419a65b611feac9ef21156d50605a0 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Thu, 24 Nov 2022 23:55:54 +0100 Subject: [PATCH] delete tasks --- .../CropDetailController.java | 26 +++++++++++++++++++ .../gartenverwaltung/types/TaskTemplate.java | 4 +++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index dd7e803..90f52d8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -218,6 +218,7 @@ public class CropDetailController { setIconToButton(edit, "editIcon.png"); setIconToButton(delete, "deleteIcon.png"); edit.setOnAction(getEditTaskEvent(task)); + delete.setOnAction(deleteTask(task)); hBox.getChildren().addAll(taskName, taskDescription, edit, delete); return hBox; @@ -260,6 +261,12 @@ public class CropDetailController { }; } + private EventHandler deleteTask(Task task) { + return (event) -> { + showDeleteTask(task); + }; + } + private void createTaskDialog(boolean newTask, Task givenTask) throws IOException { Dialog dialog = new Dialog<>(); dialog.setTitle("Set Task"); @@ -332,4 +339,23 @@ public class CropDetailController { }); } } + + private void showDeleteTask(Task task) { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Delete " + task.getName()); + alert.setHeaderText("Are you sure want to delete this Task?"); + + alert.showAndWait() + .ifPresent(buttonType -> { + if (buttonType == ButtonType.OK) { + try { + gardenSchedule.removeTask(task); + setTaskListProperty(this.crop); + } catch (IOException e) { + // TODO: Show error alert + LOG.log(Level.SEVERE, "Could not remove crop.", e); + } + } + }); + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java b/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java index 2e471cb..7298fd2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java @@ -48,6 +48,10 @@ public class TaskTemplate { public Task generateTask(LocalDate realStartDate, long cropId) { LocalDate endDate = relativeEndDate != null ? realStartDate.plusDays(relativeEndDate) : null; + if (interval == null) { + this.interval = 0; + } + return new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId) .withInterval(interval) .withEndDate(endDate);