diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java new file mode 100644 index 0000000..6e5d792 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -0,0 +1,132 @@ +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 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; + + @FXML + private Label area_label; + + @FXML + private Label cropName_label; + + @FXML + private Label description_label; + + @FXML + private VBox growthPhases_vbox; + + @FXML + private Label location_label; + + @FXML + private Label light_label; + + @FXML + private Button location_button; + + @FXML + private VBox pests_vbox; + + @FXML + private Label soil_label; + + @FXML + private Label spacing_label; + + public CropDetailController() throws IOException { + } + + @FXML + void editTaskList(ActionEvent event) { + + } + + @FXML + void goBack(ActionEvent event) { + Stage stage = (Stage) imageView.getScene().getWindow(); + stage.close(); + } + + @FXML + void setArea(ActionEvent event) { + + } + + @FXML + void setLocation(ActionEvent event) { + + } + + public void setPlantFromCrop(Crop crop) throws HardinessZoneNotSetException, IOException { + this.crop = crop; + 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 e223e8b..3d9d82c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyPlantsController.java @@ -1,27 +1,45 @@ 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; +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.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; import java.io.IOException; import java.net.URL; -import java.util.LinkedList; -import java.util.List; -import java.util.ResourceBundle; +import java.util.*; public class MyPlantsController implements Initializable { MainFXMLController mainController; - - @FXML - private Button addPlant_button; + private final TaskListModel taskListModel = new TaskListModel(); + private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); + private final PlantListModel plantListModel = new PlantListModel(); @FXML private VBox myPlants_vbox; + public MyPlantsController() throws IOException { + } + @FXML void addPlant(ActionEvent event) throws IOException { mainController.loadPane("Plants.fxml"); @@ -29,15 +47,29 @@ public class MyPlantsController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - //ToDo - List myPlants = getMyPlants(); - createPlantView(myPlants); + //ToDo update, when new crops are added + try { + loadCropList(); + } catch (HardinessZoneNotSetException | IOException e) { + e.printStackTrace(); + } } - private void createPlantView(List myPlants) { - //ToDo - for(Plant plant : myPlants) { - createPlantView(); + private void loadCropList() throws HardinessZoneNotSetException, IOException { + List cropList = new LinkedList<>(); + try { + cropList = getCropList(); + } catch (IOException e) { + e.printStackTrace(); + } + createPlantView(cropList); + } + + private void createPlantView(List crops) throws HardinessZoneNotSetException, IOException { + myPlants_vbox.getChildren().clear(); + for(Crop crop : crops) { + HBox hBox = createPlantView(crop); + myPlants_vbox.getChildren().add(hBox); } } @@ -45,14 +77,84 @@ public class MyPlantsController implements Initializable { mainController = controller; } - private List getMyPlants() { - //ToDo method to get myPlantList(scheduled) - //Method to call all Plants saved - List myPlantList = new LinkedList<>(); - return myPlantList; + private List getCropList() throws IOException { + List cropList; + cropList = gardenplanmodel.getCrops(); + return cropList; } - private void createPlantView() { - //ToDo FXML Panel with Plant data + 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(imageView, label, details, delete); + return hBox; + } + + private EventHandler getGoToCropDetailEvent(Crop crop) { + EventHandler event = new EventHandler() { + @Override + public void handle(ActionEvent event) { + Parent root; + FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("CropDetail.fxml"))); + try { + 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 | HardinessZoneNotSetException e) { + e.printStackTrace(); + } + } + }; + return event; + } + + private EventHandler getDeleteCropEvent(Crop crop) { + EventHandler event = new EventHandler() { + @Override + public void handle(ActionEvent event) { + try { + showConfirmation(crop); + } catch (IOException | HardinessZoneNotSetException e) { + e.printStackTrace(); + } + } + }; + return event; + } + + 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?"); + alert.setContentText("placeholder"); + + Optional option = alert.showAndWait(); + + if (option.get() == ButtonType.OK) { + gardenplanmodel.removeCrop(crop); + loadCropList(); + } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java index b16445a..a73052c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyScheduleController.java @@ -1,15 +1,25 @@ package ch.zhaw.gartenverwaltung; -import ch.zhaw.gartenverwaltung.types.Plant; +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; +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.scene.control.ListCell; 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; @@ -17,7 +27,12 @@ import java.util.List; import java.util.ResourceBundle; public class MyScheduleController implements Initializable { - private Plant selectedPlant = null; + 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()); @FXML private Label day1_label; @@ -65,27 +80,41 @@ public class MyScheduleController implements Initializable { private Label information_label; @FXML - private ListView scheduledPlants_listview; + private ListView scheduledPlants_listview; + + public MyScheduleController() throws IOException { + } @Override public void initialize(URL location, ResourceBundle resources) { - List plantList = new LinkedList<>(); - fillListViewMyPlantsInSchedule(plantList); - getSelectedPlantTask(); + 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(""); + try { + loadTaskList(); + } catch (IOException e) { + e.printStackTrace(); + } } - private void getSelectedPlantTask() { - scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { + private void lookForSelectedListEntries() { + scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @Override - public void changed(ObservableValue observable, Plant oldValue, Plant newValue) { - if(newValue != null) { - selectedPlant = newValue; - //ToDo update day_panel with task for the day - } else { - selectedPlant = null; - //ToDo update day_panel with task for the day (all plants) + public void changed(ObservableValue observable, Crop oldValue, Crop newValue) { + selectedCrop = newValue; + try { + loadTaskList(); + } catch (IOException e) { + e.printStackTrace(); } } }); @@ -102,23 +131,52 @@ public class MyScheduleController implements Initializable { day7_label.setText(today.plusDays(6).getDayOfWeek().toString()); } - private void fillListViewMyPlantsInSchedule(List list) { - for (Plant plant : list) { - scheduledPlants_listview.getItems().add(plant); - } - scheduledPlants_listview.setCellFactory(param -> new ListCell() { + private void setCellFactoryListView() { + scheduledPlants_listview.setCellFactory(param -> new ListCell() { @Override - protected void updateItem(Plant plant, boolean empty) { - super.updateItem(plant, empty); + protected void updateItem(Crop crop, boolean empty) { + super.updateItem(crop, empty); - if (empty || plant == null || plant.name() == null) { + if (empty || crop == null) { setText(null); } else { - setText(plant.name()); + try { + setText(plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0).name()); + } catch (HardinessZoneNotSetException | IOException e) { + e.printStackTrace(); + } } } }); } + private void loadTaskList() throws IOException { + List> taskLists = new LinkedList<>(); + if (selectedCrop != null) { + taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); + } else { + taskLists = taskListModel.getTasksUpcomingWeek(); + } + if (!taskLists.isEmpty()) { + viewTaskListOfDay(day1_pane, taskLists.get(0)); + viewTaskListOfDay(day2_pane, taskLists.get(1)); + viewTaskListOfDay(day3_pane, taskLists.get(2)); + viewTaskListOfDay(day4_pane, taskLists.get(3)); + viewTaskListOfDay(day5_pane, taskLists.get(4)); + viewTaskListOfDay(day6_pane, taskLists.get(5)); + viewTaskListOfDay(day7_pane, taskLists.get(6)); + } + } + + private void viewTaskListOfDay(Pane pane, List tasks) { + //ToDo update pane with task list + VBox vBox = new VBox(); + for (Task task : tasks) { + 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 2537e08..c5445e1 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,7 @@ 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); + 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/java/ch/zhaw/gartenverwaltung/types/Task.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java index aef9035..16de6c3 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Task.java @@ -25,7 +25,6 @@ public class 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 new file mode 100644 index 0000000..ed3a476 --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/CropDetail.fxml @@ -0,0 +1,142 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +