-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerGUI.java
172 lines (156 loc) · 4.83 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
package chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;
/*
* The server as a GUI
*/
public class ServerGUI extends JFrame implements ActionListener, WindowListener {
private static final long serialVersionUID = 1L;
// the stop and start buttons
private JButton stopStart;
// JTextArea for the chat room and the events
private JTextArea chat, event;
// The port number
private JTextField tPortNumber;
// my server
private Server server;
// server constructor that receive the port to listen to for connection as parameter
ServerGUI(int port) {
super("Chat Server");
setResizable(false);
getContentPane().setBackground(Color.BLACK);
server = null;
// in the NorthPanel the PortNumber the Start and Stop buttons
JPanel north = new JPanel();
north.setBackground(new Color(6, 94, 82));
JLabel label = new JLabel("Port number: ");
label.setForeground(Color.WHITE);
label.setFont(new Font("Arial", Font.BOLD, 11));
north.add(label);
tPortNumber = new JTextField(" " + port);
tPortNumber.setBackground(new Color(255, 255, 255));
tPortNumber.setCaretColor(new Color(0, 0, 0));
tPortNumber.setForeground(new Color(0, 0, 0));
tPortNumber.setFont(new Font("Monospaced", Font.PLAIN, 11));
tPortNumber.setBorder(new LineBorder(new Color(171, 173, 179)));
tPortNumber.setSelectedTextColor(new Color(255, 255, 255));
tPortNumber.setSelectionColor(new Color(51, 153, 255));
north.add(tPortNumber);
getContentPane().add(north, BorderLayout.NORTH);
// to stop or start the server, we start with "Start"
stopStart = new JButton("Start");
stopStart.setForeground(Color.WHITE);
stopStart.setContentAreaFilled(false);
stopStart.setFont(new Font("Arial", Font.PLAIN, 11));
stopStart.addActionListener(this);
north.add(stopStart);
// the event and chat room
JPanel center = new JPanel(new GridLayout(2,1));
chat = new JTextArea(5,5);
chat.setForeground(new Color(0, 0, 0));
chat.setBackground(new Color(204, 204, 255));
chat.setSelectionColor(new Color(51, 153, 255));
chat.setSelectedTextColor(new Color(255, 255, 255));
chat.setEditable(false);
appendRoom("Chat room.\n");
center.add(new JScrollPane(chat));
event = new JTextArea(5,5);
event.setForeground(new Color(0, 0, 0));
event.setBackground(new Color(204, 204, 255));
event.setSelectionColor(new Color(51, 153, 255));
event.setSelectedTextColor(new Color(255, 255, 255));
event.setEditable(false);
appendEvent("Events log.\n");
center.add(new JScrollPane(event));
getContentPane().add(center);
// need to be informed when the user click the close button on the frame
addWindowListener(this);
setSize(400, 600);
setVisible(true);
}
// append message to the two JTextArea
// position at the end
void appendRoom(String str) {
chat.append(str);
chat.setCaretPosition(chat.getText().length() - 1);
}
void appendEvent(String str) {
event.append(str);
try{
event.setCaretPosition(chat.getText().length() - 1);
}
catch (IllegalArgumentException a){
}
}
// start or stop where clicked
public void actionPerformed(ActionEvent e) {
// if running we have to stop
if(server != null) {
server.stop();
server = null;
tPortNumber.setEditable(true);
stopStart.setText("Start");
return;
}
// OK start the server
int port;
try {
port = Integer.parseInt(tPortNumber.getText().trim());
}
catch(Exception er) {
appendEvent("Invalid port number");
return;
}
// create a new Server
server = new Server(port, this);
// and start it as a thread
new ServerRunning().start();
stopStart.setText("Stop");
tPortNumber.setEditable(false);
}
// entry point to start the Server
public static void main(String[] arg) {
// start server default port 1500
new ServerGUI(1500);
}
/*
* If the user click the X button to close the application
* I need to close the connection with the server to free the port
*/
public void windowClosing(WindowEvent e) {
// if my Server exist
if(server != null) {
try {
server.stop(); // ask the server to close the connection
}
catch(Exception eClose) {
}
server = null;
}
// dispose the frame
dispose();
System.exit(0);
}
// I can ignore the other WindowListener method
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
/*
* A thread to run the Server
*/
class ServerRunning extends Thread {
public void run() {
server.start(); // should execute until ii fails
// the server failed
stopStart.setText("Start");
tPortNumber.setEditable(true);
appendEvent("Server interrupted.\n");
server = null;
}
}
}