summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/SaveControl.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control/SaveControl.java')
-rw-r--r--src/jrummikub/control/SaveControl.java81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/jrummikub/control/SaveControl.java b/src/jrummikub/control/SaveControl.java
new file mode 100644
index 0000000..458c0dc
--- /dev/null
+++ b/src/jrummikub/control/SaveControl.java
@@ -0,0 +1,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();
+ }
+ }
+}