summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-05-26 15:04:54 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-05-26 15:04:54 +0200
commitcff3180460c8f0bfcb0cacac9f49f58d47bd19b1 (patch)
tree1f1c751573dea41c716b1d14701f435ad1e7e958
parent04df1a24d830dab98bd9ed7f623e5499ead0c424 (diff)
downloadJRummikub-cff3180460c8f0bfcb0cacac9f49f58d47bd19b1.tar
JRummikub-cff3180460c8f0bfcb0cacac9f49f58d47bd19b1.zip
Implement color choosing and automatic color and name choice
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@282 72836036-5685-4462-b002-a69064685172
-rw-r--r--src/jrummikub/view/impl/SettingsPanel.java165
1 files changed, 148 insertions, 17 deletions
diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java
index fa00a0a..9164942 100644
--- a/src/jrummikub/view/impl/SettingsPanel.java
+++ b/src/jrummikub/view/impl/SettingsPanel.java
@@ -7,14 +7,19 @@ import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
+import java.awt.GridLayout;
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 javax.swing.BoxLayout;
+import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
+import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
@@ -32,6 +37,24 @@ import jrummikub.view.ISettingsPanel;
@SuppressWarnings("serial")
class SettingsPanel extends JPanel implements ISettingsPanel {
+ private final static Color[] PLAYER_COLORS = { new Color(1.0f, 0, 0), // red
+ new Color(0, 1.0f, 0), // lime
+ new Color(1.0f, 1.0f, 0), // yellow
+ new Color(0, 0, 1.0f), // blue
+ new Color(1.0f, 0, 1.0f), // fuchsia
+ new Color(0, 1.0f, 1.0f), // aqua
+ new Color(0.5f, 0, 0), // maroon
+ new Color(0, 0.5f, 0), // green
+ new Color(0.5f, 0.5f, 0), // olive
+ new Color(0, 0, 0.5f), // navy
+ new Color(0.5f, 0, 0.5f), // purple
+ new Color(0, 0.5f, 0.5f), // teal
+ new Color(0, 0, 0), // black
+ new Color(0.5f, 0.5f, 0.5f), // gray
+ new Color(0.75f, 0.75f, 0.75f), // silver
+ new Color(1.0f, 1.0f, 1.0f), // white
+ };
+
private JPanel playerSetupPanel;
private JPanel playerSettingsViewport;
private JPanel ruleSetupPanel;
@@ -54,7 +77,34 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
}
private void addPlayerSettings() {
- PlayerSettingsPanel panel = new PlayerSettingsPanel();
+ // Find unused player name
+ int num = playerSettingsPanels.size();
+
+ nameLoop: while (true) {
+ num++;
+ String playerName = "Spieler " + num;
+
+ for (PlayerSettings player : gameSettings.getPlayerList()) {
+ if (playerName.equals(player.getName())) {
+ continue nameLoop;
+ }
+ }
+ 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;
+ }
+
+ PlayerSettingsPanel panel = new PlayerSettingsPanel("Spieler " + num, color);
playerSettingsPanels.add(panel);
playerSettingsViewport.add(panel,
playerSettingsViewport.getComponentCount() - 1);
@@ -135,7 +185,10 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
}
private class PlayerSettingsPanel extends JPanel {
- private PlayerSettings settings = new PlayerSettings("Player", Color.RED);
+ private JTextField nameField;
+ private JButton colorButton;
+
+ private PlayerSettings settings;
private void remove() {
playerSettingsPanels.remove(PlayerSettingsPanel.this);
@@ -146,35 +199,70 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
settingsChangeEvent.emit(gameSettings);
}
- PlayerSettingsPanel() {
- setLayout(new BorderLayout());
+ private void updateColor() {
+ colorButton
+ .setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2));
+ }
- add(new JButton(ImageUtil.createColorIcon(settings.getColor(), 16, 2)),
- BorderLayout.WEST);
- final JTextField nameField = new JTextField(settings.getName());
- nameField.getDocument().addDocumentListener(new DocumentListener() {
- private void update() {
- if (nameField.getText() == settings.getName()) {
- return;
- }
+ private void setName() {
+ if (nameField.getText() == settings.getName()) {
+ return;
+ }
- settings.setName(nameField.getText());
- settingsChangeEvent.emit(gameSettings);
+ settings.setName(nameField.getText());
+ settingsChangeEvent.emit(gameSettings);
+ }
+
+ 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();
+ settingsChangeEvent.emit(gameSettings);
+ }
+
+ PlayerSettingsPanel(String name, Color color) {
+ settings = new PlayerSettings(name, color);
+
+ setLayout(new BorderLayout());
+
+ colorButton = new JButton();
+ updateColor();
+ colorButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ new ColorMenu();
+ }
+ });
+ add(colorButton, BorderLayout.WEST);
+
+ nameField = new JTextField(settings.getName());
+ nameField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
- update();
+ setName();
}
@Override
public void removeUpdate(DocumentEvent e) {
- update();
+ setName();
}
@Override
public void changedUpdate(DocumentEvent e) {
- update();
+ setName();
}
});
add(nameField);
@@ -201,5 +289,48 @@ class SettingsPanel extends JPanel implements ISettingsPanel {
public Dimension getMaximumSize() {
return new Dimension(Integer.MAX_VALUE, getPreferredSize().height);
}
+
+ private class ColorMenu extends JPanel {
+ ColorMenu() {
+ setLayout(new GridLayout(4, 4, 1, 1));
+
+ JPopupMenu menu = new JPopupMenu();
+
+ for (Color c : PLAYER_COLORS) {
+ add(new ColorButton(c, menu));
+ }
+
+ menu.setLayout(new BorderLayout());
+ menu.add(this);
+ menu.show(colorButton, 0, colorButton.getHeight());
+ }
+
+ private class ColorButton extends JLabel {
+ ColorButton(final Color c, final JPopupMenu menu) {
+ final Icon normalIcon = ImageUtil.createColorIcon(c, 16, 1);
+ final Icon hoverIcon = ImageUtil.createColorIcon(c, 16, 2);
+
+ setIcon(normalIcon);
+
+ addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseEntered(MouseEvent e) {
+ setIcon(hoverIcon);
+ }
+
+ @Override
+ public void mouseExited(MouseEvent e) {
+ setIcon(normalIcon);
+ }
+
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ menu.setVisible(false);
+ setColor(c);
+ }
+ });
+ }
+ }
+ }
}
}