From d8c4680e16fd57a17ba7484a1a0f3dd8372de9d2 Mon Sep 17 00:00:00 2001 From: Georg-Notebook Date: Sat, 17 Apr 2021 17:52:36 +0200 Subject: [PATCH] Progressbar Bugfixes --- src/tourplaner/business/DirectionMap.java | 3 + src/tourplaner/business/Exporter.java | 44 +++++++------- src/tourplaner/business/TourPlaner.java | 25 ++++++-- src/tourplaner/data/DbConnect.java | 37 +++++++++--- src/tourplaner/tourplaner.fxml | 5 +- src/tourplaner/ui/ProgressBar.java | 15 +++-- src/tourplaner/ui/TourplanerController.java | 3 +- src/tourplaner/viewmodels/ViewModel.java | 65 +++++++++++---------- 8 files changed, 126 insertions(+), 71 deletions(-) diff --git a/src/tourplaner/business/DirectionMap.java b/src/tourplaner/business/DirectionMap.java index 0c32353..9adad5a 100644 --- a/src/tourplaner/business/DirectionMap.java +++ b/src/tourplaner/business/DirectionMap.java @@ -68,7 +68,10 @@ public class DirectionMap { * @throws IOException Fehler beim besorgen der infos über die Tour */ private void getDirections(String start, String ende) throws IOException { + System.out.println("LOCS: " + start + ende); String json = HttpHelper.httpGetJsonString("https://www.mapquestapi.com/directions/v2/route?key="+ ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "key")+"&from="+start+"&to="+ende+"&outFormat=json&ambiguities=ignore&routeType=fastest&doReverseGeocode=false&enhancedNarrative=false&avoidTimedConditions=false"); + System.out.println("JSON: " + json); + System.out.println("URL https://www.mapquestapi.com/directions/v2/route?key="+ ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "key")+"&from="+start+"&to="+ende+"&outFormat=json&ambiguities=ignore&routeType=fastest&doReverseGeocode=false&enhancedNarrative=false&avoidTimedConditions=false"); this.strecke = JsonHelper.getDoubleFromJson(json, "distance"); this.dauer = formatetTimeToMinutes(JsonHelper.getStingFromJson(json, "formattedTime")); } diff --git a/src/tourplaner/business/Exporter.java b/src/tourplaner/business/Exporter.java index bf2d87f..c5b9059 100644 --- a/src/tourplaner/business/Exporter.java +++ b/src/tourplaner/business/Exporter.java @@ -31,7 +31,7 @@ public class Exporter { * @throws IOException Fehler beim File erstellen */ public void doExport() throws IOException { - ProgressBar progressBar = new ProgressBar("Exportieren"); + ProgressBar progressBar = new ProgressBar("Export..."); int step = progressBar.getProgressSize(4, 100); ArrayList data = new DbConnect().getAllTouren(); progressBar.addProgress(step); @@ -40,7 +40,7 @@ public class Exporter { JsonHelper.getJsonFromObj(data, fileWriter); progressBar.addProgress(step); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); - progressBar.addProgress(step); + progressBar.setProgress(100); bufferedWriter.close(); } @@ -48,25 +48,29 @@ public class Exporter { * Führt den Imput aus -> File holen, Daten in db erstellen * @throws IOException Fehler beim File öffnen */ - public void doImport() throws IOException { - this.touren = JsonHelper.getTourenFromJson(new FileReader(this.path)); - ProgressBar progressBar = new ProgressBar("Importiere Daten"); - DbConnect dbConnect = new DbConnect(); - progressBar.addProgress(5); - dbConnect.delAllData(); - progressBar.addProgress(5); - int size = progressBar.getProgressSize(this.touren.size() * 3, 100); - for (Tour tour:this.touren) { - new DirectionMap(tour.getStart(), tour.getZiel(), tour.getName()); - progressBar.addProgress(size); - dbConnect.addTour(tour); - progressBar.addProgress(size); - ArrayList logs = tour.getLogs(); - for (Log log:logs) { - dbConnect.addLog(tour.getName(), log); + public void doImport() { + ProgressBar progressBar = new ProgressBar("Import..."); + try { + this.touren = JsonHelper.getTourenFromJson(new FileReader(this.path)); + DbConnect dbConnect = new DbConnect(); + dbConnect.delAllData(); + progressBar.addProgress(5); + int size = progressBar.getProgressSize(this.touren.size() * 3, 100); + for (Tour tour:this.touren) { + new DirectionMap(tour.getStart(), tour.getZiel(), tour.getName()); + progressBar.addProgress(size); + dbConnect.addTour(tour); + progressBar.addProgress(size); + ArrayList logs = tour.getLogs(); + for (Log log:logs) { + dbConnect.addLog(tour.getName(), log); + } + progressBar.addProgress(size); } - progressBar.addProgress(size); + } catch (IOException e) { + LogHelper.error(e); + progressBar.setProgress(100); } - progressBar.addProgress(size); + progressBar.setProgress(100); } } diff --git a/src/tourplaner/business/TourPlaner.java b/src/tourplaner/business/TourPlaner.java index 5114cfe..2054fc4 100644 --- a/src/tourplaner/business/TourPlaner.java +++ b/src/tourplaner/business/TourPlaner.java @@ -4,6 +4,7 @@ import org.apache.log4j.Logger; import tourplaner.data.DbConnect; import tourplaner.object.Log; import tourplaner.object.Tour; +import tourplaner.ui.ProgressBar; import java.awt.*; import java.io.File; @@ -39,12 +40,18 @@ public class TourPlaner{ * @return false bei error */ public static boolean editTour(String oldname, Tour tour) throws IOException { + ProgressBar progressBar = new ProgressBar("Edit..."); + int step = progressBar.getProgressSize(3, 100); FileHelper.delFile(new File(getImagePath(oldname))); FileHelper.delFile(new File(getImagePdfPath(oldname))); + progressBar.addProgress(step); DirectionMap directionMap = new DirectionMap(tour.getStart(), tour.getZiel(), tour.getName()); + progressBar.addProgress(step); tour.setDauer(directionMap.getDauer()+""); tour.setStrecke(directionMap.getStrecke()); - return new DbConnect().editTour(oldname, tour); + boolean ret = new DbConnect().editTour(oldname, tour); + progressBar.setProgress(100); + return ret; } /** @@ -53,20 +60,30 @@ public class TourPlaner{ * @return false bei error */ public static boolean delTour(String tourname){ + ProgressBar progressBar = new ProgressBar("Del..."); + int step = progressBar.getProgressSize(3, 100); FileHelper.delFile(new File(getImagePath(tourname))); + progressBar.addProgress(step); FileHelper.delFile(new File(getImagePdfPath(tourname))); - return new DbConnect().delTour(tourname); + progressBar.addProgress(step); + boolean ret = new DbConnect().delTour(tourname); + progressBar.setProgress(100); + return ret; } /** * Fügt eine Tour hinzu * @param newTour Neue Tour * @return false bei error */ - public static boolean addTour(Tour newTour) throws IOException { + public static boolean addTour(Tour newTour, ProgressBar progressBar) throws IOException { + int step = progressBar.getProgressSize(2, 100); DirectionMap directionMap = new DirectionMap(newTour.getStart(), newTour.getZiel(), newTour.getName()); + progressBar.addProgress(step); newTour.setDauer(directionMap.getDauer()+""); newTour.setStrecke(directionMap.getStrecke()); - return new DbConnect().addTour(newTour); + boolean ret = new DbConnect().addTour(newTour); + progressBar.setProgress(100); + return ret; } /** diff --git a/src/tourplaner/data/DbConnect.java b/src/tourplaner/data/DbConnect.java index fff971a..39cc6d5 100644 --- a/src/tourplaner/data/DbConnect.java +++ b/src/tourplaner/data/DbConnect.java @@ -1,13 +1,13 @@ package tourplaner.data; import javafx.geometry.Pos; +import tourplaner.business.LogHelper; import tourplaner.object.Log; import tourplaner.object.Tour; +import tourplaner.ui.AlertHelper; +import tourplaner.ui.ProgressBar; -import java.sql.Connection; -import java.sql.Date; -import java.sql.ResultSet; -import java.sql.Statement; +import java.sql.*; import java.time.LocalDate; import java.util.ArrayList; @@ -155,21 +155,38 @@ public class DbConnect { } public boolean delLog(String tourname, String id){ - System.out.println("DELETE FROM public.log WHERE tourname = '"+tourname+"' and id = '"+id+"'"); return PostgresHelper.executeUpdate("DELETE FROM public.log WHERE tourname = '"+tourname+"' and id = '"+id+"'"); } + private int getTourSize() throws SQLException { + Connection con = PostgresHelper.con(); + Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); + ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM tour;"); + int size =0; + if (rs != null) + { + rs.last(); // moves cursor to the last row + size = rs.getInt("count"); // get row id + con.close(); + return size; + } + con.close(); + return -1; + } + /** * Holt alle Touren aus der Datenbank * @return Null bei fehler, sonst eine List aus den IDs */ - public ArrayList getAllTouren(){ + public ArrayList getAllTouren() { this.c = PostgresHelper.con(); String tourname, mapjson, start, ziel; double dauer, strecke; - ArrayList touren = new ArrayList<>(); + ProgressBar progressBar = new ProgressBar("Get..."); try { + int tourSize = getTourSize(); + int step = progressBar.getProgressSize(tourSize, 100); stmt = this.c.createStatement(); ResultSet rs = stmt.executeQuery("select * from tour;"); while (rs.next()) { @@ -181,16 +198,20 @@ public class DbConnect { strecke = rs.getDouble("strecke"); if (!tourname.isEmpty()) { touren.add(new Tour(tourname, dauer + "", mapjson, strecke, start, ziel, getLogs(tourname))); + progressBar.addProgress(step); }else { + progressBar.setProgress(100); return null; } } + progressBar.setProgress(100); rs.close(); stmt.close(); this.c.close(); return touren; } catch (Exception e) { - System.err.println(e.getClass().getName() + ": " + e.getMessage()); + LogHelper.error(e); + progressBar.setProgress(100); return null; } } diff --git a/src/tourplaner/tourplaner.fxml b/src/tourplaner/tourplaner.fxml index 453ce06..5fecea0 100644 --- a/src/tourplaner/tourplaner.fxml +++ b/src/tourplaner/tourplaner.fxml @@ -45,9 +45,9 @@ + + - - @@ -58,7 +58,6 @@ - diff --git a/src/tourplaner/ui/ProgressBar.java b/src/tourplaner/ui/ProgressBar.java index 417b39f..da41508 100644 --- a/src/tourplaner/ui/ProgressBar.java +++ b/src/tourplaner/ui/ProgressBar.java @@ -25,13 +25,21 @@ public class ProgressBar { public void addProgress(int add){ this.status += add; - System.out.println("PROGRESS: " + this.status); this.progressBar.setValue(this.status); - if(this.status == 100){ - frame.setVisible(false); + closeOnFinish(); + } + + private void closeOnFinish(){ + if(this.status >= 100){ + closeProgress(); } } + public void setProgress(int set){ + this.status = set; + closeOnFinish(); + } + public void closeProgress(){ this.frame.setVisible(false); } @@ -39,7 +47,6 @@ public class ProgressBar { public int getProgressSize(int todo, int maxLevel){ int steps = (progressBar.getValue() - maxLevel) / todo; if(steps < 0) steps = steps*-1; - System.out.println("STEPS: " + steps); return steps; } } diff --git a/src/tourplaner/ui/TourplanerController.java b/src/tourplaner/ui/TourplanerController.java index 732bdbb..8a2f173 100644 --- a/src/tourplaner/ui/TourplanerController.java +++ b/src/tourplaner/ui/TourplanerController.java @@ -169,8 +169,7 @@ public class TourplanerController implements Initializable { */ @FXML private void addTour(){ - this.viewModel.addTour(); - this.mapImageView.setImage(this.viewModel.getImage(this.viewModel.getSelectedTour().getName())); + if(this.viewModel.addTour()) this.mapImageView.setImage(this.viewModel.getImage(this.viewModel.getSelectedTour().getName())); } /** diff --git a/src/tourplaner/viewmodels/ViewModel.java b/src/tourplaner/viewmodels/ViewModel.java index 2a74180..81f1a2d 100644 --- a/src/tourplaner/viewmodels/ViewModel.java +++ b/src/tourplaner/viewmodels/ViewModel.java @@ -7,10 +7,9 @@ import tourplaner.business.*; import tourplaner.ui.AlertHelper; import tourplaner.object.Log; import tourplaner.object.Tour; +import tourplaner.ui.ProgressBar; -import javax.swing.*; import java.awt.*; -import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -80,9 +79,10 @@ public class ViewModel { /** * Input mit duplications check des Namens und ohne content im input feld + * @return false beim schließen */ - private void tourNameInput(){ - tourNameInputDuplicatCheck("", true); + private boolean tourNameInput(){ + return tourNameInputDuplicatCheck("", true); } /** @@ -153,24 +153,31 @@ public class ViewModel { /** * Fügt eine neue Tour hinzu */ - public void addTour(){ - tourNameInput(); - tourStartInput(""); - tourZielInput(""); - if (getTour(this.neueTourName) == null) { - Tour newTour = new Tour(this.neueTourName, "1", "1", 0, this.neueTourStart, this.neueTourZiel); - tourData.add(newTour); - tourNamen.add(this.neueTourName); - this.selectedTour = newTour; - try { - TourPlaner.addTour(newTour); - } catch (IOException e) { - e.printStackTrace(); - } - } + public boolean addTour(){ + if(tourNameInput()) { + if(tourStartInput("")) { + if (tourZielInput("")) { + if (getTour(this.neueTourName) == null) { + ProgressBar progressBar = new ProgressBar("Add Tour..."); + Tour newTour = new Tour(this.neueTourName, "1", "1", 0, this.neueTourStart, this.neueTourZiel); + tourData.add(newTour); + tourNamen.add(this.neueTourName); + this.selectedTour = newTour; + progressBar.addProgress(10); + try { + TourPlaner.addTour(newTour, progressBar); + } catch (IOException e) { + LogHelper.error(e); + return false; + } + } + }else return false; + }else return false; + }else return false; this.neueTourStart = null; this.neueTourZiel = null; this.neueTourName = null; + return true; } private boolean keineTourSelected(){ @@ -209,11 +216,11 @@ public class ViewModel { if (dauer >= 0) { strecke = streckeInput(this.selectedLog.getStrecke() + ""); if (strecke >= 0) { - pause = pauseInput(""); + pause = pauseInput(this.selectedLog.getPause() + ""); if(pause >= 0.0) { - hightmeter = hightmeterInput(""); + hightmeter = hightmeterInput(this.selectedLog.getHightmeter() + ""); if (hightmeter >= 0.0) { - bemerkung = bemerkungInput(""); + bemerkung = bemerkungInput(this.selectedLog.getBemerkung()); if (bemerkung) { datum = dateInput(this.selectedLog.getDatum()); if (datum != null) { @@ -482,7 +489,10 @@ public class ViewModel { public ObservableList getTourData() { tourData.clear(); - tourData.addAll(TourPlaner.getAllTours()); + ArrayList touren = TourPlaner.getAllTours(); + if(touren != null) { + tourData.addAll(touren); + } return tourData; } @@ -619,13 +629,8 @@ public class ViewModel { public void importData() { String file = AlertHelper.fileChooser("Importiere"); if (file != null){ - try { - this.tourNamen = FXCollections.observableArrayList(); - new Exporter(file).doImport(); - } catch (IOException e) { - AlertHelper.error("Error", "Import Error", "Fehler beim Importieren"); - LogHelper.error(e); - } + this.tourNamen = FXCollections.observableArrayList(); + new Exporter(file).doImport(); } }