From f22ef61d3c26b4a7f07eee2a3fe11b3cc220c332 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Sun, 30 Oct 2022 18:06:18 +0100 Subject: [PATCH 1/4] #42 bug resize window fix --- .../ch/zhaw/gartenverwaltung/MainFXMLController.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java b/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java index c3fe04d..58ea2eb 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java @@ -62,15 +62,17 @@ public class MainFXMLController implements Initializable { * @throws IOException exception when file does not exist */ public void loadPane(String fxmlFile) throws IOException { - //ToDo HGrow and VGrow of new node - Node node; + AnchorPane anchorPane; FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); - node = (Node)loader.load(); + anchorPane = loader.load(); if(fxmlFile.equals("MyPlants.fxml")) { MyPlantsController myPlantsController = loader.getController(); myPlantsController.getMainController(this); } - mainPane.getChildren().setAll(node); + mainPane.getChildren().setAll(anchorPane); + anchorPane.prefWidthProperty().bind(mainPane.widthProperty()); + anchorPane.prefHeightProperty().bind(mainPane.heightProperty()); + } private void styleChangeButton(Button button) { From c83b8695abe03dd6ccfa41ba27a22993f519be07 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Sun, 30 Oct 2022 23:23:19 +0100 Subject: [PATCH 2/4] #12 PlantsController filter list view --- .../gartenverwaltung/PlantsController.java | 149 +++++++++++------- .../plantList/PlantListModel.java | 13 ++ .../zhaw/gartenverwaltung/types/Seasons.java | 26 +++ .../ch/zhaw/gartenverwaltung/Plants.fxml | 33 +--- 4 files changed, 132 insertions(+), 89 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index a8c8d93..a350884 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -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 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, )) - //List plantList = new LinkedList<>(); - //fillListViewWithData(plantList); + private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException { + List plantList = plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate()); + fillListViewWithData(plantList); } - private void viewFilteredListBySearch(String query) { - //ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, )) - //List plantList = new LinkedList<>(); - //fillListViewWithData(plantList); + private void viewFilteredListBySearch() throws HardinessZoneNotSetException, IOException { + search_plants.textProperty().addListener((observable, oldValue, newValue) -> { + if (newValue.isEmpty()) { + fillPlantListWithDefaultValues(); + }else { + List plantList = new LinkedList<>(); + try { + plantList = plantListModel.getFilteredPlantListByString(plantListModel.getCurrentZone(), newValue); + } catch (HardinessZoneNotSetException | IOException e) { + e.printStackTrace(); + } + fillListViewWithData(plantList); + } + }); + } + + private void fillPlantListWithDefaultValues() { + List 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() { + @Override + public void changed(ObservableValue 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() { + @Override + public void changed(ObservableValue 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() { @Override public void changed(ObservableValue 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); } } }); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 7039476..060a682 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -160,4 +160,17 @@ public class PlantListModel { public List 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 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))); + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java new file mode 100644 index 0000000..5e907a4 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java @@ -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); + } +} diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml index 0cb3c6d..4fbb89b 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml @@ -3,7 +3,6 @@ - @@ -16,8 +15,9 @@ - + @@ -30,7 +30,7 @@ - + @@ -75,30 +75,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - + From 5b039eb762d43f4f140254b6c9cd4b1b8afbf764 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Mon, 31 Oct 2022 09:23:44 +0100 Subject: [PATCH 3/4] #12 small changes and java doc --- .../gartenverwaltung/PlantsController.java | 63 +++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index a350884..a63290e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -14,7 +14,6 @@ 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; import java.io.IOException; @@ -49,18 +48,25 @@ public class PlantsController implements Initializable { @FXML private TextField search_plants; - + /** + * saves the current selected plant in new JSON database + * @param event event + */ @FXML void saveToMyPlant(ActionEvent event) { //ToDo model save selectedPlant to mySelectedPlant(IO) } /** + * fill list view with current hardiness zone + * set default values + * create filter of season and hardiness zone + * create event listener for selected list entry and search by query * {@inheritDoc} */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { - fillPlantListWithDefaultValues(); + fillPlantListWithHardinessZone(); description_plant.setText(""); saveToMyPlant_button.setDisable(true); @@ -99,15 +105,28 @@ public class PlantsController implements Initializable { }); } + /** + * get plant list according to param season and hardiness zone + * fill list view with plant list + * @param season enum of seasons + * @throws HardinessZoneNotSetException throws exception + * @throws IOException throws exception + */ private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException { List plantList = plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate()); fillListViewWithData(plantList); } + /** + * get plant list filtered by search plant entry and hardiness zone + * fill list view with plant list + * @throws HardinessZoneNotSetException throws exception when no hardiness zone is defined + * @throws IOException throws exception + */ private void viewFilteredListBySearch() throws HardinessZoneNotSetException, IOException { search_plants.textProperty().addListener((observable, oldValue, newValue) -> { if (newValue.isEmpty()) { - fillPlantListWithDefaultValues(); + fillPlantListWithHardinessZone(); }else { List plantList = new LinkedList<>(); try { @@ -120,7 +139,11 @@ public class PlantsController implements Initializable { }); } - private void fillPlantListWithDefaultValues() { + /** + * get plant list of current hardiness zone + * fill list view with plant list + */ + private void fillPlantListWithHardinessZone() { List plantList = new LinkedList<>(); try { plantList = plantListModel.getPlantList(plantListModel.getCurrentZone()); @@ -130,6 +153,11 @@ public class PlantsController implements Initializable { fillListViewWithData(plantList); } + /** + * creates radio buttons for the hardiness zones defined in enum HardinessZone + * defines default value as selected + * when selected filter viewList according to hardiness zone + */ private void createFilterHardinessZone() { ToggleGroup hardinessGroup = new ToggleGroup(); for (HardinessZone zone : HardinessZone.values()) { @@ -143,13 +171,18 @@ public class PlantsController implements Initializable { @Override public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { plantListModel.setCurrentZone(zone); - fillPlantListWithDefaultValues(); + fillPlantListWithHardinessZone(); } }); climate_zones.getChildren().add(radioButton); } } + /** + * creates radio buttons for the seasons defined in enum Seasons + * defines default value as selected + * when selected filter viewList according to seasons + */ private void createFilterSeasons() { ToggleGroup seasonGroup = new ToggleGroup(); for (Seasons season : Seasons.values()) { @@ -163,7 +196,7 @@ public class PlantsController implements Initializable { @Override public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) { if (season.equals(Seasons.AllSEASONS)) { - fillPlantListWithDefaultValues(); + fillPlantListWithHardinessZone(); } else { try { viewFilteredListBySeason(season); @@ -179,7 +212,9 @@ public class PlantsController implements Initializable { } /** - * observes changes in the selectedProperty of ListView and updates the description label + * observes changes in the selectedProperty of ListView and updates: + * the description label + * image of the plant */ private void lookForSelectedListEntry() { list_plants.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @@ -189,9 +224,15 @@ public class PlantsController implements Initializable { selectedPlant = newValue; description_plant.setText(selectedPlant.description()); saveToMyPlant_button.setDisable(false); - //ToDo Uncomment if plant.java can load img - //Image img = selectedPlant.image(); - //img_plant.setImage(img); + Image img; + if(selectedPlant.image() != null) { + img = selectedPlant.image(); + } else { + img = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png"))); + } + img_plant.setImage(img); + + } else { selectedPlant = null; description_plant.setText(""); From 5f35d9983928043cd4193ed339c911ce0ccade99 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Mon, 31 Oct 2022 12:52:27 +0100 Subject: [PATCH 4/4] fixed merge conflict PlantsController --- .../gartenverwaltung/PlantsController.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index a63290e..f8d0e4d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -5,8 +5,11 @@ 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.property.ListProperty; +import javafx.beans.property.SimpleListProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; @@ -18,7 +21,6 @@ import javafx.scene.layout.VBox; import java.io.IOException; import java.net.URL; -import java.util.LinkedList; import java.util.List; import java.util.ResourceBundle; @@ -27,6 +29,8 @@ public class PlantsController implements Initializable { private Plant selectedPlant = null; private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A; + private final ListProperty plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); + @FXML private VBox seasons; @@ -66,7 +70,9 @@ public class PlantsController implements Initializable { */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { + setListCellFactory(); fillPlantListWithHardinessZone(); + list_plants.itemsProperty().bind(plantListProperty); description_plant.setText(""); saveToMyPlant_button.setDisable(true); @@ -82,15 +88,9 @@ public class PlantsController implements Initializable { } /** - * update the ListView according to the plant list provided - * Entry in ListView is plant name - * @param list plantList which fill the ListView + * set text of list view to plant name */ - private void fillListViewWithData(List list) { - clearListView(); - for (Plant plant : list) { - list_plants.getItems().add(plant); - } + private void setListCellFactory() { list_plants.setCellFactory(param -> new ListCell() { @Override protected void updateItem(Plant plant, boolean empty) { @@ -113,8 +113,8 @@ public class PlantsController implements Initializable { * @throws IOException throws exception */ private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException { - List plantList = plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate()); - fillListViewWithData(plantList); + clearListView(); + plantListProperty.addAll(plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate())); } /** @@ -128,13 +128,13 @@ public class PlantsController implements Initializable { if (newValue.isEmpty()) { fillPlantListWithHardinessZone(); }else { - List plantList = new LinkedList<>(); try { - plantList = plantListModel.getFilteredPlantListByString(plantListModel.getCurrentZone(), newValue); + List filteredPlants = plantListModel.getFilteredPlantListByString(DEFAULT_HARDINESS_ZONE, newValue); + clearListView(); + plantListProperty.addAll(filteredPlants); } catch (HardinessZoneNotSetException | IOException e) { e.printStackTrace(); } - fillListViewWithData(plantList); } }); } @@ -144,13 +144,12 @@ public class PlantsController implements Initializable { * fill list view with plant list */ private void fillPlantListWithHardinessZone() { - List plantList = new LinkedList<>(); try { - plantList = plantListModel.getPlantList(plantListModel.getCurrentZone()); + clearListView(); + plantListProperty.addAll(plantListModel.getPlantList(plantListModel.getCurrentZone())); } catch (HardinessZoneNotSetException | IOException e) { e.printStackTrace(); } - fillListViewWithData(plantList); } /** @@ -249,6 +248,6 @@ public class PlantsController implements Initializable { * clears the ListView of entries */ private void clearListView() { - list_plants.getItems().clear(); + plantListProperty.clear(); } }