1
2
3
4
5
6
7
8
9
10
11
12
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
34
35
36
37
38
39
40 public class PrefCountRegistry {
41
42
43 private static Logger log = Logger.getLogger(PrefCountRegistry.class.toString());
44
45
46 public static final String APPLICATION_NAME = "PrefCount";
47
48
49 public static final String IMAGES_DIR = "images";
50
51
52 public static final String DEFAULT_LOCALE_ID = "ru";
53
54
55 public static final String SETTINGS_FILE_NAME = "prefcount-settings.ser";
56
57
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
64 private static final PrefCountRegistry instance;
65
66
67
68
69
70 private static LocaleExt currentLocale = AVAILABLE_LOCALES[0];
71
72
73 private final MenuItemsBean menuItemsBean;
74
75
76 private GameResultBean gameResultBean;
77
78 static {
79 instance = new PrefCountRegistry();
80 }
81
82
83 private MainWindow mainWindow;
84
85
86 private PlayerDialogBaseFrame playerDialogFrame;
87
88
89 private LastInputPanel lastInputPanel;
90
91
92 private String settingsFilePath;
93
94
95 private MainController mainController;
96
97
98 private ChoosePlayerDialog choosePlayerDialog;
99
100
101
102
103
104
105 public static PrefCountRegistry getInstance() {
106 return instance;
107 }
108
109
110
111
112
113 private PrefCountRegistry() {
114 this.settingsFilePath = getSettingsFilePathHelper();
115 this.menuItemsBean = new MenuItemsBean();
116 this.gameResultBean = new GameResultBean();
117 }
118
119
120
121
122
123
124 public static LocaleExt getCurrentLocale() {
125 return currentLocale;
126 }
127
128
129
130
131
132
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
158
159
160
161 public MenuItemsBean getMenuItemsBean() {
162 return this.menuItemsBean;
163 }
164
165
166
167
168
169
170 public GameResultBean getGameResultBean() {
171 return this.gameResultBean;
172 }
173
174
175
176
177
178
179 public MainWindow getMainWindow() {
180 return this.mainWindow;
181 }
182
183
184
185
186
187
188 public void setMainWindow(MainWindow mainWindow) {
189 this.mainWindow = mainWindow;
190 }
191
192
193
194
195
196
197 public PlayerDialogBaseFrame getPlayerDialogFrame() {
198 return this.playerDialogFrame;
199 }
200
201
202
203
204
205
206 public void setPlayerDialogFrame(PlayerDialogBaseFrame playerDialogFrame) {
207 this.playerDialogFrame = playerDialogFrame;
208 }
209
210
211
212
213
214
215 public LastInputPanel getLastInputPanel() {
216 return this.lastInputPanel;
217 }
218
219
220
221
222
223
224 public void setLastInputPanel(LastInputPanel lastInputPanel) {
225 this.lastInputPanel = lastInputPanel;
226 }
227
228
229
230
231
232
233
234 public String getSettingsFilePath() {
235 return this.settingsFilePath;
236 }
237
238
239
240
241
242
243
244 public void setSettingsFilePath(String settingsFilePath) {
245 this.settingsFilePath = settingsFilePath;
246 }
247
248
249
250
251
252
253 public MainController getMainController() {
254 return this.mainController;
255 }
256
257
258
259
260
261
262 public void setMainController(MainController mainController) {
263 this.mainController = mainController;
264 }
265
266
267
268
269
270
271
272
273
274
275
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
288
289
290
291
292
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
320
321
322
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
333
334
335
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
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 }