1 package net.sourceforge.simplegamenet.framework.gui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import javax.swing.*;
6 import net.sourceforge.simplegamenet.specs.gui.PlayerSettingsPanel;
7 import net.sourceforge.simplegamenet.specs.to.PlayerSettingsMap;
8 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
9 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
10
11 class PlayersTabPanel extends JPanel implements ActionListener {
12
13 private ClientGUIFacade clientGUIFacade;
14
15
16
17 private JPanel changePlayerSettingsPanel = new JPanel();
18 private PlayerSettingsPanel playerSettingsPanel = null;
19 private JButton changePlayerSettingsButton = new JButton("Change settings");
20
21 PlayersTabPanel() {
22 ProportionLayout layout = new ProportionLayout();
23 layout.appendColumn(10);
24 layout.appendColumn(0, 1.0);
25 layout.appendColumn(10);
26 layout.appendRow(10);
27 layout.appendRow(0, 1.0);
28 layout.appendRow(10);
29 layout.appendRow(0, 1.0);
30 layout.appendRow(10);
31 setLayout(layout);
32
33
34
35 ProportionLayout changePlayerSettingsLayout = new ProportionLayout();
36 changePlayerSettingsLayout.appendColumn(5);
37 changePlayerSettingsLayout.appendColumn(0, 1.0);
38 changePlayerSettingsLayout.appendColumn(5);
39 changePlayerSettingsLayout.appendRow(0);
40 changePlayerSettingsLayout.appendRow(0, 1.0);
41 changePlayerSettingsLayout.appendRow(10);
42 changePlayerSettingsLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
43 changePlayerSettingsLayout.appendRow(5);
44 changePlayerSettingsPanel.setLayout(changePlayerSettingsLayout);
45 changePlayerSettingsPanel.setBorder(
46 BorderFactory.createTitledBorder("Your player settings"));
47 changePlayerSettingsButton.setEnabled(false);
48 changePlayerSettingsButton.addActionListener(this);
49 changePlayerSettingsPanel.add(changePlayerSettingsButton,
50 new ProportionConstraints(1, 3, 1, 1, 1, 3,
51 ProportionConstraints.CENTER, ProportionConstraints.VERTICAL));
52 add(changePlayerSettingsPanel, new ProportionConstraints(1, 3));
53 }
54
55 public void gameJoined(ClientGUIFacade clientGUIFacade,
56 PlayerSettingsPanel playerSettingsPanel,
57 PlayerSettingsMap playerSettingsMap) {
58 this.clientGUIFacade = clientGUIFacade;
59 this.playerSettingsPanel = playerSettingsPanel;
60 changePlayerSettingsPanel.add(playerSettingsPanel,
61 new ProportionConstraints(1, 1));
62
63
64 changePlayerSettingsButton.setEnabled(true);
65 validate();
66 }
67
68 public void joinedGameQuit() {
69 changePlayerSettingsButton.setEnabled(false);
70 changePlayerSettingsPanel.remove(playerSettingsPanel);
71 playerSettingsPanel = null;
72
73
74 validate();
75 }
76
77 public void playerSettingsUpdated(PlayerSettingsPanel updatedPlayerSettingsPanel) {
78 changePlayerSettingsPanel.remove(playerSettingsPanel);
79 playerSettingsPanel = updatedPlayerSettingsPanel;
80 changePlayerSettingsPanel.add(playerSettingsPanel,
81 new ProportionConstraints(1, 1));
82 validate();
83 }
84
85 /***
86 * Invoked when an action occurs.
87 */
88 public void actionPerformed(ActionEvent actionEvent) {
89 if (clientGUIFacade == null || !playerSettingsPanel.areSettingsAcceptable()) {
90 return;
91 }
92 try {
93 clientGUIFacade.changePlayerSettings(playerSettingsPanel);
94 } catch (IllegalStateException exception) {
95 JOptionPane.showMessageDialog(getTopLevelAncestor(),
96 "Cannot change player settings while game is in progress.",
97 "Change player settings failed", JOptionPane.ERROR_MESSAGE);
98 } catch (IllegalArgumentException exception) {
99 JOptionPane.showMessageDialog(getTopLevelAncestor(),
100 "Changed player settings are not compatible with current game "
101 + "state.",
102 "Change player settings failed", JOptionPane.ERROR_MESSAGE);
103 }
104 }
105
106 }