package no.ntnu.idatt1002.demo.controller; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Node; import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.stage.Modality; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.Budget.BudgetItem; import no.ntnu.idatt1002.demo.data.Budget.GeneralBudget; import no.ntnu.idatt1002.demo.data.Economics.Expense; import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Optional; enum DialogMode{ ADD, EDIT, DELETE } public class BudgetController { private DialogMode dialogMode; private List<BudgetItem> listOfBudgetItems = new ArrayList<>(); private GeneralBudget general = new GeneralBudget(31, listOfBudgetItems, 200); @FXML private Button addBudget; @FXML private Button editBudget; @FXML private TableColumn<BudgetItem, Double> amountColumn; @FXML private TableView<BudgetItem> budgetTableView = new TableView<>(); @FXML private TableColumn<BudgetItem, ExpenseCategory> categoryColumn; @FXML private TextArea daysVariable; @FXML private TableColumn<BudgetItem, String> descriptionColumn; @FXML private TextArea monthVariable; @FXML private TableColumn<?, ?> percentageColumn; @FXML private TextArea totalBudgetAmount; @FXML private ObservableList<BudgetItem> budgetList; public void initialize() throws IOException { budgetList = FXCollections.observableArrayList(listOfBudgetItems); budgetTableView.setItems(budgetList); categoryColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, ExpenseCategory>("budgetCategory")); amountColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, Double>("budgetAmount")); descriptionColumn.setCellValueFactory(new PropertyValueFactory<BudgetItem, String>("budgetDescription")); } @FXML public void switchAddBudget(javafx.event.ActionEvent event) throws IOException { BudgetItem item = null; String dialogTitle = ""; FXMLLoader loader = new FXMLLoader(SceneController.class.getResource("/view/AddBudget.fxml")); Dialog<BudgetItem> dialog = new Dialog<>(); dialog.initModality(Modality.APPLICATION_MODAL); try{ dialog.getDialogPane().setContent(loader.load()); } catch(IOException e) { e.printStackTrace(); } AddBudgetController budgetController = loader.getController(); if(event.getSource().equals(addBudget)){ dialogMode = DialogMode.ADD; dialogTitle = "New Budget"; } else if (event.getSource().equals(editBudget)) { dialogMode = DialogMode.EDIT; dialogTitle = "Edit expense"; item = budgetTableView.getSelectionModel().getSelectedItem(); budgetController.setBudget(item); } else { return; } dialog.setTitle(dialogTitle); dialog.showAndWait(); item = budgetController.getNewBudgetItem(); if(item != null && dialogMode == DialogMode.ADD){ listOfBudgetItems.add(item); } refreshObservableList(); } @FXML public void closeButton(javafx.event.ActionEvent actionEvent) { final Node source = (Node) actionEvent.getSource(); final Stage stage = (Stage) source.getScene().getWindow(); stage.close(); } @FXML public void deleteButton(ActionEvent event) { BudgetItem item = budgetTableView.getSelectionModel().getSelectedItem(); if (item == null) { return; } Optional<ButtonType> isConfirmed = showConfirmationDialog(); if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { general.deleteItemFromBudget(item.getBudgetCategory()); refreshObservableList(); } } private Optional<ButtonType> showConfirmationDialog() { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Confirm Delete"); alert.setHeaderText("Delete Confirmation"); alert.setContentText("Are you sure you would like to delete the selected entry?"); return alert.showAndWait(); } protected void refreshObservableList(){ budgetTableView.setItems(budgetList); this.budgetList.setAll(listOfBudgetItems); } }