diff options
Diffstat (limited to 'src/jrummikub/control/SaveControl.java')
-rw-r--r-- | src/jrummikub/control/SaveControl.java | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/src/jrummikub/control/SaveControl.java b/src/jrummikub/control/SaveControl.java index 458c0dc..e0589e6 100644 --- a/src/jrummikub/control/SaveControl.java +++ b/src/jrummikub/control/SaveControl.java @@ -1,12 +1,16 @@ package jrummikub.control; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IRoundState; +import jrummikub.util.Event3; +import jrummikub.util.IEvent3; import jrummikub.util.IListener1; import jrummikub.view.IView; @@ -17,12 +21,13 @@ public class SaveControl { private GameSettings gameSettings; private GameState gameState; private IRoundState roundState; + private Event3<GameSettings, GameState, IRoundState> loadEvent = new Event3<GameSettings, GameState, IRoundState>(); /** * Creates a new SaveControl * * @param view - * the view to use + * the view to use */ public SaveControl(IView view) { view.getSaveEvent().add(new IListener1<File>() { @@ -31,13 +36,24 @@ public class SaveControl { save(file); } }); + + view.getLoadEvent().add(new IListener1<File>() { + @Override + public void handle(File file) { + load(file); + } + }); + } + + public IEvent3<GameSettings, GameState, IRoundState> getLoadEvent() { + return loadEvent; } /** * Sets the current game settings * * @param gameSettings - * the game settings + * the game settings */ public void setGameSettings(GameSettings gameSettings) { this.gameSettings = gameSettings; @@ -47,7 +63,7 @@ public class SaveControl { * Sets the current game state * * @param gameState - * the game state + * the game state */ public void setGameState(GameState gameState) { this.gameState = gameState; @@ -57,16 +73,45 @@ public class SaveControl { * Sets the current round state * * @param roundState - * the round state + * the round state */ public void setRoundState(IRoundState roundState) { this.roundState = roundState; } + private void load(File file) { + try { + ObjectInputStream stream = new ObjectInputStream( + new FileInputStream(file)); + + gameSettings = (GameSettings) stream.readObject(); + gameState = (GameState) stream.readObject(); + roundState = (IRoundState) stream.readObject(); + + stream.close(); + + if (gameState == null || gameSettings == null) { + // TODO Fehlermeldung + System.err.println("laden ging nicht"); + return; + } + + loadEvent.emit(gameSettings, gameState, roundState); + + } catch (Exception e) { + e.printStackTrace(); + } + } + private void save(File file) { + if (gameState == null || gameSettings == null) { + // TODO Menüpunkt ausgrauen + System.err.println("kein aktives Spiel"); + return; + } try { - ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream( - file)); + ObjectOutputStream stream = new ObjectOutputStream( + new FileOutputStream(file)); stream.writeObject(gameSettings); stream.writeObject(gameState); |