-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerGUI.java
245 lines (176 loc) · 5.37 KB
/
ServerGUI.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
import javax.swing.*;
import serveur.EchoServer;
import common.ChatIF;
import java.awt.*;
import java.awt.event.*;
public class ServerGUI extends JFrame implements MouseListener, ActionListener,
KeyListener, ChatIF {
private static final long serialVersionUID = 1L;
JPanel panFenetre;
JPanel panStart;
JPanel panAff;
JPanel panSaisie;
BorderLayout borderLayout;
FlowLayout flowLayout;
Dimension tailleFenetre;
JLabel askStart;
JButton confirmStart;
JButton envoiMsg;
JTextPane zoneSaisie;
JTextPane zoneAffichage;
JMenuItem itemStart;
JScrollPane sc;
Color backgroundColor = new Color(27,109,182);
final public static int DEFAULT_PORT = 5555;
EchoServer server;
public ServerGUI(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(2 * largeurEcran / 3, hauteurEcran / 4);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
borderLayout = new BorderLayout();
flowLayout = new FlowLayout();
fillStartFrame();
fillChatFrame();
this.setVisible(true);
server = new EchoServer(port, this);
}
public void fillStartFrame() {
int inputHeight = 20;
int verticalPosition = tailleFenetre.width / 2 - 80;
this.setTitle("Server");
panFenetre = new JPanel();
panFenetre.setBackground(backgroundColor);
panFenetre.setLayout(borderLayout);
askStart = new JLabel("Welcome Server, please click to proceed");
confirmStart = new JButton("Start");
confirmStart.addActionListener(this);
confirmStart.addKeyListener(this);
askStart.setBounds(30, 20, 340, inputHeight);
confirmStart.setBounds(verticalPosition, askStart.getHeight() + 30, 150,
inputHeight);
panStart = new JPanel();
panStart.setBackground(backgroundColor);
panStart.setLayout(null);
panStart.add(askStart);
panStart.add(confirmStart);
panFenetre.add(panStart,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.server.handleMessageFromServerUI(message);
this.zoneSaisie.setText("");
if (message.equals("#close")) {
goToStartFrame();
}
}
private void start() {
this.server.handleMessageFromServerUI("#start");
}
private void goToChatFrame() {
panStart.setVisible(false);
panFenetre.remove(panStart);
panFenetre.setLayout(flowLayout);
panFenetre.setBackground(null);
panAff.setVisible(true);
panSaisie.setVisible(true);
panFenetre.add(this.panAff);
panFenetre.add(this.panSaisie);
}
private void goToStartFrame() {
this.zoneAffichage.setText("");
panAff.setVisible(false);
panSaisie.setVisible(false);
panFenetre.remove(this.panAff);
panFenetre.remove(this.panSaisie);
panFenetre.setLayout(borderLayout);
panFenetre.setBackground(backgroundColor);
panStart.setVisible(true);
panFenetre.add(panStart,BorderLayout.CENTER);
}
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.confirmStart) {
this.confirmStart();
}
}
}
public void confirmStart() {
start();
goToChatFrame();
}
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) {
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == confirmStart || e.getSource() == this.itemStart) {
this.confirmStart();
}
}
public static void main(String[] args) {
ServerGUI sv;
int port = 0; //Port to listen on
try {
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwable t) {
port = DEFAULT_PORT; //Set port to 5555
}
sv = new ServerGUI(port);
sv.accept(); //Put welcome message
}
}