From dcb97f1c55a926dfe32b2e42fdc0765b2321005e Mon Sep 17 00:00:00 2001 From: giavaphi Date: Mon, 21 Nov 2022 06:47:27 +0100 Subject: [PATCH] add and edit task list --- .../CropDetailController.java | 186 ++++++++++++++---- .../TaskFormularController.java | 47 +++++ .../ch/zhaw/gartenverwaltung/CropDetail.fxml | 17 +- .../zhaw/gartenverwaltung/TaskFormular.fxml | 54 +++++ 4 files changed, 260 insertions(+), 44 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java create mode 100644 src/main/resources/ch/zhaw/gartenverwaltung/TaskFormular.fxml diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 997ac93..7ec8652 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -1,5 +1,6 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.bootstrap.Inject; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.models.Garden; @@ -10,14 +11,18 @@ import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Pest; 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.event.ActionEvent; +import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.geometry.Pos; -import javafx.scene.control.Button; -import javafx.scene.control.Label; +import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; -import javafx.scene.layout.VBox; +import javafx.scene.layout.Priority; import javafx.stage.Stage; import java.io.IOException; @@ -35,7 +40,12 @@ public class CropDetailController { @Inject private Garden garden; + @Inject + AppLoader appLoader; + private static final Logger LOG = Logger.getLogger(CropDetailController.class.getName()); + private final ListProperty taskListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); + private final ListProperty pestListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); @FXML private ImageView imageView; @@ -52,9 +62,6 @@ public class CropDetailController { @FXML private Label description_label; - @FXML - private VBox growthPhases_vbox; - @FXML private Label location_label; @@ -64,9 +71,6 @@ public class CropDetailController { @FXML private Button location_button; - @FXML - private VBox pests_vbox; - @FXML private Label soil_label; @@ -74,11 +78,17 @@ public class CropDetailController { private Label spacing_label; @FXML - private Button editTaskList_button; + private Button addTask_button; @FXML - void editTaskList() { + private ListView taskList_listView; + @FXML + private ListView pests_listView; + + @FXML + void addTask() throws IOException { + createTaskDialog(true, null); } /** @@ -128,25 +138,65 @@ public class CropDetailController { } area_label.setText(""); location_label.setText(""); - createTaskLists(crop); - createPestList(plant); + + setTaskListProperty(crop); + taskList_listView.itemsProperty().bind(taskListProperty); + + pestListProperty.addAll(plant.pests()); + pests_listView.itemsProperty().bind(pestListProperty); + + } catch (HardinessZoneNotSetException | IOException e) { throw new PlantNotFoundException(); } - setIconToButton(editTaskList_button, "editIcon.png"); + setIconToButton(addTask_button, "addIcon.png"); setIconToButton(area_button, "areaIcon.png"); setIconToButton(location_button, "locationIcon.png"); + setCellFactoryPests(); + setCellFactoryTasks(); } - private void createTaskLists(Crop crop) { + private void setCellFactoryTasks() { + taskList_listView.setCellFactory(param -> new ListCell<>() { + @Override + protected void updateItem(Task task, boolean empty) { + super.updateItem(task, empty); + + if (empty || task == null) { + setText(null); + setGraphic(null); + } else { + setText(""); + setGraphic(createTaskHBox(task)); + } + } + }); + } + + private void setCellFactoryPests() { + pests_listView.setCellFactory(param -> new ListCell<>() { + @Override + protected void updateItem(Pest pest, boolean empty) { + super.updateItem(pest, empty); + + if (empty || pest == null) { + setText(null); + setGraphic(null); + } else { + setText(""); + setGraphic(createPestHBox(pest)); + } + } + }); + } + + private void setTaskListProperty(Crop crop) { crop.getCropId().ifPresent(id -> { List taskList; try { taskList = gardenSchedule.getTaskListForCrop(id); - for (Task task : taskList) { - Label label = new Label(task.getDescription()); - growthPhases_vbox.getChildren().add(label); - } + taskListProperty.clear(); + taskListProperty.addAll(taskList); } catch (IOException e) { // TODO: Alert LOG.log(Level.SEVERE, "Could not get task list for crop", e.getCause()); @@ -154,22 +204,37 @@ public class CropDetailController { }); } - private void createPestList(Plant plant) { - List pests = plant.pests(); - for (Pest pest : pests) { - Label label = new Label(pest.name() + ":"); - label.setStyle("-fx-font-weight: bold"); - HBox hBox = new HBox(); - hBox.fillHeightProperty(); - Label label1 = new Label(pest.description()); - label1.setAlignment(Pos.TOP_LEFT); - label1.setWrapText(true); - label1.setMaxWidth(600); - label1.setMaxHeight(100); - Button button = new Button("Get Counter Measures"); - hBox.getChildren().addAll(label1, button); - pests_vbox.getChildren().addAll(label, hBox); - } + private HBox createTaskHBox(Task task) { + HBox hBox = new HBox(); + Label taskName = new Label(task.getName()+": "); + taskName.setStyle("-fx-font-weight: bold"); + Label taskDescription = new Label(task.getDescription()); + taskDescription.setWrapText(true); + taskDescription.setMaxWidth(2000); + HBox.setHgrow(taskDescription, Priority.ALWAYS); + + Button edit = new Button(); + Button delete = new Button(); + setIconToButton(edit, "editIcon.png"); + setIconToButton(delete, "deleteIcon.png"); + edit.setOnAction(getEditTaskEvent(task)); + + hBox.getChildren().addAll(taskName, taskDescription, edit, delete); + return hBox; + } + + private HBox createPestHBox(Pest pest) { + Label label = new Label(pest.name() + ": "); + label.setStyle("-fx-font-weight: bold"); + HBox hBox = new HBox(); + hBox.fillHeightProperty(); + Label label1 = new Label(pest.description()); + label1.setAlignment(Pos.TOP_LEFT); + label1.setWrapText(true); + label1.setMaxWidth(600); + Button button = new Button("Get Counter Measures"); + hBox.getChildren().addAll(label, label1, button); + return hBox; } /** @@ -184,4 +249,55 @@ public class CropDetailController { imageView.setPreserveRatio(true); button.setGraphic(imageView); } + + private EventHandler getEditTaskEvent(Task task) { + return (event) -> { + try { + createTaskDialog(false, task); + } catch (IOException e) { + e.printStackTrace(); + } + }; + } + + private void createTaskDialog(boolean newTask, Task givenTask) throws IOException { + Dialog dialog = new Dialog<>(); + dialog.setTitle("Set Task"); + dialog.setHeaderText("Add/Edit Task:"); + dialog.setResizable(false); + + DialogPane dialogPane = dialog.getDialogPane(); + + ButtonType saveTask; + if(newTask) { + saveTask = new ButtonType("Add", ButtonBar.ButtonData.OK_DONE); + } else { + saveTask = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE); + } + dialogPane.getButtonTypes().addAll(saveTask, ButtonType.CANCEL); + + if (appLoader.loadPaneToDialog("TaskFormular.fxml", dialogPane) instanceof TaskFormularController controller) { + controller.setCorp(this.crop); + if (!newTask) { + controller.setTaskValue(givenTask); + } + dialog.setResultConverter(button -> button.equals(saveTask) ? controller.returnResult() : null); + + dialog.showAndWait() + .ifPresent(task -> { + if (newTask) { + try { + gardenSchedule.addTask(task); + setTaskListProperty(this.crop); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + //ToDo method to edit task + setTaskListProperty(this.crop); + } + }); + } + + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java b/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java new file mode 100644 index 0000000..46cac68 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java @@ -0,0 +1,47 @@ +package ch.zhaw.gartenverwaltung; + +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Task; +import javafx.fxml.FXML; +import javafx.scene.control.DatePicker; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; + +public class TaskFormularController { + Crop crop; + + @FXML + private TextArea description_area; + + @FXML + private DatePicker end_datePicker; + + @FXML + private TextField interval_field; + + @FXML + private DatePicker start_datePicker; + + @FXML + private TextField taskName_field; + + + public Task returnResult() { + Task task = new Task(taskName_field.getText(), description_area.getText(), + start_datePicker.getValue(), end_datePicker.getValue(), + Integer.parseInt(interval_field.getText()), crop.getCropId().get()); + return task; + } + + public void setCorp(Crop crop) { + this.crop = crop; + } + + public void setTaskValue(Task task) { + taskName_field.setText(task.getName()); + description_area.setText(task.getDescription()); + start_datePicker.setValue(task.getStartDate()); + end_datePicker.setValue(task.getEndDate().get()); + interval_field.setText(task.getInterval().get().toString()); + } +} diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml index 160c0a7..c0f0a7c 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml @@ -3,6 +3,7 @@ + @@ -12,13 +13,11 @@ - - + - + @@ -85,22 +84,22 @@ - + - -