package ch.zhaw.gartenverwaltung; 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.KeyEvent; import javafx.scene.layout.VBox; import java.io.IOException; import java.net.URL; import java.util.List; import java.util.ResourceBundle; public class PlantsController implements Initializable { private final PlantListModel plantListModel = new PlantListModel(); 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; @FXML private VBox climate_zones; @FXML private Label description_plant; @FXML private ImageView img_plant; @FXML private ListView list_plants; @FXML private Button saveToMyPlant_button; @FXML private TextField search_plants; @FXML private CheckBox sommer_filter; @FXML private CheckBox spring_filter; @FXML private CheckBox winter_filter; @FXML void filterAutum(ActionEvent event) { //ToDo } @FXML void filterSommer(ActionEvent event) { //ToDo } @FXML void filterSpring(ActionEvent event) { //ToDo } @FXML void filterWinter(ActionEvent event) { //ToDo } @FXML void saveToMyPlant(ActionEvent event) { //ToDo model save selectedPlant to mySelectedPlant(IO) } @FXML void searchForPlant(KeyEvent event) { viewFilteredListBySearch(search_plants.getText()); } /** * {@inheritDoc} */ @Override public void initialize(URL url, ResourceBundle resourceBundle) { setListCellFactory(); try { plantListProperty.addAll(plantListModel.getPlantList(DEFAULT_HARDINESS_ZONE)); } catch (HardinessZoneNotSetException | IOException e) { e.printStackTrace(); } 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); createFilterHardinessZone(); lookForSelectedListEntry(); } 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) { super.updateItem(plant, empty); if (empty || plant == null || plant.name() == null) { setText(null); } else { setText(plant.name()); } } }); } /** * 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(); boolean autumValue = autum_filter.isSelected(); boolean winterValue = winter_filter.isSelected(); //ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, )) //List plantList = new LinkedList<>(); //fillListViewWithData(plantList); } private void viewFilteredListBySearch(String query) { //ToDo getFilteredPlantList with (plantListModel.getFilteredPlantList(DEFAULT_HARDINESS_ZONE, )) try { List filteredPlants = plantListModel.getFilteredPlantListByString(DEFAULT_HARDINESS_ZONE, query); clearListView(); plantListProperty.addAll(filteredPlants); } catch (HardinessZoneNotSetException | IOException e) { e.printStackTrace(); } } private void createFilterHardinessZone() { //ToDo create radioList of hardinessZone in VBox climate_zones } /** * observes changes in the selectedProperty of ListView and updates the description label */ private void lookForSelectedListEntry() { list_plants.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, Plant oldValue, Plant newValue) { //ToDo if(newValue != null) { selectedPlant = newValue; description_plant.setText(selectedPlant.description()); saveToMyPlant_button.setDisable(false); //update img plant } else { selectedPlant = null; description_plant.setText(""); saveToMyPlant_button.setDisable(true); //update img when null placeholder PNG } } }); } /** * clears the ListView of entries */ private void clearListView() { plantListProperty.clear(); } }