Merge branch 'dev' into feature_controllerPlantList_M2

This commit is contained in:
gulerdav 2022-10-31 13:05:49 +01:00 committed by GitHub Enterprise
commit 3afeb8a22d
5 changed files with 57 additions and 30 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, AnchorPane> panes = new HashMap<>();
@FXML @FXML
private Button home_button; private Button home_button;
@ -62,12 +68,17 @@ public class MainFXMLController implements Initializable {
* @throws IOException exception when file does not exist * @throws IOException exception when file does not exist
*/ */
public void loadPane(String fxmlFile) throws IOException { public void loadPane(String fxmlFile) throws IOException {
AnchorPane anchorPane;
FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); AnchorPane anchorPane = panes.get(fxmlFile);
anchorPane = loader.load(); if (anchorPane == null) {
if(fxmlFile.equals("MyPlants.fxml")) { FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile)));
MyPlantsController myPlantsController = loader.getController(); anchorPane = loader.load();
myPlantsController.getMainController(this); panes.put(fxmlFile, anchorPane);
if(fxmlFile.equals("MyPlants.fxml")) {
MyPlantsController myPlantsController = loader.getController();
myPlantsController.getMainController(this);
}
} }
mainPane.getChildren().setAll(anchorPane); mainPane.getChildren().setAll(anchorPane);
anchorPane.prefWidthProperty().bind(mainPane.widthProperty()); anchorPane.prefWidthProperty().bind(mainPane.widthProperty());

View File

@ -5,6 +5,7 @@ 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.application.Platform;
import javafx.beans.property.ListProperty; import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty; import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
@ -14,9 +15,11 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Bounds;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import java.io.IOException; import java.io.IOException;
@ -29,6 +32,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;
// TODO: move to model
private final ListProperty<Plant> plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private final ListProperty<Plant> plantListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
@FXML @FXML

View File

@ -6,9 +6,11 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonDeserializer;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
public class PlantImageDeserializer extends JsonDeserializer<Image> { public class PlantImageDeserializer extends JsonDeserializer<Image> {
@ -18,9 +20,9 @@ public class PlantImageDeserializer extends JsonDeserializer<Image> {
Image result = null; Image result = null;
URL imageUrl = PlantDatabase.class.getResource(String.format("images/%s", parser.getText())); URL imageUrl = PlantDatabase.class.getResource(String.format("images/%s", parser.getText()));
if (imageUrl != null) { if (imageUrl != null) {
try (InputStream is = new FileInputStream(imageUrl.getFile())) { try (InputStream is = new FileInputStream(new File(imageUrl.toURI()))) {
result = new Image(is); result = new Image(is);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException | URISyntaxException e) {
// TODO: Log // TODO: Log
e.printStackTrace(); e.printStackTrace();
System.err.printf("Cannot find Image \"%s\"\n", imageUrl.getFile()); System.err.printf("Cannot find Image \"%s\"\n", imageUrl.getFile());

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,60 +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.charAt(0) == '#') { if (searchString.length() == 0) {
return getPlantList(zone);
} 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

@ -173,6 +173,8 @@ class PlantListModelTest {
assertEquals(examplePlantList.get(1), plantListResult.get(0)); assertEquals(examplePlantList.get(1), plantListResult.get(0));
plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "apple"); plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "apple");
assertEquals(0, plantListResult.size()); assertEquals(0, plantListResult.size());
plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "");
assertEquals(3, plantListResult.size());
} }
@Test @Test