加强版的带进度条的传文件和加表情的聊天室
1
package networking;
2
3
import java.io.*;
4
5
/**
6
* Class that will contain information about the acknowledgement about sending file
7
* @author
8
* @version 1.0
9
*/
10
public class AcknowledgementFrame implements Serializable
11
{
12
/**
13
* Attributes
14
*/
15
private static final long serialVersionUID = 1L;
16
private String sender;
17
private String receiver;
18
private long senderKey;
19
private long receiverKey;
20
/**
21
* Constructor
22
*/
23
public AcknowledgementFrame(String sender, String receiver, long senderKey, long receiverKey)
24
{
25
this.sender = sender;
26
this.receiver = receiver;
27
this.senderKey = senderKey;
28
this.receiverKey = receiverKey;
29
}
30
/**
31
* Class attribute getter
32
* @return the receiver
33
*/
34
public String getReceiver()
35
{
36
return this.receiver;
37
}
38
/**
39
* Class attribute setter
40
* @param receiver the receiver to set
41
*/
42
public void setReceiver(String receiver)
43
{
44
this.receiver = receiver;
45
}
46
/**
47
* Class attribute getter
48
* @return the receiverKey
49
*/
50
public long getReceiverKey()
51
{
52
return this.receiverKey;
53
}
54
/**
55
* Class attribute setter
56
* @param receiverKey the receiverKey to set
57
*/
58
public void setReceiverKey(long receiverKey)
59
{
60
this.receiverKey = receiverKey;
61
}
62
/**
63
* Class attribute getter
64
* @return the sender
65
*/
66
public String getSender()
67
{
68
return this.sender;
69
}
70
/**
71
* Class attribute setter
72
* @param sender the sender to set
73
*/
74
public void setSender(String sender)
75
{
76
this.sender = sender;
77
}
78
/**
79
* Class attribute getter
80
* @return the senderKey
81
*/
82
public long getSenderKey()
83
{
84
return this.senderKey;
85
}
86
/**
87
* Class attribute setter
88
* @param senderKey the senderKey to set
89
*/
90
public void setSenderKey(long senderKey)
91
{
92
this.senderKey = senderKey;
93
}
94
}
95
package networking;2

3
import java.io.*;4

5
/**6
* Class that will contain information about the acknowledgement about sending file7
* @author 8
* @version 1.09
*/10
public class AcknowledgementFrame implements Serializable11
{12
/**13
* Attributes14
*/15
private static final long serialVersionUID = 1L;16
private String sender;17
private String receiver;18
private long senderKey;19
private long receiverKey;20
/**21
* Constructor22
*/23
public AcknowledgementFrame(String sender, String receiver, long senderKey, long receiverKey)24
{25
this.sender = sender;26
this.receiver = receiver;27
this.senderKey = senderKey;28
this.receiverKey = receiverKey;29
}30
/**31
* Class attribute getter32
* @return the receiver33
*/34
public String getReceiver()35
{36
return this.receiver;37
}38
/**39
* Class attribute setter40
* @param receiver the receiver to set41
*/42
public void setReceiver(String receiver)43
{44
this.receiver = receiver;45
}46
/**47
* Class attribute getter48
* @return the receiverKey49
*/50
public long getReceiverKey()51
{52
return this.receiverKey;53
}54
/**55
* Class attribute setter56
* @param receiverKey the receiverKey to set57
*/58
public void setReceiverKey(long receiverKey)59
{60
this.receiverKey = receiverKey;61
}62
/**63
* Class attribute getter64
* @return the sender65
*/66
public String getSender()67
{68
return this.sender;69
}70
/**71
* Class attribute setter72
* @param sender the sender to set73
*/74
public void setSender(String sender)75
{76
this.sender = sender;77
}78
/**79
* Class attribute getter80
* @return the senderKey81
*/82
public long getSenderKey()83
{84
return this.senderKey;85
}86
/**87
* Class attribute setter88
* @param senderKey the senderKey to set89
*/90
public void setSenderKey(long senderKey)91
{92
this.senderKey = senderKey;93
}94
}95

1
package networking;
2
3
import java.io.*;
4
import java.net.*;
5
import java.util.*;
6
7
/**
8
* This class implements Observer,which will send the updated users' list back
9
* to the user
10
*
11
* @author
12
* @version 1.0
13
*/
14
public class Caller implements Observer
15
{
16
/**
17
* Attributes
18
*/
19
private Socket socket;
20
private ObjectOutputStream oos;
21
22
/**
23
* Constructor
24
*/
25
public Caller(Socket socket, ObjectOutputStream oos)
26
{
27
this.socket = socket;
28
this.oos = oos;
29
}
30
31
/**
32
* getter
33
*
34
* @return Socket
35
*/
36
public Socket getSocket()
37
{
38
return this.socket;
39
}
40
41
/**
42
* getter
43
*
44
* @return ObjectOutputStream
45
*/
46
public ObjectOutputStream getObjectOutputStream()
47
{
48
return this.oos;
49
}
50
51
/*
52
* (non-Javadoc)
53
*
54
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)
55
*/
56
public void update(Observable observable, Object object)
57
{
58
try
59
{
60
String room = (String)object;
61
Chatter chatter = Secretary.getSecretary().findChatter(socket);
62
if(chatter.getRoom().equals("Hall"))// if it is hall,send out
63
// the full list
64
{
65
try
66
{
67
DataFrame df = new DataFrame("UserList", Secretary
68
.getSecretary().getRegister());// full list
69
oos.reset();// clean cache of objectoutputstream
70
oos.writeObject(df);
71
oos.flush();
72
}
73
catch(InterruptedIOException iioe)
74
{
75
Secretary.getSecretary().deleteRecord(socket);
76
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());
77
socket.close();
78
}
79
catch(IOException e)
80
{
81
Secretary.getSecretary().deleteRecord(socket);
82
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());
83
socket.close();
84
}
85
}
86
else if(chatter.getRoom().equals(room))// notice the people in
87
// the same room that a
88
// guy logined in or
89
// logined out
90
{
91
try
92
{
93
DataFrame df = new DataFrame("UserList", Secretary
94
.getSecretary().getRegister(room));// only
95
// return
96
// the list
97
// of people
98
// in this
99
// room
100
oos.reset();
101
oos.writeObject(df);
102
oos.flush();
103
}
104
catch(InterruptedIOException iioe)// no response during
105
// the time
106
{
107
Secretary.getSecretary().deleteRecord(socket);
108
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());
109
socket.close();
110
}
111
catch(IOException e)// network problem
112
{
113
Secretary.getSecretary().deleteRecord(socket);
114
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());
115
socket.close();
116
}
117
}
118
}
119
catch(IOException e)// catch for exception that might be thrown from
120
// socket.close method
121
{
122
}
123
}
124
}
125
package networking;2

3
import java.io.*;4
import java.net.*;5
import java.util.*;6

7
/**8
* This class implements Observer,which will send the updated users' list back9
* to the user10
* 11
* @author 12
* @version 1.013
*/14
public class Caller implements Observer15
{16
/**17
* Attributes18
*/19
private Socket socket;20
private ObjectOutputStream oos;21
22
/**23
* Constructor24
*/25
public Caller(Socket socket, ObjectOutputStream oos)26
{27
this.socket = socket;28
this.oos = oos;29
}30
31
/**32
* getter33
* 34
* @return Socket35
*/36
public Socket getSocket()37
{38
return this.socket;39
}40
41
/**42
* getter43
* 44
* @return ObjectOutputStream45
*/46
public ObjectOutputStream getObjectOutputStream()47
{48
return this.oos;49
}50
51
/*52
* (non-Javadoc)53
* 54
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)55
*/56
public void update(Observable observable, Object object)57
{58
try59
{60
String room = (String)object;61
Chatter chatter = Secretary.getSecretary().findChatter(socket);62
if(chatter.getRoom().equals("Hall"))// if it is hall,send out63
// the full list64
{65
try66
{67
DataFrame df = new DataFrame("UserList", Secretary68
.getSecretary().getRegister());// full list69
oos.reset();// clean cache of objectoutputstream70
oos.writeObject(df);71
oos.flush();72
}73
catch(InterruptedIOException iioe)74
{75
Secretary.getSecretary().deleteRecord(socket);76
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());77
socket.close();78
}79
catch(IOException e)80
{81
Secretary.getSecretary().deleteRecord(socket);82
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());83
socket.close();84
}85
}86
else if(chatter.getRoom().equals(room))// notice the people in87
// the same room that a88
// guy logined in or89
// logined out90
{91
try92
{93
DataFrame df = new DataFrame("UserList", Secretary94
.getSecretary().getRegister(room));// only95
// return96
// the list97
// of people98
// in this99
// room100
oos.reset();101
oos.writeObject(df);102
oos.flush();103
}104
catch(InterruptedIOException iioe)// no response during105
// the time106
{107
Secretary.getSecretary().deleteRecord(socket);108
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());109
socket.close();110
}111
catch(IOException e)// network problem112
{113
Secretary.getSecretary().deleteRecord(socket);114
Coordinator.getCoordinator().notifyOffline(this,chatter.getRoom());115
socket.close();116
}117
}118
}119
catch(IOException e)// catch for exception that might be thrown from120
// socket.close method121
{122
}123
}124
}125

1
package networking;
2
3
import java.io.*;
4
5
/**
6
* Class that will contain information about the cancel acknowledgement
7
* @author
8
* @version 1.0
9
*/
10
public class CancelFrame implements Serializable
11
{
12
/**
13
* Attributes
14
*/
15
private static final long serialVersionUID = 1L;
16
private String sender;
17
private String receiver;
18
private long senderKey;
19
private long receiverKey;
20
/**
21
* Constructor
22
*/
23
public CancelFrame(String sender, String receiver, long senderKey,long receiverKey)
24
{
25
this.sender = sender;
26
this.receiver = receiver;
27
this.senderKey = senderKey;
28
this.receiverKey=receiverKey;
29
}
30
/**
31
* Class attribute getter
32
* @return the receiverKey
33
*/
34
public long getReceiverKey()
35
{
36
return this.receiverKey;
37
}
38
/**
39
* Class attribute setter
40
* @param receiverKey the receiverKey to set
41
*/
42
public void setReceiverKey(long receiverKey)
43
{
44
this.receiverKey = receiverKey;
45
}
46
/**
47
* Class attribute getter
48
* @return the receiver
49
*/
50
public String getReceiver()
51
{
52
return this.receiver;
53
}
54
/**
55
* Class attribute setter
56
* @param receiver the receiver to set
57
*/
58
public void setReceiver(String receiver)
59
{
60
this.receiver = receiver;
61
}
62
/**
63
* Class attribute getter
64
* @return the sender
65
*/
66
public String getSender()
67
{
68
return this.sender;
69
}
70
/**
71
* Class attribute setter
72
* @param sender the sender to set
73
*/
74
public void setSender(String sender)
75
{
76
this.sender = sender;
77
}
78
/**
79
* Class attribute getter
80
* @return the senderKey
81
*/
82
public long getSenderKey()
83
{
84
return this.senderKey;
85
}
86
/**
87
* Class attribute setter
88
* @param senderKey the senderKey to set
89
*/
90
public void setSenderKey(long senderKey)
91
{
92
this.senderKey = senderKey;
93
}
94
}
95
package networking;2

3
import java.io.*;4

5
/**6
* Class that will contain information about the cancel acknowledgement7
* @author 8
* @version 1.09
*/10
public class CancelFrame implements Serializable11
{12
/**13
* Attributes14
*/15
private static final long serialVersionUID = 1L;16
private String sender;17
private String receiver;18
private long senderKey;19
private long receiverKey;20
/**21
* Constructor22
*/23
public CancelFrame(String sender, String receiver, long senderKey,long receiverKey)24
{25
this.sender = sender;26
this.receiver = receiver;27
this.senderKey = senderKey;28
this.receiverKey=receiverKey;29
}30
/**31
* Class attribute getter32
* @return the receiverKey33
*/34
public long getReceiverKey()35
{36
return this.receiverKey;37
}38
/**39
* Class attribute setter40
* @param receiverKey the receiverKey to set41
*/42
public void setReceiverKey(long receiverKey)43
{44
this.receiverKey = receiverKey;45
}46
/**47
* Class attribute getter48
* @return the receiver49
*/50
public String getReceiver()51
{52
return this.receiver;53
}54
/**55
* Class attribute setter56
* @param receiver the receiver to set57
*/58
public void setReceiver(String receiver)59
{60
this.receiver = receiver;61
}62
/**63
* Class attribute getter64
* @return the sender65
*/66
public String getSender()67
{68
return this.sender;69
}70
/**71
* Class attribute setter72
* @param sender the sender to set73
*/74
public void setSender(String sender)75
{76
this.sender = sender;77
}78
/**79
* Class attribute getter80
* @return the senderKey81
*/82
public long getSenderKey()83
{84
return this.senderKey;85
}86
/**87
* Class attribute setter88
* @param senderKey the senderKey to set89
*/90
public void setSenderKey(long senderKey)91
{92
this.senderKey = senderKey;93
}94
}95

1
package networking;
2
3
import java.io.*;
4
import java.net.*;
5
6
/**
7
* Chatter class will hold the chatter's information,it implements
8
* Serializable,and it will be transported though network
9
*
10
* @author
11
* @version 1.0
12
*/
13
public class Chatter implements Serializable
14
{
15
/**
16
* Attributes
17
*/
18
private static final long serialVersionUID = 1L;
19
/**
20
* Attributes
21
*/
22
private String ip;
23
private int port;
24
private String name;
25
private String room;
26
27
/**
28
* Constructor
29
*/
30
public Chatter(Socket socket, String name)
31
{
32
this.ip = socket.getInetAddress().toString();
33
this.port = socket.getPort();
34
this.name = name;
35
this.room = "Hall";
36
}
37
38
/**
39
* getter
40
*
41
* @return String
42
*/
43
public String getRoom()
44
{
45
return this.room;
46
}
47
48
/**
49
* setter
50
*
51
* @param room
52
*/
53
public void setRoom(String room)
54
{
55
this.room = room;
56
}
57
58
/**
59
* getter
60
*
61
* @return String
62
*/
63
public String getIP()
64
{
65
return this.ip;
66
}
67
68
/**
69
* getter
70
*
71
* @return String
72
*/
73
public String getName()
74
{
75
return this.name;
76
}
77
78
/**
79
* getter
80
*
81
* @return int
82
*/
83
public int getPort()
84
{
85
return this.port;
86
}
87
}
88
package networking;2

3
import java.io.*;4
import java.net.*;5

6
/**7
* Chatter class will hold the chatter's information,it implements8
* Serializable,and it will be transported though network9
* 10
* @author 11
* @version 1.012
*/13
public class Chatter implements Serializable14
{15
/**16
* Attributes17
*/18
private static final long serialVersionUID = 1L;19
/**20
* Attributes21
*/22
private String ip;23
private int port;24
private String name;25
private String room;26
27
/**28
* Constructor29
*/30
public Chatter(Socket socket, String name)31
{32
this.ip = socket.getInetAddress().toString();33
this.port = socket.getPort();34
this.name = name;35
this.room = "Hall";36
}37
38
/**39
* getter40
* 41
* @return String42
*/43
public String getRoom()44
{45
return this.room;46
}47
48
/**49
* setter50
* 51
* @param room52
*/53
public void setRoom(String room)54
{55
this.room = room;56
}57
58
/**59
* getter60
* 61
* @return String62
*/63
public String getIP()64
{65
return this.ip;66
}67
68
/**69
* getter70
* 71
* @return String72
*/73
public String getName()74
{75
return this.name;76
}77
78
/**79
* getter80
* 81
* @return int82
*/83
public int getPort()84
{85
return this.port;86
}87
}88

1
package networking;
2
3
import java.io.*;
4
import java.net.*;
5
6
/**
7
* client class,that will create the connection and send data use socket
8
*
9
* @author
10
* @version 1.0
11
*/
12
public class Client
13
{
14
/**
15
* Attributes
16
*/
17
private Socket socket;
18
private ObjectInputStream ois;
19
private ObjectOutputStream oos;
20
private static final int TIMEOUT_LENGTH = 1000 * 60 * 60;// max timeout
21
22
// number,60
23
// minutes
24
/**
25
* Constructor
26
*/
27
public Client(String ip, int port) throws Exception
28
{
29
socket = new Socket(ip, port);
30
socket.setSoTimeout(TIMEOUT_LENGTH);
31
oos = new ObjectOutputStream(socket.getOutputStream());
32
ois = new ObjectInputStream(socket.getInputStream());
33
}
34
35
/**
36
* send method that will send the data to the other side
37
*
38
* @param type
39
* @param message
40
* @throws Exception
41
*/
42
public synchronized void send(String type, String message) throws Exception
43
{
44
synchronized(oos)
45
{
46
oos.reset();
47
oos.writeObject(new DataFrame(type, message));
48
oos.flush();
49
}
50
}
51
52
/**
53
* send method that will send the object to the other side
54
*
55
* @param type
56
* @param object
57
* @throws Exception
58
*/
59
public synchronized void send(String type, Object object) throws Exception
60
{
61
synchronized(oos)
62
{
63
oos.reset();
64
oos.writeObject(new DataFrame(type, object));
65
oos.flush();
66
}
67
}
68
69
/**
70
* getter
71
*
72
* @return ObjectInputStream
73
*/
74
public ObjectInputStream getOis()
75
{
76
return this.ois;
77
}
78
79
/**
80
* getter
81
*
82
* @return ObjectOutputStream
83
*/
84
public ObjectOutputStream getOos()
85
{
86
return this.oos;
87
}
88
89
/**
90
* getter
91
*
92
* @return Socket
93
*/
94
public Socket getSocket()
95
{
96
return this.socket;
97
}
98
}
99
package networking;2

3
import java.io.*;4
import java.net.*;5

6
/**7
* client class,that will create the connection and send data use socket8
* 9
* @author 10
* @version 1.011
*/12
public class Client13
{14
/**15
* Attributes16
*/17
private Socket socket;18
private ObjectInputStream ois;19
private ObjectOutputStream oos;20
private static final int TIMEOUT_LENGTH = 1000 * 60 * 60;// max timeout21
22
// number,6023
// minutes24
/**25
* Constructor26
*/27
public Client(String ip, int port) throws Exception28
{29
socket = new Socket(ip, port);30
socket.setSoTimeout(TIMEOUT_LENGTH);31
oos = new ObjectOutputStream(socket.getOutputStream());32
ois = new ObjectInputStream(socket.getInputStream());33
}34
35
/**36
* send method that will send the data to the other side37
* 38
* @param type39
* @param message40
* @throws Exception41
*/42
public synchronized void send(String type, String message) throws Exception43
{44
synchronized(oos)45
{46
oos.reset();47
oos.writeObject(new DataFrame(type, message));48
oos.flush();49
}50
}51
52
/**53
* send method that will send the object to the other side54
* 55
* @param type56
* @param object57
* @throws Exception58
*/59
public synchronized void send(String type, Object object) throws Exception60
{61
synchronized(oos)62
{63
oos.reset();64
oos.writeObject(new DataFrame(type, object));65
oos.flush();66
}67
}68
69
/**70
* getter71
* 72
* @return ObjectInputStream73
*/74
public ObjectInputStream getOis()75
{76
return this.ois;77
}78
79
/**80
* getter81
* 82
* @return ObjectOutputStream83
*/84
public ObjectOutputStream getOos()85
{86
return this.oos;87
}88
89
/**90
* getter91
* 92
* @return Socket93
*/94
public Socket getSocket()95
{96
return this.socket;97
}98
}99

1
package networking;
2
3
import java.awt.*;
4
import java.awt.event.*;
5
import javax.swing.*;
6
import java.util.*;
7
import java.io.*;
8
import javax.swing.text.*;
9
10
/**
11
* clientGUI class,that will allow the user to send the message,change room,and
12
* will update the information for the user
13
*
14
* @author
15
* @version 1.0
16
*/
17
public class ClientGUI extends JFrame
18
{
19
/**
20
* Attributes
21
*/
22
private static final long serialVersionUID = 1L;
23
private JPanel jContentPane = null;
24
private JDesktopPane jDesktopPane = null;
25
private JButton jButton = null;
26
private Client client;
27
private JScrollPane jScrollPane = null;
28
private JList jRoomList = null;
29
private JList jChatterList = null;
30
private JScrollPane jRoomScrollPane = null;
31
private JScrollPane jChatterScrollPane = null;
32
private JTextPane jContentTextArea = null;
33
private JTextPane jTextPane = null;
34
private JScrollPane jContentScrollPane = null;
35
private JLabel jRoomLabel = null;
36
private JLabel jChatterLabel = null;
37
private JButton jFileButton = null;
38
@SuppressWarnings("unused")
39
private ArrayList<Chatter> chatters = null;
40
private String myName;
41
private Map<Long, FileSender> sendingFiles;
42
private Map<Long, FileReceiver> receivingFiles;
43
private JButton jEmotionButton = null;
44
45
/**
46
* Setters
47
* @param chatters
48
*/
49
public void setChatters(ArrayList<Chatter> chatters)
50
{
51
this.chatters = chatters;
52
}
53
54
/**
55
* This is the default constructor
56
*/
57
public ClientGUI(Client client, String myName)
58
{
59
super();
60
this.client = client;
61
this.myName = myName;
62
initialize();
63
this.addWindowListener(new WindowAdapter()
64
{
65
public void windowClosing(WindowEvent e)
66
{
67
System.exit(0);// close the application,it will cause the
68
// socket to be closed
69
}
70
});
71
sendingFiles = Collections
72
.synchronizedMap(new HashMap<Long, FileSender>());
73
receivingFiles = Collections
74
.synchronizedMap(new HashMap<Long, FileReceiver>());
75
}
76
77
/**
78
* Getter
79
* @return Map<Long, FileReceiver>
80
*/
81
public Map<Long, FileReceiver> getReceivingFiles()
82
{
83
return this.receivingFiles;
84
}
85
86
/**Getter
87
* @return Map<Long, FileSender>
88
*/
89
public Map<Long, FileSender> getSendingFiles()
90
{
91
return this.sendingFiles;
92
}
93
94
/**
95
* This method initializes this
96
*
97
* @return void
98
*/
99
private void initialize()
100
{
101
this.setSize(772, 425);
102
this.setContentPane(getJContentPane());
103
this.setTitle("JFrame");
104
}
105
106
/**
107
* This method initializes jContentPane
108
*
109
* @return javax.swing.JPanel
110
*/
111
private JPanel getJContentPane()
112
{
113
if(jContentPane == null)
114
{
115
jContentPane = new JPanel();
116
jContentPane.setLayout(new BorderLayout());
117
jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
118
}
119
return jContentPane;
120
}
121
122
/**
123
* This method initializes jDesktopPane
124
*
125
* @return javax.swing.JDesktopPane
126
*/
127
private JDesktopPane getJDesktopPane()
128
{
129
if(jDesktopPane == null)
130
{
131
jChatterLabel = new JLabel();
132
jChatterLabel.setForeground(Color.red);
133
jChatterLabel.setBounds(new Rectangle(605, 18, 154, 14));
134
jChatterLabel.setText("To:All");
135
jRoomLabel = new JLabel();
136
jRoomLabel.setBounds(new Rectangle(12, 17, 194, 14));
137
jRoomLabel.setForeground(Color.red);
138
jRoomLabel.setText("Current room:hall");
139
jDesktopPane = new JDesktopPane();
140
jDesktopPane.add(getJButton(), null);
141
jDesktopPane.add(getJScrollPane(), null);
142
jDesktopPane.add(getJRoomScrollPane(), null);
143
jDesktopPane.add(getJChatterScrollPane(), null);
144
jDesktopPane.add(getJContentScrollPane(), null);
145
jDesktopPane.add(jRoomLabel, null);
146
jDesktopPane.add(getJFileButton(), null);
147
jDesktopPane.add(getJEmotionButton(), null);
148
jDesktopPane.add(jChatterLabel, null);
149
}
150
return jDesktopPane;
151
}
152
153
/**
154
* This method initializes jButton
155
*
156
* @return javax.swing.JButton
157
*/
158
private JButton getJButton()
159
{
160
if(jButton == null)
161
{
162
jButton = new JButton();
163
jButton.setBounds(new Rectangle(166, 353, 132, 21));
164
jButton.setText("Send a message");
165
Thread.currentThread().setPriority(9);
166
jButton.addActionListener(new java.awt.event.ActionListener()
167
{
168
public void actionPerformed(java.awt.event.ActionEvent e)
169
{
170
try
171
{
172
String name = jChatterLabel.getText()
173
.replace("To:", "");
174
if(name.equals("All"))// message to all the people in
175
// that room
176
{
177
client.send("Public", jContentTextArea.getText());
178
}
179
else
180
// private message to a certain person
181
{
182
name = name.substring(0, name.lastIndexOf("-"));
183
client.send("Private" + name, jContentTextArea
184
.getText());
185
}
186
}
187
catch(Exception ex)
188
{
189
ex.printStackTrace();
190
System.exit(0);
191
}
192
jContentTextArea.setText("");// clean the input text
193
// field
194
}
195
});
196
}
197
return jButton;
198
}
199
200
/**
201
* This method initializes jTextArea
202
*
203
* @return javax.swing.JTextArea
204
*/
205
public JTextPane getJTextPane()
206
{
207
if(jTextPane == null)
208
{
209
jTextPane = new JTextPane();
210
jTextPane.setEditable(false);
211
Style style = jTextPane.addStyle("JoeyText", StyleContext
212
.getDefaultStyleContext().getStyle(
213
StyleContext.DEFAULT_STYLE));
214
StyleConstants.setFontSize(style, 12);
215
style = jTextPane.addStyle("JoeyLabel", StyleContext
216
.getDefaultStyleContext().getStyle(
217
StyleContext.DEFAULT_STYLE));
218
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
219
style = jTextPane.addStyle("JoeyButton", StyleContext
220
.getDefaultStyleContext().getStyle(
221
StyleContext.DEFAULT_STYLE));
222
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
223
style = jTextPane.addStyle("[em01]", StyleContext
224
.getDefaultStyleContext().getStyle(
225
StyleContext.DEFAULT_STYLE));
226
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
227
StyleConstants.setIcon(style, new ImageIcon("../res/wodao.gif"));
228
style = jTextPane.addStyle("[em02]", StyleContext
229
.getDefaultStyleContext().getStyle(
230
StyleContext.DEFAULT_STYLE));
231
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
232
StyleConstants.setIcon(style, new ImageIcon("../res/daqiang.gif"));
233
style = jTextPane.addStyle("[em03]", StyleContext
234
.getDefaultStyleContext().getStyle(
235
StyleContext.DEFAULT_STYLE));
236
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
237
StyleConstants.setIcon(style, new ImageIcon("../res/zhaobian.jpg"));
238
style = jTextPane.addStyle("[em04]", StyleContext
239
.getDefaultStyleContext().getStyle(
240
StyleContext.DEFAULT_STYLE));
241
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
242
StyleConstants.setIcon(style, new ImageIcon("../res/shi.gif"));
243
style = jTextPane.addStyle("[em05]", StyleContext
244
.getDefaultStyleContext().getStyle(
245
StyleContext.DEFAULT_STYLE));
246
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
247
StyleConstants.setIcon(style, new ImageIcon("../res/buhuiba.jpg"));
248
style = jTextPane.addStyle("[em06]", StyleContext
249
.getDefaultStyleContext().getStyle(
250
StyleContext.DEFAULT_STYLE));
251
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
252
StyleConstants.setIcon(style,
253
new ImageIcon("../res/buyaofanwo.gif"));
254
}
255
return jTextPane;
256
}
257
258
/**
259
* This method initializes jScrollPane
260
*
261
* @return javax.swing.JScrollPane
262
*/
263
private JScrollPane getJScrollPane()
264
{
265
if(jScrollPane == null)
266
{
267
jScrollPane = new JScrollPane();
268
jScrollPane.setBounds(new Rectangle(159, 42, 428, 212));
269
jScrollPane.setViewportView(getJTextPane());
270
jScrollPane
271
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
272
}
273
return jScrollPane;
274
}
275
276
/**
277
* Method that will set the text in the message pane
278
* @param message
279
*/
280
public void setText(String message)
281
{
282
try
283
{
284
Document doc = jTextPane.getDocument();
285
int rightSide=0;
286
int leftSide=0;
287
char[] totalChars=message.toCharArray();
288
while(true)
289
{
290
rightSide=message.indexOf("[em", rightSide);//[em0x]
291
if(rightSide==-1)
292
{
293
break;
294
}
295
String temp=new String(totalChars,leftSide,rightSide-leftSide);
296
doc.insertString(doc.getLength(), temp, jTextPane
297
.getStyle("JoeyText"));
298
String number=message.substring(rightSide+4, rightSide+5);
299
if(number.equals("1"))
300
{
301
doc.insertString(doc.getLength(), "[em01]", jTextPane
302
.getStyle("[em01]"));
303
}
304
else if(number.equals("2"))
305
{
306
doc.insertString(doc.getLength(), "[em02]", jTextPane
307
.getStyle("[em02]"));
308
}
309
else if(number.equals("3"))
310
{
311
doc.insertString(doc.getLength(), "[em03]", jTextPane
312
.getStyle("[em03]"));
313
}
314
else if(number.equals("4"))
315
{
316
doc.insertString(doc.getLength(), "[em04]", jTextPane
317
.getStyle("[em04]"));
318
}
319
else if(number.equals("5"))
320
{
321
doc.insertString(doc.getLength(), "[em05]", jTextPane
322
.getStyle("[em05]"));
323
}
324
else if(number.equals("6"))
325
{
326
doc.insertString(doc.getLength(), "[em06]", jTextPane
327
.getStyle("[em06]"));
328
}
329
rightSide=rightSide+6;
330
leftSide=rightSide;
331
}
332
if(leftSide!=message.length())
333
{
334
doc.insertString(doc.getLength(),new String(totalChars,leftSide,message.length()-leftSide), jTextPane
335
.getStyle("JoeyText"));
336
}
337
doc.insertString(doc.getLength(),"\n", jTextPane
338
.getStyle("JoeyText"));
339
}
340
catch(BadLocationException ble)
341
{
342
}
343
}
344
345
/**
346
* Setter
347
* @param vector
348
*/
349
public void setChatterList(Vector<String> vector)
350
{
351
jChatterList.setListData(vector);
352
}
353
354
/**
355
* This method initializes jRoomList
356
*
357
* @return javax.swing.JList
358
*/
359
private JList getJRoomList()
360
{
361
if(jRoomList == null)
362
{
363
jRoomList = new JList();
364
Vector<String> vector = new Vector<String>();
365
vector.addElement("Hall");
366
vector.addElement("Canada");
367
vector.addElement("China");
368
vector.addElement("Mexico");
369
vector.addElement("Vietnam");
370
jRoomList.setListData(vector);
371
jRoomList
372
.addListSelectionListener(new javax.swing.event.ListSelectionListener()
373
{
374
public void valueChanged(
375
javax.swing.event.ListSelectionEvent e)
376
{
377
if(e.getValueIsAdjusting())
378
return;// only responds once,ignore the "leave
379
// focus" event but responds for the
380
// "get focus" event
381
if(jRoomList.getSelectedIndex() == -1)
382
return;
383
try
384
{
385
jRoomLabel.setText("Current room:"
386
+ (String)jRoomList.getSelectedValue());
387
client.send("Room", (String)jRoomList
388
.getSelectedValue());// apply to
389
// change room
390
// to the server
391
}
392
catch(Exception ex)
393
{
394
System.exit(0);
395
}
396
}
397
});
398
}
399
return jRoomList;
400
}
401
402
/**
403
* This method initializes jChatterList
404
*
405
* @return javax.swing.JList
406
*/
407
private JList getJChatterList()
408
{
409
if(jChatterList == null)
410
{
411
jChatterList = new JList();
412
jChatterList
413
.addListSelectionListener(new javax.swing.event.ListSelectionListener()
414
{
415
public void valueChanged(
416
javax.swing.event.ListSelectionEvent e)
417
{
418
if(e.getValueIsAdjusting())
419
return;
420
if(jChatterList.getSelectedIndex() == -1)
421
return;
422
try
423
{
424
jChatterLabel.setText("To:"
425
+ (String)jChatterList
426
.getSelectedValue());// change
427
// the
428
// receiver
429
}
430
catch(Exception ex)
431
{
432
System.exit(0);
433
}
434
}
435
});
436
}
437
return jChatterList;
438
}
439
440
/**
441
* This method initializes jRoomScrollPane
442
*
443
* @return javax.swing.JScrollPane
444
*/
445
private JScrollPane getJRoomScrollPane()
446
{
447
if(jRoomScrollPane == null)
448
{
449
jRoomScrollPane = new JScrollPane();
450
jRoomScrollPane.setLocation(new Point(11, 42));
451
jRoomScrollPane.setViewportView(getJRoomList());
452
jRoomScrollPane.setSize(new Dimension(128, 329));
453
}
454
return jRoomScrollPane;
455
}
456
457
/**
458
* This method initializes jChatterScrollPane
459
*
460
* @return javax.swing.JScrollPane
461
*/
462
private JScrollPane getJChatterScrollPane()
463
{
464
if(jChatterScrollPane == null)
465
{
466
jChatterScrollPane = new JScrollPane();
467
jChatterScrollPane.setLocation(new Point(606, 39));
468
jChatterScrollPane.setViewportView(getJChatterList());
469
jChatterScrollPane.setSize(new Dimension(128, 334));
470
}
471
return jChatterScrollPane;
472
}
473
474
/**
475
* This method initializes jContentTextArea
476
*
477
* @return javax.swing.JTextArea
478
*/
479
public JTextPane getJContentTextArea()
480
{
481
if(jContentTextArea == null)
482
{
483
jContentTextArea = new JTextPane();
484
jContentTextArea.setBounds(new Rectangle(0, 0, 428, 82));
485
Style style = jContentTextArea.addStyle("[em01]", StyleContext
486
.getDefaultStyleContext().getStyle(
487
StyleContext.DEFAULT_STYLE));
488
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
489
StyleConstants.setIcon(style, new ImageIcon("../res/wodao.gif"));
490
style = jContentTextArea.addStyle("[em02]", StyleContext
491
.getDefaultStyleContext().getStyle(
492
StyleContext.DEFAULT_STYLE));
493
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
494
StyleConstants.setIcon(style, new ImageIcon("../res/daqiang.gif"));
495
style = jContentTextArea.addStyle("[em03]", StyleContext
496
.getDefaultStyleContext().getStyle(
497
StyleContext.DEFAULT_STYLE));
498
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
499
StyleConstants.setIcon(style, new ImageIcon("../res/zhaobian.jpg"));
500
style = jContentTextArea.addStyle("[em04]", StyleContext
501
.getDefaultStyleContext().getStyle(
502
StyleContext.DEFAULT_STYLE));
503
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
504
StyleConstants.setIcon(style, new ImageIcon("../res/shi.gif"));
505
style = jContentTextArea.addStyle("[em05]", StyleContext
506
.getDefaultStyleContext().getStyle(
507
StyleContext.DEFAULT_STYLE));
508
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
509
StyleConstants.setIcon(style, new ImageIcon("../res/buhuiba.jpg"));
510
style = jContentTextArea.addStyle("[em06]", StyleContext
511
.getDefaultStyleContext().getStyle(
512
StyleContext.DEFAULT_STYLE));
513
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
514
StyleConstants.setIcon(style,
515
new ImageIcon("../res/buyaofanwo.gif"));
516
}
517
return jContentTextArea;
518
}
519
520
/**
521
* This method initializes jContentScrollPane
522
*
523
* @return javax.swing.JScrollPane
524
*/
525
private JScrollPane getJContentScrollPane()
526
{
527
if(jContentScrollPane == null)
528
{
529
jContentScrollPane = new JScrollPane();
530
jContentScrollPane.setBounds(new Rectangle(159, 260, 428, 86));
531
jContentScrollPane.setViewportView(getJContentTextArea());
532
}
533
return jContentScrollPane;
534
}
535
536
public JPanel getJPanel()
537
{
538
return this.jContentPane;
539
}
540
541
/**
542
* This method initializes jFileButton
543
*
544
* @return javax.swing.JButton
545
*/
546
private JButton getJFileButton()
547
{
548
if(jFileButton == null)
549
{
550
jFileButton = new JButton();
551
jFileButton.setBounds(new Rectangle(328, 353, 110, 21));
552
jFileButton.setText("Send a file");
553
jFileButton.addActionListener(new java.awt.event.ActionListener()
554
{
555
public void actionPerformed(java.awt.event.ActionEvent e)
556
{
557
String name = jChatterLabel.getText().replaceFirst("To:",
558
"");
559
if(name.equals("All"))
560
{
561
JOptionPane.showMessageDialog(jContentPane,
562
"Please choose a person to send to at first",
563
"", JOptionPane.ERROR_MESSAGE);
564
return;
565
}
566
JFileChooser jfc = new JFileChooser();
567
int result = jfc.showOpenDialog(ClientGUI.this);
568
switch(result)
569
{
570
case JFileChooser.APPROVE_OPTION:
571
File file = jfc.getSelectedFile();
572
name = name.substring(0, name.lastIndexOf("-"));
573
sendAFile(file, name);
574
break;
575
case JFileChooser.CANCEL_OPTION:
576
break;
577
case JFileChooser.ERROR_OPTION:
578
break;
579
}
580
}
581
});
582
}
583
return jFileButton;
584
}
585
586
/**
587
* Method that will show the send file message
588
* @param file
589
* @param receiver
590
*/
591
private void sendAFile(File file, String receiver)
592
{
593
Document doc = jTextPane.getDocument();
594
Style style = jTextPane.getStyle("JoeyLabel");
595
JoeyLabel joeyLabel = new JoeyLabel();
596
joeyLabel.setText("<html>Send file '" + file.getName() + "' to "
597
+ receiver + "</html>");
598
StyleConstants.setComponent(style, joeyLabel);
599
try
600
{
601
doc.insertString(doc.getLength(), "\n", jTextPane
602
.getStyle("JoeyLabel"));
603
}
604
catch(BadLocationException ble)
605
{
606
}
607
style = jTextPane.getStyle("JoeyButton");
608
JoeyButton joeyButton = new JoeyButton();
609
joeyButton.setText("Cancel");
610
joeyButton.setMargin(new Insets(0, 0, 0, 0));
611
StyleConstants.setComponent(style, joeyButton);
612
try
613
{
614
doc.insertString(doc.getLength(), "\n", jTextPane
615
.getStyle("JoeyButton"));
616
}
617
catch(BadLocationException ble)
618
{
619
}
620
FileSender fs = new FileSender(client, file, this.sendingFiles,
621
this.myName, receiver, joeyLabel, joeyButton);
622
fs.register();
623
fs.request();
624
}
625
626
/**
627
* Class attribute getter
628
*
629
* @return the client
630
*/
631
public Client getClient()
632
{
633
return this.client;
634
}
635
636
/**
637
* This method initializes jEmotionButton
638
*
639
* @return javax.swing.JButton
640
*/
641
private JButton getJEmotionButton()
642
{
643
if(jEmotionButton == null)
644
{
645
jEmotionButton = new JButton();
646
jEmotionButton.setSize(new Dimension(126, 21));
647
jEmotionButton.setText("Add an emotion");
648
jEmotionButton.setLocation(new Point(451, 353));
649
jEmotionButton
650
.addActionListener(new java.awt.event.ActionListener()
651
{
652
public void actionPerformed(java.awt.event.ActionEvent e)
653
{
654
EmotionButtons eb = new EmotionButtons(
655
ClientGUI.this);
656
eb.setVisible(true);
657
}
658
});
659
}
660
return jEmotionButton;
661
}
662
}
663
package networking;2

3
import java.awt.*;4
import java.awt.event.*;5
import javax.swing.*;6
import java.util.*;7
import java.io.*;8
import javax.swing.text.*;9

10
/**11
* clientGUI class,that will allow the user to send the message,change room,and12
* will update the information for the user13
* 14
* @author 15
* @version 1.016
*/17
public class ClientGUI extends JFrame18
{19
/**20
* Attributes21
*/22
private static final long serialVersionUID = 1L;23
private JPanel jContentPane = null;24
private JDesktopPane jDesktopPane = null;25
private JButton jButton = null;26
private Client client;27
private JScrollPane jScrollPane = null;28
private JList jRoomList = null;29
private JList jChatterList = null;30
private JScrollPane jRoomScrollPane = null;31
private JScrollPane jChatterScrollPane = null;32
private JTextPane jContentTextArea = null;33
private JTextPane jTextPane = null;34
private JScrollPane jContentScrollPane = null;35
private JLabel jRoomLabel = null;36
private JLabel jChatterLabel = null;37
private JButton jFileButton = null;38
@SuppressWarnings("unused")39
private ArrayList<Chatter> chatters = null;40
private String myName;41
private Map<Long, FileSender> sendingFiles;42
private Map<Long, FileReceiver> receivingFiles;43
private JButton jEmotionButton = null;44
45
/**46
* Setters47
* @param chatters48
*/49
public void setChatters(ArrayList<Chatter> chatters)50
{51
this.chatters = chatters;52
}53
54
/**55
* This is the default constructor56
*/57
public ClientGUI(Client client, String myName)58
{59
super();60
this.client = client;61
this.myName = myName;62
initialize();63
this.addWindowListener(new WindowAdapter()64
{65
public void windowClosing(WindowEvent e)66
{67
System.exit(0);// close the application,it will cause the68
// socket to be closed69
}70
});71
sendingFiles = Collections72
.synchronizedMap(new HashMap<Long, FileSender>());73
receivingFiles = Collections74
.synchronizedMap(new HashMap<Long, FileReceiver>());75
}76
77
/**78
* Getter79
* @return Map<Long, FileReceiver>80
*/81
public Map<Long, FileReceiver> getReceivingFiles()82
{83
return this.receivingFiles;84
}85
86
/**Getter87
* @return Map<Long, FileSender>88
*/89
public Map<Long, FileSender> getSendingFiles()90
{91
return this.sendingFiles;92
}93
94
/**95
* This method initializes this96
* 97
* @return void98
*/99
private void initialize()100
{101
this.setSize(772, 425);102
this.setContentPane(getJContentPane());103
this.setTitle("JFrame");104
}105
106
/**107
* This method initializes jContentPane108
* 109
* @return javax.swing.JPanel110
*/111
private JPanel getJContentPane()112
{113
if(jContentPane == null)114
{115
jContentPane = new JPanel();116
jContentPane.setLayout(new BorderLayout());117
jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);118
}119
return jContentPane;120
}121
122
/**123
* This method initializes jDesktopPane124
* 125
* @return javax.swing.JDesktopPane126
*/127
private JDesktopPane getJDesktopPane()128
{129
if(jDesktopPane == null)130
{131
jChatterLabel = new JLabel();132
jChatterLabel.setForeground(Color.red);133
jChatterLabel.setBounds(new Rectangle(605, 18, 154, 14));134
jChatterLabel.setText("To:All");135
jRoomLabel = new JLabel();136
jRoomLabel.setBounds(new Rectangle(12, 17, 194, 14));137
jRoomLabel.setForeground(Color.red);138
jRoomLabel.setText("Current room:hall");139
jDesktopPane = new JDesktopPane();140
jDesktopPane.add(getJButton(), null);141
jDesktopPane.add(getJScrollPane(), null);142
jDesktopPane.add(getJRoomScrollPane(), null);143
jDesktopPane.add(getJChatterScrollPane(), null);144
jDesktopPane.add(getJContentScrollPane(), null);145
jDesktopPane.add(jRoomLabel, null);146
jDesktopPane.add(getJFileButton(), null);147
jDesktopPane.add(getJEmotionButton(), null);148
jDesktopPane.add(jChatterLabel, null);149
}150
return jDesktopPane;151
}152
153
/**154
* This method initializes jButton155
* 156
* @return javax.swing.JButton157
*/158
private JButton getJButton()159
{160
if(jButton == null)161
{162
jButton = new JButton();163
jButton.setBounds(new Rectangle(166, 353, 132, 21));164
jButton.setText("Send a message");165
Thread.currentThread().setPriority(9);166
jButton.addActionListener(new java.awt.event.ActionListener()167
{168
public void actionPerformed(java.awt.event.ActionEvent e)169
{170
try171
{172
String name = jChatterLabel.getText()173
.replace("To:", "");174
if(name.equals("All"))// message to all the people in175
// that room176
{177
client.send("Public", jContentTextArea.getText());178
}179
else180
// private message to a certain person181
{182
name = name.substring(0, name.lastIndexOf("-"));183
client.send("Private" + name, jContentTextArea184
.getText());185
}186
}187
catch(Exception ex)188
{189
ex.printStackTrace();190
System.exit(0);191
}192
jContentTextArea.setText("");// clean the input text193
// field194
}195
});196
}197
return jButton;198
}199
200
/**201
* This method initializes jTextArea202
* 203
* @return javax.swing.JTextArea204
*/205
public JTextPane getJTextPane()206
{207
if(jTextPane == null)208
{209
jTextPane = new JTextPane();210
jTextPane.setEditable(false);211
Style style = jTextPane.addStyle("JoeyText", StyleContext212
.getDefaultStyleContext().getStyle(213
StyleContext.DEFAULT_STYLE));214
StyleConstants.setFontSize(style, 12);215
style = jTextPane.addStyle("JoeyLabel", StyleContext216
.getDefaultStyleContext().getStyle(217
StyleContext.DEFAULT_STYLE));218
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);219
style = jTextPane.addStyle("JoeyButton", StyleContext220
.getDefaultStyleContext().getStyle(221
StyleContext.DEFAULT_STYLE));222
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);223
style = jTextPane.addStyle("[em01]", StyleContext224
.getDefaultStyleContext().getStyle(225
StyleContext.DEFAULT_STYLE));226
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);227
StyleConstants.setIcon(style, new ImageIcon("../res/wodao.gif"));228
style = jTextPane.addStyle("[em02]", StyleContext229
.getDefaultStyleContext().getStyle(230
StyleContext.DEFAULT_STYLE));231
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);232
StyleConstants.setIcon(style, new ImageIcon("../res/daqiang.gif"));233
style = jTextPane.addStyle("[em03]", StyleContext234
.getDefaultStyleContext().getStyle(235
StyleContext.DEFAULT_STYLE));236
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);237
StyleConstants.setIcon(style, new ImageIcon("../res/zhaobian.jpg"));238
style = jTextPane.addStyle("[em04]", StyleContext239
.getDefaultStyleContext().getStyle(240
StyleContext.DEFAULT_STYLE));241
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);242
StyleConstants.setIcon(style, new ImageIcon("../res/shi.gif"));243
style = jTextPane.addStyle("[em05]", StyleContext244
.getDefaultStyleContext().getStyle(245
StyleContext.DEFAULT_STYLE));246
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);247
StyleConstants.setIcon(style, new ImageIcon("../res/buhuiba.jpg"));248
style = jTextPane.addStyle("[em06]", StyleContext249
.getDefaultStyleContext().getStyle(250
StyleContext.DEFAULT_STYLE));251
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);252
StyleConstants.setIcon(style,253
new ImageIcon("../res/buyaofanwo.gif"));254
}255
return jTextPane;256
}257
258
/**259
* This method initializes jScrollPane260
* 261
* @return javax.swing.JScrollPane262
*/263
private JScrollPane getJScrollPane()264
{265
if(jScrollPane == null)266
{267
jScrollPane = new JScrollPane();268
jScrollPane.setBounds(new Rectangle(159, 42, 428, 212));269
jScrollPane.setViewportView(getJTextPane());270
jScrollPane271
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);272
}273
return jScrollPane;274
}275
276
/**277
* Method that will set the text in the message pane278
* @param message279
*/280
public void setText(String message)281
{282
try283
{284
Document doc = jTextPane.getDocument();285
int rightSide=0;286
int leftSide=0;287
char[] totalChars=message.toCharArray();288
while(true)289
{290
rightSide=message.indexOf("[em", rightSide);//[em0x]291
if(rightSide==-1)292
{293
break;294
}295
String temp=new String(totalChars,leftSide,rightSide-leftSide);296
doc.insertString(doc.getLength(), temp, jTextPane297
.getStyle("JoeyText"));298
String number=message.substring(rightSide+4, rightSide+5);299
if(number.equals("1"))300
{301
doc.insertString(doc.getLength(), "[em01]", jTextPane302
.getStyle("[em01]"));303
}304
else if(number.equals("2"))305
{306
doc.insertString(doc.getLength(), "[em02]", jTextPane307
.getStyle("[em02]"));308
}309
else if(number.equals("3"))310
{311
doc.insertString(doc.getLength(), "[em03]", jTextPane312
.getStyle("[em03]"));313
}314
else if(number.equals("4"))315
{316
doc.insertString(doc.getLength(), "[em04]", jTextPane317
.getStyle("[em04]"));318
}319
else if(number.equals("5"))320
{321
doc.insertString(doc.getLength(), "[em05]", jTextPane322
.getStyle("[em05]"));323
}324
else if(number.equals("6"))325
{326
doc.insertString(doc.getLength(), "[em06]", jTextPane327
.getStyle("[em06]"));328
}329
rightSide=rightSide+6;330
leftSide=rightSide;331
}332
if(leftSide!=message.length())333
{334
doc.insertString(doc.getLength(),new String(totalChars,leftSide,message.length()-leftSide), jTextPane335
.getStyle("JoeyText"));336
}337
doc.insertString(doc.getLength(),"\n", jTextPane338
.getStyle("JoeyText"));339
}340
catch(BadLocationException ble)341
{342
}343
}344
345
/**346
* Setter347
* @param vector348
*/349
public void setChatterList(Vector<String> vector)350
{351
jChatterList.setListData(vector);352
}353
354
/**355
* This method initializes jRoomList356
* 357
* @return javax.swing.JList358
*/359
private JList getJRoomList()360
{361
if(jRoomList == null)362
{363
jRoomList = new JList();364
Vector<String> vector = new Vector<String>();365
vector.addElement("Hall");366
vector.addElement("Canada");367
vector.addElement("China");368
vector.addElement("Mexico");369
vector.addElement("Vietnam");370
jRoomList.setListData(vector);371
jRoomList372
.addListSelectionListener(new javax.swing.event.ListSelectionListener()373
{374
public void valueChanged(375
javax.swing.event.ListSelectionEvent e)376
{377
if(e.getValueIsAdjusting())378
return;// only responds once,ignore the "leave379
// focus" event but responds for the380
// "get focus" event381
if(jRoomList.getSelectedIndex() == -1)382
return;383
try384
{385
jRoomLabel.setText("Current room:"386
+ (String)jRoomList.getSelectedValue());387
client.send("Room", (String)jRoomList388
.getSelectedValue());// apply to389
// change room390
// to the server391
}392
catch(Exception ex)393
{394
System.exit(0);395
}396
}397
});398
}399
return jRoomList;400
}401
402
/**403
* This method initializes jChatterList404
* 405
* @return javax.swing.JList406
*/407
private JList getJChatterList()408
{409
if(jChatterList == null)410
{411
jChatterList = new JList();412
jChatterList413
.addListSelectionListener(new javax.swing.event.ListSelectionListener()414
{415
public void valueChanged(416
javax.swing.event.ListSelectionEvent e)417
{418
if(e.getValueIsAdjusting())419
return;420
if(jChatterList.getSelectedIndex() == -1)421
return;422
try423
{424
jChatterLabel.setText("To:"425
+ (String)jChatterList426
.getSelectedValue());// change427
// the428
// receiver429
}430
catch(Exception ex)431
{432
System.exit(0);433
}434
}435
});436
}437
return jChatterList;438
}439
440
/**441
* This method initializes jRoomScrollPane442
* 443
* @return javax.swing.JScrollPane444
*/445
private JScrollPane getJRoomScrollPane()446
{447
if(jRoomScrollPane == null)448
{449
jRoomScrollPane = new JScrollPane();450
jRoomScrollPane.setLocation(new Point(11, 42));451
jRoomScrollPane.setViewportView(getJRoomList());452
jRoomScrollPane.setSize(new Dimension(128, 329));453
}454
return jRoomScrollPane;455
}456
457
/**458
* This method initializes jChatterScrollPane459
* 460
* @return javax.swing.JScrollPane461
*/462
private JScrollPane getJChatterScrollPane()463
{464
if(jChatterScrollPane == null)465
{466
jChatterScrollPane = new JScrollPane();467
jChatterScrollPane.setLocation(new Point(606, 39));468
jChatterScrollPane.setViewportView(getJChatterList());469
jChatterScrollPane.setSize(new Dimension(128, 334));470
}471
return jChatterScrollPane;472
}473
474
/**475
* This method initializes jContentTextArea476
* 477
* @return javax.swing.JTextArea478
*/479
public JTextPane getJContentTextArea()480
{481
if(jContentTextArea == null)482
{483
jContentTextArea = new JTextPane();484
jContentTextArea.setBounds(new Rectangle(0, 0, 428, 82));485
Style style = jContentTextArea.addStyle("[em01]", StyleContext486
.getDefaultStyleContext().getStyle(487
StyleContext.DEFAULT_STYLE));488
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);489
StyleConstants.setIcon(style, new ImageIcon("../res/wodao.gif"));490
style = jContentTextArea.addStyle("[em02]", StyleContext491
.getDefaultStyleContext().getStyle(492
StyleContext.DEFAULT_STYLE));493
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);494
StyleConstants.setIcon(style, new ImageIcon("../res/daqiang.gif"));495
style = jContentTextArea.addStyle("[em03]", StyleContext496
.getDefaultStyleContext().getStyle(497
StyleContext.DEFAULT_STYLE));498
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);499
StyleConstants.setIcon(style, new ImageIcon("../res/zhaobian.jpg"));500
style = jContentTextArea.addStyle("[em04]", StyleContext501
.getDefaultStyleContext().getStyle(502
StyleContext.DEFAULT_STYLE));503
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);504
StyleConstants.setIcon(style, new ImageIcon("../res/shi.gif"));505
style = jContentTextArea.addStyle("[em05]", StyleContext506
.getDefaultStyleContext().getStyle(507
StyleContext.DEFAULT_STYLE));508
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);509
StyleConstants.setIcon(style, new ImageIcon("../res/buhuiba.jpg"));510
style = jContentTextArea.addStyle("[em06]", StyleContext511
.getDefaultStyleContext().getStyle(512
StyleContext.DEFAULT_STYLE));513
StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);514
StyleConstants.setIcon(style,515
new ImageIcon("../res/buyaofanwo.gif"));516
}517
return jContentTextArea;518
}519
520
/**521
* This method initializes jContentScrollPane522
* 523
* @return javax.swing.JScrollPane524
*/525
private JScrollPane getJContentScrollPane()526
{527
if(jContentScrollPane == null)528
{529
jContentScrollPane = new JScrollPane();530
jContentScrollPane.setBounds(new Rectangle(159, 260, 428, 86));531
jContentScrollPane.setViewportView(getJContentTextArea());532
}533
return jContentScrollPane;534
}535
536
public JPanel getJPanel()537
{538
return this.jContentPane;539
}540
541
/**542
* This method initializes jFileButton543
* 544
* @return javax.swing.JButton545
*/546
private JButton getJFileButton()547
{548
if(jFileButton == null)549
{550
jFileButton = new JButton();551
jFileButton.setBounds(new Rectangle(328, 353, 110, 21));552
jFileButton.setText("Send a file");553
jFileButton.addActionListener(new java.awt.event.ActionListener()554
{555
public void actionPerformed(java.awt.event.ActionEvent e)556
{557
String name = jChatterLabel.getText().replaceFirst("To:",558
"");559
if(name.equals("All"))560
{561
JOptionPane.showMessageDialog(jContentPane,562
"Please choose a person to send to at first",563
"", JOptionPane.ERROR_MESSAGE);564
return;565
}566
JFileChooser jfc = new JFileChooser();567
int result = jfc.showOpenDialog(ClientGUI.this);568
switch(result)569
{570
case JFileChooser.APPROVE_OPTION:571
File file = jfc.getSelectedFile();572
name = name.substring(0, name.lastIndexOf("-"));573
sendAFile(file, name);574
break;575
case JFileChooser.CANCEL_OPTION:576
break;577
case JFileChooser.ERROR_OPTION:578
break;579
}580
}581
});582
}583
return jFileButton;584
}585
586
/**587
* Method that will show the send file message588
* @param file589
* @param receiver590
*/591
private void sendAFile(File file, String receiver)592
{593
Document doc = jTextPane.getDocument();594
Style style = jTextPane.getStyle("JoeyLabel");595
JoeyLabel joeyLabel = new JoeyLabel();596
joeyLabel.setText("<html>Send file '" + file.getName() + "' to "597
+ receiver + "</html>");598
StyleConstants.setComponent(style, joeyLabel);599
try600
{601
doc.insertString(doc.getLength(), "\n", jTextPane602
.getStyle("JoeyLabel"));603
}604
catch(BadLocationException ble)605
{606
}607
style = jTextPane.getStyle("JoeyButton");608
JoeyButton joeyButton = new JoeyButton();609
joeyButton.setText("Cancel");610
joeyButton.setMargin(new Insets(0, 0, 0, 0));611
StyleConstants.setComponent(style, joeyButton);612
try613
{614
doc.insertString(doc.getLength(), "\n", jTextPane615
.getStyle("JoeyButton"));616
}617
catch(BadLocationException ble)618
{619
}620
FileSender fs = new FileSender(client, file, this.sendingFiles,621
this.myName, receiver, joeyLabel, joeyButton);622
fs.register();623
fs.request();624
}625
626
/**627
* Class attribute getter628
* 629
* @return the client630
*/631
public Client getClient()632
{633
return this.client;634
}635
636
/**637
* This method initializes jEmotionButton638
* 639
* @return javax.swing.JButton640
*/641
private JButton getJEmotionButton()642
{643
if(jEmotionButton == null)644
{645
jEmotionButton = new JButton();646
jEmotionButton.setSize(new Dimension(126, 21));647
jEmotionButton.setText("Add an emotion");648
jEmotionButton.setLocation(new Point(451, 353));649
jEmotionButton650
.addActionListener(new java.awt.event.ActionListener()651
{652
public void actionPerformed(java.awt.event.ActionEvent e)653
{654
EmotionButtons eb = new EmotionButtons(655
ClientGUI.this);656
eb.setVisible(true);657
}658
});659
}660
return jEmotionButton;661
}662
}663

1
package networking;
2
3
import java.awt.*;
4
import javax.swing.*;
5
import java.awt.event.*;
6
7
/**
8
* clientlogin is the login gui class
9
*
10
* @author
11
* @version 1.0
12
*/
13
public class ClientLogin extends JFrame
14
{
15
/**
16
* Attributes
17
*/
18
private static final long serialVersionUID = 1L;
19
private JPanel jContentPane = null;
20
private JDesktopPane jDesktopPane = null;
21
private JLabel jHostLabel = null;
22
private JTextField jHostTextField = null;
23
private JLabel jNameLabel = null;
24
private JTextField jNameTextField = null;
25
private JButton jButton = null;
26
27
/**
28
* This is the default constructor
29
*/
30
public ClientLogin()
31
{
32
super();
33
initialize();
34
this.addWindowListener(new WindowAdapter()
35
{
36
public void windowClosing(WindowEvent e)
37
{
38
System.exit(0);
39
}
40
});
41
}
42
43
/**
44
* This method initializes this
45
*
46
* @return void
47
*/
48
private void initialize()
49
{
50
this.setSize(481, 290);
51
this.setContentPane(getJContentPane());
52
this.setTitle("JFrame");
53
}
54
55
/**
56
* This method initializes jContentPane
57
*
58
* @return javax.swing.JPanel
59
*/
60
private JPanel getJContentPane()
61
{
62
if(jContentPane == null)
63
{
64
jContentPane = new JPanel();
65
jContentPane.setLayout(new BorderLayout());
66
jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
67
}
68
return jContentPane;
69
}
70
71
/**
72
* This method initializes jDesktopPane
73
*
74
* @return javax.swing.JDesktopPane
75
*/
76
private JDesktopPane getJDesktopPane()
77
{
78
if(jDesktopPane == null)
79
{
80
jNameLabel = new JLabel();
81
jNameLabel.setBounds(new Rectangle(24, 117, 110, 14));
82
jNameLabel.setForeground(Color.red);
83
jNameLabel.setText("Your name:");
84
jHostLabel = new JLabel();
85
jHostLabel.setBounds(new Rectangle(24, 38, 90, 14));
86
jHostLabel.setForeground(Color.red);
87
jHostLabel.setText("Host IP:");
88
jDesktopPane = new JDesktopPane();
89
jDesktopPane.add(jHostLabel, null);
90
jDesktopPane.add(getJHostTextField(), null);
91
jDesktopPane.add(jNameLabel, null);
92
jDesktopPane.add(getJNameTextField(), null);
93
jDesktopPane.add(getJButton(), null);
94
}
95
return jDesktopPane;
96
}
97
98
/* (non-Javadoc)
99
* @see javax.swing.JFrame#getContentPane()
100
*/
101
public JPanel getContentPane()
102
{
103
return this.jContentPane;
104
}
105
106
/**
107
* This method initializes jHostTextField
108
*
109
* @return javax.swing.JTextField
110
*/
111
private JTextField getJHostTextField()
112
{
113
if(jHostTextField == null)
114
{
115
jHostTextField = new JTextField();
116
jHostTextField.setBounds(new Rectangle(150, 38, 136, 20));
117
jHostTextField.setText("127.0.0.1");
118
}
119
return jHostTextField;
120
}
121
122
/**
123
* This method initializes jNameTextField
124
*
125
* @return javax.swing.JTextField
126
*/
127
private JTextField getJNameTextField()
128
{
129
if(jNameTextField == null)
130
{
131
jNameTextField = new JTextField();
132
jNameTextField.setBounds(new Rectangle(149, 117, 137, 20));
133
}
134
return jNameTextField;
135
}
136
137
/**
138
* This method initializes jButton
139
*
140
* @return javax.swing.JButton
141
*/
142
private JButton getJButton()
143
{
144
if(jButton == null)
145
{
146
jButton = new JButton();
147
jButton.setBounds(new Rectangle(151, 167, 133, 23));
148
jButton.setText("Login");
149
jButton.addActionListener(new java.awt.event.ActionListener()
150
{
151
public void actionPerformed(java.awt.event.ActionEvent e)
152
{
153
try
154
{
155
// login
156
Client client = new Client(jHostTextField.getText(),
157
2008);
158
ClientGUI cg = new ClientGUI(client,jNameTextField.getText());
159
ClientThreadHandler cth = new ClientThreadHandler(
160
client.getSocket(), client.getOis(), cg,
161
ClientLogin.this);
162
Thread thread = new Thread(cth);
163
thread.start();// starts a thread to listen the
164
// responses from the server
165
client.send("Login", jNameTextField.getText());
166
}
167
catch(Exception ex)
168
{
169
System.out.println(ex.getMessage());
170
JOptionPane.showMessageDialog(jContentPane,
171
"Wrong ip address", "No Title",
172
JOptionPane.ERROR_MESSAGE);
173
}
174
}
175
});
176
}
177
return jButton;
178
}
179
180
/**
181
* Main method,application entry
182
*
183
* @param args
184
*/
185
public static void main(String[] args)
186
{
187
ClientLogin clientLogin=new ClientLogin();
188
clientLogin.setVisible(true);
189
}
190
}
191
package networking;2

3
import java.awt.*;4
import javax.swing.*;5
import java.awt.event.*;6

7
/**8
* clientlogin is the login gui class9
* 10
* @author 11
* @version 1.012
*/13
public class ClientLogin extends JFrame14
{15
/**16
* Attributes17
*/18
private static final long serialVersionUID = 1L;19
private JPanel jContentPane = null;20
private JDesktopPane jDesktopPane = null;21
private JLabel jHostLabel = null;22
private JTextField jHostTextField = null;23
private JLabel jNameLabel = null;24
private JTextField jNameTextField = null;25
private JButton jButton = null;26
27
/**28
* This is the default constructor29
*/30
public ClientLogin()31
{32
super();33
initialize();34
this.addWindowListener(new WindowAdapter()35
{36
public void windowClosing(WindowEvent e)37
{38
System.exit(0);39
}40
});41
}42
43
/**44
* This method initializes this45
* 46
* @return void47
*/48
private void initialize()49
{50
this.setSize(481, 290);51
this.setContentPane(getJContentPane());52
this.setTitle("JFrame");53
}54
55
/**56
* This method initializes jContentPane57
* 58
* @return javax.swing.JPanel59
*/60
private JPanel getJContentPane()61
{62
if(jContentPane == null)63
{64
jContentPane = new JPanel();65
jContentPane.setLayout(new BorderLayout());66
jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);67
}68
return jContentPane;69
}70
71
/**72
* This method initializes jDesktopPane73
* 74
* @return javax.swing.JDesktopPane75
*/76
private JDesktopPane getJDesktopPane()77
{78
if(jDesktopPane == null)79
{80
jNameLabel = new JLabel();81
jNameLabel.setBounds(new Rectangle(24, 117, 110, 14));82
jNameLabel.setForeground(Color.red);83
jNameLabel.setText("Your name:");84
jHostLabel = new JLabel();85
jHostLabel.setBounds(new Rectangle(24, 38, 90, 14));86
jHostLabel.setForeground(Color.red);87
jHostLabel.setText("Host IP:");88
jDesktopPane = new JDesktopPane();89
jDesktopPane.add(jHostLabel, null);90
jDesktopPane.add(getJHostTextField(), null);91
jDesktopPane.add(jNameLabel, null);92
jDesktopPane.add(getJNameTextField(), null);93
jDesktopPane.add(getJButton(), null);94
}95
return jDesktopPane;96
}97
98
/* (non-Javadoc)99
* @see javax.swing.JFrame#getContentPane()100
*/101
public JPanel getContentPane()102
{103
return this.jContentPane;104
}105
106
/**107
* This method initializes jHostTextField108
* 109
* @return javax.swing.JTextField110
*/111
private JTextField getJHostTextField()112
{113
if(jHostTextField == null)114
{115
jHostTextField = new JTextField();116
jHostTextField.setBounds(new Rectangle(150, 38, 136, 20));117
jHostTextField.setText("127.0.0.1");118
}119
return jHostTextField;120
}121
122
/**123
* This method initializes jNameTextField124
* 125
* @return javax.swing.JTextField126
*/127
private JTextField getJNameTextField()128
{129
if(jNameTextField == null)130
{131
jNameTextField = new JTextField();132
jNameTextField.setBounds(new Rectangle(149, 117, 137, 20));133
}134
return jNameTextField;135
}136
137
/**138
* This method initializes jButton139
* 140
* @return javax.swing.JButton141
*/142
private JButton getJButton()143
{144
if(jButton == null)145
{146
jButton = new JButton();147
jButton.setBounds(new Rectangle(151, 167, 133, 23));148
jButton.setText("Login");149
jButton.addActionListener(new java.awt.event.ActionListener()150
{151
public void actionPerformed(java.awt.event.ActionEvent e)152
{153
try154
{155
// login156
Client client = new Client(jHostTextField.getText(),157
2008);158
ClientGUI cg = new ClientGUI(client,jNameTextField.getText());159
ClientThreadHandler cth = new ClientThreadHandler(160
client.getSocket(), client.getOis(), cg,161
ClientLogin.this);162
Thread thread = new Thread(cth);163
thread.start();// starts a thread to listen the164
// responses from the server165
client.send("Login", jNameTextField.getText());166
}167
catch(Exception ex)168
{169
System.out.println(ex.getMessage());170
JOptionPane.showMessageDialog(jContentPane,171
"Wrong ip address", "No Title",172
JOptionPane.ERROR_MESSAGE);173
}174
}175
});176
}177
return jButton;178
}179
180
/**181
* Main method,application entry182
* 183
* @param args184
*/185
public static void main(String[] args)186
{187
ClientLogin clientLogin=new ClientLogin();188
clientLogin.setVisible(true);189
}190
}191




浙公网安备 33010602011771号