summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java
blob: 07daf6dc2668cf15d75a8cceed542b4a0557b69f (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
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 cuchaz.enigma.gui.config.NetConfig;
import cuchaz.enigma.gui.elements.ValidatablePasswordField;
import cuchaz.enigma.gui.elements.ValidatableTextField;
import cuchaz.enigma.gui.util.ScaleUtil;
import cuchaz.enigma.network.EnigmaServer;
import cuchaz.enigma.utils.Pair;
import cuchaz.enigma.utils.validation.Message;
import cuchaz.enigma.utils.validation.StandardValidation;

public class CreateServerDialog extends AbstractDialog {

	private ValidatableTextField portField;
	private ValidatablePasswordField passwordField;

	public CreateServerDialog(Frame owner) {
		super(owner, "prompt.create_server.title", "prompt.create_server.confirm", "prompt.cancel");

		Dimension preferredSize = getPreferredSize();
		preferredSize.width = ScaleUtil.scale(400);
		setPreferredSize(preferredSize);
		pack();
		setLocationRelativeTo(owner);
	}

	@Override
	protected List<Pair<String, Component>> createComponents() {
		portField = new ValidatableTextField(Integer.toString(NetConfig.getServerPort()));
		passwordField = new ValidatablePasswordField(NetConfig.getServerPassword());

		portField.addActionListener(event -> confirm());
		passwordField.addActionListener(event -> confirm());

		return Arrays.asList(
				new Pair<>("prompt.create_server.port", portField),
				new Pair<>("prompt.password", passwordField)
		);
	}

	@Override
	public void validateInputs() {
		vc.setActiveElement(portField);
		StandardValidation.isIntInRange(vc, portField.getText(), 0, 65535);
		vc.setActiveElement(passwordField);
		if (passwordField.getPassword().length > EnigmaServer.MAX_PASSWORD_LENGTH) {
			vc.raise(Message.FIELD_LENGTH_OUT_OF_RANGE, EnigmaServer.MAX_PASSWORD_LENGTH);
		}
	}

	public Result getResult() {
		if (!isActionConfirm()) return null;
		vc.reset();
		validateInputs();
		if (!vc.canProceed()) return null;
		return new Result(
				Integer.parseInt(portField.getText()),
				passwordField.getPassword()
		);
	}

	public static Result show(Frame parent) {
		CreateServerDialog d = new CreateServerDialog(parent);

		d.setVisible(true);
		Result r = d.getResult();

		d.dispose();
		return r;
	}

	public static class Result {
		private final int port;
		private final char[] password;

		public Result(int port, char[] password) {
			this.port = port;
			this.password = password;
		}

		public int getPort() {
			return port;
		}

		public char[] getPassword() {
			return password;
		}
	}

}