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

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.IRoundState;
import jrummikub.util.IListener1;
import jrummikub.view.IView;

/**
 * The save control is responsible for loading and saving game and round states
 */
public class SaveControl {
	private GameSettings gameSettings;
	private GameState gameState;
	private IRoundState roundState;

	/**
	 * Creates a new SaveControl
	 * 
	 * @param view
	 *          the view to use
	 */
	public SaveControl(IView view) {
		view.getSaveEvent().add(new IListener1<File>() {
			@Override
			public void handle(File file) {
				save(file);
			}
		});
	}

	/**
	 * Sets the current game settings
	 * 
	 * @param gameSettings
	 *          the game settings
	 */
	public void setGameSettings(GameSettings gameSettings) {
		this.gameSettings = gameSettings;
	}

	/**
	 * Sets the current game state
	 * 
	 * @param gameState
	 *          the game state
	 */
	public void setGameState(GameState gameState) {
		this.gameState = gameState;
	}

	/**
	 * Sets the current round state
	 * 
	 * @param roundState
	 *          the round state
	 */
	public void setRoundState(IRoundState roundState) {
		this.roundState = roundState;
	}

	private void save(File file) {
		try {
			ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
					file));

			stream.writeObject(gameSettings);
			stream.writeObject(gameState);
			stream.writeObject(roundState);
			stream.flush();

			stream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}