From 61b943b09179e4bbdf1718c669fdef3780c5cb53 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 1 May 2022 13:39:54 +0200 Subject: [PATCH] extracted Method open Tournament to Class FactoryDecorator.java --- .../turnierverwaltung/FXController.java | 29 +++++---- .../projekt2/turnierverwaltung/Factory.java | 23 +++---- .../turnierverwaltung/FactoryDecorator.java | 62 +++++++++++++++++++ .../turnierverwaltung/main/MainWindow.java | 9 ++- .../TournamentListController.java | 11 +--- 5 files changed, 97 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java 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 f8c1570..0f3826d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java @@ -3,45 +3,44 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.scene.layout.Pane; public abstract class FXController { - Tournament tournament; TournamentDecorator tournamentDecorator; - Factory factory; + FactoryDecorator factoryDecorator; FileIO fileIO; Pane pane; - public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){ - setTournament(tournament); + public void setup(TournamentDecorator tournamentDecorator, FileIO fileIO, FactoryDecorator factoryDecorator, Pane pane){ + this.tournamentDecorator = tournamentDecorator; this.fileIO = fileIO; - this.factory = factory; + this.factoryDecorator = factoryDecorator; this.pane = pane; - tournamentDecorator = new TournamentDecorator(fileIO, tournament); tournamentDecorator.addListener(new IsObserver() { @Override public void update() { loadContent(); } }); + factoryDecorator.addListener(new IsObserver() { + @Override + public void update() { + loadContent(); + } + }); } public abstract void loadContent(); - public void setTournament(Tournament tournament) { - this.tournament = tournament; - tournamentDecorator.setTournament(tournament); - } - protected TournamentDecorator getTournamentDecorator() { return tournamentDecorator; } + protected FactoryDecorator getFactoryDecorator() { + return factoryDecorator; + } + protected FileIO getFileIO() { return fileIO; } - protected Factory getFactory() { - return factory; - } - 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 06ca006..c05347b 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -8,19 +8,20 @@ import java.io.IOException; import java.net.URL; public class Factory { - private Tournament tournament; + private TournamentDecorator tournamentDecorator; private FileIO fileIO; - public Factory(FileIO fileIO){ + public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator){ this.fileIO = fileIO; + this.tournamentDecorator = tournamentDecorator; } - public Tournament getTournament() { - return tournament; + public TournamentDecorator getTournamentDecorator() { + return tournamentDecorator; } public void setTournament(Tournament tournament) { - this.tournament = tournament; + this.tournamentDecorator = tournamentDecorator; } public BorderPane loadMainWindow(){ @@ -34,23 +35,23 @@ public class Factory { return null; } - public void loadTournamentList(BorderPane pane){ - TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml")); + public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){ + TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator); } //Can be used to Open new Scene in same Stage. //This way possible to later give object to Controller - public void loadParticipantFormular(BorderPane pane) { - setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml")); + public void loadParticipantFormular(BorderPane pane, FactoryDecorator factoryDecorator) { + setCenterOfBorderPane(pane, getClass().getResource("participantAddFormular/participantFormular.fxml"), factoryDecorator); } - private FXController setCenterOfBorderPane(BorderPane pane, URL location) { + 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(tournament, fileIO, this, pane); + controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane); controller.loadContent(); } catch (IOException e) { e.printStackTrace(); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java new file mode 100644 index 0000000..6278f10 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java @@ -0,0 +1,62 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.Pane; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class FactoryDecorator implements IsObservable{ + private Factory factory; + private FileIO fileIO; + private Pane pane; + private List listener = new ArrayList<>(); + + public FactoryDecorator(FileIO fileIO, Factory factory, Pane pane){ + setFactory(factory); + setFileIO(fileIO); + setPane(pane); + } + + public void setFileIO(FileIO fileIO) { + this.fileIO = fileIO; + } + + public void setFactory(Factory factory) { + this.factory = factory; + } + + public void setPane(Pane pane) { + this.pane = pane; + } + + @Override + public void addListener(IsObserver observer) { + listener.add(observer); + } + + @Override + public void removeListener(IsObserver observer) { + listener.remove(observer); + } + + public void openTournament(FileIO.TournamentFile tournamentFile){ + try { + factory.setTournament(fileIO.loadTournament(tournamentFile)); + factory.loadParticipantFormular((BorderPane) pane, this); //TODO load TournamentView instead of ParticipantFormular? + informListener(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } //TODO handle and logging + } + + + public void informListener() { + for(IsObserver observer : listener) { + observer.update(); + } + } +} 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 4602c5d..14348bd 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,7 +1,9 @@ package ch.zhaw.projekt2.turnierverwaltung.main; import ch.zhaw.projekt2.turnierverwaltung.Factory; +import ch.zhaw.projekt2.turnierverwaltung.FactoryDecorator; import ch.zhaw.projekt2.turnierverwaltung.FileIO; +import ch.zhaw.projekt2.turnierverwaltung.TournamentDecorator; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -14,13 +16,16 @@ import java.io.IOException; public class MainWindow extends Application { private FileIO fileIO = new FileIO(System.getProperty("user.dir") + "/tournierverwaltung_angrynerds"); - private Factory factory = new Factory(fileIO); //TODO make it private! + private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO, null); + private Factory factory = new Factory(fileIO, tournamentDecorator); //TODO make it private! + private FactoryDecorator factoryDecorator; @Override public void start(Stage primaryStage) throws Exception { BorderPane pane = factory.loadMainWindow(); - factory.loadTournamentList(pane); + factoryDecorator = new FactoryDecorator(fileIO, factory, pane); + factory.loadTournamentList(pane, factoryDecorator); Scene scene = new Scene(pane); 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 003422e..fcb26c0 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 @@ -61,15 +61,8 @@ public class TournamentListController extends FXController { @FXML void openTournament(ActionEvent event) { - File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); - try { - getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); - getFactory().loadParticipantFormular((BorderPane) getPane()); //TODO load TournamentView instead of ParticipantFormular? - } catch (IOException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } //TODO handle and logging + FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); + getFactoryDecorator().openTournament(tournamentFile); } @FXML