package no.ntnu.idatt1002.demo.controller; import java.io.IOException; import java.time.LocalDate; import java.util.Optional; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingBudget; import no.ntnu.idatt1002.demo.data.Budget.FileHandlingSelectedBudget; import no.ntnu.idatt1002.demo.data.Economics.Expense; public class CreateBudgetController { public Button okBtn; public Button cancelBtn; private String currentMonth; private Expense expense; private String budgetName; @FXML private TextField nameField; @FXML private Label errorMsg; @FXML public void initialize() { currentMonth = String.valueOf(LocalDate.now().getMonth()); okBtn.addEventFilter( ActionEvent.ACTION, event -> { if(nameField.getText().equals("")) { errorMsg.setOpacity(1); event.consume(); } }); okBtn.addEventFilter( ActionEvent.ACTION, event -> { //if(hasDuplicate() { //TODO MAYBE THIS IN THE START BUDGET FILE //errorMsg.setText("Budget for this month already exist") } ); } @FXML public void pressOkBtn(ActionEvent event) { String title = "Confirm name"; String header = "Are you sure you to use this name?"; String content = "The name cannot be changed later"; budgetName = nameField.getText(); System.out.println(budgetName); if(!createNewFiles(budgetName)) { updateCurrentFile("", ""); errorMsg.setText("A budget of the same \nname already exists"); errorMsg.setOpacity(1); return; } Optional<ButtonType> isConfirmed = showConfirmationDialog(title, header, content); if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) { updateCurrentFile(currentMonth, budgetName); } else { updateCurrentFile("", ""); } final Node source = (Node) event.getSource(); ((Stage) source.getScene().getWindow()).close(); } @FXML public void pressCancelBtn(ActionEvent event) { updateCurrentFile("", ""); final Node source = (Node) event.getSource(); ((Stage) source.getScene().getWindow()).close(); } /** * Returns an optional, which is a popup alert box, asking for confirmation for deleting an entry. * @return An alert box, asking for confirmation for deleting the selected entry of the tableview. */ @FXML private Optional<ButtonType> showConfirmationDialog(String title, String header, String content) { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle(title); alert.setHeaderText(header); alert.setContentText(content); return alert.showAndWait(); } private void showErrorMsgBox(String title, String header, String content) { Alert alert = new Alert(AlertType.ERROR); alert.setTitle(title); alert.setHeaderText(header); alert.setContentText(content); alert.showAndWait(); } public boolean createNewFiles(String budgetName) { boolean empty; try { empty = FileHandlingSelectedBudget.createBudgetDirectory(currentMonth + budgetName); FileHandlingSelectedBudget.createNewIncomeFile(currentMonth + budgetName, "Income"); FileHandlingSelectedBudget.createNewExpenseFile(currentMonth + budgetName, "Expense"); FileHandlingSelectedBudget.createNewBudgetFile(currentMonth + budgetName, "Budget"); } catch (IOException ioe) { empty = false; System.out.println(ioe.getMessage()); ioe.printStackTrace(); showErrorMsgBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); } return empty; } public void updateCurrentFile(String currentMonth, String budgetName) { try { FileHandlingSelectedBudget.updateSelectedBudget(currentMonth + budgetName); } catch (IOException ioe) { showErrorMsgBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage()); } } }