#46 DatePicker for sow and harvest day
This commit is contained in:
parent
7a6a0eb66f
commit
efc95dbcc6
|
@ -5,7 +5,6 @@ import ch.zhaw.gartenverwaltung.plantList.PlantListModel;
|
|||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import ch.zhaw.gartenverwaltung.types.Seasons;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.ListProperty;
|
||||
import javafx.beans.property.SimpleListProperty;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
|
@ -13,18 +12,23 @@ import javafx.beans.value.ObservableValue;
|
|||
import javafx.collections.FXCollections;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Bounds;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.Modality;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class PlantsController implements Initializable {
|
||||
|
@ -33,7 +37,6 @@ public class PlantsController implements Initializable {
|
|||
private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A;
|
||||
|
||||
// TODO: move to model
|
||||
|
||||
private final ListProperty<Plant> plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
|
||||
|
||||
@FXML
|
||||
|
@ -62,8 +65,17 @@ public class PlantsController implements Initializable {
|
|||
* @param event event
|
||||
*/
|
||||
@FXML
|
||||
void saveToMyPlant(ActionEvent event) {
|
||||
//ToDo model save selectedPlant to mySelectedPlant(IO)
|
||||
void saveToMyPlant(ActionEvent event) throws IOException {
|
||||
Parent root;
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(Objects.requireNonNull(getClass().getResource("SelectSowDay.fxml")));
|
||||
root = fxmlLoader.load();
|
||||
SelectSowDayController controller = fxmlLoader.getController();
|
||||
controller.getSelectedPlant(selectedPlant);
|
||||
Stage stage = new Stage();
|
||||
stage.setScene(new Scene(root));
|
||||
stage.initModality(Modality.APPLICATION_MODAL);
|
||||
stage.setResizable(false);
|
||||
stage.showAndWait();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
package ch.zhaw.gartenverwaltung;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.util.Callback;
|
||||
|
||||
import java.net.URL;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class SelectSowDayController implements Initializable {
|
||||
private Plant selectedPlant = null;
|
||||
|
||||
@FXML
|
||||
private DatePicker datepicker;
|
||||
|
||||
@FXML
|
||||
private Label popup_label;
|
||||
|
||||
@FXML
|
||||
private Button save_button;
|
||||
|
||||
@FXML
|
||||
private RadioButton sow_radio;
|
||||
|
||||
@FXML
|
||||
void cancel(ActionEvent event) {
|
||||
closeWindow();
|
||||
}
|
||||
|
||||
@FXML
|
||||
void save(ActionEvent event) {
|
||||
//ToDo save plant and sow date to crop/gardenplan model
|
||||
System.out.println(selectedPlant);
|
||||
if (sow_radio.isSelected()) {
|
||||
System.out.println(datepicker.getValue());
|
||||
} else {
|
||||
//ToDo method to get current lifecycle group in plant
|
||||
//ToDo error when
|
||||
//what's up with that group thing in life_cycle we have hardiness zone for that
|
||||
System.out.println(selectedPlant.sowDateFromHarvestDate(datepicker.getValue(), 0));
|
||||
}
|
||||
closeWindow();
|
||||
}
|
||||
|
||||
public void getSelectedPlant(Plant plant) {
|
||||
selectedPlant = plant;
|
||||
popup_label.setText("Select Harvest/Sow Date for" + selectedPlant.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
clearDatePickerEntries();
|
||||
|
||||
Callback<DatePicker, DateCell> dayCellFactory= getDayCellFactory();
|
||||
datepicker.setDayCellFactory(dayCellFactory);
|
||||
datepicker.getEditor().setEditable(false);
|
||||
|
||||
enableDisableSaveButton();
|
||||
}
|
||||
|
||||
private void clearDatePickerEntries() {
|
||||
sow_radio.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean isNowSelected) {
|
||||
datepicker.getEditor().clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Callback<DatePicker, DateCell> getDayCellFactory() {
|
||||
|
||||
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, 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;");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
return dayCellFactory;
|
||||
}
|
||||
|
||||
private void closeWindow() {
|
||||
Stage stage = (Stage) save_button.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
|
||||
private void enableDisableSaveButton() {
|
||||
save_button.setDisable(true);
|
||||
datepicker.getEditor().textProperty().addListener(new ChangeListener<String>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
||||
if (newValue == null || newValue.equals("")) {
|
||||
save_button.setDisable(true);
|
||||
} else {
|
||||
save_button.setDisable(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package ch.zhaw.gartenverwaltung.types;
|
|||
import javafx.scene.image.Image;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -47,4 +48,34 @@ public record Plant(
|
|||
int currentYear = LocalDate.now().getYear();
|
||||
return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear));
|
||||
}
|
||||
|
||||
public List<LocalDate> getDateListOfGrowthPhase(GrowthPhaseType growthPhase) {
|
||||
List<LocalDate> dates = new LinkedList<>();
|
||||
for (GrowthPhase growth : lifecycle) {
|
||||
if (growth.type().equals(growthPhase)) {
|
||||
dates = addDatesFromMonthDay(growth);
|
||||
}
|
||||
}
|
||||
if (growthPhase.equals(GrowthPhaseType.HARVEST)) {
|
||||
return removeDatesForPassedSowDates(dates);
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
private List<LocalDate> addDatesFromMonthDay(GrowthPhase growthPhase) {
|
||||
List<LocalDate> dates = new LinkedList<>();
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate start = growthPhase.startDate().atYear(today.getYear());
|
||||
LocalDate end = growthPhase.endDate().atYear(today.getYear());
|
||||
while (!start.isAfter(end)) {
|
||||
dates.add(start);
|
||||
start = start.plusDays(1);
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
private List<LocalDate> removeDatesForPassedSowDates(List<LocalDate> dates) {
|
||||
//ToDo Remove harvest dates where sow dates have already passed current day
|
||||
return dates;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.DatePicker?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.RadioButton?>
|
||||
<?import javafx.scene.control.ToggleGroup?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
|
||||
|
||||
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0"
|
||||
prefWidth="308.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.gartenverwaltung.SelectSowDayController">
|
||||
<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">
|
||||
<children>
|
||||
<Label fx:id="popup_label" text="Label" />
|
||||
<HBox alignment="CENTER" prefHeight="293.0" prefWidth="631.0">
|
||||
<children>
|
||||
<VBox alignment="CENTER_LEFT" prefHeight="88.0" prefWidth="155.0">
|
||||
<children>
|
||||
<RadioButton fx:id="sow_radio" mnemonicParsing="false" selected="true" text="Sow">
|
||||
<VBox.margin>
|
||||
<Insets bottom="10.0" />
|
||||
</VBox.margin>
|
||||
<toggleGroup>
|
||||
<ToggleGroup fx:id="group" />
|
||||
</toggleGroup>
|
||||
</RadioButton>
|
||||
<RadioButton fx:id="harvest_radio" mnemonicParsing="false" text="Harvest" toggleGroup="$group" />
|
||||
</children>
|
||||
<HBox.margin>
|
||||
<Insets top="10.0" />
|
||||
</HBox.margin>
|
||||
</VBox>
|
||||
<DatePicker fx:id="datepicker" />
|
||||
</children>
|
||||
</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>
|
||||
<padding>
|
||||
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
|
||||
</padding>
|
||||
</VBox>
|
||||
</children>
|
||||
</AnchorPane>
|
Loading…
Reference in New Issue