From 8e23124c6ba6554461eadf73acbd3ce5784a70dd Mon Sep 17 00:00:00 2001 From: giavaphi Date: Thu, 10 Nov 2022 22:50:49 +0100 Subject: [PATCH] display List of Crops, get detail of crop and display taskList --- .../CropDetailController.java | 68 +++++++++++++++++-- .../gartenverwaltung/MyPlantsController.java | 50 ++++++++++---- .../MyScheduleController.java | 17 +++-- .../SelectSowDayController.java | 3 +- .../ch/zhaw/gartenverwaltung/types/Task.java | 3 +- .../ch/zhaw/gartenverwaltung/CropDetail.fxml | 2 +- .../ch/zhaw/gartenverwaltung/MyPlants.fxml | 6 +- 7 files changed, 115 insertions(+), 34 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 5e34031..6e5d792 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -1,16 +1,34 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.plantList.PlantListModel; +import ch.zhaw.gartenverwaltung.taskList.TaskListModel; 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.event.ActionEvent; import javafx.fxml.FXML; +import javafx.geometry.Pos; import javafx.scene.control.Button; import javafx.scene.control.Label; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; +import javafx.stage.Stage; + +import java.io.IOException; +import java.util.List; public class CropDetailController { private Crop crop = null; - private Plant plant = null; + private final PlantListModel plantListModel = new PlantListModel(); + private final TaskListModel taskListModel = new TaskListModel(); + private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + + @FXML + private ImageView imageView; @FXML private Button area_button; @@ -45,6 +63,9 @@ public class CropDetailController { @FXML private Label spacing_label; + public CropDetailController() throws IOException { + } + @FXML void editTaskList(ActionEvent event) { @@ -52,7 +73,8 @@ public class CropDetailController { @FXML void goBack(ActionEvent event) { - + Stage stage = (Stage) imageView.getScene().getWindow(); + stage.close(); } @FXML @@ -65,8 +87,46 @@ public class CropDetailController { } - public void setPlantFromCrop(Crop crop) { + public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException { this.crop = crop; - this.plant = null; + Plant plant = plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0); + cropName_label.setText(plant.name()); + description_label.setText(plant.description()); + light_label.setText(String.valueOf(plant.light())); + soil_label.setText(plant.soil()); + spacing_label.setText(plant.spacing()); + if (plant.image() != null) { + imageView.setImage(plant.image()); + } + area_label.setText(""); + location_label.setText(""); + createTaskLists(crop); + createPestList(plant); + } + + private void createTaskLists(Crop crop) throws IOException { + List taskList = taskListModel.getTaskListForCrop(crop.getCropId().get()); + for (Task task : taskList) { + Label label = new Label(task.getDescription()); + growthPhases_vbox.getChildren().add(label); + } + } + + 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); + } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java index b0eb1d2..3d9d82c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java @@ -1,8 +1,11 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.plantList.PlantListModel; import ch.zhaw.gartenverwaltung.taskList.TaskListModel; import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Plant; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; @@ -14,7 +17,9 @@ import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; +import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.stage.Modality; import javafx.stage.Stage; @@ -27,6 +32,7 @@ public class MyPlantsController implements Initializable { MainFXMLController mainController; private final TaskListModel taskListModel = new TaskListModel(); private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final PlantListModel plantListModel = new PlantListModel(); @FXML private VBox myPlants_vbox; @@ -41,10 +47,15 @@ public class MyPlantsController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - loadCropList(); + //ToDo update, when new crops are added + try { + loadCropList(); + } catch (HardinessZoneNotSetException | IOException e) { + e.printStackTrace(); + } } - private void loadCropList() { + private void loadCropList() throws HardinessZoneNotSetException, IOException { List cropList = new LinkedList<>(); try { cropList = getCropList(); @@ -54,7 +65,7 @@ public class MyPlantsController implements Initializable { createPlantView(cropList); } - private void createPlantView(List crops) { + private void createPlantView(List crops) throws HardinessZoneNotSetException, IOException { myPlants_vbox.getChildren().clear(); for(Crop crop : crops) { HBox hBox = createPlantView(crop); @@ -72,16 +83,27 @@ public class MyPlantsController implements Initializable { return cropList; } - private HBox createPlantView(Crop crop) { - //ToDo add good design - //ToDo get plant information from crop - HBox hBox = new HBox(); - Label label = new Label("Placeholder"); + private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException { + //ToDo add better design + Plant plant = plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0); + HBox hBox = new HBox(10); + ImageView imageView = new ImageView(); + imageView.setPreserveRatio(false); + imageView.setFitHeight(100); + imageView.setFitWidth(100); + imageView.maxHeight(100); + if (plant.image() != null) { + imageView.setImage(plant.image()); + } + hBox.setMinHeight(100); + Label label = new Label(plant.name()); + label.setMaxWidth(2000); + HBox.setHgrow(label, Priority.ALWAYS); Button details = new Button("Details"); Button delete = new Button("delete"); details.setOnAction(getGoToCropDetailEvent(crop)); delete.setOnAction(getDeleteCropEvent(crop)); - hBox.getChildren().addAll(label, details, delete); + hBox.getChildren().addAll(imageView, label, details, delete); return hBox; } @@ -100,7 +122,7 @@ public class MyPlantsController implements Initializable { stage.initModality(Modality.APPLICATION_MODAL); stage.setResizable(true); stage.showAndWait(); - } catch (IOException e) { + } catch (IOException | HardinessZoneNotSetException e) { e.printStackTrace(); } } @@ -114,7 +136,7 @@ public class MyPlantsController implements Initializable { public void handle(ActionEvent event) { try { showConfirmation(crop); - } catch (IOException e) { + } catch (IOException | HardinessZoneNotSetException e) { e.printStackTrace(); } } @@ -122,7 +144,7 @@ public class MyPlantsController implements Initializable { return event; } - private void showConfirmation(Crop crop) throws IOException { + private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Delete Crop"); alert.setHeaderText("Are you sure want to delete this Crop?"); @@ -131,10 +153,8 @@ public class MyPlantsController implements Initializable { Optional option = alert.showAndWait(); if (option.get() == ButtonType.OK) { - //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed. - //gardenplanmodel.removeCrop(crop); + gardenplanmodel.removeCrop(crop); loadCropList(); - System.out.println("Delete Crop"); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java index 1e534cb..a73052c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -1,6 +1,8 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.plantList.PlantListModel; import ch.zhaw.gartenverwaltung.taskList.TaskListModel; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; @@ -28,6 +30,7 @@ public class MyScheduleController implements Initializable { private Crop selectedCrop = null; private final TaskListModel taskListModel = new TaskListModel(); private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final PlantListModel plantListModel = new PlantListModel(); private final ListProperty cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); @@ -84,7 +87,6 @@ public class MyScheduleController implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { - //ToDo method to load crop list List cropList; try { cropList = gardenplanmodel.getCrops(); @@ -138,7 +140,11 @@ public class MyScheduleController implements Initializable { if (empty || crop == null) { setText(null); } else { - setText("placeholder"); + try { + setText(plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0).name()); + } catch (HardinessZoneNotSetException | IOException e) { + e.printStackTrace(); + } } } }); @@ -146,11 +152,10 @@ public class MyScheduleController implements Initializable { private void loadTaskList() throws IOException { List> taskLists = new LinkedList<>(); - //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed. if (selectedCrop != null) { - //taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); + taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); } else { - //taskLists = taskListModel.getTasksUpcomingWeek(); + taskLists = taskListModel.getTasksUpcomingWeek(); } if (!taskLists.isEmpty()) { viewTaskListOfDay(day1_pane, taskLists.get(0)); @@ -167,7 +172,7 @@ public class MyScheduleController implements Initializable { //ToDo update pane with task list VBox vBox = new VBox(); for (Task task : tasks) { - Label label = new Label("placeholder"); + Label label = new Label(task.getDescription()); vBox.getChildren().add(label); } pane.getChildren().add(vBox); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index 9242697..c5445e1 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -63,8 +63,7 @@ public class SelectSowDayController implements Initializable { //ToDo method to get current lifecycle group in plant sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); } - //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed. - //gardenplanmodel.plantAsCrop(selectedPlant, sowDate); + gardenplanmodel.plantAsCrop(selectedPlant, sowDate); closeWindow(); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index 02d6480..16de6c3 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -21,11 +21,10 @@ public class Task { * default constructor * (used by Json deserializer) */ - public Task(long cropId){ + public Task(){ name= ""; description= ""; startDate = LocalDate.now(); - this.cropId = cropId; } public Task(String name, String description, LocalDate startDate, long cropId) { diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml index e6aa591..ed3a476 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml @@ -77,7 +77,7 @@ - + - +