View Javadoc

1   /*
2    * MChat.java 
3    * Created on 11 nov. 2003
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.network;
23  
24  import java.io.BufferedReader;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  
29  import net.sf.magicproject.ui.MagicUIComponents;
30  import net.sf.magicproject.ui.i18n.LanguageManager;
31  
32  
33  /***
34   * This chat is a thread looking for a chat data in the bigpipe Display
35   * discussion is : $user - $message When a null length message is sent, that
36   * would say "disconnection" since user can't send a null length message. So if
37   * user want to send a null length message nothing is sent. When disconnection
38   * is dectected, a message like " **disconnection **" is printed into the
39   * history text area.
40   * 
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.3
43   */
44  public class MChat {
45  
46  	/***
47  	 * send a specified message to opponent
48  	 * 
49  	 * @param message
50  	 *          is the specified message to send
51  	 */
52  	public void sendMessage(String message) {
53  		MBigPipe.instance.sendChatMessage(message);
54  		MagicUIComponents.chatHistoryText.append(0, message);
55  	}
56  
57  	/***
58  	 * To read a message from the specified input stream
59  	 * 
60  	 * @param in
61  	 *          the input stream
62  	 * @throws IOException
63  	 */
64  	public void newMessage(InputStream in) throws IOException {
65  		try {
66  			MagicUIComponents.chatHistoryText.append(1, new BufferedReader(
67  					new InputStreamReader(in)).readLine());
68  		} catch (IOException e) {
69  			System.out.println(" **disconnection ** in chat");
70  			MagicUIComponents.chatHistoryText.append(1, LanguageManager
71  					.getString("disconnected"));
72  			throw e;
73  		}
74  	}
75  }