From 94b1ddd70618909a9cb47b84183f8d630095b882 Mon Sep 17 00:00:00 2001
From: Harry Linrui XU <xulr0820@hotmail.com>
Date: Wed, 19 Apr 2023 12:26:34 +0200
Subject: [PATCH] Created controller for selecting budget (listview) + added
 method for switching to main menu

---
 .../demo/controller/SceneController.java      | 42 ++++++++++---
 .../controller/SelectBudgetController.java    | 63 +++++++++++++++++++
 2 files changed, 98 insertions(+), 7 deletions(-)
 create mode 100644 src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java

diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java
index 56f98861..2f8deaf0 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SceneController.java
@@ -20,6 +20,7 @@ import javafx.stage.Modality;
 import javafx.stage.Stage;
 import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
 import no.ntnu.idatt1002.demo.data.Economics.Expense;
+import no.ntnu.idatt1002.demo.data.Economics.Income;
 
 public class SceneController {
 
@@ -69,7 +70,7 @@ public class SceneController {
     dialog.showAndWait();
 
     try {
-      if (FileHandlingBudget.readCurrentFile("CurrentFile") != null) {
+      if (FileHandlingBudget.readCurrentFile() != null) {
         switchNext(event);
       }
     } catch (IOException ioe) {
@@ -77,8 +78,7 @@ public class SceneController {
     }
   }
 
-  private void switchNext(ActionEvent event) {
-    try {
+  private void switchNext(ActionEvent event) throws IOException {
       FXMLLoader loader = new FXMLLoader();
       loader.setLocation(SceneController.class.getResource("/view/dualList.fxml"));
       Parent root = loader.load();
@@ -86,9 +86,6 @@ public class SceneController {
       Scene scene = new Scene(root);
       stage.setScene(scene);
       stage.show();
-    } catch(IOException ioe) {
-      showErrorDialogBox("", "", "");
-    }
   }
   private void showErrorDialogBox(String title, String header, String content) {
     Alert alert = new Alert(AlertType.ERROR);
@@ -100,7 +97,38 @@ public class SceneController {
   }
 
   public void switchMainMenu(ActionEvent event) throws IOException {
-    FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/MainMenuNew.fxml"));
+    FXMLLoader loader = new FXMLLoader();
+    loader.setLocation(SceneController.class.getResource("/view/SelectBudget.fxml"));
+
+    String dialogTitle = "Select budget";
+    // Load the FXML file for your dialog box
+    Dialog<Income> dialog = new Dialog<>();
+
+    try {
+      // Set the Dialog's content to the loaded FXML file
+      dialog.getDialogPane().setContent(loader.load());
+    } catch (IOException e) {
+      showErrorDialogBox("Loading error", "Error in loading dialog box", "Could not load"
+          + "the AddIncome window");
+    }
+
+    //Sets the title of the dialog box
+    dialog.setTitle(dialogTitle);
+    // Show the Dialog and wait for the user to close it
+    dialog.showAndWait();
+
+    try {
+      if (FileHandlingBudget.readCurrentFile() != null) {
+        switchChosenBudget(event);
+      }
+    } catch(IOException ioe) {
+      showErrorDialogBox("Loading error", "Could not switch to main menu", "");
+    }
+  }
+
+  private void switchChosenBudget(ActionEvent event) throws IOException {
+    FXMLLoader loader = new FXMLLoader();
+    loader.setLocation(SceneController.class.getResource("/view/MainMenuNew.fxml"));
     Parent root = loader.load();
     Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
     Scene scene = new Scene(root);
diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java
new file mode 100644
index 00000000..9381f1c6
--- /dev/null
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SelectBudgetController.java
@@ -0,0 +1,63 @@
+package no.ntnu.idatt1002.demo.controller;
+
+import java.io.IOException;
+import java.util.Collection;
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableList;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import javafx.fxml.FXML;
+import javafx.scene.Node;
+import javafx.scene.control.ListView;
+import javafx.scene.input.MouseButton;
+import javafx.scene.input.MouseEvent;
+import javafx.stage.Stage;
+import no.ntnu.idatt1002.demo.data.Budget.BudgetRegister;
+import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
+import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget;
+
+public class SelectBudgetController {
+
+  @FXML
+  private ListView<String> budgetListView;
+
+  private BudgetRegister budgetRegister;
+
+  private ObservableList<String> budgetList;
+
+  @FXML
+  public void initialize() throws IOException {
+    budgetRegister = FileHandlingBudget.readBudgetArchive("");
+    System.out.println(budgetRegister);
+    System.out.println(budgetRegister.getBudgetNames().size());
+    System.out.println("this is budgetRegisr: " + budgetRegister);
+    budgetList = FXCollections.observableList(budgetRegister.getBudgetNames());
+    budgetListView.setItems(budgetList);
+
+    budgetListView.setOnMouseClicked(mouseEvent-> {
+          if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
+            if(mouseEvent.getClickCount() == 2){
+              System.out.println("Double clicked");
+            }
+          }
+        });
+      }
+
+  @FXML
+  public void selectBudget(ActionEvent event) throws IOException {
+    String name = budgetListView.getSelectionModel().getSelectedItem();
+    System.out.println(name);
+    FileHandlingBudget.updateCurrentFile(name);
+
+    final Node source = (Node) event.getSource();
+    ((Stage) source.getScene().getWindow()).close();
+  }
+
+  @FXML
+  public void exitWindow(ActionEvent event) throws IOException {
+    FileHandlingBudget.updateCurrentFile("");
+    final Node source = (Node) event.getSource();
+    ((Stage) source.getScene().getWindow()).close();
+  }
+}
+
-- 
GitLab