Commit c92dbb97 authored by Carl Johan Gützkow's avatar Carl Johan Gützkow 🎮
Browse files

refactor: set javafx components in tables

parent 1cf559d5
......@@ -3,6 +3,9 @@ package edu.ntnu.idatt2001.carljgu.client;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import edu.ntnu.idatt2001.carljgu.client.dialogs.DialogBoxBuilder;
import javafx.application.Application;
......@@ -19,7 +22,7 @@ import static javafx.scene.control.Alert.AlertType.ERROR;
* Responsibility for creating the stage.
*
* @author Carl Gützkow
* @version 1.1 27.03.2022
* @version 1.2 6.05.2022
*/
public class App extends Application {
......@@ -34,14 +37,14 @@ public class App extends Application {
@Override
public void start(Stage startStage) {
stage = startStage;
Scene scene = null;
stage.setTitle("Wargames");
stage.setMaximized(true);
try {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getClassLoader().getResource("wargames.fxml"));
scene = new Scene(fxmlLoader.load(), 1300, 740);
Scene scene = new Scene(fxmlLoader.load(), 1300, 740);
scene.getStylesheets().add("stylesheet.css");
stage.setTitle("Wargames");
Path currentPath = Paths.get("").toAbsolutePath();
Path imagePath = Paths.get(currentPath.toString(), "src","main","resources","icons","wargames-logo.png");
Image icon = new Image(imagePath.toString());
......@@ -51,7 +54,12 @@ public class App extends Application {
stage.show();
} catch (IOException | IllegalStateException e) {
new DialogBoxBuilder(ERROR)
.addHeader("Program could not be started. File was not found.")
.addHeader("Program could not be started. Expand the \"Show details\" button to see full stack trace.")
.addListMessage(
Arrays.stream(e.getStackTrace())
.map(String::valueOf)
.collect(Collectors.toCollection(ArrayList::new)
))
.build().showAndWait();
}
......
......@@ -30,7 +30,7 @@ import static javafx.scene.control.Alert.AlertType.*;
* This includes requesting to import
* armies and simulate the battle
*
* @version 1.7 01.05.2022
* @version 1.8 06.05.2022
* @author Carl Gützkow
*/
public class BattleController implements Initializable {
......@@ -50,10 +50,10 @@ public class BattleController implements Initializable {
@FXML
private Text armyTwoImportPath, armyTwoName, armyTwoToString;
private final Text[] armyImportPaths = new Text[2];
private final Text[] armyNames = new Text[2];
private final Text[] armySummaries = new Text[2];
private final ArrayList<ListView<Unit>> armyUnitListViews = new ArrayList<>();
private Text[] armyImportPathTexts;
private Text[] armyNameTexts;
private Text[] armySummaryTexts;
private ListView[] armyUnitListViews;
@FXML
private ListView<String> attackList;
......@@ -62,7 +62,7 @@ public class BattleController implements Initializable {
private Text lastSimulation, score;
private Army armyOne, armyTwo;
private final Army[] armies = new Army[2];
private Army[] armies;
private final String[] filePaths = new String[2];
private final int[] scores = new int[2];
......@@ -225,8 +225,8 @@ public class BattleController implements Initializable {
/**
* Updates the information about an army
* in the chosen elements.
* Uses both armyNumber and army to be able to display armies that are not
* the have been changed. For example after a battle.
* Uses both armyNumber and army to be able to display armies that
* have changed. For example after a battle.
*
* @param armyNumber int - 0 or 1 depending on which elements to use to display an army.
* @param army Army - the army to display.
......@@ -235,7 +235,7 @@ public class BattleController implements Initializable {
public void displayArmy(int armyNumber, Army army) throws NullPointerException {
if (army == null) throw new NullPointerException("Army is a a null object");
armyNames[armyNumber].setText(army.getName());
armyNameTexts[armyNumber].setText(army.getName());
String armyRepresentation =
"Infantry units: " + army.getInfantryUnits().size() + "\n" +
......@@ -243,12 +243,12 @@ public class BattleController implements Initializable {
"Cavalry units: " + army.getCavalryUnits().size() + "\n" +
"Commander units: " + army.getCommanderUnits().size() + "\n";
armySummaries[armyNumber].setText(armyRepresentation);
armySummaryTexts[armyNumber].setText(armyRepresentation);
armyImportPaths[armyNumber].setText(filePaths[armyNumber]);
armyImportPathTexts[armyNumber].setText(filePaths[armyNumber]);
armyUnitListViews.get(armyNumber).getItems().clear();
armyUnitListViews.get(armyNumber).getItems().addAll(army.getAllUnits());
armyUnitListViews[armyNumber].getItems().clear();
armyUnitListViews[armyNumber].getItems().addAll(army.getAllUnits());
}
/**
......@@ -261,21 +261,15 @@ public class BattleController implements Initializable {
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
armyNames[0] = armyOneName;
armyNames[1] = armyTwoName;
armyImportPaths[0] = armyOneImportPath;
armyImportPaths[1] = armyTwoImportPath;
armySummaries[0] = armyOneToString;
armySummaries[1] = armyTwoToString;
armyUnitListViews.add(armyOneUnitsListView);
armyUnitListViews.add(armyTwoUnitsListView);
armies[0] = armyOne;
armies[1] = armyTwo;
armyNameTexts = new Text[] {armyOneName, armyTwoName};
armyImportPathTexts = new Text[] {armyOneImportPath, armyTwoImportPath};
armySummaryTexts = new Text[] {armyOneToString, armyTwoToString};
armyUnitListViews = new ListView[] {armyOneUnitsListView, armyTwoUnitsListView};
armies = new Army[] {armyOne, armyTwo};
resetArmiesButton.setDisable(true);
resetAndBattleButton.setDisable(true);
terrainChoiceBox.getItems().clear();
terrainChoiceBox.getItems().addAll(Terrain.PLAINS, Terrain.FOREST, Terrain.HILL);
terrainChoiceBox.setValue(Terrain.PLAINS);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment