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 2e51f0d..f8c1570 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FXController.java @@ -4,21 +4,34 @@ import javafx.scene.layout.Pane; public abstract class FXController { Tournament tournament; + TournamentDecorator tournamentDecorator; Factory factory; FileIO fileIO; Pane pane; public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){ - this.tournament = tournament; + setTournament(tournament); this.fileIO = fileIO; this.factory = factory; this.pane = pane; + tournamentDecorator = new TournamentDecorator(fileIO, tournament); + tournamentDecorator.addListener(new IsObserver() { + @Override + public void update() { + loadContent(); + } + }); } public abstract void loadContent(); - protected Tournament getTournament() { - return tournament; + public void setTournament(Tournament tournament) { + this.tournament = tournament; + tournamentDecorator.setTournament(tournament); + } + + protected TournamentDecorator getTournamentDecorator() { + return tournamentDecorator; } protected FileIO getFileIO() { diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/IsObservable.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/IsObservable.java new file mode 100644 index 0000000..a627e78 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/IsObservable.java @@ -0,0 +1,19 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +/** + * Most basic interface for observing an object + * @author bles + * + */ +public interface IsObservable { + /** + * Add an observer that listens for updates + * @param observer + */ + void addListener(IsObserver observer); + /** + * Remove an observer from the list + * @param observer + */ + void removeListener(IsObserver observer); +} diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/IsObserver.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/IsObserver.java new file mode 100644 index 0000000..4501482 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/IsObserver.java @@ -0,0 +1,14 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +/** + * Most basic interface for beeing an observer + * @author bles + * + */ +public interface IsObserver { + /** + * This method is always called when an observed object + * changes + */ + void update(); +} diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java new file mode 100644 index 0000000..0177d05 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -0,0 +1,59 @@ +package ch.zhaw.projekt2.turnierverwaltung; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class TournamentDecorator implements IsObservable{ + private Tournament tournament; + private FileIO fileIO; + private List listener = new ArrayList<>(); + + public TournamentDecorator(FileIO fileIO, Tournament tournament){ + setTournament(tournament); + setFileIO(fileIO); + } + + public void setFileIO(FileIO fileIO) { + this.fileIO = fileIO; + } + + public void setTournament(Tournament tournament) { + this.tournament = tournament; + } + + @Override + public void addListener(IsObserver observer) { + listener.add(observer); + } + + @Override + public void removeListener(IsObserver observer) { + listener.remove(observer); + } + + public void createTournament(String name, Tournament.Type type){ + if(fileIO.tournamentExists(name)){ + System.out.println("Tournament with same name exists already."); + return; //TODO handle and logging + } + try { + Tournament tournament = new Tournament(name, type); + fileIO.saveTournament(tournament); + informListener(); + } catch (Tournament.InvalidNameException e) { + e.printStackTrace(); //TODO handle and logging + } catch (Tournament.InvalidTypeException e) { + e.printStackTrace(); //TODO handle and logging + } catch (IOException 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/tournamentList/TournamentListController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/TournamentListController.java index 433ab41..003422e 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 @@ -56,29 +56,13 @@ public class TournamentListController extends FXController { @FXML void createTournament(ActionEvent event) { - if(getFileIO().tournamentExists(tournamentNameField.getText())){ - System.out.println("Tournament with same name exists already."); - return; //TODO handle and logging - } - try { - Tournament tournament = new Tournament(tournamentNameField.getText(), modusChoiceBox.getValue()); - getFileIO().saveTournament(tournament); - loadContent(); - tournamentNameField.clear(); - } catch (Tournament.InvalidNameException e) { - e.printStackTrace(); //TODO handle and logging - } catch (Tournament.InvalidTypeException e) { - e.printStackTrace(); //TODO handle and logging - } catch (IOException e) { - e.printStackTrace(); //TODO handle and logging - } - + getTournamentDecorator().createTournament(tournamentNameField.getText(), modusChoiceBox.getValue()); } @FXML void openTournament(ActionEvent event) { + File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); try { - File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); getFactory().loadParticipantFormular((BorderPane) getPane()); //TODO load TournamentView instead of ParticipantFormular? } catch (IOException e) { @@ -105,6 +89,7 @@ public class TournamentListController extends FXController { @Override public void loadContent() { tournamentListView.setItems(getFileIO().getList()); + tournamentNameField.clear(); } }