From 7a060be84aa7e8c2d93d363eb09b1d82c289fc06 Mon Sep 17 00:00:00 2001 From: David Guler Date: Mon, 31 Oct 2022 10:06:17 +0100 Subject: [PATCH 1/2] case insensitive serarch, images --- .../gartenverwaltung/MainFXMLController.java | 23 +++++-- .../gartenverwaltung/PlantsController.java | 67 +++++++++++++------ .../plantList/PlantListModel.java | 51 +++++++------- .../ch/zhaw/gartenverwaltung/Plants.fxml | 2 +- 4 files changed, 94 insertions(+), 49 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java b/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java index c3fe04d..99a1852 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MainFXMLController.java @@ -10,10 +10,16 @@ import javafx.scene.layout.AnchorPane; import java.io.IOException; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.ResourceBundle; public class MainFXMLController implements Initializable { + /** + * Caching the panes + */ + private final Map panes = new HashMap<>(); @FXML private Button home_button; @@ -63,12 +69,17 @@ public class MainFXMLController implements Initializable { */ public void loadPane(String fxmlFile) throws IOException { //ToDo HGrow and VGrow of new node - Node node; - FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); - node = (Node)loader.load(); - if(fxmlFile.equals("MyPlants.fxml")) { - MyPlantsController myPlantsController = loader.getController(); - myPlantsController.getMainController(this); + Node node = panes.get(fxmlFile); + System.out.println(node); + if (node == null) { + FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); + node = loader.load(); + panes.put(fxmlFile, node); + + if(fxmlFile.equals("MyPlants.fxml")) { + MyPlantsController myPlantsController = loader.getController(); + myPlantsController.getMainController(this); + } } mainPane.getChildren().setAll(node); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index a8c8d93..a3646fa 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -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 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 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 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() { @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 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, )) - //List plantList = new LinkedList<>(); - //fillListViewWithData(plantList); + try { + List 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(); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 8e8a1b8..ad02864 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -33,12 +33,12 @@ public class PlantListModel { setDefaultZone(); } - public PlantListModel(PlantDatabase plantDatabase){ + public PlantListModel(PlantDatabase plantDatabase) { this.plantDatabase = plantDatabase; setDefaultZone(); } - private void setDefaultZone(){ + private void setDefaultZone() { 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 + * * @return actual Plant List in alphabetic Order */ public List getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException { @@ -62,10 +63,11 @@ public class PlantListModel { /** * 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 * @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 */ public List getSortedPlantList(HardinessZone zone, Comparator comparator) throws HardinessZoneNotSetException, IOException { @@ -75,10 +77,11 @@ public class PlantListModel { /** * Method to get Filtered plant 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 - * @throws IOException If the database cannot be accessed + * @throws IOException If the database cannot be accessed * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ public List getFilteredPlantList(HardinessZone zone, Predicate predicate) throws HardinessZoneNotSetException, IOException { @@ -88,10 +91,11 @@ public class PlantListModel { /** * Method to get Filtered plant list by id by exact match + * * @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. - * @throws IOException If the database cannot be accessed + * @throws IOException If the database cannot be accessed * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ public List 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. * @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 IOException If the database cannot be accessed + * @throws IOException If the database cannot be accessed */ public List getFilteredPlantListByString(HardinessZone zone, String searchString) throws HardinessZoneNotSetException, IOException { - if(searchString.length() == 0){ + if (searchString.length() == 0) { return getPlantList(zone); - } else if(searchString.charAt(0) == '#') { + } else if (searchString.charAt(0) == '#') { try { return getFilteredPlantListById(zone, Long.parseLong(searchString.substring(1))); } catch (NumberFormatException e) { return new ArrayList<>(); } } 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 zone selected hardiness zone * @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 * @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 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)); } /** - * * @param zone selected hardiness zone * @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 * @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 getFilteredPlantListByPlantingSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { return getFilteredPlantListBySaison(GrowthPhaseType.PLANT, 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 + * @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 + * @throws IOException If the database cannot be accessed */ public List getFilteredPlantListByHarvestSaison(HardinessZone zone, MonthDay from, MonthDay to) throws HardinessZoneNotSetException, IOException { return getFilteredPlantListBySaison(GrowthPhaseType.HARVEST, zone, from, to); diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml index 0cb3c6d..9abee94 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/Plants.fxml @@ -30,7 +30,7 @@ - + From d0cef1fe8289aa60f7db602fa1f0756943afe985 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 12:43:13 +0100 Subject: [PATCH 2/2] fixed problems with file Path #43 --- .../zhaw/gartenverwaltung/json/PlantImageDeserializer.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java index e5611fc..f22cb35 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java @@ -6,9 +6,11 @@ import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import javafx.scene.image.Image; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; import java.net.URL; public class PlantImageDeserializer extends JsonDeserializer { @@ -18,9 +20,9 @@ public class PlantImageDeserializer extends JsonDeserializer { Image result = null; URL imageUrl = PlantDatabase.class.getResource(String.format("images/%s", parser.getText())); if (imageUrl != null) { - try (InputStream is = new FileInputStream(imageUrl.getFile())) { + try (InputStream is = new FileInputStream(new File(imageUrl.toURI()))) { result = new Image(is); - } catch (IllegalArgumentException e) { + } catch (IllegalArgumentException | URISyntaxException e) { // TODO: Log e.printStackTrace(); System.err.printf("Cannot find Image \"%s\"\n", imageUrl.getFile());