Skip to content
Snippets Groups Projects
Commit 94b1ddd7 authored by Harry Linrui XU's avatar Harry Linrui XU
Browse files

Created controller for selecting budget (listview) + added method for switching to main menu

parent 9e493022
No related branches found
No related tags found
1 merge request!43Merging frontend-testing into master
...@@ -20,6 +20,7 @@ import javafx.stage.Modality; ...@@ -20,6 +20,7 @@ import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget;
import no.ntnu.idatt1002.demo.data.Economics.Expense; import no.ntnu.idatt1002.demo.data.Economics.Expense;
import no.ntnu.idatt1002.demo.data.Economics.Income;
public class SceneController { public class SceneController {
...@@ -69,7 +70,7 @@ public class SceneController { ...@@ -69,7 +70,7 @@ public class SceneController {
dialog.showAndWait(); dialog.showAndWait();
try { try {
if (FileHandlingBudget.readCurrentFile("CurrentFile") != null) { if (FileHandlingBudget.readCurrentFile() != null) {
switchNext(event); switchNext(event);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
...@@ -77,8 +78,7 @@ public class SceneController { ...@@ -77,8 +78,7 @@ public class SceneController {
} }
} }
private void switchNext(ActionEvent event) { private void switchNext(ActionEvent event) throws IOException {
try {
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
loader.setLocation(SceneController.class.getResource("/view/dualList.fxml")); loader.setLocation(SceneController.class.getResource("/view/dualList.fxml"));
Parent root = loader.load(); Parent root = loader.load();
...@@ -86,9 +86,6 @@ public class SceneController { ...@@ -86,9 +86,6 @@ public class SceneController {
Scene scene = new Scene(root); Scene scene = new Scene(root);
stage.setScene(scene); stage.setScene(scene);
stage.show(); stage.show();
} catch(IOException ioe) {
showErrorDialogBox("", "", "");
}
} }
private void showErrorDialogBox(String title, String header, String content) { private void showErrorDialogBox(String title, String header, String content) {
Alert alert = new Alert(AlertType.ERROR); Alert alert = new Alert(AlertType.ERROR);
...@@ -100,7 +97,38 @@ public class SceneController { ...@@ -100,7 +97,38 @@ public class SceneController {
} }
public void switchMainMenu(ActionEvent event) throws IOException { 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(); Parent root = loader.load();
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow(); Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
Scene scene = new Scene(root); Scene scene = new Scene(root);
......
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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment