From ced2645bd7fde87c3d6da75de525b75bdc92af69 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Tue, 8 Nov 2022 21:03:01 +0100 Subject: [PATCH] connection task list model and garden plan model with controllers --- .../CropDetailController.java | 12 +++- .../gartenverwaltung/MyPlantsController.java | 65 ++++++++++++++----- .../MyScheduleController.java | 36 ++++++++-- .../SelectSowDayController.java | 16 +++-- .../taskList/TaskListModel.java | 6 +- .../ch/zhaw/gartenverwaltung/CropDetail.fxml | 4 +- 6 files changed, 104 insertions(+), 35 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index e34f743..5e34031 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -1,5 +1,7 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.Plant; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -7,6 +9,8 @@ import javafx.scene.control.Label; import javafx.scene.layout.VBox; public class CropDetailController { + private Crop crop = null; + private Plant plant = null; @FXML private Button area_button; @@ -21,10 +25,10 @@ public class CropDetailController { private Label description_label; @FXML - private VBox growthPahses_vbox; + private VBox growthPhases_vbox; @FXML - private Label lacation_label; + private Label location_label; @FXML private Label light_label; @@ -61,4 +65,8 @@ public class CropDetailController { } + public void setPlantFromCrop(Crop crop) { + this.crop = crop; + this.plant = null; + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java index 9bbd3a8..b0eb1d2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java @@ -1,30 +1,39 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.taskList.TaskListModel; import ch.zhaw.gartenverwaltung.types.Crop; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; +import javafx.stage.Modality; +import javafx.stage.Stage; import java.io.IOException; import java.net.URL; -import java.util.LinkedList; -import java.util.List; -import java.util.Optional; -import java.util.ResourceBundle; +import java.util.*; public class MyPlantsController implements Initializable { MainFXMLController mainController; + private final TaskListModel taskListModel = new TaskListModel(); + private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); @FXML private VBox myPlants_vbox; + public MyPlantsController() throws IOException { + } + @FXML void addPlant(ActionEvent event) throws IOException { mainController.loadPane("Plants.fxml"); @@ -32,12 +41,21 @@ public class MyPlantsController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - //ToDo - List cropList = getCropList(); + loadCropList(); + } + + private void loadCropList() { + List cropList = new LinkedList<>(); + try { + cropList = getCropList(); + } catch (IOException e) { + e.printStackTrace(); + } createPlantView(cropList); } private void createPlantView(List crops) { + myPlants_vbox.getChildren().clear(); for(Crop crop : crops) { HBox hBox = createPlantView(crop); myPlants_vbox.getChildren().add(hBox); @@ -48,15 +66,15 @@ public class MyPlantsController implements Initializable { mainController = controller; } - private List getCropList() { - //ToDo method to get cropList - //Method to get Crop List - List cropList = new LinkedList<>(); + private List getCropList() throws IOException { + List cropList; + cropList = gardenplanmodel.getCrops(); return cropList; } private HBox createPlantView(Crop crop) { - //ToDo add dynamic design + //ToDo add good design + //ToDo get plant information from crop HBox hBox = new HBox(); Label label = new Label("Placeholder"); Button details = new Button("Details"); @@ -71,9 +89,17 @@ public class MyPlantsController implements Initializable { EventHandler event = new EventHandler() { @Override public void handle(ActionEvent event) { - //ToDo uncomment when new FXML exists + Parent root; + FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("CropDetail.fxml"))); try { - mainController.loadPane("CropDetail.fxml"); + root = fxmlLoader.load(); + CropDetailController controller = fxmlLoader.getController(); + controller.setPlantFromCrop(crop); + Stage stage = new Stage(); + stage.setScene(new Scene(root)); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setResizable(true); + stage.showAndWait(); } catch (IOException e) { e.printStackTrace(); } @@ -86,13 +112,17 @@ public class MyPlantsController implements Initializable { EventHandler event = new EventHandler() { @Override public void handle(ActionEvent event) { - showConfirmation(crop); + try { + showConfirmation(crop); + } catch (IOException e) { + e.printStackTrace(); + } } }; return event; } - private void showConfirmation(Crop crop) { + private void showConfirmation(Crop crop) throws IOException { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Delete Crop"); alert.setHeaderText("Are you sure want to delete this Crop?"); @@ -101,7 +131,10 @@ public class MyPlantsController implements Initializable { Optional option = alert.showAndWait(); if (option.get() == ButtonType.OK) { - //ToDo Method to remove crop + //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed. + //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 173c0ec..1e534cb 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -1,5 +1,7 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.taskList.TaskListModel; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; import javafx.beans.property.ListProperty; @@ -15,6 +17,7 @@ import javafx.scene.control.ListView; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; +import java.io.IOException; import java.net.URL; import java.time.LocalDate; import java.util.LinkedList; @@ -23,6 +26,8 @@ import java.util.ResourceBundle; public class MyScheduleController implements Initializable { private Crop selectedCrop = null; + private final TaskListModel taskListModel = new TaskListModel(); + private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); private final ListProperty cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); @@ -74,16 +79,29 @@ public class MyScheduleController implements Initializable { @FXML private ListView scheduledPlants_listview; + public MyScheduleController() throws IOException { + } + @Override public void initialize(URL location, ResourceBundle resources) { //ToDo method to load crop list - List cropList = new LinkedList<>(); + List cropList; + try { + cropList = gardenplanmodel.getCrops(); + cropListProperty.addAll(cropList); + } catch (IOException e) { + e.printStackTrace(); + } setCellFactoryListView(); scheduledPlants_listview.itemsProperty().bind(cropListProperty); lookForSelectedListEntries(); setDayLabels(); information_label.setText(""); - loadTaskList(); + try { + loadTaskList(); + } catch (IOException e) { + e.printStackTrace(); + } } private void lookForSelectedListEntries() { @@ -91,7 +109,11 @@ public class MyScheduleController implements Initializable { @Override public void changed(ObservableValue observable, Crop oldValue, Crop newValue) { selectedCrop = newValue; - loadTaskList(); + try { + loadTaskList(); + } catch (IOException e) { + e.printStackTrace(); + } } }); } @@ -122,13 +144,13 @@ public class MyScheduleController implements Initializable { }); } - private void loadTaskList() { - //ToDo load task list according to selectedCrop + 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()); } else { - + //taskLists = taskListModel.getTasksUpcomingWeek(); } if (!taskLists.isEmpty()) { viewTaskListOfDay(day1_pane, taskLists.get(0)); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index 2537e08..9242697 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -1,5 +1,9 @@ package ch.zhaw.gartenverwaltung; +import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.taskList.TaskListModel; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.Plant; import javafx.beans.value.ChangeListener; @@ -11,6 +15,7 @@ import javafx.scene.control.*; import javafx.stage.Stage; import javafx.util.Callback; +import java.io.IOException; import java.net.URL; import java.time.LocalDate; import java.util.List; @@ -18,6 +23,8 @@ import java.util.ResourceBundle; public class SelectSowDayController implements Initializable { private Plant selectedPlant = null; + private final TaskListModel taskListModel = new TaskListModel(); + private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); @FXML private DatePicker datepicker; @@ -31,6 +38,8 @@ public class SelectSowDayController implements Initializable { @FXML private RadioButton sow_radio; + public SelectSowDayController() throws IOException {} + /** * close the date selector window * @param event event @@ -46,8 +55,7 @@ public class SelectSowDayController implements Initializable { * @param event event */ @FXML - void save(ActionEvent event) { - //ToDo save plant and sow date to crop/gardenplan model + void save(ActionEvent event) throws HardinessZoneNotSetException, IOException, PlantNotFoundException { LocalDate sowDate; if (sow_radio.isSelected()) { sowDate = datepicker.getValue(); @@ -55,8 +63,8 @@ public class SelectSowDayController implements Initializable { //ToDo method to get current lifecycle group in plant sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); } - System.out.println(sowDate); - System.out.println(selectedPlant); + //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed. + //gardenplanmodel.plantAsCrop(selectedPlant, sowDate); closeWindow(); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index cb766be..af0ca00 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -1,10 +1,7 @@ package ch.zhaw.gartenverwaltung.taskList; import ch.zhaw.gartenverwaltung.Config; -import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase; -import ch.zhaw.gartenverwaltung.io.PlantDatabase; -import ch.zhaw.gartenverwaltung.io.TaskDatabase; +import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.types.*; import java.io.IOException; @@ -26,6 +23,7 @@ public class TaskListModel { public TaskListModel(){ taskDatabase = new JsonTaskDatabase(); + plantDatabase = new JsonPlantDatabase(); } /** diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml index c0cd65a..e6aa591 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml @@ -85,7 +85,7 @@ - + @@ -126,7 +126,7 @@ -