From 298d61eeca6d07d22260e7e014c4b4b7e567f8a8 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 12 May 2022 19:34:59 +0200 Subject: [PATCH] extent scope of FactoryDecorator for TournamentDecorator creat footer to show error msg if user acts not as expected --- .../projekt2/turnierverwaltung/Factory.java | 51 +++++++++++++-- .../turnierverwaltung/FactoryDecorator.java | 8 ++- .../turnierverwaltung/Tournament.java | 4 +- .../TournamentDecorator.java | 64 +++++++++++++++---- .../turnierverwaltung/main/MainWindow.java | 6 +- .../GameScheduleController.java | 7 +- .../TournamentListController.java | 1 + .../turnierverwaltung/MainWindow.fxml | 10 ++- 8 files changed, 119 insertions(+), 32 deletions(-) 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 1492c0e..9f1a856 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Factory.java @@ -4,8 +4,11 @@ import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController; import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator; import ch.zhaw.projekt2.turnierverwaltung.main.tournamentList.TournamentListController; import javafx.fxml.FXMLLoader; -import javafx.scene.layout.BorderPane; -import javafx.scene.layout.VBox; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.layout.*; +import javafx.scene.paint.Color; +import javafx.scene.text.Font; import java.io.IOException; import java.net.URL; @@ -15,7 +18,7 @@ public class Factory { private FileIO fileIO; //TODO save views instead of recreate them. - public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator){ + public Factory(FileIO fileIO, TournamentDecorator tournamentDecorator) { this.fileIO = fileIO; this.tournamentDecorator = tournamentDecorator; } @@ -28,7 +31,7 @@ public class Factory { this.tournamentDecorator.setTournament(tournament); } - public BorderPane loadMainWindow(){ + public BorderPane loadMainWindow() { FXMLLoader loader = new FXMLLoader(getClass().getResource("mainWindow.fxml")); try { return loader.load(); @@ -39,7 +42,7 @@ public class Factory { return null; } - public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator){ + public void loadTournamentList(BorderPane pane, FactoryDecorator factoryDecorator) { tournamentDecorator.setTournament(null); TournamentListController controller = (TournamentListController) setCenterOfBorderPane(pane, getClass().getResource("tournamentList/tournamentList.fxml"), factoryDecorator); } @@ -79,6 +82,8 @@ public class Factory { pane.setCenter(loader.load()); controller = loader.getController(); controller.setup(tournamentDecorator, fileIO, factoryDecorator, pane); + VBox bottom = (VBox) pane.getBottom(); + resetFooter(pane, true); } catch (IOException e) { e.printStackTrace(); //TODO handle and logging? @@ -87,6 +92,42 @@ public class Factory { } + public void printMessageToFooter(BorderPane pane, String msg, boolean error) { + VBox bottom = (VBox) pane.getBottom(); + Label label = new Label(); + VBox innerVbox; + label.setText(msg); + if (error) { + innerVbox = (VBox) bottom.getChildren().get(0); + resetFooter(pane, true); + label.setTextFill(Color.RED); + label.setFont(new Font(20)); + innerVbox.setBorder(new Border(new BorderStroke(Color.valueOf("#FF0000"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT))); + innerVbox.getChildren().add(label); + + } else { + innerVbox = (VBox) bottom.getChildren().get(1); + resetFooter(pane,false); + label.setTextFill(Color.GREEN); + label.setFont(new Font(10)); + innerVbox.getChildren().add(label); + + } + + + } + + public void resetFooter(BorderPane pane,boolean error) { + VBox bottom = (VBox) pane.getBottom(); + VBox vBox; + if (error) { + vBox = (VBox) bottom.getChildren().get(0); + } else { + vBox = (VBox) bottom.getChildren().get(1); + } + vBox.getChildren().clear(); + vBox.setBorder(null); + } } diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java index a03d878..e993fad 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FactoryDecorator.java @@ -3,7 +3,6 @@ package ch.zhaw.projekt2.turnierverwaltung; import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameController; import ch.zhaw.projekt2.turnierverwaltung.main.gameScheduleView.GameDecorator; import javafx.geometry.Pos; -import javafx.scene.Node; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; @@ -158,6 +157,13 @@ public class FactoryDecorator implements IsObservable{ return factory.loadGameView(vBox ,gameDecorator, this); } + public void printMessageToFooter(String msg, boolean error) { + factory.printMessageToFooter((BorderPane) pane,msg,error); + } + + public void clearMessage(boolean error){ + factory.resetFooter((BorderPane) pane, error); + } public void informListener() { for(IsObserver observer : listener) { 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 73976c3..bb4bd92 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/Tournament.java @@ -2,8 +2,6 @@ package ch.zhaw.projekt2.turnierverwaltung; import javafx.collections.FXCollections; import javafx.collections.ObservableList; - -import java.io.File; import java.io.Serializable; import java.util.*; @@ -29,7 +27,7 @@ public class Tournament implements Serializable { gameList = new ArrayList<>(); } - public void saveParticipant(Participant newParticipant) throws ParticipantExistsException { + public void saveParticipant(Participant newParticipant) { for(Participant participant: participants){ if(participant.equals(newParticipant)){ participant.change(newParticipant); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java index b9af3ad..6da0b55 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/TournamentDecorator.java @@ -1,5 +1,7 @@ package ch.zhaw.projekt2.turnierverwaltung; +import javafx.application.Platform; + import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -11,6 +13,7 @@ public class TournamentDecorator implements IsObservable{ private FileIO fileIO; private List listener = new ArrayList<>(); private ExecutorService executorService; + private FactoryDecorator factoryDecorator; public TournamentDecorator(FileIO fileIO){ setFileIO(fileIO); @@ -26,6 +29,10 @@ public class TournamentDecorator implements IsObservable{ executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); } + public void setFactoryDecorator(FactoryDecorator factoryDecorator) { + this.factoryDecorator = factoryDecorator; + } + public void setFileIO(FileIO fileIO) { this.fileIO = fileIO; } @@ -56,76 +63,106 @@ public class TournamentDecorator implements IsObservable{ 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 { + if(fileIO.tournamentExists(name)){ + //TODO:Logger + factoryDecorator.printMessageToFooter("Turniername existiert bereits",true); + return; + } Tournament tournament = new Tournament(name, type); fileIO.saveTournament(tournament); + factoryDecorator.clearMessage(true); informListener(); } catch (InvalidNameException e) { - e.printStackTrace(); //TODO handle and logging + e.printStackTrace(); + //TODO: Logger + factoryDecorator.printMessageToFooter("Invalider Turniername",true); } catch (Tournament.InvalidTypeException e) { - e.printStackTrace(); //TODO handle and logging + e.printStackTrace(); + //TODO: Logger + factoryDecorator.printMessageToFooter("Turniermodus nicht möglich",true); + } catch (IOException e) { - e.printStackTrace(); //TODO handle and logging + e.printStackTrace(); + //TODO: Logger + factoryDecorator.printMessageToFooter("Fehler bei Dateizugriff",true); } + } public void deleteTournament(FileIO.TournamentFile tournamentFile){ try { fileIO.deleteTournament(tournamentFile); + factoryDecorator.clearMessage(true); informListener(); } catch (IOException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Fehler bei Dateizugriff",true); } } - public void createNewGameSchedule() throws Tournament.NumberOfParticipantInvalidException { - tournament.createGameSchedule(); + public void createNewGameSchedule() { + //TODO: logging + try { + tournament.createGameSchedule(); + factoryDecorator.clearMessage(true); + } catch (Tournament.NumberOfParticipantInvalidException e) { + e.printStackTrace(); + factoryDecorator.printMessageToFooter("Anzahl Teilnehmer nicht vielfaches von 2",true); + } informListener(); } public void savePlayer(String firstName, String name, String phoneNumber, String dateOfBirth){ try { tournament.saveParticipant(new Player(firstName, name, phoneNumber, dateOfBirth)); + factoryDecorator.clearMessage(true); informListener(); - } catch (Tournament.ParticipantExistsException e) { - e.printStackTrace(); //TODO handle and logging } catch (InvalidNameException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Invalider Teilnehmername",true); } catch (Person.InvalidPhoneNumberException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Invalide Telefonnummer",true); } } public void deleteParticipant(Participant participant){ try { tournament.removeParticipant(participant); + factoryDecorator.clearMessage(true); informListener(); } catch (Tournament.ParticipantNotExistsException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Teilnehmer existiert nicht",true); + } } public void savePlace(String name){ try { tournament.addPlace(new Place(name)); + factoryDecorator.clearMessage(true); informListener(); } catch (Tournament.PlaceExistsException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Ort existiert bereits",true); + } catch (InvalidNameException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Invalider Ortsname",true); } } public void deletePlace(Place place){ try { tournament.removePlace(place); + factoryDecorator.clearMessage(true); informListener(); } catch (Tournament.PlaceNotExistsException e) { e.printStackTrace(); //TODO handle and logging + factoryDecorator.printMessageToFooter("Ort existiert nicht",true); } } @@ -143,7 +180,10 @@ public class TournamentDecorator implements IsObservable{ try { fileIO.saveTournament(tournament); } catch (IOException e) { - e.printStackTrace(); //TODO handle and logging + e.printStackTrace(); + Platform.runLater(() -> { + factoryDecorator.printMessageToFooter("Datenzugriff nicht moeglich",true); + }); } } } 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 9ff754e..f97d970 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 @@ -8,23 +8,21 @@ import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; -import javafx.scene.layout.Pane; import javafx.stage.Stage; -import java.awt.*; -import java.io.IOException; public class MainWindow extends Application { private FileIO fileIO = new FileIO(System.getProperty("user.dir") + System.getProperty("file.separator") + "tournierverwaltung_angrynerds"); + private FactoryDecorator factoryDecorator; private TournamentDecorator tournamentDecorator = new TournamentDecorator(fileIO); 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(); factoryDecorator = new FactoryDecorator(fileIO, factory, pane); + tournamentDecorator.setFactoryDecorator(factoryDecorator); factoryDecorator.openTournamentList(); diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java index 4d4410c..b81848d 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/main/gameScheduleView/GameScheduleController.java @@ -30,12 +30,7 @@ public class GameScheduleController extends FXController { @FXML void createNewSchedule(ActionEvent event) { - try { - getTournamentDecorator().createNewGameSchedule(); - } catch (Tournament.NumberOfParticipantInvalidException e) { - //TODO Method in FactoryDecorater to show msg - e.printStackTrace(); - } + getTournamentDecorator().createNewGameSchedule(); } @FXML 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 b70630a..fbef5d2 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 @@ -2,6 +2,7 @@ package ch.zhaw.projekt2.turnierverwaltung.main.tournamentList; import ch.zhaw.projekt2.turnierverwaltung.FXController; import ch.zhaw.projekt2.turnierverwaltung.FileIO; +import ch.zhaw.projekt2.turnierverwaltung.InvalidNameException; import ch.zhaw.projekt2.turnierverwaltung.Tournament; import javafx.collections.FXCollections; import javafx.collections.ObservableList; diff --git a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/MainWindow.fxml b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/MainWindow.fxml index af04e72..0648e2f 100644 --- a/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/MainWindow.fxml +++ b/app/src/main/resources/ch/zhaw/projekt2/turnierverwaltung/MainWindow.fxml @@ -8,7 +8,7 @@ - + @@ -34,4 +34,12 @@ + + + + + + + +