From 171cf7ef4e866c867b0d7571aac317e64408fe44 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 1 May 2022 10:27:21 +0200 Subject: [PATCH] implemented functionality to add new Tournament --- .../projekt2/turnierverwaltung/FileIO.java | 13 +++-- .../turnierverwaltung/Tournament.java | 54 +++++++++++++++++- .../TournamentListController.java | 44 +++++++++----- .../tournamentList/tournamentList.fxml | 23 +++----- .../turnierverwaltung/FileIOTest.java | 7 ++- .../FileIORead/saves/test1.txt | Bin 102 -> 265 bytes 6 files changed, 105 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index 9b9365d..fd46713 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -1,6 +1,9 @@ package ch.zhaw.projekt2.turnierverwaltung; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + import java.io.*; import java.net.URI; import java.util.ArrayList; @@ -34,13 +37,13 @@ public class FileIO { } } - public List getList() { + public ObservableList getList() { logger.fine("Creating a List out of all Files in the save directory and returning it"); - List tournaments = new ArrayList<>(); + ObservableList tournamentFiles = FXCollections.observableArrayList(); for(File tournament : saves.listFiles()){ - tournaments.add(new TournamentFile(tournament.toURI())); + tournamentFiles.add(new TournamentFile(tournament.toURI())); } - return tournaments; + return tournamentFiles; } /** @@ -86,7 +89,7 @@ public class FileIO { return tournament; } - public void saveTournament(Tournament tournament) { + public void saveTournament(Tournament tournament) throws IOException { if (tournament == null) { logger.warning("Given tournament file is empty"); throw new IllegalArgumentException("Null tournament received"); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java index 0da286d..2966009 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -1,12 +1,22 @@ package ch.zhaw.projekt2.turnierverwaltung; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + import java.io.Serializable; public class Tournament implements Serializable { private String name; + private Type type; - public Tournament(String name){ + + + public Tournament(String name, Type type) throws InvalidNameException { + if(!name.matches("[\\w]{1,20}")){ + throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging. + } setName(name); + setType(type); } public String getName() { @@ -16,4 +26,46 @@ public class Tournament implements Serializable { public void setName(String name) { this.name = name; } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public enum Type { + KO("KO-System"), GROUPS("Gruppenspiele"); + + private String name; + + private Type(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + + public static ObservableList getObservableList(){ + ObservableList items = FXCollections.observableArrayList(); + items.addAll(values()); + return items; + } + } + + public class InvalidNameException extends Exception { + public InvalidNameException() { + super(); + } + + public InvalidNameException(String errorMessage) { + super(errorMessage); + } + + } + + } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java index 70ad12f..1547ab1 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java @@ -1,14 +1,13 @@ package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; import ch.zhaw.projekt2.turnierverwaltung.FXController; +import ch.zhaw.projekt2.turnierverwaltung.FileIO; +import ch.zhaw.projekt2.turnierverwaltung.Tournament; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.ChoiceBox; -import javafx.scene.control.Label; -import javafx.scene.control.ListView; +import javafx.scene.control.*; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; @@ -17,6 +16,11 @@ import java.io.IOException; public class TournamentListController extends FXController { + @FXML + public void initialize(){ + modusChoiceBox.setItems(Tournament.Type.getObservableList()); + } + @FXML private Button createBtn; @@ -24,7 +28,7 @@ public class TournamentListController extends FXController { private GridPane grid; @FXML - private ChoiceBox modusChoiceBox; + private ChoiceBox modusChoiceBox; @FXML private Label newTournamentFormularTitle; @@ -36,7 +40,7 @@ public class TournamentListController extends FXController { private Label tournamentListTitle; @FXML - private ListView tournamentListView; + private ListView tournamentListView; @FXML private Label tournamentModLabel; @@ -44,8 +48,26 @@ public class TournamentListController extends FXController { @FXML private Label tournamentNameLabel; + @FXML + private TextField tournamentNameField; + @FXML void createTournament(ActionEvent event) { + for(FileIO.TournamentFile file : getFileIO().getList()) { + if(file.toString().equals(tournamentNameField.getText())){ + return; //TODO handle and logging + // Tournament with same name exists already. + } + } + try { + Tournament tournament = new Tournament(tournamentNameField.getText(), modusChoiceBox.getValue()); + getFileIO().saveTournament(tournament); + loadContent(); + } catch (Tournament.InvalidNameException e) { + e.printStackTrace(); //TODO handle and logging + } catch (IOException e) { + e.printStackTrace(); //TODO handle and logging + } } @@ -54,21 +76,17 @@ public class TournamentListController extends FXController { try { File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); - getFactory().loadParticipantFormular((BorderPane) getPane()); + getFactory().loadParticipantFormular((BorderPane) getPane()); //TODO load TournamentView instead of ParticipantFormular? } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); - } + } //TODO handle and logging } @Override public void loadContent() { - ObservableList tournamentFiles = FXCollections.observableArrayList(); - for(File tournament : getFileIO().getList()){ - tournamentFiles.add(tournament); - } - tournamentListView.setItems(tournamentFiles); + tournamentListView.setItems(getFileIO().getList()); } } diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml index 9dbbfeb..3648fdf 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml @@ -1,20 +1,11 @@ - - - - - - - - - - - - - + + + + - + @@ -66,12 +57,12 @@ -