From 68080c36ff583121cef60dd84327d98f00d611e1 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 12 May 2022 23:15:42 +0200 Subject: [PATCH 01/11] change method addPlace start TournamentTest implementation --- app/build.gradle | 6 +- .../projekt2/turnierverwaltung/Place.java | 4 + .../turnierverwaltung/Tournament.java | 22 +++-- .../TournamentDecorator.java | 4 - .../turnierverwaltung/TournamentTest.java | 85 +++++++++++++++++++ 5 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java diff --git a/app/build.gradle b/app/build.gradle index dfbf65f..e1f7b1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,6 +26,8 @@ dependencies { // Use JUnit Jupiter for testing. testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1' + testImplementation 'org.mockito:mockito-core:4.3.+' + // This dependency is used by the application. implementation 'com.google.guava:guava:30.1.1-jre' @@ -37,7 +39,9 @@ application { mainClass = 'ch.zhaw.projekt2.turnierverwaltung.App' } -tasks.named('test') { +test { // Use JUnit Platform for unit tests. useJUnitPlatform() + + } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java index f2e35bd..c09e1bf 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java @@ -29,4 +29,8 @@ public class Place implements Serializable { public boolean equals(Place place){ return name.equals(place.getName()); } + + public void change(Place place) { + //TODO: If Place gets more developt in future releases + } } 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 bb4bd92..ce9e62c 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; + import java.io.Serializable; import java.util.*; @@ -28,8 +29,8 @@ public class Tournament implements Serializable { } public void saveParticipant(Participant newParticipant) { - for(Participant participant: participants){ - if(participant.equals(newParticipant)){ + for (Participant participant : participants) { + if (participant.equals(newParticipant)) { participant.change(newParticipant); return; } @@ -50,8 +51,13 @@ public class Tournament implements Serializable { return participantsObservable; } - public void addPlace(Place newPlace) throws PlaceExistsException { - places.removeIf(place -> place.equals(newPlace)); + public void addPlace(Place newPlace) { + for (Place place : places) { + if (place.equals(newPlace)) { + place.change(newPlace); + return; + } + } places.add(newPlace); } @@ -91,7 +97,7 @@ public class Tournament implements Serializable { private boolean numberOfParticipantValid() { double res = Math.log(participants.size()) / Math.log(2); - return (res * 10) % 10 == 0 && participants.size() >=4; + return (res * 10) % 10 == 0 && participants.size() >= 4; } private void calcGameSchedule() { @@ -101,11 +107,11 @@ public class Tournament implements Serializable { List gameRound = new ArrayList<>(); if (i == 0) { for (int j = 0; j < participants.size() - 1; j += 2) { - gameRound.add(new Game(participants.get(j), participants.get(j+1))); + gameRound.add(new Game(participants.get(j), participants.get(j + 1))); } } else { - for (int j = 0; j < (gameList.get(i-1).size()/2); j++) { - gameRound.add(new Game(gameList.get(i-1).get(j*2),gameList.get(i-1).get(j*2+1))); + for (int j = 0; j < (gameList.get(i - 1).size() / 2); j++) { + gameRound.add(new Game(gameList.get(i - 1).get(j * 2), gameList.get(i - 1).get(j * 2 + 1))); } } gameList.add(gameRound); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index 28d6bac..eaa2e8d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -148,10 +148,6 @@ public class TournamentDecorator implements IsObservable{ tournament.addPlace(new Place(name)); factoryDecorator.clearMessage(true); informListener(); - } catch (Tournament.PlaceExistsException e) { - e.printStackTrace(); //TODO handle and logging - factoryDecorator.printMessageToFooter("Ort existiert bereits",true); - } catch (InvalidNameException e) { e.printStackTrace(); //TODO handle and logging factoryDecorator.printMessageToFooter("Invalider Ortsname",true); diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java new file mode 100644 index 0000000..c7aac86 --- /dev/null +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java @@ -0,0 +1,85 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +import javafx.application.Platform; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@DisplayName("TournamentTest") +public class TournamentTest { + + @Nested + @DisplayName("KOMode") + public class KOMode { + private Tournament tournament; + + @BeforeEach + void setup() { + try { + tournament = new Tournament("Name", Tournament.Type.KO); + } catch (InvalidNameException e) { + e.printStackTrace(); + fail(); + } catch (Tournament.InvalidTypeException e) { + e.printStackTrace(); + fail(); + } + } + + @Test + @DisplayName("Test invalid instance") + void makeInvalidInstance() { + assertThrows(InvalidNameException.class,() -> new Tournament(".", Tournament.Type.KO)); + } + + @Test + @DisplayName("Save Participant") + void saveParticipantTest() { + //Checks if one Participant gets added + Participant participantOne = Mockito.mock(Player.class); + when(participantOne.equals(any(Participant.class))).thenReturn(false); + tournament.saveParticipant(participantOne); + assertEquals(1,tournament.getParticipants().size()); + + //Checks if a second Participant gets added + Participant participantTwo = Mockito.mock(Player.class); + tournament.saveParticipant(participantTwo); + assertEquals(2,tournament.getParticipants().size()); + + //Checks if a allready added Particpant does not get added + when(participantOne.equals(any(Participant.class))).thenReturn(true); + tournament.saveParticipant(participantTwo); + assertEquals(2,tournament.getParticipants().size()); + + } + + @Test + @DisplayName("Remove Participant") + void removeParticipantTest() { + Participant participant = Mockito.mock(Player.class); + + //Checks if Error is thrown if Participant not in list + assertThrows(Tournament.ParticipantNotExistsException.class,() -> tournament.removeParticipant(participant)); + + //Checks if participant gets removed + tournament.saveParticipant(participant); + assertEquals(1,tournament.getParticipants().size()); + try { + tournament.removeParticipant(participant); + assertEquals(0,tournament.getParticipants().size()); + } catch (Tournament.ParticipantNotExistsException e) { + fail(); + } + } + + + + } + +} From 40cfd694364f7826270154cdecdd2bb78b685865 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 14:23:17 +0200 Subject: [PATCH 02/11] implement TournamentTest --- .../projekt2/turnierverwaltung/Factory.java | 1 + .../turnierverwaltung/Tournament.java | 3 +- .../turnierverwaltung/TournamentTest.java | 79 +++++++++++++++++-- 3 files changed, 74 insertions(+), 9 deletions(-) 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 53f9f12..3092765 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -12,6 +12,7 @@ import javafx.scene.text.Font; import java.io.IOException; import java.net.URL; +import java.nio.charset.Charset; public class Factory { private TournamentDecorator tournamentDecorator; 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 1745d07..3150c95 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; + import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; @@ -233,7 +234,7 @@ public class Tournament implements Serializable { * @param type */ public void setType(Type type) { - logger.fine("Setting the type of the tournament to: " + type); + logger.fine("Setting the type of the tournament to: " + type); this.type = type; } diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java index c7aac86..135d5c5 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java @@ -9,6 +9,7 @@ import org.mockito.Mockito; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @DisplayName("TournamentTest") @@ -35,7 +36,7 @@ public class TournamentTest { @Test @DisplayName("Test invalid instance") void makeInvalidInstance() { - assertThrows(InvalidNameException.class,() -> new Tournament(".", Tournament.Type.KO)); + assertThrows(InvalidNameException.class, () -> new Tournament(".", Tournament.Type.KO)); } @Test @@ -44,18 +45,20 @@ public class TournamentTest { //Checks if one Participant gets added Participant participantOne = Mockito.mock(Player.class); when(participantOne.equals(any(Participant.class))).thenReturn(false); + + assertEquals(0, tournament.getParticipants().size()); tournament.saveParticipant(participantOne); - assertEquals(1,tournament.getParticipants().size()); + assertEquals(1, tournament.getParticipants().size()); //Checks if a second Participant gets added Participant participantTwo = Mockito.mock(Player.class); tournament.saveParticipant(participantTwo); - assertEquals(2,tournament.getParticipants().size()); + assertEquals(2, tournament.getParticipants().size()); //Checks if a allready added Particpant does not get added when(participantOne.equals(any(Participant.class))).thenReturn(true); tournament.saveParticipant(participantTwo); - assertEquals(2,tournament.getParticipants().size()); + assertEquals(2, tournament.getParticipants().size()); } @@ -65,21 +68,81 @@ public class TournamentTest { Participant participant = Mockito.mock(Player.class); //Checks if Error is thrown if Participant not in list - assertThrows(Tournament.ParticipantNotExistsException.class,() -> tournament.removeParticipant(participant)); + assertThrows(Tournament.ParticipantNotExistsException.class, () -> tournament.removeParticipant(participant)); //Checks if participant gets removed tournament.saveParticipant(participant); - assertEquals(1,tournament.getParticipants().size()); + assertEquals(1, tournament.getParticipants().size()); try { tournament.removeParticipant(participant); - assertEquals(0,tournament.getParticipants().size()); + assertEquals(0, tournament.getParticipants().size()); } catch (Tournament.ParticipantNotExistsException e) { fail(); } } + @Test + @DisplayName("Add Place") + void addPlaceTest() { + Place place = mock(Place.class); + when(place.equals(any(Place.class))).thenReturn(false).thenReturn(true); + assertEquals(0, tournament.getPlaces().size()); + tournament.addPlace(place); + assertEquals(1, tournament.getPlaces().size()); + tournament.addPlace(place); + assertEquals(2, tournament.getPlaces().size()); + tournament.addPlace(place); + assertEquals(2, tournament.getPlaces().size()); + } + + @Test + @DisplayName("Remove Place") + void removePlaceTest() { + Place place = mock(Place.class); + + assertThrows(Tournament.PlaceNotExistsException.class, () -> tournament.removePlace(place)); + + tournament.addPlace(place); + assertEquals(1, tournament.getPlaces().size()); + try { + tournament.removePlace(place); + assertEquals(0, tournament.getPlaces().size()); + } catch (Tournament.PlaceNotExistsException e) { + fail(); + } + } + + @Test + @DisplayName("Test gameschedule calculation") + void calcGameSchedule() { + Participant participant = mock(Player.class); + when(participant.equals(any(Participant.class))).thenReturn(false); + + //Checks if invalid number of Participants throws error + assertThrows(Tournament.NumberOfParticipantInvalidException.class, () -> tournament.createGameSchedule()); + for (int i = 0; i < 8; i++) { + if (i % 4 == 0 && i > 0) { + try { + tournament.createGameSchedule(); + assertEquals(2, tournament.getGameList().size()); + tournament.saveParticipant(participant); + } catch (Tournament.NumberOfParticipantInvalidException e) { + fail(); + } + + } else { + assertThrows(Tournament.NumberOfParticipantInvalidException.class, () -> tournament.createGameSchedule()); + tournament.saveParticipant(participant); + } + } + try { + tournament.createGameSchedule(); + } catch (Tournament.NumberOfParticipantInvalidException e) { + fail(); + } + assertEquals(3, tournament.getGameList().size()); + } } - } From 127e1526374fcfd9989ab23baa70e84a64c2e231 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 14:52:58 +0200 Subject: [PATCH 03/11] implement TournamentTest --- .../zhaw/projekt2/turnierverwaltung/Place.java | 2 +- .../projekt2/turnierverwaltung/Tournament.java | 17 +---------------- .../turnierverwaltung/TournamentDecorator.java | 2 +- .../turnierverwaltung/TournamentTest.java | 9 ++++----- 4 files changed, 7 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java index c09e1bf..3834101 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Place.java @@ -31,6 +31,6 @@ public class Place implements Serializable { } public void change(Place place) { - //TODO: If Place gets more developt in future releases + //TODO: If Place gets more developed in future releases } } 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 3150c95..44b3817 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -108,9 +108,8 @@ public class Tournament implements Serializable { * Method to add a new Place, also checks if place does already exist. * * @param newPlace to be added in the list of all places - * @throws PlaceExistsException if the place already exists */ - public void addPlace(Place newPlace) { + public void savePlace(Place newPlace) { for (Place place : places) { if (place.equals(newPlace)) { place.change(newPlace); @@ -301,20 +300,6 @@ public class Tournament implements Serializable { } - /** - * Custom Exception thrown when a Place does already exist - */ - public class PlaceExistsException extends Exception { - public PlaceExistsException() { - super(); - } - - public PlaceExistsException(String errorMessage) { - super(errorMessage); - } - - } - /** * Custom Exception thrown when a Place does not exist */ diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index 94655aa..ddc4873 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -148,7 +148,7 @@ public class TournamentDecorator implements IsObservable{ public void savePlace(String name){ try { - tournament.addPlace(new Place(name)); + tournament.savePlace(new Place(name)); factoryDecorator.clearMessage(true); informListener(); } catch (InvalidNameException e) { diff --git a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java index 135d5c5..3acc30c 100644 --- a/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java +++ b/app/src/test/java/ch/zhaw/projekt2/turnierverwaltung/TournamentTest.java @@ -1,6 +1,5 @@ package ch.zhaw.projekt2.turnierverwaltung; -import javafx.application.Platform; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -88,11 +87,11 @@ public class TournamentTest { when(place.equals(any(Place.class))).thenReturn(false).thenReturn(true); assertEquals(0, tournament.getPlaces().size()); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(1, tournament.getPlaces().size()); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(2, tournament.getPlaces().size()); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(2, tournament.getPlaces().size()); } @@ -103,7 +102,7 @@ public class TournamentTest { assertThrows(Tournament.PlaceNotExistsException.class, () -> tournament.removePlace(place)); - tournament.addPlace(place); + tournament.savePlace(place); assertEquals(1, tournament.getPlaces().size()); try { tournament.removePlace(place); From 6c761c944faddf9a514661423a0ce6faf5f3266b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 13 May 2022 15:08:26 +0200 Subject: [PATCH 04/11] refactoring of Factory.java added Enum View solved #23 --- .../turnierverwaltung/FXController.java | 5 -- .../projekt2/turnierverwaltung/Factory.java | 81 ++++++++++++------- .../turnierverwaltung/FactoryDecorator.java | 12 ++- .../turnierverwaltung/main/MainWindow.java | 1 + .../GameScheduleController.java | 3 - .../ParticipantFormularController.java | 9 ++- .../PlacesFormularController.java | 9 ++- .../TournamentListController.java | 1 - 8 files changed, 76 insertions(+), 45 deletions(-) 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 68a86b6..c2c7a87 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java @@ -37,11 +37,6 @@ public abstract class FXController { factoryDecorator.addListener(listener); } - protected void removeListener(){ - tournamentDecorator.removeListener(listener); - factoryDecorator.removeListener(listener); - } - protected TournamentDecorator getTournamentDecorator() { return tournamentDecorator; } 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 53f9f12..dec1687 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -2,7 +2,6 @@ package ch.zhaw.projekt2.turnierverwaltung; import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController; import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator; -import ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.scene.control.Label; @@ -11,16 +10,15 @@ import javafx.scene.paint.Color; import javafx.scene.text.Font; import java.io.IOException; -import java.net.URL; public class Factory { private TournamentDecorator tournamentDecorator; private FileIO fileIO; - //TODO save views instead of recreate them. public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator) { this.fileIO = fileIO; this.tournamentDecorator = tournamentDecorator; + } public TournamentDecorator getTournamentDecorator() { @@ -42,25 +40,43 @@ public class Factory { return null; } - public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator) { + public void loadAllViews(FactoryDecorator factoryDecorator, BorderPane pane){ + for(View view : View.values()){ + try { + view.loadView(tournamentDecorator, fileIO, factoryDecorator, pane); + } catch (IOException e) { + e.printStackTrace(); + //TODO Handle and logging. + } + } + } + + public void showTournamentList(BorderPane pane, FactoryDecorator factoryDecorator) { tournamentDecorator.setTournament(null); - TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator); + setCenterOfBorderPane(pane, View.tournamentList); } //Can be used to Open new Scene in same Stage. //This way possible to later give object to Controller - public void loadParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) { - setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"), factoryDecorator); + public void showParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) { + setCenterOfBorderPane(pane, View.participantFormular); } - public void loadPlacesFormular(BorderPane pane, FactoryDecorator factoryDecorator) { - setCenterOfBorderPane(pane, getClass().getResource("placesAddFormular/PlacesFormular.fxml"), factoryDecorator); + public void showPlacesFormular(BorderPane pane, FactoryDecorator factoryDecorator) { + setCenterOfBorderPane(pane, View.placesFormular); } - public void loadGameScheduler(BorderPane pane, FactoryDecorator factoryDecorator) { - setCenterOfBorderPane(pane, getClass().getResource("gameScheduleView/GameSchedule.fxml"), factoryDecorator); + public void showGameScheduler(BorderPane pane, FactoryDecorator factoryDecorator) { + setCenterOfBorderPane(pane, View.gameScheduler); } + private void setCenterOfBorderPane(BorderPane pane, View view) { + FXController controller = null; + pane.setCenter(view.getPane()); + resetFooter(pane, true); + } + + public GameController loadGameView(VBox box, GameDecorator gameDecorator, FactoryDecorator factoryDecorator) { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("gameScheduleView/Game.fxml")); @@ -75,23 +91,6 @@ public class Factory { return null; } - 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(tournamentDecorator, fileIO, factoryDecorator, pane); - VBox bottom = (VBox) pane.getBottom(); - resetFooter(pane, true); - } catch (IOException e) { - e.printStackTrace(); - //TODO handle and logging? - } - return controller; - } - - public void printMessageToFooter(BorderPane pane, String msg, boolean error) { VBox bottom = (VBox) pane.getBottom(); Label label = new Label(); @@ -131,4 +130,30 @@ public class Factory { vBox.setBorder(null); } + public enum View { + tournamentList("tournamentList/tournamentList.fxml"), + participantFormular("participantAddFormular/participantFormular.fxml"), + placesFormular("placesAddFormular/PlacesFormular.fxml"), + gameScheduler("gameScheduleView/GameSchedule.fxml"); + + private String fxmlFileName; + private Pane pane; + + private View(String fxmlFileName) { + this.fxmlFileName = fxmlFileName; + } + + public Pane getPane() { + return pane; + } + + public void loadView(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, BorderPane borderPane) throws IOException { + FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFileName)); + this.pane = loader.load(); + FXController controller = loader.getController(); + controller.setup(tournamentDecorator, fileIO, factoryDecorator, borderPane); + } + + } + } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java index 0026069..5ab8da7 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java @@ -59,28 +59,32 @@ public class FactoryDecorator implements IsObservable{ } public void openTournamentList() { - factory.loadTournamentList((BorderPane) pane, this); + factory.showTournamentList((BorderPane) pane, this); informListener(); } public void openParticipantFormular() { - factory.loadParticipantFormular((BorderPane) pane, this); + factory.showParticipantFormular((BorderPane) pane, this); informListener(); } public void openPlacesFormular() { - factory.loadPlacesFormular((BorderPane) pane, this); + factory.showPlacesFormular((BorderPane) pane, this); informListener(); } public void openScheduleView() { - factory.loadGameScheduler((BorderPane) pane, this); + factory.showGameScheduler((BorderPane) pane, this); informListener(); } public void loadGameList(HBox hBoxCenter, TournamentDecorator tournamentDecorator, boolean treeView) { hBoxCenter.getChildren().clear(); + if(tournamentDecorator.getTournament() == null){ + return; + } + List> gameList = tournamentDecorator.getTournament().getGameList(); List gameDecoratorsList = new ArrayList<>(); 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 f97d970..a4cd74b 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 @@ -22,6 +22,7 @@ public class MainWindow extends Application { BorderPane pane = factory.loadMainWindow(); factoryDecorator = new FactoryDecorator(fileIO, factory, pane); + factory.loadAllViews(factoryDecorator, pane); tournamentDecorator.setFactoryDecorator(factoryDecorator); factoryDecorator.openTournamentList(); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java index b81848d..3c8352e 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java @@ -35,19 +35,16 @@ public class GameScheduleController extends FXController { @FXML void openPlacesFormular(ActionEvent event) { - removeListener(); getFactoryDecorator().openPlacesFormular(); } @FXML void openParticipantFormular(ActionEvent event) { - removeListener(); getFactoryDecorator().openParticipantFormular(); } @FXML void closeTournament(ActionEvent event) { - removeListener(); getFactoryDecorator().openTournamentList(); } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java index 49643e3..fec9af8 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/participantAddFormular/ParticipantFormularController.java @@ -3,6 +3,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.participantAddFormular; import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.Participant; import ch.zhaw.projekt2.turnierverwaltung.Player; +import ch.zhaw.projekt2.turnierverwaltung.Tournament; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -97,12 +98,16 @@ public class ParticipantFormularController extends FXController { @FXML void close(ActionEvent event) { - removeListener(); getFactoryDecorator().openScheduleView(); } @Override public void loadContent() { - participantListView.setItems(getTournamentDecorator().getTournament().getParticipants()); + Tournament tournament = getTournamentDecorator().getTournament(); + if(tournament != null){ + participantListView.setItems(tournament.getParticipants()); + } else { + participantListView.getItems().clear(); + } } } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/placesAddFormular/PlacesFormularController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/placesAddFormular/PlacesFormularController.java index a8548f4..b2a031a 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/placesAddFormular/PlacesFormularController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/placesAddFormular/PlacesFormularController.java @@ -3,6 +3,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.placesAddFormular; import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.Place; import ch.zhaw.projekt2.turnierverwaltung.Player; +import ch.zhaw.projekt2.turnierverwaltung.Tournament; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -73,12 +74,16 @@ public class PlacesFormularController extends FXController { @FXML void close(ActionEvent event) { - removeListener(); getFactoryDecorator().openScheduleView(); } @Override public void loadContent() { - placeListView.setItems(getTournamentDecorator().getTournament().getPlaces()); + Tournament tournament = getTournamentDecorator().getTournament(); + if(tournament != null){ + placeListView.setItems(tournament.getPlaces()); + } else { + placeListView.getItems().clear(); + } } } 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 fbef5d2..ab26504 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 @@ -62,7 +62,6 @@ public class TournamentListController extends FXController { @FXML void openTournament(ActionEvent event) { - removeListener(); FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); getFactoryDecorator().openTournament(tournamentFile); } From b4da0b2fefee4634ec01319de04085a7edd312a9 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 15:15:37 +0200 Subject: [PATCH 05/11] Changed number of threads in executerservice to one to prevent false order to save file --- .../ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index ddc4873..ec75d5b 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -27,7 +27,7 @@ public class TournamentDecorator implements IsObservable{ } } }); - executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + executorService = Executors.newFixedThreadPool(1); } public void setFactoryDecorator(FactoryDecorator factoryDecorator) { From bdb34fde3180da841265904359a2eeeafb90e470 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 15:35:41 +0200 Subject: [PATCH 06/11] Make Application close properly If exception while closing there is a proper log --- .../TournamentDecorator.java | 6 ++++++ .../turnierverwaltung/main/MainWindow.java | 19 +++++++++++++++---- .../main/MainWindowController.java | 3 ++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index ec75d5b..c0cb27f 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -8,6 +8,7 @@ import java.util.Date; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; public class TournamentDecorator implements IsObservable{ private Tournament tournament; @@ -175,6 +176,11 @@ public class TournamentDecorator implements IsObservable{ } } + public void closeApplication() throws InterruptedException { + executorService.shutdown(); + executorService.awaitTermination(2000, TimeUnit.MILLISECONDS); + } + private class saveTask implements Runnable { @Override 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 a4cd74b..2f4b46b 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 @@ -5,28 +5,31 @@ import ch.zhaw.projekt2.turnierverwaltung.FactoryDecorator; import ch.zhaw.projekt2.turnierverwaltung.FileIO; import ch.zhaw.projekt2.turnierverwaltung.TournamentDecorator; import javafx.application.Application; +import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; +import javafx.stage.WindowEvent; + +import java.util.logging.Logger; public class MainWindow extends Application { private FileIO fileIO = new FileIO(System.getProperty("user.dir") + System.getProperty("file.separator") + "tournierverwaltung_angrynerds"); private FactoryDecorator factoryDecorator; private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO); - private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private! + private Factory factory = new Factory(fileIO, tournamentDecorator); + private static final Logger logger = Logger.getLogger(FileIO.class.getCanonicalName()); @Override public void start(Stage primaryStage) throws Exception { - BorderPane pane = factory.loadMainWindow(); factoryDecorator = new FactoryDecorator(fileIO, factory, pane); factory.loadAllViews(factoryDecorator, pane); tournamentDecorator.setFactoryDecorator(factoryDecorator); factoryDecorator.openTournamentList(); - Scene scene = new Scene(pane); primaryStage.setScene(scene); primaryStage.setMaximized(true); @@ -35,5 +38,13 @@ public class MainWindow extends Application { primaryStage.show(); } - + @Override + public void stop() { + try { + super.stop(); + tournamentDecorator.closeApplication(); + } catch (Exception e) { + logger.warning("Exception while closing application"); + } + } } 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 ef5e1d6..ffb528b 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 @@ -1,6 +1,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main; import ch.zhaw.projekt2.turnierverwaltung.FXController; +import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -13,7 +14,7 @@ public class MainWindowController extends FXController { @FXML void closeApplication(ActionEvent event) { - + Platform.exit(); } @Override From 0a7ca1ac3e107f223c41c6962f6d90daaa4cc93e Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 13 May 2022 15:36:29 +0200 Subject: [PATCH 07/11] fixed places are deleted. solved #45 --- .../java/ch/zhaw/projekt2/turnierverwaltung/Game.java | 8 -------- .../main/gameScheduleView/GameController.java | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java index 51bbded..594d1b2 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java @@ -19,14 +19,6 @@ public class Game implements Serializable { this.previousGame2 = previousGame2; } - public Place getLocation() { - return place; - } - - public void setLocation(Place place) { - this.place = place; - } - public int getPoints1() { return points1; } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java index c8f5761..07f4de1 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameController.java @@ -74,7 +74,7 @@ public class GameController extends FXController{ public void setup(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, Pane pane, GameDecorator gameDecorator) { setTournamentDecorator(tournamentDecorator); this.gameDecorator = gameDecorator; - placesChoiceBox.getSelectionModel().selectedItemProperty().addListener((ObservableValue observable, Place oldValue, Place newValue) -> saveGamePlace(newValue)); + placesChoiceBox.getSelectionModel().selectedItemProperty().addListener((ObservableValue observable, Place oldValue, Place newValue) -> saveGamePlace(newValue == null ? oldValue : newValue)); } } \ No newline at end of file From c862492be357099a934975db9fa74365d2f8de1b Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 13 May 2022 15:57:39 +0200 Subject: [PATCH 08/11] fixes in Layout solved #34 --- .../turnierverwaltung/main/MainWindow.java | 2 + .../gameScheduleView/GameSchedule.fxml | 40 +++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) 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 2f4b46b..82d4c0a 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 @@ -32,6 +32,8 @@ public class MainWindow extends Application { Scene scene = new Scene(pane); primaryStage.setScene(scene); + primaryStage.setMinHeight(600); + primaryStage.setMinWidth(800); primaryStage.setMaximized(true); primaryStage.setResizable(true); primaryStage.setFullScreen(false); diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/GameSchedule.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/GameSchedule.fxml index f3f9207..8fe7bac 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/GameSchedule.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/gameScheduleView/GameSchedule.fxml @@ -1,15 +1,29 @@ - - - + + + + + + - + +
+ + + + + +
- + - - - + + -
- - - - - -
From 53cf3bdacc53649871ba8656ca1c4abab068b679 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 13 May 2022 15:58:58 +0200 Subject: [PATCH 09/11] rmeoved tournament Type GROUPS from Tournament Enum Type. --- .../java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 44b3817..276f201 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -252,7 +252,7 @@ public class Tournament implements Serializable { * (In the Prototype only the KO-System has full functionality */ public enum Type { - KO("KO-System"), GROUPS("Gruppenspiele"); + KO("KO-System"); //GROUPS("Gruppenspiele"); //Type GROUPS is not implemented in this prototype. private String name; From 55517a531ef4a925f3f368f25c6831c347fc60ee Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 16:04:19 +0200 Subject: [PATCH 10/11] Create alert popup if user wants to create new game schedule --- .../gameScheduleView/AlertNewSchedule.java | 26 +++++++++++++++++++ .../GameScheduleController.java | 10 +++++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/AlertNewSchedule.java diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/AlertNewSchedule.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/AlertNewSchedule.java new file mode 100644 index 0000000..17a823d --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/AlertNewSchedule.java @@ -0,0 +1,26 @@ +package ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView; + +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonBar; +import javafx.scene.control.ButtonType; + +public class AlertNewSchedule extends Alert { + private ButtonType yesButton = new ButtonType("Ja", ButtonBar.ButtonData.YES); + private ButtonType noButton = new ButtonType("Nein", ButtonBar.ButtonData.NO); + private boolean result; + + public AlertNewSchedule() { + super(AlertType.WARNING); + setTitle("Neu erstellen"); + setHeaderText("Spielplan neu erstellen?"); + setContentText("Sind Sie sicher, dass Sie den Spielplan neu erstellen moechten?\nAlle Spielfortschritte gehen daraufhin verloren!"); + getButtonTypes().setAll(yesButton,noButton); + } + + public boolean showAndGetResult() { + showAndWait().ifPresent(input -> { + result = input == yesButton; + }); + return result; + } +} diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java index 3c8352e..f6e15a1 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java @@ -1,7 +1,6 @@ package ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView; import ch.zhaw.projekt2.turnierverwaltung.FXController; -import ch.zhaw.projekt2.turnierverwaltung.Tournament; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; @@ -30,7 +29,14 @@ public class GameScheduleController extends FXController { @FXML void createNewSchedule(ActionEvent event) { - getTournamentDecorator().createNewGameSchedule(); + if (getTournamentDecorator().getTournament().getGameList().size() > 0) { + AlertNewSchedule alert = new AlertNewSchedule(); + if (alert.showAndGetResult()) { + getTournamentDecorator().createNewGameSchedule(); + } + } else { + getTournamentDecorator().createNewGameSchedule(); + } } @FXML From 7dea7919c5ad76743685f11a00fbdfd34ed0735e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 13 May 2022 16:12:06 +0200 Subject: [PATCH 11/11] AlertDelete shortened code --- .../turnierverwaltung/main/tournamentList/AlertDelete.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/AlertDelete.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/AlertDelete.java index 6c400dd..8aada10 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/AlertDelete.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/AlertDelete.java @@ -21,9 +21,7 @@ public class AlertDelete extends Alert { public boolean showAndGetResult() { result = false; showAndWait().ifPresent(type -> { - if (type == yesButton) { - result = true; - } + result = type == yesButton; }); return result; }