extracted Method open Tournament to Class FactoryDecorator.java

This commit is contained in:
schrom01 2022-05-01 13:39:54 +02:00
parent 9f005281af
commit 61b943b091
5 changed files with 97 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

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