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

Made entries double-clickable when choosing budget

parent ac064e62
No related branches found
No related tags found
1 merge request!51Added javadoc to all methods
......@@ -4,16 +4,21 @@ import java.io.IOException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
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.Label;
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.FileHandlingBudgetArchive;
import no.ntnu.idatt1002.demo.data.Budget.FileHandlingSelectedBudget;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
/**
* Class for selecting a budget from the archive.
......@@ -34,10 +39,14 @@ public class SelectBudgetController {
private BudgetRegister budgetRegister;
private ObservableList<String> budgetList;
/**
* Initializes the view when it is loaded. Prepares the view by adding event filters,
* instantiating the budget register and setting the list view.
* @throws IOException
*/
@FXML
public void initialize() throws IOException {
//Prevent users from choosing nothing
okBtn.addEventFilter(
ActionEvent.ACTION, event -> {
if (budgetListView.getSelectionModel().getSelectedItem() == null) {
......@@ -46,6 +55,7 @@ public class SelectBudgetController {
}
});
//Load budget register from file.
try {
if (FileHandlingBudgetArchive.isBudgetRegisterEmpty("budgets/Archive")) {
budgetRegister = new BudgetRegister();
......@@ -56,32 +66,68 @@ public class SelectBudgetController {
ioe.printStackTrace();
}
System.out.println("budget register is: " + budgetRegister);
budgetList = FXCollections.observableList(budgetRegister.getBudgetNames());
ObservableList<String> budgetList = FXCollections.observableList(
budgetRegister.getBudgetNames());
budgetListView.setItems(budgetList);
//Double clicking the entry also chooses it
budgetListView.setOnMouseClicked(mouseEvent-> {
if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
if(mouseEvent.getClickCount() == 2){
System.out.println("Double clicked");
selectBudget(mouseEvent);
}
}
});
}
/**
* Selects the budget in the budget list view and stores this value as
* the currently selected budget.
* @param event Double mouseclick or a click on the ok button.
*/
@FXML
public void selectBudget(ActionEvent event) throws IOException {
String name = budgetListView.getSelectionModel().getSelectedItem();
FileHandlingSelectedBudget.updateSelectedBudget(name, "budgets/SelectedBudget");
public void selectBudget(Event event) {
try {
String name = budgetListView.getSelectionModel().getSelectedItem();
FileHandlingSelectedBudget.updateSelectedBudget(name, "budgets/SelectedBudget");
} catch(IOException ioe) {
showErrorDialogBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage());
}
final Node source = (Node) event.getSource();
((Stage) source.getScene().getWindow()).close();
}
/**
* Closes the dialog box and clears the currently selected budget.
* @param event Button press on cancel button
*/
@FXML
public void exitWindow(ActionEvent event) throws IOException {
FileHandlingSelectedBudget.clearSelectedBudget("budgets/SelectedBudget");
public void exitWindow(ActionEvent event) {
try {
FileHandlingSelectedBudget.clearSelectedBudget("budgets/SelectedBudget");
} catch (IOException ioe) {
showErrorDialogBox(ioe.getMessage(), ioe.getMessage(), ioe.getMessage());
}
final Node source = (Node) event.getSource();
((Stage) source.getScene().getWindow()).close();
}
/**
* Displays an error message dialog box with a customizable title, header and content.
* @param title The dialog title.
* @param header The dialog header.
* @param content The dialog content.
*/
public void showErrorDialogBox(String title, String header, String content) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.showAndWait();
}
}
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