Replace old event framework with dev.kske:event-bus:0.0.3

This commit is contained in:
Kai S. K. Engelbart 2020-09-08 13:41:22 +02:00
parent 4566b33522
commit 2c5521d2ff
9 changed files with 62 additions and 168 deletions

12
pom.xml
View File

@ -44,7 +44,19 @@
<maven.compiler.target>11</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>kske-repo</id>
<url>https://kske.dev/maven-repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>

View File

@ -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: <strong>Chess</strong><br>

View File

@ -1,18 +0,0 @@
package dev.kske.chess.event;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Event.java</strong><br>
* Created: <strong>7 Aug 2019</strong><br>
*
* @since Chess v0.4-alpha
* @author Kai S. K. Engelbart
* @param <T> the type of the event's value
*/
public interface Event<T> {
/**
* @return The data associated with the event
*/
T getData();
}

View File

@ -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.<br>
* <br>
* Project: <strong>Chess</strong><br>
* File: <strong>EventBus.java</strong><br>
* Created: <strong>7 Aug 2019</strong><br>
*
* @since Chess v0.4-alpha
* @author Kai S. K. Engelbart
*/
public class EventBus {
private List<Subscriber> 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<Subscriber> getSubscribers() { return subscribers; }
}

View File

@ -1,6 +1,7 @@
package dev.kske.chess.event;
import dev.kske.chess.game.Game;
import dev.kske.eventbus.IEvent;
/**
* Project: <strong>Chess</strong><br>
@ -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<Game> {
public class GameStartEvent implements IEvent {
private final Game game;
@ -23,6 +24,8 @@ public class GameStartEvent implements Event<Game> {
game = source;
}
@Override
public Game getData() { return game; }
/**
* @return the started game
*/
public Game getGame() { return game; }
}

View File

@ -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: <strong>Chess</strong><br>
@ -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<Move> {
public class MoveEvent implements IEvent {
private final Move move;
private final BoardState boardState;
@ -27,8 +27,10 @@ public class MoveEvent implements Event<Move> {
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

View File

@ -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.<br>
* <br>
* Project: <strong>Chess</strong><br>
* File: <strong>Subscribable.java</strong><br>
* Created: <strong>7 Aug 2019</strong><br>
*
* @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<Class<?>> supports();
}

View File

@ -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: <strong>Chess</strong><br>

View File

@ -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<MoveNode> 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<MoveNode> pgnList = new JList<>();
pgnList = new JList<>();
pgnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pgnList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
pgnList.setVisibleRowCount(0);
@ -180,23 +176,27 @@ 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)
@Event
public void onMove(MoveEvent evt) {
btnSwapColors.setEnabled(
evt.getBoardState() != BoardState.CHECKMATE
&& evt.getBoardState() != BoardState.STALEMATE
);
updateLog();
}
@Event
public void onGameStart(GameStartEvent evt) {
btnSwapColors.setEnabled(
game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer ^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer
);
updateLog();
}
private void updateLog() {
if (game.getBoard().getLog() == null)
return;
@ -205,15 +205,6 @@ public class GamePane extends JComponent {
pgnList.setModel(model);
}
@Override
public Set<Class<?>> supports() {
return new HashSet<>(
Arrays.asList(MoveEvent.class, GameStartEvent.class)
);
}
});
}
/**
* @return The {@link BoardPane} instance associated with this game pane
*/