diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-13 22:29:28 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-13 22:29:28 +0200 |
commit | 570187c95000afd1ac9f6d6e8bb5cab30b984e87 (patch) | |
tree | cd6ed34d64da7621545d66c9f1126c723645b58f /src | |
parent | 741a94953f2d4667de564d8ade37e68a04442fe0 (diff) | |
download | JRummikub-570187c95000afd1ac9f6d6e8bb5cab30b984e87.tar JRummikub-570187c95000afd1ac9f6d6e8bb5cab30b984e87.zip |
Implemented most of network settings control
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@425 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src')
-rw-r--r-- | src/jrummikub/control/RoundControl.java | 6 | ||||
-rw-r--r-- | src/jrummikub/control/SettingsControl.java | 106 | ||||
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 4 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkControl.java | 2 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkSettingsControl.java | 114 | ||||
-rw-r--r-- | src/jrummikub/model/PlayerSettings.java | 4 | ||||
-rw-r--r-- | src/jrummikub/view/ISettingsPanel.java | 27 | ||||
-rw-r--r-- | src/jrummikub/view/impl/GameListPanel.java | 2 | ||||
-rw-r--r-- | src/jrummikub/view/impl/SettingsPanel.java | 198 |
9 files changed, 294 insertions, 169 deletions
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index a420b6d..89baeb2 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -100,7 +100,7 @@ public class RoundControl { private void prepareTurn() { boolean isHuman = roundState.getActivePlayer().getPlayerSettings() - .getTurnControlType() == HUMAN; + .getType() == HUMAN; clonedTable = (ITable) roundState.getTable().clone(); clonedHand = (IHand) roundState.getActivePlayer().getHand().clone(); @@ -123,7 +123,7 @@ public class RoundControl { if (turnControl != null) return; boolean isHuman = roundState.getActivePlayer().getPlayerSettings() - .getTurnControlType() == HUMAN; + .getType() == HUMAN; TurnMode turnMode = TurnMode.NORMAL_TURN; @@ -139,7 +139,7 @@ public class RoundControl { } turnControl = TurnControlFactory.getFactory( roundState.getActivePlayer().getPlayerSettings() - .getTurnControlType()).create(); + .getType()).create(); turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand, roundState.getActivePlayer().getLaidOut(), turnMode), roundState.getGameSettings(), view); diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index abcbd5d..398ce5d 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -3,6 +3,7 @@ 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 java.util.Set; @@ -34,20 +35,44 @@ public class SettingsControl { * Create a new settings control * * @param view - * the view to use + * the view to use * @param settings - * initial game settings + * initial game settings */ public SettingsControl(IView view, GameSettings settings) { this.view = view; this.settings = settings; view.getSettingsPanel().enableNetworkMode(false); + view.getSettingsPanel().setPlayerNamesEditable( + Collections.<Boolean> emptyList()); + addPlayer(); + addPlayer(); + + addListeners(); + + connections.add(view.getSettingsPanel().getStartGameEvent() + .add(new IListener() { + @Override + public void handle() { + startGame(); + } + })); + } + + protected SettingsControl() { + } + + protected void addListeners() { + addPlayerSettingsListeners(); + + addOptionListeners1(); + addOptionListeners2(); } /** - * the start game event is emitted when the user wants to start a game and - * the settings made are valid + * the start game event is emitted when the user wants to start a game and the + * settings made are valid * * @return the event */ @@ -59,28 +84,12 @@ public class SettingsControl { * Start the operation of the settings control */ public void startSettings() { - addPlayer(); - addPlayer(); - - addPlayerSettingsListeners(); - - addOptionListeners1(); - addOptionListeners2(); - - connections.add(view.getSettingsPanel().getStartGameEvent() - .add(new IListener() { - @Override - public void handle() { - startGame(); - } - })); view.showSettingsPanel(true); } private void addOptionListeners1() { connections.add(view.getSettingsPanel() - .getChangeInitialMeldThresholdEvent() - .add(new IListener1<Integer>() { + .getChangeInitialMeldThresholdEvent().add(new IListener1<Integer>() { @Override public void handle(Integer value) { settings.setInitialMeldThreshold(value); @@ -105,8 +114,7 @@ public class SettingsControl { update(); } })); - connections.add(view.getSettingsPanel() - .getChangeNumberOfStonesDealtEvent() + connections.add(view.getSettingsPanel().getChangeNumberOfStonesDealtEvent() .add(new IListener1<Integer>() { @Override public void handle(Integer value) { @@ -217,7 +225,7 @@ public class SettingsControl { })); } - private void addPlayer() { + protected void addPlayer() { if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) { return; } @@ -237,7 +245,13 @@ public class SettingsControl { break; } - // Next, find unused color + settings.getPlayerList().add( + new PlayerSettings("Spieler " + num, findUnusedColor())); + + update(); + } + + protected Color findUnusedColor() { Color color = null; colorLoop: for (Color c : ISettingsPanel.PLAYER_COLORS) { color = c; @@ -248,11 +262,7 @@ public class SettingsControl { } break; } - - settings.getPlayerList().add( - new PlayerSettings("Spieler " + num, color)); - - update(); + return color; } private void removePlayer(int i) { @@ -283,17 +293,18 @@ public class SettingsControl { update(); } - private void setPlayerType(int i, Type type) { - settings.getPlayerList().get(i).setTurnControlType(type); + protected void setPlayerType(int i, Type type) { + settings.getPlayerList().get(i).setType(type); update(); } protected void update() { + boolean enableRemoveButtons = settings.getPlayerList().size() > 2; view.getSettingsPanel().enableRemovePlayerButtons( - settings.getPlayerList().size() > 2); - view.getSettingsPanel() - .enableAddPlayerButton( - settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); + Collections.nCopies(settings.getPlayerList().size(), + enableRemoveButtons)); + view.getSettingsPanel().enableAddPlayerButton( + settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); checkSettings(); @@ -305,7 +316,7 @@ public class SettingsControl { view.getSettingsPanel().setGameSettings(settings); } - private boolean checkSettings() { + protected boolean checkSettings() { if (!checkErrors()) { return false; } @@ -328,13 +339,19 @@ public class SettingsControl { } } for (int i = 0; i < settings.getPlayerList().size(); ++i) { - String name = settings.getPlayerList().get(i).getName(); + PlayerSettings player1 = settings.getPlayerList().get(i); + String name = player1.getName(); + Type type = player1.getType(); + + if (type == Type.NETWORK) { + continue; + } for (int j = i + 1; j < settings.getPlayerList().size(); ++j) { - if (settings.getPlayerList().get(j).getName().equals(name)) { - view.getSettingsPanel() - .setError( - ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); + PlayerSettings player2 = settings.getPlayerList().get(j); + if (player2.getName().equals(name) && player2.getType() == type) { + view.getSettingsPanel().setError( + ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); view.getSettingsPanel().enableStartGameButton(false); return false; } @@ -343,8 +360,7 @@ public class SettingsControl { int totalStonesDealt = settings.getNumberOfStonesDealt() * settings.getPlayerList().size(); - int totalStones = settings.getHighestValue() - * settings.getStoneSetNumber() + int totalStones = settings.getHighestValue() * settings.getStoneSetNumber() * settings.getStoneColors().size() + settings.getJokerNumber(); if (totalStones <= totalStonesDealt) { @@ -373,7 +389,7 @@ public class SettingsControl { boolean humanPlayerFound = false; for (PlayerSettings player : settings.getPlayerList()) { - if (player.getTurnControlType() == Type.HUMAN) { + if (player.getType() == Type.HUMAN) { humanPlayerFound = true; break; } diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 56dac8c..fd8087a 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -49,6 +49,10 @@ class ConnectionControl { this.loginData = loginData; } + String getNickname() { + return muc.getNickname(); + } + void connect() { new Thread(new ConnectRunner()).start(); } diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java index 8a1c8e4..929ce03 100644 --- a/src/jrummikub/control/network/NetworkControl.java +++ b/src/jrummikub/control/network/NetworkControl.java @@ -61,7 +61,7 @@ public class NetworkControl { view.showGameListPanel(false); settingsControl = new NetworkSettingsControl( - connectionControl, view, new GameSettings()); + connectionControl.getNickname(), view, new GameSettings()); settingsControl.startSettings(); } } diff --git a/src/jrummikub/control/network/NetworkSettingsControl.java b/src/jrummikub/control/network/NetworkSettingsControl.java index b9af560..61fcfa3 100644 --- a/src/jrummikub/control/network/NetworkSettingsControl.java +++ b/src/jrummikub/control/network/NetworkSettingsControl.java @@ -1,54 +1,126 @@ package jrummikub.control.network; +import java.util.ArrayList; import java.util.Arrays; -import java.util.UUID; +import java.util.Collections; +import java.util.List; import jrummikub.control.SettingsControl; import jrummikub.model.GameSettings; +import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; -import jrummikub.view.IGameListPanel.GameData; +import jrummikub.view.ISettingsPanel; import jrummikub.view.IView; /** * Settings for network games, entered by host */ public class NetworkSettingsControl extends SettingsControl { - private GameData gameData = new GameData(UUID.randomUUID(), settings); - private ConnectionControl connectionControl; + private String nickname; /** * Creates a new network settings control * * @param connectionControl - * for gameOffers + * for gameOffers * @param view - * for events and handlers + * for events and handlers * @param settings - * for playerNumber, color... + * for playerNumber, color... */ - public NetworkSettingsControl(ConnectionControl connectionControl, - IView view, GameSettings settings) { - super(view, settings); - this.connectionControl = connectionControl; - /*view.getSettingsPanel().setPlayerTypeChoices( - Arrays.asList(Type.NETWORK, Type.COMPUTER));*/ + public NetworkSettingsControl(String nickname, IView view, + GameSettings settings) { + this.nickname = nickname; + this.view = view; + this.settings = settings; view.getSettingsPanel().enableNetworkMode(true); + view.getSettingsPanel().setPlayerNamesEditable( + Collections.<Boolean> emptyList()); + + addFirstPlayer(); + addPlayer(); + + addListeners(); } @Override - public void startSettings() { - super.startSettings(); + protected void update() { + view.getSettingsPanel().enableAddPlayerButton( + settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); + + checkSettings(); + + List<List<Type>> choices = new ArrayList<List<Type>>(); + List<Boolean> enableRemoveButtons = new ArrayList<Boolean>(); + List<Boolean> playerNamesEditable = new ArrayList<Boolean>(); + + for (int i = 0; i < settings.getPlayerList().size(); i++) { + if (i == 0) { + choices.add(Collections.singletonList(Type.HUMAN)); + enableRemoveButtons.add(false); + } else { + choices.add(Arrays.asList(Type.NETWORK, Type.COMPUTER)); + enableRemoveButtons.add(settings.getPlayerList().size() > 2); + } + + playerNamesEditable + .add(settings.getPlayerList().get(i).getType() == Type.COMPUTER); + } + view.getSettingsPanel().enableRemovePlayerButtons(enableRemoveButtons); + view.getSettingsPanel().setPlayerNamesEditable(playerNamesEditable); + view.getSettingsPanel().setPlayerTypeChoices(choices); + + view.getSettingsPanel().setGameSettings(settings); } @Override - protected void update() { - super.update(); - connectionControl.offerGame(gameData); + protected void setPlayerType(int i, Type type) { + Type oldType = settings.getPlayerList().get(i).getType(); + + settings.getPlayerList().get(i).setType(type); + + if (type == Type.NETWORK) { + settings.getPlayerList().get(i).setName("Offen"); + } else if (oldType != Type.COMPUTER && type == Type.COMPUTER) { + // Find unused player name + int num = 0; + + nameLoop: while (true) { + num++; + String playerName = "Computer " + num; + + for (PlayerSettings player : settings.getPlayerList()) { + if (player.getType() == Type.COMPUTER + && playerName.equals(player.getName())) { + continue nameLoop; + } + } + break; + } + + settings.getPlayerList().get(i).setName("Computer " + num); + } + + update(); + } + + private void addFirstPlayer() { + PlayerSettings playerSettings = new PlayerSettings(nickname, + findUnusedColor()); + settings.getPlayerList().add(playerSettings); } @Override - public void abort() { - super.abort(); - connectionControl.withdrawGame(gameData.getGameID()); + protected void addPlayer() { + if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) { + return; + } + + PlayerSettings playerSettings = new PlayerSettings("Offen", + findUnusedColor()); + playerSettings.setType(Type.NETWORK); + settings.getPlayerList().add(playerSettings); + + update(); } } diff --git a/src/jrummikub/model/PlayerSettings.java b/src/jrummikub/model/PlayerSettings.java index e2fa9ae..b08e7e1 100644 --- a/src/jrummikub/model/PlayerSettings.java +++ b/src/jrummikub/model/PlayerSettings.java @@ -85,7 +85,7 @@ public class PlayerSettings implements Serializable { * @param turnControlType * player's TurnControlFactory type */ - public void setTurnControlType(Type turnControlType) { + public void setType(Type turnControlType) { this.turnControlType = turnControlType; } @@ -94,7 +94,7 @@ public class PlayerSettings implements Serializable { * * @return player's TurnControlFactory type */ - public Type getTurnControlType() { + public Type getType() { return turnControlType; } } diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java index 2b5aa58..f1737a9 100644 --- a/src/jrummikub/view/ISettingsPanel.java +++ b/src/jrummikub/view/ISettingsPanel.java @@ -10,7 +10,6 @@ import jrummikub.model.StoneColor; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; -import jrummikub.util.Pair; /** * The panel for the game setup @@ -40,8 +39,8 @@ public interface ISettingsPanel { public void enableNetworkMode(boolean enable); /** - * The add player event is emitted when the user wants to add a player to - * the player list + * The add player event is emitted when the user wants to add a player to the + * player list * * @return the event */ @@ -88,8 +87,8 @@ public interface ISettingsPanel { public IEvent1<Integer> getChangeInitialMeldThresholdEvent(); /** - * The change StoneSet number event is emitted when the user wants to use - * more or less than 2 StoneSets per color + * The change StoneSet number event is emitted when the user wants to use more + * or less than 2 StoneSets per color * * @return number of SoneSets */ @@ -130,7 +129,7 @@ public interface ISettingsPanel { * Sets an error to display * * @param error - * the kind of error + * the kind of error */ public void setError(SettingsError error); @@ -138,7 +137,7 @@ public interface ISettingsPanel { * Enables or disables the start game button * * @param enable - * specifies if the button is to be enabled or disabled + * specifies if the button is to be enabled or disabled */ public void enableStartGameButton(boolean enable); @@ -146,7 +145,7 @@ public interface ISettingsPanel { * Enables or disables the add player button * * @param enable - * specifies if the button is to be enabled or disabled + * specifies if the button is to be enabled or disabled */ public void enableAddPlayerButton(boolean enable); @@ -154,16 +153,16 @@ public interface ISettingsPanel { * Enables or disables the remove player buttons * * @param enable - * specifies if the buttons are to be enabled or disabled + * specifies if the buttons are to be enabled or disabled */ - public void enableRemovePlayerButtons(boolean enable); + public void enableRemovePlayerButtons(List<Boolean> enable); /** * Sets the game settings to display * * @param gameSettings - * the settings + * the settings */ public void setGameSettings(GameSettings gameSettings); @@ -203,9 +202,13 @@ public interface ISettingsPanel { public IEvent getSetVariantChildrenEvent(); public IEvent getBackEvent(); - + public void setPlayerTypeChoices(List<List<Type>> choices); + public void setPlayerNamesEditable(List<Boolean> editable); + + public IEvent getOfferGameEvent(); + /** * Specifies the different kinds of settings errors that can be displayed */ diff --git a/src/jrummikub/view/impl/GameListPanel.java b/src/jrummikub/view/impl/GameListPanel.java index 99807f8..28c7634 100644 --- a/src/jrummikub/view/impl/GameListPanel.java +++ b/src/jrummikub/view/impl/GameListPanel.java @@ -187,7 +187,7 @@ class GameListPanel extends JPanel implements IGameListPanel { int index, boolean isSelected, boolean cellHasFocus) { String host = "", playerCount = ""; - if (value instanceof GameData) { + if (value instanceof GameData && ((GameData)value).getGameSettings() != null) { GameData gameData = (GameData) value; host = gameData.getHost(); diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index 0292d2c..1d3e1b5 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -16,10 +16,10 @@ import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -60,12 +60,13 @@ import jrummikub.util.Event2; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; -import jrummikub.util.Pair; import jrummikub.view.ISettingsPanel; @SuppressWarnings("serial") class SettingsPanel extends JPanel implements ISettingsPanel { - private List<List<Type>> playerTypeChoices = new ArrayList<List<Type>>(); + private List<List<Type>> playerTypeChoices = Collections.emptyList(); + private List<Boolean> removeButtonsEnabled = Collections.emptyList(); + private List<Boolean> playerNamesEditable = Collections.emptyList(); private JTabbedPane tabbedPane; private JPanel playerSetupPanel; @@ -74,8 +75,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JButton addPlayerButton; private JLabel errorMessageLabel; private JButton startButton; + private JButton offerButton; - private boolean removeButtonsEnabled = true; private List<PlayerSettingsPanel> playerSettingsPanels = new ArrayList<PlayerSettingsPanel>(); private JButton setVariantDefaultButton; @@ -98,6 +99,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event startGameEvent = new Event(); private Event loadGameEvent = new Event(); private Event networkGameEvent = new Event(); + private Event offerGameEvent = new Event(); + private Event backEvent = new Event(); private Event addPlayerEvent = new Event(); private Event1<Integer> removePlayerEvent = new Event1<Integer>(); private Event2<Integer, Color> changePlayerColorEvent = new Event2<Integer, Color>(); @@ -113,13 +116,17 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event1<Boolean> changeNoLimitsEvent = new Event1<Boolean>(); private Event setVariantDefaultEvent = new Event(); private Event setVariantChildrenEvent = new Event(); - private Event backEvent = new Event(); @Override public IEvent getStartGameEvent() { return startGameEvent; } + @Override + public IEvent getOfferGameEvent() { + return offerGameEvent; + } + IEvent getLoadGameEvent() { return loadGameEvent; } @@ -216,37 +223,37 @@ class SettingsPanel extends JPanel implements ISettingsPanel { @Override public void setError(SettingsError error) { switch (error) { - case NO_ERROR: - errorMessageLabel.setText(" "); - break; - case DUPLICATE_PLAYER_NAME_ERROR: - errorMessageLabel - .setText("Jeder Spielername darf nur einmal verwendet werden."); - errorMessageLabel.setForeground(Color.RED); - break; - case NO_PLAYER_NAME_ERROR: - errorMessageLabel.setText("Jeder Spieler muss einen Namen haben."); - errorMessageLabel.setForeground(Color.RED); - break; - case NOT_ENOUGH_STONES_ERROR: - errorMessageLabel - .setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl."); - errorMessageLabel.setForeground(Color.RED); - break; - case NOT_ENOUGH_COLORS_ERROR: - errorMessageLabel - .setText("Es m\u00fcssen mindestens drei Farben ausgew\u00e4hlt werden."); - errorMessageLabel.setForeground(Color.RED); - break; - case COMPUTER_PLAYERS_ONLY_WARNING: - errorMessageLabel.setText("Es gibt keinen menschlichen Spieler."); - errorMessageLabel.setForeground(Color.ORANGE.darker()); - break; - case TOO_HIGH_THRESHOLD_WARNING: - errorMessageLabel - .setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch"); - errorMessageLabel.setForeground(Color.ORANGE.darker()); - break; + case NO_ERROR: + errorMessageLabel.setText(" "); + break; + case DUPLICATE_PLAYER_NAME_ERROR: + errorMessageLabel + .setText("Jeder Spielername darf nur einmal verwendet werden."); + errorMessageLabel.setForeground(Color.RED); + break; + case NO_PLAYER_NAME_ERROR: + errorMessageLabel.setText("Jeder Spieler muss einen Namen haben."); + errorMessageLabel.setForeground(Color.RED); + break; + case NOT_ENOUGH_STONES_ERROR: + errorMessageLabel + .setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl."); + errorMessageLabel.setForeground(Color.RED); + break; + case NOT_ENOUGH_COLORS_ERROR: + errorMessageLabel + .setText("Es m\u00fcssen mindestens drei Farben ausgew\u00e4hlt werden."); + errorMessageLabel.setForeground(Color.RED); + break; + case COMPUTER_PLAYERS_ONLY_WARNING: + errorMessageLabel.setText("Es gibt keinen menschlichen Spieler."); + errorMessageLabel.setForeground(Color.ORANGE.darker()); + break; + case TOO_HIGH_THRESHOLD_WARNING: + errorMessageLabel + .setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch"); + errorMessageLabel.setForeground(Color.ORANGE.darker()); + break; } } @@ -262,17 +269,35 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } @Override - public void enableRemovePlayerButtons(boolean enable) { + public void enableRemovePlayerButtons(List<Boolean> enable) { removeButtonsEnabled = enable; + Iterator<Boolean> it = enable.iterator(); + for (PlayerSettingsPanel psp : playerSettingsPanels) { + if (!it.hasNext()) { + psp.enableRemoveButton(false); + } else { + psp.enableRemoveButton(it.next()); + } + } + } + + @Override + public void setPlayerNamesEditable(List<Boolean> editable) { + playerNamesEditable = editable; + + Iterator<Boolean> it = editable.iterator(); for (PlayerSettingsPanel psp : playerSettingsPanels) { - psp.enableRemoveButton(enable); + if (!it.hasNext()) { + psp.setPlayerNameEditable(true); + } else { + psp.setPlayerNameEditable(it.next()); + } } } @Override public void setGameSettings(GameSettings gameSettings) { - while (playerSettingsPanels.size() > gameSettings.getPlayerList() - .size()) { + while (playerSettingsPanels.size() > gameSettings.getPlayerList().size()) { removePlayerSettingsPanel(); } @@ -281,16 +306,14 @@ class SettingsPanel extends JPanel implements ISettingsPanel { if (i < playerTypeChoices.size()) { choices = playerTypeChoices.get(i); } - updatePlayerSettingsPanel(i, gameSettings.getPlayerList().get(i), - choices); + updatePlayerSettingsPanel(i, gameSettings.getPlayerList().get(i), choices); } - initialMeldThresholdSpinner.setValue(gameSettings - .getInitialMeldThreshold()); + initialMeldThresholdSpinner + .setValue(gameSettings.getInitialMeldThreshold()); stoneSetNumberSpinner.setValue(gameSettings.getStoneSetNumber()); highestValueSpinner.setValue(gameSettings.getHighestValue()); - numberOfStonesDealtSpinner.setValue(gameSettings - .getNumberOfStonesDealt()); + numberOfStonesDealtSpinner.setValue(gameSettings.getNumberOfStonesDealt()); jokerNumberSpinner.setValue(gameSettings.getJokerNumber()); timeSpinner.setValue(gameSettings.getTotalTime()); noLimitsBox.setSelected(gameSettings.isNoLimits()); @@ -315,16 +338,17 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private void addPlayerSettingsPanel(int i, PlayerSettings settings, List<Type> choices) { - PlayerSettingsPanel panel = new PlayerSettingsPanel(i, settings, - choices); + PlayerSettingsPanel panel = new PlayerSettingsPanel(i, settings, choices); playerSettingsPanels.add(panel); playerSettingsViewport.add(panel, playerSettingsViewport.getComponentCount() - 1); + enableRemovePlayerButtons(removeButtonsEnabled); + setPlayerNamesEditable(playerNamesEditable); } private void removePlayerSettingsPanel() { - PlayerSettingsPanel p = playerSettingsPanels - .remove(playerSettingsPanels.size() - 1); + PlayerSettingsPanel p = playerSettingsPanels.remove(playerSettingsPanels + .size() - 1); playerSettingsViewport.remove(p); } @@ -346,8 +370,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { playerSettingsViewport.add(addPlayerPanel); addPlayerButton = new JButton("+"); - addPlayerButton - .setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); + addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); addPlayerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -512,8 +535,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.insets = new Insets(2, 2, 2, 2); final JSpinner spinner = new JSpinner(); spinner.setModel(new SpinnerNumberModel(min, min, max, step)); - spinner.setPreferredSize(new Dimension(60, - spinner.getMinimumSize().height)); + spinner + .setPreferredSize(new Dimension(60, spinner.getMinimumSize().height)); spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { @@ -535,7 +558,6 @@ class SettingsPanel extends JPanel implements ISettingsPanel { optionsPanel.add(label, c); } - // TODO wiederfinden SettingsPanel() { setLayout(new GridBagLayout()); @@ -566,15 +588,16 @@ class SettingsPanel extends JPanel implements ISettingsPanel { add(buttonPanel, c); startButton = createButton("Spiel starten", startGameEvent); - loadButton = createButton("Spiel laden...", loadGameEvent); networkButton = createButton("Netzwerkspiel...", networkGameEvent); + + offerButton = createButton("Spiel anbieten", offerGameEvent); backButton = createButton("Zur\u00fcck", backEvent); addDefaultButtons(); - setBorder(new CompoundBorder(new LineBorder(Color.BLACK), - new EmptyBorder(10, 10, 10, 10))); + setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder( + 10, 10, 10, 10))); } void resetTabbedPane() { @@ -609,7 +632,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.weightx = 1; c.weighty = 1; - buttonPanel.add(startButton, c); + buttonPanel.add(offerButton, c); c.weightx = 0; buttonPanel.add(Box.createHorizontalStrut(10), c); c.weightx = 1; @@ -643,19 +666,23 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JTextField nameField; private JButton removeButton; private JComboBox playerType; + private List<Type> playerTypeChoices; public void setSettings(PlayerSettings settings) { if (!nameField.getText().equals(settings.getName())) { nameField.setText(settings.getName()); } - // playerType.setSelectedIndex(0); - int index = Arrays.binarySearch(Type.values(), - settings.getTurnControlType()); - if (index != playerType.getSelectedIndex()) { + int index = playerTypeChoices.indexOf(settings.getType()); + if (index != playerType.getSelectedIndex() && index >= 0 + && index < playerType.getModel().getSize()) { playerType.setSelectedIndex(index); } - colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(), - 16, 2)); + colorButton + .setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2)); + } + + public void setPlayerNameEditable(boolean editable) { + nameField.setEditable(editable); } public void enableRemoveButton(boolean enable) { @@ -691,18 +718,20 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } public void setChoices(List<Type> playerTypeChoices) { + this.playerTypeChoices = playerTypeChoices; + Vector<String> choices = new Vector<String>(); for (Type t : playerTypeChoices) { switch (t) { - case COMPUTER: - choices.add("Computer"); - break; - case HUMAN: - choices.add("Mensch"); - break; - case NETWORK: - choices.add("Netzwerk"); - break; + case COMPUTER: + choices.add("Computer"); + break; + case HUMAN: + choices.add("Mensch"); + break; + case NETWORK: + choices.add("Netzwerk"); + break; } } playerType.setModel(new DefaultComboBoxModel(choices)); @@ -723,8 +752,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.gridy = 0; add(colorButton, c); - nameField.getDocument().addDocumentListener( - new NameChangeListener()); + nameField.getDocument().addDocumentListener(new NameChangeListener()); c2.gridy = 0; c2.weightx = 1; c2.fill = GridBagConstraints.HORIZONTAL; @@ -737,6 +765,10 @@ class SettingsPanel extends JPanel implements ISettingsPanel { changeType(); } }); + playerType.setPreferredSize(new Dimension(120, playerType + .getPreferredSize().height)); + playerType.setMinimumSize(new Dimension(120, + playerType.getMinimumSize().height)); add(playerType, c); removeButton = new JButton("\u00d7"); @@ -747,7 +779,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { remove(); } }); - removeButton.setEnabled(removeButtonsEnabled); + add(removeButton, c); } @@ -755,9 +787,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { public Dimension getPreferredSize() { Dimension preferredSize = super.getPreferredSize(); for (Component component : this.getComponents()) { - preferredSize.setSize( - preferredSize.width, - Math.max(preferredSize.height, + preferredSize + .setSize(preferredSize.width, Math.max(preferredSize.height, component.getPreferredSize().height)); } return preferredSize; @@ -767,9 +798,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { public Dimension getMaximumSize() { Dimension preferredSize = super.getPreferredSize(); for (Component component : this.getComponents()) { - preferredSize.setSize( - Integer.MAX_VALUE, - Math.max(preferredSize.height, + preferredSize + .setSize(Integer.MAX_VALUE, Math.max(preferredSize.height, component.getPreferredSize().height)); } return preferredSize; @@ -785,7 +815,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private void changeType() { changePlayerTypeEvent.emit(playerNumber, - Type.values()[playerType.getSelectedIndex()]); + playerTypeChoices.get(playerType.getSelectedIndex())); } private void remove() { |