-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUsernameGUI.java
109 lines (98 loc) · 2.83 KB
/
UsernameGUI.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
package Client;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
public class UsernameGUI {
public ErrorBoxGUI eBX = new ErrorBoxGUI("You need to Type a Username");
protected Shell shell;
private Text username;
private Client client;
public UsernameGUI(Client c){
client = c;
}
/**
* Open the window.
*/
public void close(){
shell.close();
}
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
* @wbp.parser.entryPoint
*/
protected void createContents() {
shell = new Shell(Display.getDefault(), SWT.CLOSE | SWT.TITLE);
shell.addShellListener(new ShellAdapter() {
@Override
public void shellClosed(ShellEvent e) {
if(username.getText().equals("")){ // Only closes the application,when user doesn't typed a username and closed it
System.exit(0);
}
}
});
shell.setSize(450, 100);
shell.setText("Username");
shell.setLayout(new GridLayout(2, false));
Label lblUsername = new Label(shell, SWT.NONE);
lblUsername.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblUsername.setText("Username:");
username = new Text(shell, SWT.BORDER);
username.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
try{
if(!username.getText().equals("")){
client.setUsername(username.getText());
close();
}else{
eBX.open();
}
}catch(Exception ex){
}
}
}
});
username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Button btnUsernameChose = new Button(shell, SWT.NONE);
btnUsernameChose.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try{
if(!username.getText().equals("")){
client.setUsername(username.getText());
close();
}else{
eBX.open();
}
}catch(Exception ex){
}
}
});
GridData gd_btnUsernameChose = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
gd_btnUsernameChose.widthHint = 424;
btnUsernameChose.setLayoutData(gd_btnUsernameChose);
btnUsernameChose.setText("Chose");
}
}