-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientGUI.java
306 lines (235 loc) · 6.93 KB
/
ClientGUI.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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import client.*;
import common.*;
public class ClientGUI extends JFrame implements MouseListener, ActionListener,
KeyListener, ChatIF {
private static final long serialVersionUID = 1L;
JPanel panFenetre;
JPanel panLogin;
JPanel panAff;
JPanel panSaisie;
BorderLayout borderLayout;
FlowLayout flowLayout;
Dimension tailleFenetre;
JLabel askLogin;
JTextField saisieLogin;
JButton confirmLogin;
JButton envoiMsg;
JTextPane zoneSaisie;
JTextPane zoneAffichage;
JScrollPane sc;
Color backgroundColor = new Color(27,109,182);
final public static int DEFAULT_PORT = 5555;
ChatClient client;
public ClientGUI(String id, String host, int port) {
Dimension dimensions = (Toolkit.getDefaultToolkit()).getScreenSize();
int hauteurEcran = dimensions.height;
int largeurEcran = dimensions.width;
tailleFenetre = new Dimension (400, 360);
this.setSize(tailleFenetre);
this.setResizable(false);
this.setLocation(largeurEcran / 3, hauteurEcran / 4);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
borderLayout = new BorderLayout();
flowLayout = new FlowLayout();
fillLoginFrame();
fillChatFrame();
this.setVisible(true);
try {
client = new ChatClient(id, host, port, this);
}
catch(IOException exception) {
this.display("Error: Can't setup connection!");
System.exit(1);
}
}
public void fillLoginFrame() {
int inputHeight = 20;
int verticalPosition = tailleFenetre.width / 2 - 80;
this.setTitle("Client");
panFenetre = new JPanel();
panFenetre.setBackground(backgroundColor);
panFenetre.setLayout(borderLayout);
askLogin = new JLabel("Welcome ! Please tape your login");
saisieLogin = new JTextField("");
confirmLogin = new JButton("Connect");
saisieLogin.addKeyListener(this);
saisieLogin.addMouseListener(this);
confirmLogin.addKeyListener(this);
confirmLogin.addActionListener(this);
askLogin.setBounds(verticalPosition, 0, 300, 20);
saisieLogin.setBounds(verticalPosition, askLogin.getHeight() + 10, 150,
inputHeight);
confirmLogin.setBounds(verticalPosition,
askLogin.getHeight() + saisieLogin.getHeight() + 20, 150,
inputHeight);
panLogin = new JPanel();
panLogin.setBackground(backgroundColor);
panLogin.setLayout(null);
panLogin.add(askLogin);
panLogin.add(saisieLogin);
panLogin.add(confirmLogin);
panFenetre.add(panLogin,BorderLayout.CENTER);
this.add(panFenetre);
}
public void fillChatFrame() {
int outputHeight = 240;
int inputHeight = 60;
panAff = new JPanel();
panAff.setPreferredSize(new Dimension(tailleFenetre.width,
outputHeight));
panAff.setLayout(new BorderLayout());
zoneAffichage = new JTextPane();
zoneAffichage.setAutoscrolls(true);
zoneAffichage.setEditable(false);
zoneAffichage.setForeground(backgroundColor);
sc = new JScrollPane(zoneAffichage);
sc.setPreferredSize(new Dimension(tailleFenetre.width, outputHeight));
panAff.add(sc);
panSaisie = new JPanel();
panSaisie.setPreferredSize(new Dimension(tailleFenetre.width - 10,
inputHeight));
panSaisie.setLayout(new BorderLayout());
zoneSaisie = new JTextPane();
envoiMsg = new JButton("Send");
zoneSaisie.setPreferredSize(new Dimension(320, inputHeight));
zoneSaisie.addKeyListener(this);
envoiMsg.addActionListener(this);
panSaisie.add(zoneSaisie,BorderLayout.WEST);
panSaisie.add(envoiMsg,BorderLayout.EAST);
}
private void send(String message) {
this.client.handleMessageFromClientUI(message);
this.zoneSaisie.setText("");
this.setTitle("Client " + client.getId());
if (message.equals("#logoff")) {
goToLoginFrame();
}
}
private void goToChatFrame(String login) {
this.setTitle("Client " + login);
panLogin.setVisible(false);
panFenetre.remove(panLogin);
panFenetre.setLayout(flowLayout);
panFenetre.setBackground(null);
panAff.setVisible(true);
panSaisie.setVisible(true);
panFenetre.add(this.panAff);
panFenetre.add(this.panSaisie);
}
private void goToLoginFrame() {
this.setTitle("Client");
this.zoneAffichage.setText("");
panAff.setVisible(false);
panSaisie.setVisible(false);
panFenetre.remove(this.panAff);
panFenetre.remove(this.panSaisie);
panFenetre.setLayout(borderLayout);
panFenetre.setBackground(backgroundColor);
panLogin.setVisible(true);
panFenetre.add(this.panLogin, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == confirmLogin) {
this.confirmLogin();
}
if (e.getSource() == envoiMsg) {
send(this.zoneSaisie.getText());
}
if (e.getSource() == saisieLogin) {
saisieLogin.setText("");
}
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\n') {
if (e.getSource() == this.zoneSaisie) {
String msg = this.zoneSaisie.getText();
send(msg.substring(0, msg.length() - 1));
}
else if (e.getSource() == this.confirmLogin
|| e.getSource() == this.saisieLogin) {
this.confirmLogin();
}
}
}
public void confirmLogin() {
String login = saisieLogin.getText();
String msg;
saisieLogin.setText("");
this.client.handleMessageFromClientUI("#login " + login);
if (login.equals(this.client.getId())) {
if(this.client.clientObs.isConnected()) {
this.setTitle("Client " + login);
goToChatFrame(login);
}
else {
msg = "The server isn't currently connected.";
JOptionPane.showMessageDialog(this, msg,
"Warning",
JOptionPane.WARNING_MESSAGE);
}
}
else {
msg = "You must enter a correct login.";
JOptionPane.showMessageDialog(this, msg,
"Warning",
JOptionPane.WARNING_MESSAGE);
}
}
public void display(String message) {
this.zoneAffichage.setText(this.zoneAffichage.getText() +
'\n' + message);
zoneAffichage.setCaretPosition(zoneAffichage.getText().length());
}
public void accept() {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
public void keyPressed(KeyEvent arg0) {
}
public void keyReleased(KeyEvent arg0) {
}
public static void main(String[] args) {
ClientGUI chat;
String host = "";
String id = "";
int port = 0; //The port number
try {
id = args[0];
}
catch(ArrayIndexOutOfBoundsException e) {
id = "without id";
}
try {
host = args[2];
}
catch(ArrayIndexOutOfBoundsException e) {
host = "localhost";
}
catch(NumberFormatException e) {
host = "localhost";
}
try {
port = Integer.parseInt(args[1]);
}
catch(ArrayIndexOutOfBoundsException e) {
port = DEFAULT_PORT;
}
catch(NumberFormatException e) {
port = DEFAULT_PORT;
}
chat = new ClientGUI(id, host, port);
chat.accept(); //Put welcome message
}
}