summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java
blob: eede946c6a997f2e4f909868633abaf1cb17e110 (plain) (blame)
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
package cuchaz.enigma.gui.dialog;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.*;

import cuchaz.enigma.gui.elements.ValidatableTextField;
import cuchaz.enigma.network.EnigmaServer;
import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.utils.ServerAddress;
import cuchaz.enigma.utils.validation.Message;
import cuchaz.enigma.utils.validation.ValidationContext;

public class ConnectToServerDialog extends JDialog {

	private final ValidationContext vc = new ValidationContext();

	private final JTextField usernameField;
	private final ValidatableTextField ipField;
	private final JPasswordField passwordField;
	private boolean success = false;

	public ConnectToServerDialog(Frame owner) {
		super(owner, I18n.translate("prompt.connect.title"), true);

		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		Container inputContainer = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		usernameField = new JTextField(System.getProperty("user.name"));
		ipField = new ValidatableTextField();
		passwordField = new JPasswordField();

		List<JLabel> labels = Stream.of("prompt.connect.username", "prompt.connect.address", "prompt.password")
				.map(I18n::translate)
				.map(JLabel::new)
				.collect(Collectors.toList());
		List<JTextField> inputs = Arrays.asList(usernameField, ipField, passwordField);

		for (int i = 0; i < inputs.size(); i += 1) {
			c.gridy = i;
			c.insets = new Insets(4, 4, 4, 4);

			c.gridx = 0;
			c.weightx = 0.0;
			c.anchor = GridBagConstraints.LINE_END;
			c.fill = GridBagConstraints.NONE;
			inputContainer.add(labels.get(i), c);

			c.gridx = 1;
			c.weightx = 1.0;
			c.anchor = GridBagConstraints.LINE_START;
			c.fill = GridBagConstraints.HORIZONTAL;
			inputs.get(i).addActionListener(event -> confirm());
			inputContainer.add(inputs.get(i), c);
		}
		contentPane.add(inputContainer, BorderLayout.CENTER);
		Container buttonContainer = new JPanel(new GridBagLayout());
		c = new GridBagConstraints();
		c.weightx = 1.0;
		c.insets = new Insets(4, 4, 4, 4);
		c.anchor = GridBagConstraints.LINE_END;
		JButton connectButton = new JButton(I18n.translate("prompt.connect.confirm"));
		connectButton.addActionListener(event -> confirm());
		buttonContainer.add(connectButton, c);
		c.weightx = 0.0;
		c.anchor = GridBagConstraints.CENTER;
		JButton abortButton = new JButton(I18n.translate("prompt.connect.cancel"));
		abortButton.addActionListener(event -> cancel());
		buttonContainer.add(abortButton, c);
		contentPane.add(buttonContainer, BorderLayout.SOUTH);

		setLocationRelativeTo(owner);
		setSize(new Dimension(400, 185));
	}

	private void confirm() {
		vc.reset();
		validateInputs();
		if (vc.canProceed()) {
			success = true;
			setVisible(false);
		}
	}

	private void cancel() {
		success = false;
		setVisible(false);
	}

	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, ipField.getText());
		}
	}

	public Result getResult() {
		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;
		}
	}

}