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);