From 49b6407ce63a69a4556ac1a5f60135bc1d99c9ff Mon Sep 17 00:00:00 2001
From: Georg-Notebook <georg@dergeorg.at>
Date: Sat, 17 Apr 2021 15:38:16 +0200
Subject: [PATCH] Progress Bar

---
 src/tourplaner/business/Exporter.java       | 17 ++++++++
 src/tourplaner/tourplaner.fxml              | 12 +++++-
 src/tourplaner/ui/AlertHelper.java          |  4 ++
 src/tourplaner/ui/ProgressBar.java          | 45 +++++++++++++++++++++
 src/tourplaner/ui/TourplanerController.java | 15 ++++++-
 src/tourplaner/viewmodels/ViewModel.java    |  2 +-
 6 files changed, 91 insertions(+), 4 deletions(-)
 create mode 100644 src/tourplaner/ui/ProgressBar.java

diff --git a/src/tourplaner/business/Exporter.java b/src/tourplaner/business/Exporter.java
index b0dae5e..bf2d87f 100644
--- a/src/tourplaner/business/Exporter.java
+++ b/src/tourplaner/business/Exporter.java
@@ -3,7 +3,10 @@ package tourplaner.business;
 import tourplaner.data.DbConnect;
 import tourplaner.object.Log;
 import tourplaner.object.Tour;
+import tourplaner.ui.AlertHelper;
+import tourplaner.ui.ProgressBar;
 
+import javax.swing.*;
 import java.io.*;
 import java.util.ArrayList;
 
@@ -28,10 +31,16 @@ public class Exporter {
      * @throws IOException Fehler beim File erstellen
      */
     public void doExport() throws IOException {
+        ProgressBar progressBar = new ProgressBar("Exportieren");
+        int step = progressBar.getProgressSize(4, 100);
         ArrayList<Tour> data = new DbConnect().getAllTouren();
+        progressBar.addProgress(step);
         FileWriter fileWriter = new FileWriter(path + ".json");
+        progressBar.addProgress(step);
         JsonHelper.getJsonFromObj(data, fileWriter);
+        progressBar.addProgress(step);
         BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
+        progressBar.addProgress(step);
         bufferedWriter.close();
     }
 
@@ -41,15 +50,23 @@ public class Exporter {
      */
     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<Log> logs = tour.getLogs();
             for (Log log:logs) {
                 dbConnect.addLog(tour.getName(), log);
             }
+            progressBar.addProgress(size);
         }
+        progressBar.addProgress(size);
     }
 }
diff --git a/src/tourplaner/tourplaner.fxml b/src/tourplaner/tourplaner.fxml
index b6c97a9..453ce06 100644
--- a/src/tourplaner/tourplaner.fxml
+++ b/src/tourplaner/tourplaner.fxml
@@ -46,19 +46,27 @@
         <Menu fx:id="menueFile" mnemonicParsing="false" text="Datei">
           <items>
             <MenuItem fx:id="beendenButton" mnemonicParsing="false" onAction="#quitApp" text="Beenden" />
+                  <MenuItem mnemonicParsing="false" onAction="#reportSum" text="Report alles" />
+                  <MenuItem mnemonicParsing="false" onAction="#tourReport" text="Tour Report erstellen" />
           </items>
         </Menu>
         <Menu mnemonicParsing="false" text="Bearbeiten">
                <items>
                   <MenuItem mnemonicParsing="false" onAction="#importBtn" text="Import" />
                   <MenuItem mnemonicParsing="false" onAction="#exportBtn" text="Export" />
-                  <MenuItem mnemonicParsing="false" onAction="#reportSum" text="Report alles" />
-                  <MenuItem mnemonicParsing="false" onAction="#tourReport" text="Tour Report erstellen" />
                </items>
         </Menu>
             <Menu mnemonicParsing="false" text="Optionen">
                <items>
                   <MenuItem mnemonicParsing="false" onAction="#nimpButton" text="Keine Funktion" />
+                  <CheckMenuItem mnemonicParsing="false" text="Map automatisch öffnen" />
+                  <CheckMenuItem mnemonicParsing="false" text="Report automatisch öffnen" />
+                  <Menu mnemonicParsing="false" text="Sprache">
+                    <items>
+                        <RadioMenuItem mnemonicParsing="false" text="Deutsch" />
+                        <RadioMenuItem mnemonicParsing="false" text="Englisch" />
+                    </items>
+                  </Menu>
                </items>
             </Menu>
         <Menu mnemonicParsing="false" text="Hilfe">
diff --git a/src/tourplaner/ui/AlertHelper.java b/src/tourplaner/ui/AlertHelper.java
index d7ab3df..5282239 100644
--- a/src/tourplaner/ui/AlertHelper.java
+++ b/src/tourplaner/ui/AlertHelper.java
@@ -12,6 +12,10 @@ import javafx.stage.WindowEvent;
 import tourplaner.business.ConfigHelper;
 
 import javax.swing.*;
+import javax.swing.border.TitledBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import java.awt.*;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.time.LocalDate;
diff --git a/src/tourplaner/ui/ProgressBar.java b/src/tourplaner/ui/ProgressBar.java
new file mode 100644
index 0000000..417b39f
--- /dev/null
+++ b/src/tourplaner/ui/ProgressBar.java
@@ -0,0 +1,45 @@
+package tourplaner.ui;
+
+import javax.swing.*;
+import javax.swing.border.TitledBorder;
+import java.awt.*;
+
+public class ProgressBar {
+    private int status;
+    private JFrame frame;
+    private JProgressBar progressBar;
+
+    public ProgressBar(String title){
+        this.frame = new JFrame(title);
+        this.frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
+        Container content = this.frame.getContentPane();
+        this.progressBar = new JProgressBar();
+        this.progressBar.setValue(0);
+        this.progressBar.setStringPainted(true);
+        TitledBorder border = BorderFactory.createTitledBorder("Laden....");
+        this.progressBar.setBorder(border);
+        content.add(this.progressBar, BorderLayout.NORTH);
+        this.frame.setSize(300, 100);
+        this.frame.setVisible(true);
+    }
+
+    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);
+        }
+    }
+
+    public void closeProgress(){
+        this.frame.setVisible(false);
+    }
+
+    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 cde6341..732bdbb 100644
--- a/src/tourplaner/ui/TourplanerController.java
+++ b/src/tourplaner/ui/TourplanerController.java
@@ -17,6 +17,7 @@ import tourplaner.object.Log;
 import tourplaner.object.Tour;
 import tourplaner.viewmodels.ViewModel;
 
+import javax.swing.*;
 import java.awt.*;
 import java.io.IOException;
 import java.net.URI;
@@ -114,10 +115,13 @@ public class TourplanerController implements Initializable {
      */
     @FXML
     private void tourListSelectedItem(MouseEvent mouseEvent){
+        ProgressBar progressBar = new ProgressBar("Tour auswählen...");
         String selectedItem = TourListView.getSelectionModel().getSelectedItem();
         this.viewModel.selectTour(selectedItem);
         titleTextView.setText(selectedItem);
+        progressBar.addProgress(10);
         syncTour(selectedItem);
+        progressBar.addProgress(10);
         startCol.setCellValueFactory(new PropertyValueFactory<Tour, String>("start"));
         zielCol.setCellValueFactory(new PropertyValueFactory<Tour, String>("ziel"));
         dauerCol.setCellValueFactory(new PropertyValueFactory<Tour, String>("dauer"));
@@ -127,7 +131,9 @@ public class TourplanerController implements Initializable {
         //Log anzeigen
         logTableView.setPlaceholder(new Label( ConfigHelper.getLangIniString("keinelogsvorhanden")));
         logTableView.getItems().clear();
+        progressBar.addProgress(10);
         logTableView.setItems(this.viewModel.getLogData());
+        progressBar.addProgress(10);
         logDauerCol.setCellValueFactory(new PropertyValueFactory<Log, String>("dauer"));
         logStreckeCol.setCellValueFactory(new PropertyValueFactory<Log, String>("strecke"));
         logDatumCol.setCellValueFactory(new PropertyValueFactory<Log, String>("datum"));
@@ -136,12 +142,16 @@ public class TourplanerController implements Initializable {
         logPauseCol.setCellValueFactory(new PropertyValueFactory<Log, String>("pause"));
         logGegangenCol.setCellValueFactory(new PropertyValueFactory<Log, String>("gegangen"));
         logBemerkungCol.setCellValueFactory(new PropertyValueFactory<Log, String>("bemerkung"));
-
+        progressBar.addProgress(10);
         mapImageView.setImage(this.viewModel.getImage(this.viewModel.getSelectedTour().getName()));
         if(this.viewModel.isSucheAktiv()){
             this.viewModel.setSucheAktiv(false);
+            progressBar.addProgress(10);
             syncTourNamen();
+            progressBar.addProgress(40);
+            this.sucheInput.setText("");
         }
+        progressBar.addProgress(50);
     }
 
     /**
@@ -182,7 +192,10 @@ public class TourplanerController implements Initializable {
      */
     @FXML
     private void suche(){
+        ProgressBar progressBar = new ProgressBar("Suche");
         this.viewModel.suche(this.sucheInput.getText());
+        progressBar.addProgress(100);
+        progressBar.closeProgress();
     }
 
     /**
diff --git a/src/tourplaner/viewmodels/ViewModel.java b/src/tourplaner/viewmodels/ViewModel.java
index 7277ed0..2a74180 100644
--- a/src/tourplaner/viewmodels/ViewModel.java
+++ b/src/tourplaner/viewmodels/ViewModel.java
@@ -8,6 +8,7 @@ import tourplaner.ui.AlertHelper;
 import tourplaner.object.Log;
 import tourplaner.object.Tour;
 
+import javax.swing.*;
 import java.awt.*;
 import java.io.FileNotFoundException;
 import java.io.IOException;
@@ -617,7 +618,6 @@ public class ViewModel {
      */
     public void importData() {
         String file = AlertHelper.fileChooser("Importiere");
-        System.out.println("IMPORT: " + file);
         if (file != null){
             try {
                 this.tourNamen = FXCollections.observableArrayList();