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

View File

@ -10,10 +10,16 @@ import javafx.scene.layout.AnchorPane;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class MainFXMLController implements Initializable { public class MainFXMLController implements Initializable {
/**
* Caching the panes
*/
private final Map<String, Node> panes = new HashMap<>();
@FXML @FXML
private Button home_button; private Button home_button;
@ -63,12 +69,17 @@ public class MainFXMLController implements Initializable {
*/ */
public void loadPane(String fxmlFile) throws IOException { public void loadPane(String fxmlFile) throws IOException {
//ToDo HGrow and VGrow of new node //ToDo HGrow and VGrow of new node
Node node; Node node = panes.get(fxmlFile);
FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); System.out.println(node);
node = (Node)loader.load(); if (node == null) {
if(fxmlFile.equals("MyPlants.fxml")) { FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile)));
MyPlantsController myPlantsController = loader.getController(); node = loader.load();
myPlantsController.getMainController(this); panes.put(fxmlFile, node);
if(fxmlFile.equals("MyPlants.fxml")) {
MyPlantsController myPlantsController = loader.getController();
myPlantsController.getMainController(this);
}
} }
mainPane.getChildren().setAll(node); mainPane.getChildren().setAll(node);
} }

View File

@ -4,19 +4,23 @@ import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.plantList.PlantListModel; 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 javafx.application.Platform;
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;
import javafx.geometry.Bounds;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.InputMethodEvent; import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox; 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;
@ -25,6 +29,9 @@ 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;
// TODO: move to model
private final ListProperty<Plant> plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@FXML @FXML
private CheckBox autum_filter; private CheckBox autum_filter;
@ -85,7 +92,7 @@ public class PlantsController implements Initializable {
} }
@FXML @FXML
void searchForPlant(InputMethodEvent event) { void searchForPlant(KeyEvent event) {
viewFilteredListBySearch(search_plants.getText()); viewFilteredListBySearch(search_plants.getText());
} }
@ -94,13 +101,20 @@ public class PlantsController implements Initializable {
*/ */
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
List<Plant> plantList = new LinkedList<>(); setListCellFactory();
try { try {
plantList = plantListModel.getPlantList(DEFAULT_HARDINESS_ZONE); plantListProperty.addAll(plantListModel.getPlantList(DEFAULT_HARDINESS_ZONE));
} catch (HardinessZoneNotSetException | IOException e) { } catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace(); 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(""); description_plant.setText("");
saveToMyPlant_button.setDisable(true); saveToMyPlant_button.setDisable(true);
@ -109,16 +123,12 @@ public class PlantsController implements Initializable {
lookForSelectedListEntry(); lookForSelectedListEntry();
} }
/** private void centerImage() {
* update the ListView according to the plant list provided
* Entry in ListView is plant name //img_plant.setX(0.3);
* @param list plantList which fill the ListView //img_plant.setX(-100);
*/ }
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) {
@ -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() { private void viewFilteredListByFilters() {
boolean springValue = spring_filter.isSelected(); boolean springValue = spring_filter.isSelected();
boolean sommerValue = sommer_filter.isSelected(); boolean sommerValue = sommer_filter.isSelected();
@ -145,8 +168,14 @@ public class PlantsController implements Initializable {
private void viewFilteredListBySearch(String query) { private void viewFilteredListBySearch(String query) {
//ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, <predicate>)) //ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, <predicate>))
//List<Plant> plantList = new LinkedList<>(); try {
//fillListViewWithData(plantList); List<Plant> filteredPlants = plantListModel.getFilteredPlantListByString(DEFAULT_HARDINESS_ZONE, query);
clearListView();
plantListProperty.addAll(filteredPlants);
} catch (HardinessZoneNotSetException | IOException e) {
e.printStackTrace();
}
} }
private void createFilterHardinessZone() { private void createFilterHardinessZone() {
@ -181,6 +210,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();
} }
} }

View File

@ -33,12 +33,12 @@ public class PlantListModel {
setDefaultZone(); setDefaultZone();
} }
public PlantListModel(PlantDatabase plantDatabase){ public PlantListModel(PlantDatabase plantDatabase) {
this.plantDatabase = plantDatabase; this.plantDatabase = plantDatabase;
setDefaultZone(); setDefaultZone();
} }
private void setDefaultZone(){ private void setDefaultZone() {
currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Config currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Config
} }
@ -52,6 +52,7 @@ public class PlantListModel {
/** /**
* Method to get actual Plant List in alphabetic Order * Method to get actual Plant List in alphabetic Order
*
* @return actual Plant List in alphabetic Order * @return actual Plant List in alphabetic Order
*/ */
public List<Plant> getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException { public List<Plant> getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException {
@ -62,10 +63,11 @@ public class PlantListModel {
/** /**
* Method to get the actual Plant list in custom Order * Method to get the actual Plant list in custom Order
* @param zone selected hardiness zone *
* @param zone selected hardiness zone
* @param comparator comparator to sort the list * @param comparator comparator to sort the list
* @return sorted list with plants in the given hardiness zone * @return sorted list with plants in the given hardiness zone
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/ */
public List<Plant> getSortedPlantList(HardinessZone zone, Comparator<Plant> comparator) throws HardinessZoneNotSetException, IOException { public List<Plant> getSortedPlantList(HardinessZone zone, Comparator<Plant> comparator) throws HardinessZoneNotSetException, IOException {
@ -75,10 +77,11 @@ public class PlantListModel {
/** /**
* Method to get Filtered plant list * Method to get Filtered plant list
*
* @param predicate predicate to filter the list * @param predicate predicate to filter the list
* @param zone selected hardiness zone * @param zone selected hardiness zone
* @return filterd list with plants in the hardinness zone * @return filterd list with plants in the hardinness zone
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/ */
public List<Plant> getFilteredPlantList(HardinessZone zone, Predicate<Plant> predicate) throws HardinessZoneNotSetException, IOException { public List<Plant> getFilteredPlantList(HardinessZone zone, Predicate<Plant> predicate) throws HardinessZoneNotSetException, IOException {
@ -88,10 +91,11 @@ public class PlantListModel {
/** /**
* Method to get Filtered plant list by id by exact match * Method to get Filtered plant list by id by exact match
*
* @param zone selected hardiness zone * @param zone selected hardiness zone
* @param id id of plant * @param id id of plant
* @return if id doesn't exist: empty List, else list with 1 plant entry. * @return if id doesn't exist: empty List, else list with 1 plant entry.
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/ */
public List<Plant> getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException { public List<Plant> getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException {
@ -102,62 +106,63 @@ public class PlantListModel {
} }
/** /**
* * @param zone selected hardiness zone
* @param zone selected hardiness zone
* @param searchString the string to search plant List, set '#' as first char the search by id. * @param searchString the string to search plant List, set '#' as first char the search by id.
* @return List of plants found in Plant List which contain the search String in the name or description * @return List of plants found in Plant List which contain the search String in the name or description
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
public List<Plant> getFilteredPlantListByString(HardinessZone zone, String searchString) throws HardinessZoneNotSetException, IOException { public List<Plant> getFilteredPlantListByString(HardinessZone zone, String searchString) throws HardinessZoneNotSetException, IOException {
if(searchString.length() == 0){ if (searchString.length() == 0) {
return getPlantList(zone); return getPlantList(zone);
} else if(searchString.charAt(0) == '#') { } else if (searchString.charAt(0) == '#') {
try { try {
return getFilteredPlantListById(zone, Long.parseLong(searchString.substring(1))); return getFilteredPlantListById(zone, Long.parseLong(searchString.substring(1)));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return new ArrayList<>(); return new ArrayList<>();
} }
} else { } else {
return getFilteredPlantList(zone, plant -> plant.name().contains(searchString) || plant.description().contains(searchString)); String caseInsensitiveSearchString = searchString.toLowerCase();
return getFilteredPlantList(zone, plant ->
plant.name().toLowerCase().contains(caseInsensitiveSearchString) ||
plant.description().toLowerCase().contains(caseInsensitiveSearchString)
);
} }
} }
/** /**
*
* @param type GrowPhaseType to filter * @param type GrowPhaseType to filter
* @param zone selected hardiness zone * @param zone selected hardiness zone
* @param from the earliest date to for the filter * @param from the earliest date to for the filter
* @param to the lastest date for the filter * @param to the lastest date for the filter
* @return List of Plants with selected saison * @return List of Plants with selected saison
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private List<Plant> getFilteredPlantListBySaison(GrowthPhaseType type, HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { private List<Plant> getFilteredPlantListBySaison(GrowthPhaseType type, 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) && growthPhase.type() == type)); return getFilteredPlantList(zone, plant -> plant.lifecycle().stream().anyMatch(growthPhase -> growthPhase.startDate().compareTo(from) >= 0 && (growthPhase.startDate().compareTo(to) <= 0) && growthPhase.type() == type));
} }
/** /**
*
* @param zone selected hardiness zone * @param zone selected hardiness zone
* @param from the earliest date to for the filter * @param from the earliest date to for the filter
* @param to the lastest date for the filter * @param to the lastest date for the filter
* @return List of Plants with selected saison * @return List of Plants with selected saison
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
public List<Plant> getFilteredPlantListByPlantingSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { public List<Plant> getFilteredPlantListByPlantingSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException {
return getFilteredPlantListBySaison(GrowthPhaseType.PLANT, zone, from, to); return getFilteredPlantListBySaison(GrowthPhaseType.PLANT, zone, from, to);
} }
/** /**
*
* @param zone selected hardiness zone * @param zone selected hardiness zone
* @param from the earliest date to for the filter * @param from the earliest date to for the filter
* @param to the lastest date for the filter * @param to the lastest date for the filter
* @return List of Plants with selected saison * @return List of Plants with selected saison
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
public List<Plant> getFilteredPlantListByHarvestSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { public List<Plant> getFilteredPlantListByHarvestSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException {
return getFilteredPlantListBySaison(GrowthPhaseType.HARVEST, zone, from, to); return getFilteredPlantListBySaison(GrowthPhaseType.HARVEST, zone, from, to);

View File

@ -30,7 +30,7 @@
<Font name="System Bold" size="30.0" /> <Font name="System Bold" size="30.0" />
</font> </font>
</Label> </Label>
<TextField fx:id="search_plants" onInputMethodTextChanged="#searchForPlant" promptText="Search for Plant Name" /> <TextField fx:id="search_plants" onKeyTyped="#searchForPlant" promptText="Search for Plant Name" />
<HBox alignment="CENTER_LEFT" prefHeight="480.0" prefWidth="881.0" VBox.vgrow="ALWAYS"> <HBox alignment="CENTER_LEFT" prefHeight="480.0" prefWidth="881.0" VBox.vgrow="ALWAYS">
<children> <children>
<ListView fx:id="list_plants" maxWidth="1.7976931348623157E308" prefHeight="497.0" prefWidth="580.0" HBox.hgrow="ALWAYS" /> <ListView fx:id="list_plants" maxWidth="1.7976931348623157E308" prefHeight="497.0" prefWidth="580.0" HBox.hgrow="ALWAYS" />