summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/LoginControl.java
blob: ccf6e2e117216c24cf410fbbde98966691db17f6 (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
package jrummikub.control;

import java.util.ArrayList;
import java.util.List;

import jrummikub.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.LoginData;
import jrummikub.view.IView;

/**
 * Control checking and processing login data for network game
 * 
 */
public class LoginControl {
	private IView view;
	private Event1<LoginData> loginEvent = new Event1<LoginData>();
	private Event cancelEvent = new Event();
	private List<Connection> connections = new ArrayList<Connection>();

	/**
	 * Constructor for login Control
	 * 
	 * @param view
	 *          for events which need handling
	 */
	public LoginControl(final IView view) {
		this.view = view;
		connections.add(view.getLoginPanel().getLoginEvent()
				.add(new IListener1<LoginData>() {
					@Override
					public void handle(LoginData loginData) {
						abort();
						// TODO connectPanel anzeigen
						loginEvent.emit(loginData);
					}
				}));

		connections.add(view.getLoginPanel().getCancelEvent().add(new IListener() {
			@Override
			public void handle() {
				abort();
				cancelEvent.emit();
			}
		}));
	}

	/**
	 * Open Login panel
	 * 
	 */
	public void startLogin() {
		view.showLoginPanel(true);
	}

	/**
	 * Getter for loginEvent
	 * 
	 * @return loginEvent
	 */
	public IEvent1<LoginData> getLoginEvent() {
		return loginEvent;
	}

	/**
	 * Getter for cancelEvent
	 * 
	 * @return cancelEvent
	 */
	public IEvent getCancelEvent() {
		return cancelEvent;
	}

	/**
	 * Control abortion once all login data have been processed or login was
	 * canceled
	 * 
	 */
	public void abort() {
		view.showLoginPanel(false);
		for (Connection c : connections) {
			c.remove();
		}
	}
}