summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/LoginPanel.java
blob: 9fef5813f5577ca291eda12662fa668ef56dd006 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.LoginData;
import jrummikub.view.ILoginPanel;

@SuppressWarnings("serial")
class LoginPanel extends JPanel implements ILoginPanel {
	private Event1<LoginData> loginEvent = new Event1<LoginData>();
	private Event cancelEvent = new Event();
	private Event1<String> useDedicatedServer = new Event1<String>();
	private JTextField userNameField;
	private JTextField serverNameField;
	private JTextField passwordField;
	private JTextField channelNameField;
	private JButton startDedicatedServerButton;

	LoginPanel() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		c.weighty = 1;

		ActionListener loginAction = createInputFields();

		c.weighty = 0;
		add(startDedicatedServerButton, c);
		startDedicatedServerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				useDedicatedServer.emit(passwordField.getText());
			}
		});

		c.weighty = 1;
		add(Box.createVerticalGlue(), c);

		c.gridwidth = 1;
		c.weighty = 0;
		JButton loginButton = new JButton("Login");
		loginButton.addActionListener(loginAction);
		add(loginButton, c);

		c.weightx = 0;
		add(Box.createHorizontalStrut(10), c);

		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		JButton cancelButton = new JButton("Abbrechen");
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cancelEvent.emit();
			}
		});
		add(cancelButton, c);

		setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
				new EmptyBorder(10, 10, 10, 10)));
	}

	private ActionListener createInputFields() {
		ActionListener loginAction = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				login();
			}
		};

		userNameField = addInputRow("Benutzername:", new JTextField());
		userNameField.addActionListener(loginAction);
		serverNameField = addInputRow("Server:", new JTextField());
		serverNameField.addActionListener(loginAction);
		passwordField = addInputRow("Passwort:", new JPasswordField());
		passwordField.addActionListener(loginAction);
		channelNameField = addInputRow("Channel:", new JTextField());
		channelNameField.addActionListener(loginAction);
		startDedicatedServerButton = new JButton("Dedizierten Server starten");
		// this fixes some strange layouting bug
		startDedicatedServerButton.setPreferredSize(new Dimension(500, 10));
		return loginAction;
	}

	void resetLoginPanel() {
		// TODO leer machen
		userNameField.setText("test1");
		serverNameField.setText("universe-factory.net");
		passwordField.setText("");
		channelNameField.setText("jrummikub@muc.universe-factory.net");
	}

	@Override
	public IEvent1<LoginData> getLoginEvent() {
		return loginEvent;
	}

	@Override
	public IEvent getCancelEvent() {
		return cancelEvent;
	}

	@Override
	public IEvent1<String> getUseDedicatedServerEvent() {
		return useDedicatedServer;
	}

	private JTextField addInputRow(String label, JTextField textField) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 1;
		c.weightx = 1;
		c.weighty = 0;

		add(new JLabel(label), c);

		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = GridBagConstraints.REMAINDER;
		add(textField, c);
		return textField;
	}

	private void login() {
		loginEvent.emit(new LoginData(userNameField.getText(), serverNameField
				.getText(), passwordField.getText().isEmpty() ? "jrummikub"
				: passwordField.getText(), channelNameField.getText()));
	}

	@Override
	public void setServer(String server) {
		serverNameField.setText(server);
	}

	@Override
	public void setChannel(String channel) {
		channelNameField.setText(channel);
	}

	@Override
	public void setDedicatedServerRunning(boolean running) {
		startDedicatedServerButton
				.setText(running ? "Dedizierten Server nutzen"
						: "Dedizierten Server starten");
	}
}