implemented functionality to add new Tournament

This commit is contained in:
schrom01
2022-05-01 10:27:21 +02:00
parent fd2e775c94
commit 171cf7ef4e
6 changed files with 105 additions and 36 deletions
@@ -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<TournamentFile> getList() {
public ObservableList<TournamentFile> getList() {
logger.fine("Creating a List out of all Files in the save directory and returning it");
List<TournamentFile> tournaments = new ArrayList<>();
ObservableList<TournamentFile> 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");
@@ -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<Type> getObservableList(){
ObservableList<Type> items = FXCollections.observableArrayList();
items.addAll(values());
return items;
}
}
public class InvalidNameException extends Exception {
public InvalidNameException() {
super();
}
public InvalidNameException(String errorMessage) {
super(errorMessage);
}
}
}
@@ -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<Tournament.Type> modusChoiceBox;
@FXML
private Label newTournamentFormularTitle;
@@ -36,7 +40,7 @@ public class TournamentListController extends FXController {
private Label tournamentListTitle;
@FXML
private ListView<File> tournamentListView;
private ListView<FileIO.TournamentFile> 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<File> tournamentFiles = FXCollections.observableArrayList();
for(File tournament : getFileIO().getList()){
tournamentFiles.add(tournament);
}
tournamentListView.setItems(tournamentFiles);
tournamentListView.setItems(getFileIO().getList());
}
}