From 43ace4c18a5aeb0d466ff9180614ba07dc207f33 Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Sun, 29 May 2011 21:49:03 +0200 Subject: Added player type selection to the settings panel git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@315 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/impl/SettingsPanel.java | 177 ++++++++++++++++++----------- 1 file changed, 112 insertions(+), 65 deletions(-) (limited to 'src/jrummikub/view/impl') diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index 15c6ee4..a96c5a7 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -2,6 +2,7 @@ package jrummikub.view.impl; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; @@ -14,12 +15,14 @@ import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.Icon; import javax.swing.JButton; +import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; @@ -37,6 +40,7 @@ import javax.swing.event.ChangeListener; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import jrummikub.control.turn.TurnControlFactory; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.util.Event; @@ -67,6 +71,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event1 removePlayerEvent = new Event1(); private Event2 changePlayerColorEvent = new Event2(); private Event2 changePlayerNameEvent = new Event2(); + private Event2 changePlayerTypeEvent = new Event2(); private Event1 changeInitialMeldThresholdEvent = new Event1(); private Event1 changeJokerNumberEvent = new Event1(); @@ -208,60 +213,55 @@ class SettingsPanel extends JPanel implements ISettingsPanel { GridBagLayout layout = new GridBagLayout(); optionsPanel.setLayout(layout); - GridBagConstraints labelC = new GridBagConstraints(); - GridBagConstraints optionC = new GridBagConstraints(); - - labelC.anchor = GridBagConstraints.EAST; - labelC.gridx = 0; - labelC.insets = new Insets(4, 4, 4, 4); - optionC.anchor = GridBagConstraints.WEST; - optionC.gridx = 1; - optionC.insets = new Insets(2, 2, 2, 2); - - JLabel label = new JLabel("Auslegeschranke:"); - labelC.gridy = 0; - optionsPanel.add(label, labelC); - - initialMeldThresholdSpinner = new JSpinner(); - initialMeldThresholdSpinner.setModel(new SpinnerNumberModel(1, 1, 999, - 1)); - initialMeldThresholdSpinner.setPreferredSize(new Dimension(60, - initialMeldThresholdSpinner.getMinimumSize().height)); - initialMeldThresholdSpinner.addChangeListener(new ChangeListener() { - @Override - public void stateChanged(ChangeEvent e) { - changeInitialMeldThresholdEvent - .emit((Integer) initialMeldThresholdSpinner.getValue()); - } - }); - optionC.gridy = 0; - optionsPanel.add(initialMeldThresholdSpinner, optionC); + makeOptionLabel(0, "Auslegeschranke:"); + initialMeldThresholdSpinner = makeOptionSpinner(0, 1, 999, 1, + changeInitialMeldThresholdEvent); + + makeOptionLabel(1, "Jokeranzahl:"); + jokerNumberSpinner = makeOptionSpinner(1, 1, 999, 1, + changeJokerNumberEvent); + + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 1; + c.gridy = 2; + c.fill = GridBagConstraints.BOTH; + c.weightx = 1; + c.weighty = 1; + optionsPanel.add(makeFiller(), c); - label = new JLabel("Jokeranzahl:"); - labelC.gridx = 0; - labelC.gridy = 1; - optionsPanel.add(label, labelC); + // TODO Knöpfe für Dinge + } - jokerNumberSpinner = new JSpinner(); - jokerNumberSpinner.setModel(new SpinnerNumberModel(0, 0, 999, 1)); - jokerNumberSpinner.addChangeListener(new ChangeListener() { + private JSpinner makeOptionSpinner(int row, int min, int max, int step, + final Event1 targetEvent) { + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.WEST; + c.gridx = 1; + c.gridy = row; + c.insets = new Insets(2, 2, 2, 2); + final JSpinner spinner = new JSpinner(); + spinner.setModel(new SpinnerNumberModel(min, min, max, step)); + spinner.setPreferredSize(new Dimension(60, + spinner.getMinimumSize().height)); + spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { - changeJokerNumberEvent.emit((Integer) jokerNumberSpinner - .getValue()); + targetEvent.emit((Integer) spinner.getValue()); } }); - optionC.gridy = 1; - optionsPanel.add(jokerNumberSpinner, optionC); + optionsPanel.add(spinner, c); + return spinner; + } - labelC.gridx = 1; - labelC.gridy = 2; - labelC.fill = GridBagConstraints.BOTH; - labelC.weightx = 1; - labelC.weighty = 1; - optionsPanel.add(makeFiller(), labelC); + private void makeOptionLabel(int row, String name) { + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.EAST; + c.gridx = 0; + c.gridy = row; + c.insets = new Insets(4, 4, 4, 4); - // TODO Knöpfe für Dinge + JLabel label = new JLabel(name); + optionsPanel.add(label, c); } SettingsPanel() { @@ -305,11 +305,16 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JButton colorButton; private JTextField nameField; private JButton removeButton; + private JComboBox playerType; public void setSettings(PlayerSettings settings) { if (!nameField.getText().equals(settings.getName())) { nameField.setText(settings.getName()); } + // playerType.setSelectedIndex(0); + playerType.setSelectedIndex(Arrays.binarySearch( + TurnControlFactory.Type.values(), + settings.getTurnControlType())); colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2)); @@ -319,41 +324,64 @@ class SettingsPanel extends JPanel implements ISettingsPanel { removeButton.setEnabled(enable); } + class NameChangeListener implements DocumentListener { + @Override + public void insertUpdate(DocumentEvent e) { + changeName(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + changeName(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + changeName(); + } + } + public PlayerSettingsPanel(int playerNumber, PlayerSettings settings) { - setLayout(new BorderLayout()); + setLayout(new GridBagLayout()); this.playerNumber = playerNumber; + createControls(settings); + setSettings(settings); + } + + private void createControls(PlayerSettings settings) { + GridBagConstraints c = new GridBagConstraints(); + GridBagConstraints c2 = new GridBagConstraints(); colorButton = new JButton(); nameField = new JTextField(settings.getName()); - setSettings(settings); - colorButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new ColorMenu(); } }); - add(colorButton, BorderLayout.WEST); + c.gridy = 0; + add(colorButton, c); - nameField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void insertUpdate(DocumentEvent e) { - changeName(); - } + nameField.getDocument().addDocumentListener( + new NameChangeListener()); + c2.gridy = 0; + c2.weightx = 1; + c2.fill = GridBagConstraints.HORIZONTAL; + add(nameField, c2); - @Override - public void removeUpdate(DocumentEvent e) { - changeName(); - } + String[] choices = { "Mensch", "Computer" }; + playerType = new JComboBox(choices); + playerType.addActionListener(new ActionListener() { @Override - public void changedUpdate(DocumentEvent e) { - changeName(); + public void actionPerformed(ActionEvent e) { + changeType(); } }); - add(nameField); + add(playerType, c); removeButton = new JButton("\u00d7"); removeButton.setFont(removeButton.getFont().deriveFont(Font.BOLD)); @@ -364,17 +392,31 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } }); removeButton.setEnabled(removeButtonsEnabled); - add(removeButton, BorderLayout.EAST); + add(removeButton, c); } @Override public Dimension getPreferredSize() { - return getComponent(1).getPreferredSize(); + Dimension preferredSize = super.getPreferredSize(); + for (Component component : this.getComponents()) { + preferredSize.setSize( + preferredSize.width, + Math.max(preferredSize.height, + component.getPreferredSize().height)); + } + return preferredSize; } @Override public Dimension getMaximumSize() { - return new Dimension(Integer.MAX_VALUE, getPreferredSize().height); + Dimension preferredSize = super.getPreferredSize(); + for (Component component : this.getComponents()) { + preferredSize.setSize( + Integer.MAX_VALUE, + Math.max(preferredSize.height, + component.getPreferredSize().height)); + } + return preferredSize; } private void changeName() { @@ -385,6 +427,11 @@ class SettingsPanel extends JPanel implements ISettingsPanel { changePlayerColorEvent.emit(playerNumber, c); } + private void changeType() { + changePlayerTypeEvent.emit(playerNumber, TurnControlFactory.Type + .values()[playerType.getSelectedIndex()]); + } + private void remove() { removePlayerEvent.emit(playerNumber); } -- cgit v1.2.3