-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionUI.java
More file actions
353 lines (316 loc) · 9.84 KB
/
Copy pathConnectionUI.java
File metadata and controls
353 lines (316 loc) · 9.84 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
/**
* Connection UI class defines the dialog that appears when pressing "new connection"
* Separate from the UI class as it has a different UI appearance, and has a significant amount of
* unique functionality not shared with the UI.
*
* @author Maxence Weyrich
*
*/
public class ConnectionUI extends JDialog {
private static final long serialVersionUID = 1L;
private final JPanel contentPanel = new JPanel();
private JTextField IPAddress;
private JTextField portNumber;
private JTextField userNameField;
private JPasswordField passwordField;
private boolean hasCustomUserName, hasPort, hasIPAddress, hasPassword;
private JButton connectButton;
private JLabel connectionResults;
private Client client;
/**
* Create the dialog.
* @param client the Client object needed to perform non-UI tasks
*/
public ConnectionUI(Client client) {
this.client = client;
hasCustomUserName = hasPort = hasIPAddress = hasPassword = false;
setType(Type.UTILITY);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 640, 100);
getContentPane().setLayout(new BorderLayout());
contentPanel.setFont(new Font("Tahoma", Font.PLAIN, 14));
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
IPAddress = new JTextField();
IPAddress.setCaretColor(Color.BLACK);
IPAddress.setText("IP Address");
IPAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
IPAddress.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if(!hasIPAddress) {
IPAddress.setText("");
hasIPAddress = true;
}
}
@Override
public void focusLost(FocusEvent arg0) {
if(IPAddress.getText().equals("")) {
IPAddress.setText("IP Address");
hasIPAddress = false;
} else {
hasIPAddress = true;
}
}
});
IPAddress.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleClick();
}
});
IPAddress.setBounds(325, 15, 130, 20);
contentPanel.add(IPAddress);
IPAddress.setColumns(10);
}
{
portNumber = new JTextField();
portNumber.setText("58755");
hasPort = true;
portNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
portNumber.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if(!hasPort) {
portNumber.setText("");
hasPort = true;
}
}
@Override
public void focusLost(FocusEvent arg0) {
if(portNumber.getText().equals("")) {
portNumber.setText("Port");
hasPort = false;
} else {
hasPort = true;
}
}
});
portNumber.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleClick();
}
});
portNumber.setBounds(466, 15, 50, 20);
contentPanel.add(portNumber);
portNumber.setColumns(10);
}
{
userNameField = new JTextField();
userNameField.setText("Username");
userNameField.setFont(new Font("Tahoma", Font.PLAIN, 14));
userNameField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if(!hasCustomUserName) {
userNameField.setText("");
hasCustomUserName = true;
}
}
@Override
public void focusLost(FocusEvent arg0) {
if(userNameField.getText().equals("")) {
userNameField.setText("Username");
hasCustomUserName = false;
} else {
hasCustomUserName = true;
}
}
});
userNameField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleClick();
}
});
userNameField.setBounds(10, 15, 150, 20);
contentPanel.add(userNameField);
userNameField.setColumns(10);
}
{
passwordField = new JPasswordField();
passwordField.setText("Password");
passwordField.setEchoChar('\0');
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 14));
passwordField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if(!hasPassword) {
passwordField.setText("");
passwordField.setEchoChar((char)8226);
hasPassword = true;
}
}
@Override
public void focusLost(FocusEvent arg0) {
if(passwordField.getPassword().length == 0) {
passwordField.setText("Password");
passwordField.setEchoChar('\0');
hasPassword = false;
} else {
hasPassword = true;
}
}
});
passwordField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleClick();
}
});
passwordField.setBounds(165, 15, 150, 20);
contentPanel.add(passwordField);
}
{
connectButton = new JButton("Connect...");
connectButton.setFocusable(false);
connectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
handleClick();
}
});
connectButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
connectButton.setBounds(526, 15, 100, 20);
contentPanel.add(connectButton);
}
JLabel seperatorText = new JLabel(":");
seperatorText.setFocusable(false);
seperatorText.setFont(new Font("Tahoma", Font.BOLD, 14));
seperatorText.setBounds(458, 14, 5, 20);
contentPanel.add(seperatorText);
connectionResults = new JLabel("Not connected");
connectionResults.setFont(new Font("Tahoma", Font.PLAIN, 12));
connectionResults.setBounds(10, 45, 620, 14);
contentPanel.add(connectionResults);
connectionResults.requestFocusInWindow();
}
/**
* Disables the input boxes when attempting to connect
* Serves to show user that program is currently working, and to prevent user from changing input once started
*
* @param enabled boolean: true will enable all buttons, false will disable everything
*/
private void changeEnabledStatus(boolean enabled) {
IPAddress.setEnabled(enabled);
portNumber.setEnabled(enabled);
userNameField.setEnabled(enabled);
passwordField.setEnabled(enabled);
connectButton.setEnabled(enabled);
}
/**
* Handles when the user has pressed the connect button.
* Will begin to process all input data, verify that data is correctly entered, and attempt to connect.
* On failure, it will also update the connection result text with a reason for the failure, and re-enable
* all fields for the user to update.
*
* Successfully connecting to the server will dismiss the connection dialog.
*/
public void handleClick() {
changeEnabledStatus(false);
InetAddress addr;
int portNo;
String userName;
char[] password;
userName = userNameField.getText().trim();
if(userName.length() == 0 || !hasCustomUserName) {
connectionResults.setText("Connection failed - Enter a valid username");
changeEnabledStatus(true);
userNameField.requestFocusInWindow();
return;
} else if(!userName.matches("\\w{1,10}")) {
connectionResults.setText("Connection failed - Username must be alphanumeric of 1 - 10 characters");
changeEnabledStatus(true);
userNameField.requestFocusInWindow();
return;
}
if(!hasPort) {
connectionResults.setText("Connection failed - Enter a port number");
changeEnabledStatus(true);
portNumber.requestFocusInWindow();
return;
}
if(!hasIPAddress) {
connectionResults.setText("Connection failed - Enter an IP address");
changeEnabledStatus(true);
IPAddress.requestFocusInWindow();
return;
}
try {
portNo = Integer.parseInt(portNumber.getText());
if(portNo < 0 || portNo > 65535) {
connectionResults.setText("Connection failed - Port number must be between 0 - 65535");
changeEnabledStatus(true);
portNumber.requestFocusInWindow();
return;
}
} catch(Exception e) {
connectionResults.setText("Connection failed - Bad port number");
changeEnabledStatus(true);
portNumber.requestFocusInWindow();
return;
}
try {
if(!IPAddress.getText().matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) {
throw new UnknownHostException();
}
addr = InetAddress.getByName(IPAddress.getText());
} catch(Exception e) {
connectionResults.setText("Connection failed - Bad IP address");
changeEnabledStatus(true);
IPAddress.requestFocusInWindow();
return;
}
if(hasPassword) {
password = passwordField.getPassword();
} else {
password = new char[]{}; //empty character array
}
// input has passed all checks. now attempt to connect.
connectionResults.setText("Connecting...");
Thread t = new Thread(new Runnable(){
public void run() {
try {
client.connect(userName, password, addr, portNo);
} catch(IOException e) {
connectionResults.setText("Connection failed - " + e.getMessage());
changeEnabledStatus(true);
}
}
});
t.start();
}
/**
* Sets the connection result to "Connection failed - REASON" where REASON is the
* string given as an argument
* @param message String the reason for which the connection failed
*/
public void setConnectionResult(String message) {
connectionResults.setText("Connection failed - " + message.trim());
changeEnabledStatus(true);
}
/**
* Re-enables the text fields, and resets the connection result text area to defaults.
* NOTE that it will keep previously entered data rather than clearing it.
*/
public void reset() {
connectionResults.setText("Not connected.");
changeEnabledStatus(true);
}
}