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 as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount;
16  
17  import java.io.File;
18  import java.util.Locale;
19  import java.util.logging.Logger;
20  
21  import net.curre.prefcount.bean.GameResultBean;
22  import net.curre.prefcount.event.MainController;
23  import net.curre.prefcount.gui.LastInputPanel;
24  import net.curre.prefcount.gui.MainWindow;
25  import net.curre.prefcount.gui.PlayerDialogBaseFrame;
26  import net.curre.prefcount.gui.ChoosePlayerDialog;
27  import net.curre.prefcount.gui.menu.MenuItemsBean;
28  import net.curre.prefcount.service.ServiceException;
29  import net.curre.prefcount.util.LocaleExt;
30  import net.curre.prefcount.util.Utilities;
31  
32  /**
33   * This is the central place for all prefcount
34   * settings and configuration data.
35   * <p/>
36   * Created date: May 30, 2007
37   *
38   * @author Yevgeny Nyden
39   */
40  public class PrefCountRegistry {
41  
42    /** Private class logger. */
43    private static Logger log = Logger.getLogger(PrefCountRegistry.class.toString());
44  
45    /** Application name. */
46    public static final String APPLICATION_NAME = "PrefCount";
47  
48    /** Location of the images directory. */
49    public static final String IMAGES_DIR = "images";
50  
51    /** Default value for the locale ID (case insensitive language name). */
52    public static final String DEFAULT_LOCALE_ID = "ru";
53  
54    /** Name of the settings file. */
55    public static final String SETTINGS_FILE_NAME = "prefcount-settings.ser";
56  
57    /** Array of available locales in the application. */
58    public static final LocaleExt[] AVAILABLE_LOCALES = new LocaleExt[]{
59        new LocaleExt("ru", "RU", "\u0420\u0443\u0441\u0441\u043A\u0438\u0439"),
60        new LocaleExt("us", "US", "English US")
61    };
62  
63    /** Reference to the singelton instance of this class. */
64    private static final PrefCountRegistry instance;
65  
66    /**
67     * Current locale (the first locale on the
68     * availableLocales array is the default).
69     */
70    private static LocaleExt currentLocale = AVAILABLE_LOCALES[0];
71  
72    /** Reference to the menu items bean. */
73    private final MenuItemsBean menuItemsBean;
74  
75    /** Reference to the result bean */
76    private GameResultBean gameResultBean;
77  
78    static {
79      instance = new PrefCountRegistry();
80    }
81  
82    /** Reference to the main window. */
83    private MainWindow mainWindow;
84  
85    /** Reference to the player dialog base frame. */
86    private PlayerDialogBaseFrame playerDialogFrame;
87  
88    /** Reference to the last input panel. */
89    private LastInputPanel lastInputPanel;
90  
91    /** Absolute path to the settings file (including filename). */
92    private String settingsFilePath;
93  
94    /** Reference to the main controller bean. */
95    private MainController mainController;
96  
97    /** Reference to the choose player dialog window. */
98    private ChoosePlayerDialog choosePlayerDialog;
99  
100   /**
101    * Returns the singleton instance of this class.
102    *
103    * @return The singleton instance of this class.
104    */
105   public static PrefCountRegistry getInstance() {
106     return instance;
107   }
108 
109   /**
110    * Private constructor. Sets settingsFilePath with
111    * the result of the getSettingsFilePathHelper() method.
112    */
113   private PrefCountRegistry() {
114     this.settingsFilePath = getSettingsFilePathHelper();
115     this.menuItemsBean = new MenuItemsBean();
116     this.gameResultBean = new GameResultBean();
117   }
118 
119   /**
120    * Returns the current locale.
121    *
122    * @return The current locale.
123    */
124   public static LocaleExt getCurrentLocale() {
125     return currentLocale;
126   }
127 
128   /**
129    * Setter for the current locale.
130    *
131    * @param localeId Identifier (case insensitive language name)
132    *                 for the locale to which to set.
133    */
134   public synchronized void setCurrentLocale(String localeId) {
135     LocaleExt locale;
136     try {
137       locale = findLocaleById(localeId);
138     } catch (ServiceException e) {
139       log.warning("Error: " + e.getMessage() + " setting locale to default.");
140       try {
141         locale = findLocaleById(PrefCountRegistry.DEFAULT_LOCALE_ID);
142       } catch (ServiceException e1) {
143         log.severe("Error: " + e1.getMessage() + " - unable to set locale to default.");
144         System.exit(1);
145         throw new NullPointerException();
146       }
147     }
148     Locale.setDefault(locale.getLocale());
149     currentLocale = locale;
150 
151     if (this.mainWindow != null) {
152       this.mainWindow.refreshComponents();
153     }
154   }
155 
156   /**
157    * Getter for the reference to the menu items bean.
158    *
159    * @return reference to the menu items bean.
160    */
161   public MenuItemsBean getMenuItemsBean() {
162     return this.menuItemsBean;
163   }
164 
165   /**
166    * Getter for the game result bean.
167    *
168    * @return reference to the game result bean.
169    */
170   public GameResultBean getGameResultBean() {
171     return this.gameResultBean;
172   }
173 
174   /**
175    * Getter for the main window reference.
176    *
177    * @return The reference to the main window object.
178    */
179   public MainWindow getMainWindow() {
180     return this.mainWindow;
181   }
182 
183   /**
184    * Setter for the main window reference.
185    *
186    * @param mainWindow Reference to the main window object.
187    */
188   public void setMainWindow(MainWindow mainWindow) {
189     this.mainWindow = mainWindow;
190   }
191 
192   /**
193    * Getter for the player dialog frame.
194    *
195    * @return reference to the player dialog frame.
196    */
197   public PlayerDialogBaseFrame getPlayerDialogFrame() {
198     return this.playerDialogFrame;
199   }
200 
201   /**
202    * Setter for the player dialog frame.
203    *
204    * @param playerDialogFrame reference to the player dialog frame.
205    */
206   public void setPlayerDialogFrame(PlayerDialogBaseFrame playerDialogFrame) {
207     this.playerDialogFrame = playerDialogFrame;
208   }
209 
210   /**
211    * Getter for the past input panel reference.
212    *
213    * @return Reference to the last input panel.
214    */
215   public LastInputPanel getLastInputPanel() {
216     return this.lastInputPanel;
217   }
218 
219   /**
220    * Setter for the last input panel reference.
221    *
222    * @param lastInputPanel Reference to the last input panel to set.
223    */
224   public void setLastInputPanel(LastInputPanel lastInputPanel) {
225     this.lastInputPanel = lastInputPanel;
226   }
227 
228   /**
229    * Getter for property 'settingsFilePath'
230    * (absolute path to the settings file, including filename).
231    *
232    * @return Value for property 'settingsFilePath'.
233    */
234   public String getSettingsFilePath() {
235     return this.settingsFilePath;
236   }
237 
238   /**
239    * Setter for property 'settingsFilePath'
240    * (absolute path to the settings file, including filename).
241    *
242    * @param settingsFilePath Value to set for property 'settingsFilePath'.
243    */
244   public void setSettingsFilePath(String settingsFilePath) {
245     this.settingsFilePath = settingsFilePath;
246   }
247 
248   /**
249    * Gets reference to the main controller bean.
250    *
251    * @return reference to the main controller bean.
252    */
253   public MainController getMainController() {
254     return this.mainController;
255   }
256 
257   /**
258    * Sets the main controller bean reference.
259    *
260    * @param mainController reference to the main controller bean.
261    */
262   public void setMainController(MainController mainController) {
263     this.mainController = mainController;
264   }
265 
266   /** Private methods ********************** */
267 
268   /**
269    * Gets a <code>LocaleExt</code> object given it's
270    * corresponding language name (case insensitive).
271    *
272    * @param localeId Locale identifier (case insensitive langiage name).
273    * @return <code>LocaleExt</code> object given it's
274    *         corresponding language name (case insensitive).
275    * @throws ServiceException if locale with the given ID is not found.
276    */
277   private static LocaleExt findLocaleById(String localeId) throws ServiceException {
278     for (LocaleExt locale : AVAILABLE_LOCALES) {
279       if (locale.getLocale().getLanguage().equalsIgnoreCase(localeId)) {
280         return locale;
281       }
282     }
283     throw new ServiceException("Locale with ID (language) \"" + localeId + "\" is not found!");
284   }
285 
286   /**
287    * Returns a string that represents an absolute path
288    * to the settings file including the file name. If there are
289    * custom directories in the path that don't exist, they will be created.
290    * Note that the path is platform-specific.
291    *
292    * @return Absolute path to the settings file including the file name.
293    */
294   private static String getSettingsFilePathHelper() {
295     StringBuilder path = new StringBuilder(System.getProperties().getProperty("user.home"));
296     path.append(File.separatorChar);
297     switch (Utilities.getPlatformType()) {
298       case MAC_OS:
299         path.append("Library").append(File.separatorChar).append("Preferences").
300             append(File.separatorChar).append(APPLICATION_NAME);
301         createDirIfDoesntExist(path);
302         path.append(File.separatorChar).append(SETTINGS_FILE_NAME);
303         break;
304       case LINUX:
305         path.append('.').append(SETTINGS_FILE_NAME);
306         break;
307       case WINDOWS:
308         path.append("UserData");
309         createDirIfDoesntExist(path);
310         path.append(File.separatorChar).append(SETTINGS_FILE_NAME);
311         break;
312       default:
313         path.append(File.separatorChar).append(SETTINGS_FILE_NAME);
314     }
315     return path.toString();
316   }
317 
318   /**
319    * Creates a directory if it doesn't exist
320    * (only the last one in the provided path).
321    *
322    * @param path Path to the directory.
323    */
324   private static void createDirIfDoesntExist(StringBuilder path) {
325     File dir = new File(path.toString());
326     if (dir.exists() == false) {
327       dir.mkdir();
328     }
329   }
330 
331   /**
332    * Gets the choose player dialog (note, that it will
333    * be created if if doesn't exist).
334    *  
335    * @return reference to the choose player dialog.
336    */
337   public ChoosePlayerDialog getChoosePlayerDialog() {
338     if (this.choosePlayerDialog == null) {
339       this.choosePlayerDialog = new ChoosePlayerDialog(this.playerDialogFrame);
340       this.choosePlayerDialog.setVisible(true);
341     }
342 
343     return this.choosePlayerDialog;
344   }
345 
346   /** Destroys the choose player dialog. */
347   public void disposeChoosePlayerDialog() {
348     if (this.choosePlayerDialog != null) {
349       this.choosePlayerDialog.setVisible(false);
350       this.choosePlayerDialog.dispose();
351     }
352     this.choosePlayerDialog = null;
353   }
354 
355 }