1
2
3
4
5
6
7
8
9
10
11
12
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
32
33
34
35
36
37
38 public class SettingsService {
39
40
41 private static Logger log = Logger.getLogger(SettingsService.class.toString());
42
43
44 private static Settings settings;
45
46 static {
47
48 settings = loadSettings();
49 }
50
51
52
53
54
55
56 public static Settings getSettings() {
57 return settings;
58 }
59
60
61
62
63
64
65
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
76
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
90
91
92
93 public static void updateSkin(PrefSkin skin) {
94 settings.setLafSkinId(skin.getNameResourceKey());
95 }
96
97
98
99
100
101
102
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
116 persistSettings();
117 }
118
119
120
121
122
123
124
125 public static void resetSettings() throws ServiceException {
126 settings.reset();
127 persistSettings();
128 }
129
130
131
132
133
134
135
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 }