View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.firemox.network;
20  
21  import net.sf.firemox.network.message.ChatMessage;
22  import net.sf.firemox.tools.Log;
23  import net.sf.firemox.ui.MagicUIComponents;
24  
25  /***
26   * This chat is a thread looking for a chat data in the big pipe Display
27   * discussion is : $user - $message When a null length message is sent, that
28   * would say "disconnection" since user can't send a null length message. So if
29   * user want to send a null length message nothing is sent. When disconnection
30   * is detected, a message like " **disconnection **" is printed into the history
31   * text area.
32   * 
33   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34   * @since 0.3
35   */
36  public class MChat {
37  
38  	/***
39  	 * Create a new instance private of this class.
40  	 */
41  	private MChat() {
42  		super();
43  	}
44  
45  	/***
46  	 * Send a specified message to opponent and display it into our chat.
47  	 * 
48  	 * @param message
49  	 *          is the specified message to send.
50  	 */
51  	public void sendMessage(String message) {
52  		try {
53  			MagicUIComponents.chatHistoryText.append(0, message);
54  			MBigPipe.instance.send(new ChatMessage(message));
55  		} catch (Throwable t) {
56  			MBigPipe.instance.send(new ChatMessage("<i>" + message
57  					+ "</i> could not be sent : " + t.getMessage()));
58  			Log.error("Message could not be sent", t);
59  		}
60  	}
61  
62  	/***
63  	 * To receive a message.
64  	 * 
65  	 * @param message
66  	 *          the input message.
67  	 */
68  	public void receiveMessage(ChatMessage message) {
69  		MagicUIComponents.chatHistoryText.append(1, message.getText());
70  	}
71  
72  	/***
73  	 * Return the unique instance of this class.
74  	 * 
75  	 * @return the unique instance of this class.
76  	 */
77  	public static MChat getInstance() {
78  		return instance;
79  	}
80  
81  	/***
82  	 * Unique instance of this class.
83  	 */
84  	private final static MChat instance = new MChat();
85  }