package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.types.GrowthPhaseType; import ch.zhaw.gartenverwaltung.types.Plant; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.util.Callback; import java.time.LocalDate; import java.util.List; public class SelectSowDayController { private Plant selectedPlant; @FXML private DatePicker datepicker; @FXML private RadioButton harvest_radio; 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); } return sowDate; } /** * Set the {@link Plant} for which a date should be selected. * * @param plant Plant */ public void setSelectedPlant(Plant plant) { selectedPlant = plant; } /** * add listener and set default values */ @FXML public void initialize() { clearDatePickerEntries(); Callback dayCellFactory = getDayCellFactory(); datepicker.setDayCellFactory(dayCellFactory); datepicker.setEditable(false); } public void initSaveButton(Button saveButton) { saveButton.disableProperty().bind(datepicker.valueProperty().isNull()); } /** * clear date picker editor when radio button is changed */ private void clearDatePickerEntries() { harvest_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.setValue(null)); } /** * date picker disable/enable dates according to selected plant: sow or harvest day * @return cellFactory of datePicker */ private Callback getDayCellFactory() { return (datePicker) -> new DateCell() { @Override public void updateItem(LocalDate item, boolean empty) { super.updateItem(item, empty); setDisable(true); setStyle("-fx-background-color: #ffc0cb;"); List 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;"); } } }; } }