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

Created controller for income

parent 231cf319
No related branches found
No related tags found
10 merge requests!43Merging frontend-testing into master,!38"Made progressbar dynamic in accordance to spending. Added balance field....,!37Made the sub progress bars respond to changes in expense,!32Added input validation to add dialog boxes.,!30Redesigned scenes,!29Redesigned scenes,!28Redesigned scenes,!26Redesigned Main menu and expense/income windows,!24Merging frontend-testing with master,!23Merging frontend-testing and master
package no.ntnu.idatt1002.demo.controller;
import java.io.IOException;
import java.util.Optional;
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.Parent;
import javafx.scene.Scene;
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.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeCategory;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
import no.ntnu.idatt1002.demo.data.Economics.FileHandling;
import no.ntnu.idatt1002.demo.data.Economics.Income;
import no.ntnu.idatt1002.demo.data.Economics.IncomeRegister;
import no.ntnu.idatt1002.demo.data.Economics.Item;
import no.ntnu.idatt1002.demo.data.Economics.ItemRegister;
public class IncomeController {
/**
* The mode of the dialog. NEW if new contact, EDIT if edit existing contact.
*/
private DialogMode dialogMode;
@FXML
private Button addBtn;
@FXML
private Button editBtn;
@FXML
private Button deleteBtn;
@FXML
private ComboBox<String> show;
@FXML
private Button expenseBtn;
@FXML
private Button overviewBtn;
@FXML
private Button budgetBtn;
@FXML
private Button returnBtn;
@FXML
private TableColumn<Income, Double> amountColumn;
@FXML
private TableColumn<Income, IncomeCategory> categoryColumn;
@FXML
private TableColumn<Income, String> dateColumn;
@FXML
private TableColumn<Income, String> descriptionColumn;
@FXML
private TableColumn<Income, Boolean> recurringColumn;
@FXML
private TableView<Income> incomeTableView;
IncomeRegister incomeRegister;
ObservableList<Income> income;
ObservableList<String> filter;
@FXML
public void initialize()
throws IOException { //TODO SAME REGISTER FOR BOTH, BUT LOAD DIFFERENT DATA DEPENDING ON WHICH IT IS
dateColumn.setCellValueFactory(new PropertyValueFactory<Income, String>("date"));
amountColumn.setCellValueFactory(new PropertyValueFactory<Income, Double>("amount"));
categoryColumn.setCellValueFactory(
new PropertyValueFactory<Income, IncomeCategory>("category"));
descriptionColumn.setCellValueFactory(new PropertyValueFactory<Income, String>("description"));
recurringColumn.setCellValueFactory(new PropertyValueFactory<Income, Boolean>("recurring"));
filter = FXCollections.observableArrayList("All", "Gift", "Salary", "Student loan",
"Fixed income");
incomeRegister = loadIncomeDataFromFile("Income");
income = FXCollections.observableArrayList(incomeRegister.getItems());
incomeTableView.setItems(income);
show.setItems(filter);
show.setValue("All");
}
@FXML
protected void handleAddButton(ActionEvent event) {
handleEditButton(event);
}
@FXML
protected void handleEditButton(ActionEvent event) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/AddIncome.fxml"));
Income newIncome = null;
String dialogTitle = "";
// Load the FXML file for your dialog box
Dialog<Income> dialog = new Dialog<>();
dialog.initModality(Modality.APPLICATION_MODAL);
try {
// Set the Dialog's content to the loaded FXML file
dialog.getDialogPane().setContent(loader.load());
} catch (IOException e) {
e.printStackTrace();
}
// Get the controller for the loaded FXML file
AddIncomeController dialogController = loader.getController();
if (event.getSource().equals(addBtn)) {
dialogMode = DialogMode.ADD;
dialogTitle = "Add income";
} else if (event.getSource().equals(editBtn)
&& incomeTableView.getSelectionModel().getSelectedItem() != null) {
dialogMode = DialogMode.EDIT;
dialogTitle = "Edit income";
newIncome = incomeTableView.getSelectionModel().getSelectedItem();
dialogController.setIncome(newIncome);
}
dialog.setTitle(dialogTitle);
// Show the Dialog and wait for the user to close it
dialog.showAndWait();
//Get the newly created income from the dialog pane
newIncome = dialogController.getNewIncome();
if (newIncome != null && dialogMode == DialogMode.ADD) {
incomeRegister.addItem(newIncome);
refreshObservableList();
}
}
//Only add the income to the tableview, if the income is not null
@FXML
public void handleDeleteBtn(ActionEvent event) {
Income chosenIncome = incomeTableView.getSelectionModel().getSelectedItem();
if (chosenIncome == null) {
return;
}
Optional<ButtonType> isConfirmed = showConfirmationDialog();
if (isConfirmed.isPresent() && isConfirmed.get() == ButtonType.OK) {
incomeRegister.removeItem(chosenIncome);
refreshObservableList();
}
}
protected void refreshObservableList() {
this.income.setAll(incomeRegister.getItems());
}
private Optional<ButtonType> showConfirmationDialog() {
Alert alert = new 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();
}
public IncomeRegister loadIncomeDataFromFile(String fileName) throws IOException {
//ItemRegister<T extends Item>
FileHandling fileHandling = new FileHandling();
if (fileHandling.isEmpty(fileName)) {
incomeRegister = new IncomeRegister();
} else {
try {
incomeRegister = fileHandling.readIncomeRegisterFromFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
return incomeRegister;
}
public void saveDataToFile(String fileName) throws IOException {
FileHandling fileHandling = new FileHandling();
fileHandling.writeItemRegisterToFile(incomeRegister, fileName);
}
@FXML
public void switchScene(ActionEvent event) throws IOException {
saveDataToFile("Income");
FXMLLoader loader = new FXMLLoader();
if (event.getSource() == expenseBtn) {
loader.setLocation(SceneController.class.getResource("/view/Expenses.fxml"));
} else if (event.getSource() == overviewBtn) {
loader.setLocation(SceneController.class.getResource("/view/Overview.fxml"));
} else if (event.getSource() == returnBtn) {
loader.setLocation(SceneController.class.getResource("/view/FirstMenu.fxml"));
} else if (event.getSource() == budgetBtn) {
loader.setLocation(SceneController.class.getResource("/view/underProgress.fxml"));
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
}
\ No newline at end of file
......@@ -33,7 +33,7 @@
<top>
<HBox BorderPane.alignment="CENTER">
<children>
<Button fx:id="returnBtn" mnemonicParsing="false" onAction="#switchScene" text="Return ">
<Button mnemonicParsing="false" onAction="#switchStartMenu" text="Return ">
<opaqueInsets>
<Insets left="100.0" />
</opaqueInsets>
......@@ -73,7 +73,7 @@
<children>
<HBox alignment="BOTTOM_LEFT" prefWidth="410.0" spacing="5.0">
<children>
<Button fx:id="addBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleAddButton" text="Add" textAlignment="CENTER">
<Button fx:id="add" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#addIncome" text="Add" textAlignment="CENTER">
<graphic>
<ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
<image>
......@@ -82,7 +82,7 @@
</ImageView>
</graphic>
</Button>
<Button fx:id="editBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleEditButton" text="Edit" textAlignment="CENTER">
<Button alignment="TOP_CENTER" mnemonicParsing="false" text="Edit" textAlignment="CENTER">
<graphic>
<ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
<image>
......@@ -91,7 +91,7 @@
</ImageView>
</graphic>
</Button>
<Button fx:id="deleteBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleDeleteBtn" text="Delete" textAlignment="CENTER">
<Button alignment="TOP_CENTER" mnemonicParsing="false" text="Delete" textAlignment="CENTER">
<graphic>
<ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
<image>
......@@ -122,10 +122,15 @@
</VBox>
<HBox prefHeight="100.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowIndex="2">
<children>
<Button disable="true" mnemonicParsing="false" text="Income" />
<Button fx:id="budgetBtn" mnemonicParsing="false" onAction="#switchScene" text="Budget" />
<Button mnemonicParsing="false" text="Expenses" />
<Button mnemonicParsing="false" onAction="#switchScene" text="Next">
<Button mnemonicParsing="false" onAction="#switchOverview" text="Overview">
<HBox.margin>
<Insets right="5.0" />
</HBox.margin>
</Button>
<Button disable="true" mnemonicParsing="false" onAction="#switchIncome" text="Income" />
<Button mnemonicParsing="false" onAction="#switchExpenses" text="Expenses" />
<Button disable="true" mnemonicParsing="false" text="Savings" />
<Button mnemonicParsing="false" onAction="#switchExpenses" text="Next">
<HBox.margin>
<Insets left="170.0" />
</HBox.margin>
......@@ -135,13 +140,13 @@
<Insets top="10.0" />
</padding>
</HBox>
<TableView fx:id="incomeTableView" prefHeight="260.0" prefWidth="485.0" GridPane.columnSpan="2" GridPane.rowIndex="1">
<TableView fx:id="expenseTableView" prefHeight="260.0" prefWidth="485.0" GridPane.columnSpan="2" GridPane.rowIndex="1">
<columns>
<TableColumn fx:id="dateColumn" prefWidth="75.0" text="Date" />
<TableColumn fx:id="amountColumn" prefWidth="75.0" text="Amount" />
<TableColumn fx:id="categoryColumn" prefWidth="75.0" text="Category" />
<TableColumn fx:id="descriptionColumn" prefWidth="75.0" text="Description" />
<TableColumn fx:id="recurringColumn" prefWidth="75.0" text="Recurring" />
<TableColumn fx:id="date" prefWidth="75.0" text="Date" />
<TableColumn fx:id="amount" prefWidth="75.0" text="Amount" />
<TableColumn fx:id="category" prefWidth="75.0" text="Category" />
<TableColumn fx:id="description" prefWidth="75.0" text="Description" />
<TableColumn fx:id="recurring" prefWidth="75.0" text="Recurring" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
......
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