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
|
package cuchaz.enigma.gui.dialog;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import cuchaz.enigma.gui.elements.ValidatableTextField;
import cuchaz.enigma.network.EnigmaServer;
import cuchaz.enigma.utils.Pair;
import cuchaz.enigma.utils.ServerAddress;
import cuchaz.enigma.utils.validation.Message;
public class ConnectToServerDialog extends AbstractDialog {
private JTextField usernameField;
private ValidatableTextField ipField;
private JPasswordField passwordField;
public ConnectToServerDialog(Frame owner) {
super(owner, "prompt.connect.title", "prompt.connect.confirm", "prompt.cancel");
setSize(new Dimension(400, 185));
setLocationRelativeTo(owner);
}
@Override
protected List<Pair<String, Component>> createComponents() {
usernameField = new JTextField(System.getProperty("user.name"));
ipField = new ValidatableTextField();
passwordField = new JPasswordField();
usernameField.addActionListener(event -> confirm());
ipField.addActionListener(event -> confirm());
passwordField.addActionListener(event -> confirm());
return Arrays.asList(
new Pair<>("prompt.connect.username", usernameField),
new Pair<>("prompt.connect.address", ipField),
new Pair<>("prompt.password", passwordField)
);
}
public void validateInputs() {
vc.setActiveElement(ipField);
if (ipField.getText().trim().isEmpty()) {
vc.raise(Message.EMPTY_FIELD);
} else if (ServerAddress.from(ipField.getText(), EnigmaServer.DEFAULT_PORT) == null) {
vc.raise(Message.INVALID_IP);
}
}
public Result getResult() {
if (!isActionConfirm()) return null;
vc.reset();
validateInputs();
if (!vc.canProceed()) return null;
return new Result(
usernameField.getText(),
Objects.requireNonNull(ServerAddress.from(ipField.getText(), EnigmaServer.DEFAULT_PORT)),
passwordField.getPassword()
);
}
public static Result show(Frame parent) {
ConnectToServerDialog d = new ConnectToServerDialog(parent);
d.setVisible(true);
Result r = d.getResult();
d.dispose();
return r;
}
public static class Result {
private final String username;
private final ServerAddress address;
private final char[] password;
public Result(String username, ServerAddress address, char[] password) {
this.username = username;
this.address = address;
this.password = password;
}
public String getUsername() {
return username;
}
public ServerAddress getAddress() {
return address;
}
public char[] getPassword() {
return password;
}
}
}
|