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 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
88
89
90
91 public static void updateSkin(PrefSkin skin) {
92 settings.setLafSkinId(skin.getNameResourceKey());
93 }
94
95
96
97
98
99
100
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
115 PrefSkin skin = LafThemeService.getInstance().getPendingSkin();
116 if (skin != null) {
117 settings.setLafSkinId(skin.getNameResourceKey());
118 }
119
120
121 persistSettings();
122 }
123
124
125
126
127
128
129
130 public static void resetSettings() throws ServiceException {
131 LafThemeService.getInstance().clearPendingSkin();
132 settings.reset();
133 persistSettings();
134 }
135
136
137
138
139
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
153
154 }