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();
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;
}

View File

@ -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?

View File

@ -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() {

View File

@ -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();
}
}

View File

@ -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;

View File

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

View File

@ -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);

View File

@ -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;
}

View File

@ -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<IsObserver> 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();
}
}
}

View File

@ -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<List<Game>> gameList = getTournamentDecorator().getTournament().getGameList();
List<GameDecorator> gameDecorators = new ArrayList<>();
for (int i = 0; i < gameList.size(); i++) {
List<GameDecorator> 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;
}
}