Make player name setup work
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@281 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
10cd2cb902
commit
04df1a24d8
7 changed files with 183 additions and 24 deletions
|
@ -84,4 +84,10 @@ public class MockView implements IView {
|
||||||
return newRoundEvent;
|
return newRoundEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ISettingsPanel getSettingsPanel() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -39,4 +39,24 @@ public class PlayerSettings {
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
24
src/jrummikub/view/ISettingsPanel.java
Normal file
24
src/jrummikub/view/ISettingsPanel.java
Normal file
|
@ -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();
|
||||||
|
}
|
|
@ -9,6 +9,13 @@ import jrummikub.util.IEvent;
|
||||||
* The top-level view interface
|
* The top-level view interface
|
||||||
*/
|
*/
|
||||||
public interface IView {
|
public interface IView {
|
||||||
|
/**
|
||||||
|
* Returns the settings panel
|
||||||
|
*
|
||||||
|
* @return the settings panel
|
||||||
|
*/
|
||||||
|
public ISettingsPanel getSettingsPanel();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the table
|
* Returns the table
|
||||||
*
|
*
|
||||||
|
|
|
@ -10,7 +10,7 @@ class ImageUtil {
|
||||||
private 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 image = new BufferedImage(size, size,
|
||||||
BufferedImage.TYPE_INT_RGB);
|
BufferedImage.TYPE_INT_RGB);
|
||||||
Graphics2D g = image.createGraphics();
|
Graphics2D g = image.createGraphics();
|
||||||
|
@ -19,7 +19,7 @@ class ImageUtil {
|
||||||
g.fillRect(0, 0, size, size);
|
g.fillRect(0, 0, size, size);
|
||||||
|
|
||||||
g.setColor(c);
|
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);
|
return new ImageIcon(image);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,17 @@ package jrummikub.view.impl;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.event.ComponentAdapter;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.event.ComponentEvent;
|
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 java.util.LinkedList;
|
||||||
|
|
||||||
import javax.swing.BoxLayout;
|
import javax.swing.BoxLayout;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTabbedPane;
|
import javax.swing.JTabbedPane;
|
||||||
|
@ -16,20 +21,45 @@ import javax.swing.JTextField;
|
||||||
import javax.swing.border.CompoundBorder;
|
import javax.swing.border.CompoundBorder;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.border.LineBorder;
|
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")
|
@SuppressWarnings("serial")
|
||||||
class SettingsPanel extends JPanel {
|
class SettingsPanel extends JPanel implements ISettingsPanel {
|
||||||
private JPanel playerSetupPanel;
|
private JPanel playerSetupPanel;
|
||||||
private JPanel playerSettingsViewport;
|
private JPanel playerSettingsViewport;
|
||||||
private JPanel ruleSetupPanel;
|
private JPanel ruleSetupPanel;
|
||||||
|
|
||||||
|
private GameSettings gameSettings = new GameSettings();
|
||||||
|
|
||||||
private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();
|
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() {
|
private void addPlayerSettings() {
|
||||||
PlayerSettingsPanel panel = new PlayerSettingsPanel();
|
PlayerSettingsPanel panel = new PlayerSettingsPanel();
|
||||||
playerSettingsPanels.add(panel);
|
playerSettingsPanels.add(panel);
|
||||||
playerSettingsViewport.add(panel,
|
playerSettingsViewport.add(panel,
|
||||||
playerSettingsViewport.getComponentCount() - 1);
|
playerSettingsViewport.getComponentCount() - 1);
|
||||||
|
|
||||||
|
settingsChangeEvent.emit(gameSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createPlayerSetupPanel() {
|
private void createPlayerSetupPanel() {
|
||||||
|
@ -45,7 +75,20 @@ class SettingsPanel extends JPanel {
|
||||||
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
||||||
playerSetupPanel.add(scrollPane);
|
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();
|
||||||
addPlayerSettings();
|
addPlayerSettings();
|
||||||
|
@ -56,7 +99,7 @@ class SettingsPanel extends JPanel {
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingsPanel() {
|
SettingsPanel() {
|
||||||
setLayout(new BorderLayout());
|
setLayout(new GridBagLayout());
|
||||||
|
|
||||||
final JTabbedPane tabbedPane = new JTabbedPane();
|
final JTabbedPane tabbedPane = new JTabbedPane();
|
||||||
|
|
||||||
|
@ -66,34 +109,87 @@ class SettingsPanel extends JPanel {
|
||||||
createRuleSetupPanel();
|
createRuleSetupPanel();
|
||||||
tabbedPane.addTab("Regeln", ruleSetupPanel);
|
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");
|
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(
|
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(
|
||||||
10, 10, 10, 10)));
|
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;
|
|
||||||
|
|
||||||
// tabbedPane.setBounds(x, y, width, height);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class PlayerSettingsPanel extends JPanel {
|
private class PlayerSettingsPanel extends JPanel {
|
||||||
|
private PlayerSettings settings = new PlayerSettings("Player", Color.RED);
|
||||||
|
|
||||||
|
private void remove() {
|
||||||
|
playerSettingsPanels.remove(PlayerSettingsPanel.this);
|
||||||
|
playerSettingsViewport.remove(PlayerSettingsPanel.this);
|
||||||
|
playerSettingsViewport.revalidate();
|
||||||
|
|
||||||
|
gameSettings.getPlayerList().remove(settings);
|
||||||
|
settingsChangeEvent.emit(gameSettings);
|
||||||
|
}
|
||||||
|
|
||||||
PlayerSettingsPanel() {
|
PlayerSettingsPanel() {
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
add(new JButton(ImageUtil.createColorIcon(Color.RED, 16)),
|
add(new JButton(ImageUtil.createColorIcon(settings.getColor(), 16, 2)),
|
||||||
BorderLayout.WEST);
|
BorderLayout.WEST);
|
||||||
add(new JTextField());
|
final JTextField nameField = new JTextField(settings.getName());
|
||||||
add(new JButton("\u2716"), BorderLayout.EAST);
|
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
|
@Override
|
||||||
|
|
|
@ -15,6 +15,7 @@ import jrummikub.model.Stone;
|
||||||
import jrummikub.util.IEvent;
|
import jrummikub.util.IEvent;
|
||||||
import jrummikub.view.IHandPanel;
|
import jrummikub.view.IHandPanel;
|
||||||
import jrummikub.view.IPlayerPanel;
|
import jrummikub.view.IPlayerPanel;
|
||||||
|
import jrummikub.view.ISettingsPanel;
|
||||||
import jrummikub.view.ITablePanel;
|
import jrummikub.view.ITablePanel;
|
||||||
import jrummikub.view.IView;
|
import jrummikub.view.IView;
|
||||||
|
|
||||||
|
@ -40,6 +41,11 @@ public class View extends JFrame implements IView {
|
||||||
return 2 * (int) (d / 2);
|
return 2 * (int) (d / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ISettingsPanel getSettingsPanel() {
|
||||||
|
return settingsPanel;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ITablePanel getTablePanel() {
|
public ITablePanel getTablePanel() {
|
||||||
return table;
|
return table;
|
||||||
|
|
Reference in a new issue