From 6b7a6f095d4448fc21a431aa844560748e6ea291 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Thu, 24 Nov 2022 23:18:14 +0100 Subject: [PATCH 1/4] dialog pane for location and area of crop --- .../CropDetailController.java | 40 +++++++++++++++++-- .../TextFieldFormularController.java | 27 +++++++++++++ .../gartenverwaltung/TextFieldFormular.fxml | 22 ++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java create mode 100644 src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 7ec8652..dd7e803 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -104,16 +104,16 @@ public class CropDetailController { * open dialog to set area */ @FXML - void setArea() { - + void setArea() throws IOException { + openTextFieldDialog("set Text Area", "Text Area", area_label.getText(), false); } /** * open dialog to set location */ @FXML - void setLocation() { - + void setLocation() throws IOException { + openTextFieldDialog("set Location", "Location", location_label.getText(), true); } /** @@ -300,4 +300,36 @@ public class CropDetailController { } } + + private void openTextFieldDialog(String title, String labelDescription, String value, boolean isLocation) throws IOException { + Dialog dialog = new Dialog<>(); + dialog.setTitle(title); + dialog.setHeaderText(title); + dialog.setResizable(false); + + DialogPane dialogPane = dialog.getDialogPane(); + + ButtonType save = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE); + dialogPane.getButtonTypes().addAll(save, ButtonType.CANCEL); + + if (appLoader.loadPaneToDialog("TextFieldFormular.fxml", dialogPane) instanceof TextFieldFormularController controller) { + controller.setDescription_label(labelDescription); + controller.setValueTextArea(value); + + dialog.setResultConverter(button -> button.equals(save) ? controller.getValue() : null); + + dialog.showAndWait() + .ifPresent(string -> { + if (isLocation) { + System.out.println(string); + //ToDo method to set location + location_label.setText(string); + } else { + System.out.println(string); + //ToDo method to set area of crop in garden + area_label.setText(string); + } + }); + } + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java b/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java new file mode 100644 index 0000000..ebbbfe2 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java @@ -0,0 +1,27 @@ +package ch.zhaw.gartenverwaltung; + +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; + +public class TextFieldFormularController { + + @FXML + private Label description_label; + + @FXML + private TextField text_area; + + + public void setDescription_label(String string) { + description_label.setText(string); + } + + public void setValueTextArea(String string) { + text_area.setText(string); + } + + public String getValue() { + return text_area.getText(); + } +} diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml new file mode 100644 index 0000000..2d4dfdc --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + From df19edb25a419a65b611feac9ef21156d50605a0 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Thu, 24 Nov 2022 23:55:54 +0100 Subject: [PATCH 2/4] delete tasks --- .../CropDetailController.java | 26 +++++++++++++++++++ .../gartenverwaltung/types/TaskTemplate.java | 4 +++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index dd7e803..90f52d8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -218,6 +218,7 @@ public class CropDetailController { setIconToButton(edit, "editIcon.png"); setIconToButton(delete, "deleteIcon.png"); edit.setOnAction(getEditTaskEvent(task)); + delete.setOnAction(deleteTask(task)); hBox.getChildren().addAll(taskName, taskDescription, edit, delete); return hBox; @@ -260,6 +261,12 @@ public class CropDetailController { }; } + private EventHandler deleteTask(Task task) { + return (event) -> { + showDeleteTask(task); + }; + } + private void createTaskDialog(boolean newTask, Task givenTask) throws IOException { Dialog dialog = new Dialog<>(); dialog.setTitle("Set Task"); @@ -332,4 +339,23 @@ public class CropDetailController { }); } } + + private void showDeleteTask(Task task) { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.setTitle("Delete " + task.getName()); + alert.setHeaderText("Are you sure want to delete this Task?"); + + alert.showAndWait() + .ifPresent(buttonType -> { + if (buttonType == ButtonType.OK) { + try { + gardenSchedule.removeTask(task); + setTaskListProperty(this.crop); + } catch (IOException e) { + // TODO: Show error alert + LOG.log(Level.SEVERE, "Could not remove crop.", e); + } + } + }); + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java b/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java index 2e471cb..7298fd2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/TaskTemplate.java @@ -48,6 +48,10 @@ public class TaskTemplate { public Task generateTask(LocalDate realStartDate, long cropId) { LocalDate endDate = relativeEndDate != null ? realStartDate.plusDays(relativeEndDate) : null; + if (interval == null) { + this.interval = 0; + } + return new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId) .withInterval(interval) .withEndDate(endDate); From 1c83fc469472c246188702dd71db8179df96c9d8 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Fri, 25 Nov 2022 02:00:13 +0100 Subject: [PATCH 3/4] add disable properties to dialog save buttons --- .../CropDetailController.java | 2 + .../TaskFormularController.java | 61 ++++++++++++++++--- .../TextFieldFormularController.java | 5 ++ 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 90f52d8..bbe7cea 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java @@ -285,6 +285,7 @@ public class CropDetailController { if (appLoader.loadPaneToDialog("TaskFormular.fxml", dialogPane) instanceof TaskFormularController controller) { controller.setCorp(this.crop); + controller.initSaveButton((Button) dialogPane.lookupButton(saveTask)); if (!newTask) { controller.setTaskValue(givenTask); } @@ -322,6 +323,7 @@ public class CropDetailController { if (appLoader.loadPaneToDialog("TextFieldFormular.fxml", dialogPane) instanceof TextFieldFormularController controller) { controller.setDescription_label(labelDescription); controller.setValueTextArea(value); + controller.initSaveButton((Button) dialogPane.lookupButton(save)); dialog.setResultConverter(button -> button.equals(save) ? controller.getValue() : null); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java b/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java index 938eea9..936bf09 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/TaskFormularController.java @@ -2,12 +2,14 @@ package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; +import javafx.beans.binding.Binding; +import javafx.beans.binding.Bindings; +import javafx.beans.binding.BooleanBinding; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.DateCell; -import javafx.scene.control.DatePicker; -import javafx.scene.control.TextArea; -import javafx.scene.control.TextField; +import javafx.scene.control.*; import javafx.util.Callback; import java.net.URL; @@ -56,7 +58,7 @@ public class TaskFormularController implements Initializable { } } - private Callback getDayCellFactory() { + private Callback getDayCellFactoryStartDate() { return (datePicker) -> new DateCell() { private final LocalDate today = LocalDate.now(); @@ -71,16 +73,61 @@ public class TaskFormularController implements Initializable { setDisable(false); setStyle("-fx-background-color: #32CD32;"); } + + if (end_datePicker.getValue() != null && item.compareTo(end_datePicker.getValue()) > 0) { + setDisable(true); + setStyle("-fx-background-color: #ffc0cb;"); + } } }; } + private Callback getDayCellFactoryEndDate() { + + return (datePicker) -> new DateCell() { + private final LocalDate today = LocalDate.now(); + + @Override + public void updateItem(LocalDate item, boolean empty) { + super.updateItem(item, empty); + setDisable(true); + setStyle("-fx-background-color: #ffc0cb;"); + + if (item.compareTo(today) > 0 && item.compareTo(crop.getStartDate()) > 0) { + setDisable(false); + setStyle("-fx-background-color: #32CD32;"); + } + + if (start_datePicker.getValue() != null && item.compareTo(start_datePicker.getValue()) < 0) { + setDisable(true); + setStyle("-fx-background-color: #ffc0cb;"); + } + } + }; + } + + public void initSaveButton(Button button) { + interval_field.textProperty().addListener((observable, oldValue, newValue) -> { + if (!newValue.matches("\\d*")) { + interval_field.setText(newValue.replaceAll("[^\\d]", "")); + } + }); + + button.disableProperty().bind(start_datePicker.valueProperty().isNull() + .or(end_datePicker.valueProperty().isNull()) + .or(taskName_field.textProperty().isEmpty()) + .or(description_area.textProperty().isEmpty()) + .or(interval_field.textProperty().isEmpty())); + + + } + @Override public void initialize(URL location, ResourceBundle resources) { - start_datePicker.setDayCellFactory(getDayCellFactory()); + start_datePicker.setDayCellFactory(getDayCellFactoryStartDate()); start_datePicker.setEditable(false); - end_datePicker.setDayCellFactory(getDayCellFactory()); + end_datePicker.setDayCellFactory(getDayCellFactoryEndDate()); end_datePicker.setEditable(false); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java b/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java index ebbbfe2..a53c240 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java @@ -1,6 +1,7 @@ package ch.zhaw.gartenverwaltung; import javafx.fxml.FXML; +import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; @@ -24,4 +25,8 @@ public class TextFieldFormularController { public String getValue() { return text_area.getText(); } + + public void initSaveButton(Button button) { + button.disableProperty().bind(text_area.textProperty().isEmpty()); + } } From e22cb0b24d3745d988b2f6fcd80e84b40ff2d1b4 Mon Sep 17 00:00:00 2001 From: giavaphi Date: Fri, 25 Nov 2022 02:29:07 +0100 Subject: [PATCH 4/4] init css file and bind with app --- .../java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java | 5 ++++- .../ch/zhaw/gartenverwaltung/TextFieldFormular.fxml | 3 ++- .../resources/ch/zhaw/gartenverwaltung/bootstrap/styles.css | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/ch/zhaw/gartenverwaltung/bootstrap/styles.css diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 59a4f7d..9c14010 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -73,7 +73,10 @@ public class AppLoader { public Object loadSceneToStage(String fxmlFile, Stage appendee) throws IOException { FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile))); Pane root = loader.load(); - appendee.setScene(new Scene(root)); + Scene scene = new Scene(root); + String css = Objects.requireNonNull(this.getClass().getResource("styles.css")).toExternalForm(); + appendee.setScene(scene); + scene.getStylesheets().add(css); Object controller = loader.getController(); annotationInject(controller); return controller; diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml b/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml index 2d4dfdc..c02dc37 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml +++ b/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml @@ -7,7 +7,8 @@ - + diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/bootstrap/styles.css b/src/main/resources/ch/zhaw/gartenverwaltung/bootstrap/styles.css new file mode 100644 index 0000000..f0fea6e --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/bootstrap/styles.css @@ -0,0 +1,6 @@ +.button{ + -fx-text-fill: rgb(49, 89, 23); + -fx-border-color: rgb(49, 89, 23); + -fx-border-radius: 5; + -fx-padding: 3 6 6 6; +} \ No newline at end of file