created Class TournamentDecorator.java

This commit is contained in:
schrom01 2022-05-01 13:10:20 +02:00
parent 50f98b4ac1
commit 9f005281af
5 changed files with 111 additions and 21 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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) {
try {
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) {
@ -105,6 +89,7 @@ public class TournamentListController extends FXController {
@Override
public void loadContent() {
tournamentListView.setItems(getFileIO().getList());
tournamentNameField.clear();
}
}