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 0f3826d..bb07387 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java @@ -29,18 +29,34 @@ public abstract class FXController { public abstract void loadContent(); + protected void setTournamentDecorator(TournamentDecorator tournamentDecorator) { + this.tournamentDecorator = tournamentDecorator; + } + protected TournamentDecorator getTournamentDecorator() { return tournamentDecorator; } + protected void setFactoryDecorator(FactoryDecorator factoryDecorator) { + this.factoryDecorator = factoryDecorator; + } + protected FactoryDecorator getFactoryDecorator() { return factoryDecorator; } + protected void setFileIO(FileIO fileIO) { + this.fileIO = fileIO; + } + protected FileIO getFileIO() { return fileIO; } + protected void setPane(Pane pane) { + this.pane = pane; + } + protected Pane getPane() { return pane; } 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 c3cc6e6..1492c0e 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -13,6 +13,7 @@ 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; @@ -57,17 +58,18 @@ public class Factory { setCenterOfBorderPane(pane, getClass().getResource("gameScheduleView/GameSchedule.fxml"), factoryDecorator); } - public void loadGameView(VBox box, Game game, FactoryDecorator factoryDecorator) { + public GameController loadGameView(VBox box, GameDecorator gameDecorator, FactoryDecorator factoryDecorator) { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("gameScheduleView/Game.fxml")); box.getChildren().add(loader.load()); GameController controller = loader.getController(); - controller.setup(tournamentDecorator, fileIO, factoryDecorator, box, new GameDecorator(game)); - controller.loadContent(); + controller.setup(tournamentDecorator, fileIO, factoryDecorator, box, gameDecorator); + return controller; } catch (IOException e) { e.printStackTrace(); //TODO LOGGER } + return null; } private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) { @@ -77,7 +79,6 @@ public class Factory { pane.setCenter(loader.load()); controller = loader.getController(); controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane); - controller.loadContent(); } catch (IOException e) { e.printStackTrace(); //TODO handle and logging? 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 1677489..a186616 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java @@ -1,5 +1,6 @@ package ch.zhaw.projekt2.turnierverwaltung; +import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController; import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; @@ -46,8 +47,7 @@ public class FactoryDecorator implements IsObservable{ public void openTournament(FileIO.TournamentFile tournamentFile){ try { factory.setTournament(fileIO.loadTournament(tournamentFile)); - factory.loadGameScheduler((BorderPane) pane, this); - informListener(); + openScheduleView(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { @@ -55,20 +55,28 @@ public class FactoryDecorator implements IsObservable{ } //TODO handle and logging } + public void openTournamentList() { + factory.loadTournamentList((BorderPane) pane, this); + informListener(); + } + public void openParticipantFormular() { factory.loadParticipantFormular((BorderPane) pane, this); + informListener(); } public void openPlacesFormular() { factory.loadPlacesFormular((BorderPane) pane, this); + informListener(); } public void openScheduleView() { factory.loadGameScheduler((BorderPane) pane, this); + informListener(); } - public void openGameView(VBox vBox, Game game) { - factory.loadGameView(vBox ,game, this); + public GameController openGameView(VBox vBox, GameDecorator gameDecorator) { + return factory.loadGameView(vBox ,gameDecorator, this); } public void informListener() { 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 8ca7041..51bbded 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Game.java @@ -6,6 +6,7 @@ public class Game implements Serializable { private Participant participant1, participant2; private int points1, points2; private Place place; + private Game previousGame1, previousGame2; public Game(Participant participant1, Participant participant2) { @@ -13,6 +14,11 @@ public class Game implements Serializable { this.participant2 = participant2; } + public Game(Game previousGame1, Game previousGame2){ + this.previousGame1 = previousGame1; + this.previousGame2 = previousGame2; + } + public Place getLocation() { return place; } @@ -71,4 +77,9 @@ public class Game implements Serializable { } } + public void refreshParticipants(){ + participant1 = previousGame1.getWinner(); + participant2 = previousGame2.getWinner(); + } + } 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 2d4a204..15f701a 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -102,7 +102,7 @@ public class Tournament implements Serializable { } } else { for (int j = 0; j < (gameList.get(i-1).size()/2); j++) { - gameRound.add(new Game(null,null)); + gameRound.add(new Game(gameList.get(i-1).get(j*2),gameList.get(i-1).get(j*2+1))); } } gameList.add(gameRound); @@ -110,20 +110,6 @@ public class Tournament implements Serializable { } - public void refreshGameParticipants(){ - for(int i = 1; i < gameList.size(); i++){ - for(int j = 0; j/2 < gameList.get(i).size(); j ++){ - Participant winner = gameList.get(i-1).get(j).getWinner(); - if(winner != null){ - if(j % 2 == 0){ - gameList.get(i).get(j/2).setParticipant1(winner); - } else{ - gameList.get(i).get(j/2).setParticipant2(winner); - } - } - } - } - } public String getName() { return name; 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 9ba380f..b0c6393 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -126,10 +126,6 @@ public class TournamentDecorator implements IsObservable{ } } - public void refreshGameParticipants() { - getTournament().refreshGameParticipants(); - informListener(); - } public void informListener() { for(IsObserver observer : listener) { 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 c1f059e..f970bee 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 @@ -25,7 +25,7 @@ public class MainWindow extends Application { BorderPane pane = factory.loadMainWindow(); factoryDecorator = new FactoryDecorator(fileIO, factory, pane); - factory.loadTournamentList(pane, factoryDecorator); + factoryDecorator.openTournamentList(); Scene scene = new Scene(pane); 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 8d566b6..e1fe378 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 @@ -31,8 +31,6 @@ public class GameController extends FXController{ @FXML void saveGamerResult(ActionEvent event) { gameDecorator.saveGame(pointsTeamOne.getText(), pointsTeamTwo.getText(), placesChoiceBox.getValue()); - getTournamentDecorator().refreshGameParticipants(); - loadContent(); } @Override @@ -43,8 +41,23 @@ public class GameController extends FXController{ pointsTeamTwo.setText(String.valueOf(gameDecorator.getPoints2())); } + public void refreshParticipants(){ + gameDecorator.refreshParticipants(); + } + + public void addListener(GameDecorator gameDecorator){ + gameDecorator.addListener(new IsObserver() { + @Override + public void update() { + refreshParticipants(); + loadContent(); + } + }); + } + + public void setup(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, Pane pane, GameDecorator gameDecorator) { - super.setup(tournamentDecorator, fileIO, factoryDecorator, pane); + setTournamentDecorator(tournamentDecorator); this.gameDecorator = gameDecorator; } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java index ef7ed94..0b339b4 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameDecorator.java @@ -6,7 +6,7 @@ import java.util.ArrayList; import java.util.List; -public class GameDecorator { +public class GameDecorator implements IsObservable{ private Game game; private List listener = new ArrayList<>(); @@ -14,10 +14,21 @@ public class GameDecorator { this.game = game; } + @Override + public void addListener(IsObserver observer) { + listener.add(observer); + } + + @Override + public void removeListener(IsObserver observer) { + listener.remove(observer); + } + public void saveGame(String points1, String points2, Place place){ game.setPoints1(Integer.parseInt(points1)); game.setPoints2(Integer.parseInt(points2)); game.setPlace(place); + informListener(); } public String getPoints1() { @@ -42,7 +53,17 @@ public class GameDecorator { return "2"; } + public void refreshParticipants(){ + game.refreshParticipants(); + } + public Place getLocation() { return game.getPlace(); } + + public void informListener() { + for(IsObserver observer : listener) { + observer.update(); + } + } } 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 d7e25f8..075a1e8 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 @@ -13,6 +13,7 @@ import javafx.scene.layout.VBox; import org.checkerframework.checker.units.qual.C; import java.io.IOException; +import java.util.ArrayList; import java.util.List; public class GameScheduleController extends FXController { @@ -55,12 +56,22 @@ public class GameScheduleController extends FXController { List> gameList = getTournamentDecorator().getTournament().getGameList(); + List gameDecorators = new ArrayList<>(); for (int i = 0; i < gameList.size(); i++) { + List newGameDecorators = new ArrayList<>(); VBox vBox = new VBox(); for (int j = 0; j < gameList.get(i).size(); j++) { - getFactoryDecorator().openGameView(vBox,gameList.get(i).get(j)); + GameDecorator gameDecorator = new GameDecorator(gameList.get(i).get(j)); + newGameDecorators.add(gameDecorator); + GameController controller = getFactoryDecorator().openGameView(vBox,gameDecorator); + if(i>0){ + controller.addListener(gameDecorators.get(j*2)); + controller.addListener(gameDecorators.get(j*2+1)); + } + controller.loadContent(); } hBoxCenter.getChildren().add(vBox); + gameDecorators = newGameDecorators; } }