From 5b575727877372b438c5a00e55a85e8701c8b673 Mon Sep 17 00:00:00 2001
From: Harry Linrui XU <xulr0820@hotmail.com>
Date: Wed, 12 Apr 2023 10:45:27 +0200
Subject: [PATCH] Added error msg to income and expenses in case of
 IOexceptions

---
 .../controller/IncomeExpenseController.java   | 66 ++++++++++++-------
 .../resources/view/IncomeAndExpenses.fxml     | 27 ++++----
 2 files changed, 57 insertions(+), 36 deletions(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java
index e9d59c36..7b07a818 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/IncomeExpenseController.java
@@ -108,6 +108,9 @@ public class IncomeExpenseController implements FinanceController {
   @FXML
   private Label title;
 
+  @FXML
+  private Label errorMsg;
+
   @FXML
   private MenuItem editIncomeMenu;
 
@@ -136,31 +139,37 @@ public class IncomeExpenseController implements FinanceController {
   @FXML
   private PieChart incomePieChart;
 
+
   FileHandling fileHandling;
 
   @FXML
-  public void initialize() throws IOException {
+  public void initialize() {
     fileHandling = new FileHandling();
 
     //Initialize columns
     setColumns();
 
-    //Initialize registers and tableview
-    incomeRegister = loadIncomeDataFromFile("Income");
-    income = FXCollections.observableArrayList(incomeRegister.getItems());
-    incomeTableView.setItems(income);
+    try {
+      //Initialize registers and tableview
+      incomeRegister = loadIncomeDataFromFile("Income");
+      income = FXCollections.observableArrayList(incomeRegister.getItems());
+      incomeTableView.setItems(income);
+
+      expenseRegister = loadExpenseDataFromFile("Expense");
+      expenses = FXCollections.observableArrayList(expenseRegister.getItems());
+      expenseTableView.setItems(expenses);
 
-    expenseRegister = loadExpenseDataFromFile("Expense");
-    expenses = FXCollections.observableArrayList(expenseRegister.getItems());
-    expenseTableView.setItems(expenses);
+      //Setting pie chart values to correspond with the registers
+      incomePieChart.setLegendSide(Side.RIGHT);
 
-    //Setting pie chart values to correspond with the registers
-    incomePieChart.setLegendSide(Side.RIGHT);
+      expensePieChart.setLegendSide(Side.RIGHT);
+      expensePieChart.setLabelLineLength(10);
 
-    expensePieChart.setLegendSide(Side.RIGHT);
-    expensePieChart.setLabelLineLength(10);
+      refreshPieCharts();
+    } catch(IOException ioe) {
+      errorMsg.setOpacity(1);
+    }
 
-    refreshPieCharts();
     refreshProgress();
 
     formatDatePicker();
@@ -320,7 +329,8 @@ public class IncomeExpenseController implements FinanceController {
       // Set the Dialog's content to the loaded FXML file
       dialog.getDialogPane().setContent(loader.load());
     } catch (IOException e) {
-      e.printStackTrace();
+      errorMsg.setText("Error in loading in dialog box");
+      errorMsg.setOpacity(1);
     }
 
     // Get the controller for the loaded FXML file
@@ -355,7 +365,8 @@ public class IncomeExpenseController implements FinanceController {
       // Set the Dialog's content to the loaded FXML file
       dialog.getDialogPane().setContent(loader.load());
     } catch (IOException e) {
-      e.printStackTrace();
+        errorMsg.setText("Error in loading in dialog box");
+        errorMsg.setOpacity(1);
     }
 
     // Get the controller for the loaded FXML file
@@ -544,18 +555,23 @@ public class IncomeExpenseController implements FinanceController {
   /**
    * Switches scenes back to main menu, by loading a new FXML file and setting the scene to this location.
    * @param event A button click on the return to main menu button
-   * @throws IOException If an error occurs with loading any of the FXML files.
    */
   @FXML
-  public void returnToMainMenu(javafx.event.ActionEvent event) throws IOException {
-    saveDataToFile();
-    FXMLLoader loader = new FXMLLoader();
-    loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml"));
+  public void returnToMainMenu(javafx.event.ActionEvent event) {
+    try {
+      saveDataToFile();
+      FXMLLoader loader = new FXMLLoader();
+      loader.setLocation(getClass().getResource("/view/MainMenuNew.fxml"));
+
+      Parent root = loader.load();
+      Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
+      Scene scene = new Scene(root);
+      stage.setScene(scene);
+      stage.show();
+    } catch(IOException ioe) {
+      errorMsg.setText("Error in saving to file");
+      errorMsg.setOpacity(1);
+    }
 
-    Parent root = loader.load();
-    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
-    Scene scene = new Scene(root);
-    stage.setScene(scene);
-    stage.show();
   }
 }
diff --git a/src/main/resources/view/IncomeAndExpenses.fxml b/src/main/resources/view/IncomeAndExpenses.fxml
index 80d225d8..dc5c4fa5 100644
--- a/src/main/resources/view/IncomeAndExpenses.fxml
+++ b/src/main/resources/view/IncomeAndExpenses.fxml
@@ -63,11 +63,16 @@
                   </Pane>
                </left>
                <center>
-                  <Label fx:id="title" text="INCOME AND EXPENSES" textAlignment="CENTER" BorderPane.alignment="CENTER">
-                     <font>
-                        <Font name="Lucida Console" size="48.0" />
-                     </font>
-                  </Label>
+                  <Pane BorderPane.alignment="CENTER">
+                     <children>
+                        <Label fx:id="title" layoutX="100.0" layoutY="47.0" text="INCOME AND EXPENSES" textAlignment="CENTER">
+                           <font>
+                              <Font name="Lucida Console" size="48.0" />
+                           </font>
+                        </Label>
+                        <Label fx:id="errorMsg" layoutX="309.0" layoutY="112.0" opacity="0.0" text="Couldn't load saved data" textAlignment="CENTER" textFill="#f20808" />
+                     </children>
+                  </Pane>
                </center>
                <VBox.margin>
                   <Insets left="15.0" />
@@ -155,11 +160,6 @@
                               </ContextMenu>
                            </contextMenu>
                         </TableView>
-                        <Label fx:id="inSum" text="Sum: ">
-                           <font>
-                              <Font name="Lucida Console" size="14.0" />
-                           </font>
-                        </Label>
                      </children>
                   </VBox>
                   <Pane GridPane.columnIndex="1" GridPane.rowIndex="1">
@@ -179,6 +179,11 @@
                               <Font name="Lucida Console" size="14.0" />
                            </font>
                         </Text>
+                        <Label fx:id="inSum" layoutX="64.0" layoutY="4.0" text="Sum: ">
+                           <font>
+                              <Font name="Lucida Console" size="14.0" />
+                           </font>
+                        </Label>
                      </children>
                   </Pane>
                   <VBox GridPane.rowIndex="3">
@@ -205,7 +210,7 @@
                         </TableView>
                         <Label fx:id="expSum" text="Sum: ">
                            <font>
-                              <Font name="Lucida Console" size="14.0" />
+                              <Font name="Lucida Console" size="12.0" />
                            </font>
                         </Label>
                      </children>
-- 
GitLab