summaryrefslogtreecommitdiffstats
path: root/src/jrummikub
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub')
-rw-r--r--src/jrummikub/control/network/AbstractGameBeginControl.java108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/jrummikub/control/network/AbstractGameBeginControl.java b/src/jrummikub/control/network/AbstractGameBeginControl.java
new file mode 100644
index 0000000..d938f58
--- /dev/null
+++ b/src/jrummikub/control/network/AbstractGameBeginControl.java
@@ -0,0 +1,108 @@
+package jrummikub.control.network;
+
+import java.awt.Color;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import jrummikub.model.PlayerSettings;
+import jrummikub.model.PlayerSettings.Type;
+import jrummikub.util.Connection;
+import jrummikub.util.Event;
+import jrummikub.util.GameData;
+import jrummikub.util.IListener;
+import jrummikub.util.IListener2;
+import jrummikub.view.ISettingsPanel;
+import jrummikub.view.ISettingsPanel.SettingsMode;
+import jrummikub.view.IView;
+
+public abstract class AbstractGameBeginControl {
+ protected List<Connection> connections = new ArrayList<Connection>();
+ protected GameData gameData;
+ protected ConnectionControl connectionControl;
+ protected IView view;
+ protected Event backEvent = new Event();
+
+ public AbstractGameBeginControl(ConnectionControl connection, IView view,
+ final GameData gameData, SettingsMode settingsMode) {
+ this.connectionControl = connection;
+ this.view = view;
+ this.gameData = gameData;
+
+ view.getSettingsPanel().setSettingsMode(settingsMode);
+ view.getSettingsPanel().enableAddPlayerButton(false);
+ updateSettingsPanel();
+
+ connections.add(view.getSettingsPanel().getBackEvent()
+ .add(new IListener() {
+ @Override
+ public void handle() {
+ goBack();
+ }
+ }));
+ connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
+ .add(new IListener2<Integer, Color>() {
+ @Override
+ public void handle(Integer i, Color color) {
+ for (PlayerSettings player : gameData.getGameSettings()
+ .getPlayerList()) {
+ if (player.getColor() == color) {
+ return;
+ }
+ }
+ PlayerSettings player = gameData.getGameSettings()
+ .getPlayerList().get(i);
+ if (player.getType() != Type.HUMAN) {
+ return;
+ }
+ player.setColor(color);
+ connectionControl.changeColor(color);
+ updateSettingsPanel();
+ }
+ }));
+ connections.add(connectionControl.getChangeColorEvent().add(
+ new IListener2<String, Color>() {
+ @Override
+ public void handle(String sender, Color color) {
+ List<PlayerSettings> players = gameData
+ .getGameSettings().getPlayerList();
+ for (PlayerSettings s : players) {
+ if (s.getName().equals(sender)
+ && s.getType() == Type.NETWORK) {
+ s.setColor(color);
+ break;
+ }
+ }
+ updateSettingsPanel();
+ }
+ }));
+ }
+
+ protected abstract void goBack();
+
+ public Event getBackEvent() {
+ return backEvent;
+ }
+
+ protected void abort() {
+ view.showSettingsPanel(false);
+ for (Connection c : connections) {
+ c.remove();
+ }
+ }
+
+ protected void updateSettingsPanel() {
+ view.getSettingsPanel().setGameSettings(gameData.getGameSettings());
+
+ Set<Color> colors = new HashSet<Color>(
+ Arrays.asList(ISettingsPanel.PLAYER_COLORS));
+
+ for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
+ colors.remove(player.getColor());
+ }
+
+ view.getSettingsPanel().setPlayerColors(colors);
+ }
+}