加强版的带进度条的传文件和加表情的聊天室 -2
1package networking;
2
3import java.awt.*;
4import java.io.*;
5import java.net.*;
6import java.util.*;
7import javax.swing.*;
8import javax.swing.text.*;
9
10/**
11 * client thread that will listent the responses from the server,and update the
12 * information to the gui class
13 *
14 * @author
15 * @version 1.0
16 */
17public class ClientThreadHandler implements Runnable
18{
19 /**
20 * Attributes
21 */
22 private Socket socket;
23 private ObjectInputStream ois;
24 private ClientGUI clientGUI;
25 private ClientLogin clientLogin;
26
27 /**
28 * Constructor
29 */
30 public ClientThreadHandler(Socket socket, ObjectInputStream ois,
31 ClientGUI clientGUI, ClientLogin clientLogin)
32 {
33 this.socket = socket;
34 this.ois = ois;
35 this.clientGUI = clientGUI;
36 this.clientLogin = clientLogin;
37 }
38
39 /*
40 * (non-Javadoc)
41 *
42 * @see java.lang.Runnable#run()
43 */
44 @SuppressWarnings("unchecked")
45 public void run()
46 {
47 while(true)
48 {
49 try
50 {
51 try
52 {
53 DataFrame df = (DataFrame)ois.readObject();
54 if(df.getType().equals("Error"))
55 {
56 String error = (String)df.getObject();
57 if(error.equals("Name already exists"))// name exists
58 {
59 JOptionPane.showMessageDialog(clientLogin
60 .getContentPane(), error, "No Title",
61 JOptionPane.ERROR_MESSAGE);
62 // clientGUI.setVisible(true);
63 clientGUI.dispose();
64 break;
65 }
66 else if(error.equals("IP and port already exist"))// ip
67 // and
68 // port
69 // exist
70 {
71 JOptionPane.showMessageDialog(clientLogin
72 .getContentPane(), error, "No Title",
73 JOptionPane.ERROR_MESSAGE);
74 // clientGUI.setVisible(true);
75 clientGUI.dispose();
76 break;
77 }
78 }
79 else
80 // ok to login
81 {
82 clientLogin.setVisible(false);
83 clientGUI.setVisible(true);
84 }
85 if(df.getType().equals("Message"))// message from the
86 // server
87 {
88 clientGUI.setText((String)df.getObject());
89 }
90 else if(df.getType().startsWith("UserList"))// userlist from
91 // the server
92 {
93 ArrayList<Chatter> al = (ArrayList<Chatter>)df
94 .getObject();
95 clientGUI.setChatters(al);
96 Vector<String> vector = new Vector<String>();
97 vector.addElement("All");// always add "All" option
98 // for the user
99 for(int i = 0; i < al.size(); i++)
100 {
101 vector.addElement(al.get(i).getName() + "-("
102 + al.get(i).getRoom() + ")");
103 }
104 clientGUI.setChatterList(vector);
105 }
106 else if(df.getType().equals("SendFileError"))
107 {
108 RequestFrame rf = (RequestFrame)df.getObject();
109 Map sendingFiles = clientGUI.getSendingFiles();
110 FileSender fs = (FileSender)sendingFiles.get(rf
111 .getSenderKey());
112 fs.setOkToSend(false);
113 fs.getJoeyLabel().setProcessing(false);
114 fs.getJoeyLabel().setText("Send file error,canceled");
115 fs.getJoeyButton().setVisible(false);
116 sendingFiles.remove(rf.getSenderKey());
117 }
118 else if(df.getType().equals("SendFileSectionError"))
119 {
120 FileFrame ff = (FileFrame)df.getObject();
121 Map sendingFiles = clientGUI.getSendingFiles();
122 FileSender fs = (FileSender)sendingFiles.get(ff
123 .getSenderKey());
124 fs.setOkToSend(false);
125 fs.getJoeyLabel().setProcessing(false);
126 fs.getJoeyLabel().setText("Send file error,canceled");
127 fs.getJoeyButton().setVisible(false);
128 sendingFiles.remove(ff.getSenderKey());
129 }
130 else if(df.getType().equals("ReceiveFileRequest"))
131 {
132 RequestFrame rf = (RequestFrame)df.getObject();
133 Document doc = clientGUI.getJTextPane().getDocument();
134 Style style = clientGUI.getJTextPane().getStyle("JoeyLabel");
135 JoeyLabel joeyLabel = new JoeyLabel();
136 joeyLabel.setText("<html>Receive file '"
137 + rf.getFileName() + "' from " + rf.getSender()
138 + "</html>");
139 StyleConstants.setComponent(style, joeyLabel);
140 try
141 {
142 doc.insertString(doc.getLength(), "\n", clientGUI
143 .getJTextPane().getStyle("JoeyLabel"));
144 }
145 catch(BadLocationException ble)
146 {
147 }
148 style = clientGUI.getJTextPane().getStyle("JoeyButton");
149 JoeyButton joeyButton1 = new JoeyButton();
150 joeyButton1.setText("Accept");
151 joeyButton1.setMargin(new Insets(0, 0, 0, 0));
152 StyleConstants.setComponent(style, joeyButton1);
153 try
154 {
155 doc.insertString(doc.getLength(), "\n", clientGUI
156 .getJTextPane().getStyle("JoeyButton"));
157 }
158 catch(BadLocationException ble)
159 {
160 }
161 JoeyButton joeyButton2 = new JoeyButton();
162 joeyButton2.setText("Cancel");
163 joeyButton2.setMargin(new Insets(0, 0, 0, 0));
164 StyleConstants.setComponent(style, joeyButton2);
165 try
166 {
167 doc.insertString(doc.getLength(), "\n", clientGUI
168 .getJTextPane().getStyle("JoeyButton"));
169 }
170 catch(BadLocationException ble)
171 {
172 }
173 FileReceiver fr = new FileReceiver(clientGUI, clientGUI
174 .getClient(), rf,
175 clientGUI.getReceivingFiles(), joeyLabel,
176 joeyButton1, joeyButton2, rf.getFileSize(), rf
177 .getTotalSections(), rf
178 .getPackageSize(), rf.getSenderKey(),
179 rf.getSender(), rf.getReceiver(),rf.getFileName());
180 fr.register();
181 NotifyFrame nf = new NotifyFrame(rf.getSender(), rf
182 .getReceiver(), rf.getSenderKey(), fr.getKey());
183 clientGUI.getClient().send("IGotTheRequest", nf);
184 }
185 else if(df.getType().equals("ReceiveFileAcknowledgement"))
186 {
187 AcknowledgementFrame af = (AcknowledgementFrame)df
188 .getObject();
189 Map sendingFiles = clientGUI.getSendingFiles();
190 FileSender fs = (FileSender)sendingFiles.get(af
191 .getSenderKey());
192 //fs.getJoeyButton().setVisible(false);
193 if(fs != null)
194 {
195 fs.setOkToSend(true);
196 Thread thread = new Thread(
197 new FileSendThreadHandler(fs, af
198 .getReceiverKey()));
199 thread.setPriority(1);
200 thread.start();
201 }
202 }
203 else if(df.getType().equals("ReceiveFileSection"))
204 {
205 FileFrame ff = (FileFrame)df.getObject();
206 Map receivingFiles = clientGUI.getReceivingFiles();
207 FileReceiver fr = (FileReceiver)receivingFiles.get(ff
208 .getReceiverKey());
209 if(fr != null)
210 {
211 fr.receive(ff);
212 }
213 }
214 else if(df.getType().equals("SheGotTheRequest"))
215 {
216 NotifyFrame nf = (NotifyFrame)df.getObject();
217 Map sendingFiles = clientGUI.getSendingFiles();
218 FileSender fs = (FileSender)sendingFiles.get(nf
219 .getSenderKey());
220 if(fs != null)
221 {
222 fs.setReceiverKey(nf.getReceiverKey());
223 }
224 }
225 else if(df.getType().equals(
226 "ReceiveCancelFileSendFromSender"))
227 {
228 CancelFrame cf = (CancelFrame)df.getObject();
229 Map receivingFiles = clientGUI.getReceivingFiles();
230 FileReceiver fr = (FileReceiver)receivingFiles.get(cf
231 .getReceiverKey());
232 if(fr != null)
233 {
234 fr.stop();
235 }
236 }
237 else if(df.getType().equals(
238 "ReceiveCancelFileSendFromReceiver"))
239 {
240 CancelFrame cf = (CancelFrame)df.getObject();
241 Map sendingFiles = clientGUI.getSendingFiles();
242 FileSender fs = (FileSender)sendingFiles.get(cf
243 .getSenderKey());
244 if(fs != null)
245 {
246 fs.stop();
247 }
248 }
249 }
250 catch(Exception e)
251 {
252 socket.close();
253 System.exit(0);
254 }
255 }
256 catch(IOException e)
257 {
258 }
259 }
260 }
261}
262
2
3import java.awt.*;
4import java.io.*;
5import java.net.*;
6import java.util.*;
7import javax.swing.*;
8import javax.swing.text.*;
9
10/**
11 * client thread that will listent the responses from the server,and update the
12 * information to the gui class
13 *
14 * @author
15 * @version 1.0
16 */
17public class ClientThreadHandler implements Runnable
18{
19 /**
20 * Attributes
21 */
22 private Socket socket;
23 private ObjectInputStream ois;
24 private ClientGUI clientGUI;
25 private ClientLogin clientLogin;
26
27 /**
28 * Constructor
29 */
30 public ClientThreadHandler(Socket socket, ObjectInputStream ois,
31 ClientGUI clientGUI, ClientLogin clientLogin)
32 {
33 this.socket = socket;
34 this.ois = ois;
35 this.clientGUI = clientGUI;
36 this.clientLogin = clientLogin;
37 }
38
39 /*
40 * (non-Javadoc)
41 *
42 * @see java.lang.Runnable#run()
43 */
44 @SuppressWarnings("unchecked")
45 public void run()
46 {
47 while(true)
48 {
49 try
50 {
51 try
52 {
53 DataFrame df = (DataFrame)ois.readObject();
54 if(df.getType().equals("Error"))
55 {
56 String error = (String)df.getObject();
57 if(error.equals("Name already exists"))// name exists
58 {
59 JOptionPane.showMessageDialog(clientLogin
60 .getContentPane(), error, "No Title",
61 JOptionPane.ERROR_MESSAGE);
62 // clientGUI.setVisible(true);
63 clientGUI.dispose();
64 break;
65 }
66 else if(error.equals("IP and port already exist"))// ip
67 // and
68 // port
69 // exist
70 {
71 JOptionPane.showMessageDialog(clientLogin
72 .getContentPane(), error, "No Title",
73 JOptionPane.ERROR_MESSAGE);
74 // clientGUI.setVisible(true);
75 clientGUI.dispose();
76 break;
77 }
78 }
79 else
80 // ok to login
81 {
82 clientLogin.setVisible(false);
83 clientGUI.setVisible(true);
84 }
85 if(df.getType().equals("Message"))// message from the
86 // server
87 {
88 clientGUI.setText((String)df.getObject());
89 }
90 else if(df.getType().startsWith("UserList"))// userlist from
91 // the server
92 {
93 ArrayList<Chatter> al = (ArrayList<Chatter>)df
94 .getObject();
95 clientGUI.setChatters(al);
96 Vector<String> vector = new Vector<String>();
97 vector.addElement("All");// always add "All" option
98 // for the user
99 for(int i = 0; i < al.size(); i++)
100 {
101 vector.addElement(al.get(i).getName() + "-("
102 + al.get(i).getRoom() + ")");
103 }
104 clientGUI.setChatterList(vector);
105 }
106 else if(df.getType().equals("SendFileError"))
107 {
108 RequestFrame rf = (RequestFrame)df.getObject();
109 Map sendingFiles = clientGUI.getSendingFiles();
110 FileSender fs = (FileSender)sendingFiles.get(rf
111 .getSenderKey());
112 fs.setOkToSend(false);
113 fs.getJoeyLabel().setProcessing(false);
114 fs.getJoeyLabel().setText("Send file error,canceled");
115 fs.getJoeyButton().setVisible(false);
116 sendingFiles.remove(rf.getSenderKey());
117 }
118 else if(df.getType().equals("SendFileSectionError"))
119 {
120 FileFrame ff = (FileFrame)df.getObject();
121 Map sendingFiles = clientGUI.getSendingFiles();
122 FileSender fs = (FileSender)sendingFiles.get(ff
123 .getSenderKey());
124 fs.setOkToSend(false);
125 fs.getJoeyLabel().setProcessing(false);
126 fs.getJoeyLabel().setText("Send file error,canceled");
127 fs.getJoeyButton().setVisible(false);
128 sendingFiles.remove(ff.getSenderKey());
129 }
130 else if(df.getType().equals("ReceiveFileRequest"))
131 {
132 RequestFrame rf = (RequestFrame)df.getObject();
133 Document doc = clientGUI.getJTextPane().getDocument();
134 Style style = clientGUI.getJTextPane().getStyle("JoeyLabel");
135 JoeyLabel joeyLabel = new JoeyLabel();
136 joeyLabel.setText("<html>Receive file '"
137 + rf.getFileName() + "' from " + rf.getSender()
138 + "</html>");
139 StyleConstants.setComponent(style, joeyLabel);
140 try
141 {
142 doc.insertString(doc.getLength(), "\n", clientGUI
143 .getJTextPane().getStyle("JoeyLabel"));
144 }
145 catch(BadLocationException ble)
146 {
147 }
148 style = clientGUI.getJTextPane().getStyle("JoeyButton");
149 JoeyButton joeyButton1 = new JoeyButton();
150 joeyButton1.setText("Accept");
151 joeyButton1.setMargin(new Insets(0, 0, 0, 0));
152 StyleConstants.setComponent(style, joeyButton1);
153 try
154 {
155 doc.insertString(doc.getLength(), "\n", clientGUI
156 .getJTextPane().getStyle("JoeyButton"));
157 }
158 catch(BadLocationException ble)
159 {
160 }
161 JoeyButton joeyButton2 = new JoeyButton();
162 joeyButton2.setText("Cancel");
163 joeyButton2.setMargin(new Insets(0, 0, 0, 0));
164 StyleConstants.setComponent(style, joeyButton2);
165 try
166 {
167 doc.insertString(doc.getLength(), "\n", clientGUI
168 .getJTextPane().getStyle("JoeyButton"));
169 }
170 catch(BadLocationException ble)
171 {
172 }
173 FileReceiver fr = new FileReceiver(clientGUI, clientGUI
174 .getClient(), rf,
175 clientGUI.getReceivingFiles(), joeyLabel,
176 joeyButton1, joeyButton2, rf.getFileSize(), rf
177 .getTotalSections(), rf
178 .getPackageSize(), rf.getSenderKey(),
179 rf.getSender(), rf.getReceiver(),rf.getFileName());
180 fr.register();
181 NotifyFrame nf = new NotifyFrame(rf.getSender(), rf
182 .getReceiver(), rf.getSenderKey(), fr.getKey());
183 clientGUI.getClient().send("IGotTheRequest", nf);
184 }
185 else if(df.getType().equals("ReceiveFileAcknowledgement"))
186 {
187 AcknowledgementFrame af = (AcknowledgementFrame)df
188 .getObject();
189 Map sendingFiles = clientGUI.getSendingFiles();
190 FileSender fs = (FileSender)sendingFiles.get(af
191 .getSenderKey());
192 //fs.getJoeyButton().setVisible(false);
193 if(fs != null)
194 {
195 fs.setOkToSend(true);
196 Thread thread = new Thread(
197 new FileSendThreadHandler(fs, af
198 .getReceiverKey()));
199 thread.setPriority(1);
200 thread.start();
201 }
202 }
203 else if(df.getType().equals("ReceiveFileSection"))
204 {
205 FileFrame ff = (FileFrame)df.getObject();
206 Map receivingFiles = clientGUI.getReceivingFiles();
207 FileReceiver fr = (FileReceiver)receivingFiles.get(ff
208 .getReceiverKey());
209 if(fr != null)
210 {
211 fr.receive(ff);
212 }
213 }
214 else if(df.getType().equals("SheGotTheRequest"))
215 {
216 NotifyFrame nf = (NotifyFrame)df.getObject();
217 Map sendingFiles = clientGUI.getSendingFiles();
218 FileSender fs = (FileSender)sendingFiles.get(nf
219 .getSenderKey());
220 if(fs != null)
221 {
222 fs.setReceiverKey(nf.getReceiverKey());
223 }
224 }
225 else if(df.getType().equals(
226 "ReceiveCancelFileSendFromSender"))
227 {
228 CancelFrame cf = (CancelFrame)df.getObject();
229 Map receivingFiles = clientGUI.getReceivingFiles();
230 FileReceiver fr = (FileReceiver)receivingFiles.get(cf
231 .getReceiverKey());
232 if(fr != null)
233 {
234 fr.stop();
235 }
236 }
237 else if(df.getType().equals(
238 "ReceiveCancelFileSendFromReceiver"))
239 {
240 CancelFrame cf = (CancelFrame)df.getObject();
241 Map sendingFiles = clientGUI.getSendingFiles();
242 FileSender fs = (FileSender)sendingFiles.get(cf
243 .getSenderKey());
244 if(fs != null)
245 {
246 fs.stop();
247 }
248 }
249 }
250 catch(Exception e)
251 {
252 socket.close();
253 System.exit(0);
254 }
255 }
256 catch(IOException e)
257 {
258 }
259 }
260 }
261}
262
1package networking;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6
7/**
8 * coordinator class that works as a coordinator,which will notice the users
9 * when a new user login or logout,or a user wants to send a message,etc
10 *
11 * @author
12 * @version 1.0
13 */
14public class Coordinator
15{
16 /**
17 * Attributes
18 */
19 private ArrayList<Caller> callers;
20 private MessageCenter mc;
21 public static Coordinator coordinator;
22 /**
23 * Private class that will work as observable
24 * @author
25 * @version 1.0
26 */
27 private class MessageCenter extends Observable
28 {
29 public void setChanged()
30 {
31 super.setChanged();
32 }
33 }
34
35 /**
36 * method that will return all the sockets connected
37 *
38 * @return ArrayList<Socket>
39 */
40 public ArrayList<Socket> getSockets()
41 {
42 ArrayList<Socket> sockets = new ArrayList<Socket>();
43 synchronized(callers)// add a lock,to make sure that when one thread
44 // is dealing with the list,other threads which
45 // want to change the list must wait
46 {
47 for(int i = 0; i < callers.size(); i++)
48 {
49 sockets.add(callers.get(i).getSocket());
50 }
51 }
52 return sockets;
53 }
54
55 /**
56 * Constructor
57 */
58 private Coordinator()
59 {
60 callers = new ArrayList<Caller>();
61 mc = new MessageCenter();
62 }
63
64 /**
65 * Singleton pattern that will only hold one instance of this class
66 *
67 * @return Coordinator
68 */
69 public static Coordinator getCoordinator()
70 {
71 if(coordinator == null)
72 {
73 coordinator = new Coordinator();
74 }
75 return coordinator;
76 }
77 /**
78 * Method that will send private information,like sending file request
79 * @param object
80 * @param correctInformation
81 * @param errorInformation
82 * @param receiver
83 * @param sender
84 */
85 public void sendPrivateInformation(Object object,String correctInformation,String errorInformation,Chatter receiver,
86 Chatter sender)
87 {
88 if(receiver == null)// no such a receiver in the user list
89 {
90 synchronized(callers)// lock to protect the data integrity
91 {
92 for(Caller caller: callers)
93 {
94 if(caller.getSocket().getInetAddress().toString().equals(
95 sender.getIP()))// ip is the same
96 {
97 if(caller.getSocket().getPort() == sender.getPort())// port
98 // is
99 // the
100 // same
101 {
102 try
103 {
104 try
105 {
106 ObjectOutputStream oos = caller
107 .getObjectOutputStream();
108 oos.reset();
109 oos.writeObject(new DataFrame(
110 errorInformation,
111 object));// send error message
112 oos.flush();
113 }
114 catch(InterruptedIOException iioe)
115 {
116 synchronized(this)
117 {
118 Chatter chatter = Secretary
119 .getSecretary().findChatter(
120 caller.getSocket());
121 Secretary.getSecretary().deleteRecord(
122 caller.getSocket());
123 this.notifyOffline(caller, chatter
124 .getRoom());
125 }
126 caller.getSocket().close();
127 }
128 catch(IOException e)
129 {
130 synchronized(this)
131 {
132 Chatter chatter = Secretary
133 .getSecretary().findChatter(
134 caller.getSocket());
135 Secretary.getSecretary().deleteRecord(
136 caller.getSocket());
137 this.notifyOffline(caller, chatter
138 .getRoom());
139 }
140 caller.getSocket().close();
141 }
142 }
143 catch(IOException e)
144 {
145 }
146 }
147 }
148 }
149 }
150 }
151 else
152 // the receiver exists
153 {
154 synchronized(callers)// lock to protect the data integrity
155 {
156 for(Caller caller: callers)
157 {
158 if(caller.getSocket().getInetAddress().toString().equals(
159 receiver.getIP()))
160 {
161 if(caller.getSocket().getPort() == receiver.getPort())
162 {
163 try
164 {
165 try
166 {
167 ObjectOutputStream oos = caller
168 .getObjectOutputStream();
169 oos.reset();
170 oos.writeObject(new DataFrame(
171 correctInformation, object));//send correct message
172 oos.flush();
173 }
174 catch(InterruptedIOException iioe)// time is
175 // up
176 {
177 synchronized(this)
178 {
179 Chatter chatter = Secretary
180 .getSecretary().findChatter(
181 caller.getSocket());
182 Secretary.getSecretary().deleteRecord(
183 caller.getSocket());
184 this.notifyOffline(caller, chatter
185 .getRoom());
186 }
187 caller.getSocket().close();
188 }
189 catch(IOException e)// disconnected
190 {
191 synchronized(this)
192 {
193 Chatter chatter = Secretary
194 .getSecretary().findChatter(
195 caller.getSocket());
196 Secretary.getSecretary().deleteRecord(
197 caller.getSocket());
198 this.notifyOffline(caller, chatter
199 .getRoom());
200 }
201 caller.getSocket().close();
202 }
203 }
204 catch(IOException e)
205 {
206 }
207 }
208 }
209 }
210 }
211 }
212 }
213 /**
214 * method that will send a private message from the sender to the receiver
215 *
216 * @param message
217 * @param receiver
218 * @param sender
219 */
220 public void sendPrivateMessage(String message, Chatter receiver,
221 Chatter sender)
222 {
223 if(message == null)
224 {
225 return;
226 }
227 if(receiver == null)// no such a receiver in the user list
228 {
229 synchronized(callers)// lock to protect the data integrity
230 {
231 for(Caller caller: callers)
232 {
233 if(caller.getSocket().getInetAddress().toString().equals(
234 sender.getIP()))// ip is the same
235 {
236 if(caller.getSocket().getPort() == sender.getPort())// port
237 // is
238 // the
239 // same
240 {
241 try
242 {
243 try
244 {
245 ObjectOutputStream oos = caller
246 .getObjectOutputStream();
247 oos.reset();
248 oos.writeObject(new DataFrame("Error",
249 "No such name"));// send error
250 // message back
251 // to the sender
252 oos.flush();
253 }
254 catch(InterruptedIOException iioe)
255 {
256 synchronized(this)
257 {
258 Chatter chatter = Secretary
259 .getSecretary().findChatter(
260 caller.getSocket());
261 Secretary.getSecretary().deleteRecord(
262 caller.getSocket());
263 this.notifyOffline(caller, chatter
264 .getRoom());
265 }
266 caller.getSocket().close();
267 }
268 catch(IOException e)
269 {
270 synchronized(this)
271 {
272 Chatter chatter = Secretary
273 .getSecretary().findChatter(
274 caller.getSocket());
275 Secretary.getSecretary().deleteRecord(
276 caller.getSocket());
277 this.notifyOffline(caller, chatter
278 .getRoom());
279 }
280 caller.getSocket().close();
281 }
282 }
283 catch(IOException e)
284 {
285 }
286 }
287 }
288 }
289 }
290 }
291 else
292 // the receiver exists
293 {
294 synchronized(callers)// lock to protect the data integrity
295 {
296 for(Caller caller: callers)
297 {
298 if(caller.getSocket().getInetAddress().toString().equals(
299 receiver.getIP()))
300 {
301 if(caller.getSocket().getPort() == receiver.getPort())
302 {
303 try
304 {
305 try
306 {
307 ObjectOutputStream oos = caller
308 .getObjectOutputStream();
309 oos.reset();
310 oos.writeObject(new DataFrame("Message",
311 "From " + sender.getName() + " To "
312 + receiver.getName()
313 + ":\n" + message));// send
314 // the
315 // private
316 // message
317 oos.flush();
318 }
319 catch(InterruptedIOException iioe)// time is
320 // up
321 {
322 synchronized(this)
323 {
324 Chatter chatter = Secretary
325 .getSecretary().findChatter(
326 caller.getSocket());
327 Secretary.getSecretary().deleteRecord(
328 caller.getSocket());
329 this.notifyOffline(caller, chatter
330 .getRoom());
331 }
332 caller.getSocket().close();
333 }
334 catch(IOException e)// disconnected
335 {
336 synchronized(this)
337 {
338 Chatter chatter = Secretary
339 .getSecretary().findChatter(
340 caller.getSocket());
341 Secretary.getSecretary().deleteRecord(
342 caller.getSocket());
343 this.notifyOffline(caller, chatter
344 .getRoom());
345 }
346 caller.getSocket().close();
347 }
348 }
349 catch(IOException e)
350 {
351 }
352 }
353 }
354 }
355 }
356 synchronized(callers)// lock to protect the data integrity
357 {
358 for(Caller caller: callers)
359 {
360 if(caller.getSocket().getInetAddress().toString().equals(
361 sender.getIP()))// ip is the same
362 {
363 if(caller.getSocket().getPort() == sender.getPort())// port
364 // is
365 // the
366 // same
367 {
368 try
369 {
370 try
371 {
372 ObjectOutputStream oos = caller
373 .getObjectOutputStream();
374 oos.reset();
375 oos.writeObject(new DataFrame("Message",
376 "From " + sender.getName() + " To "
377 + receiver.getName()
378 + ":\n" + message));// send
379 // the
380 // private
381 // message
382 oos.flush();
383 }
384 catch(InterruptedIOException iioe)
385 {
386 synchronized(this)
387 {
388 Chatter chatter = Secretary
389 .getSecretary().findChatter(
390 caller.getSocket());
391 Secretary.getSecretary().deleteRecord(
392 caller.getSocket());
393 this.notifyOffline(caller, chatter
394 .getRoom());
395 }
396 caller.getSocket().close();
397 }
398 catch(IOException e)
399 {
400 synchronized(this)
401 {
402 Chatter chatter = Secretary
403 .getSecretary().findChatter(
404 caller.getSocket());
405 Secretary.getSecretary().deleteRecord(
406 caller.getSocket());
407 this.notifyOffline(caller, chatter
408 .getRoom());
409 }
410 caller.getSocket().close();
411 }
412 }
413 catch(IOException e)
414 {
415 }
416 }
417 }
418 }
419 }
420 }
421 }
422 /**
423 * method that will send the public message to all the people in that room
424 *
425 * @param chatter
426 * @param message
427 */
428 public void sendPublicMessage(Chatter chatter, String message)
429 {
430 if(message == null)
431 {
432 return;
433 }
434 ArrayList<Caller> disconnects = new ArrayList<Caller>();
435 synchronized(callers)// lock to protect the data integrity
436 {
437 for(Caller caller: callers)
438 {
439 Chatter targetChatter = Secretary.getSecretary().findChatter(
440 caller.getSocket());
441 if(targetChatter.getRoom().equals(chatter.getRoom()))
442 {
443 try
444 {
445 ObjectOutputStream oos = caller.getObjectOutputStream();
446 oos.reset();
447 oos.writeObject(new DataFrame("Message", "From "
448 + chatter.getName() + " To " + "all" + ":\n"
449 + message));// send the public message
450 oos.flush();
451 }
452 catch(InterruptedIOException iioe)
453 {
454 disconnects.add(caller);
455 }
456 catch(IOException e)
457 {
458 disconnects.add(caller);
459 }
460 }
461 }
462 }
463 for(Caller caller: disconnects)
464 {
465 try
466 {
467 synchronized(this)
468 {
469 Chatter targetChatter = Secretary.getSecretary()
470 .findChatter(caller.getSocket());
471 Secretary.getSecretary().deleteRecord(caller.getSocket());
472 this.notifyOffline(caller, targetChatter.getRoom());// notify
473 // the
474 // users
475 // that
476 // this
477 // user has left
478 }
479 caller.getSocket().close();
480 }
481 catch(IOException e)
482 {
483 }
484 }
485 }
486
487 /**
488 * method that will notify all the people in the user's previous room that
489 * the user has left,and it will notify all the people in the user's next
490 * room that the user has joined,and it will also notify the people in the
491 * hall that the user has changed his room
492 *
493 * @param previousRoom
494 * @param nextRoom
495 */
496 public void updateRooms(String previousRoom, String nextRoom)
497 {
498 synchronized(this.mc)
499 {
500 this.mc.setChanged();
501 this.mc.notifyObservers(previousRoom);
502 this.mc.setChanged();
503 this.mc.notifyObservers(nextRoom);
504 }
505 }
506
507 /**
508 * method that will find the chatter through the socket
509 *
510 * @param socket
511 * @return Caller
512 */
513 public Caller findCaller(Socket socket)
514 {
515 synchronized(callers)
516 {
517 for(Caller caller: callers)
518 {
519 if(caller.getSocket().getInetAddress().equals(
520 socket.getInetAddress())
521 && caller.getSocket().getPort() == socket.getPort())
522 {
523 return caller;
524 }
525 }
526 }
527 return null;
528 }
529
530 /**
531 * method that will notify all the people in the hall that a new user has
532 * joined
533 *
534 * @param caller
535 */
536 public void notifyOnline(Caller caller)
537 {
538 if(caller != null)
539 {
540 synchronized(this)
541 {
542 synchronized(this.callers)
543 {
544 this.callers.add(caller);
545 }
546 synchronized(this.mc)
547 {
548 this.mc.addObserver(caller);
549 this.mc.setChanged();
550 this.mc.notifyObservers("Hall");
551 }
552 }
553 }
554 }
555
556 /**
557 * method that will notify the people in the user's room that he has
558 * left,and it will also notify all the people in the hall that the user has
559 * left
560 *
561 * @param caller
562 * @param room
563 */
564 public void notifyOffline(Caller caller, String room)
565 {
566 if(caller != null)
567 {
568 synchronized(this)
569 {
570 synchronized(this.callers)
571 {
572 this.callers.remove(caller);
573 }
574 synchronized(this.mc)
575 {
576 this.mc.deleteObserver(caller);
577 this.mc.setChanged();
578 this.mc.notifyObservers(room);
579 }
580 }
581 }
582 }
583
584 /**
585 * method that will return the count of all the callers(connected endpoint)
586 *
587 * @return total callers
588 */
589 public int count()
590 {
591 synchronized(this.callers)
592 {
593 return this.callers.size();
594 }
595 }
596}
597
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6
7/**
8 * coordinator class that works as a coordinator,which will notice the users
9 * when a new user login or logout,or a user wants to send a message,etc
10 *
11 * @author
12 * @version 1.0
13 */
14public class Coordinator
15{
16 /**
17 * Attributes
18 */
19 private ArrayList<Caller> callers;
20 private MessageCenter mc;
21 public static Coordinator coordinator;
22 /**
23 * Private class that will work as observable
24 * @author
25 * @version 1.0
26 */
27 private class MessageCenter extends Observable
28 {
29 public void setChanged()
30 {
31 super.setChanged();
32 }
33 }
34
35 /**
36 * method that will return all the sockets connected
37 *
38 * @return ArrayList<Socket>
39 */
40 public ArrayList<Socket> getSockets()
41 {
42 ArrayList<Socket> sockets = new ArrayList<Socket>();
43 synchronized(callers)// add a lock,to make sure that when one thread
44 // is dealing with the list,other threads which
45 // want to change the list must wait
46 {
47 for(int i = 0; i < callers.size(); i++)
48 {
49 sockets.add(callers.get(i).getSocket());
50 }
51 }
52 return sockets;
53 }
54
55 /**
56 * Constructor
57 */
58 private Coordinator()
59 {
60 callers = new ArrayList<Caller>();
61 mc = new MessageCenter();
62 }
63
64 /**
65 * Singleton pattern that will only hold one instance of this class
66 *
67 * @return Coordinator
68 */
69 public static Coordinator getCoordinator()
70 {
71 if(coordinator == null)
72 {
73 coordinator = new Coordinator();
74 }
75 return coordinator;
76 }
77 /**
78 * Method that will send private information,like sending file request
79 * @param object
80 * @param correctInformation
81 * @param errorInformation
82 * @param receiver
83 * @param sender
84 */
85 public void sendPrivateInformation(Object object,String correctInformation,String errorInformation,Chatter receiver,
86 Chatter sender)
87 {
88 if(receiver == null)// no such a receiver in the user list
89 {
90 synchronized(callers)// lock to protect the data integrity
91 {
92 for(Caller caller: callers)
93 {
94 if(caller.getSocket().getInetAddress().toString().equals(
95 sender.getIP()))// ip is the same
96 {
97 if(caller.getSocket().getPort() == sender.getPort())// port
98 // is
99 // the
100 // same
101 {
102 try
103 {
104 try
105 {
106 ObjectOutputStream oos = caller
107 .getObjectOutputStream();
108 oos.reset();
109 oos.writeObject(new DataFrame(
110 errorInformation,
111 object));// send error message
112 oos.flush();
113 }
114 catch(InterruptedIOException iioe)
115 {
116 synchronized(this)
117 {
118 Chatter chatter = Secretary
119 .getSecretary().findChatter(
120 caller.getSocket());
121 Secretary.getSecretary().deleteRecord(
122 caller.getSocket());
123 this.notifyOffline(caller, chatter
124 .getRoom());
125 }
126 caller.getSocket().close();
127 }
128 catch(IOException e)
129 {
130 synchronized(this)
131 {
132 Chatter chatter = Secretary
133 .getSecretary().findChatter(
134 caller.getSocket());
135 Secretary.getSecretary().deleteRecord(
136 caller.getSocket());
137 this.notifyOffline(caller, chatter
138 .getRoom());
139 }
140 caller.getSocket().close();
141 }
142 }
143 catch(IOException e)
144 {
145 }
146 }
147 }
148 }
149 }
150 }
151 else
152 // the receiver exists
153 {
154 synchronized(callers)// lock to protect the data integrity
155 {
156 for(Caller caller: callers)
157 {
158 if(caller.getSocket().getInetAddress().toString().equals(
159 receiver.getIP()))
160 {
161 if(caller.getSocket().getPort() == receiver.getPort())
162 {
163 try
164 {
165 try
166 {
167 ObjectOutputStream oos = caller
168 .getObjectOutputStream();
169 oos.reset();
170 oos.writeObject(new DataFrame(
171 correctInformation, object));//send correct message
172 oos.flush();
173 }
174 catch(InterruptedIOException iioe)// time is
175 // up
176 {
177 synchronized(this)
178 {
179 Chatter chatter = Secretary
180 .getSecretary().findChatter(
181 caller.getSocket());
182 Secretary.getSecretary().deleteRecord(
183 caller.getSocket());
184 this.notifyOffline(caller, chatter
185 .getRoom());
186 }
187 caller.getSocket().close();
188 }
189 catch(IOException e)// disconnected
190 {
191 synchronized(this)
192 {
193 Chatter chatter = Secretary
194 .getSecretary().findChatter(
195 caller.getSocket());
196 Secretary.getSecretary().deleteRecord(
197 caller.getSocket());
198 this.notifyOffline(caller, chatter
199 .getRoom());
200 }
201 caller.getSocket().close();
202 }
203 }
204 catch(IOException e)
205 {
206 }
207 }
208 }
209 }
210 }
211 }
212 }
213 /**
214 * method that will send a private message from the sender to the receiver
215 *
216 * @param message
217 * @param receiver
218 * @param sender
219 */
220 public void sendPrivateMessage(String message, Chatter receiver,
221 Chatter sender)
222 {
223 if(message == null)
224 {
225 return;
226 }
227 if(receiver == null)// no such a receiver in the user list
228 {
229 synchronized(callers)// lock to protect the data integrity
230 {
231 for(Caller caller: callers)
232 {
233 if(caller.getSocket().getInetAddress().toString().equals(
234 sender.getIP()))// ip is the same
235 {
236 if(caller.getSocket().getPort() == sender.getPort())// port
237 // is
238 // the
239 // same
240 {
241 try
242 {
243 try
244 {
245 ObjectOutputStream oos = caller
246 .getObjectOutputStream();
247 oos.reset();
248 oos.writeObject(new DataFrame("Error",
249 "No such name"));// send error
250 // message back
251 // to the sender
252 oos.flush();
253 }
254 catch(InterruptedIOException iioe)
255 {
256 synchronized(this)
257 {
258 Chatter chatter = Secretary
259 .getSecretary().findChatter(
260 caller.getSocket());
261 Secretary.getSecretary().deleteRecord(
262 caller.getSocket());
263 this.notifyOffline(caller, chatter
264 .getRoom());
265 }
266 caller.getSocket().close();
267 }
268 catch(IOException e)
269 {
270 synchronized(this)
271 {
272 Chatter chatter = Secretary
273 .getSecretary().findChatter(
274 caller.getSocket());
275 Secretary.getSecretary().deleteRecord(
276 caller.getSocket());
277 this.notifyOffline(caller, chatter
278 .getRoom());
279 }
280 caller.getSocket().close();
281 }
282 }
283 catch(IOException e)
284 {
285 }
286 }
287 }
288 }
289 }
290 }
291 else
292 // the receiver exists
293 {
294 synchronized(callers)// lock to protect the data integrity
295 {
296 for(Caller caller: callers)
297 {
298 if(caller.getSocket().getInetAddress().toString().equals(
299 receiver.getIP()))
300 {
301 if(caller.getSocket().getPort() == receiver.getPort())
302 {
303 try
304 {
305 try
306 {
307 ObjectOutputStream oos = caller
308 .getObjectOutputStream();
309 oos.reset();
310 oos.writeObject(new DataFrame("Message",
311 "From " + sender.getName() + " To "
312 + receiver.getName()
313 + ":\n" + message));// send
314 // the
315 // private
316 // message
317 oos.flush();
318 }
319 catch(InterruptedIOException iioe)// time is
320 // up
321 {
322 synchronized(this)
323 {
324 Chatter chatter = Secretary
325 .getSecretary().findChatter(
326 caller.getSocket());
327 Secretary.getSecretary().deleteRecord(
328 caller.getSocket());
329 this.notifyOffline(caller, chatter
330 .getRoom());
331 }
332 caller.getSocket().close();
333 }
334 catch(IOException e)// disconnected
335 {
336 synchronized(this)
337 {
338 Chatter chatter = Secretary
339 .getSecretary().findChatter(
340 caller.getSocket());
341 Secretary.getSecretary().deleteRecord(
342 caller.getSocket());
343 this.notifyOffline(caller, chatter
344 .getRoom());
345 }
346 caller.getSocket().close();
347 }
348 }
349 catch(IOException e)
350 {
351 }
352 }
353 }
354 }
355 }
356 synchronized(callers)// lock to protect the data integrity
357 {
358 for(Caller caller: callers)
359 {
360 if(caller.getSocket().getInetAddress().toString().equals(
361 sender.getIP()))// ip is the same
362 {
363 if(caller.getSocket().getPort() == sender.getPort())// port
364 // is
365 // the
366 // same
367 {
368 try
369 {
370 try
371 {
372 ObjectOutputStream oos = caller
373 .getObjectOutputStream();
374 oos.reset();
375 oos.writeObject(new DataFrame("Message",
376 "From " + sender.getName() + " To "
377 + receiver.getName()
378 + ":\n" + message));// send
379 // the
380 // private
381 // message
382 oos.flush();
383 }
384 catch(InterruptedIOException iioe)
385 {
386 synchronized(this)
387 {
388 Chatter chatter = Secretary
389 .getSecretary().findChatter(
390 caller.getSocket());
391 Secretary.getSecretary().deleteRecord(
392 caller.getSocket());
393 this.notifyOffline(caller, chatter
394 .getRoom());
395 }
396 caller.getSocket().close();
397 }
398 catch(IOException e)
399 {
400 synchronized(this)
401 {
402 Chatter chatter = Secretary
403 .getSecretary().findChatter(
404 caller.getSocket());
405 Secretary.getSecretary().deleteRecord(
406 caller.getSocket());
407 this.notifyOffline(caller, chatter
408 .getRoom());
409 }
410 caller.getSocket().close();
411 }
412 }
413 catch(IOException e)
414 {
415 }
416 }
417 }
418 }
419 }
420 }
421 }
422 /**
423 * method that will send the public message to all the people in that room
424 *
425 * @param chatter
426 * @param message
427 */
428 public void sendPublicMessage(Chatter chatter, String message)
429 {
430 if(message == null)
431 {
432 return;
433 }
434 ArrayList<Caller> disconnects = new ArrayList<Caller>();
435 synchronized(callers)// lock to protect the data integrity
436 {
437 for(Caller caller: callers)
438 {
439 Chatter targetChatter = Secretary.getSecretary().findChatter(
440 caller.getSocket());
441 if(targetChatter.getRoom().equals(chatter.getRoom()))
442 {
443 try
444 {
445 ObjectOutputStream oos = caller.getObjectOutputStream();
446 oos.reset();
447 oos.writeObject(new DataFrame("Message", "From "
448 + chatter.getName() + " To " + "all" + ":\n"
449 + message));// send the public message
450 oos.flush();
451 }
452 catch(InterruptedIOException iioe)
453 {
454 disconnects.add(caller);
455 }
456 catch(IOException e)
457 {
458 disconnects.add(caller);
459 }
460 }
461 }
462 }
463 for(Caller caller: disconnects)
464 {
465 try
466 {
467 synchronized(this)
468 {
469 Chatter targetChatter = Secretary.getSecretary()
470 .findChatter(caller.getSocket());
471 Secretary.getSecretary().deleteRecord(caller.getSocket());
472 this.notifyOffline(caller, targetChatter.getRoom());// notify
473 // the
474 // users
475 // that
476 // this
477 // user has left
478 }
479 caller.getSocket().close();
480 }
481 catch(IOException e)
482 {
483 }
484 }
485 }
486
487 /**
488 * method that will notify all the people in the user's previous room that
489 * the user has left,and it will notify all the people in the user's next
490 * room that the user has joined,and it will also notify the people in the
491 * hall that the user has changed his room
492 *
493 * @param previousRoom
494 * @param nextRoom
495 */
496 public void updateRooms(String previousRoom, String nextRoom)
497 {
498 synchronized(this.mc)
499 {
500 this.mc.setChanged();
501 this.mc.notifyObservers(previousRoom);
502 this.mc.setChanged();
503 this.mc.notifyObservers(nextRoom);
504 }
505 }
506
507 /**
508 * method that will find the chatter through the socket
509 *
510 * @param socket
511 * @return Caller
512 */
513 public Caller findCaller(Socket socket)
514 {
515 synchronized(callers)
516 {
517 for(Caller caller: callers)
518 {
519 if(caller.getSocket().getInetAddress().equals(
520 socket.getInetAddress())
521 && caller.getSocket().getPort() == socket.getPort())
522 {
523 return caller;
524 }
525 }
526 }
527 return null;
528 }
529
530 /**
531 * method that will notify all the people in the hall that a new user has
532 * joined
533 *
534 * @param caller
535 */
536 public void notifyOnline(Caller caller)
537 {
538 if(caller != null)
539 {
540 synchronized(this)
541 {
542 synchronized(this.callers)
543 {
544 this.callers.add(caller);
545 }
546 synchronized(this.mc)
547 {
548 this.mc.addObserver(caller);
549 this.mc.setChanged();
550 this.mc.notifyObservers("Hall");
551 }
552 }
553 }
554 }
555
556 /**
557 * method that will notify the people in the user's room that he has
558 * left,and it will also notify all the people in the hall that the user has
559 * left
560 *
561 * @param caller
562 * @param room
563 */
564 public void notifyOffline(Caller caller, String room)
565 {
566 if(caller != null)
567 {
568 synchronized(this)
569 {
570 synchronized(this.callers)
571 {
572 this.callers.remove(caller);
573 }
574 synchronized(this.mc)
575 {
576 this.mc.deleteObserver(caller);
577 this.mc.setChanged();
578 this.mc.notifyObservers(room);
579 }
580 }
581 }
582 }
583
584 /**
585 * method that will return the count of all the callers(connected endpoint)
586 *
587 * @return total callers
588 */
589 public int count()
590 {
591 synchronized(this.callers)
592 {
593 return this.callers.size();
594 }
595 }
596}
597
1package networking;
2
3import java.io.*;
4
5/**
6 * class that will be used to transport through the network
7 *
8 * @author
9 * @version 1.0
10 */
11public class DataFrame implements Serializable
12{
13 /**
14 * Attributes
15 */
16 private static final long serialVersionUID = 1L;
17 private String type;
18 private Object object;
19
20 /**
21 * Constructor
22 */
23 public DataFrame(String type, Object object)
24 {
25 this.type = type;
26 this.object = object;
27 }
28
29 /**
30 * getter
31 *
32 * @return Objecct
33 */
34 public Object getObject()
35 {
36 return this.object;
37 }
38
39 /**
40 * getter
41 *
42 * @return String
43 */
44 public String getType()
45 {
46 return this.type;
47 }
48}
49
2
3import java.io.*;
4
5/**
6 * class that will be used to transport through the network
7 *
8 * @author
9 * @version 1.0
10 */
11public class DataFrame implements Serializable
12{
13 /**
14 * Attributes
15 */
16 private static final long serialVersionUID = 1L;
17 private String type;
18 private Object object;
19
20 /**
21 * Constructor
22 */
23 public DataFrame(String type, Object object)
24 {
25 this.type = type;
26 this.object = object;
27 }
28
29 /**
30 * getter
31 *
32 * @return Objecct
33 */
34 public Object getObject()
35 {
36 return this.object;
37 }
38
39 /**
40 * getter
41 *
42 * @return String
43 */
44 public String getType()
45 {
46 return this.type;
47 }
48}
49
1package networking;
2
3import java.awt.*;
4import javax.swing.*;
5import javax.swing.text.*;
6
7/**
8 * Class that will allow the users to choose emotion pictures
9 * @author
10 * @version 1.0
11 */
12public class EmotionButtons extends JFrame
13{
14 /**
15 * Attributes
16 */
17 private static final long serialVersionUID = 1L;
18 private JPanel jContentPane = null;
19 private JDesktopPane jDesktopPane = null;
20 private JButton jButton = null;
21 private JButton jButton1 = null;
22 private JButton jButton2 = null;
23 private JButton jButton3 = null;
24 private JButton jButton4 = null;
25 private JButton jButton5 = null;
26 private ClientGUI clientGUI = null;
27
28 /**
29 * This is the default constructor
30 */
31 public EmotionButtons(ClientGUI clientGUI)
32 {
33 this.clientGUI = clientGUI;
34 initialize();
35 }
36
37 /**
38 * This method initializes this
39 *
40 * @return void
41 */
42 private void initialize()
43 {
44 this.setSize(757, 424);
45 this.setContentPane(getJContentPane());
46 this.setTitle("JFrame");
47 }
48
49 /**
50 * This method initializes jContentPane
51 *
52 * @return javax.swing.JPanel
53 */
54 private JPanel getJContentPane()
55 {
56 if(jContentPane == null)
57 {
58 jContentPane = new JPanel();
59 jContentPane.setLayout(new BorderLayout());
60 jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
61 }
62 return jContentPane;
63 }
64
65 /**
66 * This method initializes jDesktopPane
67 *
68 * @return javax.swing.JDesktopPane
69 */
70 private JDesktopPane getJDesktopPane()
71 {
72 if(jDesktopPane == null)
73 {
74 jDesktopPane = new JDesktopPane();
75 jDesktopPane.add(getJButton(), null);
76 jDesktopPane.add(getJButton1(), null);
77 jDesktopPane.add(getJButton2(), null);
78 jDesktopPane.add(getJButton3(), null);
79 jDesktopPane.add(getJButton4(), null);
80 jDesktopPane.add(getJButton5(), null);
81 }
82 return jDesktopPane;
83 }
84
85 /**
86 * This method initializes jButton
87 *
88 * @return javax.swing.JButton
89 */
90 private JButton getJButton()
91 {
92 if(jButton == null)
93 {
94 jButton = new JButton();
95 jButton.setBounds(new Rectangle(457, 80, 290, 307));
96 jButton.setBorder(null);
97 jButton.setIcon(new ImageIcon("../res/wodao.gif"));
98 jButton.addActionListener(new java.awt.event.ActionListener()
99 {
100 public void actionPerformed(java.awt.event.ActionEvent e)
101 {
102 Document doc = clientGUI.getJContentTextArea()
103 .getDocument();
104 try
105 {
106 doc.insertString(doc.getLength(), "[em01]", clientGUI
107 .getJContentTextArea().getStyle("[em01]"));
108 EmotionButtons.this.dispose();
109 }
110 catch(BadLocationException ble)
111 {
112 }
113 }
114 });
115 }
116 return jButton;
117 }
118
119 /**
120 * This method initializes jButton1
121 *
122 * @return javax.swing.JButton
123 */
124 private JButton getJButton1()
125 {
126 if(jButton1 == null)
127 {
128 jButton1 = new JButton();
129 jButton1.setBounds(new Rectangle(6, 4, 280, 249));
130 jButton1.setBorder(null);
131 jButton1.setIcon(new ImageIcon("../res/daqiang.gif"));
132 jButton1.addActionListener(new java.awt.event.ActionListener()
133 {
134 public void actionPerformed(java.awt.event.ActionEvent e)
135 {
136 Document doc = clientGUI.getJContentTextArea()
137 .getDocument();
138 try
139 {
140 doc.insertString(doc.getLength(), "[em02]", clientGUI
141 .getJContentTextArea().getStyle("[em02]"));
142 EmotionButtons.this.dispose();
143 }
144 catch(BadLocationException ble)
145 {
146 }
147 }
148 });
149 }
150 return jButton1;
151 }
152
153 /**
154 * This method initializes jButton2
155 *
156 * @return javax.swing.JButton
157 */
158 private JButton getJButton2()
159 {
160 if(jButton2 == null)
161 {
162 jButton2 = new JButton();
163 jButton2.setBounds(new Rectangle(313, 11, 100, 100));
164 jButton2.setBorder(null);
165 jButton2.setIcon(new ImageIcon("../res/zhaobian.jpg"));
166 jButton2.addActionListener(new java.awt.event.ActionListener()
167 {
168 public void actionPerformed(java.awt.event.ActionEvent e)
169 {
170 Document doc = clientGUI.getJContentTextArea()
171 .getDocument();
172 try
173 {
174 doc.insertString(doc.getLength(), "[em03]", clientGUI
175 .getJContentTextArea().getStyle("[em03]"));
176 EmotionButtons.this.dispose();
177 }
178 catch(BadLocationException ble)
179 {
180 }
181 }
182 });
183 }
184 return jButton2;
185 }
186
187 /**
188 * This method initializes jButton3
189 *
190 * @return javax.swing.JButton
191 */
192 private JButton getJButton3()
193 {
194 if(jButton3 == null)
195 {
196 jButton3 = new JButton();
197 jButton3.setBounds(new Rectangle(312, 257, 100, 120));
198 jButton3.setBorder(null);
199 jButton3.setIcon(new ImageIcon("../res/shi.gif"));
200 jButton3.addActionListener(new java.awt.event.ActionListener()
201 {
202 public void actionPerformed(java.awt.event.ActionEvent e)
203 {
204 Document doc = clientGUI.getJContentTextArea()
205 .getDocument();
206 try
207 {
208 doc.insertString(doc.getLength(), "[em04]", clientGUI
209 .getJContentTextArea().getStyle("[em04]"));
210 EmotionButtons.this.dispose();
211 }
212 catch(BadLocationException ble)
213 {
214 }
215 }
216 });
217 }
218 return jButton3;
219 }
220
221 /**
222 * This method initializes jButton4
223 *
224 * @return javax.swing.JButton
225 */
226 private JButton getJButton4()
227 {
228 if(jButton4 == null)
229 {
230 jButton4 = new JButton();
231 jButton4.setBounds(new Rectangle(152, 285, 105, 92));
232 jButton4.setBorder(null);
233 jButton4.setIcon(new ImageIcon("../res/buhuiba.jpg"));
234 jButton4.addActionListener(new java.awt.event.ActionListener()
235 {
236 public void actionPerformed(java.awt.event.ActionEvent e)
237 {
238 Document doc = clientGUI.getJContentTextArea()
239 .getDocument();
240 try
241 {
242 doc.insertString(doc.getLength(), "[em05]", clientGUI
243 .getJContentTextArea().getStyle("[em05]"));
244 EmotionButtons.this.dispose();
245 }
246 catch(BadLocationException ble)
247 {
248 }
249 }
250 });
251 }
252 return jButton4;
253 }
254
255 /**
256 * This method initializes jButton5
257 *
258 * @return javax.swing.JButton
259 */
260 private JButton getJButton5()
261 {
262 if(jButton5 == null)
263 {
264 jButton5 = new JButton();
265 jButton5.setBounds(new Rectangle(302, 127, 128, 128));
266 jButton5.setBorder(null);
267 jButton5.setIcon(new ImageIcon("../res/buyaofanwo.gif"));
268 jButton5.addActionListener(new java.awt.event.ActionListener()
269 {
270 public void actionPerformed(java.awt.event.ActionEvent e)
271 {
272 Document doc = clientGUI.getJContentTextArea()
273 .getDocument();
274 try
275 {
276 doc.insertString(doc.getLength(), "[em06]", clientGUI
277 .getJContentTextArea().getStyle("[em06]"));
278 EmotionButtons.this.dispose();
279 }
280 catch(BadLocationException ble)
281 {
282 }
283 }
284 });
285 }
286 return jButton5;
287 }
288}
289
2
3import java.awt.*;
4import javax.swing.*;
5import javax.swing.text.*;
6
7/**
8 * Class that will allow the users to choose emotion pictures
9 * @author
10 * @version 1.0
11 */
12public class EmotionButtons extends JFrame
13{
14 /**
15 * Attributes
16 */
17 private static final long serialVersionUID = 1L;
18 private JPanel jContentPane = null;
19 private JDesktopPane jDesktopPane = null;
20 private JButton jButton = null;
21 private JButton jButton1 = null;
22 private JButton jButton2 = null;
23 private JButton jButton3 = null;
24 private JButton jButton4 = null;
25 private JButton jButton5 = null;
26 private ClientGUI clientGUI = null;
27
28 /**
29 * This is the default constructor
30 */
31 public EmotionButtons(ClientGUI clientGUI)
32 {
33 this.clientGUI = clientGUI;
34 initialize();
35 }
36
37 /**
38 * This method initializes this
39 *
40 * @return void
41 */
42 private void initialize()
43 {
44 this.setSize(757, 424);
45 this.setContentPane(getJContentPane());
46 this.setTitle("JFrame");
47 }
48
49 /**
50 * This method initializes jContentPane
51 *
52 * @return javax.swing.JPanel
53 */
54 private JPanel getJContentPane()
55 {
56 if(jContentPane == null)
57 {
58 jContentPane = new JPanel();
59 jContentPane.setLayout(new BorderLayout());
60 jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
61 }
62 return jContentPane;
63 }
64
65 /**
66 * This method initializes jDesktopPane
67 *
68 * @return javax.swing.JDesktopPane
69 */
70 private JDesktopPane getJDesktopPane()
71 {
72 if(jDesktopPane == null)
73 {
74 jDesktopPane = new JDesktopPane();
75 jDesktopPane.add(getJButton(), null);
76 jDesktopPane.add(getJButton1(), null);
77 jDesktopPane.add(getJButton2(), null);
78 jDesktopPane.add(getJButton3(), null);
79 jDesktopPane.add(getJButton4(), null);
80 jDesktopPane.add(getJButton5(), null);
81 }
82 return jDesktopPane;
83 }
84
85 /**
86 * This method initializes jButton
87 *
88 * @return javax.swing.JButton
89 */
90 private JButton getJButton()
91 {
92 if(jButton == null)
93 {
94 jButton = new JButton();
95 jButton.setBounds(new Rectangle(457, 80, 290, 307));
96 jButton.setBorder(null);
97 jButton.setIcon(new ImageIcon("../res/wodao.gif"));
98 jButton.addActionListener(new java.awt.event.ActionListener()
99 {
100 public void actionPerformed(java.awt.event.ActionEvent e)
101 {
102 Document doc = clientGUI.getJContentTextArea()
103 .getDocument();
104 try
105 {
106 doc.insertString(doc.getLength(), "[em01]", clientGUI
107 .getJContentTextArea().getStyle("[em01]"));
108 EmotionButtons.this.dispose();
109 }
110 catch(BadLocationException ble)
111 {
112 }
113 }
114 });
115 }
116 return jButton;
117 }
118
119 /**
120 * This method initializes jButton1
121 *
122 * @return javax.swing.JButton
123 */
124 private JButton getJButton1()
125 {
126 if(jButton1 == null)
127 {
128 jButton1 = new JButton();
129 jButton1.setBounds(new Rectangle(6, 4, 280, 249));
130 jButton1.setBorder(null);
131 jButton1.setIcon(new ImageIcon("../res/daqiang.gif"));
132 jButton1.addActionListener(new java.awt.event.ActionListener()
133 {
134 public void actionPerformed(java.awt.event.ActionEvent e)
135 {
136 Document doc = clientGUI.getJContentTextArea()
137 .getDocument();
138 try
139 {
140 doc.insertString(doc.getLength(), "[em02]", clientGUI
141 .getJContentTextArea().getStyle("[em02]"));
142 EmotionButtons.this.dispose();
143 }
144 catch(BadLocationException ble)
145 {
146 }
147 }
148 });
149 }
150 return jButton1;
151 }
152
153 /**
154 * This method initializes jButton2
155 *
156 * @return javax.swing.JButton
157 */
158 private JButton getJButton2()
159 {
160 if(jButton2 == null)
161 {
162 jButton2 = new JButton();
163 jButton2.setBounds(new Rectangle(313, 11, 100, 100));
164 jButton2.setBorder(null);
165 jButton2.setIcon(new ImageIcon("../res/zhaobian.jpg"));
166 jButton2.addActionListener(new java.awt.event.ActionListener()
167 {
168 public void actionPerformed(java.awt.event.ActionEvent e)
169 {
170 Document doc = clientGUI.getJContentTextArea()
171 .getDocument();
172 try
173 {
174 doc.insertString(doc.getLength(), "[em03]", clientGUI
175 .getJContentTextArea().getStyle("[em03]"));
176 EmotionButtons.this.dispose();
177 }
178 catch(BadLocationException ble)
179 {
180 }
181 }
182 });
183 }
184 return jButton2;
185 }
186
187 /**
188 * This method initializes jButton3
189 *
190 * @return javax.swing.JButton
191 */
192 private JButton getJButton3()
193 {
194 if(jButton3 == null)
195 {
196 jButton3 = new JButton();
197 jButton3.setBounds(new Rectangle(312, 257, 100, 120));
198 jButton3.setBorder(null);
199 jButton3.setIcon(new ImageIcon("../res/shi.gif"));
200 jButton3.addActionListener(new java.awt.event.ActionListener()
201 {
202 public void actionPerformed(java.awt.event.ActionEvent e)
203 {
204 Document doc = clientGUI.getJContentTextArea()
205 .getDocument();
206 try
207 {
208 doc.insertString(doc.getLength(), "[em04]", clientGUI
209 .getJContentTextArea().getStyle("[em04]"));
210 EmotionButtons.this.dispose();
211 }
212 catch(BadLocationException ble)
213 {
214 }
215 }
216 });
217 }
218 return jButton3;
219 }
220
221 /**
222 * This method initializes jButton4
223 *
224 * @return javax.swing.JButton
225 */
226 private JButton getJButton4()
227 {
228 if(jButton4 == null)
229 {
230 jButton4 = new JButton();
231 jButton4.setBounds(new Rectangle(152, 285, 105, 92));
232 jButton4.setBorder(null);
233 jButton4.setIcon(new ImageIcon("../res/buhuiba.jpg"));
234 jButton4.addActionListener(new java.awt.event.ActionListener()
235 {
236 public void actionPerformed(java.awt.event.ActionEvent e)
237 {
238 Document doc = clientGUI.getJContentTextArea()
239 .getDocument();
240 try
241 {
242 doc.insertString(doc.getLength(), "[em05]", clientGUI
243 .getJContentTextArea().getStyle("[em05]"));
244 EmotionButtons.this.dispose();
245 }
246 catch(BadLocationException ble)
247 {
248 }
249 }
250 });
251 }
252 return jButton4;
253 }
254
255 /**
256 * This method initializes jButton5
257 *
258 * @return javax.swing.JButton
259 */
260 private JButton getJButton5()
261 {
262 if(jButton5 == null)
263 {
264 jButton5 = new JButton();
265 jButton5.setBounds(new Rectangle(302, 127, 128, 128));
266 jButton5.setBorder(null);
267 jButton5.setIcon(new ImageIcon("../res/buyaofanwo.gif"));
268 jButton5.addActionListener(new java.awt.event.ActionListener()
269 {
270 public void actionPerformed(java.awt.event.ActionEvent e)
271 {
272 Document doc = clientGUI.getJContentTextArea()
273 .getDocument();
274 try
275 {
276 doc.insertString(doc.getLength(), "[em06]", clientGUI
277 .getJContentTextArea().getStyle("[em06]"));
278 EmotionButtons.this.dispose();
279 }
280 catch(BadLocationException ble)
281 {
282 }
283 }
284 });
285 }
286 return jButton5;
287 }
288}
289