diff --git a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java index d7efaf4..b6270ca 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/MyGardenController.java @@ -13,16 +13,12 @@ import ch.zhaw.gartenverwaltung.types.Plant; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; -import javafx.scene.control.Alert; -import javafx.scene.control.Button; -import javafx.scene.control.ButtonType; -import javafx.scene.control.Label; +import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; -import javafx.scene.layout.VBox; import javafx.stage.Modality; import javafx.stage.Stage; @@ -43,9 +39,9 @@ public class MyGardenController { @FXML public AnchorPane myGardenRoot; @FXML - private VBox myPlants_vbox; - @FXML private Button addPlant_button; + @FXML + private ListView myGarden_listView; /** * initialize crop list @@ -55,19 +51,10 @@ public class MyGardenController { @AfterInject @SuppressWarnings("unused") public void init() { - garden.getPlantedCrops().addListener((observable, oldValue, newValue) -> { - try { - createPlantView(newValue); - } catch (HardinessZoneNotSetException | IOException e) { - LOG.log(Level.SEVERE, "Could not update view of croplist!", e); - } - }); - try { - createPlantView(garden.getPlantedCrops()); - } catch (HardinessZoneNotSetException | IOException e) { - LOG.log(Level.SEVERE, "Could not update view of croplist!", e); - } + System.out.println("once"); setIconToButton(addPlant_button, "addIcon.png"); + myGarden_listView.itemsProperty().bind(garden.getPlantedCrops()); + setCellFactory(); } /** @@ -78,15 +65,38 @@ public class MyGardenController { myGardenRoot.fireEvent(new ChangeViewEvent(ChangeViewEvent.CHANGE_MAIN_VIEW, "Plants.fxml")); } - private void createPlantView(List crops) throws HardinessZoneNotSetException, IOException { - myPlants_vbox.getChildren().clear(); - for (Crop crop : crops) { - HBox hBox = createPlantView(crop); - myPlants_vbox.getChildren().add(hBox); - } + /** + * set cell factory to load {@link HBox} as list view content + */ + private void setCellFactory() { + myGarden_listView.setCellFactory(param -> new ListCell<>() { + @Override + protected void updateItem(Crop crop, boolean empty) { + super.updateItem(crop, empty); + + if (empty || crop == null) { + setText(null); + setGraphic(null); + } else { + try { + setText(""); + setGraphic(createHBoxForListView(crop)); + } catch (HardinessZoneNotSetException | IOException e) { + LOG.log(Level.WARNING, "Could not get plant for Cell", e); + } + } + } + }); } - private HBox createPlantView(Crop crop) throws HardinessZoneNotSetException, IOException { + /** + * Creates and returns HBox of the crop + * @param crop {@link Crop} which is selected + * @return {@link HBox} of the {@link Crop} + * @throws HardinessZoneNotSetException exception + * @throws IOException exception + */ + private HBox createHBoxForListView(Crop crop) throws HardinessZoneNotSetException, IOException { //ToDo add better design Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(); HBox hBox = new HBox(10); @@ -103,8 +113,8 @@ public class MyGardenController { label.setMaxWidth(2000); HBox.setHgrow(label, Priority.ALWAYS); - Button details = new Button(""); - Button delete = new Button(""); + Button details = new Button(); + Button delete = new Button(); setIconToButton(details, "detailsIcon.png"); setIconToButton(delete, "deleteIcon.png"); details.setOnAction(getGoToCropDetailEvent(crop)); @@ -173,7 +183,7 @@ public class MyGardenController { private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException { Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(); Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - alert.setTitle("Delete" + plant.name()); + alert.setTitle("Delete " + plant.name()); alert.setHeaderText("Are you sure want to delete this Crop?"); alert.setContentText("Deleting this crop will remove all associated tasks from your schedule."); diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/MyGarden.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/MyGarden.fxml index 26ecaa7..3685395 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/MyGarden.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/MyGarden.fxml @@ -3,11 +3,12 @@ + - + @@ -19,7 +20,7 @@ - +