From d223eaaa9839dc974b5d51cbd004f92818e2ada4 Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Mon, 13 Jun 2011 23:57:02 +0200 Subject: es gibt eine AbstractSettingsControl git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@427 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/SettingsControl.java | 316 +-------------------- .../control/network/NetworkSettingsControl.java | 8 +- 2 files changed, 12 insertions(+), 312 deletions(-) (limited to 'src/jrummikub') diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index 32e80fe..6d736a6 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -1,36 +1,26 @@ 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; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; -import jrummikub.model.StoneColor; import jrummikub.util.Connection; import jrummikub.util.Event1; import jrummikub.util.IEvent1; import jrummikub.util.IListener; -import jrummikub.util.IListener1; -import jrummikub.util.IListener2; import jrummikub.view.ISettingsPanel; import jrummikub.view.IView; /** * The settings control controls the settings panel */ -public class SettingsControl { - protected IView view; - protected Event1 startGameEvent = new Event1(); - private List connections = new ArrayList(); - - protected GameSettings settings; - +public class SettingsControl extends AbstractSettingsControl { + private Event1 startGameEvent = new Event1(); + /** * Create a new settings control * @@ -40,8 +30,7 @@ public class SettingsControl { * initial game settings */ public SettingsControl(IView view, GameSettings settings) { - this.view = view; - this.settings = settings; + super(view, settings); view.getSettingsPanel().enableNetworkMode(false); view.getSettingsPanel().setPlayerNamesEditable( Collections. emptyList()); @@ -60,15 +49,6 @@ public class SettingsControl { })); } - protected SettingsControl() { - } - - protected void addListeners() { - addPlayerSettingsListeners(); - - addOptionListeners1(); - addOptionListeners2(); - } /** * the start game event is emitted when the user wants to start a game and @@ -79,154 +59,8 @@ public class SettingsControl { public IEvent1 getStartGameEvent() { return startGameEvent; } - - /** - * Start the operation of the settings control - */ - public void startSettings() { - view.showSettingsPanel(true); - } - - private void addOptionListeners1() { - connections.add(view.getSettingsPanel() - .getChangeInitialMeldThresholdEvent() - .add(new IListener1() { - @Override - public void handle(Integer value) { - settings.setInitialMeldThreshold(value); - update(); - } - })); - - connections.add(view.getSettingsPanel().getChangeJokerNumberEvent() - .add(new IListener1() { - @Override - public void handle(Integer value) { - settings.setJokerNumber(value); - update(); - } - })); - - connections.add(view.getSettingsPanel().getChangeStoneSetNumberEvent() - .add(new IListener1() { - @Override - public void handle(Integer value) { - settings.setStoneSetNumber(value); - update(); - } - })); - connections.add(view.getSettingsPanel() - .getChangeNumberOfStonesDealtEvent() - .add(new IListener1() { - @Override - public void handle(Integer value) { - settings.setNumberOfStonesDealt(value); - update(); - } - })); - } - - private void addOptionListeners2() { - connections.add(view.getSettingsPanel().getChangeHighestValueEvent() - .add(new IListener1() { - @Override - public void handle(Integer value) { - settings.setHighestValue(value); - update(); - } - })); - connections.add(view.getSettingsPanel().getChangeTimeEvent() - .add(new IListener1() { - @Override - public void handle(Integer value) { - settings.setTotalTime(value); - update(); - } - })); - connections.add(view.getSettingsPanel().getChangeStoneColorsEvent() - .add(new IListener1>() { - @Override - public void handle(Set value) { - settings.setStoneColors(new HashSet(value)); - update(); - } - })); - - connections.add(view.getSettingsPanel().getChangeNoLimitsEvent() - .add(new IListener1() { - @Override - public void handle(Boolean value) { - settings.setNoLimits(value); - update(); - } - })); - addVariantListeners(); - } - - /** - * Add event listeners for variants, e.g. standard or children - */ - public void addVariantListeners() { - connections.add(view.getSettingsPanel().getSetVariantDefaultEvent() - .add(new IListener() { - @Override - public void handle() { - settings.reset(); - update(); - } - })); - connections.add(view.getSettingsPanel().getSetVariantChildrenEvent() - .add(new IListener() { - @Override - public void handle() { - settings.reset(); - settings.setHighestValue(10); - settings.setJokerNumber(6); - settings.setTotalTime(120); - settings.setInitialMeldThreshold(20); - update(); - } - })); - } - - private void addPlayerSettingsListeners() { - connections.add(view.getSettingsPanel().getAddPlayerEvent() - .add(new IListener() { - @Override - public void handle() { - addPlayer(); - } - })); - connections.add(view.getSettingsPanel().getRemovePlayerEvent() - .add(new IListener1() { - @Override - public void handle(Integer i) { - removePlayer(i); - } - })); - connections.add(view.getSettingsPanel().getChangePlayerColorEvent() - .add(new IListener2() { - @Override - public void handle(Integer i, Color color) { - setPlayerColor(i, color); - } - })); - connections.add(view.getSettingsPanel().getChangePlayerNameEvent() - .add(new IListener2() { - @Override - public void handle(Integer i, String name) { - setPlayerName(i, name); - } - })); - connections.add(view.getSettingsPanel().getChangePlayerTypeEvent() - .add(new IListener2() { - @Override - public void handle(Integer i, Type type) { - setPlayerType(i, type); - } - })); - } - + + @Override protected void addPlayer() { if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) { return; @@ -253,53 +87,13 @@ public class SettingsControl { update(); } - protected Color findUnusedColor() { - Color color = null; - colorLoop: for (Color c : ISettingsPanel.PLAYER_COLORS) { - color = c; - for (PlayerSettings player : settings.getPlayerList()) { - if (c == player.getColor()) { - continue colorLoop; - } - } - break; - } - return color; - } - - private void removePlayer(int i) { - settings.getPlayerList().remove(i); - update(); - } - - private void setPlayerColor(int i, Color color) { - PlayerSettings player = settings.getPlayerList().get(i); - - if (player.getColor() == color) { - return; - } - - for (PlayerSettings other : settings.getPlayerList()) { - if (other.getColor() == color) { - other.setColor(player.getColor()); - break; - } - } - - player.setColor(color); - update(); - } - - private void setPlayerName(int i, String name) { - settings.getPlayerList().get(i).setName(name); - update(); - } - + @Override protected void setPlayerType(int i, Type type) { settings.getPlayerList().get(i).setType(type); update(); } + @Override protected void update() { boolean enableRemoveButtons = settings.getPlayerList().size() > 2; view.getSettingsPanel().enableRemovePlayerButtons( @@ -319,100 +113,6 @@ public class SettingsControl { view.getSettingsPanel().setGameSettings(settings); } - protected boolean checkSettings() { - if (!checkName()) { - return false; - } - if (!checkErrors()) { - return false; - } - - view.getSettingsPanel().setError(ISettingsPanel.SettingsError.NO_ERROR); - view.getSettingsPanel().enableStartGameButton(true); - - checkWarnings(); - - return true; - } - - private boolean checkName() { - for (PlayerSettings player : settings.getPlayerList()) { - if (player.getName().isEmpty()) { - view.getSettingsPanel().setError( - ISettingsPanel.SettingsError.NO_PLAYER_NAME_ERROR); - view.getSettingsPanel().enableStartGameButton(false); - return false; - } - } - for (int i = 0; i < settings.getPlayerList().size(); ++i) { - 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) { - 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; - } - } - } - return true; - } - - private boolean checkErrors() { - int totalStonesDealt = settings.getNumberOfStonesDealt() - * settings.getPlayerList().size(); - int totalStones = settings.getHighestValue() - * settings.getStoneSetNumber() - * settings.getStoneColors().size() + settings.getJokerNumber(); - - if (totalStones <= totalStonesDealt) { - view.getSettingsPanel().setError( - ISettingsPanel.SettingsError.NOT_ENOUGH_STONES_ERROR); - view.getSettingsPanel().enableStartGameButton(false); - return false; - } - - if (settings.getStoneColors().size() < 3) { - view.getSettingsPanel().setError( - ISettingsPanel.SettingsError.NOT_ENOUGH_COLORS_ERROR); - view.getSettingsPanel().enableStartGameButton(false); - return false; - } - - return true; - } - - private void checkWarnings() { - if (settings.getInitialMeldThreshold() >= 100) { - view.getSettingsPanel().setError( - ISettingsPanel.SettingsError.TOO_HIGH_THRESHOLD_WARNING); - return; - } - - boolean humanPlayerFound = false; - for (PlayerSettings player : settings.getPlayerList()) { - if (player.getType() == Type.HUMAN) { - humanPlayerFound = true; - break; - } - } - - if (!humanPlayerFound) { - view.getSettingsPanel().setError( - ISettingsPanel.SettingsError.COMPUTER_PLAYERS_ONLY_WARNING); - return; - } - } - private void startGame() { if (!checkSettings()) { return; diff --git a/src/jrummikub/control/network/NetworkSettingsControl.java b/src/jrummikub/control/network/NetworkSettingsControl.java index 3c8dc42..8afe49e 100644 --- a/src/jrummikub/control/network/NetworkSettingsControl.java +++ b/src/jrummikub/control/network/NetworkSettingsControl.java @@ -5,7 +5,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import jrummikub.control.SettingsControl; +import jrummikub.control.AbstractSettingsControl; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; @@ -15,7 +15,7 @@ import jrummikub.view.IView; /** * Settings for network games, entered by host */ -public class NetworkSettingsControl extends SettingsControl { +public class NetworkSettingsControl extends AbstractSettingsControl { private String nickname; /** @@ -30,9 +30,9 @@ public class NetworkSettingsControl extends SettingsControl { */ public NetworkSettingsControl(String nickname, IView view, GameSettings settings) { + super(view, settings); this.nickname = nickname; - this.view = view; - this.settings = settings; + view.getSettingsPanel().enableNetworkMode(true); view.getSettingsPanel().setPlayerNamesEditable( Collections. emptyList()); -- cgit v1.2.3