From 1e02cbe75a91cb6b96d3944323e6357f40223e17 Mon Sep 17 00:00:00 2001 From: Harry Linrui XU <xulr0820@hotmail.com> Date: Mon, 27 Mar 2023 14:18:53 +0200 Subject: [PATCH] "Refactored method names and added new handleAddButton method" --- .../demo/controller/BudgetController.java | 46 ++++++++----------- src/main/resources/view/Budget.fxml | 6 +-- src/main/resources/view/BudgetNew.fxml | 6 +-- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java index b366b962..da50a50b 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/BudgetController.java @@ -75,8 +75,7 @@ public class BudgetController { /** - * Initializes the budget register, the observable budget list and the tableview, along with all dynamic fields that relates to the tableview. - * The method is called each time the FXML of this scene is loaded. + * Initializes the budget register, the observable budget list and the tableview, along with the values of the dropbox used for filtering the tableview. * @throws IOException If there occurs any exception when loading the budget register from a file. */ @FXML @@ -95,13 +94,17 @@ public class BudgetController { sum.setText(String.valueOf(general.totalSum())); } + @FXML + protected void handleAddButton(ActionEvent event) { + handleEditButton(event); + } /** * Adds or edits a budget item, depending on what mode the DialogMode enum is at. The method brings up a dialog box popup in which the user can fill and choose * values that the budget item will have. Open exiting the popup, the changes the saved to the tableview. * @param event A button click on either the add or delete button. */ @FXML - public void switchAddBudget(ActionEvent event) { + public void handleEditButton(ActionEvent event) { BudgetItem item = null; String dialogTitle = ""; DialogMode dialogMode; @@ -124,7 +127,6 @@ public class BudgetController { if(event.getSource().equals(addBudget)){ dialogMode = DialogMode.ADD; dialogTitle = "New Budget"; - } else if (event.getSource().equals(editBudget) && budgetTableView.getSelectionModel().getSelectedItem() != null) { dialogMode = DialogMode.EDIT; @@ -138,6 +140,7 @@ public class BudgetController { } dialog.setTitle(dialogTitle); + // Show the Dialog and wait for the user to close it dialog.showAndWait(); //Adds the new item to the register @@ -153,23 +156,13 @@ public class BudgetController { refreshTableView(); } - /** - * Closes the popup window. - * @param event A button click on the close button - */ - @FXML - public void closeButton(ActionEvent event) { - final Node source = (Node) event.getSource(); - final Stage stage = (Stage) source.getScene().getWindow(); - stage.close(); - } /** * Deletes an entry from the tableview, if an entry has been selected. The method brings up a popup window, asking for confirmation for deleting the entry. * @param event A button click on the delete button */ @FXML - public void deleteButton(ActionEvent event) { + public void handleDeleteBtn(ActionEvent event) { //Gets the selected item from the tableview BudgetItem item = budgetTableView.getSelectionModel().getSelectedItem(); //Exits the method if nothing is selected @@ -184,9 +177,19 @@ public class BudgetController { } } + /** + * Method for synching the register with the tableview. The observable list to which the tableview is set, is being refilled with all the entries + * in the register, keeping it updated with new changes. + */ + protected void refreshTableView(){ + this.budgetList.setAll(general.getBudgetItems()); + //Refreshing the sum of the amounts of the budget + this.sum.setText(String.valueOf(general.totalSum())); + } + /** * Returns an optional, which is a popup alert box, asking for confirmation for deleting an entry. - * @return An alertbox, asking for confirmation for deleting the selected entry of the tableview. + * @return An alert box, asking for confirmation for deleting the selected entry of the tableview. */ private Optional<ButtonType> showConfirmationDialog() { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); @@ -240,17 +243,6 @@ public class BudgetController { fileHandlingBudget.writeGeneralBudgetToFile(fileName, general); } - - /** - * Method for synching the register with the tableview. The observable list to which the tableview is set, is being refilled with all the entries - * in the register, keeping it updated with new changes. - */ - protected void refreshTableView(){ - this.budgetList.setAll(general.getBudgetItems()); - //Refreshing the sum of the amounts of the budget - this.sum.setText(String.valueOf(general.totalSum())); - } - /** * Switches scenes from the budget scene to another, by loading a new FXML file and setting the scene to this location. * The destination depends entirely on which button is pressed. diff --git a/src/main/resources/view/Budget.fxml b/src/main/resources/view/Budget.fxml index 461014f8..9b1f0cf2 100644 --- a/src/main/resources/view/Budget.fxml +++ b/src/main/resources/view/Budget.fxml @@ -35,9 +35,9 @@ <children> <HBox alignment="TOP_CENTER" prefHeight="98.0" prefWidth="600.0" spacing="40.0" GridPane.rowIndex="3"> <children> - <Button fx:id="addBudget" minHeight="60.0" minWidth="100.0" mnemonicParsing="false" onAction="#switchAddBudget" prefWidth="100.0" text="Add" /> - <Button fx:id="editBudget" minHeight="60.0" minWidth="100.0" mnemonicParsing="false" onAction="#switchAddBudget" prefWidth="100.0" text="Edit" /> - <Button fx:id="deleteBudget" minHeight="60.0" minWidth="100.0" mnemonicParsing="false" onAction="#deleteButton" prefWidth="100.0" text="Delete" /> + <Button fx:id="addBudget" minHeight="60.0" minWidth="100.0" mnemonicParsing="false" onAction="#handleEditButton" prefWidth="100.0" text="Add" /> + <Button fx:id="editBudget" minHeight="60.0" minWidth="100.0" mnemonicParsing="false" onAction="#handleEditButton" prefWidth="100.0" text="Edit" /> + <Button fx:id="deleteBudget" minHeight="60.0" minWidth="100.0" mnemonicParsing="false" onAction="#handleDeleteBtn" prefWidth="100.0" text="Delete" /> <Button minHeight="60.0" minWidth="100.0" mnemonicParsing="false" prefWidth="100.0" text="Save/Back" /> </children> </HBox> diff --git a/src/main/resources/view/BudgetNew.fxml b/src/main/resources/view/BudgetNew.fxml index 9cda09ce..c92fd969 100644 --- a/src/main/resources/view/BudgetNew.fxml +++ b/src/main/resources/view/BudgetNew.fxml @@ -76,7 +76,7 @@ <children> <HBox alignment="BOTTOM_LEFT" prefWidth="410.0" spacing="5.0"> <children> - <Button fx:id="addBudget" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#switchAddBudget" text="Add" textAlignment="CENTER"> + <Button fx:id="addBudget" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleAddButton" text="Add" textAlignment="CENTER"> <graphic> <ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true"> <image> @@ -85,7 +85,7 @@ </ImageView> </graphic> </Button> - <Button fx:id="editBudget" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#switchAddBudget" text="Edit" textAlignment="CENTER"> + <Button fx:id="editBudget" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleEditButton" text="Edit" textAlignment="CENTER"> <graphic> <ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true"> <image> @@ -94,7 +94,7 @@ </ImageView> </graphic> </Button> - <Button fx:id="deleteBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#deleteButton" text="Delete" textAlignment="CENTER"> + <Button fx:id="deleteBtn" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#handleDeleteBtn" text="Delete" textAlignment="CENTER"> <graphic> <ImageView fitHeight="19.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true"> <image> -- GitLab