Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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.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 = FileHandlingBudget.createBudgetDirectory(currentMonth + budgetName);
FileHandlingBudget.createNewIncomeFile(currentMonth + budgetName, "Income");
FileHandlingBudget.createNewExpenseFile(currentMonth + budgetName, "Expense");
FileHandlingBudget.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 {
FileHandlingBudget.updateCurrentFile(currentMonth + budgetName);
} catch (IOException ioe) {
showErrorMsgBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage());
}
}
}