From 6d5dd700a054a062b3349b4ae859e1ddcf9c8dd2 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sat, 30 Apr 2022 17:09:01 +0200 Subject: [PATCH] implemented functionality to show existing Tournaments. --- .../main/tournamentList/tournamentList.fxml | 100 ------------------ .../turnierverwaltung/FXController.java | 10 +- .../projekt2/turnierverwaltung/Factory.java | 22 ++-- .../projekt2/turnierverwaltung/FileIO.java | 23 +++- .../turnierverwaltung/main/MainWindow.java | 5 +- .../main/MainWindowController.java | 5 + .../tournamentListController.java | 18 +++- .../participantFormular.fxml | 2 +- .../tournamentList/tournamentList.fxml | 2 +- .../projekt2/turnierverwaltung/AppTest.java | 14 --- .../turnierverwaltung/FileIOTest.java | 2 +- 11 files changed, 69 insertions(+), 134 deletions(-) delete mode 100644 app/build/resources/main/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/tournamentList.fxml delete mode 100644 app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/AppTest.java diff --git a/app/build/resources/main/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/tournamentList.fxml b/app/build/resources/main/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/tournamentList.fxml deleted file mode 100644 index 9a52479..0000000 --- a/app/build/resources/main/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/tournamentList.fxml +++ /dev/null @@ -1,100 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java index 5c5c545..babea73 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java @@ -3,16 +3,24 @@ package ch.zhaw.projekt2.turnierverwaltung; public abstract class FXController { Tournament tournament; Factory factory; + FileIO fileIO; - public void setup(Tournament tournament, Factory factory){ + public void setup(Tournament tournament, FileIO fileIO, Factory factory){ this.tournament = tournament; + this.fileIO = fileIO; this.factory = factory; } + public abstract void loadContent(); + protected Tournament getTournament() { return tournament; } + protected FileIO getFileIO() { + return fileIO; + } + protected Factory getFactory() { return factory; } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java index 5e851fa..a272b0e 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -1,21 +1,18 @@ package ch.zhaw.projekt2.turnierverwaltung; +import ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController; import javafx.fxml.FXMLLoader; -import javafx.scene.Scene; -import javafx.scene.layout.Border; import javafx.scene.layout.BorderPane; -import javafx.scene.layout.Pane; -import javafx.stage.Stage; import java.io.IOException; import java.net.URL; public class Factory { private Tournament tournament; + private FileIO fileIO; - - public Factory(){ - + public Factory(FileIO fileIO){ + this.fileIO = fileIO; } public Tournament getTournament() { @@ -38,7 +35,7 @@ public class Factory { } public void loadTournamentList(BorderPane pane){ - setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml")); + TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml")); } //Can be used to Open new Scene in same Stage. @@ -47,16 +44,19 @@ public class Factory { setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml")); } - private void setCenterOfBorderPane(BorderPane pane, URL location) { + private FXController setCenterOfBorderPane(BorderPane pane, URL location) { + FXController controller = null; try { FXMLLoader loader = new FXMLLoader(location); pane.setCenter(loader.load()); - FXController controller = loader.getController(); - controller.setup(tournament, this); + controller = loader.getController(); + controller.setup(tournament, fileIO, this); + controller.loadContent(); } catch (IOException e) { e.printStackTrace(); //TODO handle and logging? } + return controller; } 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 d072da5..9b9365d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -2,6 +2,8 @@ package ch.zhaw.projekt2.turnierverwaltung; import java.io.*; +import java.net.URI; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.logging.Logger; @@ -32,9 +34,13 @@ public class FileIO { } } - public List getList() { + public List getList() { logger.fine("Creating a List out of all Files in the save directory and returning it"); - return Arrays.asList(saves.listFiles()); + List tournaments = new ArrayList<>(); + for(File tournament : saves.listFiles()){ + tournaments.add(new TournamentFile(tournament.toURI())); + } + return tournaments; } /** @@ -110,4 +116,17 @@ public class FileIO { } } + + public class TournamentFile extends File{ + + public TournamentFile(URI uri) { + super(uri); + } + + public String toString(){ + String name = getName(); + return name.split("\\.")[0]; + } + + } } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java index c0f80f4..4602c5d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindow.java @@ -1,6 +1,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main; import ch.zhaw.projekt2.turnierverwaltung.Factory; +import ch.zhaw.projekt2.turnierverwaltung.FileIO; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -12,10 +13,12 @@ import java.awt.*; import java.io.IOException; public class MainWindow extends Application { - private Factory factory = new Factory(); //TODO make it private! + private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds"); + private Factory factory = new Factory(fileIO); //TODO make it private! @Override public void start(Stage primaryStage) throws Exception { + BorderPane pane = factory.loadMainWindow(); factory.loadTournamentList(pane); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindowController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindowController.java index f33fb69..ef5e1d6 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindowController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/MainWindowController.java @@ -15,4 +15,9 @@ public class MainWindowController extends FXController { void closeApplication(ActionEvent event) { } + + @Override + public void loadContent() { + + } } 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 cc399e7..cb9a548 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,7 +1,11 @@ package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; import ch.zhaw.projekt2.turnierverwaltung.FXController; +import ch.zhaw.projekt2.turnierverwaltung.FileIO; 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; @@ -10,7 +14,9 @@ import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.layout.GridPane; -public class tournamentListController extends FXController { +import java.io.File; + +public class TournamentListController extends FXController { @FXML private Button createBtn; @@ -31,7 +37,7 @@ public class tournamentListController extends FXController { private Label tournierListTitle; @FXML - private ListView tournierListView; + private ListView tournierListView; @FXML private Label tournierModLabel; @@ -49,5 +55,13 @@ public class tournamentListController extends FXController { } + @Override + public void loadContent() { + ObservableList tournamentFiles = FXCollections.observableArrayList(); + for(File tournament : getFileIO().getList()){ + tournamentFiles.add(tournament); + } + tournierListView.setItems(tournamentFiles); + } } diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml index 0441e61..caa919e 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/participantAddFormular/participantFormular.fxml @@ -17,7 +17,7 @@ - + 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 5f5aac6..74c2bf2 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 @@ -14,7 +14,7 @@ - + diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/AppTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/AppTest.java deleted file mode 100644 index 252063c..0000000 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/AppTest.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This Java source file was generated by the Gradle 'init' task. - */ -package ch.zhaw.projekt2.turnierverwaltung; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -class AppTest { - @Test void appHasAGreeting() { - App classUnderTest = new App(); - assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); - } -} diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java index ad0b9e8..fd68e22 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/FileIOTest.java @@ -60,7 +60,7 @@ class FileIOTest { @Test void getList() { - List tournaments = io.getList(); + List tournaments = io.getList(); assertEquals("empty.txt", tournaments.get(0).getName()); assertEquals("test1.txt", tournaments.get(1).getName()); }