diff --git a/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java b/src/main/java/ch/zhaw/gartenverwaltung/CropDetailController.java index 7ec8652..bbe7cea 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); } /** @@ -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"); @@ -278,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); } @@ -300,4 +308,56 @@ 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); + controller.initSaveButton((Button) dialogPane.lookupButton(save)); + + 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); + } + }); + } + } + + 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/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 new file mode 100644 index 0000000..a53c240 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/TextFieldFormularController.java @@ -0,0 +1,32 @@ +package ch.zhaw.gartenverwaltung; + +import javafx.fxml.FXML; +import javafx.scene.control.Button; +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(); + } + + public void initSaveButton(Button button) { + button.disableProperty().bind(text_area.textProperty().isEmpty()); + } +} 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/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); 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..c02dc37 --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/TextFieldFormular.fxml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + 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