package jrummikub.control; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IRoundState; import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.util.IListener3; 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 SaveControl saveControl; private IView view; private GameControl gameControl; /** * Creates a new application control * * @param view * the view to use */ public ApplicationControl(IView view) { this.view = view; saveControl = new SaveControl(view); view.getMenuQuitEvent().add(new IListener() { @Override public void handle() { System.exit(0); } }); } /** * Starts the application by showing the game settings dialog panel */ public void startApplication() { view.showScorePanel(false); view.setBottomPanel(BottomPanelType.START_GAME_PANEL); saveControl.setGameSettings(null); saveControl.setGameState(null); final SettingsControl settingsControl = new SettingsControl(view, new GameSettings()); saveControl.getLoadEvent().add( new IListener3() { @Override public void handle(GameSettings settings, GameState gameState, IRoundState roundState) { settingsControl.abort(); if (gameControl != null) { gameControl.abortGame(); } gameControl = new GameControl(settings, saveControl, view); addGameControlListeners(gameControl); gameControl.continueGame(gameState, roundState); } }); settingsControl.getStartGameEvent().add(new IListener1() { @Override public void handle(GameSettings settings) { saveControl.setGameSettings(settings); gameControl = new GameControl(settings, saveControl, view); addGameControlListeners(gameControl); gameControl.startGame(); } }); settingsControl.startSettings(); } private void addGameControlListeners(GameControl gameControl) { gameControl.getEndOfGameEvent().add(new IListener() { @Override public void handle() { startApplication(); } }); } }