Tournament list #11

Merged
schrom01 merged 13 commits from TournamentList into main 2022-05-01 18:45:02 +02:00
14 changed files with 420 additions and 93 deletions

View File

@ -3,32 +3,44 @@ package ch.zhaw.projekt2.turnierverwaltung;
import javafx.scene.layout.Pane;
public abstract class FXController {
Tournament tournament;
Factory factory;
TournamentDecorator tournamentDecorator;
FactoryDecorator factoryDecorator;
FileIO fileIO;
Pane pane;
public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){
this.tournament = tournament;
public void setup(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, Pane pane){
this.tournamentDecorator = tournamentDecorator;
this.fileIO = fileIO;
this.factory = factory;
this.factoryDecorator = factoryDecorator;
this.pane = pane;
tournamentDecorator.addListener(new IsObserver() {
@Override
public void update() {
loadContent();
}
});
factoryDecorator.addListener(new IsObserver() {
@Override
public void update() {
loadContent();
}
});
}
public abstract void loadContent();
protected Tournament getTournament() {
return tournament;
protected TournamentDecorator getTournamentDecorator() {
return tournamentDecorator;
}
protected FactoryDecorator getFactoryDecorator() {
return factoryDecorator;
}
protected FileIO getFileIO() {
return fileIO;
}
protected Factory getFactory() {
return factory;
}
protected Pane getPane() {
return pane;
}

View File

@ -8,19 +8,20 @@ import java.io.IOException;
import java.net.URL;
public class Factory {
private Tournament tournament;
private TournamentDecorator tournamentDecorator;
private FileIO fileIO;
public Factory(FileIO fileIO){
public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator){
this.fileIO = fileIO;
this.tournamentDecorator = tournamentDecorator;
}
public Tournament getTournament() {
return tournament;
public TournamentDecorator getTournamentDecorator() {
return tournamentDecorator;
}
public void setTournament(Tournament tournament) {
this.tournament = tournament;
this.tournamentDecorator = tournamentDecorator;
}
public BorderPane loadMainWindow(){
@ -34,23 +35,23 @@ public class Factory {
return null;
}
public void loadTournamentList(BorderPane pane){
TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"));
public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){
TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator);
}
//Can be used to Open new Scene in same Stage.
//This way possible to later give object to Controller
public void loadParticipantFormular(BorderPane pane) {
setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"));
public void loadParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) {
setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"), factoryDecorator);
}
private FXController setCenterOfBorderPane(BorderPane pane, URL location) {
private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) {
FXController controller = null;
try {
FXMLLoader loader = new FXMLLoader(location);
pane.setCenter(loader.load());
controller = loader.getController();
controller.setup(tournament, fileIO, this, pane);
controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane);
controller.loadContent();
} catch (IOException e) {
e.printStackTrace();

View File

@ -0,0 +1,62 @@
package ch.zhaw.projekt2.turnierverwaltung;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FactoryDecorator implements IsObservable{
private Factory factory;
private FileIO fileIO;
private Pane pane;
private List<IsObserver> listener = new ArrayList<>();
public FactoryDecorator(FileIO fileIO, Factory factory, Pane pane){
setFactory(factory);
setFileIO(fileIO);
setPane(pane);
}
public void setFileIO(FileIO fileIO) {
this.fileIO = fileIO;
}
public void setFactory(Factory factory) {
this.factory = factory;
}
public void setPane(Pane pane) {
this.pane = pane;
}
@Override
public void addListener(IsObserver observer) {
listener.add(observer);
}
@Override
public void removeListener(IsObserver observer) {
listener.remove(observer);
}
public void openTournament(FileIO.TournamentFile tournamentFile){
try {
factory.setTournament(fileIO.loadTournament(tournamentFile));
factory.loadParticipantFormular((BorderPane) pane, this); //TODO load TournamentView instead of ParticipantFormular?
informListener();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} //TODO handle and logging
}
public void informListener() {
for(IsObserver observer : listener) {
observer.update();
}
}
}

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,22 @@ 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;
}
public boolean tournamentExists(String name){
for(TournamentFile file : getList()) {
if(file.toString().toLowerCase().equals(name.toLowerCase())){
return true;
}
}
return false;
}
/**
@ -86,7 +98,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");
@ -124,9 +136,13 @@ public class FileIO {
}
public String toString(){
String name = getName();
return name.split("\\.")[0];
return getName().split("\\.")[0];
}
}
public void deleteTournament(File tournamentFile) throws IOException {
throw new UnsupportedOperationException("Method deleteTournament not implemented yet.");
}
}

View File

@ -0,0 +1,19 @@
package ch.zhaw.projekt2.turnierverwaltung;
/**
* Most basic interface for observing an object
* @author bles
*
*/
public interface IsObservable {
/**
* Add an observer that listens for updates
* @param observer
*/
void addListener(IsObserver observer);
/**
* Remove an observer from the list
* @param observer
*/
void removeListener(IsObserver observer);
}

View File

@ -0,0 +1,14 @@
package ch.zhaw.projekt2.turnierverwaltung;
/**
* Most basic interface for beeing an observer
* @author bles
*
*/
public interface IsObserver {
/**
* This method is always called when an observed object
* changes
*/
void update();
}

View File

@ -1,12 +1,26 @@
package ch.zhaw.projekt2.turnierverwaltung;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.Serializable;
import java.util.Arrays;
public class Tournament implements Serializable {
private String name;
private Type type;
public Tournament(String name, Type type) throws InvalidNameException, InvalidTypeException {
if(!name.matches("[\\w]{1,20}")){
throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging.
} else if(!Arrays.asList(Type.values()).contains(type)){
throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging.
}
public Tournament(String name){
setName(name);
setType(type);
}
public String getName() {
@ -16,4 +30,57 @@ 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);
}
}
public class InvalidTypeException extends Exception {
public InvalidTypeException() {
super();
}
public InvalidTypeException(String errorMessage) {
super(errorMessage);
}
}
}

View File

@ -0,0 +1,68 @@
package ch.zhaw.projekt2.turnierverwaltung;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TournamentDecorator implements IsObservable{
private Tournament tournament;
private FileIO fileIO;
private List<IsObserver> listener = new ArrayList<>();
public TournamentDecorator(FileIO fileIO, Tournament tournament){
setTournament(tournament);
setFileIO(fileIO);
}
public void setFileIO(FileIO fileIO) {
this.fileIO = fileIO;
}
public void setTournament(Tournament tournament) {
this.tournament = tournament;
}
@Override
public void addListener(IsObserver observer) {
listener.add(observer);
}
@Override
public void removeListener(IsObserver observer) {
listener.remove(observer);
}
public void createTournament(String name, Tournament.Type type){
if(fileIO.tournamentExists(name)){
System.out.println("Tournament with same name exists already.");
return; //TODO handle and logging
}
try {
Tournament tournament = new Tournament(name, type);
fileIO.saveTournament(tournament);
informListener();
} catch (Tournament.InvalidNameException e) {
e.printStackTrace(); //TODO handle and logging
} catch (Tournament.InvalidTypeException e) {
e.printStackTrace(); //TODO handle and logging
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
}
public void deleteTournament(FileIO.TournamentFile tournamentFile){
try {
fileIO.deleteTournament(tournamentFile);
informListener();
} catch (IOException e) {
e.printStackTrace(); //TODO handle and logging
}
}
public void informListener() {
for(IsObserver observer : listener) {
observer.update();
}
}
}

View File

@ -1,7 +1,9 @@
package ch.zhaw.projekt2.turnierverwaltung.main;
import ch.zhaw.projekt2.turnierverwaltung.Factory;
import ch.zhaw.projekt2.turnierverwaltung.FactoryDecorator;
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import ch.zhaw.projekt2.turnierverwaltung.TournamentDecorator;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
@ -14,13 +16,16 @@ import java.io.IOException;
public class MainWindow extends Application {
private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds");
private Factory factory = new Factory(fileIO); //TODO make it private!
private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO, null);
private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private!
private FactoryDecorator factoryDecorator;
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = factory.loadMainWindow();
factory.loadTournamentList(pane);
factoryDecorator = new FactoryDecorator(fileIO, factory, pane);
factory.loadTournamentList(pane, factoryDecorator);
Scene scene = new Scene(pane);

View File

@ -0,0 +1,34 @@
package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import java.io.IOException;
public class AlertDelete extends Alert {
ButtonType yesButton = new ButtonType("Ja", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("Nein", ButtonBar.ButtonData.NO);
Boolean result;
public AlertDelete(String name){
super(Alert.AlertType.WARNING);
setTitle("Entfernen");
setHeaderText("Turnier entfernen?");
setContentText("Sind Sie sicher, dass sie das Turnier " + name + " entfernen wollen?\n" +
"Nach diesem Vorgang kann es nicht wiederhergestellt werden.");
getButtonTypes().setAll(yesButton, noButton);
}
public boolean showAndGetResult() {
result = false;
showAndWait().ifPresent(type -> {
if (type == yesButton) {
result = true;
}
});
return result;
}
}

View File

@ -1,19 +1,13 @@
package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList;
import ch.zhaw.projekt2.turnierverwaltung.FXController;
import ch.zhaw.projekt2.turnierverwaltung.Factory;
import ch.zhaw.projekt2.turnierverwaltung.FileIO;
import ch.zhaw.projekt2.turnierverwaltung.Tournament;
import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow;
import javafx.beans.Observable;
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;
@ -22,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;
@ -29,7 +28,7 @@ public class TournamentListController extends FXController {
private GridPane grid;
@FXML
private ChoiceBox<?> modusChoiceBox;
private ChoiceBox<Tournament.Type> modusChoiceBox;
@FXML
private Label newTournamentFormularTitle;
@ -38,42 +37,47 @@ public class TournamentListController extends FXController {
private Button openBtn;
@FXML
private Label tournierListTitle;
private Button deleteBtn;
@FXML
private ListView<File> tournierListView;
private Label tournamentListTitle;
@FXML
private Label tournierModLabel;
private ListView<FileIO.TournamentFile> tournamentListView;
@FXML
private Label turnierNameLabel;
private Label tournamentModLabel;
@FXML
private Label tournamentNameLabel;
@FXML
private TextField tournamentNameField;
@FXML
void createTournament(ActionEvent event) {
getTournamentDecorator().createTournament(tournamentNameField.getText(), modusChoiceBox.getValue());
}
@FXML
void openTournament(ActionEvent event) {
try {
File tournamentFile = tournierListView.getSelectionModel().getSelectedItems().get(0);
getFactory().setTournament(getFileIO().loadTournament(tournamentFile));
getFactory().loadParticipantFormular((BorderPane) getPane());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
getFactoryDecorator().openTournament(tournamentFile);
}
@FXML
void deleteTournament(ActionEvent event) {
FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
AlertDelete alert = new AlertDelete(tournamentFile.toString());
if(alert.showAndGetResult()){
getTournamentDecorator().deleteTournament(tournamentFile);
}
}
@Override
public void loadContent() {
ObservableList<File> tournamentFiles = FXCollections.observableArrayList();
for(File tournament : getFileIO().getList()){
tournamentFiles.add(tournament);
}
tournierListView.setItems(tournamentFiles);
tournamentListView.setItems(getFileIO().getList());
tournamentNameField.clear();
}
}

View File

@ -1,24 +1,15 @@
<?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>
<Label fx:id="tournierListTitle" text="Bestehende Turniere">
<Label fx:id="tournamentListTitle" text="Bestehende Turniere">
<font>
<Font name="System Bold" size="21.0" />
</font>
@ -26,16 +17,20 @@
<Insets bottom="20.0" />
</VBox.margin>
</Label>
<ListView fx:id="tournierListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<ListView fx:id="tournamentListView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets />
</VBox.margin>
</ListView>
<Button fx:id="openBtn" mnemonicParsing="false" onAction="#openTournament" text="Öffnen">
<VBox.margin>
<Insets bottom="20.0" top="40.0" />
</VBox.margin>
</Button>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<VBox.margin>
<Insets bottom="20.0" top="40.0" />
</VBox.margin>
<children>
<Button fx:id="openBtn" mnemonicParsing="false" onAction="#openTournament" text="Öffnen" />
<Button fx:id="deleteBtn" layoutX="138.0" layoutY="28.0" mnemonicParsing="false" onAction="#deleteTournament" text="Löschen" />
</children>
</HBox>
</children>
<HBox.margin>
<Insets left="40.0" />
@ -66,17 +61,17 @@
<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>
</TextField>
<Label fx:id="tournierModLabel" styleClass="lableGrid" text="Turnier Modus:" GridPane.rowIndex="1">
<Label fx:id="tournamentModLabel" styleClass="lableGrid" text="Turnier Modus:" GridPane.rowIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>

View File

@ -82,19 +82,19 @@ class FileIOTest {
@Test
void loadTournamentNotExisting(){
io = new FileIO(mainDir);
assertThrows(FileNotFoundException.class, () -> io.loadTournament(new File("Not-existing-File")));
File file = new File("Not-existing-File");
assertFalse(file.exists());
assertThrows(FileNotFoundException.class, () -> io.loadTournament(file));
assertFalse(file.exists());
}
@Test
void loadTournamentEmpty(){
io = new FileIO(mainDir);
assertThrows(IOException.class, () -> io.loadTournament(new File(mainDir + "/saves/empty.txt")));
}
@Test
void loadTournamentFileNull(){
io = new FileIO(mainDir);
assertThrows(IllegalArgumentException.class, () -> io.loadTournament(null));
}
}
@ -108,15 +108,14 @@ class FileIOTest {
}
@Test
void saveTournament() throws IOException {
Tournament tournament = new Tournament("test1");
void saveTournament() throws IOException, Tournament.InvalidNameException, Tournament.InvalidTypeException {
Tournament tournament = null;
tournament = new Tournament("test1", Tournament.Type.KO);
io.saveTournament(tournament);
File file = new File(mainDir + "/saves/test1.txt");
if(file.exists()){
file.delete();
} else {
fail();
}
assertTrue(file.exists());
assertTrue(file.delete());
assertFalse(file.exists());
}
@Test
@ -124,4 +123,35 @@ class FileIOTest {
assertThrows(IllegalArgumentException.class, () -> io.saveTournament(null));
}
}
@Nested
class Delete{
@BeforeEach
void setup(){
mainDir = RESOURCES_DIR + "FileIODelete";
io = new FileIO(mainDir);
}
@Test
void deleteTournament() throws IOException {
File file = new File(mainDir + "/saves/test1.txt");
file.createNewFile();
assertTrue(file.exists());
io.deleteTournament(file);
assertFalse(file.exists());
}
@Test
void deleteTournamentNotExisting() throws IOException {
File file = new File("Not-existing-File");
assertFalse(file.exists());
assertThrows(FileNotFoundException.class, () -> io.deleteTournament(file));
assertFalse(file.exists());
}
@Test
void deleteTournamentNull(){
assertThrows(IllegalArgumentException.class, () -> io.deleteTournament(null));
}
}
}