summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'enigma-swing/src/main/java')
-rw-r--r--enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java37
-rw-r--r--enigma-swing/src/main/java/cuchaz/enigma/gui/elements/VerifiableTextField.java83
2 files changed, 21 insertions, 99 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java
index 4bac97b6..eede946c 100644
--- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java
+++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java
@@ -9,15 +9,19 @@ import java.util.stream.Stream;
9 9
10import javax.swing.*; 10import javax.swing.*;
11 11
12import cuchaz.enigma.gui.elements.VerifiableTextField; 12import cuchaz.enigma.gui.elements.ValidatableTextField;
13import cuchaz.enigma.network.EnigmaServer; 13import cuchaz.enigma.network.EnigmaServer;
14import cuchaz.enigma.utils.I18n; 14import cuchaz.enigma.utils.I18n;
15import cuchaz.enigma.utils.ServerAddress; 15import cuchaz.enigma.utils.ServerAddress;
16import cuchaz.enigma.utils.validation.Message;
17import cuchaz.enigma.utils.validation.ValidationContext;
16 18
17public class ConnectToServerDialog extends JDialog { 19public class ConnectToServerDialog extends JDialog {
18 20
21 private final ValidationContext vc = new ValidationContext();
22
19 private final JTextField usernameField; 23 private final JTextField usernameField;
20 private final VerifiableTextField ipField; 24 private final ValidatableTextField ipField;
21 private final JPasswordField passwordField; 25 private final JPasswordField passwordField;
22 private boolean success = false; 26 private boolean success = false;
23 27
@@ -29,7 +33,7 @@ public class ConnectToServerDialog extends JDialog {
29 Container inputContainer = new JPanel(new GridBagLayout()); 33 Container inputContainer = new JPanel(new GridBagLayout());
30 GridBagConstraints c = new GridBagConstraints(); 34 GridBagConstraints c = new GridBagConstraints();
31 usernameField = new JTextField(System.getProperty("user.name")); 35 usernameField = new JTextField(System.getProperty("user.name"));
32 ipField = new VerifiableTextField(); 36 ipField = new ValidatableTextField();
33 passwordField = new JPasswordField(); 37 passwordField = new JPasswordField();
34 38
35 List<JLabel> labels = Stream.of("prompt.connect.username", "prompt.connect.address", "prompt.password") 39 List<JLabel> labels = Stream.of("prompt.connect.username", "prompt.connect.address", "prompt.password")
@@ -76,31 +80,32 @@ public class ConnectToServerDialog extends JDialog {
76 } 80 }
77 81
78 private void confirm() { 82 private void confirm() {
79 if (validateInputs()) { 83 vc.reset();
84 validateInputs();
85 if (vc.canProceed()) {
80 success = true; 86 success = true;
81 setVisible(false); 87 setVisible(false);
82 } 88 }
83 } 89 }
84 90
85 private void cancel() { 91 private void cancel() {
86 success = false; 92 success = false;
87 setVisible(false); 93 setVisible(false);
88 } 94 }
89 95
90 public boolean validateInputs() { 96 public void validateInputs() {
91 boolean error = false; 97 vc.setActiveElement(ipField);
92 ipField.clearErrorState(); 98 if (ipField.getText().trim().isEmpty()) {
93 99 vc.raise(Message.EMPTY_FIELD);
94 if (ServerAddress.from(ipField.getText(), EnigmaServer.DEFAULT_PORT) == null) { 100 } else if (ServerAddress.from(ipField.getText(), EnigmaServer.DEFAULT_PORT) == null) {
95 ipField.addError("Invalid IP/Port combination"); 101 vc.raise(Message.INVALID_IP, ipField.getText());
96 error = true;
97 } 102 }
98
99 return !error;
100 } 103 }
101 104
102 public Result getResult() { 105 public Result getResult() {
103 if (!success) return null; 106 vc.reset();
107 validateInputs();
108 if (!vc.canProceed()) return null;
104 return new Result( 109 return new Result(
105 usernameField.getText(), 110 usernameField.getText(),
106 Objects.requireNonNull(ServerAddress.from(ipField.getText(), EnigmaServer.DEFAULT_PORT)), 111 Objects.requireNonNull(ServerAddress.from(ipField.getText(), EnigmaServer.DEFAULT_PORT)),
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/VerifiableTextField.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/VerifiableTextField.java
deleted file mode 100644
index 928372c3..00000000
--- a/enigma-swing/src/main/java/cuchaz/enigma/gui/elements/VerifiableTextField.java
+++ /dev/null
@@ -1,83 +0,0 @@
1package cuchaz.enigma.gui.elements;
2
3import java.awt.Color;
4import java.awt.Graphics;
5import java.awt.event.FocusAdapter;
6import java.awt.event.FocusEvent;
7
8import javax.swing.JTextField;
9import javax.swing.event.DocumentEvent;
10import javax.swing.event.DocumentListener;
11import javax.swing.text.Document;
12
13public class VerifiableTextField extends JTextField {
14
15 private boolean hasError;
16
17 public VerifiableTextField() {
18 }
19
20 public VerifiableTextField(String text) {
21 super(text);
22 }
23
24 public VerifiableTextField(int columns) {
25 super(columns);
26 }
27
28 public VerifiableTextField(String text, int columns) {
29 super(text, columns);
30 }
31
32 public VerifiableTextField(Document doc, String text, int columns) {
33 super(doc, text, columns);
34 }
35
36 {
37 getDocument().addDocumentListener(new DocumentListener() {
38 @Override
39 public void insertUpdate(DocumentEvent e) {
40 clearErrorState();
41 }
42
43 @Override
44 public void removeUpdate(DocumentEvent e) {
45 clearErrorState();
46 }
47
48 @Override
49 public void changedUpdate(DocumentEvent e) {
50 clearErrorState();
51 }
52 });
53 }
54
55 @Override
56 public void setText(String t) {
57 super.setText(t);
58 }
59
60 public void clearErrorState() {
61 this.hasError = false;
62 repaint();
63 }
64
65 public void addError(String message) {
66 this.hasError = true;
67 repaint();
68 }
69
70 @Override
71 public void paint(Graphics g) {
72 super.paint(g);
73 if (hasError) {
74 g.setColor(Color.RED);
75 int x1 = getWidth() - 9;
76 int x2 = getWidth() - 2;
77 int y1 = 1;
78 int y2 = 8;
79 g.fillPolygon(new int[]{x1, x2, x2}, new int[]{y1, y1, y2}, 3);
80 }
81 }
82
83}