update my garden from VBox to ListView

This commit is contained in:
giavaphi 2022-11-20 23:38:24 +01:00
parent 7920bdff28
commit 2963872237
2 changed files with 42 additions and 31 deletions

View File

@ -13,16 +13,12 @@ import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Alert; import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority; import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Modality; import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
@ -43,9 +39,9 @@ public class MyGardenController {
@FXML @FXML
public AnchorPane myGardenRoot; public AnchorPane myGardenRoot;
@FXML @FXML
private VBox myPlants_vbox;
@FXML
private Button addPlant_button; private Button addPlant_button;
@FXML
private ListView<Crop> myGarden_listView;
/** /**
* initialize crop list * initialize crop list
@ -55,19 +51,10 @@ public class MyGardenController {
@AfterInject @AfterInject
@SuppressWarnings("unused") @SuppressWarnings("unused")
public void init() { public void init() {
garden.getPlantedCrops().addListener((observable, oldValue, newValue) -> { System.out.println("once");
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);
}
setIconToButton(addPlant_button, "addIcon.png"); 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")); myGardenRoot.fireEvent(new ChangeViewEvent(ChangeViewEvent.CHANGE_MAIN_VIEW, "Plants.fxml"));
} }
private void createPlantView(List<Crop> crops) throws HardinessZoneNotSetException, IOException { /**
myPlants_vbox.getChildren().clear(); * set cell factory to load {@link HBox} as list view content
for (Crop crop : crops) { */
HBox hBox = createPlantView(crop); private void setCellFactory() {
myPlants_vbox.getChildren().add(hBox); 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 //ToDo add better design
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(); Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get();
HBox hBox = new HBox(10); HBox hBox = new HBox(10);
@ -103,8 +113,8 @@ public class MyGardenController {
label.setMaxWidth(2000); label.setMaxWidth(2000);
HBox.setHgrow(label, Priority.ALWAYS); HBox.setHgrow(label, Priority.ALWAYS);
Button details = new Button(""); Button details = new Button();
Button delete = new Button(""); Button delete = new Button();
setIconToButton(details, "detailsIcon.png"); setIconToButton(details, "detailsIcon.png");
setIconToButton(delete, "deleteIcon.png"); setIconToButton(delete, "deleteIcon.png");
details.setOnAction(getGoToCropDetailEvent(crop)); details.setOnAction(getGoToCropDetailEvent(crop));
@ -173,7 +183,7 @@ public class MyGardenController {
private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException { private void showConfirmation(Crop crop) throws IOException, HardinessZoneNotSetException {
Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get(); Plant plant = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get();
Alert alert = new Alert(Alert.AlertType.CONFIRMATION); 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.setHeaderText("Are you sure want to delete this Crop?");
alert.setContentText("Deleting this crop will remove all associated tasks from your schedule."); alert.setContentText("Deleting this crop will remove all associated tasks from your schedule.");

View File

@ -3,11 +3,12 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="655.0" prefWidth="1175.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.gartenverwaltung.MyGardenController" fx:id="myGardenRoot"> <AnchorPane fx:id="myGardenRoot" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="655.0" prefWidth="1175.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.gartenverwaltung.MyGardenController">
<children> <children>
<VBox layoutY="49.0" prefHeight="655.0" prefWidth="1175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <VBox layoutY="49.0" prefHeight="655.0" prefWidth="1175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
@ -19,7 +20,7 @@
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</VBox.margin> </VBox.margin>
</Label> </Label>
<VBox fx:id="myPlants_vbox" maxWidth="1.7976931348623157E308" prefHeight="200.0" prefWidth="100.0" VBox.vgrow="ALWAYS" /> <ListView fx:id="myGarden_listView" maxHeight="1.7976931348623157E308" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
<Button fx:id="addPlant_button" mnemonicParsing="false" onAction="#addPlant" prefHeight="45.0" prefWidth="155.0" text="Add new Plant"> <Button fx:id="addPlant_button" mnemonicParsing="false" onAction="#addPlant" prefHeight="45.0" prefWidth="155.0" text="Add new Plant">
<VBox.margin> <VBox.margin>
<Insets top="10.0" /> <Insets top="10.0" />