From e3a96ec19389797d981e27bf04ab5a8f10201ced Mon Sep 17 00:00:00 2001 From: fdai7736 Date: Thu, 8 Feb 2024 22:19:43 +0100 Subject: [PATCH] add remove propertyChange listeners at destroy of controller --- README.md | 3 +++ src/main/java/de/hsfulda/onses/App.java | 2 +- .../de/hsfulda/onses/controllers/AppController.java | 9 ++------- .../de/hsfulda/onses/controllers/CardController.java | 12 ++++++++---- .../de/hsfulda/onses/controllers/GameController.java | 11 +++++++---- .../hsfulda/onses/controllers/PlayerController.java | 9 +++++++-- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b7219f9..b5308df 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Onses - Uno Game +## Disclaimer for Tests +**!!Do not move the mouse while tests are running!!** + ## git setup after clone ```shell git config user.name "" diff --git a/src/main/java/de/hsfulda/onses/App.java b/src/main/java/de/hsfulda/onses/App.java index fff6bd7..8a7436f 100644 --- a/src/main/java/de/hsfulda/onses/App.java +++ b/src/main/java/de/hsfulda/onses/App.java @@ -20,8 +20,8 @@ public class App extends Application { this.stage = stage; final AppController appController = new AppController(this, new GameService()); - stage.setTitle("Onses - Uno"); stage.setScene(new Scene(new Label("Loading..."))); + stage.setOnCloseRequest(e -> controller.destroy()); show(appController); stage.show(); diff --git a/src/main/java/de/hsfulda/onses/controllers/AppController.java b/src/main/java/de/hsfulda/onses/controllers/AppController.java index 80d6da9..4a59ee0 100644 --- a/src/main/java/de/hsfulda/onses/controllers/AppController.java +++ b/src/main/java/de/hsfulda/onses/controllers/AppController.java @@ -8,7 +8,6 @@ import javafx.scene.Parent; import javafx.scene.control.Button; import java.io.IOException; -import java.util.ArrayList; import java.util.Objects; public class AppController implements Controller { @@ -16,16 +15,14 @@ public class AppController implements Controller { private final GameService gameService; private final App app; - private final ArrayList controllers = new ArrayList<>(); - public AppController(App app, GameService gameService) { this.app = app; this.gameService = gameService; } + @Override public Parent render() throws IOException { GameController gameController = new GameController(app, gameService); - controllers.add(gameController); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml"))); Button button = (Button) parent.lookup("#startGameBtn"); @@ -42,8 +39,6 @@ public class AppController implements Controller { @Override public void destroy() { - for (Controller controller : controllers) { - controller.destroy(); - } + } } diff --git a/src/main/java/de/hsfulda/onses/controllers/CardController.java b/src/main/java/de/hsfulda/onses/controllers/CardController.java index 2329232..3d05811 100644 --- a/src/main/java/de/hsfulda/onses/controllers/CardController.java +++ b/src/main/java/de/hsfulda/onses/controllers/CardController.java @@ -8,6 +8,7 @@ import javafx.scene.Parent; import javafx.scene.control.Label; import javafx.scene.layout.Pane; +import java.beans.PropertyChangeListener; import java.io.IOException; import java.util.Objects; @@ -16,10 +17,13 @@ public class CardController implements Controller { private final Card card; private final Player player; + private PropertyChangeListener cardSelectedChangeListener; + public CardController(Card card, Player player) { this.card = card; this.player = player; } + @Override public Parent render() throws IOException { final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/card.fxml"))); @@ -68,7 +72,7 @@ public class CardController implements Controller { }); } - card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, e -> { + cardSelectedChangeListener = e -> { boolean oldValue = (boolean) e.getOldValue(); if(oldValue) { mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: black")); @@ -76,8 +80,8 @@ public class CardController implements Controller { mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: pink")); } - }); - + }; + card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener); return parent; } @@ -93,6 +97,6 @@ public class CardController implements Controller { @Override public void destroy() { - + card.listeners().removePropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener); } } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index 1dff304..3eccd0c 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -10,7 +10,7 @@ import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.layout.Pane; -import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; @@ -20,7 +20,7 @@ public class GameController implements Controller { private final Game game; private final App app; - private PropertyChangeEvent lastPlayedCardPropertyChangeEvent; + private PropertyChangeListener lastPlayedCardPropertyChangeListener; private final ArrayList controllers = new ArrayList<>(); public GameController(App app, GameService gameService) { @@ -28,6 +28,7 @@ public class GameController implements Controller { this.gameService = gameService; this.game = gameService.getGame(); } + @Override public Parent render() throws IOException { final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/game.fxml"))); @@ -46,7 +47,7 @@ public class GameController implements Controller { controllers.add(playerController); controllers.add(enemyController); - game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> { + lastPlayedCardPropertyChangeListener = e -> { lastPlayedCardPane.getChildren().removeAll(); try { CardController tmp = new CardController((Card) e.getNewValue(), null); @@ -55,7 +56,8 @@ public class GameController implements Controller { } catch (IOException ex) { throw new RuntimeException(ex); } - }); + }; + game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener); exitGameButton.setOnAction(e -> { app.show(new AppController(app, new GameService())); @@ -93,5 +95,6 @@ public class GameController implements Controller { for (Controller controller : controllers) { controller.destroy(); } + game.listeners().removePropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener); } } diff --git a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java index 4815e02..67286ea 100644 --- a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java +++ b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java @@ -8,6 +8,7 @@ import javafx.scene.Parent; import javafx.scene.control.Label; import javafx.scene.layout.HBox; +import java.beans.PropertyChangeListener; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; @@ -16,10 +17,12 @@ public class PlayerController implements Controller { private final Player player; private final ArrayList controllers = new ArrayList<>(); + private PropertyChangeListener playerDeckChangeListener; public PlayerController(Player player) { this.player = player; } + @Override public Parent render() throws IOException { final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/player.fxml"))); @@ -32,7 +35,7 @@ public class PlayerController implements Controller { cards.getChildren().add(newCardController.render()); } - player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> { + playerDeckChangeListener = e -> { cards.getChildren().clear(); for(Card card : player.getPlayerDeck()) { try { @@ -41,7 +44,8 @@ public class PlayerController implements Controller { throw new RuntimeException(ex); } } - }); + }; + player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener); playerNameLabel.setText(player.getPlayerName()); @@ -58,5 +62,6 @@ public class PlayerController implements Controller { for (Controller controller : controllers) { controller.destroy(); } + player.listeners().removePropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener); } }