refactor: first attempt at dependency injection

also some more renaming and improving date-picker dialog
This commit is contained in:
David Guler
2022-11-14 20:00:01 +01:00
parent 4f80a0a3e0
commit 15279838b7
14 changed files with 378 additions and 333 deletions
@@ -3,7 +3,6 @@ package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.models.PlantNotFoundException;
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.event.ActionEvent;
@@ -21,8 +20,12 @@ import java.util.ResourceBundle;
public class SelectSowDayController implements Initializable {
private Plant selectedPlant = null;
private final GardenSchedule gardenSchedule = new GardenSchedule();
private final Garden garden = new Garden(gardenSchedule);
private Garden garden;
@SuppressWarnings("unused")
public void injectDependencies(Garden garden) {
this.garden = garden;
}
@FXML
private DatePicker datepicker;
@@ -34,9 +37,7 @@ public class SelectSowDayController implements Initializable {
private Button save_button;
@FXML
private RadioButton sow_radio;
public SelectSowDayController() throws IOException {}
private RadioButton harvest_radio;
/**
* close the date selector window
@@ -54,10 +55,8 @@ public class SelectSowDayController implements Initializable {
*/
@FXML
void save(ActionEvent event) throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
LocalDate sowDate;
if (sow_radio.isSelected()) {
sowDate = datepicker.getValue();
} else {
LocalDate sowDate = datepicker.getValue();
if (harvest_radio.isSelected()) {
//ToDo method to get current lifecycle group in plant
sowDate = selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0);
}
@@ -69,9 +68,9 @@ public class SelectSowDayController implements Initializable {
* save the plant which will be planted and update label
* @param plant Plant
*/
public void getSelectedPlant(Plant plant) {
public void setSelectedPlant(Plant plant) {
selectedPlant = plant;
popup_label.setText("Select Harvest/Sow Date for" + selectedPlant.name());
popup_label.setText(String.format("Select Harvest/Sow Date for %s:", selectedPlant.name()));
}
/**
@@ -86,16 +85,16 @@ public class SelectSowDayController implements Initializable {
Callback<DatePicker, DateCell> dayCellFactory= getDayCellFactory();
datepicker.setDayCellFactory(dayCellFactory);
datepicker.getEditor().setEditable(false);
datepicker.setEditable(false);
enableDisableSaveButton();
save_button.disableProperty().bind(datepicker.valueProperty().isNull());
}
/**
* clear date picker editor when radio button is changed
*/
private void clearDatePickerEntries() {
sow_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.getEditor().clear());
harvest_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.setValue(null));
}
/**
@@ -104,40 +103,33 @@ public class SelectSowDayController implements Initializable {
*/
private Callback<DatePicker, DateCell> getDayCellFactory() {
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
return (datePicker) -> new DateCell() {
@Override
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
List<LocalDate> dates;
LocalDate today = LocalDate.now();
if (sow_radio.isSelected()) {
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW);
} else {
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST);
}
for (LocalDate date : dates) {
if (item.getMonth() == date.getMonth()
&& item.getDayOfMonth() == date.getDayOfMonth()
&& item.compareTo(today) > 0) {
setDisable(false);
setStyle("-fx-background-color: #32CD32;");
}
}
if ((!sow_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
List<LocalDate> dates;
LocalDate today = LocalDate.now();
if (harvest_radio.isSelected()) {
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.HARVEST);
} else {
dates = selectedPlant.getDateListOfGrowthPhase(GrowthPhaseType.SOW);
}
for (LocalDate date : dates) {
if (item.getMonth() == date.getMonth()
&& item.getDayOfMonth() == date.getDayOfMonth()
&& item.compareTo(today) > 0) {
setDisable(false);
setStyle("-fx-background-color: #32CD32;");
}
};
}
if ((harvest_radio.isSelected() && selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) < 0)) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
}
};
return dayCellFactory;
}
/**
@@ -147,18 +139,4 @@ public class SelectSowDayController implements Initializable {
Stage stage = (Stage) save_button.getScene().getWindow();
stage.close();
}
/**
* disable save button, when there is no date selected in date picker
*/
private void enableDisableSaveButton() {
save_button.setDisable(true);
datepicker.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null || newValue.equals("")) {
save_button.setDisable(true);
} else {
save_button.setDisable(false);
}
});
}
}