#12 PlantsController filter list view
This commit is contained in:
@@ -4,12 +4,15 @@ import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
|
||||
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.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.InputMethodEvent;
|
||||
import javafx.scene.layout.VBox;
|
||||
@@ -26,7 +29,7 @@ public class PlantsController implements Initializable {
|
||||
private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A;
|
||||
|
||||
@FXML
|
||||
private CheckBox autum_filter;
|
||||
private VBox seasons;
|
||||
|
||||
@FXML
|
||||
private VBox climate_zones;
|
||||
@@ -46,67 +49,30 @@ public class PlantsController implements Initializable {
|
||||
@FXML
|
||||
private TextField search_plants;
|
||||
|
||||
@FXML
|
||||
private CheckBox sommer_filter;
|
||||
|
||||
@FXML
|
||||
private CheckBox spring_filter;
|
||||
|
||||
@FXML
|
||||
private CheckBox winter_filter;
|
||||
|
||||
@FXML
|
||||
void filterAutum(ActionEvent event) {
|
||||
//ToDo
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
void filterSommer(ActionEvent event) {
|
||||
//ToDo
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
void filterSpring(ActionEvent event) {
|
||||
//ToDo
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
void filterWinter(ActionEvent event) {
|
||||
//ToDo
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
void saveToMyPlant(ActionEvent event) {
|
||||
//ToDo model save selectedPlant to mySelectedPlant(IO)
|
||||
}
|
||||
|
||||
@FXML
|
||||
void searchForPlant(InputMethodEvent event) {
|
||||
viewFilteredListBySearch(search_plants.getText());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||
List<Plant> plantList = new LinkedList<>();
|
||||
try {
|
||||
plantList = plantListModel.getPlantList(DEFAULT_HARDINESS_ZONE);
|
||||
} catch (HardinessZoneNotSetException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
fillListViewWithData(plantList);
|
||||
fillPlantListWithDefaultValues();
|
||||
|
||||
description_plant.setText("");
|
||||
saveToMyPlant_button.setDisable(true);
|
||||
|
||||
createFilterSeasons();
|
||||
createFilterHardinessZone();
|
||||
lookForSelectedListEntry();
|
||||
try {
|
||||
viewFilteredListBySearch();
|
||||
} catch (HardinessZoneNotSetException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,24 +99,83 @@ public class PlantsController implements Initializable {
|
||||
});
|
||||
}
|
||||
|
||||
private void viewFilteredListByFilters() {
|
||||
boolean springValue = spring_filter.isSelected();
|
||||
boolean sommerValue = sommer_filter.isSelected();
|
||||
boolean autumValue = autum_filter.isSelected();
|
||||
boolean winterValue = winter_filter.isSelected();
|
||||
//ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, <predicate>))
|
||||
//List<Plant> plantList = new LinkedList<>();
|
||||
//fillListViewWithData(plantList);
|
||||
private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException {
|
||||
List<Plant> plantList = plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate());
|
||||
fillListViewWithData(plantList);
|
||||
}
|
||||
|
||||
private void viewFilteredListBySearch(String query) {
|
||||
//ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, <predicate>))
|
||||
//List<Plant> plantList = new LinkedList<>();
|
||||
//fillListViewWithData(plantList);
|
||||
private void viewFilteredListBySearch() throws HardinessZoneNotSetException, IOException {
|
||||
search_plants.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (newValue.isEmpty()) {
|
||||
fillPlantListWithDefaultValues();
|
||||
}else {
|
||||
List<Plant> plantList = new LinkedList<>();
|
||||
try {
|
||||
plantList = plantListModel.getFilteredPlantListByString(plantListModel.getCurrentZone(), newValue);
|
||||
} catch (HardinessZoneNotSetException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
fillListViewWithData(plantList);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void fillPlantListWithDefaultValues() {
|
||||
List<Plant> plantList = new LinkedList<>();
|
||||
try {
|
||||
plantList = plantListModel.getPlantList(plantListModel.getCurrentZone());
|
||||
} catch (HardinessZoneNotSetException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
fillListViewWithData(plantList);
|
||||
}
|
||||
|
||||
private void createFilterHardinessZone() {
|
||||
//ToDo create radioList of hardinessZone in VBox climate_zones
|
||||
ToggleGroup hardinessGroup = new ToggleGroup();
|
||||
for (HardinessZone zone : HardinessZone.values()) {
|
||||
RadioButton radioButton = new RadioButton(zone.name());
|
||||
radioButton.setToggleGroup(hardinessGroup);
|
||||
radioButton.setPadding(new Insets(0,0,10,0));
|
||||
if (zone.equals(DEFAULT_HARDINESS_ZONE)) {
|
||||
radioButton.setSelected(true);
|
||||
}
|
||||
radioButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
|
||||
plantListModel.setCurrentZone(zone);
|
||||
fillPlantListWithDefaultValues();
|
||||
}
|
||||
});
|
||||
climate_zones.getChildren().add(radioButton);
|
||||
}
|
||||
}
|
||||
|
||||
private void createFilterSeasons() {
|
||||
ToggleGroup seasonGroup = new ToggleGroup();
|
||||
for (Seasons season : Seasons.values()) {
|
||||
RadioButton radioButton = new RadioButton(season.name());
|
||||
radioButton.setToggleGroup(seasonGroup);
|
||||
radioButton.setPadding(new Insets(0,0,10,0));
|
||||
if (season.equals(Seasons.AllSEASONS)) {
|
||||
radioButton.setSelected(true);
|
||||
}
|
||||
radioButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
|
||||
if (season.equals(Seasons.AllSEASONS)) {
|
||||
fillPlantListWithDefaultValues();
|
||||
} else {
|
||||
try {
|
||||
viewFilteredListBySeason(season);
|
||||
} catch (HardinessZoneNotSetException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
seasons.getChildren().add(radioButton);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,17 +185,19 @@ public class PlantsController implements Initializable {
|
||||
list_plants.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Plant>() {
|
||||
@Override
|
||||
public void changed(ObservableValue<? extends Plant> observable, Plant oldValue, Plant newValue) {
|
||||
//ToDo
|
||||
if(newValue != null) {
|
||||
selectedPlant = newValue;
|
||||
description_plant.setText(selectedPlant.description());
|
||||
saveToMyPlant_button.setDisable(false);
|
||||
//update img plant
|
||||
//ToDo Uncomment if plant.java can load img
|
||||
//Image img = selectedPlant.image();
|
||||
//img_plant.setImage(img);
|
||||
} else {
|
||||
selectedPlant = null;
|
||||
description_plant.setText("");
|
||||
saveToMyPlant_button.setDisable(true);
|
||||
//update img when null placeholder PNG
|
||||
Image img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
|
||||
img_plant.setImage(img);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -160,4 +160,17 @@ public class PlantListModel {
|
||||
public List<Plant> getFilteredPlantListByHarvestSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException {
|
||||
return getFilteredPlantListBySaison(GrowthPhaseType.HARVEST, zone, from, to);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param zone selected hardiness zone
|
||||
* @param from the earliest date to for the filter
|
||||
* @param to the lastest date for the filter
|
||||
* @return List of Plants with selected saison
|
||||
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Plant> getFilteredPlantListBySaisonWithoutGrowthPhase(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException {
|
||||
return getFilteredPlantList(zone, plant -> plant.lifecycle().stream().anyMatch(growthPhase -> growthPhase.startDate().compareTo(from) >= 0 && (growthPhase.startDate().compareTo(to) <= 0)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package ch.zhaw.gartenverwaltung.types;
|
||||
|
||||
import java.time.MonthDay;
|
||||
|
||||
public enum Seasons {
|
||||
AllSEASONS("--01-01", "--12-31"),
|
||||
SPRING("--03-01", "--05-30"),
|
||||
SOMMER("--06-01", "--08-30"),
|
||||
AUTUM("--09-01", "--11-30"),
|
||||
WINTER("--12-01", "--02-28");
|
||||
|
||||
public final String startDate;
|
||||
public final String endDate;
|
||||
|
||||
Seasons(String startDate, String endDate) {
|
||||
this.startDate = startDate;
|
||||
this.endDate = endDate;
|
||||
}
|
||||
|
||||
public MonthDay getStartDate() {
|
||||
return MonthDay.parse(this.startDate);
|
||||
}
|
||||
public MonthDay getEndDate() {
|
||||
return MonthDay.parse(this.endDate);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user