-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathConnectGUI.java
132 lines (115 loc) · 3.42 KB
/
ConnectGUI.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
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 ConnectGUI {
protected Shell shell;
protected Text server;
protected Text port;
private Client client;
public ConnectGUI(Client c){
client = c;
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void close(){
shell.close();
}
/**
* 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(!client.getIsConnected()){ // Only closes the application,when user doesn't want to connect
System.exit(0);
}
}
});
shell.setSize(450, 125);
shell.setText("Connect");
shell.setLayout(new GridLayout(2, false));
Label lblServer = new Label(shell, SWT.NONE);
lblServer.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblServer.setText("Server:");
server = new Text(shell, SWT.BORDER);
server.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
try{
client.setHost(server.getText());
client.setPortNumber(Integer.parseInt(port.getText()));
client.tryConnect();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
});
server.setText("Admin-PC");
server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblPort = new Label(shell, SWT.NONE);
lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblPort.setText("Port:");
port = new Text(shell, SWT.BORDER);
port.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
try{
client.setHost(server.getText());
client.setPortNumber(Integer.parseInt(port.getText()));
client.tryConnect();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
});
port.setText("5000");
port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Button btnConnect = new Button(shell, SWT.NONE);
btnConnect.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try{
client.setHost(server.getText());
client.setPortNumber(Integer.parseInt(port.getText()));
client.tryConnect();
}catch(Exception ex){
ex.printStackTrace();
}
}
});
GridData gd_btnConnect = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
gd_btnConnect.widthHint = 424;
btnConnect.setLayoutData(gd_btnConnect);
btnConnect.setText("Connect");
}
}