1 | public class PreferencesManager {↵ | | 1 | public class SQLScriptPreferencesManager {↵
|
|
2 | /** Logger for this class. */↵ | | 2 | /** Logger for this class. */↵
|
3 | private final static ILogger s_log = ↵ | | 3 | private final static ILogger s_log = ↵
|
4 | LoggerController.createLogger(PreferencesManager.class);↵ | | 4 | LoggerController.createLogger(SQLScriptPreferencesManager.class);↵
|
5 | ↵ | | 5 | ↵
|
6 | /** Name of preferences file. */↵ | | 6 | /** Name of preferences file. */↵
|
7 | private static final String USER_PREFS_FILE_NAME = "prefs.xml"; ↵ | | 7 | private static final String USER_PREFS_FILE_NAME = "prefs.xml"; ↵
|
8 | ↵ | | 8 | ↵
|
9 | /** Folder to store user settings in. */↵ | | 9 | /** Folder to store user settings in. */↵
|
10 | private static File _userSettingsFolder;↵ | | 10 | private static File _userSettingsFolder;↵
|
11 | ↵ | | 11 | ↵
|
12 | private static DBCopyPreferenceBean _prefs = null;↵ | | 12 | private static SQLScriptPreferenceBean _prefs = null;↵
|
13 | ↵ | | 13 | ↵
|
14 | private static IPlugin plugin = null;↵ | | 14 | private static IPlugin plugin = null;↵
|
15 | ↵ | | 15 | ↵
|
16 | public static void initialize(IPlugin thePlugin) throws PluginException {↵ | | 16 | public static void initialize(IPlugin thePlugin) throws PluginException {↵
|
17 | plugin = thePlugin;↵ | | 17 | plugin = thePlugin;↵
|
18 | ↵ | | 18 | ↵
|
19 | // Folder to store user settings.↵ | | 19 | // Folder to store user settings.↵
|
20 | try {↵ | | 20 | try {↵
|
21 | _userSettingsFolder = plugin.getPluginUserSettingsFolder();↵ | | 21 | _userSettingsFolder = plugin.getPluginUserSettingsFolder();↵
|
22 | } catch (IOException ex) {↵ | | 22 | } catch (IOException ex) {↵
|
23 | throw new PluginException(ex);↵ | | 23 | throw new PluginException(ex);↵
|
24 | } ↵ | | 24 | } ↵
|
25 | ↵ | | 25 | ↵
|
26 | loadPrefs();↵ | | 26 | loadPrefs();↵
|
27 | }↵ | | 27 | }↵
|
28 | ↵ | | 28 | ↵
|
29 | public static DBCopyPreferenceBean getPreferences() {↵ | | 29 | public static SQLScriptPreferenceBean getPreferences() {↵
|
30 | return _prefs;↵ | | 30 | return _prefs;↵
|
31 | }↵ | | 31 | }↵
|
32 | ↵ | | 32 | ↵
|
33 | public static void unload() {↵ | | 33 | public static void unload() {↵
|
34 | savePrefs();↵ | | 34 | savePrefs();↵
|
35 | }↵ | | 35 | }↵
|
36 | ↵ | | 36 | ↵
|
37 | /**↵ | | 37 | /**↵
|
38 | * Save preferences to disk. Always write to the user settings folder, not↵ | | 38 | * Save preferences to disk.↵
|
39 | * the application settings folder.↵ | | |
|
40 | */↵ | | 39 | */↵
|
41 | public static void savePrefs() {↵ | | 40 | public static void savePrefs() {↵
|
42 | try {↵ | | 41 | try {↵
|
43 | XMLBeanWriter wtr = new XMLBeanWriter(_prefs);↵ | | 42 | XMLBeanWriter wtr = new XMLBeanWriter(_prefs);↵
|
44 | wtr.save(new File(_userSettingsFolder, USER_PREFS_FILE_NAME));↵ | | 43 | wtr.save(new File(_userSettingsFolder, USER_PREFS_FILE_NAME));↵
|
45 | } catch (Exception ex) {↵ | | 44 | } catch (Exception ex) {↵
|
46 | s_log.error("Error occured writing to preferences file: "↵ | | 45 | s_log.error("Error occured writing to preferences file: "↵
|
47 | + USER_PREFS_FILE_NAME, ex);↵ | | 46 | + USER_PREFS_FILE_NAME, ex);↵
|
48 | }↵ | | 47 | }↵
|
49 | }↵ | | 48 | }↵
|
|
50 | /**↵ | | 49 | /**↵
|
51 | * Load from preferences file.↵ | | 50 | * Load from preferences file.↵
|
52 | */↵ | | 51 | */↵
|
53 | private static void loadPrefs() {↵ | | 52 | private static void loadPrefs() {↵
|
54 | File prefFile = null;↵ | | |
|
55 | try {↵ | | 53 | try {↵
|
56 | XMLBeanReader doc = new XMLBeanReader();↵ | | 54 | XMLBeanReader doc = new XMLBeanReader();↵
|
57 | ↵ | | 55 | ↵
|
58 | prefFile = PreferenceUtil.getPreferenceFileToReadFrom(plugin);↵ | | 56 | File prefFile = PreferenceUtil.getPreferenceFileToReadFrom(plugin);↵
|
59 | ↵ | | 57 | ↵
|
60 | doc.load(prefFile, DBCopyPreferenceBean.class.getClassLoader());↵ | | 58 | doc.load(prefFile, SQLScriptPreferenceBean.class.getClassLoader());↵
|
61 | ↵ | | 59 | ↵
|
|
62 | Iterator<Object> it = doc.iterator();↵ | | 60 | Iterator<?> it = doc.iterator();↵
|
63 | if (it.hasNext()) {↵ | | 61 | if (it.hasNext()) {↵
|
64 | _prefs = (DBCopyPreferenceBean)it.next();↵ | | 62 | _prefs = (SQLScriptPreferenceBean)it.next();↵
|
65 | }↵ | | 63 | }↵
|
66 | } catch (FileNotFoundException ignore) {↵ | | 64 | } catch (FileNotFoundException ignore) {↵
|
67 | s_log.info(USER_PREFS_FILE_NAME + "("+prefFile.getAbsolutePath()+↵ | | 65 | s_log.info(USER_PREFS_FILE_NAME + "↵
|
68 | ") not found - will be created");↵ | | 66 | not found - will be created");↵
|
69 | } catch (Exception ex) {↵ | | 67 | } catch (Exception ex) {↵
|
70 | s_log.error("Error occured reading from preferences file: "↵ | | 68 | s_log.error("Error occured reading from preferences file: "↵
|
71 | + USER_PREFS_FILE_NAME, ex);↵ | | 69 | + USER_PREFS_FILE_NAME, ex);↵
|
72 | }↵ | | 70 | }↵
|
73 | if (_prefs == null) {↵ | | 71 | if (_prefs == null) {↵
|
74 | _prefs = new DBCopyPreferenceBean();↵ | | 72 | _prefs = new SQLScriptPreferenceBean();↵
|
75 | }↵ | | 73 | }↵
|
|
76 | _prefs.setClientName(Version.getApplicationName() + "/" + plugin.getDescriptiveName());↵ | | 74 | _prefs.setClientName(Version.getApplicationName() + "/" + plugin.getDescriptiveName());↵
|
77 | _prefs.setClientVersion(Version.getShortVersion() + "/" + plugin.getVersion()) | | 75 | _prefs.setClientVersion(Version.getShortVersion() + "/" + plugin.getVersion())
|