diff options
author | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-05-29 21:49:03 +0200 |
---|---|---|
committer | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-05-29 21:49:03 +0200 |
commit | 43ace4c18a5aeb0d466ff9180614ba07dc207f33 (patch) | |
tree | 9a6497b0ae1c6036dc8abf44cbad96bd352ac480 /src | |
parent | 08e9b28f3b679a8f511ca40f5b8b6aa5ec8aa188 (diff) | |
download | JRummikub-43ace4c18a5aeb0d466ff9180614ba07dc207f33.tar JRummikub-43ace4c18a5aeb0d466ff9180614ba07dc207f33.zip |
Added player type selection to the settings panel
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@315 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src')
-rw-r--r-- | src/jrummikub/control/RoundControl.java | 5 | ||||
-rw-r--r-- | src/jrummikub/control/turn/TurnControlFactory.java | 24 | ||||
-rw-r--r-- | src/jrummikub/model/PlayerSettings.java | 27 | ||||
-rw-r--r-- | src/jrummikub/view/impl/SettingsPanel.java | 177 |
4 files changed, 150 insertions, 83 deletions
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index fbe5873..bcef40e 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Set; import jrummikub.control.turn.ITurnControl; +import jrummikub.control.turn.TurnControlFactory; import jrummikub.model.Hand; import jrummikub.model.IHand; import jrummikub.model.IPlayer; @@ -94,8 +95,8 @@ public class RoundControl { view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal); - ITurnControl turnControl = roundState.getActivePlayer() - .getPlayerSettings().getTurnControlFactory().create(); + ITurnControl turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer() + .getPlayerSettings().getTurnControlType()).create(); turnControl.setup(roundState.getActivePlayer().getHand(), clonedTable, view, inspectOnly, mayRedeal); turnControl.getEndOfTurnEvent().add(new IListener() { diff --git a/src/jrummikub/control/turn/TurnControlFactory.java b/src/jrummikub/control/turn/TurnControlFactory.java index 48c028e..ecfc5ff 100644 --- a/src/jrummikub/control/turn/TurnControlFactory.java +++ b/src/jrummikub/control/turn/TurnControlFactory.java @@ -4,6 +4,26 @@ import jrummikub.model.IHand; import jrummikub.model.ITable; import jrummikub.view.IView; -public interface TurnControlFactory { - public ITurnControl create(); +public abstract class TurnControlFactory { + /** + * Type of turn control. + */ + public enum Type { + /** */ + HUMAN, + /** */ + COMPUTER + }; + + public abstract ITurnControl create(); + + static public TurnControlFactory getFactory(Type type) { + switch(type) { + case HUMAN: + return HumanTurnControl.getFactory(); + case COMPUTER: + return BaseAIControl.getFactory(); + } + return null; + } } diff --git a/src/jrummikub/model/PlayerSettings.java b/src/jrummikub/model/PlayerSettings.java index 217b1fc..cc1c6e4 100644 --- a/src/jrummikub/model/PlayerSettings.java +++ b/src/jrummikub/model/PlayerSettings.java @@ -2,7 +2,6 @@ package jrummikub.model; import java.awt.Color; -import jrummikub.control.turn.HumanTurnControl; import jrummikub.control.turn.TurnControlFactory; /** @@ -11,7 +10,7 @@ import jrummikub.control.turn.TurnControlFactory; public class PlayerSettings { private String name; private Color color; - private TurnControlFactory turnControlFactory; + private TurnControlFactory.Type turnControlType; /** * Create a new player @@ -27,9 +26,9 @@ public class PlayerSettings { TurnControlFactory turnControlFactory) { this.name = name; this.color = color; - this.turnControlFactory = turnControlFactory; + this.turnControlType = TurnControlFactory.Type.HUMAN; } - + /** * Create a new human player * @@ -41,7 +40,7 @@ public class PlayerSettings { public PlayerSettings(String name, Color color) { this.name = name; this.color = color; - this.turnControlFactory = HumanTurnControl.getFactory(); + this.turnControlType = TurnControlFactory.Type.HUMAN; } /** @@ -83,21 +82,21 @@ public class PlayerSettings { } /** - * Set the player's TurnControlFactory + * Set the player's TurnControlFactory type * - * @param turnControlFactory - * player's TurnControlFactory + * @param turnControlType + * player's TurnControlFactory type */ - public void setTurnControlFactory(TurnControlFactory turnControlFactory) { - this.turnControlFactory = turnControlFactory; + public void setTurnControlType(TurnControlFactory.Type turnControlType) { + this.turnControlType = turnControlType; } /** - * Get the player's TurnControlFactory + * Get the player's TurnControlFactory type * - * @return player's TurnControlFactory + * @return player's TurnControlFactory type */ - public TurnControlFactory getTurnControlFactory() { - return turnControlFactory; + public TurnControlFactory.Type getTurnControlType() { + return turnControlType; } } diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index 15c6ee4..a96c5a7 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -2,6 +2,7 @@ package jrummikub.view.impl; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; @@ -14,12 +15,14 @@ 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.List; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.Icon; import javax.swing.JButton; +import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; @@ -37,6 +40,7 @@ import javax.swing.event.ChangeListener; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import jrummikub.control.turn.TurnControlFactory; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.util.Event; @@ -67,6 +71,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event1<Integer> removePlayerEvent = new Event1<Integer>(); private Event2<Integer, Color> changePlayerColorEvent = new Event2<Integer, Color>(); private Event2<Integer, String> changePlayerNameEvent = new Event2<Integer, String>(); + private Event2<Integer, TurnControlFactory.Type> changePlayerTypeEvent = new Event2<Integer, TurnControlFactory.Type>(); private Event1<Integer> changeInitialMeldThresholdEvent = new Event1<Integer>(); private Event1<Integer> changeJokerNumberEvent = new Event1<Integer>(); @@ -208,60 +213,55 @@ class SettingsPanel extends JPanel implements ISettingsPanel { GridBagLayout layout = new GridBagLayout(); optionsPanel.setLayout(layout); - GridBagConstraints labelC = new GridBagConstraints(); - GridBagConstraints optionC = new GridBagConstraints(); - - labelC.anchor = GridBagConstraints.EAST; - labelC.gridx = 0; - labelC.insets = new Insets(4, 4, 4, 4); - optionC.anchor = GridBagConstraints.WEST; - optionC.gridx = 1; - optionC.insets = new Insets(2, 2, 2, 2); - - JLabel label = new JLabel("Auslegeschranke:"); - labelC.gridy = 0; - optionsPanel.add(label, labelC); - - initialMeldThresholdSpinner = new JSpinner(); - initialMeldThresholdSpinner.setModel(new SpinnerNumberModel(1, 1, 999, - 1)); - initialMeldThresholdSpinner.setPreferredSize(new Dimension(60, - initialMeldThresholdSpinner.getMinimumSize().height)); - initialMeldThresholdSpinner.addChangeListener(new ChangeListener() { - @Override - public void stateChanged(ChangeEvent e) { - changeInitialMeldThresholdEvent - .emit((Integer) initialMeldThresholdSpinner.getValue()); - } - }); - optionC.gridy = 0; - optionsPanel.add(initialMeldThresholdSpinner, optionC); + makeOptionLabel(0, "Auslegeschranke:"); + initialMeldThresholdSpinner = makeOptionSpinner(0, 1, 999, 1, + changeInitialMeldThresholdEvent); + + makeOptionLabel(1, "Jokeranzahl:"); + jokerNumberSpinner = makeOptionSpinner(1, 1, 999, 1, + changeJokerNumberEvent); + + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 1; + c.gridy = 2; + c.fill = GridBagConstraints.BOTH; + c.weightx = 1; + c.weighty = 1; + optionsPanel.add(makeFiller(), c); - label = new JLabel("Jokeranzahl:"); - labelC.gridx = 0; - labelC.gridy = 1; - optionsPanel.add(label, labelC); + // TODO Knöpfe für Dinge + } - jokerNumberSpinner = new JSpinner(); - jokerNumberSpinner.setModel(new SpinnerNumberModel(0, 0, 999, 1)); - jokerNumberSpinner.addChangeListener(new ChangeListener() { + private JSpinner makeOptionSpinner(int row, int min, int max, int step, + final Event1<Integer> targetEvent) { + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.WEST; + c.gridx = 1; + c.gridy = row; + 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.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - changeJokerNumberEvent.emit((Integer) jokerNumberSpinner - .getValue()); + targetEvent.emit((Integer) spinner.getValue()); } }); - optionC.gridy = 1; - optionsPanel.add(jokerNumberSpinner, optionC); + optionsPanel.add(spinner, c); + return spinner; + } - labelC.gridx = 1; - labelC.gridy = 2; - labelC.fill = GridBagConstraints.BOTH; - labelC.weightx = 1; - labelC.weighty = 1; - optionsPanel.add(makeFiller(), labelC); + private void makeOptionLabel(int row, String name) { + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.EAST; + c.gridx = 0; + c.gridy = row; + c.insets = new Insets(4, 4, 4, 4); - // TODO Knöpfe für Dinge + JLabel label = new JLabel(name); + optionsPanel.add(label, c); } SettingsPanel() { @@ -305,11 +305,16 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JButton colorButton; private JTextField nameField; private JButton removeButton; + private JComboBox playerType; public void setSettings(PlayerSettings settings) { if (!nameField.getText().equals(settings.getName())) { nameField.setText(settings.getName()); } + // playerType.setSelectedIndex(0); + playerType.setSelectedIndex(Arrays.binarySearch( + TurnControlFactory.Type.values(), + settings.getTurnControlType())); colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2)); @@ -319,41 +324,64 @@ class SettingsPanel extends JPanel implements ISettingsPanel { removeButton.setEnabled(enable); } + class NameChangeListener implements DocumentListener { + @Override + public void insertUpdate(DocumentEvent e) { + changeName(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + changeName(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + changeName(); + } + } + public PlayerSettingsPanel(int playerNumber, PlayerSettings settings) { - setLayout(new BorderLayout()); + setLayout(new GridBagLayout()); this.playerNumber = playerNumber; + createControls(settings); + setSettings(settings); + } + + private void createControls(PlayerSettings settings) { + GridBagConstraints c = new GridBagConstraints(); + GridBagConstraints c2 = new GridBagConstraints(); colorButton = new JButton(); nameField = new JTextField(settings.getName()); - setSettings(settings); - colorButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new ColorMenu(); } }); - add(colorButton, BorderLayout.WEST); + c.gridy = 0; + add(colorButton, c); - nameField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void insertUpdate(DocumentEvent e) { - changeName(); - } + nameField.getDocument().addDocumentListener( + new NameChangeListener()); + c2.gridy = 0; + c2.weightx = 1; + c2.fill = GridBagConstraints.HORIZONTAL; + add(nameField, c2); - @Override - public void removeUpdate(DocumentEvent e) { - changeName(); - } + String[] choices = { "Mensch", "Computer" }; + playerType = new JComboBox(choices); + playerType.addActionListener(new ActionListener() { @Override - public void changedUpdate(DocumentEvent e) { - changeName(); + public void actionPerformed(ActionEvent e) { + changeType(); } }); - add(nameField); + add(playerType, c); removeButton = new JButton("\u00d7"); removeButton.setFont(removeButton.getFont().deriveFont(Font.BOLD)); @@ -364,17 +392,31 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } }); removeButton.setEnabled(removeButtonsEnabled); - add(removeButton, BorderLayout.EAST); + add(removeButton, c); } @Override public Dimension getPreferredSize() { - return getComponent(1).getPreferredSize(); + Dimension preferredSize = super.getPreferredSize(); + for (Component component : this.getComponents()) { + preferredSize.setSize( + preferredSize.width, + Math.max(preferredSize.height, + component.getPreferredSize().height)); + } + return preferredSize; } @Override public Dimension getMaximumSize() { - return new Dimension(Integer.MAX_VALUE, getPreferredSize().height); + Dimension preferredSize = super.getPreferredSize(); + for (Component component : this.getComponents()) { + preferredSize.setSize( + Integer.MAX_VALUE, + Math.max(preferredSize.height, + component.getPreferredSize().height)); + } + return preferredSize; } private void changeName() { @@ -385,6 +427,11 @@ class SettingsPanel extends JPanel implements ISettingsPanel { changePlayerColorEvent.emit(playerNumber, c); } + private void changeType() { + changePlayerTypeEvent.emit(playerNumber, TurnControlFactory.Type + .values()[playerType.getSelectedIndex()]); + } + private void remove() { removePlayerEvent.emit(playerNumber); } |