summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/ApplicationControl.java
blob: 7f5cf6dd1f483f22e571aaff2c4a958e9f105caa (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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.Connection;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.util.IListener3;
import jrummikub.util.LoginData;
import jrummikub.view.IQuitWarningPanel.QuitMode;
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 Connection tempConnection;

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

		addQuitGameHandlers();

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

				createLoginControl(true);
			}
		});

		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 addQuitGameHandlers() {
		view.getMenuNewGameEvent().add(new IListener() {
			@Override
			public void handle() {
				showRestartWarning();
			}
		});
		view.getQuitEvent().add(new IListener() {
			@Override
			public void handle() {
				if (networkControl != null) {
					return;
				}
				if (gameControl == null) {
					System.exit(0);
				} else {
					showQuitWarning();
				}
			}
		});
		view.getQuitWarningPanel().getCancelEvent().add(new IListener() {
			@Override
			public void handle() {
				view.showQuitWarningPanel(false);
				if (tempConnection != null) {
					tempConnection.remove();
					tempConnection = null;
				}
			}
		});
	}

	private void showRestartWarning() {
		view.getQuitWarningPanel().setMode(QuitMode.QUIT_GAME);
		view.showQuitWarningPanel(true);
		tempConnection = view.getQuitWarningPanel().getQuitEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						abortControls();
						startApplication();
						view.showQuitWarningPanel(false);
						tempConnection.remove();
						tempConnection = null;
					}
				});
	}

	private void showQuitWarning() {
		view.getQuitWarningPanel().setMode(QuitMode.QUIT_PROCESS);
		view.showQuitWarningPanel(true);
		tempConnection = view.getQuitWarningPanel().getQuitEvent()
				.add(new IListener() {
					@Override
					public void handle() {
						System.exit(0);
					}
				});
	}

	/**
	 * Create a new network login control
	 */
	private void createLoginControl(boolean reset) {
		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(reset);
	}

	/**
	 * End all controls in case of e.g. quit event
	 */
	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;
		}

		view.showSidePanel(false);
		view.showScorePanel(false);
		view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
	}

	/**
	 * Starts the application by showing the game settings dialog panel
	 */
	public void startApplication() {
		view.showSidePanel(false);
		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();
	}

	/**
	 * Adds events listeners to game control events
	 * 
	 * @param gameControl
	 *            of current game
	 */
	private void addGameControlListeners(GameControl gameControl) {
		gameControl.getEndOfGameEvent().add(new IListener() {
			@Override
			public void handle() {
				startApplication();
			}
		});
	}

	/**
	 * Create a new network game control
	 * 
	 * @param loginData
	 *            users login data for channel
	 */
	private void createNetworkControl(LoginData loginData) {
		ConnectionControl connectionControl = new ConnectionControl(loginData);
		networkControl = new NetworkControl(loginData, connectionControl,
				saveControl, view);

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

		networkControl.getBackToLoginEvent().add(new IListener() {
			@Override
			public void handle() {
				networkControl = null;
				createLoginControl(false);
			}
		});

		networkControl.startNetwork();
	}
}