1 package net.sourceforge.simplegamenet.framework.gui;
2
3 import java.awt.*;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import javax.swing.*;
7 import javax.swing.text.DefaultStyledDocument;
8 import javax.swing.text.StyledDocument;
9 import net.sourceforge.simplegamenet.specs.gui.PlayerSettingsPanel;
10 import net.sourceforge.simplegamenet.specs.to.PlayerSettings;
11 import net.sourceforge.simplegamenet.specs.to.PlayerSettingsMap;
12 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
13 import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
14
15 public class ChatTabPanel extends JPanel implements ActionListener {
16
17 private static final String START_ALLOWED_STATE = "startAllowedState";
18 private static final String ABORT_ALLOWED_STATE = "abortAllowedState";
19
20 private ClientGUIFacade clientGUIFacade = null;
21
22 private JSplitPane innerSplitPane;
23
24 private StyledDocument chatLogDocument = new DefaultStyledDocument();
25 private ChatClientPanel chatClientPanel;
26
27 private JTabbedPane playersTabbedPane = new JTabbedPane();
28 private PlayersTabPanel playersTabPanel = new PlayersTabPanel();
29
30
31
32 private CardLayout startAbortLayout = new CardLayout();
33 private JPanel startAbortPanel = new JPanel();
34
35 private JButton startGameButton = new JButton("Start game");
36 private JButton abortGameButton = new JButton("Abort game");
37
38 public ChatTabPanel() {
39 ProportionLayout layout = new ProportionLayout();
40 layout.appendColumn(10);
41 layout.appendColumn(0, 1.0);
42 layout.appendColumn(10);
43 layout.appendRow(10);
44 layout.appendRow(0, 1.0);
45 layout.appendRow(10);
46 setLayout(layout);
47
48 chatClientPanel = new ChatClientPanel(chatLogDocument);
49 chatClientPanel.setEnabled(false);
50 ProportionLayout playersLayout = new ProportionLayout();
51 playersLayout.appendColumn(10);
52 playersLayout.appendColumn(0, 1.0);
53 playersLayout.appendColumn(10);
54 playersLayout.appendRow(0, 1.0);
55 playersLayout.appendRow(10);
56 playersLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
57 JPanel playersPanel = new JPanel(playersLayout);
58 playersTabbedPane.add("All", playersTabPanel);
59 playersPanel.add(playersTabbedPane,
60 new ProportionConstraints(0, 0, 3, 1, 1, 0));
61 startAbortPanel.setLayout(startAbortLayout);
62 startGameButton.setEnabled(false);
63 startGameButton.addActionListener(this);
64 startAbortPanel.add(startGameButton, START_ALLOWED_STATE);
65 abortGameButton.setEnabled(false);
66 abortGameButton.addActionListener(this);
67 startAbortPanel.add(abortGameButton, ABORT_ALLOWED_STATE);
68 playersPanel.add(startAbortPanel, new ProportionConstraints(1, 2, 1, 1, 1, 2,
69 ProportionConstraints.CENTER, ProportionConstraints.VERTICAL));
70 innerSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
71 chatClientPanel, playersPanel);
72 innerSplitPane.setBorder(BorderFactory.createEmptyBorder());
73 innerSplitPane.setOneTouchExpandable(false);
74 innerSplitPane.setDividerSize(10);
75 innerSplitPane.setDividerLocation(0.5);
76 innerSplitPane.setResizeWeight(0.5);
77 add(innerSplitPane, new ProportionConstraints(1, 1));
78 }
79
80 public StyledDocument getChatLogDocument() {
81 return chatLogDocument;
82 }
83
84 public void gameJoined(ClientGUIFacade clientGUIFacade,
85 PlayerSettingsPanel playerSettingsPanel,
86 PlayerSettingsMap playerSettingsMap, boolean clientIsHost) {
87 this.clientGUIFacade = clientGUIFacade;
88 playersTabPanel.gameJoined(clientGUIFacade, playerSettingsPanel, playerSettingsMap);
89 chatClientPanel.gameJoined(clientGUIFacade);
90
91 startGameButton.setEnabled(clientIsHost);
92 abortGameButton.setEnabled(clientIsHost);
93
94
95
96 validate();
97 }
98
99 public void joinedGameQuit() {
100 startGameButton.setEnabled(false);
101 abortGameButton.setEnabled(false);
102 playersTabPanel.joinedGameQuit();
103 chatClientPanel.joinedGameQuit();
104 startAbortLayout.show(startAbortPanel, START_ALLOWED_STATE);
105 clientGUIFacade = null;
106
107
108
109 validate();
110 }
111
112 public void gameStarted(boolean clientIsHost) {
113 startGameButton.setEnabled(clientIsHost);
114 abortGameButton.setEnabled(clientIsHost);
115 startAbortLayout.show(startAbortPanel, ABORT_ALLOWED_STATE);
116 }
117
118 public void gameEnding(boolean clientIsHost) {
119 startGameButton.setEnabled(false);
120 abortGameButton.setEnabled(false);
121 startAbortLayout.show(startAbortPanel, START_ALLOWED_STATE);
122 }
123
124 public void gameEnded(boolean clientIsHost) {
125 startGameButton.setEnabled(clientIsHost);
126 abortGameButton.setEnabled(clientIsHost);
127 startAbortLayout.show(startAbortPanel, START_ALLOWED_STATE);
128 }
129
130 public void playerSettingsUpdated(PlayerSettings updatedPlayerSettings, boolean thisClient) {
131
132
133 if (thisClient) {
134 playersTabPanel.playerSettingsUpdated(
135 updatedPlayerSettings.createPlayerSettingsPanel());
136 }
137 }
138
139 public void actionPerformed(ActionEvent actionEvent) {
140 if (actionEvent.getSource() == startGameButton) {
141 try {
142 clientGUIFacade.startGame();
143 startGameButton.setEnabled(false);
144 abortGameButton.setEnabled(false);
145 } catch (IllegalStateException exception) {
146 JOptionPane.showMessageDialog(this,
147 "The game could not be started.",
148 "Start game failed", JOptionPane.ERROR_MESSAGE);
149 }
150 } else if (actionEvent.getSource() == abortGameButton) {
151 try {
152 clientGUIFacade.abortGame();
153 startGameButton.setEnabled(false);
154 abortGameButton.setEnabled(false);
155 } catch (IllegalStateException exception) {
156 JOptionPane.showMessageDialog(this,
157 "The game could not be aborted.",
158 "Abort game failed", JOptionPane.ERROR_MESSAGE);
159 }
160 }
161 }
162
163 }