From 0b8f8f996d10db1be69a068756cda340812a53a5 Mon Sep 17 00:00:00 2001 From: Georg-Notebook Date: Fri, 19 Mar 2021 22:40:30 +0100 Subject: [PATCH] Logik von Controller -> ViewModel verschoben --- .idea/uiDesigner.xml | 124 ++++++++++++++ log/log4j/log.out | 2 + src/tourplaner/data/DbConnect.java | 2 - .../{business => data}/PostgresHelper.java | 5 +- .../{business => ui}/AlertHelper.java | 5 +- src/tourplaner/ui/TourplanerController.java | 95 ++--------- src/tourplaner/viewmodels/ViewModel.java | 157 +++++++++++++++--- 7 files changed, 279 insertions(+), 111 deletions(-) create mode 100644 .idea/uiDesigner.xml rename src/tourplaner/{business => data}/PostgresHelper.java (95%) rename src/tourplaner/{business => ui}/AlertHelper.java (98%) diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/log/log4j/log.out b/log/log4j/log.out index 655ab23..1c58c9a 100644 --- a/log/log4j/log.out +++ b/log/log4j/log.out @@ -8,3 +8,5 @@ 2021-03-18 23:05:00 ERROR ViewModel -> DelTour:33 - Keine Tour ausgewählt! 2021-03-18 23:11:45 ERROR ViewModel -> DelTour:33 - Keine Tour ausgewählt! 2021-03-18 23:14:55 ERROR ViewModel -> DelTour:33 - Keine Tour ausgewählt! +2021-03-19 11:56:22 ERROR ViewModel -> DelTour:33 - Keine Tour ausgewählt! +2021-03-19 22:35:19 ERROR ViewModel -> DelTour:33 - Keine Tour ausgewählt! diff --git a/src/tourplaner/data/DbConnect.java b/src/tourplaner/data/DbConnect.java index f57f59b..56a2888 100644 --- a/src/tourplaner/data/DbConnect.java +++ b/src/tourplaner/data/DbConnect.java @@ -1,7 +1,5 @@ package tourplaner.data; -import tourplaner.business.PostgresHelper; - import java.sql.Connection; import java.sql.Statement; import java.util.ArrayList; diff --git a/src/tourplaner/business/PostgresHelper.java b/src/tourplaner/data/PostgresHelper.java similarity index 95% rename from src/tourplaner/business/PostgresHelper.java rename to src/tourplaner/data/PostgresHelper.java index 2209191..b300920 100644 --- a/src/tourplaner/business/PostgresHelper.java +++ b/src/tourplaner/data/PostgresHelper.java @@ -1,6 +1,9 @@ -package tourplaner.business; +package tourplaner.data; +import tourplaner.business.ConfigHelper; +import tourplaner.business.LogHelper; + import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; diff --git a/src/tourplaner/business/AlertHelper.java b/src/tourplaner/ui/AlertHelper.java similarity index 98% rename from src/tourplaner/business/AlertHelper.java rename to src/tourplaner/ui/AlertHelper.java index 136e83f..6b91c13 100644 --- a/src/tourplaner/business/AlertHelper.java +++ b/src/tourplaner/ui/AlertHelper.java @@ -1,4 +1,4 @@ -package tourplaner.business; +package tourplaner.ui; import javafx.scene.Scene; import javafx.scene.control.*; @@ -6,13 +6,12 @@ import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.image.Image; import javafx.scene.layout.*; -import javafx.scene.text.Text; import javafx.stage.Stage; +import tourplaner.business.ConfigHelper; import java.io.PrintWriter; import java.io.StringWriter; import java.time.LocalDate; -import java.util.Date; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; diff --git a/src/tourplaner/ui/TourplanerController.java b/src/tourplaner/ui/TourplanerController.java index eca225b..3065656 100644 --- a/src/tourplaner/ui/TourplanerController.java +++ b/src/tourplaner/ui/TourplanerController.java @@ -9,7 +9,6 @@ import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.input.MouseEvent; -import tourplaner.business.AlertHelper; import tourplaner.business.ConfigHelper; import tourplaner.business.LogHelper; import tourplaner.object.Log; @@ -28,7 +27,7 @@ import java.util.concurrent.atomic.AtomicReference; public class TourplanerController implements Initializable { //VM - public ViewModel viewModel = new ViewModel(); + private final ViewModel viewModel = new ViewModel(); //Tour list -> links public ListView TourListView = new ListView<>(); //Tabs zu Tour -> rechts oben @@ -47,7 +46,7 @@ public class TourplanerController implements Initializable { */ @FXML private void gitWebBrowser(){ - openBrowser(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "hilfe", "gitrepo"), "Git"); + this.viewModel.gitWebBrowser(); } /** @@ -56,7 +55,7 @@ public class TourplanerController implements Initializable { */ @FXML private void javaDocBrowser(){ - openBrowser(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "hilfe", "javadoc"), "JavaDoc"); + this.viewModel.javaDocBrowser(); } /** @@ -65,27 +64,10 @@ public class TourplanerController implements Initializable { */ @FXML private void doxygenDocBrowser(){ - openBrowser(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "hilfe", "doxygendoc"), "Doxygen"); + this.viewModel.doxygenDocBrowser(); } - /** - * Öffnet einen link im Standart Browser - * @param uriString - * @param appname - */ - private void openBrowser(String uriString, String appname){ - Desktop desktop = java.awt.Desktop.getDesktop(); - try { - URI oURL = new URI( - uriString); - desktop.browse(oURL); - } catch (URISyntaxException | IOException e) { - LogHelper.error(e.getMessage(), ConfigHelper.getLangIniString("openbrowsererror") + appname); - AlertHelper.exerror(ConfigHelper.getLangIniString("browserexception"), - ConfigHelper.getLangIniString("openbrowsererror") + appname, - ConfigHelper.getLangIniString("browserexceptionmsg"), e); - } - } + /** * Wird gestartet wenn eine Tour in der Tour listView ausgewählt wird @@ -139,7 +121,6 @@ public class TourplanerController implements Initializable { this.beschreibungTableView.getItems().removeIf(s -> true); //löscht alles aus der tabelle this.titleTextView.setText(ConfigHelper.getLangIniString("keinetourselected")); this.viewModel.delTour(); - this.viewModel.setSelectedTour(null); logTableView.setPlaceholder(new Label( ConfigHelper.getLangIniString("keinetourselected"))); } @@ -149,14 +130,7 @@ public class TourplanerController implements Initializable { */ @FXML private void suche(){ - String sucheInput = this.sucheInput.getText(); - if(sucheInput.isEmpty()){ - AlertHelper.warn(ConfigHelper.getLangIniString("achtung"), - ConfigHelper.getLangIniString("suchfeldleer"), - ConfigHelper.getLangIniString("suchtextzuerst")); - }else { - this.viewModel.suche(sucheInput); - } + this.viewModel.suche(this.sucheInput.getText()); } /** @@ -165,54 +139,13 @@ public class TourplanerController implements Initializable { */ @FXML private void addLog(){ - Tour selectedTour = this.viewModel.getSelectedTour(); - if (selectedTour == null){ - AlertHelper.warn(ConfigHelper.getLangIniString("achtung"), - ConfigHelper.getLangIniString("keinetourselected"), - ConfigHelper.getLangIniString("logtournotselectedmsg")); - }else { - ObservableList tourData = this.viewModel.getTourData(); - tourData.forEach(s -> { - if (s.getName().equals(selectedTour.getName())) { - ObservableList logData = this.viewModel.getLogData(); - AtomicReference newId = new AtomicReference<>(); - newId.set(UUID.randomUUID().toString()); - logData.forEach(ss -> { - if (ss.getId().equals(newId.get())) { - newId.set(UUID.randomUUID().toString()); - } - }); - LocalDate neuesDatum = null; - while (neuesDatum == null) { - neuesDatum = AlertHelper.datePicker(ConfigHelper.getLangIniString("datum")); - } - double neueDauer = AlertHelper.inputNumber(ConfigHelper.getLangIniString("dauer"), - ConfigHelper.getLangIniString("dauermsg"), - ConfigHelper.getLangIniString("dauer") + - ConfigHelper.getLangIniString("doppelpunkt")); - double neueStrecke = AlertHelper.inputNumber(ConfigHelper.getLangIniString("strecke"), - ConfigHelper.getLangIniString("streckemsg"), - ConfigHelper.getLangIniString("strecke") + - ConfigHelper.getLangIniString("doppelpunkt")); - - Log newLog = new Log(newId.get(), neueDauer + "", neuesDatum, neueStrecke); - logData.add(newLog); - s.addLog(newLog); - } - }); - } + this.viewModel.addLog(); } @FXML private void delLog(){ - Log selectedLog = this.viewModel.getSelectedLog(); - if(selectedLog != null) { - this.viewModel.delLog(selectedLog.getId()); - }else{ - AlertHelper.warn(ConfigHelper.getLangIniString("achtung"), - ConfigHelper.getLangIniString("keinetourselected"), - ConfigHelper.getLangIniString("deltournoselectmsg")); - } + this.viewModel.delLog(); + } /** @@ -221,9 +154,7 @@ public class TourplanerController implements Initializable { */ @FXML private void logItemSelected(MouseEvent mouseEvent){ - String selectedItem = logTableView.getSelectionModel().getSelectedItem().getId(); - this.viewModel.selectLog(selectedItem); - System.out.println(selectedItem); + this.viewModel.selectLog(logTableView.getSelectionModel().getSelectedItem().getId()); } /** @@ -249,10 +180,8 @@ public class TourplanerController implements Initializable { * Funktion für noch nicht implementierte sachen wie im Menu der 'Bearbeiten' und 'Optionen' Knopf */ @FXML - public void nimpButton(){ - AlertHelper.inform(ConfigHelper.getLangIniString("achtung"), - ConfigHelper.getLangIniString("fktnichtimplementiert"), - ConfigHelper.getLangIniString("vergessenodernochnichtsoweit")); + private void nimpButton(){ + this.viewModel.nimpButton(); } } diff --git a/src/tourplaner/viewmodels/ViewModel.java b/src/tourplaner/viewmodels/ViewModel.java index 1157d89..47c0702 100644 --- a/src/tourplaner/viewmodels/ViewModel.java +++ b/src/tourplaner/viewmodels/ViewModel.java @@ -2,13 +2,19 @@ package tourplaner.viewmodels; import javafx.collections.FXCollections; import javafx.collections.ObservableList; -import tourplaner.business.AlertHelper; +import javafx.fxml.FXML; +import tourplaner.ui.AlertHelper; import tourplaner.business.ConfigHelper; import tourplaner.business.LogHelper; import tourplaner.object.Log; import tourplaner.object.Tour; -import java.util.concurrent.atomic.AtomicInteger; +import java.awt.*; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.time.LocalDate; +import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; public class ViewModel { @@ -22,6 +28,48 @@ public class ViewModel { private Log selectedLog; + /** + * Fügt einen Log eintrag zu einer Tour hinzu. + * Ist keine Tour ausgewählt, dann kommt eine Warnung an den User! + */ + @FXML + public void addLog(){ + if (this.selectedTour == null){ + AlertHelper.warn(ConfigHelper.getLangIniString("achtung"), + ConfigHelper.getLangIniString("keinetourselected"), + ConfigHelper.getLangIniString("logtournotselectedmsg")); + }else { + this.tourData.forEach(s -> { + if (s.getName().equals(this.selectedTour.getName())) { + AtomicReference newId = new AtomicReference<>(); + newId.set(UUID.randomUUID().toString()); + this.logData.forEach(ss -> { + if (ss.getId().equals(newId.get())) { + newId.set(UUID.randomUUID().toString()); + } + }); + LocalDate neuesDatum = null; + while (neuesDatum == null) { + neuesDatum = AlertHelper.datePicker(ConfigHelper.getLangIniString("datum")); + } + double neueDauer = AlertHelper.inputNumber(ConfigHelper.getLangIniString("dauer"), + ConfigHelper.getLangIniString("dauermsg"), + ConfigHelper.getLangIniString("dauer") + + ConfigHelper.getLangIniString("doppelpunkt")); + double neueStrecke = AlertHelper.inputNumber(ConfigHelper.getLangIniString("strecke"), + ConfigHelper.getLangIniString("streckemsg"), + ConfigHelper.getLangIniString("strecke") + + ConfigHelper.getLangIniString("doppelpunkt")); + + Log newLog = new Log(newId.get(), neueDauer + "", neuesDatum, neueStrecke); + this.logData.add(newLog); + s.addLog(newLog); + } + }); + } + } + + /** * Fügt eine neue Tour hinzu */ @@ -59,27 +107,33 @@ public class ViewModel { } /** - * Entfernt ein Log anhand dessen Id - * @param id Id des Logs welches entfernt werden soll + * Entfernt ein Log anhand des selectierten Logs */ - public void delLog(String id){ - this.logData.removeIf(s -> s.getId().equals(id)); - AtomicReference tourToEdit = new AtomicReference<>(); - this.tourData.forEach(s -> { - if (s.getLogs() != null) { - s.getLogs().forEach(ss -> { - if (ss.getId().equals(id)) { - tourToEdit.set(s); - } - }); - } - }); + public void delLog(){ + if(this.selectedLog != null) { + this.logData.removeIf(s -> s.getId().equals(this.selectedLog.getId())); + AtomicReference tourToEdit = new AtomicReference<>(); + this.tourData.forEach(s -> { + if (s.getLogs() != null) { + s.getLogs().forEach(ss -> { + if (ss.getId().equals(this.selectedLog.getId())) { + tourToEdit.set(s); + } + }); + } + }); - if (tourToEdit.get() != null){ - Tour toEdit = tourToEdit.get(); - toEdit.delLog(id); - this.tourData.removeIf(s -> s.getName().equals(toEdit.getName())); - this.tourData.add(toEdit); + if (tourToEdit.get() != null){ + Tour toEdit = tourToEdit.get(); + toEdit.delLog(this.selectedLog.getId()); + this.tourData.removeIf(s -> s.getName().equals(toEdit.getName())); + this.tourData.add(toEdit); + this.selectedLog = null; + } + }else{ + AlertHelper.warn(ConfigHelper.getLangIniString("achtung"), + ConfigHelper.getLangIniString("keinetourselected"), + ConfigHelper.getLangIniString("deltournoselectmsg")); } } @@ -201,6 +255,7 @@ public class ViewModel { tourData.removeIf(s -> s.getName().equals(tourname)); tourNamen.removeIf(s -> s.equals(tourname)); logData.removeIf(s -> true); + setSelectedTour(null); }catch (NullPointerException e){ LogHelper.error(ConfigHelper.getLangIniString("keinetourselected"), ConfigHelper.getLangIniString("deltournoselect")); @@ -212,8 +267,66 @@ public class ViewModel { /** * Sucht eine Tour + * @param sucheInput Text nach dem gesucht werden soll */ - public void suche(String suchString){ + public void suche(String sucheInput){ + if(sucheInput.isEmpty()){ + AlertHelper.warn(ConfigHelper.getLangIniString("achtung"), + ConfigHelper.getLangIniString("suchfeldleer"), + ConfigHelper.getLangIniString("suchtextzuerst")); + } + // TODO: 19.03.2021 Suchlogik + } + /** + * Öffnet github im standart browser + * + */ + public void gitWebBrowser(){ + openBrowser(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "hilfe", "gitrepo"), "Git"); + } + + /** + * Öffnet Java Doc im standart browser + * + */ + public void javaDocBrowser(){ + openBrowser(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "hilfe", "javadoc"), "JavaDoc"); + } + + /** + * Öffnet Doxygen Doc im standart browser + * + */ + public void doxygenDocBrowser(){ + openBrowser(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "hilfe", "doxygendoc"), "Doxygen"); + } + + /** + * Öffnet einen link im Standart Browser + * @param uriString + * @param appname + */ + private void openBrowser(String uriString, String appname){ + Desktop desktop = java.awt.Desktop.getDesktop(); + try { + URI oURL = new URI( + uriString); + desktop.browse(oURL); + } catch (URISyntaxException | IOException e) { + LogHelper.error(e.getMessage(), ConfigHelper.getLangIniString("openbrowsererror") + appname); + AlertHelper.exerror(ConfigHelper.getLangIniString("browserexception"), + ConfigHelper.getLangIniString("openbrowsererror") + appname, + ConfigHelper.getLangIniString("browserexceptionmsg"), e); + } + } + + /** + * Funktion für noch nicht implementierte sachen wie im Menu der 'Bearbeiten' und 'Optionen' Knopf + */ + public void nimpButton(){ + AlertHelper.inform(ConfigHelper.getLangIniString("achtung"), + ConfigHelper.getLangIniString("fktnichtimplementiert"), + ConfigHelper.getLangIniString("vergessenodernochnichtsoweit")); } }