display List of Crops, get detail of crop and display taskList

This commit is contained in:
giavaphi 2022-11-10 22:50:49 +01:00
parent 0e40bc6304
commit 8e23124c6b
7 changed files with 115 additions and 34 deletions

View File

@ -1,16 +1,34 @@
package ch.zhaw.gartenverwaltung; 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.Crop;
import ch.zhaw.gartenverwaltung.types.Pest;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Task;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.List;
public class CropDetailController { public class CropDetailController {
private Crop crop = null; 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 @FXML
private Button area_button; private Button area_button;
@ -45,6 +63,9 @@ public class CropDetailController {
@FXML @FXML
private Label spacing_label; private Label spacing_label;
public CropDetailController() throws IOException {
}
@FXML @FXML
void editTaskList(ActionEvent event) { void editTaskList(ActionEvent event) {
@ -52,7 +73,8 @@ public class CropDetailController {
@FXML @FXML
void goBack(ActionEvent event) { void goBack(ActionEvent event) {
Stage stage = (Stage) imageView.getScene().getWindow();
stage.close();
} }
@FXML @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.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<Task> 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<Pest> 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);
}
} }
} }

View File

@ -1,8 +1,11 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; 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.taskList.TaskListModel;
import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -14,7 +17,9 @@ import javafx.scene.control.Alert;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ButtonType; import javafx.scene.control.ButtonType;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Modality; import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -27,6 +32,7 @@ public class MyPlantsController implements Initializable {
MainFXMLController mainController; MainFXMLController mainController;
private final TaskListModel taskListModel = new TaskListModel(); private final TaskListModel taskListModel = new TaskListModel();
private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
private final PlantListModel plantListModel = new PlantListModel();
@FXML @FXML
private VBox myPlants_vbox; private VBox myPlants_vbox;
@ -41,10 +47,15 @@ public class MyPlantsController implements Initializable {
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
//ToDo update, when new crops are added
try {
loadCropList(); loadCropList();
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
}
} }
private void loadCropList() { private void loadCropList() throws HardinessZoneNotSetException, IOException {
List<Crop> cropList = new LinkedList<>(); List<Crop> cropList = new LinkedList<>();
try { try {
cropList = getCropList(); cropList = getCropList();
@ -54,7 +65,7 @@ public class MyPlantsController implements Initializable {
createPlantView(cropList); createPlantView(cropList);
} }
private void createPlantView(List<Crop> crops) { private void createPlantView(List<Crop> crops) throws HardinessZoneNotSetException, IOException {
myPlants_vbox.getChildren().clear(); myPlants_vbox.getChildren().clear();
for(Crop crop : crops) { for(Crop crop : crops) {
HBox hBox = createPlantView(crop); HBox hBox = createPlantView(crop);
@ -72,16 +83,27 @@ public class MyPlantsController implements Initializable {
return cropList; return cropList;
} }
private HBox createPlantView(Crop crop) { private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException {
//ToDo add good design //ToDo add better design
//ToDo get plant information from crop Plant plant = plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0);
HBox hBox = new HBox(); HBox hBox = new HBox(10);
Label label = new Label("Placeholder"); 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 details = new Button("Details");
Button delete = new Button("delete"); Button delete = new Button("delete");
details.setOnAction(getGoToCropDetailEvent(crop)); details.setOnAction(getGoToCropDetailEvent(crop));
delete.setOnAction(getDeleteCropEvent(crop)); delete.setOnAction(getDeleteCropEvent(crop));
hBox.getChildren().addAll(label, details, delete); hBox.getChildren().addAll(imageView, label, details, delete);
return hBox; return hBox;
} }
@ -100,7 +122,7 @@ public class MyPlantsController implements Initializable {
stage.initModality(Modality.APPLICATION_MODAL); stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(true); stage.setResizable(true);
stage.showAndWait(); stage.showAndWait();
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -114,7 +136,7 @@ public class MyPlantsController implements Initializable {
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
try { try {
showConfirmation(crop); showConfirmation(crop);
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -122,7 +144,7 @@ public class MyPlantsController implements Initializable {
return event; 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 alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Delete Crop"); alert.setTitle("Delete Crop");
alert.setHeaderText("Are you sure want to delete this Crop?"); alert.setHeaderText("Are you sure want to delete this Crop?");
@ -131,10 +153,8 @@ public class MyPlantsController implements Initializable {
Optional<ButtonType> option = alert.showAndWait(); Optional<ButtonType> option = alert.showAndWait();
if (option.get() == ButtonType.OK) { if (option.get() == ButtonType.OK) {
//ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed. gardenplanmodel.removeCrop(crop);
//gardenplanmodel.removeCrop(crop);
loadCropList(); loadCropList();
System.out.println("Delete Crop");
} }
} }
} }

View File

@ -1,6 +1,8 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel; 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.taskList.TaskListModel;
import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Task; import ch.zhaw.gartenverwaltung.types.Task;
@ -28,6 +30,7 @@ public class MyScheduleController implements Initializable {
private Crop selectedCrop = null; private Crop selectedCrop = null;
private final TaskListModel taskListModel = new TaskListModel(); private final TaskListModel taskListModel = new TaskListModel();
private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel); private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
private final PlantListModel plantListModel = new PlantListModel();
private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@ -84,7 +87,6 @@ public class MyScheduleController implements Initializable {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
//ToDo method to load crop list
List<Crop> cropList; List<Crop> cropList;
try { try {
cropList = gardenplanmodel.getCrops(); cropList = gardenplanmodel.getCrops();
@ -138,7 +140,11 @@ public class MyScheduleController implements Initializable {
if (empty || crop == null) { if (empty || crop == null) {
setText(null); setText(null);
} else { } 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 { private void loadTaskList() throws IOException {
List<List<Task>> taskLists = new LinkedList<>(); List<List<Task>> taskLists = new LinkedList<>();
//ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed.
if (selectedCrop != null) { if (selectedCrop != null) {
//taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get()); taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get());
} else { } else {
//taskLists = taskListModel.getTasksUpcomingWeek(); taskLists = taskListModel.getTasksUpcomingWeek();
} }
if (!taskLists.isEmpty()) { if (!taskLists.isEmpty()) {
viewTaskListOfDay(day1_pane, taskLists.get(0)); viewTaskListOfDay(day1_pane, taskLists.get(0));
@ -167,7 +172,7 @@ public class MyScheduleController implements Initializable {
//ToDo update pane with task list //ToDo update pane with task list
VBox vBox = new VBox(); VBox vBox = new VBox();
for (Task task : tasks) { for (Task task : tasks) {
Label label = new Label("placeholder"); Label label = new Label(task.getDescription());
vBox.getChildren().add(label); vBox.getChildren().add(label);
} }
pane.getChildren().add(vBox); pane.getChildren().add(vBox);

View File

@ -63,8 +63,7 @@ public class SelectSowDayController implements Initializable {
//ToDo method to get current lifecycle group in plant //ToDo method to get current lifecycle group in plant
sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); 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(); closeWindow();
} }

View File

@ -21,11 +21,10 @@ public class Task {
* default constructor * default constructor
* (used by Json deserializer) * (used by Json deserializer)
*/ */
public Task(long cropId){ public Task(){
name= ""; name= "";
description= ""; description= "";
startDate = LocalDate.now(); startDate = LocalDate.now();
this.cropId = cropId;
} }
public Task(String name, String description, LocalDate startDate, long cropId) { public Task(String name, String description, LocalDate startDate, long cropId) {

View File

@ -77,7 +77,7 @@
</Label> </Label>
</children> </children>
</GridPane> </GridPane>
<ImageView fitHeight="300.0" fitWidth="300.0" pickOnBounds="true" preserveRatio="true" HBox.hgrow="NEVER" /> <ImageView fx:id="imageView" fitHeight="300.0" fitWidth="300.0" pickOnBounds="true" preserveRatio="true" HBox.hgrow="NEVER" />
</children> </children>
</HBox> </HBox>
<Label text="Growth Phases:"> <Label text="Growth Phases:">

View File

@ -7,9 +7,7 @@
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="655.0" prefWidth="1175.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.gartenverwaltung.MyPlantsController">
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="655.0" prefWidth="1175.0" xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.gartenverwaltung.MyPlantsController">
<children> <children>
<VBox layoutY="49.0" prefHeight="655.0" prefWidth="1175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <VBox layoutY="49.0" prefHeight="655.0" prefWidth="1175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
@ -21,7 +19,7 @@
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</VBox.margin> </VBox.margin>
</Label> </Label>
<VBox fx:id="myPlants_vbox" prefHeight="200.0" prefWidth="100.0" VBox.vgrow="ALWAYS" /> <VBox fx:id="myPlants_vbox" maxWidth="1.7976931348623157E308" prefHeight="200.0" prefWidth="100.0" VBox.vgrow="ALWAYS" />
<Button fx:id="addPlant_button" mnemonicParsing="false" onAction="#addPlant" prefHeight="45.0" prefWidth="155.0" text="Add new Plant"> <Button fx:id="addPlant_button" mnemonicParsing="false" onAction="#addPlant" prefHeight="45.0" prefWidth="155.0" text="Add new Plant">
<VBox.margin> <VBox.margin>
<Insets top="10.0" /> <Insets top="10.0" />