View Javadoc

1   /**
2    * This program is free software; you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License version 2 as 
4    * published by the Free Software Foundation;
5    */
6   
7   package net.curre.prefcount.event;
8   
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.util.logging.Level;
12  import java.util.logging.Logger;
13  import javax.swing.AbstractAction;
14  import javax.swing.Action;
15  
16  import net.curre.prefcount.PrefCountRegistry;
17  import net.curre.prefcount.bean.GameResultBean;
18  import net.curre.prefcount.bean.Settings;
19  import net.curre.prefcount.gui.MainWindow;
20  import net.curre.prefcount.gui.PlayerDialogBaseFrame;
21  import net.curre.prefcount.gui.ChoosePlayerDialog;
22  import net.curre.prefcount.gui.menu.MenuItemsBean;
23  import net.curre.prefcount.gui.type.WindowComponent;
24  import static net.curre.prefcount.gui.type.WindowComponent.DIALOG_FORWARD;
25  import static net.curre.prefcount.gui.type.WindowComponent.DIVISIBLE_BY_N;
26  import static net.curre.prefcount.gui.type.WindowComponent.LENINGRAD;
27  import static net.curre.prefcount.gui.type.WindowComponent.MAIN_3_PLAYERS;
28  import static net.curre.prefcount.gui.type.WindowComponent.MAIN_4_PLAYERS;
29  import static net.curre.prefcount.gui.type.WindowComponent.SAVE_SETTINGS_ACTION;
30  import net.curre.prefcount.service.MainService;
31  import net.curre.prefcount.service.ResultService;
32  import net.curre.prefcount.service.ServiceException;
33  import net.curre.prefcount.service.SettingsService;
34  import net.curre.prefcount.util.Utilities;
35  
36  /**
37   * This is the main controller for handling
38   * event actions.
39   * <p/>
40   * Created date: Apr 15, 2008
41   *
42   * @author Yevgeny Nyden
43   */
44  public class MainController implements ActionListener {
45  
46    /** Private class logger. */
47    private static Logger log = Logger.getLogger(MainController.class.toString());
48  
49    /** Reference to the main window frame. */
50    private MainWindow mainWindow;
51  
52    /** Reference to the player dialog frame. */
53    private PlayerDialogBaseFrame playerDialog;
54  
55    /**
56     * Constructs a new <code>MainController</code> object.
57     *
58     * @param mainWindow   reference to the main window.
59     * @param playerDialog reference to the player dialog frame.
60     */
61    public MainController(MainWindow mainWindow, PlayerDialogBaseFrame playerDialog) {
62      this.mainWindow = mainWindow;
63      this.playerDialog = playerDialog;
64    }
65  
66    /** {@inheritDoc} */
67    public void actionPerformed(ActionEvent event) {
68      selectMenuBarOptions((WindowComponent) event.getSource());
69    }
70  
71    /**
72     * Creates and returns an action object for the given
73     * component (represented by a <code>WindowComponent</code> enum).
74     *
75     * @param compEnum component enum for which an action needs to be created.
76     * @return a new action object.
77     */
78    public static Action getActionForComponent(final WindowComponent compEnum) {
79      return new AbstractAction() {
80        public void actionPerformed(ActionEvent event) {
81          event.setSource(compEnum);
82          PrefCountRegistry.getInstance().getMainController().actionPerformed(event);
83        }
84      };
85    }
86  
87    /** Private methods ***********************/
88  
89    /**
90     * Performs an action according to the selected (passed)
91     * component enum.
92     *
93     * @param itemEnum selected item.
94     */
95    private void selectMenuBarOptions(WindowComponent itemEnum) {
96      Settings settings = SettingsService.getSettings();
97      PrefCountRegistry registry = PrefCountRegistry.getInstance();
98      GameResultBean resultBean = registry.getGameResultBean();
99  
100     String itemName = itemEnum.name();
101 
102     switch (itemEnum) {
103       case LENINGRAD:
104       case SOCHINKA:
105         settings.setPrefType(itemName);
106         resultBean.setLeningradka(LENINGRAD.equals(itemEnum));
107         recomputeScoresHelper();
108         break;
109 
110       case MAIN_3_PLAYERS:
111       case MAIN_4_PLAYERS:
112         numberOfPlayersHelper(itemEnum);
113         break;
114 
115       case DIVISIBLE_IGNORE:
116       case DIVISIBLE_BY_N:
117         settings.setDivisibleBy(itemName);
118         resultBean.setMountDivisibleByN(DIVISIBLE_BY_N.equals(itemEnum));
119         recomputeScoresHelper();
120         break;
121 
122       case DIALOG_BACK:
123       case DIALOG_FORWARD:
124         this.playerDialog.nextQuestionEventHelper(DIALOG_FORWARD.equals(itemEnum));
125         break;
126 
127       case DIALOG_FORWARD2:
128         ChoosePlayerDialog dialog = registry.getChoosePlayerDialog();
129         resultBean.setDivisibleByNPlayer(dialog.getSelectedPlayer());
130         registry.disposeChoosePlayerDialog();
131         break;
132 
133       case SAVE_SETTINGS_ACTION:
134       case RESET_SETTINGS_ACTION:
135         saveResetActionHelper(SAVE_SETTINGS_ACTION.equals(itemEnum));
136         break;
137 
138       case PRINT_SCORES_ACTION:
139         MainService.doPrint();
140         break;
141 
142       case QUIT_ACTION:
143         MainService.doQuit();
144         break;
145 
146       case ABOUT_ACTION:
147       case ABOUT_ACTION2:
148         PrefCountRegistry.getInstance().getMainWindow().showAboutInfo();
149         break;
150 
151       case HELP_COUNT_ACTION:
152       case HELP_COUNT_ACTION2:
153       case HELP_PREF_ACTION:
154       case HELP_PREF_ACTION2:
155       case HELP_COMMON_ACTION:
156       case HELP_COMMON_ACTION2:
157         MainService.doShowHelp(itemEnum);
158         break;
159 
160       case PRINT_TEMPLATE3_ACTION:
161         MainService.doPrintTemplate(3);
162         break;
163 
164       case PRINT_TEMPLATE4_ACTION:
165         MainService.doPrintTemplate(4);
166         break;
167 
168       default:
169         // ignore for now...
170     }
171   }
172 
173   /**
174    * Method to recompute the final scores and refresh the UI
175    * (only when the final scores were ready).
176    */
177   private void recomputeScoresHelper() {
178     GameResultBean resultBean = PrefCountRegistry.getInstance().getGameResultBean();
179     if (resultBean.isFinalScoresReady()) {
180       ResultService.generateFinalResults(resultBean);
181       this.playerDialog.refreshTable();
182       this.mainWindow.repaint();
183     }
184   }
185 
186   /**
187    * Assists with the changing number of players actions.
188    *
189    * @param itemEnum enum that represents the required action
190    *                 (MAIN_3_PLAYERS or MAIN_4_PLAYERS).
191    */
192   private void numberOfPlayersHelper(WindowComponent itemEnum) {
193     // let's first see if the new value is different
194     Settings settings = SettingsService.getSettings();
195     MenuItemsBean menuItemsBean = PrefCountRegistry.getInstance().getMenuItemsBean();
196     final String currNumber = settings.getPlayersNumber();
197     if (itemEnum.name().equals(currNumber) == false) {
198 
199       // need to check if we are not erasing any data
200       if (this.playerDialog.isSomeDataEntered()) {
201         if (Utilities.displayOkCancelMessage("pref.dialog.warn.resetData",
202                                              "pref.dialog.buttons.yes",
203                                              "pref.dialog.buttons.cancel")) {
204           settings.setPlayersNumber(itemEnum.name());
205           this.mainWindow.initializeNumberOfPlayers();
206 
207         } else {
208           // canceling action
209           if (MAIN_3_PLAYERS.equals(itemEnum)) {
210             menuItemsBean.setSelected(MAIN_4_PLAYERS, true);
211           } else {
212             menuItemsBean.setSelected(MAIN_3_PLAYERS, true);
213           }
214         }
215       } else {
216         settings.setPlayersNumber(itemEnum.name());
217         this.mainWindow.initializeNumberOfPlayers();
218       }
219     }
220   }
221 
222   /**
223    * Assists with the save/reset settings actions.
224    *
225    * @param isSave true if this is save action; false - reset action.
226    */
227   private void saveResetActionHelper(boolean isSave) {
228     try {
229       if (isSave) {
230         SettingsService.saveSettings();
231       } else {
232 
233         // warn the user first tha we need to quit
234         if (Utilities.displayOkCancelMessage("pref.settings.restart",
235                                              "pref.dialog.buttons.yes",
236                                              "pref.dialog.buttons.no")) {
237           SettingsService.resetSettings();
238           MainService.doQuit();
239         }
240 
241       }
242     } catch (ServiceException e) {
243       log.log(Level.WARNING, "Unable to perform save/reset action!", e);
244     }
245   }
246 
247 }