summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-05-26 01:33:17 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-05-26 01:33:17 +0200
commit04df1a24d830dab98bd9ed7f623e5499ead0c424 (patch)
treedf7e2f34ed2085925634414096a23abadbaed5f9
parent10cd2cb9024c457c99e25be25ead4071407073fd (diff)
downloadJRummikub-04df1a24d830dab98bd9ed7f623e5499ead0c424.tar
JRummikub-04df1a24d830dab98bd9ed7f623e5499ead0c424.zip
Make player name setup work
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@281 72836036-5685-4462-b002-a69064685172
-rw-r--r--mock/jrummikub/view/MockView.java6
-rw-r--r--src/jrummikub/model/PlayerSettings.java20
-rw-r--r--src/jrummikub/view/ISettingsPanel.java24
-rw-r--r--src/jrummikub/view/IView.java7
-rw-r--r--src/jrummikub/view/impl/ImageUtil.java4
-rw-r--r--src/jrummikub/view/impl/SettingsPanel.java138
-rw-r--r--src/jrummikub/view/impl/View.java6
7 files changed, 182 insertions, 23 deletions
diff --git a/mock/jrummikub/view/MockView.java b/mock/jrummikub/view/MockView.java
index 95996a4..1536a5b 100644
--- a/mock/jrummikub/view/MockView.java
+++ b/mock/jrummikub/view/MockView.java
@@ -84,4 +84,10 @@ public class MockView implements IView {
return newRoundEvent;
}
+ @Override
+ public ISettingsPanel getSettingsPanel() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
} \ No newline at end of file
diff --git a/src/jrummikub/model/PlayerSettings.java b/src/jrummikub/model/PlayerSettings.java
index 87c2a27..5ba01b5 100644
--- a/src/jrummikub/model/PlayerSettings.java
+++ b/src/jrummikub/model/PlayerSettings.java
@@ -39,4 +39,24 @@ public class PlayerSettings {
public String getName() {
return name;
}
+
+ /**
+ * Sets the player's color
+ *
+ * @param color
+ * the new color
+ */
+ public void setColor(Color color) {
+ this.color = color;
+ }
+
+ /**
+ * Sets the player's name
+ *
+ * @param name
+ * the new name
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
}
diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java
new file mode 100644
index 0000000..4437f58
--- /dev/null
+++ b/src/jrummikub/view/ISettingsPanel.java
@@ -0,0 +1,24 @@
+package jrummikub.view;
+
+import jrummikub.model.GameSettings;
+import jrummikub.util.IEvent1;
+
+/**
+ * The panel for the game setup
+ */
+public interface ISettingsPanel {
+ /**
+ * The settings change event is emitted whenever the user has changed a game
+ * settings without starting the game
+ *
+ * @return the event
+ */
+ public IEvent1<GameSettings> getSettingsChangeEvent();
+
+ /**
+ * the start game event is emitted when the user wants to start the game
+ *
+ * @return the event
+ */
+ public IEvent1<GameSettings> getStartGameEvent();
+} \ No newline at end of file
diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java
index 8514c21..ec154be 100644
--- a/src/jrummikub/view/IView.java
+++ b/src/jrummikub/view/IView.java
@@ -10,6 +10,13 @@ import jrummikub.util.IEvent;
*/
public interface IView {
/**
+ * Returns the settings panel
+ *
+ * @return the settings panel
+ */
+ public ISettingsPanel getSettingsPanel();
+
+ /**
* Returns the table
*
* @return the table
diff --git a/src/jrummikub/view/impl/ImageUtil.java b/src/jrummikub/view/impl/ImageUtil.java
index 78f825b..0254343 100644
--- a/src/jrummikub/view/impl/ImageUtil.java
+++ b/src/jrummikub/view/impl/ImageUtil.java
@@ -10,7 +10,7 @@ class ImageUtil {
private ImageUtil() {
}
- static ImageIcon createColorIcon(Color c, int size) {
+ static ImageIcon createColorIcon(Color c, int size, int border) {
BufferedImage image = new BufferedImage(size, size,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
@@ -19,7 +19,7 @@ class ImageUtil {
g.fillRect(0, 0, size, size);
g.setColor(c);
- g.fillRect(1, 1, size - 2, size - 2);
+ g.fillRect(border, border, size - 2*border, size - 2*border);
return new ImageIcon(image);
}
diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java
index 9aba78c..fa00a0a 100644
--- a/src/jrummikub/view/impl/SettingsPanel.java
+++ b/src/jrummikub/view/impl/SettingsPanel.java
@@ -3,12 +3,17 @@ package jrummikub.view.impl;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
-import java.awt.event.ComponentAdapter;
-import java.awt.event.ComponentEvent;
+import java.awt.FlowLayout;
+import java.awt.Font;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
+import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
@@ -16,20 +21,45 @@ import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+
+import jrummikub.model.GameSettings;
+import jrummikub.model.PlayerSettings;
+import jrummikub.util.Event1;
+import jrummikub.util.IEvent1;
+import jrummikub.view.ISettingsPanel;
@SuppressWarnings("serial")
-class SettingsPanel extends JPanel {
+class SettingsPanel extends JPanel implements ISettingsPanel {
private JPanel playerSetupPanel;
private JPanel playerSettingsViewport;
private JPanel ruleSetupPanel;
+ private GameSettings gameSettings = new GameSettings();
+
private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();
+ private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>();
+ private Event1<GameSettings> startGameEvent = new Event1<GameSettings>();
+
+ @Override
+ public IEvent1<GameSettings> getSettingsChangeEvent() {
+ return settingsChangeEvent;
+ }
+
+ @Override
+ public IEvent1<GameSettings> getStartGameEvent() {
+ return startGameEvent;
+ }
+
private void addPlayerSettings() {
PlayerSettingsPanel panel = new PlayerSettingsPanel();
playerSettingsPanels.add(panel);
playerSettingsViewport.add(panel,
playerSettingsViewport.getComponentCount() - 1);
+
+ settingsChangeEvent.emit(gameSettings);
}
private void createPlayerSetupPanel() {
@@ -45,7 +75,20 @@ class SettingsPanel extends JPanel {
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
playerSetupPanel.add(scrollPane);
- playerSettingsViewport.add(new JButton("Neuer Spieler"));
+ JPanel addPlayerPanel = new JPanel();
+ addPlayerPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 2));
+ playerSettingsViewport.add(addPlayerPanel);
+
+ JButton addPlayerButton = new JButton("+");
+ addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD));
+ addPlayerButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ addPlayerSettings();
+ playerSettingsViewport.revalidate();
+ }
+ });
+ addPlayerPanel.add(addPlayerButton);
addPlayerSettings();
addPlayerSettings();
@@ -56,7 +99,7 @@ class SettingsPanel extends JPanel {
}
SettingsPanel() {
- setLayout(new BorderLayout());
+ setLayout(new GridBagLayout());
final JTabbedPane tabbedPane = new JTabbedPane();
@@ -66,34 +109,87 @@ class SettingsPanel extends JPanel {
createRuleSetupPanel();
tabbedPane.addTab("Regeln", ruleSetupPanel);
- add(tabbedPane);
+ GridBagConstraints c = new GridBagConstraints();
+ c.fill = GridBagConstraints.BOTH;
+ c.gridwidth = GridBagConstraints.REMAINDER;
+ c.weightx = 1;
+ c.weighty = 1;
+ add(tabbedPane, c);
+
+ JLabel errorMessageLabel = new JLabel("Fehler: Implementierung fehlt!");
+ errorMessageLabel.setForeground(Color.RED);
+ c.weighty = 0;
+ add(errorMessageLabel, c);
JButton startButton = new JButton("Spiel starten");
- add(startButton, BorderLayout.SOUTH);
+ startButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ startGameEvent.emit(gameSettings);
+ }
+ });
+ add(startButton, c);
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(
10, 10, 10, 10)));
+ }
- addComponentListener(new ComponentAdapter() {
- @Override
- public void componentResized(ComponentEvent e) {
- // Insets insets = getInsets();
- // int x = insets.left, y = insets.top, width = getWidth() - insets.left
- // - insets.right, height = getHeight() - insets.top - insets.bottom;
+ private class PlayerSettingsPanel extends JPanel {
+ private PlayerSettings settings = new PlayerSettings("Player", Color.RED);
- // tabbedPane.setBounds(x, y, width, height);
- }
- });
- }
+ private void remove() {
+ playerSettingsPanels.remove(PlayerSettingsPanel.this);
+ playerSettingsViewport.remove(PlayerSettingsPanel.this);
+ playerSettingsViewport.revalidate();
+
+ gameSettings.getPlayerList().remove(settings);
+ settingsChangeEvent.emit(gameSettings);
+ }
- private static class PlayerSettingsPanel extends JPanel {
PlayerSettingsPanel() {
setLayout(new BorderLayout());
- add(new JButton(ImageUtil.createColorIcon(Color.RED, 16)),
+ add(new JButton(ImageUtil.createColorIcon(settings.getColor(), 16, 2)),
BorderLayout.WEST);
- add(new JTextField());
- add(new JButton("\u2716"), BorderLayout.EAST);
+ final JTextField nameField = new JTextField(settings.getName());
+ nameField.getDocument().addDocumentListener(new DocumentListener() {
+ private void update() {
+ if (nameField.getText() == settings.getName()) {
+ return;
+ }
+
+ settings.setName(nameField.getText());
+ settingsChangeEvent.emit(gameSettings);
+ }
+
+ @Override
+ public void insertUpdate(DocumentEvent e) {
+ update();
+ }
+
+ @Override
+ public void removeUpdate(DocumentEvent e) {
+ update();
+ }
+
+ @Override
+ public void changedUpdate(DocumentEvent e) {
+ update();
+ }
+ });
+ add(nameField);
+
+ JButton removeButton = new JButton("\u00d7");
+ removeButton.setFont(removeButton.getFont().deriveFont(Font.BOLD));
+ removeButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ remove();
+ }
+ });
+ add(removeButton, BorderLayout.EAST);
+
+ gameSettings.getPlayerList().add(settings);
}
@Override
diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java
index 8c6e3b4..3c9dbe9 100644
--- a/src/jrummikub/view/impl/View.java
+++ b/src/jrummikub/view/impl/View.java
@@ -15,6 +15,7 @@ import jrummikub.model.Stone;
import jrummikub.util.IEvent;
import jrummikub.view.IHandPanel;
import jrummikub.view.IPlayerPanel;
+import jrummikub.view.ISettingsPanel;
import jrummikub.view.ITablePanel;
import jrummikub.view.IView;
@@ -41,6 +42,11 @@ public class View extends JFrame implements IView {
}
@Override
+ public ISettingsPanel getSettingsPanel() {
+ return settingsPanel;
+ }
+
+ @Override
public ITablePanel getTablePanel() {
return table;
}