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          settings.testSettings();
78          return settings;
79        }
80      } catch (Exception e) {
81        log.log(Level.WARNING, "Caught exception while loading settings! Ignoring.");
82      }
83      return new Settings();
84    }
85  
86    /**
87     * Updates the skin on the current settings object.
88     *
89     * @param skin PrefSkin to set.
90     */
91    public static void updateSkin(PrefSkin skin) {
92      settings.setLafSkinId(skin.getNameResourceKey());
93    }
94  
95    /**
96     * Method to save user settings. Application settings are
97     * saved in the place and under the name specified in the
98     * PrefCountRegistry bean.
99     *
100    * @throws ServiceException If there was an error when saving settings.
101    */
102   public static void saveSettings() throws ServiceException {
103     MainWindow mainWindow = PrefCountRegistry.getInstance().getMainWindow();
104     if (mainWindow != null) {
105       settings.setMainFrameWidth(mainWindow.getWidth());
106       settings.setMainFrameHeight(mainWindow.getHeight());
107       if (mainWindow.playerDialogFrame != null) {
108         settings.setDialogFrameWidth(mainWindow.playerDialogFrame.getWidth());
109         settings.setDialogFrameHeight(mainWindow.playerDialogFrame.getHeight());
110       }
111     }
112     settings.setLocaleId(PrefCountRegistry.getCurrentLocale().getLocale().getLanguage());
113 
114     // saving the pending skin if it is set
115     PrefSkin skin = LafThemeService.getInstance().getPendingSkin();
116     if (skin != null) {
117       settings.setLafSkinId(skin.getNameResourceKey());
118     }
119 
120     // settings LAF should be already set
121     persistSettings();
122   }
123 
124   /**
125    * Method to reset user settings.
126    * The settings are recreated and saved.
127    *
128    * @throws ServiceException If there was an error when resetting settings.
129    */
130   public static void resetSettings() throws ServiceException {
131     LafThemeService.getInstance().clearPendingSkin(); 
132     settings.reset();
133     persistSettings();
134   }
135 
136   /**
137    * Method to persist the current settings.
138    *
139    * @throws ServiceException If there was an error when saving settings.
140    */
141   public static void persistSettings() throws ServiceException {
142     try {
143       File file = new File(PrefCountRegistry.getInstance().getSettingsFilePath());
144       FileOutputStream fStream = new FileOutputStream(file);
145       ObjectOutputStream oStream = new ObjectOutputStream(fStream);
146       oStream.writeObject(settings);
147     } catch (Exception e) {
148       throw new ServiceException("ERROR while saving settings!", e);
149     }
150   }
151 
152   /** Private methods ********************** */
153 
154 }