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..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,32 +3,44 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.scene.layout.Pane; public abstract class FXController { - Tournament tournament; - Factory factory; + TournamentDecorator tournamentDecorator; + FactoryDecorator factoryDecorator; FileIO fileIO; Pane pane; - public void setup(Tournament tournament, FileIO fileIO, Factory factory, Pane pane){ - this.tournament = 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.addListener(new IsObserver() { + @Override + public void update() { + loadContent(); + } + }); + factoryDecorator.addListener(new IsObserver() { + @Override + public void update() { + loadContent(); + } + }); } public abstract void loadContent(); - protected Tournament getTournament() { - return 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/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index 9b9365d..98b6d8b 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -1,6 +1,9 @@ package ch.zhaw.projekt2.turnierverwaltung; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + import java.io.*; import java.net.URI; import java.util.ArrayList; @@ -34,13 +37,22 @@ public class FileIO { } } - public List getList() { + public ObservableList getList() { logger.fine("Creating a List out of all Files in the save directory and returning it"); - List tournaments = new ArrayList<>(); + ObservableList tournamentFiles = FXCollections.observableArrayList(); for(File tournament : saves.listFiles()){ - tournaments.add(new TournamentFile(tournament.toURI())); + tournamentFiles.add(new TournamentFile(tournament.toURI())); } - return tournaments; + return tournamentFiles; + } + + public boolean tournamentExists(String name){ + for(TournamentFile file : getList()) { + if(file.toString().toLowerCase().equals(name.toLowerCase())){ + return true; + } + } + return false; } /** @@ -86,7 +98,7 @@ public class FileIO { return tournament; } - public void saveTournament(Tournament tournament) { + public void saveTournament(Tournament tournament) throws IOException { if (tournament == null) { logger.warning("Given tournament file is empty"); throw new IllegalArgumentException("Null tournament received"); @@ -124,9 +136,13 @@ public class FileIO { } public String toString(){ - String name = getName(); - return name.split("\\.")[0]; + return getName().split("\\.")[0]; } } + + public void deleteTournament(File tournamentFile) throws IOException { + throw new UnsupportedOperationException("Method deleteTournament not implemented yet."); + } + } 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/Tournament.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java index 0da286d..e46f505 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -1,12 +1,26 @@ package ch.zhaw.projekt2.turnierverwaltung; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + import java.io.Serializable; +import java.util.Arrays; public class Tournament implements Serializable { private String name; + private Type type; + + + + public Tournament(String name, Type type) throws InvalidNameException, InvalidTypeException { + if(!name.matches("[\\w]{1,20}")){ + throw new Tournament.InvalidNameException("Invalid Name entered"); //TODO handle en logging. + } else if(!Arrays.asList(Type.values()).contains(type)){ + throw new InvalidTypeException("Invalid Type selected"); //TODO handle en logging. + } - public Tournament(String name){ setName(name); + setType(type); } public String getName() { @@ -16,4 +30,57 @@ public class Tournament implements Serializable { public void setName(String name) { this.name = name; } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public enum Type { + KO("KO-System"), GROUPS("Gruppenspiele"); + + private String name; + + private Type(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + + public static ObservableList getObservableList(){ + ObservableList items = FXCollections.observableArrayList(); + items.addAll(values()); + return items; + } + } + + public class InvalidNameException extends Exception { + public InvalidNameException() { + super(); + } + + public InvalidNameException(String errorMessage) { + super(errorMessage); + } + + } + + public class InvalidTypeException extends Exception { + public InvalidTypeException() { + super(); + } + + public InvalidTypeException(String errorMessage) { + super(errorMessage); + } + + } + + } 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..a848a4a --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -0,0 +1,68 @@ +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 deleteTournament(FileIO.TournamentFile tournamentFile){ + try { + fileIO.deleteTournament(tournamentFile); + informListener(); + } 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/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/AlertDelete.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/AlertDelete.java new file mode 100644 index 0000000..e440807 --- /dev/null +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/tournamentList/AlertDelete.java @@ -0,0 +1,34 @@ +package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; + +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonBar; +import javafx.scene.control.ButtonType; + +import java.io.IOException; + +public class AlertDelete extends Alert { + ButtonType yesButton = new ButtonType("Ja", ButtonBar.ButtonData.YES); + ButtonType noButton = new ButtonType("Nein", ButtonBar.ButtonData.NO); + Boolean result; + + public AlertDelete(String name){ + super(Alert.AlertType.WARNING); + setTitle("Entfernen"); + setHeaderText("Turnier entfernen?"); + setContentText("Sind Sie sicher, dass sie das Turnier " + name + " entfernen wollen?\n" + + "Nach diesem Vorgang kann es nicht wiederhergestellt werden."); + getButtonTypes().setAll(yesButton, noButton); + } + + public boolean showAndGetResult() { + result = false; + showAndWait().ifPresent(type -> { + if (type == yesButton) { + result = true; + } + }); + return result; + } + + +} 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 37c5c7b..bcc1109 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 @@ -1,19 +1,13 @@ package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; import ch.zhaw.projekt2.turnierverwaltung.FXController; -import ch.zhaw.projekt2.turnierverwaltung.Factory; import ch.zhaw.projekt2.turnierverwaltung.FileIO; import ch.zhaw.projekt2.turnierverwaltung.Tournament; -import ch.zhaw.projekt2.turnierverwaltung.main.MainWindow; -import javafx.beans.Observable; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.ChoiceBox; -import javafx.scene.control.Label; -import javafx.scene.control.ListView; +import javafx.scene.control.*; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; @@ -22,6 +16,11 @@ import java.io.IOException; public class TournamentListController extends FXController { + @FXML + public void initialize(){ + modusChoiceBox.setItems(Tournament.Type.getObservableList()); + } + @FXML private Button createBtn; @@ -29,7 +28,7 @@ public class TournamentListController extends FXController { private GridPane grid; @FXML - private ChoiceBox modusChoiceBox; + private ChoiceBox modusChoiceBox; @FXML private Label newTournamentFormularTitle; @@ -38,42 +37,47 @@ public class TournamentListController extends FXController { private Button openBtn; @FXML - private Label tournierListTitle; + private Button deleteBtn; @FXML - private ListView tournierListView; + private Label tournamentListTitle; @FXML - private Label tournierModLabel; + private ListView tournamentListView; @FXML - private Label turnierNameLabel; + private Label tournamentModLabel; + + @FXML + private Label tournamentNameLabel; + + @FXML + private TextField tournamentNameField; @FXML void createTournament(ActionEvent event) { - + getTournamentDecorator().createTournament(tournamentNameField.getText(), modusChoiceBox.getValue()); } @FXML void openTournament(ActionEvent event) { - try { - File tournamentFile = tournierListView.getSelectionModel().getSelectedItems().get(0); - getFactory().setTournament(getFileIO().loadTournament(tournamentFile)); - getFactory().loadParticipantFormular((BorderPane) getPane()); - } catch (IOException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); + FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); + getFactoryDecorator().openTournament(tournamentFile); + } + + @FXML + void deleteTournament(ActionEvent event) { + FileIO.TournamentFile tournamentFile = tournamentListView.getSelectionModel().getSelectedItems().get(0); + AlertDelete alert = new AlertDelete(tournamentFile.toString()); + if(alert.showAndGetResult()){ + getTournamentDecorator().deleteTournament(tournamentFile); } } @Override public void loadContent() { - ObservableList tournamentFiles = FXCollections.observableArrayList(); - for(File tournament : getFileIO().getList()){ - tournamentFiles.add(tournament); - } - tournierListView.setItems(tournamentFiles); + tournamentListView.setItems(getFileIO().getList()); + tournamentNameField.clear(); } } diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml index 74c2bf2..24c2e89 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/tournamentList/tournamentList.fxml @@ -1,24 +1,15 @@ - - - - - - - - - - - - - + + + + - + -