diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java index b002e00..3568b50 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java @@ -129,7 +129,7 @@ public class MyGardenController { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Delete Crop"); alert.setHeaderText("Are you sure want to delete this Crop?"); - alert.setContentText("placeholder"); + alert.setContentText("Deleting this crop will remove all associated tasks from your schedule."); alert.showAndWait() .ifPresent(buttonType -> { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index 0b66618..200ccf9 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -5,7 +5,9 @@ import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.bootstrap.ChangeViewEvent; import ch.zhaw.gartenverwaltung.bootstrap.Inject; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.PlantListModel; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Seasons; @@ -19,10 +21,10 @@ import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; -import javafx.stage.Modality; import javafx.stage.Stage; import java.io.IOException; +import java.time.LocalDate; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,6 +36,8 @@ public class PlantsController { private PlantListModel plantListModel; @Inject private AppLoader appLoader; + @Inject + private Garden garden; private Plant selectedPlant = null; private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A; @@ -71,15 +75,32 @@ public class PlantsController { @FXML void selectSowDate() throws IOException { Stage stage = new Stage(); - if (appLoader.loadSceneToStage("SelectSowDay.fxml", stage) instanceof SelectSowDayController controller) { - controller.setSelectedPlant(selectedPlant); - } + Dialog dateSelection = new Dialog<>(); + dateSelection.setTitle("Select Date"); + dateSelection.setHeaderText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name())); + dateSelection.setResizable(false); - stage.initModality(Modality.APPLICATION_MODAL); - stage.setResizable(false); - stage.showAndWait(); - // TODO: change to dialog - plantsRoot.fireEvent(new ChangeViewEvent(ChangeViewEvent.CHANGE_MAIN_VIEW, "MyGarden.fxml")); + DialogPane dialogPane = dateSelection.getDialogPane(); + + ButtonType sowButton = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE); + dialogPane.getButtonTypes().addAll(sowButton, ButtonType.CANCEL); + + if (appLoader.loadSceneToStage("SelectSowDay.fxml", stage) instanceof SelectSowDayController controller) { + controller.initSaveButton((Button) dialogPane.lookupButton(sowButton)); + controller.setSelectedPlant(selectedPlant); + dateSelection.setResultConverter(button -> button.equals(sowButton) ? controller.retrieveResult() : null); + } + dialogPane.setContent(stage.getScene().getRoot()); + + dateSelection.showAndWait() + .ifPresent(date -> { + try { + garden.plantAsCrop(selectedPlant, date); + } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { + LOG.log(Level.SEVERE, "Couldn't save Crop", e); + } + plantsRoot.fireEvent(new ChangeViewEvent(ChangeViewEvent.CHANGE_MAIN_VIEW, "MyGarden.fxml")); + }); } /** @@ -113,7 +134,7 @@ public class PlantsController { } /** - * set text of list view to plant name + * set text of list view to plant name */ private void setListCellFactory() { list_plants.setCellFactory(param -> new ListCell<>() { @@ -133,9 +154,10 @@ public class PlantsController { /** * get plant list according to param season and hardiness zone * fill list view with plant list + * * @param season enum of seasons * @throws HardinessZoneNotSetException throws exception - * @throws IOException throws exception + * @throws IOException throws exception */ private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException { clearListView(); @@ -145,8 +167,9 @@ public class PlantsController { /** * get plant list filtered by search plant entry and hardiness zone * fill list view with plant list + * * @throws HardinessZoneNotSetException throws exception when no hardiness zone is defined - * @throws IOException throws exception + * @throws IOException throws exception */ private void viewFilteredListBySearch() throws HardinessZoneNotSetException, IOException { search_plants.textProperty().addListener((observable, oldValue, newValue) -> { @@ -191,7 +214,7 @@ public class PlantsController { for (HardinessZone zone : HardinessZone.values()) { RadioButton radioButton = new RadioButton(zone.name()); radioButton.setToggleGroup(hardinessGroup); - radioButton.setPadding(new Insets(0,0,10,0)); + radioButton.setPadding(new Insets(0, 0, 10, 0)); if (zone.equals(DEFAULT_HARDINESS_ZONE)) { radioButton.setSelected(true); } @@ -213,7 +236,7 @@ public class PlantsController { for (Seasons season : Seasons.values()) { RadioButton radioButton = new RadioButton(season.getName()); radioButton.setToggleGroup(seasonGroup); - radioButton.setPadding(new Insets(0,0,10,0)); + radioButton.setPadding(new Insets(0, 0, 10, 0)); if (season.equals(Seasons.AllSEASONS)) { radioButton.setSelected(true); } @@ -247,12 +270,12 @@ public class PlantsController { Image img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png"))); img_plant.setImage(img); list_plants.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { - if(newValue != null) { + if (newValue != null) { selectedPlant = newValue; description_plant.setText(selectedPlant.description()); selectSowDay_button.setDisable(false); Image img1; - if(selectedPlant.image() != null) { + if (selectedPlant.image() != null) { img1 = selectedPlant.image(); } else { img1 = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png"))); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java index 5260067..67338be 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/SelectSowDayController.java @@ -1,88 +1,55 @@ package ch.zhaw.gartenverwaltung; -import ch.zhaw.gartenverwaltung.bootstrap.Inject; -import ch.zhaw.gartenverwaltung.models.Garden; -import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; -import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.Plant; import javafx.fxml.FXML; -import javafx.fxml.Initializable; 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; -import java.util.ResourceBundle; -public class SelectSowDayController implements Initializable { - private Plant selectedPlant = null; - - @Inject - private Garden garden; +public class SelectSowDayController { + private Plant selectedPlant; @FXML private DatePicker datepicker; - @FXML - private Label popup_label; - - @FXML - private Button save_button; - @FXML private RadioButton harvest_radio; - /** - * close the date selector window - */ - @FXML - void cancel() { - closeWindow(); - } - - /** - * get sow date from datePicker or calculate sow date from harvest date - * save selected plant and sow date - */ - @FXML - void save() throws HardinessZoneNotSetException, IOException, PlantNotFoundException { + public LocalDate retrieveResult() { LocalDate sowDate = datepicker.getValue(); if (harvest_radio.isSelected()) { //ToDo method to get current lifecycle group in plant sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0); } - garden.plantAsCrop(selectedPlant, sowDate); - closeWindow(); + return sowDate; } /** - * save the plant which will be planted and update label + * Set the {@link Plant} for which a date should be selected. + * * @param plant Plant */ public void setSelectedPlant(Plant plant) { selectedPlant = plant; - popup_label.setText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name())); } /** * add listener and set default values - * {@inheritDoc} - * @param location location - * @param resources resources */ - @Override - public void initialize(URL location, ResourceBundle resources) { + @FXML + public void initialize() { clearDatePickerEntries(); - Callback dayCellFactory= getDayCellFactory(); + Callback dayCellFactory = getDayCellFactory(); datepicker.setDayCellFactory(dayCellFactory); datepicker.setEditable(false); + } - save_button.disableProperty().bind(datepicker.valueProperty().isNull()); + public void initSaveButton(Button saveButton) { + saveButton.disableProperty().bind(datepicker.valueProperty().isNull()); } /** @@ -126,12 +93,4 @@ public class SelectSowDayController implements Initializable { } }; } - - /** - * close date picker window - */ - private void closeWindow() { - Stage stage = (Stage) save_button.getScene().getWindow(); - stage.close(); - } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml index ae44b29..75f33c3 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/SelectSowDay.fxml @@ -1,9 +1,7 @@ - - @@ -16,12 +14,11 @@ -