refactoring of GameController.java to load content only if previous games are edited.

This commit is contained in:
schrom01 2022-05-07 15:23:26 +02:00
parent 62df1db34b
commit 923be52610
10 changed files with 96 additions and 33 deletions

View File

@ -29,18 +29,34 @@ public abstract class FXController {
public abstract void loadContent(); public abstract void loadContent();
protected void setTournamentDecorator(TournamentDecorator tournamentDecorator) {
this.tournamentDecorator = tournamentDecorator;
}
protected TournamentDecorator getTournamentDecorator() { protected TournamentDecorator getTournamentDecorator() {
return tournamentDecorator; return tournamentDecorator;
} }
protected void setFactoryDecorator(FactoryDecorator factoryDecorator) {
this.factoryDecorator = factoryDecorator;
}
protected FactoryDecorator getFactoryDecorator() { protected FactoryDecorator getFactoryDecorator() {
return factoryDecorator; return factoryDecorator;
} }
protected void setFileIO(FileIO fileIO) {
this.fileIO = fileIO;
}
protected FileIO getFileIO() { protected FileIO getFileIO() {
return fileIO; return fileIO;
} }
protected void setPane(Pane pane) {
this.pane = pane;
}
protected Pane getPane() { protected Pane getPane() {
return pane; return pane;
} }

View File

@ -13,6 +13,7 @@ import java.net.URL;
public class Factory { public class Factory {
private TournamentDecorator tournamentDecorator; private TournamentDecorator tournamentDecorator;
private FileIO fileIO; private FileIO fileIO;
//TODO save views instead of recreate them.
public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator){ public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator){
this.fileIO = fileIO; this.fileIO = fileIO;
@ -57,17 +58,18 @@ public class Factory {
setCenterOfBorderPane(pane, getClass().getResource("gameScheduleView/GameSchedule.fxml"), factoryDecorator); 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 { try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("gameScheduleView/Game.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("gameScheduleView/Game.fxml"));
box.getChildren().add(loader.load()); box.getChildren().add(loader.load());
GameController controller = loader.getController(); GameController controller = loader.getController();
controller.setup(tournamentDecorator, fileIO, factoryDecorator, box, new GameDecorator(game)); controller.setup(tournamentDecorator, fileIO, factoryDecorator, box, gameDecorator);
controller.loadContent(); return controller;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
//TODO LOGGER //TODO LOGGER
} }
return null;
} }
private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) { private FXController setCenterOfBorderPane(BorderPane pane, URL location, FactoryDecorator factoryDecorator) {
@ -77,7 +79,6 @@ public class Factory {
pane.setCenter(loader.load()); pane.setCenter(loader.load());
controller = loader.getController(); controller = loader.getController();
controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane); controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane);
controller.loadContent();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
//TODO handle and logging? //TODO handle and logging?

View File

@ -1,5 +1,6 @@
package ch.zhaw.projekt2.turnierverwaltung; 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.gameScheduleView.GameDecorator;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
@ -46,8 +47,7 @@ public class FactoryDecorator implements IsObservable{
public void openTournament(FileIO.TournamentFile tournamentFile){ public void openTournament(FileIO.TournamentFile tournamentFile){
try { try {
factory.setTournament(fileIO.loadTournament(tournamentFile)); factory.setTournament(fileIO.loadTournament(tournamentFile));
factory.loadGameScheduler((BorderPane) pane, this); openScheduleView();
informListener();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -55,20 +55,28 @@ public class FactoryDecorator implements IsObservable{
} //TODO handle and logging } //TODO handle and logging
} }
public void openTournamentList() {
factory.loadTournamentList((BorderPane) pane, this);
informListener();
}
public void openParticipantFormular() { public void openParticipantFormular() {
factory.loadParticipantFormular((BorderPane) pane, this); factory.loadParticipantFormular((BorderPane) pane, this);
informListener();
} }
public void openPlacesFormular() { public void openPlacesFormular() {
factory.loadPlacesFormular((BorderPane) pane, this); factory.loadPlacesFormular((BorderPane) pane, this);
informListener();
} }
public void openScheduleView() { public void openScheduleView() {
factory.loadGameScheduler((BorderPane) pane, this); factory.loadGameScheduler((BorderPane) pane, this);
informListener();
} }
public void openGameView(VBox vBox, Game game) { public GameController openGameView(VBox vBox, GameDecorator gameDecorator) {
factory.loadGameView(vBox ,game, this); return factory.loadGameView(vBox ,gameDecorator, this);
} }
public void informListener() { public void informListener() {

View File

@ -6,6 +6,7 @@ public class Game implements Serializable {
private Participant participant1, participant2; private Participant participant1, participant2;
private int points1, points2; private int points1, points2;
private Place place; private Place place;
private Game previousGame1, previousGame2;
public Game(Participant participant1, Participant participant2) { public Game(Participant participant1, Participant participant2) {
@ -13,6 +14,11 @@ public class Game implements Serializable {
this.participant2 = participant2; this.participant2 = participant2;
} }
public Game(Game previousGame1, Game previousGame2){
this.previousGame1 = previousGame1;
this.previousGame2 = previousGame2;
}
public Place getLocation() { public Place getLocation() {
return place; return place;
} }
@ -71,4 +77,9 @@ public class Game implements Serializable {
} }
} }
public void refreshParticipants(){
participant1 = previousGame1.getWinner();
participant2 = previousGame2.getWinner();
}
} }

View File

@ -102,7 +102,7 @@ public class Tournament implements Serializable {
} }
} else { } else {
for (int j = 0; j < (gameList.get(i-1).size()/2); j++) { 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); 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() { public String getName() {
return name; return name;

View File

@ -126,10 +126,6 @@ public class TournamentDecorator implements IsObservable{
} }
} }
public void refreshGameParticipants() {
getTournament().refreshGameParticipants();
informListener();
}
public void informListener() { public void informListener() {
for(IsObserver observer : listener) { for(IsObserver observer : listener) {

View File

@ -25,7 +25,7 @@ public class MainWindow extends Application {
BorderPane pane = factory.loadMainWindow(); BorderPane pane = factory.loadMainWindow();
factoryDecorator = new FactoryDecorator(fileIO, factory, pane); factoryDecorator = new FactoryDecorator(fileIO, factory, pane);
factory.loadTournamentList(pane, factoryDecorator); factoryDecorator.openTournamentList();
Scene scene = new Scene(pane); Scene scene = new Scene(pane);

View File

@ -31,8 +31,6 @@ public class GameController extends FXController{
@FXML @FXML
void saveGamerResult(ActionEvent event) { void saveGamerResult(ActionEvent event) {
gameDecorator.saveGame(pointsTeamOne.getText(), pointsTeamTwo.getText(), placesChoiceBox.getValue()); gameDecorator.saveGame(pointsTeamOne.getText(), pointsTeamTwo.getText(), placesChoiceBox.getValue());
getTournamentDecorator().refreshGameParticipants();
loadContent();
} }
@Override @Override
@ -43,8 +41,23 @@ public class GameController extends FXController{
pointsTeamTwo.setText(String.valueOf(gameDecorator.getPoints2())); 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) { public void setup(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, Pane pane, GameDecorator gameDecorator) {
super.setup(tournamentDecorator, fileIO, factoryDecorator, pane); setTournamentDecorator(tournamentDecorator);
this.gameDecorator = gameDecorator; this.gameDecorator = gameDecorator;
} }

View File

@ -6,7 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GameDecorator { public class GameDecorator implements IsObservable{
private Game game; private Game game;
private List<IsObserver> listener = new ArrayList<>(); private List<IsObserver> listener = new ArrayList<>();
@ -14,10 +14,21 @@ public class GameDecorator {
this.game = game; 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){ public void saveGame(String points1, String points2, Place place){
game.setPoints1(Integer.parseInt(points1)); game.setPoints1(Integer.parseInt(points1));
game.setPoints2(Integer.parseInt(points2)); game.setPoints2(Integer.parseInt(points2));
game.setPlace(place); game.setPlace(place);
informListener();
} }
public String getPoints1() { public String getPoints1() {
@ -42,7 +53,17 @@ public class GameDecorator {
return "2"; return "2";
} }
public void refreshParticipants(){
game.refreshParticipants();
}
public Place getLocation() { public Place getLocation() {
return game.getPlace(); return game.getPlace();
} }
public void informListener() {
for(IsObserver observer : listener) {
observer.update();
}
}
} }

View File

@ -13,6 +13,7 @@ import javafx.scene.layout.VBox;
import org.checkerframework.checker.units.qual.C; import org.checkerframework.checker.units.qual.C;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GameScheduleController extends FXController { public class GameScheduleController extends FXController {
@ -55,12 +56,22 @@ public class GameScheduleController extends FXController {
List<List<Game>> gameList = getTournamentDecorator().getTournament().getGameList(); List<List<Game>> gameList = getTournamentDecorator().getTournament().getGameList();
List<GameDecorator> gameDecorators = new ArrayList<>();
for (int i = 0; i < gameList.size(); i++) { for (int i = 0; i < gameList.size(); i++) {
List<GameDecorator> newGameDecorators = new ArrayList<>();
VBox vBox = new VBox(); VBox vBox = new VBox();
for (int j = 0; j < gameList.get(i).size(); j++) { 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); hBoxCenter.getChildren().add(vBox);
gameDecorators = newGameDecorators;
} }
} }