From 2c5521d2ff55190108cf3b6c24c1ff89df6e56ba Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 8 Sep 2020 13:41:22 +0200 Subject: [PATCH] Replace old event framework with dev.kske:event-bus:0.0.3 --- pom.xml | 12 ++++ src/main/java/dev/kske/chess/board/Board.java | 2 +- src/main/java/dev/kske/chess/event/Event.java | 18 ----- .../java/dev/kske/chess/event/EventBus.java | 59 --------------- .../dev/kske/chess/event/GameStartEvent.java | 9 ++- .../java/dev/kske/chess/event/MoveEvent.java | 12 ++-- .../java/dev/kske/chess/event/Subscriber.java | 31 -------- src/main/java/dev/kske/chess/game/Game.java | 16 ++--- src/main/java/dev/kske/chess/ui/GamePane.java | 71 ++++++++----------- 9 files changed, 62 insertions(+), 168 deletions(-) delete mode 100644 src/main/java/dev/kske/chess/event/Event.java delete mode 100644 src/main/java/dev/kske/chess/event/EventBus.java delete mode 100644 src/main/java/dev/kske/chess/event/Subscriber.java diff --git a/pom.xml b/pom.xml index 62f0411..b15012b 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,19 @@ 11 + + + kske-repo + https://kske.dev/maven-repo + + + + + dev.kske + event-bus + 0.0.3 + org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/dev/kske/chess/board/Board.java b/src/main/java/dev/kske/chess/board/Board.java index a435c17..08dfd04 100644 --- a/src/main/java/dev/kske/chess/board/Board.java +++ b/src/main/java/dev/kske/chess/board/Board.java @@ -3,8 +3,8 @@ package dev.kske.chess.board; import java.util.*; import dev.kske.chess.board.Piece.Color; -import dev.kske.chess.event.EventBus; import dev.kske.chess.event.MoveEvent; +import dev.kske.eventbus.EventBus; /** * Project: Chess
diff --git a/src/main/java/dev/kske/chess/event/Event.java b/src/main/java/dev/kske/chess/event/Event.java deleted file mode 100644 index 2cc982f..0000000 --- a/src/main/java/dev/kske/chess/event/Event.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.kske.chess.event; - -/** - * Project: Chess
- * File: Event.java
- * Created: 7 Aug 2019
- * - * @since Chess v0.4-alpha - * @author Kai S. K. Engelbart - * @param the type of the event's value - */ -public interface Event { - - /** - * @return The data associated with the event - */ - T getData(); -} diff --git a/src/main/java/dev/kske/chess/event/EventBus.java b/src/main/java/dev/kske/chess/event/EventBus.java deleted file mode 100644 index 8ae27ae..0000000 --- a/src/main/java/dev/kske/chess/event/EventBus.java +++ /dev/null @@ -1,59 +0,0 @@ -package dev.kske.chess.event; - -import java.util.ArrayList; -import java.util.List; - -/** - * Dispatches {@link Event}s to various {@link Subscriber}s.
- *
- * Project: Chess
- * File: EventBus.java
- * Created: 7 Aug 2019
- * - * @since Chess v0.4-alpha - * @author Kai S. K. Engelbart - */ -public class EventBus { - - private List subscribers; - - private static EventBus instance; - - /** - * @return a singleton instance of {@link EventBus} - */ - public static EventBus getInstance() { - if (instance == null) - instance = new EventBus(); - return instance; - } - - private EventBus() { - subscribers = new ArrayList<>(); - } - - /** - * Registers a subscriber to which future events will be dispatched. - * - * @param subscribable the subscriber to register - */ - public void register(Subscriber subscribable) { - subscribers.add(subscribable); - } - - /** - * Dispatches an event to all {@Subscriber}s registered at this event bus. - * - * @param event the event to dispatch - */ - public void dispatch(Event event) { - subscribers.stream() - .filter(e -> e.supports().contains(event.getClass())) - .forEach(e -> e.handle(event)); - } - - /** - * @return a list of all registered subscribers - */ - public List getSubscribers() { return subscribers; } -} diff --git a/src/main/java/dev/kske/chess/event/GameStartEvent.java b/src/main/java/dev/kske/chess/event/GameStartEvent.java index f2dbd47..14cac7d 100644 --- a/src/main/java/dev/kske/chess/event/GameStartEvent.java +++ b/src/main/java/dev/kske/chess/event/GameStartEvent.java @@ -1,6 +1,7 @@ package dev.kske.chess.event; import dev.kske.chess.game.Game; +import dev.kske.eventbus.IEvent; /** * Project: Chess
@@ -10,7 +11,7 @@ import dev.kske.chess.game.Game; * @since Chess v0.5-alpha * @author Kai S. K. Engelbart */ -public class GameStartEvent implements Event { +public class GameStartEvent implements IEvent { private final Game game; @@ -23,6 +24,8 @@ public class GameStartEvent implements Event { game = source; } - @Override - public Game getData() { return game; } + /** + * @return the started game + */ + public Game getGame() { return game; } } diff --git a/src/main/java/dev/kske/chess/event/MoveEvent.java b/src/main/java/dev/kske/chess/event/MoveEvent.java index 87a91b5..1e1c3c6 100644 --- a/src/main/java/dev/kske/chess/event/MoveEvent.java +++ b/src/main/java/dev/kske/chess/event/MoveEvent.java @@ -1,7 +1,7 @@ package dev.kske.chess.event; -import dev.kske.chess.board.BoardState; -import dev.kske.chess.board.Move; +import dev.kske.chess.board.*; +import dev.kske.eventbus.IEvent; /** * Project: Chess
@@ -11,7 +11,7 @@ import dev.kske.chess.board.Move; * @since Chess v0.4-alpha * @author Kai S. K. Engelbart */ -public class MoveEvent implements Event { +public class MoveEvent implements IEvent { private final Move move; private final BoardState boardState; @@ -27,8 +27,10 @@ public class MoveEvent implements Event { this.boardState = boardState; } - @Override - public Move getData() { return move; } + /** + * @return the move + */ + public Move getMove() { return move; } /** * @return the state of the board after the move diff --git a/src/main/java/dev/kske/chess/event/Subscriber.java b/src/main/java/dev/kske/chess/event/Subscriber.java deleted file mode 100644 index b967996..0000000 --- a/src/main/java/dev/kske/chess/event/Subscriber.java +++ /dev/null @@ -1,31 +0,0 @@ -package dev.kske.chess.event; - -import java.util.Set; - -/** - * Implementations of this interface can register themselves at the - * {@link EventBus} and will be triggered every time an {@link Event} of a - * supported type.
- *
- * Project: Chess
- * File: Subscribable.java
- * Created: 7 Aug 2019
- * - * @since Chess v0.4-alpha - * @author Kai S. K. Engelbart - */ -public interface Subscriber { - - /** - * Consumes an event dispatched by an event bus. - * - * @param event The event dispatched by the event bus, only of supported - * type - */ - void handle(Event event); - - /** - * @return A set of classes this class is supposed to handle in events - */ - Set> supports(); -} diff --git a/src/main/java/dev/kske/chess/game/Game.java b/src/main/java/dev/kske/chess/game/Game.java index d4bb36c..a89e8fa 100644 --- a/src/main/java/dev/kske/chess/game/Game.java +++ b/src/main/java/dev/kske/chess/game/Game.java @@ -1,23 +1,17 @@ package dev.kske.chess.game; -import java.util.EnumMap; -import java.util.Map; +import java.util.*; import javax.swing.JOptionPane; -import dev.kske.chess.board.Board; -import dev.kske.chess.board.BoardState; -import dev.kske.chess.board.Move; +import dev.kske.chess.board.*; import dev.kske.chess.board.Piece.Color; -import dev.kske.chess.event.EventBus; -import dev.kske.chess.event.GameStartEvent; -import dev.kske.chess.event.MoveEvent; +import dev.kske.chess.event.*; import dev.kske.chess.game.ai.AIPlayer; import dev.kske.chess.io.EngineUtil; import dev.kske.chess.io.EngineUtil.EngineInfo; -import dev.kske.chess.ui.BoardComponent; -import dev.kske.chess.ui.BoardPane; -import dev.kske.chess.ui.OverlayComponent; +import dev.kske.chess.ui.*; +import dev.kske.eventbus.EventBus; /** * Project: Chess
diff --git a/src/main/java/dev/kske/chess/ui/GamePane.java b/src/main/java/dev/kske/chess/ui/GamePane.java index 44f36de..36a1dd6 100644 --- a/src/main/java/dev/kske/chess/ui/GamePane.java +++ b/src/main/java/dev/kske/chess/ui/GamePane.java @@ -1,20 +1,15 @@ package dev.kske.chess.ui; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.GridLayout; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; +import java.awt.*; import javax.swing.*; -import dev.kske.chess.board.BoardState; -import dev.kske.chess.board.MoveNode; +import dev.kske.chess.board.*; import dev.kske.chess.board.Piece.Color; import dev.kske.chess.event.*; -import dev.kske.chess.game.Game; -import dev.kske.chess.game.NaturalPlayer; +import dev.kske.chess.game.*; +import dev.kske.eventbus.*; +import dev.kske.eventbus.Event; /** * The part of this application's {@link MainWindow} that displays {@link Game}s @@ -27,7 +22,7 @@ import dev.kske.chess.game.NaturalPlayer; * @since Chess v0.4-alpha * @author Kai S. K. Engelbart */ -public class GamePane extends JComponent { +public class GamePane extends JComponent implements EventListener { private static final long serialVersionUID = 4349772338239617477L; @@ -37,6 +32,7 @@ public class GamePane extends JComponent { private Color activeColor; private JPanel moveSelectionPanel; private JButton btnFirst, btnPrevious, btnNext, btnLast; + private JList pgnList; /** * Creates an instance of {@link GamePane}. @@ -170,7 +166,7 @@ public class GamePane extends JComponent { gbc_scrollPane.gridy = 1; add(scrollPane, gbc_scrollPane); - JList pgnList = new JList<>(); + pgnList = new JList<>(); pgnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); pgnList.setLayoutOrientation(JList.HORIZONTAL_WRAP); pgnList.setVisibleRowCount(0); @@ -180,38 +176,33 @@ public class GamePane extends JComponent { // Listen to moves and game (re-)starts and update the move list or // disable the // color switching buttons if necessary - EventBus.getInstance().register(new Subscriber() { + EventBus.getInstance().registerListener(this); + } - @Override - public void handle(Event event) { - if ( - event instanceof MoveEvent && (((MoveEvent) event) - .getBoardState() == BoardState.CHECKMATE - || ((MoveEvent) event) - .getBoardState() == BoardState.STALEMATE) - ) - btnSwapColors.setEnabled(false); - else - if (event instanceof GameStartEvent) - btnSwapColors.setEnabled( - game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer - ); + @Event + public void onMove(MoveEvent evt) { + btnSwapColors.setEnabled( + evt.getBoardState() != BoardState.CHECKMATE + && evt.getBoardState() != BoardState.STALEMATE + ); + updateLog(); + } - if (game.getBoard().getLog() == null) - return; + @Event + public void onGameStart(GameStartEvent evt) { + btnSwapColors.setEnabled( + game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer + ); + updateLog(); + } - DefaultListModel model = new DefaultListModel<>(); - game.getBoard().getLog().forEach(model::addElement); - pgnList.setModel(model); - } + private void updateLog() { + if (game.getBoard().getLog() == null) + return; - @Override - public Set> supports() { - return new HashSet<>( - Arrays.asList(MoveEvent.class, GameStartEvent.class) - ); - } - }); + DefaultListModel model = new DefaultListModel<>(); + game.getBoard().getLog().forEach(model::addElement); + pgnList.setModel(model); } /**