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  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   * This is the central place for all prefcount
37   * settings and configuration data.
38   * <p/>
39   * Created date: May 30, 2007
40   *
41   * @author Yevgeny Nyden
42   */
43  public class PrefCountRegistry {
44  
45    /** Private class logger. */
46    private static Logger log = Logger.getLogger(PrefCountRegistry.class.toString());
47  
48    /** Application name. */
49    public static final String APPLICATION_NAME = "PrefCount";
50  
51    /** Location of the images directory. */
52    public static final String IMAGES_DIR = "images";
53  
54    /** Default value for the locale ID (case insensitive language name). */
55    public static final String DEFAULT_LOCALE_ID = "ru";
56  
57    /** Name of the settings file. */
58    public static final String SETTINGS_FILE_NAME = "prefcount-settings.ser";
59  
60    /** Array of available locales in the application. */
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    /** Reference to the singelton instance of this class. */
67    private static PrefCountRegistry instance;
68  
69    static {
70      instance = new PrefCountRegistry();
71    }
72  
73    /** Reference to the main window. */
74    private MainWindow mainWindow;
75  
76    /** Reference to the last input panel. */
77    private LastInputPanel lastInputPanel;
78  
79    /** Absolute path to the settings file (including filename). */
80    private String settingsFilePath;
81  
82    /**
83     * Returns the singleton instance of this class.
84     *
85     * @return The singleton instance of this class.
86     */
87    public static PrefCountRegistry getInstance() {
88      return instance;
89    }
90  
91    /**
92     * Current locale (the first locale on the
93     * availableLocales array is the default).
94     */
95    private static LocaleExt currentLocale = AVAILABLE_LOCALES[0];
96  
97    /**
98     * Returns the current locale.
99     *
100    * @return The current locale.
101    */
102   public static LocaleExt getCurrentLocale() {
103     return currentLocale;
104   }
105 
106   /**
107    * Setter for the current locale.
108    *
109    * @param localeId Identifier (case insensitive language name)
110    *                 for the locale to which to set.
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    * Private constructor. Sets settingsFilePath with
138    * the result of the getSettingsFilePathHelper() method.
139    */
140   private PrefCountRegistry() {
141     settingsFilePath = getSettingsFilePathHelper();
142   }
143 
144   /**
145    * Getter for the main window reference.
146    *
147    * @return The reference to the main window object.
148    */
149   public MainWindow getMainWindow() {
150     return mainWindow;
151   }
152 
153   /**
154    * Setter for the main window reference.
155    *
156    * @param mainWindow Reference to the main window object.
157    */
158   public void setMainWindow(MainWindow mainWindow) {
159     this.mainWindow = mainWindow;
160   }
161 
162   /**
163    * Getter for the past input panel reference.
164    *
165    * @return Reference to the last input panel.
166    */
167   public LastInputPanel getLastInputPanel() {
168     return lastInputPanel;
169   }
170 
171   /**
172    * Setter for the last input panel reference.
173    *
174    * @param lastInputPanel Reference to the last input panel to set.
175    */
176   public void setLastInputPanel(LastInputPanel lastInputPanel) {
177     this.lastInputPanel = lastInputPanel;
178   }
179   
180   /**
181    * Getter for property 'settingsFilePath'
182    * (absolute path to the settings file, including filename).
183    *
184    * @return Value for property 'settingsFilePath'.
185    */
186   public String getSettingsFilePath() {
187     return settingsFilePath;
188   }
189 
190   /**
191    * Setter for property 'settingsFilePath'
192    * (absolute path to the settings file, including filename).
193    *
194    * @param settingsFilePath Value to set for property 'settingsFilePath'.
195    */
196   public void setSettingsFilePath(String settingsFilePath) {
197     this.settingsFilePath = settingsFilePath;
198   }
199 
200   /** Disposes all frames and quits the application. */
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   /** Private methods ********************** */
213 
214   /**
215    * Gets a <code>LocaleExt</code> object given it's
216    * corresponding language name (case insensitive).
217    *
218    * @param localeId Locale identifier (case insensitive langiage name).
219    * @return <code>LocaleExt</code> object given it's
220    *         corresponding language name (case insensitive).
221    * @throws ServiceException if locale with the given ID is not found.
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    * Returns a string that represents an absolute path
234    * to the settings file including the file name. If there are
235    * custom directories in the path that don't exist, they will be created.
236    * Note that the path is platform-specific.
237    *
238    * @return Absolute path to the settings file including the file name.
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    * Creates an appropriate menu bar for the main window
266    * and sets it on the mainWindow object. Since sunstance
267    * LAF does not work with native Mac menu bar, an awt
268    * menu bar is created for the Mac platform; for other
269    * platforms, a swing menu bar is created.
270    *
271    * @param frame    Frame to add menu bar to.
272    * @param topPanel Reference to the main window top panel -
273    *                 the language indicator icon will be added there on Mac OS.
274    * @return Reference to the created PrefCount menu bar.
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    * Creates a menu bar for the player dialog window
290    * and sets it on the mainWindow.playerDialogFrame object.
291    * This menu bar is created and added only when running
292    * on Mac OS platform.
293    *
294    * @param frame Reference to the frame to add this menu bar to.
295    * @return Reference to the created PrefCount menu bar or
296    *         null of running on not Mac OS platform.
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    * Creates a directory if it doesn't exist
309    * (only the last one in the provided path).
310    *
311    * @param path Path to the directory.
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 }