case insensitive serarch, images

This commit is contained in:
David Guler
2022-10-31 10:06:17 +01:00
parent 6c00b7f182
commit 7a060be84a
4 changed files with 94 additions and 49 deletions
@@ -4,19 +4,23 @@ 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 javafx.application.Platform;
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;
import javafx.geometry.Bounds;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.InputMethodEvent;
import javafx.scene.input.KeyEvent;
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;
@@ -25,6 +29,9 @@ public class PlantsController implements Initializable {
private Plant selectedPlant = null;
private final HardinessZone DEFAULT_HARDINESS_ZONE = HardinessZone.ZONE_8A;
// TODO: move to model
private final ListProperty<Plant> plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@FXML
private CheckBox autum_filter;
@@ -85,7 +92,7 @@ public class PlantsController implements Initializable {
}
@FXML
void searchForPlant(InputMethodEvent event) {
void searchForPlant(KeyEvent event) {
viewFilteredListBySearch(search_plants.getText());
}
@@ -94,13 +101,20 @@ public class PlantsController implements Initializable {
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
List<Plant> plantList = new LinkedList<>();
setListCellFactory();
try {
plantList = plantListModel.getPlantList(DEFAULT_HARDINESS_ZONE);
plantListProperty.addAll(plantListModel.getPlantList(DEFAULT_HARDINESS_ZONE));
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
}
fillListViewWithData(plantList);
list_plants.itemsProperty().bind(plantListProperty);
list_plants.getSelectionModel().selectedItemProperty()
.addListener((observable, oldPlant, newPlant) -> {
if (newPlant != null) {
img_plant.setImage(newPlant.image());
}
});
description_plant.setText("");
saveToMyPlant_button.setDisable(true);
@@ -109,16 +123,12 @@ public class PlantsController implements Initializable {
lookForSelectedListEntry();
}
/**
* update the ListView according to the plant list provided
* Entry in ListView is plant name
* @param list plantList which fill the ListView
*/
private void fillListViewWithData(List<Plant> list) {
clearListView();
for (Plant plant : list) {
list_plants.getItems().add(plant);
}
private void centerImage() {
//img_plant.setX(0.3);
//img_plant.setX(-100);
}
private void setListCellFactory() {
list_plants.setCellFactory(param -> new ListCell<Plant>() {
@Override
protected void updateItem(Plant plant, boolean empty) {
@@ -133,6 +143,19 @@ 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
*/
private void fillListViewWithData(List<Plant> list) {
clearListView();
for (Plant plant : list) {
list_plants.getItems().add(plant);
}
}
private void viewFilteredListByFilters() {
boolean springValue = spring_filter.isSelected();
boolean sommerValue = sommer_filter.isSelected();
@@ -145,8 +168,14 @@ public class PlantsController implements Initializable {
private void viewFilteredListBySearch(String query) {
//ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, <predicate>))
//List<Plant> plantList = new LinkedList<>();
//fillListViewWithData(plantList);
try {
List<Plant> filteredPlants = plantListModel.getFilteredPlantListByString(DEFAULT_HARDINESS_ZONE, query);
clearListView();
plantListProperty.addAll(filteredPlants);
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
}
}
private void createFilterHardinessZone() {
@@ -181,6 +210,6 @@ public class PlantsController implements Initializable {
* clears the ListView of entries
*/
private void clearListView() {
list_plants.getItems().clear();
plantListProperty.clear();
}
}