diff --git a/src/main/java/edu/ntnu/idatt2001/carljgu/client/BattleController.java b/src/main/java/edu/ntnu/idatt2001/carljgu/client/BattleController.java index 5d6656a269b60193b9c7b7adcee4a0e2d485a245..4a80f417e4458d3ec594d5ed7210e2edce39c4b3 100644 --- a/src/main/java/edu/ntnu/idatt2001/carljgu/client/BattleController.java +++ b/src/main/java/edu/ntnu/idatt2001/carljgu/client/BattleController.java @@ -4,20 +4,24 @@ import edu.ntnu.idatt2001.carljgu.Terrain; import edu.ntnu.idatt2001.carljgu.battle.Battle; import edu.ntnu.idatt2001.carljgu.FileExtensionException; import edu.ntnu.idatt2001.carljgu.ArmyFileHandler; +import edu.ntnu.idatt2001.carljgu.client.dialogs.DialogBoxBuilder; import edu.ntnu.idatt2001.carljgu.units.Army; import edu.ntnu.idatt2001.carljgu.units.Unit; +import edu.ntnu.idatt2001.carljgu.units.UnitFactory; import java.io.IOException; import java.net.URL; import java.util.ArrayList; +import java.util.Optional; import java.util.ResourceBundle; -import edu.ntnu.idatt2001.carljgu.units.UnitFactory; -import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.fxml.FXML; +import javafx.scene.control.ButtonType; import javafx.scene.control.ListView; import javafx.scene.input.MouseButton; import javafx.scene.text.Text; +import static javafx.scene.control.Alert.AlertType.*; /** * A controller class which @@ -61,26 +65,36 @@ public class BattleController implements Initializable { * display its information in the * given text elements. * - * @param army - int - the selected army to import. + * @param armyNumber - int - the selected army to import. * 0 for army one and 1 for army two */ - private void importArmy(int army) { + private void importArmy(int armyNumber) { ArmyFileHandler fileHandler = new ArmyFileHandler(); + new DialogBoxBuilder(ERROR).build().showAndWait(); try { - filePaths[army] = fileHandler.getFilePath(); + filePaths[armyNumber] = fileHandler.getFilePath(); - if (filePaths[army] == null) + if (filePaths[armyNumber] == null) return; - armies[army] = fileHandler.readArmyFromFile(filePaths[army]); - displayArmy(army, armies[army]); - DialogBoxUtility.giveInformation("Army was successfully imported.\n" + - "In the process " + fileHandler.getReadLinesSkipped() + " units were corrupted"); + armies[armyNumber] = fileHandler.readArmyFromFile(filePaths[armyNumber]); + displayArmy(armyNumber, armies[armyNumber]); + new DialogBoxBuilder(INFORMATION) + .addTitle("Army imported") + .addMessage("Army was successfully imported.\n" + + "In the process " + fileHandler.getReadLinesSkipped() + " units were corrupted") + .build().showAndWait(); } catch (FileExtensionException e) { - DialogBoxUtility.giveError(e.getMessage()); + new DialogBoxBuilder(ERROR) + .addTitle("File is not supported") + .addMessage(e.getMessage()) + .build().showAndWait(); } catch (IOException e) { - DialogBoxUtility.giveError("File could not be loaded."); + new DialogBoxBuilder(ERROR) + .addHeader("File could not be loaded") + .addMessage(DialogBoxBuilder.recurringErrorMessage) + .build().showAndWait(); } scores[0] = 0; scores[1] = 0; @@ -134,9 +148,14 @@ public class BattleController implements Initializable { result = true; } catch (IllegalArgumentException e) { - DialogBoxUtility.giveError(e.getMessage()); + new DialogBoxBuilder(ERROR) + .addMessage(e.getMessage()) + .build().showAndWait(); } catch (NullPointerException e) { - DialogBoxUtility.giveError("Armies have not been imported."); + new DialogBoxBuilder(NONE) + .addTitle("Army does not exist") + .addMessage("Armies have not been imported") + .build().showAndWait(); } return result; } @@ -165,7 +184,9 @@ public class BattleController implements Initializable { displayArmy(0, armyOne); displayArmy(1, armyTwo); } catch (UnsupportedOperationException | NullPointerException e) { - DialogBoxUtility.giveError(e.getMessage()); + new DialogBoxBuilder(ERROR) + .addMessage(e.getMessage()) + .build().showAndWait(); } } @@ -210,14 +231,17 @@ public class BattleController implements Initializable { if (click.getButton() == MouseButton.PRIMARY && click.getClickCount() == 2) { Unit selectedUnit = armyUnitListViews.get(armyNumber).getSelectionModel().getSelectedItem(); if (selectedUnit != null) { - boolean result = DialogBoxUtility.getConfirmation("Are you sure you want to delete this unit? \n" + - selectedUnit); + Optional<ButtonType> optional = new DialogBoxBuilder(CONFIRMATION) + .addTitle("Delete unit?") + .addMessage("Are you sure you want to delete this unit?") + .build().showAndWait(); + boolean result = optional.filter(buttonType -> buttonType == ButtonType.OK).isPresent(); if (result) { armies[armyNumber].remove(selectedUnit); + displayArmy(armyNumber, armies[armyNumber]); } } } - displayArmy(armyNumber, armies[armyNumber]); }); } diff --git a/src/main/java/edu/ntnu/idatt2001/carljgu/client/DialogBoxUtility.java b/src/main/java/edu/ntnu/idatt2001/carljgu/client/DialogBoxUtility.java deleted file mode 100644 index 8af5e7ed43857824eec1a9882de646e265131cf5..0000000000000000000000000000000000000000 --- a/src/main/java/edu/ntnu/idatt2001/carljgu/client/DialogBoxUtility.java +++ /dev/null @@ -1,77 +0,0 @@ -package edu.ntnu.idatt2001.carljgu.client; - - -import javafx.scene.control.Alert; -import javafx.scene.control.ButtonType; -import javafx.scene.layout.Region; - -import java.util.Optional; - -/** - * A utility class with static methods - * and private constructor for giving - * out useful alerts and dialogs. - * - * @author Carl Gützkow - * @version 1.1 13.04.2022 - */ -public class DialogBoxUtility { - - private DialogBoxUtility() { - throw new UnsupportedOperationException("It is not possible to create an instance of this utility class."); - } - - /** - * Creates an information dialog box. - * Static method that can be called by the controller. - * - * @param message String - the message to display - */ - public static void giveInformation(String message) { - Alert alert = new Alert(Alert.AlertType.INFORMATION); - alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE); - alert.setTitle("Information"); - alert.setHeaderText(null); - alert.setContentText(message); - - alert.showAndWait(); - } - - /** - * Creates an error dialog box. - * Static method that can be called by the controller. - * - * @param message String - the message to display - */ - public static void giveError(String message) { - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE); - alert.setTitle("Error"); - alert.setHeaderText(message); - alert.setContentText("If this a recurring and unsolvable event, please contact the creator of this program."); - - alert.showAndWait(); - } - - /** - * Creates a confirmation dialog box. - * Returns a boolean value if depending on - * what the user clicked on. - * Static method that can be called by the controller. - * If the user closes the window, false is returned. - * - * @param message String - the message to display - * @return result - Boolean - the confirmation from the user - */ - public static boolean getConfirmation(String message) { - Alert alert = new Alert(Alert.AlertType.CONFIRMATION); - alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE); - alert.setTitle("Confirmation"); - alert.setHeaderText(null); - alert.setContentText(message); - - Optional<ButtonType> result = alert.showAndWait(); - return result.filter(buttonType -> buttonType == ButtonType.OK).isPresent(); - } - -} diff --git a/src/main/java/edu/ntnu/idatt2001/carljgu/client/dialogs/DialogBox.java b/src/main/java/edu/ntnu/idatt2001/carljgu/client/dialogs/DialogBox.java new file mode 100644 index 0000000000000000000000000000000000000000..8a800b1fc81eeb6974c76331e8c91138ad0d8a2d --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2001/carljgu/client/dialogs/DialogBox.java @@ -0,0 +1,45 @@ +package edu.ntnu.idatt2001.carljgu.client.dialogs; + +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; +import javafx.scene.image.ImageView; +import javafx.stage.Stage; + +/** + * A dialog box that extends alert to use + * a builder to build a dialog box. + * + * @author Carl Gützkow + * @version 1.1 17.04.2022 + */ +public class DialogBox extends Alert { + + /** + * Constructor for a dialog box + * that uses a builder to set + * attributes. If the alert type is NONE, + * the window will only close if there is at + * least one button. + * If the builder has no image defined, then + * the default image is used. Otherwise, this + * constructor will add image from resource folder. + * + * @param builder + */ + public DialogBox(DialogBoxBuilder builder) { + super(builder.getAlertType()); + this.setTitle(builder.getTitle()); + this.setHeaderText(builder.getHeader()); + this.setContentText(builder.getMessage()); + + if (builder.getAlertType() == AlertType.NONE) { + this.getDialogPane().getButtonTypes().add(ButtonType.OK); + } + if (builder.getImage() != null) { + this.setGraphic(new ImageView(builder.getImage())); + Stage stage = (Stage) this.getDialogPane().getScene().getWindow(); + stage.getIcons().add(builder.getImage()); + } + } + +} diff --git a/src/main/java/edu/ntnu/idatt2001/carljgu/client/dialogs/DialogBoxBuilder.java b/src/main/java/edu/ntnu/idatt2001/carljgu/client/dialogs/DialogBoxBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..dd51f27d614525397ea18369b3a9894064b5400b --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2001/carljgu/client/dialogs/DialogBoxBuilder.java @@ -0,0 +1,145 @@ +package edu.ntnu.idatt2001.carljgu.client.dialogs; + +import javafx.scene.control.Alert.AlertType; +import javafx.scene.image.Image; + +/** + * A builder class which simplifies the + * DialogBox class. Using a builder makes it easy + * to create complex dialogs while still avoiding + * the telescoping constructors problem. + * It also makes it easier to add attributes in the future. + * + * @author Carl Gützkow + * @version 1.1 17.04.2022 + */ +public class DialogBoxBuilder { + + public static final String recurringErrorMessage = + "If this a recurring and unsolvable event, please contact the creator of this program."; + + private AlertType alertType; + private String title; + private String header; + private String message; + private Image image; + + /** + * Instantiates a new dialog box builder. + * Sets the title and message to a default. + * They are still optional to set. + * + * @param alertType AlertType - enum from inside the Alert class. + * Either ERROR, INFORMATION, NONE, CONFIRMATION or WARNING + */ + public DialogBoxBuilder(AlertType alertType) { + this.alertType = alertType; + this.title = alertType.name(); + this.message = recurringErrorMessage; + } + + /** + * Create the dialog box with the + * current configurations from this + * builder object. + * + * @return dialogBox - DialogBox - a dialog box with this builder's configurations. + */ + public DialogBox build() { + return new DialogBox(this); + } + + /** + * Add title to the dialog box builder configuration. + * The title is displayed on the top of the window. + * + * @param title String - the title of the dialog box + * @return dialogBoxBuilder - DialogBoxBuilder - returned to add further configurations. + */ + public DialogBoxBuilder addTitle(String title) { + this.title = title; + return this; + } + + /** + * Add header to the dialog box builder configuration. + * A header is displayed above the message + * + * @param header String - the header of the dialog box + * @return dialogBoxBuilder - DialogBoxBuilder - returned to add further configurations. + */ + public DialogBoxBuilder addHeader(String header) { + this.header = header; + return this; + } + + /** + * Add message to the dialog box builder configuration. + * + * @param message String - the message of the dialog box + * @return dialogBoxBuilder - DialogBoxBuilder - returned to add further configurations. + */ + public DialogBoxBuilder addMessage(String message) { + this.message = message; + return this; + } + + /** + * Add image to the dialog box builder configuration. + * The image is displayed as both the window icon + * and on the actual scene. + * + * @param imageName String - the name of the image to display. + * @return dialogBoxBuilder - DialogBoxBuilder - returned to add further configurations. + */ + public DialogBoxBuilder addImage(String imageName) { + this.image = new Image(imageName); + return this; + } + + /** + * Gets the alert type for the dialog box. + * + * @return alertType - AlertType - enum from inside the Alert class. + * Either ERROR, INFORMATION, NONE, CONFIRMATION or WARNING + */ + public AlertType getAlertType() { + return alertType; + } + + /** + * Gets the title of the dialog box. + * + * @return title - String - the dialog box' title + */ + public String getTitle() { + return title; + } + + /** + * Gets the header for the dialog box. + * + * @return header - String - the dialog box' header + */ + public String getHeader() { + return header; + } + + /** + * Gets the message for the dialog box. + * + * @return message - String - the dialog box' message + */ + public String getMessage() { + return message; + } + + /** + * Gets the image for the dialog box- + * + * @return image - Image - the dialog box' icon and image + */ + public Image getImage() { + return image; + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 6179f57ab3cde3556ba9ea338cd13ebc7f8b5b13..98e2cdec3af61cbb4591a12809cdbb5c8a8ceed6 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -8,4 +8,6 @@ module edu.ntnu.idatt2001.carljgu.client { exports edu.ntnu.idatt2001.carljgu.units ; exports edu.ntnu.idatt2001.carljgu ; exports edu.ntnu.idatt2001.carljgu.battle; + exports edu.ntnu.idatt2001.carljgu.client.dialogs; + opens edu.ntnu.idatt2001.carljgu.client.dialogs to javafx.fxml; }