diff --git a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java index 56bffeb..4ca9ddd 100644 --- a/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java +++ b/src/main/java/de/hitec/nhplus/medication/AllMedicationController.java @@ -95,7 +95,7 @@ public class AllMedicationController { Stage stage = new Stage(); MedicationModalController controller = loader.getController(); - controller.initialize(); + controller.initialize(null); stage.setScene(scene); stage.setTitle("NHPlus - Neues Medikament"); diff --git a/src/main/java/de/hitec/nhplus/medication/Ingredient.java b/src/main/java/de/hitec/nhplus/medication/Ingredient.java index bbf2f36..49f93f6 100644 --- a/src/main/java/de/hitec/nhplus/medication/Ingredient.java +++ b/src/main/java/de/hitec/nhplus/medication/Ingredient.java @@ -17,7 +17,7 @@ public class Ingredient { } public String getName() { - return name.get(); + return name.getValue(); } public SimpleStringProperty nameProperty() { diff --git a/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java b/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java new file mode 100644 index 0000000..a69eb64 --- /dev/null +++ b/src/main/java/de/hitec/nhplus/medication/IngredientListCell.java @@ -0,0 +1,92 @@ +package de.hitec.nhplus.medication; + +import javafx.beans.value.ObservableValue; +import javafx.geometry.Insets; +import javafx.scene.control.Button; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.scene.control.TextField; +import javafx.scene.layout.BorderPane; +import javafx.scene.text.Text; + +public class IngredientListCell extends ListCell { + private final TextField textField; + private final Button deleteButton; + + private static final double BUILTIN_BORDER_WIDTH = 1; + private static final double CELL_SPACING = 4; + private static final double CELL_PADDING = 4; + private static final double BUTTON_PADDING_X = 8; + private static final double BUTTON_PADDING_Y = 4; + private final double totalSpacing; + + public IngredientListCell() { + this.setPadding(new Insets(CELL_PADDING)); + + textField = new TextField(); + textField.setPromptText("Inhaltsstoff"); + textField.textProperty().addListener(this::onTextFieldUpdate); + + deleteButton = new Button("-"); + deleteButton.setPadding(new Insets( + BUTTON_PADDING_Y, + BUTTON_PADDING_X, + BUTTON_PADDING_Y, + BUTTON_PADDING_X + )); + deleteButton.setOnAction(event -> getListView().getItems().remove(this)); + + // Calculate Delete Button Width + Text textNode = new Text(deleteButton.getText()); + textNode.setFont(deleteButton.getFont()); + double calculatedDeleteButtonWidth = textNode.getLayoutBounds().getWidth() + BUTTON_PADDING_X * 2; + + totalSpacing = BUILTIN_BORDER_WIDTH * 2 // List View + + CELL_PADDING * 2 + + CELL_SPACING + + calculatedDeleteButtonWidth; + } + + @Override + protected void updateItem(Ingredient item, boolean empty) { + super.updateItem(item, empty); + if (empty || item == null) { + setGraphic(null); + } else { + String oldText = textField.getText(); + String newText = oldText != null && !oldText.isEmpty() ? oldText : item.getName(); + textField.setText(newText); + + BorderPane cellPane = new BorderPane(); + cellPane.setCenter(textField); + cellPane.setRight(deleteButton); + BorderPane.setMargin(deleteButton, new Insets(0, 0, 0, CELL_SPACING)); + setGraphic(cellPane); + } + } + + private void onTextFieldUpdate(ObservableValue observable, String oldValue, String newValue) { + commitEdit(new Ingredient(textField.getText())); + + textField.setMinWidth(getTextFieldRequiredWidth(textField)); + + ListView listView = getListView(); + double max = listView.lookupAll("*") + .stream() + .filter(node -> node instanceof IngredientListCell) + .mapToDouble(node -> getTextFieldRequiredWidth(((IngredientListCell) node).textField)) + .max() + .orElse(0); + + getListView().setMinWidth(max + totalSpacing); + } + + private double getTextFieldRequiredWidth(TextField textField) { + Text textNode = new Text(textField.getText()); + textNode.setFont(textField.getFont()); + return textNode.getLayoutBounds().getWidth() + + textField.getPadding().getLeft() + + textField.getPadding().getRight() + + BUILTIN_BORDER_WIDTH * 2; + } +} diff --git a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java index 50585f0..cb41700 100644 --- a/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java +++ b/src/main/java/de/hitec/nhplus/medication/MedicationModalController.java @@ -1,8 +1,52 @@ package de.hitec.nhplus.medication; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.scene.control.ListView; +import java.util.ArrayList; + public class MedicationModalController { - public void initialize(){ + @FXML + public ListView listViewIngredients; + + private Medication medication; + private final ObservableList ingredients = FXCollections.observableArrayList(); + private AllMedicationController controller; + + @FXML + public void initialize(Medication medication) { + this.medication = medication != null ? medication : new Medication( + "", + "", + new ArrayList<>(), + "", + "", + 0 + ); + + listViewIngredients.setCellFactory(cellData -> new IngredientListCell()); + listViewIngredients.setItems(ingredients); + + showData(); + } + + private void showData(){ + ingredients.setAll(medication.getIngredients()); + } + + @FXML + public void handleSave() { + } + + @FXML + public void handleCancel() { + } + + @FXML + public void handleAddIngredient() { + ingredients.add(new Ingredient(null)); } } diff --git a/src/main/resources/de/hitec/nhplus/medication/MedicationModal.fxml b/src/main/resources/de/hitec/nhplus/medication/MedicationModal.fxml index 792550a..2b22c77 100644 --- a/src/main/resources/de/hitec/nhplus/medication/MedicationModal.fxml +++ b/src/main/resources/de/hitec/nhplus/medication/MedicationModal.fxml @@ -1,18 +1,130 @@ + + - - - + + + + + + + + + + + + + + + + +
-