From 5f35d9983928043cd4193ed339c911ce0ccade99 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Mon, 31 Oct 2022 12:52:27 +0100 Subject: [PATCH] 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(); } }