refactor: converted SelectSowDay popup to proper JFX Dialog

This commit is contained in:
David Guler 2022-11-15 11:03:36 +01:00
parent 2b7cec7e6a
commit 09e582b8a2
4 changed files with 53 additions and 84 deletions

View File

@ -129,7 +129,7 @@ public class MyGardenController {
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?");
alert.setContentText("placeholder"); alert.setContentText("Deleting this crop will remove all associated tasks from your schedule.");
alert.showAndWait() alert.showAndWait()
.ifPresent(buttonType -> { .ifPresent(buttonType -> {

View File

@ -5,7 +5,9 @@ import ch.zhaw.gartenverwaltung.bootstrap.AppLoader;
import ch.zhaw.gartenverwaltung.bootstrap.ChangeViewEvent; import ch.zhaw.gartenverwaltung.bootstrap.ChangeViewEvent;
import ch.zhaw.gartenverwaltung.bootstrap.Inject; import ch.zhaw.gartenverwaltung.bootstrap.Inject;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.models.PlantListModel; import ch.zhaw.gartenverwaltung.models.PlantListModel;
import ch.zhaw.gartenverwaltung.models.PlantNotFoundException;
import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Seasons; import ch.zhaw.gartenverwaltung.types.Seasons;
@ -19,10 +21,10 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -34,6 +36,8 @@ public class PlantsController {
private PlantListModel plantListModel; private PlantListModel plantListModel;
@Inject @Inject
private AppLoader appLoader; private AppLoader appLoader;
@Inject
private Garden garden;
private Plant selectedPlant = null; private Plant selectedPlant = null;
private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A; private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A;
@ -71,15 +75,32 @@ public class PlantsController {
@FXML @FXML
void selectSowDate() throws IOException { void selectSowDate() throws IOException {
Stage stage = new Stage(); Stage stage = new Stage();
if (appLoader.loadSceneToStage("SelectSowDay.fxml", stage) instanceof SelectSowDayController controller) { Dialog<LocalDate> dateSelection = new Dialog<>();
controller.setSelectedPlant(selectedPlant); dateSelection.setTitle("Select Date");
} dateSelection.setHeaderText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name()));
dateSelection.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL); DialogPane dialogPane = dateSelection.getDialogPane();
stage.setResizable(false);
stage.showAndWait(); ButtonType sowButton = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE);
// TODO: change to dialog 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")); plantsRoot.fireEvent(new ChangeViewEvent(ChangeViewEvent.CHANGE_MAIN_VIEW, "MyGarden.fxml"));
});
} }
/** /**
@ -133,6 +154,7 @@ public class PlantsController {
/** /**
* get plant list according to param season and hardiness zone * get plant list according to param season and hardiness zone
* fill list view with plant list * fill list view with plant list
*
* @param season enum of seasons * @param season enum of seasons
* @throws HardinessZoneNotSetException throws exception * @throws HardinessZoneNotSetException throws exception
* @throws IOException throws exception * @throws IOException throws exception
@ -145,6 +167,7 @@ public class PlantsController {
/** /**
* get plant list filtered by search plant entry and hardiness zone * get plant list filtered by search plant entry and hardiness zone
* fill list view with plant list * fill list view with plant list
*
* @throws HardinessZoneNotSetException throws exception when no hardiness zone is defined * @throws HardinessZoneNotSetException throws exception when no hardiness zone is defined
* @throws IOException throws exception * @throws IOException throws exception
*/ */
@ -191,7 +214,7 @@ public class PlantsController {
for (HardinessZone zone : HardinessZone.values()) { for (HardinessZone zone : HardinessZone.values()) {
RadioButton radioButton = new RadioButton(zone.name()); RadioButton radioButton = new RadioButton(zone.name());
radioButton.setToggleGroup(hardinessGroup); 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)) { if (zone.equals(DEFAULT_HARDINESS_ZONE)) {
radioButton.setSelected(true); radioButton.setSelected(true);
} }
@ -213,7 +236,7 @@ public class PlantsController {
for (Seasons season : Seasons.values()) { for (Seasons season : Seasons.values()) {
RadioButton radioButton = new RadioButton(season.getName()); RadioButton radioButton = new RadioButton(season.getName());
radioButton.setToggleGroup(seasonGroup); radioButton.setToggleGroup(seasonGroup);
radioButton.setPadding(new Insets(0,0,10,0)); radioButton.setPadding(new Insets(0, 0, 10, 0));
if (season.equals(Seasons.AllSEASONS)) { if (season.equals(Seasons.AllSEASONS)) {
radioButton.setSelected(true); radioButton.setSelected(true);
} }
@ -247,12 +270,12 @@ public class PlantsController {
Image img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png"))); Image img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
img_plant.setImage(img); img_plant.setImage(img);
list_plants.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { list_plants.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if(newValue != null) { if (newValue != null) {
selectedPlant = newValue; selectedPlant = newValue;
description_plant.setText(selectedPlant.description()); description_plant.setText(selectedPlant.description());
selectSowDay_button.setDisable(false); selectSowDay_button.setDisable(false);
Image img1; Image img1;
if(selectedPlant.image() != null) { if (selectedPlant.image() != null) {
img1 = selectedPlant.image(); img1 = selectedPlant.image();
} else { } else {
img1 = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png"))); img1 = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));

View File

@ -1,88 +1,55 @@
package ch.zhaw.gartenverwaltung; 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.GrowthPhaseType;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.util.Callback; import javafx.util.Callback;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.ResourceBundle;
public class SelectSowDayController implements Initializable { public class SelectSowDayController {
private Plant selectedPlant = null; private Plant selectedPlant;
@Inject
private Garden garden;
@FXML @FXML
private DatePicker datepicker; private DatePicker datepicker;
@FXML
private Label popup_label;
@FXML
private Button save_button;
@FXML @FXML
private RadioButton harvest_radio; private RadioButton harvest_radio;
/** public LocalDate retrieveResult() {
* 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 {
LocalDate sowDate = datepicker.getValue(); LocalDate sowDate = datepicker.getValue();
if (harvest_radio.isSelected()) { if (harvest_radio.isSelected()) {
//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);
} }
garden.plantAsCrop(selectedPlant, sowDate); return sowDate;
closeWindow();
} }
/** /**
* save the plant which will be planted and update label * Set the {@link Plant} for which a date should be selected.
*
* @param plant Plant * @param plant Plant
*/ */
public void setSelectedPlant(Plant plant) { public void setSelectedPlant(Plant plant) {
selectedPlant = plant; selectedPlant = plant;
popup_label.setText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name()));
} }
/** /**
* add listener and set default values * add listener and set default values
* {@inheritDoc}
* @param location location
* @param resources resources
*/ */
@Override @FXML
public void initialize(URL location, ResourceBundle resources) { public void initialize() {
clearDatePickerEntries(); clearDatePickerEntries();
Callback<DatePicker, DateCell> dayCellFactory= getDayCellFactory(); Callback<DatePicker, DateCell> dayCellFactory = getDayCellFactory();
datepicker.setDayCellFactory(dayCellFactory); datepicker.setDayCellFactory(dayCellFactory);
datepicker.setEditable(false); 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();
}
} }

View File

@ -1,9 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?> <?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?> <?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ToggleGroup?> <?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.AnchorPane?>
@ -16,12 +14,11 @@
<children> <children>
<VBox maxWidth="1.7976931348623157E308" prefHeight="408.0" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <VBox maxWidth="1.7976931348623157E308" prefHeight="408.0" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
<Label fx:id="popup_label" text="Label" />
<HBox alignment="CENTER" prefHeight="293.0" prefWidth="631.0"> <HBox alignment="CENTER" prefHeight="293.0" prefWidth="631.0">
<children> <children>
<VBox alignment="CENTER_LEFT" prefHeight="88.0" prefWidth="155.0"> <VBox alignment="CENTER_LEFT" prefHeight="88.0" prefWidth="155.0">
<children> <children>
<RadioButton fx:id="sow_radio" mnemonicParsing="false" selected="true" text="Sow"> <RadioButton fx:id="sow_radio" mnemonicParsing="false" selected="true" text="Sow" toggleGroup="$group">
<VBox.margin> <VBox.margin>
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</VBox.margin> </VBox.margin>
@ -38,16 +35,6 @@
<DatePicker fx:id="datepicker" /> <DatePicker fx:id="datepicker" />
</children> </children>
</HBox> </HBox>
<HBox fillHeight="false" prefHeight="54.0" prefWidth="631.0" VBox.vgrow="NEVER">
<children>
<Button fx:id="save_button" mnemonicParsing="false" onAction="#save" prefHeight="25.0" prefWidth="53.0" text="Save">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
<Button fx:id="cancel_button" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
</children>
</HBox>
</children> </children>
<padding> <padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />