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.service;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.FileOutputStream;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import net.curre.prefcount.PrefCountRegistry;
26  import net.curre.prefcount.bean.Settings;
27  import net.curre.prefcount.gui.MainWindow;
28  import net.curre.prefcount.gui.theme.skin.PrefSkin;
29  
30  /**
31   * This is a service bean that assists with
32   * handling application settings.
33   * <p/>
34   * Created date: Jun 22, 2007
35   *
36   * @author Yevgeny Nyden
37   */
38  public class SettingsService {
39  
40    /** Private class logger. */
41    private static Logger log = Logger.getLogger(SettingsService.class.toString());
42  
43    /** Reference to the settings object. */
44    private static Settings settings;
45  
46    static {
47      // initializing the settings object
48      settings = loadSettings();
49    }
50  
51    /**
52     * Returns application settings (current settings).
53     *
54     * @return Reference to the application settings bean.
55     */
56    public static Settings getSettings() {
57      return settings;
58    }
59  
60    /**
61     * Method to load settings stored on disk.
62     *
63     * @return Settings, loaded from the settings file,
64     *         or a new <code>Settings</code> object if
65     *         no settings file is found.
66     */
67    public static Settings loadSettings() {
68      try {
69        File file = new File(PrefCountRegistry.getInstance().getSettingsFilePath());
70        if (file.exists()) {
71          FileInputStream fStream = new FileInputStream(file);
72          ObjectInputStream oStream = new ObjectInputStream(fStream);
73          Settings settings = (Settings) oStream.readObject();
74  
75          // test each newer field for null values here
76          // in case when an old settings file is loaded
77          if (settings.getLocaleId() == null) {
78            settings.setLocaleId(PrefCountRegistry.DEFAULT_LOCALE_ID);
79          }
80          return settings;
81        }
82      } catch (Exception e) {
83        log.log(Level.WARNING, "Caught exception while loading settings! Ignoring.");
84      }
85      return new Settings();
86    }
87  
88    /**
89     * Updates the skin on the current settings object.
90     *
91     * @param skin PrefSkin to set.
92     */
93    public static void updateSkin(PrefSkin skin) {
94      settings.setLafSkinId(skin.getNameResourceKey());
95    }
96  
97    /**
98     * Method to save user settings. Application settings are
99     * saved in the place and under the name specified in the
100    * PrefCountRegistry bean.
101    *
102    * @throws ServiceException If there was an error when saving settings.
103    */
104   public static void saveSettings() throws ServiceException {
105     MainWindow mainWindow = PrefCountRegistry.getInstance().getMainWindow();
106     if (mainWindow != null) {
107       settings.setMainFrameWidth(mainWindow.getWidth());
108       settings.setMainFrameHeight(mainWindow.getHeight());
109       if (mainWindow.playerDialogFrame != null) {
110         settings.setDialogFrameWidth(mainWindow.playerDialogFrame.getWidth());
111         settings.setDialogFrameHeight(mainWindow.playerDialogFrame.getHeight());
112       }
113     }
114     settings.setLocaleId(PrefCountRegistry.getCurrentLocale().getLocale().getLanguage());
115     // settings LAF should be already set
116     persistSettings();
117   }
118 
119   /**
120    * Method to reset user settings.
121    * The settings are recreated and saved.
122    *
123    * @throws ServiceException If there was an error when resetting settings.
124    */
125   public static void resetSettings() throws ServiceException {
126     settings.reset();
127     persistSettings();
128   }
129 
130   /** Private methods ********************** */
131 
132   /**
133    * Helper method to persist current settings.
134    *
135    * @throws ServiceException If there was an error when saving settings.
136    */
137   private static void persistSettings() throws ServiceException {
138     try {
139       File file = new File(PrefCountRegistry.getInstance().getSettingsFilePath());
140       FileOutputStream fStream = new FileOutputStream(file);
141       ObjectOutputStream oStream = new ObjectOutputStream(fStream);
142       oStream.writeObject(settings);
143     } catch (Exception e) {
144       throw new ServiceException("ERROR while saving settings!", e);
145     }
146   }
147 
148 }