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 import java.awt.MenuBar;
21
22 import javax.swing.JMenuBar;
23 import javax.swing.JFrame;
24 import javax.swing.JPanel;
25
26 import net.curre.prefcount.gui.MainWindow;
27 import net.curre.prefcount.gui.LastInputPanel;
28 import net.curre.prefcount.gui.menu.PrefCountMenuBar;
29 import net.curre.prefcount.gui.menu.AwtMenuBar;
30 import net.curre.prefcount.gui.menu.SwingMenuBar;
31 import net.curre.prefcount.service.ServiceException;
32 import net.curre.prefcount.util.LocaleExt;
33 import net.curre.prefcount.util.Utilities;
34
35
36
37
38
39
40
41
42
43 public class PrefCountRegistry {
44
45
46 private static Logger log = Logger.getLogger(PrefCountRegistry.class.toString());
47
48
49 public static final String APPLICATION_NAME = "PrefCount";
50
51
52 public static final String IMAGES_DIR = "images";
53
54
55 public static final String DEFAULT_LOCALE_ID = "ru";
56
57
58 public static final String SETTINGS_FILE_NAME = "prefcount-settings.ser";
59
60
61 public static final LocaleExt[] AVAILABLE_LOCALES = new LocaleExt[]{
62 new LocaleExt("ru", "RU", "\u0420\u0443\u0441\u0441\u043A\u0438\u0439"),
63 new LocaleExt("us", "US", "English US")
64 };
65
66
67 private static PrefCountRegistry instance;
68
69 static {
70 instance = new PrefCountRegistry();
71 }
72
73
74 private MainWindow mainWindow;
75
76
77 private LastInputPanel lastInputPanel;
78
79
80 private String settingsFilePath;
81
82
83
84
85
86
87 public static PrefCountRegistry getInstance() {
88 return instance;
89 }
90
91
92
93
94
95 private static LocaleExt currentLocale = AVAILABLE_LOCALES[0];
96
97
98
99
100
101
102 public static LocaleExt getCurrentLocale() {
103 return currentLocale;
104 }
105
106
107
108
109
110
111
112 public synchronized void setCurrentLocale(String localeId) {
113 LocaleExt locale;
114 try {
115 locale = findLocaleById(localeId);
116 } catch (ServiceException e) {
117 log.warning("Error: " + e.getMessage() + " setting locale to default.");
118 try {
119 locale = findLocaleById(PrefCountRegistry.DEFAULT_LOCALE_ID);
120 } catch (ServiceException e1) {
121 log.severe("Error: " + e1.getMessage() + " - unable to set locale to default.");
122 System.exit(1);
123 throw new NullPointerException();
124 }
125 }
126 Locale.setDefault(locale.getLocale());
127 currentLocale = locale;
128 if (mainWindow != null) {
129 mainWindow.repaint();
130 if (mainWindow.playerDialogFrame != null) {
131 mainWindow.playerDialogFrame.repaint();
132 }
133 }
134 }
135
136
137
138
139
140 private PrefCountRegistry() {
141 settingsFilePath = getSettingsFilePathHelper();
142 }
143
144
145
146
147
148
149 public MainWindow getMainWindow() {
150 return mainWindow;
151 }
152
153
154
155
156
157
158 public void setMainWindow(MainWindow mainWindow) {
159 this.mainWindow = mainWindow;
160 }
161
162
163
164
165
166
167 public LastInputPanel getLastInputPanel() {
168 return lastInputPanel;
169 }
170
171
172
173
174
175
176 public void setLastInputPanel(LastInputPanel lastInputPanel) {
177 this.lastInputPanel = lastInputPanel;
178 }
179
180
181
182
183
184
185
186 public String getSettingsFilePath() {
187 return settingsFilePath;
188 }
189
190
191
192
193
194
195
196 public void setSettingsFilePath(String settingsFilePath) {
197 this.settingsFilePath = settingsFilePath;
198 }
199
200
201 public void doQuit() {
202 mainWindow.setVisible(false);
203 if (mainWindow.playerDialogFrame != null) {
204 mainWindow.playerDialogFrame.setVisible(false);
205 mainWindow.playerDialogFrame.dispose();
206 }
207 mainWindow.dispose();
208
209 System.exit(0);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 private static LocaleExt findLocaleById(String localeId) throws ServiceException {
224 for (LocaleExt locale : AVAILABLE_LOCALES) {
225 if (locale.getLocale().getLanguage().equalsIgnoreCase(localeId)) {
226 return locale;
227 }
228 }
229 throw new ServiceException("Locale with ID (language) \"" + localeId + "\" is not found!");
230 }
231
232
233
234
235
236
237
238
239
240 private static String getSettingsFilePathHelper() {
241 StringBuilder path = new StringBuilder(System.getProperties().getProperty("user.home"));
242 path.append(File.separatorChar);
243 switch (Utilities.getPlatformType()) {
244 case MAC_OS:
245 path.append("Library").append(File.separatorChar).append("Preferences").
246 append(File.separatorChar).append(APPLICATION_NAME);
247 createDirIfDoesntExist(path);
248 path.append(File.separatorChar).append(SETTINGS_FILE_NAME);
249 break;
250 case LINUX:
251 path.append('.').append(SETTINGS_FILE_NAME);
252 break;
253 case WINDOWS:
254 path.append("UserData");
255 createDirIfDoesntExist(path);
256 path.append(File.separatorChar).append(SETTINGS_FILE_NAME);
257 break;
258 default:
259 path.append(File.separatorChar).append(SETTINGS_FILE_NAME);
260 }
261 return path.toString();
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276 public PrefCountMenuBar addMainWindowMenuBar(JFrame frame, JPanel topPanel) {
277 PrefCountMenuBar menuBar;
278 if (Utilities.isMacOs()) {
279 menuBar = new AwtMenuBar(PrefCountMenuBar.MenuBarType.MAIN_WINDOW, topPanel);
280 frame.setMenuBar((MenuBar) menuBar);
281 } else {
282 menuBar = new SwingMenuBar();
283 frame.setJMenuBar((JMenuBar) menuBar);
284 }
285 return menuBar;
286 }
287
288
289
290
291
292
293
294
295
296
297
298 public PrefCountMenuBar addPlayerDialogMenuBar(JFrame frame) {
299 PrefCountMenuBar menuBar = null;
300 if (Utilities.isMacOs()) {
301 menuBar = new AwtMenuBar(PrefCountMenuBar.MenuBarType.PLAYER_DIALOG, null);
302 frame.setMenuBar((MenuBar) menuBar);
303 }
304 return menuBar;
305 }
306
307
308
309
310
311
312
313 private static void createDirIfDoesntExist(StringBuilder path) {
314 File dir = new File(path.toString());
315 if (!dir.exists()) {
316 dir.mkdir();
317 }
318 }
319
320 }