fixed merge conflict PlantsController

This commit is contained in:
giavaphi 2022-10-31 12:52:27 +01:00
parent 5b039eb762
commit 5f35d99839
1 changed files with 17 additions and 18 deletions

View File

@ -5,8 +5,11 @@ import ch.zhaw.gartenverwaltung.plantList.PlantListModel;
import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant; import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Seasons; 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.ChangeListener;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
@ -18,7 +21,6 @@ import javafx.scene.layout.VBox;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -27,6 +29,8 @@ public class PlantsController implements Initializable {
private Plant selectedPlant = null; private Plant selectedPlant = null;
private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A; private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A;
private final ListProperty<Plant> plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@FXML @FXML
private VBox seasons; private VBox seasons;
@ -66,7 +70,9 @@ public class PlantsController implements Initializable {
*/ */
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
setListCellFactory();
fillPlantListWithHardinessZone(); fillPlantListWithHardinessZone();
list_plants.itemsProperty().bind(plantListProperty);
description_plant.setText(""); description_plant.setText("");
saveToMyPlant_button.setDisable(true); saveToMyPlant_button.setDisable(true);
@ -82,15 +88,9 @@ public class PlantsController implements Initializable {
} }
/** /**
* update the ListView according to the plant list provided * set text of list view to plant name
* Entry in ListView is plant name
* @param list plantList which fill the ListView
*/ */
private void fillListViewWithData(List<Plant> list) { private void setListCellFactory() {
clearListView();
for (Plant plant : list) {
list_plants.getItems().add(plant);
}
list_plants.setCellFactory(param -> new ListCell<Plant>() { list_plants.setCellFactory(param -> new ListCell<Plant>() {
@Override @Override
protected void updateItem(Plant plant, boolean empty) { protected void updateItem(Plant plant, boolean empty) {
@ -113,8 +113,8 @@ public class PlantsController implements Initializable {
* @throws IOException throws exception * @throws IOException throws exception
*/ */
private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException { private void viewFilteredListBySeason(Seasons season) throws HardinessZoneNotSetException, IOException {
List<Plant> plantList = plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate()); clearListView();
fillListViewWithData(plantList); plantListProperty.addAll(plantListModel.getFilteredPlantListBySaisonWithoutGrowthPhase(plantListModel.getCurrentZone(), season.getStartDate(), season.getEndDate()));
} }
/** /**
@ -128,13 +128,13 @@ public class PlantsController implements Initializable {
if (newValue.isEmpty()) { if (newValue.isEmpty()) {
fillPlantListWithHardinessZone(); fillPlantListWithHardinessZone();
}else { }else {
List<Plant> plantList = new LinkedList<>();
try { try {
plantList = plantListModel.getFilteredPlantListByString(plantListModel.getCurrentZone(), newValue); List<Plant> filteredPlants = plantListModel.getFilteredPlantListByString(DEFAULT_HARDINESS_ZONE, newValue);
clearListView();
plantListProperty.addAll(filteredPlants);
} catch (HardinessZoneNotSetException | IOException e) { } catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
fillListViewWithData(plantList);
} }
}); });
} }
@ -144,13 +144,12 @@ public class PlantsController implements Initializable {
* fill list view with plant list * fill list view with plant list
*/ */
private void fillPlantListWithHardinessZone() { private void fillPlantListWithHardinessZone() {
List<Plant> plantList = new LinkedList<>();
try { try {
plantList = plantListModel.getPlantList(plantListModel.getCurrentZone()); clearListView();
plantListProperty.addAll(plantListModel.getPlantList(plantListModel.getCurrentZone()));
} catch (HardinessZoneNotSetException | IOException e) { } catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
fillListViewWithData(plantList);
} }
/** /**
@ -249,6 +248,6 @@ public class PlantsController implements Initializable {
* clears the ListView of entries * clears the ListView of entries
*/ */
private void clearListView() { private void clearListView() {
list_plants.getItems().clear(); plantListProperty.clear();
} }
} }