Tournament list #11

Merged
schrom01 merged 13 commits from TournamentList into main 2022-05-01 18:45:02 +02:00
6 changed files with 105 additions and 36 deletions
Showing only changes of commit 171cf7ef4e - Show all commits

View File

@ -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");

View File

@ -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);
}
}
}

View File

@ -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());
}
}

View File

@ -1,20 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<HBox alignment="CENTER" VBox.vgrow="ALWAYS" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController">
<children>
<VBox alignment="TOP_CENTER" prefHeight="331.0" prefWidth="308.0" HBox.hgrow="ALWAYS">
<children>
@ -66,12 +57,12 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="turnierNameLabel" styleClass="lableGrid" text="Turnier Name:">
<Label fx:id="tournamentNameLabel" styleClass="lableGrid" text="Turnier Name:">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<TextField styleClass="inputGrid" GridPane.columnIndex="1">
<TextField fx:id="tournamentNameField" styleClass="inputGrid" GridPane.columnIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>

View File

@ -109,7 +109,12 @@ class FileIOTest {
@Test
void saveTournament() throws IOException {
Tournament tournament = new Tournament("test1");
Tournament tournament = null;
try {
tournament = new Tournament("test1", Tournament.Type.KO);
} catch (Tournament.InvalidNameException e) {
e.printStackTrace();
}
io.saveTournament(tournament);
File file = new File(mainDir + "/saves/test1.txt");
if(file.exists()){