refactor: fixed and simplified dayCellFactory even more
Added method to check if a date is within a GrowthPhaseType to plant, thus removing the need for the ugly getMinDate methods and moving knowledge of the phase-internals to the Plant class. Also removed the need to specify the lifecycle-group to the sowDateFromHarvest method
This commit is contained in:
@@ -7,7 +7,6 @@ import javafx.scene.control.*;
|
||||
import javafx.util.Callback;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.MonthDay;
|
||||
|
||||
public class SelectSowDayController {
|
||||
private Plant selectedPlant;
|
||||
@@ -17,12 +16,15 @@ public class SelectSowDayController {
|
||||
|
||||
@FXML
|
||||
private RadioButton harvest_radio;
|
||||
@FXML
|
||||
private RadioButton sow_radio;
|
||||
@FXML
|
||||
public ToggleGroup phase_group;
|
||||
|
||||
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);
|
||||
sowDate = selectedPlant.sowDateFromHarvestDate(sowDate);
|
||||
}
|
||||
return sowDate;
|
||||
}
|
||||
@@ -46,6 +48,9 @@ public class SelectSowDayController {
|
||||
Callback<DatePicker, DateCell> dayCellFactory = getDayCellFactory();
|
||||
datepicker.setDayCellFactory(dayCellFactory);
|
||||
datepicker.setEditable(false);
|
||||
|
||||
sow_radio.setUserData(GrowthPhaseType.SOW);
|
||||
harvest_radio.setUserData(GrowthPhaseType.HARVEST);
|
||||
}
|
||||
|
||||
public void initSaveButton(Button saveButton) {
|
||||
@@ -75,12 +80,10 @@ public class SelectSowDayController {
|
||||
setDisable(true);
|
||||
setStyle("-fx-background-color: #ffc0cb;");
|
||||
|
||||
if (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item, 0).compareTo(today) >= 0)) {
|
||||
GrowthPhaseType selectedPhase = harvest_radio.isSelected() ? GrowthPhaseType.HARVEST : GrowthPhaseType.SOW;
|
||||
MonthDay minDate = selectedPlant.getMinDateForGrowthPhase(selectedPhase);
|
||||
MonthDay maxDate = selectedPlant.getMaxDateForGrowthPhase(selectedPhase);
|
||||
if (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item).compareTo(today) >= 0)) {
|
||||
GrowthPhaseType selectedPhase = (GrowthPhaseType) phase_group.getSelectedToggle().getUserData();
|
||||
|
||||
if (dateInRange(item, minDate, maxDate)) {
|
||||
if (selectedPlant.isDateInPhase(item, selectedPhase)) {
|
||||
setDisable(false);
|
||||
setStyle("-fx-background-color: #32CD32;");
|
||||
}
|
||||
@@ -88,22 +91,4 @@ public class SelectSowDayController {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given {@link LocalDate} is within the given {@link MonthDay} range.
|
||||
* (regardless of year)
|
||||
*
|
||||
* @param subject The date to check
|
||||
* @param min The start of the date-range
|
||||
* @param max The end of the date-range
|
||||
* @return Whether the subject is within the range.
|
||||
*/
|
||||
private boolean dateInRange(LocalDate subject, MonthDay min, MonthDay max) {
|
||||
return subject.getMonth().compareTo(min.getMonth()) >= 0 &&
|
||||
subject.getMonth().compareTo(max.getMonth()) <= 0 &&
|
||||
// if the day is less than the minimum day, the minimum month must not be equal
|
||||
(subject.getDayOfMonth() >= min.getDayOfMonth() || !subject.getMonth().equals(min.getMonth())) &&
|
||||
// if the day is greater than the maximum day, the maximum month must not be equal
|
||||
(subject.getDayOfMonth() <= max.getDayOfMonth() || !subject.getMonth().equals(max.getMonth()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user