Browse Source

add remove propertyChange listeners at destroy of controller

main^2
fdai7736 11 months ago
parent
commit
e3a96ec193
  1. 3
      README.md
  2. 2
      src/main/java/de/hsfulda/onses/App.java
  3. 9
      src/main/java/de/hsfulda/onses/controllers/AppController.java
  4. 12
      src/main/java/de/hsfulda/onses/controllers/CardController.java
  5. 11
      src/main/java/de/hsfulda/onses/controllers/GameController.java
  6. 9
      src/main/java/de/hsfulda/onses/controllers/PlayerController.java

3
README.md

@ -1,5 +1,8 @@
# Onses - Uno Game # Onses - Uno Game
## Disclaimer for Tests
**!!Do not move the mouse while tests are running!!**
## git setup after clone ## git setup after clone
```shell ```shell
git config user.name "<FD-Nummer>" git config user.name "<FD-Nummer>"

2
src/main/java/de/hsfulda/onses/App.java

@ -20,8 +20,8 @@ public class App extends Application {
this.stage = stage; this.stage = stage;
final AppController appController = new AppController(this, new GameService()); final AppController appController = new AppController(this, new GameService());
stage.setTitle("Onses - Uno");
stage.setScene(new Scene(new Label("Loading..."))); stage.setScene(new Scene(new Label("Loading...")));
stage.setOnCloseRequest(e -> controller.destroy());
show(appController); show(appController);
stage.show(); stage.show();

9
src/main/java/de/hsfulda/onses/controllers/AppController.java

@ -8,7 +8,6 @@ import javafx.scene.Parent;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
public class AppController implements Controller { public class AppController implements Controller {
@ -16,16 +15,14 @@ public class AppController implements Controller {
private final GameService gameService; private final GameService gameService;
private final App app; private final App app;
private final ArrayList<Controller> controllers = new ArrayList<>();
public AppController(App app, GameService gameService) { public AppController(App app, GameService gameService) {
this.app = app; this.app = app;
this.gameService = gameService; this.gameService = gameService;
} }
@Override @Override
public Parent render() throws IOException { public Parent render() throws IOException {
GameController gameController = new GameController(app, gameService); GameController gameController = new GameController(app, gameService);
controllers.add(gameController);
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml"))); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml")));
Button button = (Button) parent.lookup("#startGameBtn"); Button button = (Button) parent.lookup("#startGameBtn");
@ -42,8 +39,6 @@ public class AppController implements Controller {
@Override @Override
public void destroy() { public void destroy() {
for (Controller controller : controllers) {
controller.destroy();
}
} }
} }

12
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.control.Label;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import java.beans.PropertyChangeListener;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@ -16,10 +17,13 @@ public class CardController implements Controller {
private final Card card; private final Card card;
private final Player player; private final Player player;
private PropertyChangeListener cardSelectedChangeListener;
public CardController(Card card, Player player) { public CardController(Card card, Player player) {
this.card = card; this.card = card;
this.player = player; this.player = player;
} }
@Override @Override
public Parent render() throws IOException { public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/card.fxml"))); 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(); boolean oldValue = (boolean) e.getOldValue();
if(oldValue) { if(oldValue) {
mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: black")); 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")); mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: pink"));
} }
});
};
card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener);
return parent; return parent;
} }
@ -93,6 +97,6 @@ public class CardController implements Controller {
@Override @Override
public void destroy() { public void destroy() {
card.listeners().removePropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener);
} }
} }

11
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.control.Button;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
@ -20,7 +20,7 @@ public class GameController implements Controller {
private final Game game; private final Game game;
private final App app; private final App app;
private PropertyChangeEvent lastPlayedCardPropertyChangeEvent;
private PropertyChangeListener lastPlayedCardPropertyChangeListener;
private final ArrayList<Controller> controllers = new ArrayList<>(); private final ArrayList<Controller> controllers = new ArrayList<>();
public GameController(App app, GameService gameService) { public GameController(App app, GameService gameService) {
@ -28,6 +28,7 @@ public class GameController implements Controller {
this.gameService = gameService; this.gameService = gameService;
this.game = gameService.getGame(); this.game = gameService.getGame();
} }
@Override @Override
public Parent render() throws IOException { public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/game.fxml"))); 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(playerController);
controllers.add(enemyController); controllers.add(enemyController);
game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> {
lastPlayedCardPropertyChangeListener = e -> {
lastPlayedCardPane.getChildren().removeAll(); lastPlayedCardPane.getChildren().removeAll();
try { try {
CardController tmp = new CardController((Card) e.getNewValue(), null); CardController tmp = new CardController((Card) e.getNewValue(), null);
@ -55,7 +56,8 @@ public class GameController implements Controller {
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
});
};
game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener);
exitGameButton.setOnAction(e -> { exitGameButton.setOnAction(e -> {
app.show(new AppController(app, new GameService())); app.show(new AppController(app, new GameService()));
@ -93,5 +95,6 @@ public class GameController implements Controller {
for (Controller controller : controllers) { for (Controller controller : controllers) {
controller.destroy(); controller.destroy();
} }
game.listeners().removePropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, lastPlayedCardPropertyChangeListener);
} }
} }

9
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.control.Label;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import java.beans.PropertyChangeListener;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
@ -16,10 +17,12 @@ public class PlayerController implements Controller {
private final Player player; private final Player player;
private final ArrayList<Controller> controllers = new ArrayList<>(); private final ArrayList<Controller> controllers = new ArrayList<>();
private PropertyChangeListener playerDeckChangeListener;
public PlayerController(Player player) { public PlayerController(Player player) {
this.player = player; this.player = player;
} }
@Override @Override
public Parent render() throws IOException { public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/player.fxml"))); 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()); cards.getChildren().add(newCardController.render());
} }
player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> {
playerDeckChangeListener = e -> {
cards.getChildren().clear(); cards.getChildren().clear();
for(Card card : player.getPlayerDeck()) { for(Card card : player.getPlayerDeck()) {
try { try {
@ -41,7 +44,8 @@ public class PlayerController implements Controller {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
});
};
player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener);
playerNameLabel.setText(player.getPlayerName()); playerNameLabel.setText(player.getPlayerName());
@ -58,5 +62,6 @@ public class PlayerController implements Controller {
for (Controller controller : controllers) { for (Controller controller : controllers) {
controller.destroy(); controller.destroy();
} }
player.listeners().removePropertyChangeListener(Player.PROPERTY_PLAYER_DECK, playerDeckChangeListener);
} }
} }
Loading…
Cancel
Save