From 04df1a24d830dab98bd9ed7f623e5499ead0c424 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 26 May 2011 01:33:17 +0200 Subject: Make player name setup work git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@281 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/impl/ImageUtil.java | 4 +- src/jrummikub/view/impl/SettingsPanel.java | 138 ++++++++++++++++++++++++----- src/jrummikub/view/impl/View.java | 6 ++ 3 files changed, 125 insertions(+), 23 deletions(-) (limited to 'src/jrummikub/view/impl') 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 playerSettingsPanels = new LinkedList(); + private Event1 settingsChangeEvent = new Event1(); + private Event1 startGameEvent = new Event1(); + + @Override + public IEvent1 getSettingsChangeEvent() { + return settingsChangeEvent; + } + + @Override + public IEvent1 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; @@ -40,6 +41,11 @@ public class View extends JFrame implements IView { return 2 * (int) (d / 2); } + @Override + public ISettingsPanel getSettingsPanel() { + return settingsPanel; + } + @Override public ITablePanel getTablePanel() { return table; -- cgit v1.2.3