update my garden from VBox to ListView
This commit is contained in:
parent
7920bdff28
commit
2963872237
|
@ -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<Crop> 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<Crop> 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.");
|
||||
|
||||
|
|
|
@ -3,11 +3,12 @@
|
|||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.ListView?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?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>
|
||||
<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>
|
||||
|
@ -19,7 +20,7 @@
|
|||
<Insets bottom="10.0" />
|
||||
</VBox.margin>
|
||||
</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">
|
||||
<VBox.margin>
|
||||
<Insets top="10.0" />
|
||||
|
|
Loading…
Reference in New Issue