summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/ApplicationControl.java
blob: 1ed517f0ce0bb2cb89df95b1ede7b97cf2f4fbf0 (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
168
169
170
171
172
173
package jrummikub.control;

import jrummikub.control.network.ConnectionControl;
import jrummikub.control.network.NetworkControl;
import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IRoundState;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener3;
import jrummikub.util.LoginData;
import jrummikub.view.IView;
import jrummikub.view.IView.BottomPanelType;

/**
 * The application control controls the settings for a new games and create the
 * game control
 */
public class ApplicationControl {
	private SettingsControl settingsControl;
	private LoginControl loginControl;
	private NetworkControl networkControl;
	private SaveControl saveControl;
	private GameControl gameControl;

	private IView view;

	/**
	 * Creates a new application control
	 * 
	 * @param view
	 *            the view to use
	 */
	public ApplicationControl(final IView view) {
		this.view = view;
		saveControl = new SaveControl(view);

		view.getMenuNewGameEvent().add(new IListener() {
			@Override
			public void handle() {
				abortControls();
				startApplication();
			}
		});
		view.getMenuQuitEvent().add(new IListener() {
			@Override
			public void handle() {
				System.exit(0);
			}
		});

		addLoginControlListeners();

		saveControl.getLoadEvent().add(
				new IListener3<GameSettings, GameState, IRoundState>() {

					@Override
					public void handle(GameSettings settings,
							GameState gameState, IRoundState roundState) {
						abortControls();
						gameControl = new GameControl(settings, saveControl,
								view);
						addGameControlListeners(gameControl);
						gameControl.continueGame(gameState, roundState);

					}
				});
		saveControl.getLoadErrorEvent().add(new IListener() {
			@Override
			public void handle() {
				view.showLoadingError();
			}
		});
	}

	private void addLoginControlListeners() {
		view.getNetworkGameEvent().add(new IListener() {
			@Override
			public void handle() {
				abortControls();

				loginControl = new LoginControl(view);
				loginControl.getLoginEvent().add(new IListener1<LoginData>() {
					@Override
					public void handle(LoginData loginData) {
						createNetworkControl(loginData);
					}
				});
				loginControl.getCancelEvent().add(new IListener() {
					@Override
					public void handle() {
						startApplication();
					}
				});
				loginControl.startLogin();
			}
		});
	}

	private void abortControls() {
		if (settingsControl != null) {
			settingsControl.abort();
			settingsControl = null;
		}

		if (gameControl != null) {
			gameControl.abortGame();
			gameControl = null;
		}

		if (loginControl != null) {
			loginControl.abort();
			loginControl = null;
		}

		if (networkControl != null) {
			networkControl.abort();
			networkControl = null;
		}
	}

	/**
	 * Starts the application by showing the game settings dialog panel
	 */
	public void startApplication() {
		view.showScorePanel(false);
		view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
		saveControl.setGameSettings(null);
		saveControl.setGameState(null);

		settingsControl = new SettingsControl(view, new GameSettings());
		view.enableSave(false);

		settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() {
			@Override
			public void handle(GameSettings settings) {
				view.enableSave(true);
				settingsControl = null;

				saveControl.setGameSettings(settings);

				gameControl = new GameControl(settings, saveControl, view);
				addGameControlListeners(gameControl);

				gameControl.startGame();
			}
		});
		settingsControl.startSettings();
	}

	private void addGameControlListeners(GameControl gameControl) {
		gameControl.getEndOfGameEvent().add(new IListener() {
			@Override
			public void handle() {
				startApplication();
			}
		});
	}

	private void createNetworkControl(LoginData loginData) {
		ConnectionControl connectionControl = new ConnectionControl(loginData);
		networkControl = new NetworkControl(loginData, connectionControl, view);

		networkControl.getStopNetworkEvent().add(new IListener() {
			@Override
			public void handle() {
				startApplication();
			}
		});

		networkControl.startNetwork();
	}
}