diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index 200ccf9..8b821c1 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -21,7 +21,6 @@ import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; -import javafx.stage.Stage; import java.io.IOException; import java.time.LocalDate; @@ -74,7 +73,6 @@ public class PlantsController { */ @FXML void selectSowDate() throws IOException { - Stage stage = new Stage(); Dialog dateSelection = new Dialog<>(); dateSelection.setTitle("Select Date"); dateSelection.setHeaderText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name())); @@ -85,22 +83,21 @@ public class PlantsController { ButtonType sowButton = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE); dialogPane.getButtonTypes().addAll(sowButton, ButtonType.CANCEL); - if (appLoader.loadSceneToStage("SelectSowDay.fxml", stage) instanceof SelectSowDayController controller) { + if (appLoader.loadPaneToDialog("SelectSowDay.fxml", dialogPane) 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")); - }); + 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")); + }); + } } /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index f55f6d5..59a4f7d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -12,6 +12,7 @@ import ch.zhaw.gartenverwaltung.models.GardenSchedule; import ch.zhaw.gartenverwaltung.models.PlantListModel; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; +import javafx.scene.control.DialogPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; @@ -78,6 +79,24 @@ public class AppLoader { return controller; } + /** + * Loads the given fxml-file from resources (no caching) and appendeds it's + * contents to the given {@link DialogPane}. + * Performs dependency-injection. + * + * @param fxmlFile The file name to be loaded + * @param appendee The {@link DialogPane} to which the FXML contents are to be appended. + * @return The controller of the loaded scene. + * @throws IOException if the file could not be loaded + */ + public Object loadPaneToDialog(String fxmlFile, DialogPane appendee) throws IOException { + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + appendee.setContent(loader.load()); + Object controller = loader.getController(); + annotationInject(controller); + return controller; + } + /** * Loads the given fxml-file from resources and caches the pane. * Performs dependency-injection.