summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jrummikub/view/impl/SettingsPanel.java285
1 files changed, 133 insertions, 152 deletions
diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java
index ea1c181..a5ad8a3 100644
--- a/src/jrummikub/view/impl/SettingsPanel.java
+++ b/src/jrummikub/view/impl/SettingsPanel.java
@@ -12,7 +12,8 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.Icon;
@@ -33,6 +34,7 @@ import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.util.Event;
import jrummikub.util.Event1;
+import jrummikub.util.Event2;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
@@ -43,48 +45,114 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
private JPanel playerSetupPanel;
private JPanel playerSettingsViewport;
private JPanel ruleSetupPanel;
+ private JButton addPlayerButton;
+ private JLabel errorMessageLabel;
+ private JButton startButton;
- private GameSettings gameSettings = new GameSettings();
+ private List<PlayerSettingsPanel> playerSettingsPanels = new ArrayList<PlayerSettingsPanel>();
- private LinkedList<PlayerSettingsPanel> playerSettingsPanels = new LinkedList<PlayerSettingsPanel>();
-
- private Event1<GameSettings> settingsChangeEvent = new Event1<GameSettings>();
private Event startGameEvent = new Event();
+ private Event addPlayerEvent = new Event();
+ 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 Event1<Integer> changeInitialMeldThresholdEvent = new Event1<Integer>();
- private void addPlayerSettings() {
- // Find unused player name
- int num = playerSettingsPanels.size();
+ @Override
+ public IEvent getStartGameEvent() {
+ return startGameEvent;
+ }
- nameLoop: while (true) {
- num++;
- String playerName = "Spieler " + num;
+ @Override
+ public IEvent getAddPlayerEvent() {
+ return addPlayerEvent;
+ }
- for (PlayerSettings player : gameSettings.getPlayerList()) {
- if (playerName.equals(player.getName())) {
- continue nameLoop;
- }
- }
- break;
+ @Override
+ public IEvent1<Integer> getRemovePlayerEvent() {
+ return removePlayerEvent;
+ }
+
+ @Override
+ public IEvent2<Integer, Color> getChangePlayerColorEvent() {
+ return changePlayerColorEvent;
+ }
+
+ @Override
+ public IEvent2<Integer, String> getChangePlayerNameEvent() {
+ return changePlayerNameEvent;
+ }
+
+ @Override
+ public IEvent1<Integer> getChangeInitialMeldThresholdEvent() {
+ return changeInitialMeldThresholdEvent;
+ }
+
+ @Override
+ public void setError(SettingsError error) {
+ switch (error) {
+ case NO_ERROR:
+ errorMessageLabel.setText(" ");
+ break;
+ case DUPLICATE_PLAYER_NAME:
+ errorMessageLabel
+ .setText("Jeder Spielername darf nur einmal verwendet werden.");
+ break;
+ case NO_PLAYER_NAME:
+ errorMessageLabel.setText("Jeder Spieler muss einen Namen haben.");
+ break;
}
+ }
- // Next, find unused color
- Color color = null;
- colorLoop: for (Color c : PLAYER_COLORS) {
- color = c;
- for (PlayerSettings player : gameSettings.getPlayerList()) {
- if (c == player.getColor()) {
- continue colorLoop;
- }
- }
- break;
+ @Override
+ public void enableStartGameButton(boolean enable) {
+ startButton.setEnabled(enable);
+ }
+
+ @Override
+ public void enableAddPlayerButton(boolean enable) {
+ addPlayerButton.setVisible(enable);
+ }
+
+ @Override
+ public void enableRemovePlayerButtons(boolean enable) {
+ for (PlayerSettingsPanel psp : playerSettingsPanels) {
+ psp.enableRemoveButton(enable);
}
+ }
- PlayerSettingsPanel panel = new PlayerSettingsPanel("Spieler " + num, color);
+ @Override
+ public void setGameSettings(GameSettings gameSettings) {
+ while (playerSettingsPanels.size() > gameSettings.getPlayerList().size()) {
+ removePlayerSettingsPanel();
+ }
+
+ for (int i = 0; i < gameSettings.getPlayerList().size(); ++i) {
+ updatePlayerSettingsPanel(i, gameSettings.getPlayerList().get(i));
+ }
+
+ playerSettingsViewport.revalidate();
+ }
+
+ private void updatePlayerSettingsPanel(int i, PlayerSettings settings) {
+ if (i < playerSettingsPanels.size()) {
+ playerSettingsPanels.get(i).setSettings(settings);
+ } else {
+ addPlayerSettingsPanel(i, settings);
+ }
+ }
+
+ private void addPlayerSettingsPanel(int i, PlayerSettings settings) {
+ PlayerSettingsPanel panel = new PlayerSettingsPanel(i, settings);
playerSettingsPanels.add(panel);
playerSettingsViewport.add(panel,
playerSettingsViewport.getComponentCount() - 1);
+ }
- settingsChangeEvent.emit(gameSettings);
+ private void removePlayerSettingsPanel() {
+ PlayerSettingsPanel p = playerSettingsPanels.remove(playerSettingsPanels
+ .size() - 1);
+ playerSettingsViewport.remove(p);
}
private void createPlayerSetupPanel() {
@@ -104,19 +172,15 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
addPlayerPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 2));
playerSettingsViewport.add(addPlayerPanel);
- JButton addPlayerButton = new JButton("+");
+ addPlayerButton = new JButton("+");
addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD));
addPlayerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
- addPlayerSettings();
- playerSettingsViewport.revalidate();
+ addPlayerEvent.emit();
}
});
addPlayerPanel.add(addPlayerButton);
-
- addPlayerSettings();
- addPlayerSettings();
}
private void createRuleSetupPanel() {
@@ -141,12 +205,12 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
c.weighty = 1;
add(tabbedPane, c);
- JLabel errorMessageLabel = new JLabel(" ");
+ errorMessageLabel = new JLabel(" ");
errorMessageLabel.setForeground(Color.RED);
c.weighty = 0;
add(errorMessageLabel, c);
- JButton startButton = new JButton("Spiel starten");
+ startButton = new JButton("Spiel starten");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@@ -160,61 +224,34 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
}
private class PlayerSettingsPanel extends JPanel {
- private JTextField nameField;
+ private int playerNumber;
private JButton colorButton;
+ private JTextField nameField;
+ private JButton removeButton;
- private PlayerSettings settings;
-
- private void remove() {
- playerSettingsPanels.remove(PlayerSettingsPanel.this);
- playerSettingsViewport.remove(PlayerSettingsPanel.this);
- playerSettingsViewport.revalidate();
-
- gameSettings.getPlayerList().remove(settings);
- settingsChangeEvent.emit(gameSettings);
- }
+ public void setSettings(PlayerSettings settings) {
+ if (!nameField.getText().equals(settings.getName())) {
+ nameField.setText(settings.getName());
+ }
- private void updateColor() {
colorButton
.setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2));
}
- private void setName() {
- if (nameField.getText() == settings.getName()) {
- return;
- }
-
- settings.setName(nameField.getText());
- settingsChangeEvent.emit(gameSettings);
+ public void enableRemoveButton(boolean enable) {
+ removeButton.setEnabled(enable);
}
- private void setColor(Color c) {
- if (c == settings.getColor()) {
- return;
- }
-
- // Exchange colors if the color is already used
- for (PlayerSettingsPanel other : playerSettingsPanels) {
- if (other.settings.getColor() == c) {
- other.settings.setColor(settings.getColor());
- other.updateColor();
- break;
- }
- }
-
- settings.setColor(c);
- updateColor();
+ public PlayerSettingsPanel(int playerNumber, PlayerSettings settings) {
+ setLayout(new BorderLayout());
- settingsChangeEvent.emit(gameSettings);
- }
+ this.playerNumber = playerNumber;
- PlayerSettingsPanel(String name, Color color) {
- settings = new PlayerSettings(name, color);
+ colorButton = new JButton();
+ nameField = new JTextField(settings.getName());
- setLayout(new BorderLayout());
+ setSettings(settings);
- colorButton = new JButton();
- updateColor();
colorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@@ -223,26 +260,25 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
});
add(colorButton, BorderLayout.WEST);
- nameField = new JTextField(settings.getName());
nameField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
- setName();
+ changeName();
}
@Override
public void removeUpdate(DocumentEvent e) {
- setName();
+ changeName();
}
@Override
public void changedUpdate(DocumentEvent e) {
- setName();
+ changeName();
}
});
add(nameField);
- JButton removeButton = new JButton("\u00d7");
+ removeButton = new JButton("\u00d7");
removeButton.setFont(removeButton.getFont().deriveFont(Font.BOLD));
removeButton.addActionListener(new ActionListener() {
@Override
@@ -251,8 +287,6 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
}
});
add(removeButton, BorderLayout.EAST);
-
- gameSettings.getPlayerList().add(settings);
}
@Override
@@ -265,6 +299,18 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
return new Dimension(Integer.MAX_VALUE, getPreferredSize().height);
}
+ private void changeName() {
+ changePlayerNameEvent.emit(playerNumber, nameField.getText());
+ }
+
+ private void changeColor(Color c) {
+ changePlayerColorEvent.emit(playerNumber, c);
+ }
+
+ private void remove() {
+ removePlayerEvent.emit(playerNumber);
+ }
+
private class ColorMenu extends JPanel {
ColorMenu() {
setLayout(new GridLayout(4, 4, 1, 1));
@@ -301,76 +347,11 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
@Override
public void mouseClicked(MouseEvent e) {
menu.setVisible(false);
- setColor(c);
+ changeColor(c);
}
});
}
}
}
}
-
- @Override
- public IEvent getAddPlayerEvent() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public IEvent1<Integer> getRemovePlayerEvent() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public IEvent2<Integer, Color> getChangePlayerColorEvent() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public IEvent2<Integer, String> getChangePlayerNameEvent() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public IEvent1<Integer> getChangeInitialMeldThresholdEvent() {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public void setError(SettingsError error) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void enableStartGameButton(boolean enable) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void enableAddPlayerButton(boolean enable) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void enableRemovePlayerButtons(boolean enable) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public IEvent getStartGameEvent() {
- return startGameEvent;
- }
-
- @Override
- public void setGameSettings(GameSettings gameSettings) {
- // TODO Auto-generated method stub
-
- }
}