-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientGui.java
More file actions
321 lines (278 loc) · 11.6 KB
/
ClientGui.java
File metadata and controls
321 lines (278 loc) · 11.6 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.html.*;
import java.util.ArrayList;
import java.util.Arrays;
public class ClientGui extends Thread {
final JTextPane jtextFilDiscu = new JTextPane();
final JTextPane jtextListUsers = new JTextPane();
final JTextField jtextInputChat = new JTextField();
private String oldMsg = "";
private Thread read;
private String serverName;
private int PORT;
private String name;
BufferedReader input;
PrintWriter output;
Socket server;
public ClientGui() {
this.serverName = "localhost";
this.PORT = 12345;
this.name = "nickname";
String fontfamily = "Arial, sans-serif";
Font font = new Font(fontfamily, Font.PLAIN, 15);
final JFrame jfr = new JFrame("Chat");
jfr.getContentPane().setLayout(null);
jfr.setSize(700, 500);
jfr.setResizable(false);
jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Chat message area
jtextFilDiscu.setBounds(25, 25, 490, 320);
jtextFilDiscu.setFont(font);
jtextFilDiscu.setMargin(new Insets(6, 6, 6, 6));
jtextFilDiscu.setEditable(false);
JScrollPane jtextFilDiscuSP = new JScrollPane(jtextFilDiscu);
jtextFilDiscuSP.setBounds(25, 25, 490, 320);
jtextFilDiscu.setContentType("text/html");
jtextFilDiscu.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
// User list area
jtextListUsers.setBounds(520, 25, 156, 320);
jtextListUsers.setFont(font);
jtextListUsers.setMargin(new Insets(6, 6, 6, 6));
jtextListUsers.setEditable(false);
JScrollPane jsplistuser = new JScrollPane(jtextListUsers);
jsplistuser.setBounds(520, 25, 156, 320);
jtextListUsers.setContentType("text/html");
jtextListUsers.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
// Message input field for the user
jtextInputChat.setBounds(0, 350, 400, 50);
jtextInputChat.setFont(font);
jtextInputChat.setMargin(new Insets(6, 6, 6, 6));
final JScrollPane jtextInputChatSP = new JScrollPane(jtextInputChat);
jtextInputChatSP.setBounds(25, 350, 650, 50);
// Send button
final JButton jsbtn = new JButton("Send");
jsbtn.setFont(font);
jsbtn.setBounds(575, 410, 100, 35);
// Disconnect button
final JButton jsbtndeco = new JButton("Disconnect");
jsbtndeco.setFont(font);
jsbtndeco.setBounds(25, 410, 130, 35);
// Key listener for input field
jtextInputChat.addKeyListener(new KeyAdapter() {
// Send message on Enter
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
sendMessage();
}
// Recall the last typed message
if (e.getKeyCode() == KeyEvent.VK_UP) {
String currentMessage = jtextInputChat.getText().trim();
jtextInputChat.setText(oldMsg);
oldMsg = currentMessage;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
String currentMessage = jtextInputChat.getText().trim();
jtextInputChat.setText(oldMsg);
oldMsg = currentMessage;
}
}
});
// Action for send button
jsbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sendMessage();
}
});
// Connection input fields
final JTextField jtfName = new JTextField(this.name);
final JTextField jtfport = new JTextField(Integer.toString(this.PORT));
final JTextField jtfAddr = new JTextField(this.serverName);
final JButton jcbtn = new JButton("Connect");
// Enable connect button only if fields are not empty
jtfName.getDocument().addDocumentListener(new TextListener(jtfName, jtfport, jtfAddr, jcbtn));
jtfport.getDocument().addDocumentListener(new TextListener(jtfName, jtfport, jtfAddr, jcbtn));
jtfAddr.getDocument().addDocumentListener(new TextListener(jtfName, jtfport, jtfAddr, jcbtn));
// Set positions of components
jcbtn.setFont(font);
jtfAddr.setBounds(25, 380, 135, 40);
jtfName.setBounds(375, 380, 135, 40);
jtfport.setBounds(200, 380, 135, 40);
jcbtn.setBounds(575, 380, 100, 40);
// Default background colors for chat and user list
jtextFilDiscu.setBackground(Color.LIGHT_GRAY);
jtextListUsers.setBackground(Color.LIGHT_GRAY);
// Add components to frame
jfr.add(jcbtn);
jfr.add(jtextFilDiscuSP);
jfr.add(jsplistuser);
jfr.add(jtfName);
jfr.add(jtfport);
jfr.add(jtfAddr);
jfr.setVisible(true);
// Chat instructions
appendToPane(jtextFilDiscu, "<h4>Available chat commands:</h4>"
+ "<ul>"
+ "<li><b>@nickname</b> to send a private message to user 'nickname'</li>"
+ "<li><b>#d3961b</b> to change the color of your nickname using a hex code</li>"
+ "<li><b>;)</b> some smileys are supported</li>"
+ "<li><b>Up arrow</b> to recall the last typed message</li>"
+ "</ul><br/>");
// Connect button action
jcbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
name = jtfName.getText();
String port = jtfport.getText();
serverName = jtfAddr.getText();
PORT = Integer.parseInt(port);
appendToPane(jtextFilDiscu, "<span>Connecting to " + serverName + " on port " + PORT + "...</span>");
server = new Socket(serverName, PORT);
appendToPane(jtextFilDiscu, "<span>Connected to " +
server.getRemoteSocketAddress() + "</span>");
input = new BufferedReader(new InputStreamReader(server.getInputStream()));
output = new PrintWriter(server.getOutputStream(), true);
// Send nickname to server
output.println(name);
// Start thread to read incoming messages
read = new Read();
read.start();
// Switch UI to chat mode
jfr.remove(jtfName);
jfr.remove(jtfport);
jfr.remove(jtfAddr);
jfr.remove(jcbtn);
jfr.add(jsbtn);
jfr.add(jtextInputChatSP);
jfr.add(jsbtndeco);
jfr.revalidate();
jfr.repaint();
jtextFilDiscu.setBackground(Color.WHITE);
jtextListUsers.setBackground(Color.WHITE);
} catch (Exception ex) {
appendToPane(jtextFilDiscu, "<span>Could not connect to server</span>");
JOptionPane.showMessageDialog(jfr, ex.getMessage());
}
}
});
// Disconnect button action
jsbtndeco.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jfr.add(jtfName);
jfr.add(jtfport);
jfr.add(jtfAddr);
jfr.add(jcbtn);
jfr.remove(jsbtn);
jfr.remove(jtextInputChatSP);
jfr.remove(jsbtndeco);
jfr.revalidate();
jfr.repaint();
read.interrupt();
jtextListUsers.setText(null);
jtextFilDiscu.setBackground(Color.LIGHT_GRAY);
jtextListUsers.setBackground(Color.LIGHT_GRAY);
appendToPane(jtextFilDiscu, "<span>Connection closed.</span>");
output.close();
}
});
}
// Listener to enable/disable Connect button based on input fields
public class TextListener implements DocumentListener {
JTextField jtf1;
JTextField jtf2;
JTextField jtf3;
JButton jcbtn;
public TextListener(JTextField jtf1, JTextField jtf2, JTextField jtf3, JButton jcbtn) {
this.jtf1 = jtf1;
this.jtf2 = jtf2;
this.jtf3 = jtf3;
this.jcbtn = jcbtn;
}
public void changedUpdate(DocumentEvent e) {
}
public void removeUpdate(DocumentEvent e) {
if (jtf1.getText().trim().equals("") ||
jtf2.getText().trim().equals("") ||
jtf3.getText().trim().equals("")) {
jcbtn.setEnabled(false);
} else {
jcbtn.setEnabled(true);
}
}
public void insertUpdate(DocumentEvent e) {
if (jtf1.getText().trim().equals("") ||
jtf2.getText().trim().equals("") ||
jtf3.getText().trim().equals("")) {
jcbtn.setEnabled(false);
} else {
jcbtn.setEnabled(true);
}
}
}
// Send a message
public void sendMessage() {
try {
String message = jtextInputChat.getText().trim();
if (message.equals("")) {
return;
}
this.oldMsg = message;
output.println(message);
jtextInputChat.requestFocus();
jtextInputChat.setText(null);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
System.exit(0);
}
}
public static void main(String[] args) throws Exception {
ClientGui client = new ClientGui();
}
// Thread to read incoming messages from server
class Read extends Thread {
public void run() {
String message;
while (!Thread.currentThread().isInterrupted()) {
try {
message = input.readLine();
if (message != null) {
if (message.charAt(0) == '[') {
message = message.substring(1, message.length() - 1);
ArrayList<String> ListUser = new ArrayList<String>(
Arrays.asList(message.split(", "))
);
jtextListUsers.setText(null);
for (String user : ListUser) {
appendToPane(jtextListUsers, "@" + user);
}
} else {
appendToPane(jtextFilDiscu, message);
}
}
} catch (IOException ex) {
System.err.println("Failed to read incoming message");
}
}
}
}
// Append HTML message to JTextPane
private void appendToPane(JTextPane tp, String msg) {
HTMLDocument doc = (HTMLDocument) tp.getDocument();
HTMLEditorKit editorKit = (HTMLEditorKit) tp.getEditorKit();
try {
editorKit.insertHTML(doc, doc.getLength(), msg, 0, 0, null);
tp.setCaretPosition(doc.getLength());
} catch (Exception e) {
e.printStackTrace();
}
}
}