From 8c3925ea1a0b5b4b6088cb70364d5b379a203674 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Mon, 31 Jan 2022 12:42:31 +0100 Subject: [PATCH] Refactor --- .../CreateEventController.java | 2 +- .../{main => events}/EditEventController.java | 2 +- .../src/main/java/main/MainApplication.java | 33 +++++ .../src/main/java/main/MainController.java | 122 ++++-------------- .../src/main/java/main/OptionController.java | 51 +++----- client/app/src/main/java/ui/EventPane.java | 97 ++++++++++++++ .../src/main/resources/main/create-event.fxml | 2 +- .../src/main/resources/main/edit-event.fxml | 2 +- 8 files changed, 175 insertions(+), 136 deletions(-) rename client/app/src/main/java/{main => events}/CreateEventController.java (99%) rename client/app/src/main/java/{main => events}/EditEventController.java (98%) create mode 100644 client/app/src/main/java/ui/EventPane.java diff --git a/client/app/src/main/java/main/CreateEventController.java b/client/app/src/main/java/events/CreateEventController.java similarity index 99% rename from client/app/src/main/java/main/CreateEventController.java rename to client/app/src/main/java/events/CreateEventController.java index ed86f69..e2d2b67 100644 --- a/client/app/src/main/java/main/CreateEventController.java +++ b/client/app/src/main/java/events/CreateEventController.java @@ -1,4 +1,4 @@ -package main; +package events; import com.jfoenix.controls.*; import helper.HttpRequestException; diff --git a/client/app/src/main/java/main/EditEventController.java b/client/app/src/main/java/events/EditEventController.java similarity index 98% rename from client/app/src/main/java/main/EditEventController.java rename to client/app/src/main/java/events/EditEventController.java index cce82d9..ca3833b 100644 --- a/client/app/src/main/java/main/EditEventController.java +++ b/client/app/src/main/java/events/EditEventController.java @@ -1,4 +1,4 @@ -package main; +package events; import helper.HttpRequestException; import container.DataController; diff --git a/client/app/src/main/java/main/MainApplication.java b/client/app/src/main/java/main/MainApplication.java index 4e514f0..2ed843c 100644 --- a/client/app/src/main/java/main/MainApplication.java +++ b/client/app/src/main/java/main/MainApplication.java @@ -2,15 +2,20 @@ package main; import config.Config; import config.ConfigLoader; +import helper.Tuple; import javafx.application.Application; +import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; +import javafx.scene.Node; import javafx.scene.Scene; +import javafx.stage.Modality; import javafx.stage.Stage; import container.DataController; import container.HttpRequest; import java.io.IOException; import java.util.Objects; +import java.util.function.Consumer; public class MainApplication extends Application { @Override @@ -70,4 +75,32 @@ public class MainApplication extends Application { public static void main(String[] args) { launch(); } + + public static void loadScene( + String title, + String fxml, + String css, + int width, + int height, + Consumer method + ) { + FXMLLoader fxmlLoader = new FXMLLoader( + MainApplication.class.getResource(fxml)); + try { + Scene scene = new Scene(fxmlLoader.load(), width, height); + scene.getStylesheets().add(Objects.requireNonNull( + MainApplication.class.getResource(css)).toExternalForm()); + Stage stage = new Stage(); + stage.setTitle(title); + stage.setScene(scene); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setResizable(false); + + if(method != null)method.accept(fxmlLoader); + + stage.showAndWait(); + } catch (IOException e) { + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/client/app/src/main/java/main/MainController.java b/client/app/src/main/java/main/MainController.java index e823492..2ac07fb 100644 --- a/client/app/src/main/java/main/MainController.java +++ b/client/app/src/main/java/main/MainController.java @@ -2,12 +2,13 @@ package main; import config.Config; import config.ConfigLoader; +import events.EditEventController; import ui.DayPane; +import ui.EventPane; import ui.SvgBtnCreator; import helper.HttpRequestException; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; -import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; @@ -125,21 +126,14 @@ public class MainController { } protected void onSettingBtnClick(){ - try{ - FXMLLoader fxmlLoader = new FXMLLoader( - MainApplication.class.getResource("option-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 650, 650); - scene.getStylesheets().add(Objects.requireNonNull( - MainApplication.class.getResource("option-view.css")).toExternalForm()); - Stage stage = new Stage(); - stage.setTitle("Einstellungen"); - stage.setScene(scene); - stage.initModality(Modality.APPLICATION_MODAL); - stage.setResizable(false); - stage.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); - } + MainApplication.loadScene( + "Einstellungen", + "option-view.fxml", + "option-view.css", + 650, + 600, + null + ); } protected void onLogoutBtnClick(ActionEvent event){ @@ -161,104 +155,38 @@ public class MainController { } private void addEvent(Event event) { - VBox vBox = new VBox(); - vBox.getStyleClass().add("event"); - vBox.setSpacing(5); + EventPane eventPane = new EventPane(event); + eventPane.getEditBtn().setOnAction(event1 -> MainApplication.loadScene( + "edit-event.fxml", + "create-event.css", + "Termin bearbeiten", + 600, + 600, + fxmlLoader -> { + EditEventController editEventController = fxmlLoader.getController(); + editEventController.setCurrentEvent(event); + } + )); - HBox btnHBox = new HBox(); - btnHBox.setAlignment(Pos.BOTTOM_RIGHT); - - Group svgDel = new Group( - SvgBtnCreator.createPath("M0 0h24v24H0z", "transparent", "transparent"), - SvgBtnCreator.createPath("M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z", - "white", "gray") - ); - Button deleteBtn = SvgBtnCreator.createBtn(svgDel, 24, "", "Löschen des Termins"); - - deleteBtn.getStyleClass().add("deleteEventBtn"); - deleteBtn.setOnAction(e -> { + eventPane.getDeleteBtn().setOnAction(e -> { DataController dataController = new DataController(); try { dataController.deleteEvent(event.getOwnerId(), event.getId(), event.getDate()); } catch (HttpRequestException ex) { - ex.printStackTrace(); + new Alert(Alert.AlertType.ERROR, ex.getMessage()).showAndWait(); } updateEvents(); }); - Group svgEdit = new Group( - SvgBtnCreator.createPath("M0 0h24v24H0z", "transparent", "transparent"), - SvgBtnCreator.createPath("M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z", - "white", "gray") - ); - Button editBtn = SvgBtnCreator.createBtn(svgEdit, 24, "", "Bearbeiten des Termins"); - editBtn.getStyleClass().add("editEventBtn"); - editBtn.setOnAction(event1 -> { - try { - FXMLLoader fxmlLoader = new FXMLLoader( - MainApplication.class.getResource("edit-event.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 650, 650); - scene.getStylesheets().add(Objects.requireNonNull( - MainApplication.class.getResource("create-event.css")).toExternalForm()); - Stage stage = new Stage(); - stage.setTitle("Termin bearbeiten"); - stage.setScene(scene); - stage.initModality(Modality.APPLICATION_MODAL); - stage.setResizable(false); - EditEventController editEventController = fxmlLoader.getController(); - editEventController.setCurrentEvent(event); - stage.showAndWait(); - updateEvents(); - } catch (IOException e) { - e.printStackTrace(); - } - }); - btnHBox.getChildren().add(editBtn); - btnHBox.getChildren().add(deleteBtn); - vBox.getChildren().add(btnHBox); - - Label nameLabel = new Label(event.getName()); - nameLabel.setWrapText(true); - vBox.getChildren().add(nameLabel); - - if (event.getStart() != null || event.getEnd() != null) { - String timeStr = (event.getStart() != null ? formatTime(event.getStart()) : "") - + (event.getEnd() != null ? " - " + formatTime(event.getEnd()) : ""); - Label timeLabel = new Label(timeStr); - vBox.getChildren().add(timeLabel); - } - - Label typeLabel = new Label("Wer: " + event.getOwnerName()); - vBox.getChildren().add(typeLabel); - - Label prioLabel = new Label("Priorität: " + event.getPriority()); - vBox.getChildren().add(prioLabel); - - if (event.isFullDay()) { - Label fullDayLabel = new Label("Dieser Termin bockiert den ganzen Tag!"); - fullDayLabel.setWrapText(true); - vBox.getChildren().add(fullDayLabel); - } - - LocalDateTime eventDate = event.getDate(); - int day = (int) Duration.between( weekStartDateTime.toLocalDate().atStartOfDay(), eventDate.toLocalDate().atStartOfDay()).toDays(); if (day >= 0 && day < 7) { - dayVBoxes[day].getChildren().add(vBox); + dayVBoxes[day].getChildren().add(eventPane); } } - private String formatTime(String time) { - String[] timeArr = time.split(":"); - if (timeArr.length > 2) { - return timeArr[0] + ":" + timeArr[1]; - } - return time; - } - private void setDates() { DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("LLLL yyyy"); DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("E dd.MM"); diff --git a/client/app/src/main/java/main/OptionController.java b/client/app/src/main/java/main/OptionController.java index 9d85042..9cf0d85 100644 --- a/client/app/src/main/java/main/OptionController.java +++ b/client/app/src/main/java/main/OptionController.java @@ -11,24 +11,18 @@ import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Node; -import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; -import javafx.stage.Modality; import javafx.stage.Stage; import container.DataController; import container.User; import users.EditUserController; -import java.awt.*; -import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.Optional; -import java.util.function.Consumer; public class OptionController { @@ -80,7 +74,14 @@ public class OptionController { } public void onCreateBtnClick(ActionEvent actionEvent) { - loadUserScene(actionEvent, "User erstellen", "../users/create-user.fxml", null); + MainApplication.loadScene( + "User erstellen", + "../users/create-user.fxml", + "../users/create-user.css", + 800, + 650, + null + ); } public void onUpdateBtnClick(ActionEvent actionEvent) { @@ -88,11 +89,16 @@ public class OptionController { if(editIndex < 0 || editIndex >= users.size()) return; - FXMLLoader fxmlLoader = loadUserScene( - actionEvent, + MainApplication.loadScene( "User bearbeiten", "../users/edit-user.fxml", - this::setUserAtController + "../users/create-user.css", + 800, + 650, + fxmlLoader -> { + EditUserController editUserController = fxmlLoader.getController(); + editUserController.setCurrentUser(users.get(editIndex)); + } ); } @@ -113,31 +119,6 @@ public class OptionController { } } - private FXMLLoader loadUserScene(ActionEvent actionEvent, String title, String fxml, Consumer method) { - FXMLLoader fxmlLoader = new FXMLLoader( - MainApplication.class.getResource(fxml)); - try { - Scene scene = new Scene(fxmlLoader.load(), 800, 650); - scene.getStylesheets().add(Objects.requireNonNull( - MainApplication.class.getResource("../users/create-user.css")).toExternalForm()); - Stage stage = new Stage(); - stage.setTitle(title); - stage.setScene(scene); - stage.initModality(Modality.APPLICATION_MODAL); - stage.setResizable(false); - - if(method != null)method.accept(fxmlLoader); - - stage.showAndWait(); - Stage stageOld = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow(); - stageOld.close(); - } catch (IOException e) { - e.printStackTrace(); - e.printStackTrace(); - } - return fxmlLoader; - } - private void setUserAtController(FXMLLoader fxmlLoader){ int editIndex = comboBox.getSelectionModel().getSelectedIndex(); EditUserController editUserController = fxmlLoader.getController(); diff --git a/client/app/src/main/java/ui/EventPane.java b/client/app/src/main/java/ui/EventPane.java new file mode 100644 index 0000000..ae77e2c --- /dev/null +++ b/client/app/src/main/java/ui/EventPane.java @@ -0,0 +1,97 @@ +package ui; + +import container.DataController; +import container.Event; +import events.EditEventController; +import helper.HttpRequestException; +import javafx.fxml.FXMLLoader; +import javafx.geometry.Pos; +import javafx.scene.Group; +import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.stage.Modality; +import javafx.stage.Stage; +import main.MainApplication; + +import java.io.IOException; +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.Objects; + +public class EventPane extends VBox { + + private Button deleteBtn; + private Button editBtn; + + public EventPane(Event event) { + this.getStyleClass().add("event"); + this.setSpacing(5); + + HBox btnHBox = new HBox(); + btnHBox.setAlignment(Pos.BOTTOM_RIGHT); + + Group svgDel = new Group( + SvgBtnCreator.createPath("M0 0h24v24H0z", "transparent", "transparent"), + SvgBtnCreator.createPath("M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z", + "white", "gray") + ); + deleteBtn = SvgBtnCreator.createBtn(svgDel, 24, "", "Löschen des Termins"); + deleteBtn.getStyleClass().add("deleteEventBtn"); + + Group svgEdit = new Group( + SvgBtnCreator.createPath("M0 0h24v24H0z", "transparent", "transparent"), + SvgBtnCreator.createPath("M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z", + "white", "gray") + ); + editBtn = SvgBtnCreator.createBtn(svgEdit, 24, "", "Bearbeiten des Termins"); + editBtn.getStyleClass().add("editEventBtn"); + + btnHBox.getChildren().add(editBtn); + btnHBox.getChildren().add(deleteBtn); + this.getChildren().add(btnHBox); + + Label nameLabel = new Label(event.getName()); + nameLabel.setWrapText(true); + this.getChildren().add(nameLabel); + + if (event.getStart() != null || event.getEnd() != null) { + String timeStr = (event.getStart() != null ? formatTime(event.getStart()) : "") + + (event.getEnd() != null ? " - " + formatTime(event.getEnd()) : ""); + Label timeLabel = new Label(timeStr); + this.getChildren().add(timeLabel); + } + + Label typeLabel = new Label("Wer: " + event.getOwnerName()); + this.getChildren().add(typeLabel); + + Label prioLabel = new Label("Priorität: " + event.getPriority()); + this.getChildren().add(prioLabel); + + if (event.isFullDay()) { + Label fullDayLabel = new Label("Dieser Termin bockiert den ganzen Tag!"); + fullDayLabel.setWrapText(true); + this.getChildren().add(fullDayLabel); + } + } + + public Button getDeleteBtn() { + return deleteBtn; + } + + public Button getEditBtn() { + return editBtn; + } + + + private String formatTime(String time) { + String[] timeArr = time.split(":"); + if (timeArr.length > 2) { + return timeArr[0] + ":" + timeArr[1]; + } + return time; + } +} diff --git a/client/app/src/main/resources/main/create-event.fxml b/client/app/src/main/resources/main/create-event.fxml index e2798db..272fca2 100644 --- a/client/app/src/main/resources/main/create-event.fxml +++ b/client/app/src/main/resources/main/create-event.fxml @@ -8,7 +8,7 @@ + fx:controller="events.CreateEventController"> diff --git a/client/app/src/main/resources/main/edit-event.fxml b/client/app/src/main/resources/main/edit-event.fxml index 939dddb..922fe93 100644 --- a/client/app/src/main/resources/main/edit-event.fxml +++ b/client/app/src/main/resources/main/edit-event.fxml @@ -8,7 +8,7 @@ + fx:controller="events.EditEventController">