-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatClient.java
251 lines (240 loc) · 7.5 KB
/
ChatClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;
import javax.swing.ImageIcon;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import javax.swing.SwingUtilities;
public class ChatClient extends JFrame
{
private GridBagLayout layout;
private GridBagConstraints constraints;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
private JButton btnSubmission;
private JLabel lblMessage;
private JLabel lblSideBar;
private JLabel lblSideBarRight;
private JLabel lblFiller;
private JLabel lblHeader;
private JLabel lblCopyright;
private JTextArea txtSubmission;
private JTextArea txtDiscussion;
private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
private JScrollPane scrollPane = null;
public ChatClient(String host)
{
super( "Let's Chat! Client" );
layout = new GridBagLayout();
setLayout( layout );
getContentPane().setBackground(new Color(0xFA, 0xF7, 0xF0) );
constraints = new GridBagConstraints();
Border border = BorderFactory.createLineBorder(new Color(0x09, 0x8A, 0xBD));
lblSideBar = new JLabel(" ");
lblSideBarRight = new JLabel(" ");
lblFiller = new JLabel(" ");
lblHeader = new JLabel("Let's Chat!");
lblCopyright = new JLabel( "Copyright 2015 Ninjas4Code" );
txtSubmission = new JTextArea();
txtSubmission.setBorder(border);
lblMessage = new JLabel();
Icon myIcon = new ImageIcon(getClass().getResource("chat-icon.png"));
lblHeader.setIcon(myIcon);
txtDiscussion = new JTextArea();
txtDiscussion.setBorder(border);
txtDiscussion.setBackground(new Color(0x09, 0x8A, 0xBD));
txtDiscussion.setForeground(Color.WHITE);
txtDiscussion.setBorder(border);
txtDiscussion.setLineWrap(true);
btnSubmission = new JButton(" Send ");
scrollPane = new JScrollPane(txtDiscussion,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
addComponent( btnSubmission, 5, 1, 2, 1 );
constraints.fill = GridBagConstraints.BOTH;
addComponent( lblSideBar, 1, 0, 1, 4 );
addComponent( lblSideBarRight, 1, 3, 1, 4 );
addComponent( lblMessage, 1, 1, 2, 1 );
addComponent( lblFiller, 4, 1, 2, 1 );
constraints.weightx = 1000;
constraints.weighty = 0.2;
constraints.fill = GridBagConstraints.BOTH;
addComponent(txtSubmission, 3, 1, 2, 1 );
constraints.weightx = 1000;
constraints.weighty = 2;
constraints.fill = GridBagConstraints.BOTH;
addComponent( scrollPane, 2, 1, 2, 1 );
constraints.weightx = 1000;
constraints.weighty = 0.4;
constraints.fill = GridBagConstraints.BOTH;
addComponent( lblCopyright, 6, 2, 1, 1 );
addComponent( lblHeader, 0, 2, 1, 1 );
chatServer = host;
btnSubmission.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
sendData(txtSubmission.getText());
txtSubmission.setText( "" );
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing( java.awt.event.WindowEvent e )
{
closeConnection();
System.exit(0);
}
});
setVisible( true );
}
private void addComponent( Component component,
int row, int column, int width, int height )
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints( component, constraints );
add( component );
}
public void runClient()
{
try
{
connectToServer();
getStreams();
processConnection();
}
catch ( EOFException eofException )
{
displayMessage( "\nClient terminated connection" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
finally
{
closeConnection();
}
}
private void connectToServer() throws IOException
{
displayMessage( "Attempting connection\n" );
client = new Socket( InetAddress.getByName( chatServer ), 12345 );
displayMessage( "Connected to: " + client.getInetAddress().getHostName() );
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream( client.getOutputStream() );
output.flush();
input = new ObjectInputStream( client.getInputStream() );
//displayMessage( "\nGot I/O streams\n" );
}
private void processConnection() throws IOException
{
setTextFieldEditable( true );
do
{
try
{
message = ( String ) input.readObject(); // read new message
displayMessage( "\n" + message ); // display message
}
catch ( ClassNotFoundException classNotFoundException )
{
displayMessage( "\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
}
private void closeConnection()
{
displayMessage( "\nClosing connection" );
setTextFieldEditable( false );
try
{
if(output != null)
{
output.close();
}
if(input != null)
{
input.close();
}
if(client != null)
{
client.close();
}
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
private void sendData( String message )
{
try
{
String dateStampWithMessage = "[" + dateFormat.format(new Date()) + "] " + message;
output.writeObject( "CLIENT>>> " + dateStampWithMessage );
output.flush();
displayMessage( "\nCLIENT>>> " + dateStampWithMessage);
}
catch ( IOException ioException )
{
txtDiscussion.append( "\nError writing object" );
}
}
private void displayMessage( final String messageToDisplay )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
txtDiscussion.append( messageToDisplay );
txtSubmission.requestFocus();
}
}
);
}
private void setTextFieldEditable( final boolean editable )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
txtSubmission.setEditable( editable );
}
}
);
}
}