Tournament list #11

Merged
schrom01 merged 13 commits from TournamentList into main 2022-05-01 18:45:02 +02:00
5 changed files with 111 additions and 21 deletions
Showing only changes of commit 9f005281af - Show all commits

View File

@ -4,21 +4,34 @@ import javafx.scene.layout.Pane;
public abstract class FXController { public abstract class FXController {
Tournament tournament; Tournament tournament;
TournamentDecorator tournamentDecorator;
Factory factory; Factory factory;
FileIO fileIO; FileIO fileIO;
Pane pane; Pane pane;
public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){ public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){
this.tournament = tournament; setTournament(tournament);
this.fileIO = fileIO; this.fileIO = fileIO;
this.factory = factory; this.factory = factory;
this.pane = pane; this.pane = pane;
tournamentDecorator = new TournamentDecorator(fileIO, tournament);
tournamentDecorator.addListener(new IsObserver() {
@Override
public void update() {
loadContent();
}
});
} }
public abstract void loadContent(); public abstract void loadContent();
protected Tournament getTournament() { public void setTournament(Tournament tournament) {
return tournament; this.tournament = tournament;
tournamentDecorator.setTournament(tournament);
}
protected TournamentDecorator getTournamentDecorator() {
return tournamentDecorator;
} }
protected FileIO getFileIO() { 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 @FXML
void createTournament(ActionEvent event) { void createTournament(ActionEvent event) {
if(getFileIO().tournamentExists(tournamentNameField.getText())){ getTournamentDecorator().createTournament(tournamentNameField.getText(), modusChoiceBox.getValue());
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
}
} }
@FXML @FXML
void openTournament(ActionEvent event) { void openTournament(ActionEvent event) {
File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
try { try {
File tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0);
getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); getFactory().setTournament(getFileIO().loadTournament(tournamentFile));
getFactory().loadParticipantFormular((BorderPane) getPane()); //TODO load TournamentView instead of ParticipantFormular? getFactory().loadParticipantFormular((BorderPane) getPane()); //TODO load TournamentView instead of ParticipantFormular?
} catch (IOException e) { } catch (IOException e) {
@ -105,6 +89,7 @@ public class TournamentListController extends FXController {
@Override @Override
public void loadContent() { public void loadContent() {
tournamentListView.setItems(getFileIO().getList()); tournamentListView.setItems(getFileIO().getList());
tournamentNameField.clear();
} }
} }