summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java
blob: eea1dff14b6432bbe969e475c912c447d29a9da7 (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
package cuchaz.enigma.gui.dialog;

import cuchaz.enigma.network.EnigmaServer;
import cuchaz.enigma.utils.I18n;

import javax.swing.*;
import java.awt.*;

public class CreateServerDialog {

	public static Result show(Frame parentComponent) {
		JTextField portField = new JTextField(String.valueOf(EnigmaServer.DEFAULT_PORT), 10);
		JPanel portRow = new JPanel();
		portRow.add(new JLabel(I18n.translate("prompt.port")));
		portRow.add(portField);
		JPasswordField passwordField = new JPasswordField(20);
		JPanel passwordRow = new JPanel();
		passwordRow.add(new JLabel(I18n.translate("prompt.password")));
		passwordRow.add(passwordField);

		int response = JOptionPane.showConfirmDialog(parentComponent, new Object[]{portRow, passwordRow}, I18n.translate("prompt.create_server.title"), JOptionPane.OK_CANCEL_OPTION);
		if (response != JOptionPane.OK_OPTION) {
			return null;
		}

		int port;
		try {
			port = Integer.parseInt(portField.getText());
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.port.nan"), I18n.translate("prompt.create_server.title"), JOptionPane.ERROR_MESSAGE);
			return null;
		}
		if (port < 0 || port >= 65536) {
			JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.port.invalid"), I18n.translate("prompt.create_server.title"), JOptionPane.ERROR_MESSAGE);
			return null;
		}

		char[] password = passwordField.getPassword();
		if (password.length > EnigmaServer.MAX_PASSWORD_LENGTH) {
			JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.password.too_long"), I18n.translate("prompt.create_server.title"), JOptionPane.ERROR_MESSAGE);
			return null;
		}

		return new Result(port, password);
	}

	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;
		}
	}

}