Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Observable and ModelChangeListener with EventBus #3749

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
import net.rptools.maptool.model.library.addon.AddOnLibraryImporter;
import net.rptools.maptool.model.library.addon.TransferableAddOnLibrary;
import net.rptools.maptool.model.player.Player;
import net.rptools.maptool.model.zones.TokensAdded;
import net.rptools.maptool.model.zones.TokensRemoved;
import net.rptools.maptool.model.zones.ZoneAdded;
import net.rptools.maptool.model.zones.ZoneRemoved;
import net.rptools.maptool.server.Mapper;
import net.rptools.maptool.server.ServerMessageHandler;
import net.rptools.maptool.server.ServerPolicy;
Expand Down Expand Up @@ -666,8 +669,14 @@ private void handle(RemoveZoneMsg msg) {
EventQueue.invokeLater(
() -> {
var zoneGUID = GUID.valueOf(msg.getZoneGuid());
final var renderer = MapTool.getFrame().getZoneRenderer(zoneGUID);
final var zone = renderer.getZone();
MapTool.getCampaign().removeZone(zoneGUID);
MapTool.getFrame().removeZoneRenderer(MapTool.getFrame().getZoneRenderer(zoneGUID));
MapTool.getFrame().removeZoneRenderer(renderer);

// Now we have fire off adding the tokens in the zone
new MapToolEventBus().getMainEventBus().post(new TokensRemoved(zone, zone.getTokens()));
new MapToolEventBus().getMainEventBus().post(new ZoneRemoved(zone));
});
}

Expand Down Expand Up @@ -731,7 +740,10 @@ private void handle(PutZoneMsg msg) {
if (MapTool.getFrame().getCurrentZoneRenderer() == null && zone.isVisible()) {
MapTool.getFrame().setCurrentZoneRenderer(renderer);
}

new MapToolEventBus().getMainEventBus().post(new ZoneAdded(zone));
// Now we have fire off adding the tokens in the zone
new MapToolEventBus().getMainEventBus().post(new TokensAdded(zone, zone.getTokens()));
});
}

Expand Down
47 changes: 26 additions & 21 deletions src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
import net.rptools.lib.net.RPTURLStreamHandlerFactory;
import net.rptools.lib.sound.SoundManager;
import net.rptools.lib.swing.SwingUtil;
import net.rptools.maptool.client.events.ChatMessageAdded;
import net.rptools.maptool.client.events.PlayerConnected;
import net.rptools.maptool.client.events.PlayerDisconnected;
import net.rptools.maptool.client.events.ServerStopped;
import net.rptools.maptool.client.functions.UserDefinedMacroFunctions;
import net.rptools.maptool.client.swing.MapToolEventQueue;
import net.rptools.maptool.client.swing.NoteFrame;
Expand All @@ -78,7 +82,6 @@
import net.rptools.maptool.model.Campaign;
import net.rptools.maptool.model.CampaignFactory;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.ObservableList;
import net.rptools.maptool.model.TextMessage;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.model.ZoneFactory;
Expand All @@ -88,7 +91,10 @@
import net.rptools.maptool.model.player.PlayerDatabase;
import net.rptools.maptool.model.player.PlayerDatabaseFactory;
import net.rptools.maptool.model.player.Players;
import net.rptools.maptool.model.zones.TokensAdded;
import net.rptools.maptool.model.zones.TokensRemoved;
import net.rptools.maptool.model.zones.ZoneAdded;
import net.rptools.maptool.model.zones.ZoneRemoved;
import net.rptools.maptool.protocol.syrinscape.SyrinscapeURLStreamHandler;
import net.rptools.maptool.server.MapToolServer;
import net.rptools.maptool.server.ServerCommand;
Expand Down Expand Up @@ -145,8 +151,7 @@ public class MapTool {

private static Campaign campaign;

private static ObservableList<Player> playerList;
private static ObservableList<TextMessage> messageList;
private static List<Player> playerList;
private static LocalPlayer player;

private static MapToolConnection conn;
Expand Down Expand Up @@ -651,9 +656,7 @@ private static void initialize() {
assetTransferManager = new AssetTransferManager();
assetTransferManager.addConsumerListener(new AssetTransferHandler());

playerList = new ObservableList<Player>();
messageList =
new ObservableList<TextMessage>(Collections.synchronizedList(new ArrayList<TextMessage>()));
playerList = new ArrayList<>();

handler = new ClientMessageHandler();

Expand Down Expand Up @@ -744,6 +747,7 @@ public static MapToolServer getServer() {
public static void addPlayer(Player player) {
if (!playerList.contains(player)) {
playerList.add(player);
new MapToolEventBus().getMainEventBus().post(new PlayerConnected(player));
new Players().playerSignedIn(player);

// LATER: Make this non-anonymous
Expand All @@ -757,20 +761,13 @@ public static void addPlayer(Player player) {
}
}

public Player getPlayer(String name) {
for (int i = 0; i < playerList.size(); i++) {
if (playerList.get(i).getName().equals(name)) {
return playerList.get(i);
}
}
return null;
}

public static void removePlayer(Player player) {
if (player == null) {
return;
}
playerList.remove(player);
new MapToolEventBus().getMainEventBus().post(new PlayerDisconnected(player));

new Players().playerSignedOut(player);

if (MapTool.getPlayer() != null && !player.equals(MapTool.getPlayer())) {
Expand All @@ -780,10 +777,6 @@ public static void removePlayer(Player player) {
}
}

public static ObservableList<TextMessage> getMessageList() {
return messageList;
}

/**
* These are the messages that originate from the server
*
Expand Down Expand Up @@ -816,7 +809,8 @@ public static void addServerMessage(TextMessage message) {
if (message.isWhisper()) {
setLastWhisperer(message.getSource());
}
messageList.add(message);

new MapToolEventBus().getMainEventBus().post(new ChatMessageAdded(message));
}

/**
Expand Down Expand Up @@ -960,6 +954,8 @@ public static void setCampaign(Campaign campaign, GUID defaultRendererId) {
currRenderer = renderer;
}
new MapToolEventBus().getMainEventBus().post(new ZoneAdded(zone));
// Now we have fire off adding the tokens in the zone
new MapToolEventBus().getMainEventBus().post(new TokensAdded(zone, zone.getTokens()));
}
clientFrame.setCurrentZoneRenderer(currRenderer);
clientFrame.getInitiativePanel().setOwnerPermissions(campaign.isInitiativeOwnerPermissions());
Expand Down Expand Up @@ -1072,7 +1068,7 @@ public static void stopServer() {
getFrame().getConnectionPanel().stopHosting();
}

public static ObservableList<Player> getPlayerList() {
public static List<Player> getPlayerList() {
return playerList;
}

Expand Down Expand Up @@ -1120,6 +1116,10 @@ public static void removeZone(Zone zone) {
MapTool.serverCommand().removeZone(zone.getId());
MapTool.getFrame().removeZoneRenderer(MapTool.getFrame().getZoneRenderer(zone.getId()));
MapTool.getCampaign().removeZone(zone.getId());

// Now we have fire off adding the tokens in the zone
new MapToolEventBus().getMainEventBus().post(new TokensRemoved(zone, zone.getTokens()));
new MapToolEventBus().getMainEventBus().post(new ZoneRemoved(zone));
}

public static void addZone(Zone zone) {
Expand All @@ -1137,6 +1137,8 @@ public static void addZone(Zone zone, boolean changeZone) {
getCampaign().putZone(zone);
serverCommand().putZone(zone);
new MapToolEventBus().getMainEventBus().post(new ZoneAdded(zone));
// Now we have fire off adding the tokens in the zone
new MapToolEventBus().getMainEventBus().post(new TokensAdded(zone, zone.getTokens()));

// Show the new zone
if (changeZone) {
Expand Down Expand Up @@ -1254,7 +1256,10 @@ public static void disconnect() {
// This isn't critical, we're closing it anyway
log.debug("While closing connection", ioe);
}

new MapToolEventBus().getMainEventBus().post(new ServerStopped());
playerList.clear();

MapTool.getFrame()
.getConnectionStatusPanel()
.setStatus(ConnectionStatusPanel.Status.disconnected);
Expand Down
48 changes: 0 additions & 48 deletions src/main/java/net/rptools/maptool/client/PlayerListModel.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.model.tokens;
package net.rptools.maptool.client.events;

import java.util.Set;
import net.rptools.maptool.model.TextMessage;

/** Event fired when a token is added to a map. */
public record TokensAddedEvent(Set<TokenInfo> info) {}
public record ChatMessageAdded(TextMessage message) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.events;

import net.rptools.maptool.model.player.Player;

public record PlayerConnected(Player player) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.events;

import net.rptools.maptool.model.player.Player;

public record PlayerDisconnected(Player player) {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.model;
package net.rptools.maptool.client.events;

public interface ModelChangeListener {
public void modelChanged(ModelChangeEvent event);
}
public record ServerStopped() {}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import net.rptools.maptool.client.ui.zone.ZoneRenderer;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.ObservableList;
import net.rptools.maptool.model.TextMessage;
import net.rptools.maptool.model.Token;
import net.rptools.maptool.model.Zone;
Expand Down Expand Up @@ -584,7 +583,7 @@ private static void doOutput(
}

private static void doWhisper(String message, Token token, String playerName) {
ObservableList<Player> playerList = MapTool.getPlayerList();
List<Player> playerList = MapTool.getPlayerList();
List<String> players = new ArrayList<>();
for (int count = 0; count < playerList.size(); count++) {
Player p = playerList.get(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.ObservableList;
import net.rptools.maptool.model.player.Player;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
Expand All @@ -46,7 +45,7 @@ public Object childEvaluate(
if (functionName.equalsIgnoreCase("getPlayerName")) {
return MapTool.getPlayer().getName();
} else if ("getAllPlayerNames".equalsIgnoreCase(functionName)) {
ObservableList<Player> players = MapTool.getPlayerList();
List<Player> players = MapTool.getPlayerList();
String[] playerArray = new String[players.size()];
Iterator<Player> iter = players.iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import net.rptools.maptool.client.macro.MacroContext;
import net.rptools.maptool.client.macro.MacroDefinition;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.ObservableList;
import net.rptools.maptool.model.TextMessage;
import net.rptools.maptool.model.player.Player;
import net.rptools.maptool.util.MessageUtil;
Expand All @@ -45,7 +44,7 @@ public void execute(MacroContext context, String macro, MapToolMacroContext exec
(macro.startsWith("\"")) ? macro.indexOf(" ", playerName.length() + 2) : macro.indexOf(" ");

String message = processText(macro.substring(indexSpace + 1));
ObservableList<Player> playerList = MapTool.getPlayerList();
List<Player> playerList = MapTool.getPlayerList();
List<String> players = new ArrayList<String>();
for (int count = 0; count < playerList.size(); count++) {
Player p = playerList.get(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.model.ModelChangeEvent;
import net.rptools.maptool.model.ModelChangeListener;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.model.drawing.Drawable;
import net.rptools.maptool.model.drawing.Pen;
Expand All @@ -41,7 +39,7 @@
* @version $Revision: 5828 $ $Date: 2011-11-26 18:29:24 -0500 (Sat, 26 Nov 2011) $ $Author:
* azhrei_fje $
*/
public class UndoPerZone implements ModelChangeListener {
public class UndoPerZone {
private static final Logger log = LogManager.getLogger(UndoPerZone.class);

/** Swing's undo/redo support */
Expand Down Expand Up @@ -190,8 +188,4 @@ public void redo() throws CannotRedoException {
MapTool.serverCommand().draw(zone.getId(), pen, drawable);
}
}

public void modelChanged(ModelChangeEvent event) {
log.debug("Inside the modelChanged() event");
}
}
Loading