1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 package com.buckosoft.fibs.BuckoFIBS.gui.chatWindow;
83
84 import java.awt.Dimension;
85 import java.awt.Font;
86 import java.awt.GridBagConstraints;
87 import java.awt.GridBagLayout;
88 import java.awt.Insets;
89
90 import javax.swing.JComboBox;
91 import javax.swing.JPanel;
92 import javax.swing.JTextField;
93
94 import com.buckosoft.fibs.BuckoFIBS.CommandDispatcher;
95
96
97
98
99
100
101
102
103 public class ChatPane extends JPanel {
104 private final static boolean DEBUG = false;
105 private static final long serialVersionUID = 1L;
106
107 private CommandDispatcher commandDispatcher = null;
108
109 private JTextField jChatInput = null;
110
111 private ChatTabs chatTabs = null;
112
113 private JComboBox<Command> jCommandComboBox = null;
114 private JComboBox<String> jComboBoxPlayer = null;
115
116
117
118 public enum Command {
119 Command,
120 Kibitz,
121 Say,
122 Shout,
123 Tell,
124 Whisper
125 }
126
127
128
129
130 public ChatPane() {
131 super();
132 getChatTabs();
133 initialize();
134 }
135
136
137
138
139 public void setCommandDispatcher(CommandDispatcher commandDispatcher) {
140 this.commandDispatcher = commandDispatcher;
141 chatTabs.setBFProperties(this.commandDispatcher.getProperties());
142 }
143
144
145
146
147
148 public void writeChatMessage(String name, int cookie, String text) {
149 chatTabs.addChatMessage(name, cookie, text);
150 }
151
152 private void sendChatCommand() {
153 StringBuilder sb = new StringBuilder();
154 if (!this.getJComboBoxCommand().getSelectedItem().equals(Command.Command)) {
155 sb.append(this.getJComboBoxCommand().getSelectedItem());
156 sb.append(" ");
157 }
158 if (this.getJComboBoxCommand().getSelectedItem().equals(Command.Tell)) {
159 String s = (String)this.getJComboBoxPlayer().getSelectedItem();
160 maybeAddPlayer(s);
161 sb.append(s);
162 sb.append(" ");
163 }
164 sb.append(this.getJChatInput().getText());
165 if (DEBUG)
166 System.out.println(sb.toString());
167 this.commandDispatcher.dispatch(CommandDispatcher.Command.SEND_COMMAND, sb.toString());
168
169 }
170
171
172
173
174
175 public void maybeAddPlayer(String playerName) {
176 int z = this.getJComboBoxPlayer().getItemCount();
177 boolean found = false;
178 for (int i=0; i<z; i++) {
179 if (this.getJComboBoxPlayer().getItemAt(i).equals(playerName)) {
180 found = true;
181 break;
182 }
183 }
184 if (!found)
185 this.getJComboBoxPlayer().addItem(playerName);
186 this.getJComboBoxPlayer().setSelectedItem(playerName);
187 }
188
189
190
191 protected void notifyTabChanged() {
192 if (chatTabs == null)
193 return;
194 int i = this.chatTabs.getSelectedIndex();
195 System.out.println("tab = " + i);
196 if (i == 0)
197 setupComboBoxCommand(Command.Kibitz);
198 else if (i == 1)
199 setupComboBoxCommand(Command.Shout);
200 else {
201 setupComboBoxCommand(Command.Tell);
202 String player = this.chatTabs.getTitleAt(i);
203 maybeAddPlayer(player);
204 }
205 this.setupComboBoxPlayer();
206 }
207
208
209 private void initialize() {
210 GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
211 gridBagConstraints11.fill = GridBagConstraints.VERTICAL;
212 gridBagConstraints11.gridy = 1;
213 gridBagConstraints11.weightx = 1.0;
214 gridBagConstraints11.anchor = GridBagConstraints.WEST;
215 gridBagConstraints11.gridx = 1;
216 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
217 gridBagConstraints1.fill = GridBagConstraints.NONE;
218 gridBagConstraints1.gridy = 1;
219 gridBagConstraints1.weightx = 1.0;
220 gridBagConstraints1.anchor = GridBagConstraints.WEST;
221 gridBagConstraints1.gridx = 0;
222 GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
223 gridBagConstraints4.fill = GridBagConstraints.BOTH;
224 gridBagConstraints4.gridx = 2;
225 gridBagConstraints4.gridy = 1;
226 gridBagConstraints4.ipadx = 311;
227 gridBagConstraints4.ipady = 0;
228 gridBagConstraints4.weightx = 100.0;
229 gridBagConstraints4.insets = new Insets(0, 0, 0, 0);
230 GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
231 gridBagConstraints3.gridy = 0;
232 gridBagConstraints3.weightx = 2.0D;
233 gridBagConstraints3.weighty = 2.0D;
234 gridBagConstraints3.anchor = GridBagConstraints.NORTH;
235 gridBagConstraints3.fill = GridBagConstraints.BOTH;
236 gridBagConstraints3.gridwidth = 3;
237 gridBagConstraints3.gridx = 0;
238 GridBagConstraints gridBagConstraints = new GridBagConstraints();
239 gridBagConstraints.gridx = 0;
240 gridBagConstraints.gridy = 0;
241 this.setLayout(new GridBagLayout());
242 this.setSize(315, 221);
243 this.add(getChatTabs(), gridBagConstraints3);
244 this.add(getJChatInput(), gridBagConstraints4);
245 this.add(getJComboBoxCommand(), gridBagConstraints1);
246 this.add(getJComboBoxPlayer(), gridBagConstraints11);
247 setupComboBoxPlayer();
248 }
249
250 private void setupComboBoxPlayer() {
251 if (getJComboBoxCommand().getSelectedItem().equals(Command.Tell))
252 getJComboBoxPlayer().setVisible(true);
253 else
254 getJComboBoxPlayer().setVisible(false);
255 }
256
257
258
259
260 public void setupComboBoxCommand(Command type) {
261 getJComboBoxCommand().setSelectedItem(type);
262 }
263
264
265
266
267
268
269 private JTextField getJChatInput() {
270 if (jChatInput == null) {
271 jChatInput = new JTextField();
272 jChatInput.setPreferredSize(new Dimension(315, 25));
273 jChatInput.setFont(new Font("Dialog", Font.PLAIN, 12));
274 jChatInput.addActionListener(new java.awt.event.ActionListener() {
275 public void actionPerformed(java.awt.event.ActionEvent e) {
276 sendChatCommand();
277 }
278 });
279 }
280 return jChatInput;
281 }
282
283
284
285
286
287
288 private ChatTabs getChatTabs() {
289 if (chatTabs == null) {
290 chatTabs = new ChatTabs(this);
291 }
292 return chatTabs;
293 }
294
295
296
297
298
299
300 private JComboBox<Command> getJComboBoxCommand() {
301 if (jCommandComboBox == null) {
302 jCommandComboBox = new JComboBox<Command>();
303 jCommandComboBox.setPreferredSize(new Dimension(100, 25));
304 jCommandComboBox.addItemListener(new java.awt.event.ItemListener() {
305 public void itemStateChanged(java.awt.event.ItemEvent e) {
306 if (DEBUG)
307 System.out.println("itemStateChanged()");
308 setupComboBoxPlayer();
309 }
310 });
311 for (Command c : Command.values())
312 jCommandComboBox.addItem(c);
313 }
314 return jCommandComboBox;
315 }
316
317
318
319
320
321
322 private JComboBox<String> getJComboBoxPlayer() {
323 if (jComboBoxPlayer == null) {
324 jComboBoxPlayer = new JComboBox<String>();
325 jComboBoxPlayer.setEditable(true);
326 jComboBoxPlayer.setPreferredSize(new Dimension(100, 25));
327 }
328 return jComboBoxPlayer;
329 }
330 }