diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jrummikub/control/ApplicationControl.java | 9 | ||||
-rw-r--r-- | src/jrummikub/control/GameControl.java | 18 | ||||
-rw-r--r-- | src/jrummikub/control/SaveControl.java | 81 | ||||
-rw-r--r-- | src/jrummikub/view/IView.java | 19 | ||||
-rw-r--r-- | src/jrummikub/view/impl/View.java | 50 |
5 files changed, 150 insertions, 27 deletions
diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java index 0f73310..a0bdf35 100644 --- a/src/jrummikub/control/ApplicationControl.java +++ b/src/jrummikub/control/ApplicationControl.java @@ -11,6 +11,7 @@ import jrummikub.view.IView.BottomPanelType; * game control */ public class ApplicationControl { + private SaveControl saveControl; private IView view; /** @@ -21,6 +22,7 @@ public class ApplicationControl { */ public ApplicationControl(IView view) { this.view = view; + saveControl = new SaveControl(view); view.getMenuQuitEvent().add(new IListener() { @Override @@ -38,11 +40,13 @@ public class ApplicationControl { view.setBottomPanel(BottomPanelType.START_GAME_PANEL); SettingsControl settingsControl = new SettingsControl(view, new GameSettings()); - settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() { + settingsControl.getStartGameEvent().add(new IListener1<GameSettings>() { @Override public void handle(GameSettings settings) { - GameControl gameControl = new GameControl(settings, view); + saveControl.setGameSettings(settings); + + GameControl gameControl = new GameControl(settings, saveControl, view); gameControl.getEndOfGameEvent().add(new IListener() { @Override public void handle() { @@ -55,5 +59,4 @@ public class ApplicationControl { }); settingsControl.startSettings(); } - } diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java index 247e67a..fb3fcf8 100644 --- a/src/jrummikub/control/GameControl.java +++ b/src/jrummikub/control/GameControl.java @@ -20,6 +20,8 @@ import jrummikub.view.IView.BottomPanelType; * Controls a Game, at some point including all Rounds, starts new Rounds */ public class GameControl { + private SaveControl saveControl; + private GameSettings gameSettings; private IView view; RoundControl roundControl; @@ -31,15 +33,21 @@ public class GameControl { * Constructor * * @param gameSettings - * the game settings + * the game settings + * @param saveControl + * the save control * @param view - * the view + * the view */ - public GameControl(GameSettings gameSettings, IView view) { + public GameControl(GameSettings gameSettings, SaveControl saveControl, + IView view) { this.gameSettings = gameSettings; + this.saveControl = saveControl; this.view = view; gameState = new GameState(); + saveControl.setGameState(gameState); + gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings .getPlayerList().size())); @@ -96,6 +104,7 @@ public class GameControl { view.showScorePanel(false); IRoundState roundState = new RoundState(gameSettings); + saveControl.setRoundState(roundState); roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer() + gameState.getScores().size()); @@ -133,8 +142,7 @@ public class GameControl { view.getScorePanel().setPlayers(gameSettings.getPlayerList()); view.getScorePanel().setScores(gameState.getScores()); - view.getScorePanel().setAccumulatedScore( - gameState.getAccumulatedScore()); + view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore()); view.getScorePanel().update(); view.showScorePanel(true); } 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(); + } + } +} diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index 1c95ad4..8280a1b 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -1,10 +1,12 @@ package jrummikub.view; import java.awt.Color; +import java.io.File; import java.util.Collection; import jrummikub.model.Stone; import jrummikub.util.IEvent; +import jrummikub.util.IEvent1; /** * The top-level view interface @@ -154,26 +156,25 @@ public interface IView { public IEvent getMenuNewGameEvent(); /** - * The menu load event is emitted when the user selects the load menu - * entry + * The menu quit event is emitted when the user selects the quit menu entry * * @return the event */ - public IEvent getMenuLoadEvent(); + public IEvent getMenuQuitEvent(); /** - * The menu save event is emitted when the user selects the save menu - * entry + * The load event is emitted when the user wants to load a previously saved + * game state * * @return the event */ - public IEvent getMenuSaveEvent(); + IEvent1<File> getLoadEvent(); /** - * The menu quit event is emitted when the user selects the quit menu - * entry + * The save event is emitted when the user wants to save the current game + * state * * @return the event */ - public IEvent getMenuQuitEvent(); + IEvent1<File> getSaveEvent(); } diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index 0bc7120..a3bded6 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -5,11 +5,13 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.io.File; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JMenu; @@ -17,13 +19,16 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.border.MatteBorder; +import javax.swing.filechooser.FileNameExtensionFilter; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.util.Event; +import jrummikub.util.Event1; import jrummikub.util.IEvent; +import jrummikub.util.IEvent1; import jrummikub.util.Pair; import jrummikub.view.IHandPanel; import jrummikub.view.IPlayerPanel; @@ -52,11 +57,14 @@ public class View extends JFrame implements IView { private SettingsPanel settingsPanel; private ScorePanel scorePanel; + private JFileChooser chooser; + private Event menuNewGameEvent = new Event(); - private Event menuLoadEvent = new Event(); - private Event menuSaveEvent = new Event(); private Event menuQuitEvent = new Event(); + private Event1<File> loadEvent = new Event1<File>(); + private Event1<File> saveEvent = new Event1<File>(); + private static int even(double d) { return 2 * (int) (d / 2); } @@ -92,18 +100,25 @@ public class View extends JFrame implements IView { } @Override - public IEvent getMenuLoadEvent() { - return menuLoadEvent; + public IEvent getMenuQuitEvent() { + return menuQuitEvent; } @Override - public IEvent getMenuSaveEvent() { - return menuSaveEvent; + public IEvent1<File> getLoadEvent() { + return loadEvent; } @Override - public IEvent getMenuQuitEvent() { - return menuQuitEvent; + public IEvent1<File> getSaveEvent() { + return saveEvent; + } + + private void createFileChooser() { + chooser = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter( + "JRummikub-Spiele", "rum"); + chooser.setFileFilter(filter); } private void createMenuBar() { @@ -127,7 +142,7 @@ public class View extends JFrame implements IView { loadItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - menuLoadEvent.emit(); + load(); } }); gameMenu.add(loadItem); @@ -135,7 +150,7 @@ public class View extends JFrame implements IView { saveItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - menuSaveEvent.emit(); + save(); } }); gameMenu.add(saveItem); @@ -154,6 +169,20 @@ public class View extends JFrame implements IView { setJMenuBar(menuBar); } + private void load() { + int returnVal = chooser.showOpenDialog(View.this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + loadEvent.emit(chooser.getSelectedFile()); + } + } + + private void save() { + int returnVal = chooser.showSaveDialog(View.this); + if (returnVal == JFileChooser.APPROVE_OPTION) { + saveEvent.emit(chooser.getSelectedFile()); + } + } + /** * Create a new instance of the view */ @@ -164,6 +193,7 @@ public class View extends JFrame implements IView { setSize(1000, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); + createFileChooser(); createMenuBar(); layeredPane = new JLayeredPane(); |