connection task list model and garden plan model with controllers

This commit is contained in:
giavaphi 2022-11-08 21:03:01 +01:00
parent 802f238d69
commit ced2645bd7
6 changed files with 104 additions and 35 deletions

View File

@ -1,5 +1,7 @@
package ch.zhaw.gartenverwaltung; package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
@ -7,6 +9,8 @@ import javafx.scene.control.Label;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
public class CropDetailController { public class CropDetailController {
private Crop crop = null;
private Plant plant = null;
@FXML @FXML
private Button area_button; private Button area_button;
@ -21,10 +25,10 @@ public class CropDetailController {
private Label description_label; private Label description_label;
@FXML @FXML
private VBox growthPahses_vbox; private VBox growthPhases_vbox;
@FXML @FXML
private Label lacation_label; private Label location_label;
@FXML @FXML
private Label light_label; private Label light_label;
@ -61,4 +65,8 @@ public class CropDetailController {
} }
public void setPlantFromCrop(Crop crop) {
this.crop = crop;
this.plant = null;
}
} }

View File

@ -1,30 +1,39 @@
package ch.zhaw.gartenverwaltung; 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.Crop;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert; 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.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.LinkedList; import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
public class MyPlantsController implements Initializable { public class MyPlantsController implements Initializable {
MainFXMLController mainController; MainFXMLController mainController;
private final TaskListModel taskListModel = new TaskListModel();
private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
@FXML @FXML
private VBox myPlants_vbox; private VBox myPlants_vbox;
public MyPlantsController() throws IOException {
}
@FXML @FXML
void addPlant(ActionEvent event) throws IOException { void addPlant(ActionEvent event) throws IOException {
mainController.loadPane("Plants.fxml"); mainController.loadPane("Plants.fxml");
@ -32,12 +41,21 @@ public class MyPlantsController implements Initializable {
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
//ToDo loadCropList();
List<Crop> cropList = getCropList(); }
private void loadCropList() {
List<Crop> cropList = new LinkedList<>();
try {
cropList = getCropList();
} catch (IOException e) {
e.printStackTrace();
}
createPlantView(cropList); createPlantView(cropList);
} }
private void createPlantView(List<Crop> crops) { private void createPlantView(List<Crop> crops) {
myPlants_vbox.getChildren().clear();
for(Crop crop : crops) { for(Crop crop : crops) {
HBox hBox = createPlantView(crop); HBox hBox = createPlantView(crop);
myPlants_vbox.getChildren().add(hBox); myPlants_vbox.getChildren().add(hBox);
@ -48,15 +66,15 @@ public class MyPlantsController implements Initializable {
mainController = controller; mainController = controller;
} }
private List<Crop> getCropList() { private List<Crop> getCropList() throws IOException {
//ToDo method to get cropList List<Crop> cropList;
//Method to get Crop List cropList = gardenplanmodel.getCrops();
List<Crop> cropList = new LinkedList<>();
return cropList; return cropList;
} }
private HBox createPlantView(Crop crop) { private HBox createPlantView(Crop crop) {
//ToDo add dynamic design //ToDo add good design
//ToDo get plant information from crop
HBox hBox = new HBox(); HBox hBox = new HBox();
Label label = new Label("Placeholder"); Label label = new Label("Placeholder");
Button details = new Button("Details"); Button details = new Button("Details");
@ -71,9 +89,17 @@ public class MyPlantsController implements Initializable {
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() { EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
//ToDo uncomment when new FXML exists Parent root;
FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("CropDetail.fxml")));
try { 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -86,13 +112,17 @@ public class MyPlantsController implements Initializable {
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() { EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
showConfirmation(crop); try {
showConfirmation(crop);
} catch (IOException e) {
e.printStackTrace();
}
} }
}; };
return event; return event;
} }
private void showConfirmation(Crop crop) { private void showConfirmation(Crop crop) throws IOException {
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?");
@ -101,7 +131,10 @@ 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 Method to remove crop //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed.
//gardenplanmodel.removeCrop(crop);
loadCropList();
System.out.println("Delete Crop");
} }
} }
} }

View File

@ -1,5 +1,7 @@
package ch.zhaw.gartenverwaltung; 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.Crop;
import ch.zhaw.gartenverwaltung.types.Task; import ch.zhaw.gartenverwaltung.types.Task;
import javafx.beans.property.ListProperty; import javafx.beans.property.ListProperty;
@ -15,6 +17,7 @@ import javafx.scene.control.ListView;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.LinkedList; import java.util.LinkedList;
@ -23,6 +26,8 @@ import java.util.ResourceBundle;
public class MyScheduleController implements Initializable { public class MyScheduleController implements Initializable {
private Crop selectedCrop = null; private Crop selectedCrop = null;
private final TaskListModel taskListModel = new TaskListModel();
private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@ -74,16 +79,29 @@ public class MyScheduleController implements Initializable {
@FXML @FXML
private ListView<Crop> scheduledPlants_listview; private ListView<Crop> scheduledPlants_listview;
public MyScheduleController() throws IOException {
}
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
//ToDo method to load crop list //ToDo method to load crop list
List<Crop> cropList = new LinkedList<>(); List<Crop> cropList;
try {
cropList = gardenplanmodel.getCrops();
cropListProperty.addAll(cropList);
} catch (IOException e) {
e.printStackTrace();
}
setCellFactoryListView(); setCellFactoryListView();
scheduledPlants_listview.itemsProperty().bind(cropListProperty); scheduledPlants_listview.itemsProperty().bind(cropListProperty);
lookForSelectedListEntries(); lookForSelectedListEntries();
setDayLabels(); setDayLabels();
information_label.setText(""); information_label.setText("");
loadTaskList(); try {
loadTaskList();
} catch (IOException e) {
e.printStackTrace();
}
} }
private void lookForSelectedListEntries() { private void lookForSelectedListEntries() {
@ -91,7 +109,11 @@ public class MyScheduleController implements Initializable {
@Override @Override
public void changed(ObservableValue<? extends Crop> observable, Crop oldValue, Crop newValue) { public void changed(ObservableValue<? extends Crop> observable, Crop oldValue, Crop newValue) {
selectedCrop = newValue; selectedCrop = newValue;
loadTaskList(); try {
loadTaskList();
} catch (IOException e) {
e.printStackTrace();
}
} }
}); });
} }
@ -122,13 +144,13 @@ public class MyScheduleController implements Initializable {
}); });
} }
private void loadTaskList() { private void loadTaskList() throws IOException {
//ToDo load task list according to selectedCrop
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());
} else { } else {
//taskLists = taskListModel.getTasksUpcomingWeek();
} }
if (!taskLists.isEmpty()) { if (!taskLists.isEmpty()) {
viewTaskListOfDay(day1_pane, taskLists.get(0)); viewTaskListOfDay(day1_pane, taskLists.get(0));

View File

@ -1,5 +1,9 @@
package ch.zhaw.gartenverwaltung; 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.GrowthPhaseType;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
@ -11,6 +15,7 @@ import javafx.scene.control.*;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Callback; import javafx.util.Callback;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
@ -18,6 +23,8 @@ import java.util.ResourceBundle;
public class SelectSowDayController implements Initializable { public class SelectSowDayController implements Initializable {
private Plant selectedPlant = null; private Plant selectedPlant = null;
private final TaskListModel taskListModel = new TaskListModel();
private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
@FXML @FXML
private DatePicker datepicker; private DatePicker datepicker;
@ -31,6 +38,8 @@ public class SelectSowDayController implements Initializable {
@FXML @FXML
private RadioButton sow_radio; private RadioButton sow_radio;
public SelectSowDayController() throws IOException {}
/** /**
* close the date selector window * close the date selector window
* @param event event * @param event event
@ -46,8 +55,7 @@ public class SelectSowDayController implements Initializable {
* @param event event * @param event event
*/ */
@FXML @FXML
void save(ActionEvent event) { void save(ActionEvent event) throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
//ToDo save plant and sow date to crop/gardenplan model
LocalDate sowDate; LocalDate sowDate;
if (sow_radio.isSelected()) { if (sow_radio.isSelected()) {
sowDate = datepicker.getValue(); sowDate = datepicker.getValue();
@ -55,8 +63,8 @@ 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);
} }
System.out.println(sowDate); //ToDo uncomment, when error from JsonTaskDatabase.java loadTaskListFromFile() is fixed.
System.out.println(selectedPlant); //gardenplanmodel.plantAsCrop(selectedPlant, sowDate);
closeWindow(); closeWindow();
} }

View File

@ -1,10 +1,7 @@
package ch.zhaw.gartenverwaltung.taskList; package ch.zhaw.gartenverwaltung.taskList;
import ch.zhaw.gartenverwaltung.Config; import ch.zhaw.gartenverwaltung.Config;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.*;
import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase;
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
import ch.zhaw.gartenverwaltung.io.TaskDatabase;
import ch.zhaw.gartenverwaltung.types.*; import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException; import java.io.IOException;
@ -26,6 +23,7 @@ public class TaskListModel {
public TaskListModel(){ public TaskListModel(){
taskDatabase = new JsonTaskDatabase(); taskDatabase = new JsonTaskDatabase();
plantDatabase = new JsonPlantDatabase();
} }
/** /**

View File

@ -85,7 +85,7 @@
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</VBox.margin> </VBox.margin>
</Label> </Label>
<VBox fx:id="growthPahses_vbox" prefHeight="135.0" prefWidth="879.0"> <VBox fx:id="growthPhases_vbox" prefHeight="135.0" prefWidth="879.0">
<VBox.margin> <VBox.margin>
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</VBox.margin> </VBox.margin>
@ -126,7 +126,7 @@
<Insets right="40.0" /> <Insets right="40.0" />
</HBox.margin> </HBox.margin>
</Label> </Label>
<Label fx:id="lacation_label" text="Label"> <Label fx:id="location_label" text="Label">
<HBox.margin> <HBox.margin>
<Insets right="10.0" /> <Insets right="10.0" />
</HBox.margin> </HBox.margin>