View Javadoc
1   /******************************************************************************
2    * ChatPane.java - Component that encapsulates the Chat subsystem.
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright(c) 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.7  2011/06/22 06:13:20  dick
10   * Fix utf-8 copyright.
11   *
12   * Revision 1.6  2011/06/22 06:02:10  dick
13   * Support the new ChatTabs.
14   *
15   * Revision 1.5  2011/05/06 23:16:06  dick
16   * Catch and ignore a scroll exception.
17   *
18   * Revision 1.4  2010/12/22 04:38:18  dick
19   * Select the player that we just maybe added.
20   *
21   * Revision 1.3  2010/03/03 13:12:22  inim
22   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
23   *
24   * Revision 1.2  2010/03/03 12:19:49  inim
25   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
26   *
27   * Revision 1.1  2010/02/04 05:57:53  inim
28   * Mavenized project folder layout
29   *
30   * Revision 1.4  2010/01/25 21:07:24  dick
31   * Allow the program to add players and pre-select which command.
32   *
33   * Revision 1.3  2009/02/14 15:37:51  dick
34   * BuckoFIBS is released under the GNU license.
35   *
36   * Revision 1.2  2009/02/05 05:58:44  dick
37   * Bleh, editing the cvs log to get rid of the toodoo marker.
38   *
39   * Revision 1.1  2009/01/28 19:38:39  dick
40   * package com.buckosoft.fibs.gui.chatWindow becomes com.buckosoft.fibs.BuckoFIBS.gui.chatWindow.
41   *
42   * Revision 1.7  2009/01/28 08:32:30  dick
43   * Get rid of todo tag.
44   *
45   * Revision 1.6  2009/01/22 05:05:37  dick
46   * SEND_CHAT_COMMAND becomes the more generic SEND_COMMAND.
47   *
48   * Revision 1.5  2009/01/16 07:27:10  dick
49   * ChatPane now sends messages.
50   *
51   * Revision 1.4  2009/01/09 07:19:17  dick
52   * Scroll to the bottom on output.
53   *
54   * Revision 1.3  2008/12/11 08:49:57  dick
55   * Inbound chat is being written to the window.
56   *
57   * Revision 1.2  2008/12/07 22:51:23  dick
58   * Javadoc.
59   *
60   * Revision 1.1  2008/10/08 23:06:04  dick
61   * Define the Chat panel.
62   */
63  
64  /* 
65   * This program is free software: you can redistribute it and/or modify
66   * it under the terms of the GNU General Public License as published by
67   * the Free Software Foundation, either version 3 of the License, or
68   * (at your option) any later version.
69   *
70   * This program is distributed in the hope that it will be useful,
71   * but WITHOUT ANY WARRANTY; without even the implied warranty of
72   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
73   * GNU General Public License for more details.
74   *
75   * You should have received a copy of the GNU General Public License
76   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
77   *
78   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
79   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
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  /** Component that encapsulates the Chat subsystem.
98   * @author Dick Balaska
99   * @since 2008/10/01
100  * @version $Revision$ <br> $Date$
101  * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/chatWindow/ChatPane.java">cvs ChatPane.java</a>
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;  //  @jve:decl-index=0:
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 	/** The possible "chat" command types.
117 	 */
118 	public enum Command {
119 		Command,
120 		Kibitz,
121 		Say,
122 		Shout,
123 		Tell,
124 		Whisper
125 	}
126 
127 	/**
128 	 * This is the default constructor
129 	 */
130 	public ChatPane() {
131 		super();
132 		getChatTabs();
133 		initialize();
134 	}
135 
136 	/** Set the reference to the CommandDispatcher
137 	 * @param commandDispatcher The application CommandDispatcher
138 	 */
139 	public void setCommandDispatcher(CommandDispatcher commandDispatcher) {
140 		this.commandDispatcher = commandDispatcher;
141 		chatTabs.setBFProperties(this.commandDispatcher.getProperties());
142 	}
143 
144 	/** Write an incoming chat message to the screen
145 	 * @param name 
146 	 * @param s The message
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 	/** Maybe add this player to the player dropdown box.
172 	 * Add it unless it is a duplicate.
173 	 * @param playerName The name of the player to add to the list
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 	/** Notification when a different ChatOutput is made visible
190 	 */
191 	protected void notifyTabChanged() {
192 		if (chatTabs == null)
193 			return;		// don't die during init
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 	/** This method initializes this */
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 	/** Make the player combobox point to this type
258 	 * @param type The command type to set the combo box to.
259 	 */
260 	public void setupComboBoxCommand(Command type) {
261 		getJComboBoxCommand().setSelectedItem(type);
262 	}
263 
264 	/**
265 	 * This method initializes jChatInput	
266 	 * 	
267 	 * @return javax.swing.JTextField	
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 	 * This method initializes chatOutput	
285 	 * 	
286 	 * @return javax.swing.JScrollPane	
287 	 */
288 	private ChatTabs getChatTabs() {
289 		if (chatTabs == null) {
290 			chatTabs = new ChatTabs(this);
291 		}
292 		return chatTabs;
293 	}
294 
295 	/**
296 	 * This method initializes jCommandComboBox	
297 	 * 	
298 	 * @return javax.swing.JComboBox	
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 	 * This method initializes jComboBoxPlayer	
319 	 * 	
320 	 * @return javax.swing.JComboBox	
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 }  //  @jve:decl-index=0:visual-constraint="10,10"