diff options
Diffstat (limited to 'src/jrummikub')
-rw-r--r-- | src/jrummikub/control/SettingsControl.java | 6 | ||||
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 38 | ||||
-rw-r--r-- | src/jrummikub/control/network/GameJoinControl.java | 21 | ||||
-rw-r--r-- | src/jrummikub/control/network/GameOfferControl.java | 35 | ||||
-rw-r--r-- | src/jrummikub/view/ISettingsPanel.java | 2 | ||||
-rw-r--r-- | src/jrummikub/view/impl/SettingsPanel.java | 17 |
6 files changed, 112 insertions, 7 deletions
diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index 4659828..e54fa6d 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -1,20 +1,21 @@ package jrummikub.control; +import java.awt.Color; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; -import jrummikub.util.Connection; import jrummikub.util.Event1; import jrummikub.util.IEvent1; import jrummikub.util.IListener; import jrummikub.view.ISettingsPanel; -import jrummikub.view.IView; import jrummikub.view.ISettingsPanel.SettingsMode; +import jrummikub.view.IView; /** * The settings control controls the settings panel @@ -35,6 +36,7 @@ public class SettingsControl extends AbstractSettingsControl { view.getSettingsPanel().setSettingsMode(SettingsMode.DEFAULT); view.getSettingsPanel().setPlayerNamesEditable( Collections.<Boolean> emptyList()); + view.getSettingsPanel().setPlayerColors(new HashSet<Color>(Arrays.asList(ISettingsPanel.PLAYER_COLORS))); addPlayer(); addPlayer(); diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index d6bb5fe..6cd3fc0 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -1,5 +1,6 @@ package jrummikub.control.network; +import java.awt.Color; import java.util.UUID; import javax.swing.SwingUtilities; @@ -8,10 +9,12 @@ import jrummikub.model.GameSettings; import jrummikub.util.Event; import jrummikub.util.Event1; import jrummikub.util.Event2; +import jrummikub.util.Event3; import jrummikub.util.GameData; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; +import jrummikub.util.IEvent3; import jrummikub.util.LoginData; import org.jivesoftware.smack.Connection; @@ -50,6 +53,8 @@ class ConnectionControl { private Event2<UUID, Boolean> gameJoinAckEvent = new Event2<UUID, Boolean>(); + private Event3<UUID, String, Color> changeColorEvent = new Event3<UUID, String, Color>(); + private volatile GameData offeredGame; ConnectionControl(LoginData loginData) { @@ -99,6 +104,10 @@ class ConnectionControl { return gameJoinAckEvent; } + IEvent3<UUID, String, Color> getChangeColorEvent() { + return changeColorEvent; + } + void offerGame(GameData data) { offeredGame = data; @@ -181,6 +190,9 @@ class ConnectionControl { } else if (messageType.equals("game_join_ack")) { gameJoinAckEvent.emit(UUID.fromString(extension.getValue("uuid")), Boolean.valueOf(extension.getValue("ack"))); + } else if (messageType.equals("changeColor")) { + changeColorEvent.emit(UUID.fromString(extension.getValue("uuid")), + sender, (Color) Base64.decodeToObject(extension.getValue("color"))); } else { System.err.println("Received unrecognized message of type '" + messageType + "'"); @@ -225,6 +237,15 @@ class ConnectionControl { return createMessage(extension); } + private Message createChangeColorMessage(Color color) { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "change_color"); + extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); + + return createMessage(extension); + } + private Message createGameAckMessage(UUID uuid, String recipient, boolean ack) { DefaultPacketExtension extension = createJRummikubExtension(); @@ -376,6 +397,23 @@ class ConnectionControl { } } + private class SendChangeColorRunner implements Runnable { + private Color color; + + public SendChangeColorRunner(Color color) { + this.color = color; + } + + @Override + public void run() { + synchronized (ConnectionControl.this) { + if (connection != null) { + connection.sendPacket(createChangeColorMessage(color)); + } + } + } + } + private class SendGameLeaveRunner implements Runnable { private UUID uuid; diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 048491a..55f584d 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -1,7 +1,11 @@ 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.GameSettings; import jrummikub.model.PlayerSettings; @@ -9,6 +13,7 @@ import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.GameData; import jrummikub.util.IListener1; +import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; @@ -38,12 +43,26 @@ public class GameJoinControl { fixGameSettings(settings); gameData.setGameSettings(settings); - view.getSettingsPanel().setGameSettings(settings); + + updateSettingsPanel(settings); } } })); } + private void updateSettingsPanel(GameSettings settings) { + view.getSettingsPanel().setGameSettings(settings); + + Set<Color> colors = new HashSet<Color>( + Arrays.asList(ISettingsPanel.PLAYER_COLORS)); + + for (PlayerSettings player : settings.getPlayerList()) { + colors.remove(player.getColor()); + } + + view.getSettingsPanel().setPlayerColors(colors); + } + private void fixGameSettings(GameSettings settings) { for (PlayerSettings player : settings.getPlayerList()) { if (player.getType() == Type.HUMAN) { diff --git a/src/jrummikub/control/network/GameOfferControl.java b/src/jrummikub/control/network/GameOfferControl.java index 7cd289f..247cd18 100644 --- a/src/jrummikub/control/network/GameOfferControl.java +++ b/src/jrummikub/control/network/GameOfferControl.java @@ -1,7 +1,11 @@ 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 java.util.UUID; import jrummikub.model.GameSettings; @@ -10,6 +14,7 @@ import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.GameData; import jrummikub.util.IListener2; +import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; @@ -28,7 +33,7 @@ public class GameOfferControl { view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_OFFER); view.getSettingsPanel().enableAddPlayerButton(false); - view.getSettingsPanel().setGameSettings(settings); + updateSettingsPanel(settings); connections.add(connectionControl.getGameJoinEvent().add( new IListener2<UUID, String>() { @@ -42,7 +47,7 @@ public class GameOfferControl { if (player.getType() == Type.VACANT) { player.setName(sender); player.setType(Type.NETWORK); - view.getSettingsPanel().setGameSettings(settings); + updateSettingsPanel(settings); connectionControl.ackJoinGame(uuid, sender, true); connectionControl.offerGame(gameData); return; @@ -52,6 +57,32 @@ public class GameOfferControl { connectionControl.ackJoinGame(uuid, sender, false); } })); + connections.add(view.getSettingsPanel().getChangePlayerColorEvent() + .add(new IListener2<Integer, Color>() { + @Override + public void handle(Integer i, Color color) { + for (PlayerSettings player : settings.getPlayerList()) { + if (player.getColor() == color) { + return; + } + } + settings.getPlayerList().get(i).setColor(color); + updateSettingsPanel(settings); + } + })); + } + + private void updateSettingsPanel(GameSettings settings) { + view.getSettingsPanel().setGameSettings(settings); + + Set<Color> colors = new HashSet<Color>( + Arrays.asList(ISettingsPanel.PLAYER_COLORS)); + + for (PlayerSettings player : settings.getPlayerList()) { + colors.remove(player.getColor()); + } + + view.getSettingsPanel().setPlayerColors(colors); } public void startGameOffer() { diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java index 279526a..d01129e 100644 --- a/src/jrummikub/view/ISettingsPanel.java +++ b/src/jrummikub/view/ISettingsPanel.java @@ -237,6 +237,8 @@ public interface ISettingsPanel { */ public IEvent getOfferGameEvent(); + public void setPlayerColors(Set<Color> colors); + /** * Specifies the different kinds of settings errors that can be displayed */ diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index d5599a1..b5a69c5 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -41,6 +41,7 @@ import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.SpinnerNumberModel; +import javax.swing.UIManager; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; @@ -67,6 +68,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private List<Boolean> removeButtonsEnabled = Collections.emptyList(); private List<Boolean> playerNamesEditable = Collections.emptyList(); + private Set<Color> playerColors; + private SettingsMode settingsMode = SettingsMode.DEFAULT; private JTabbedPane tabbedPane; @@ -235,6 +238,11 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } @Override + public void setPlayerColors(Set<Color> colors) { + playerColors = colors; + } + + @Override public void setError(SettingsError error) { switch (error) { case NO_ERROR: @@ -454,7 +462,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { jokerNumberLabel = new JLabel(); jokerNumberSpinner = createOptionSpinner(1, 999, 1, changeJokerNumberEvent); - makeOptionLabel(row++, "Zeit für Spielzug:"); + makeOptionLabel(row++, "Zeit f\u00fcr Spielzug:"); timeLabel = new JLabel(); timeSpinner = createOptionSpinner(1, 999, 1, changeTimeEvent); @@ -1019,7 +1027,12 @@ class SettingsPanel extends JPanel implements ISettingsPanel { JPopupMenu menu = new JPopupMenu(); for (Color c : PLAYER_COLORS) { - add(new ColorButton(c, menu)); + if (playerColors.contains(c)) { + add(new ColorButton(c, menu)); + } else { + add(new JLabel(ImageUtil.createColorIcon( + UIManager.getColor("PopupMenu.background"), 16, 0))); + } } menu.setLayout(new BorderLayout()); |