001: /*
002: * Copyright (C) 2005 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.plugins.dbcopy.prefs;
020:
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.Iterator;
025:
026: import net.sourceforge.squirrel_sql.client.Version;
027: import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
028: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
029: import net.sourceforge.squirrel_sql.client.plugin.PreferenceUtil;
030: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
031: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
032: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
033: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
034:
035: public class PreferencesManager {
036:
037: /** Logger for this class. */
038: private final static ILogger s_log = LoggerController
039: .createLogger(PreferencesManager.class);
040:
041: /** Name of preferences file. */
042: private static final String USER_PREFS_FILE_NAME = "prefs.xml";
043:
044: /** Folder to store user settings in. */
045: private static File _userSettingsFolder;
046:
047: private static DBCopyPreferenceBean _prefs = null;
048:
049: private static IPlugin plugin = null;
050:
051: public static void initialize(IPlugin thePlugin)
052: throws PluginException {
053: plugin = thePlugin;
054:
055: // Folder to store user settings.
056: try {
057: _userSettingsFolder = plugin.getPluginUserSettingsFolder();
058: } catch (IOException ex) {
059: throw new PluginException(ex);
060: }
061:
062: loadPrefs();
063: }
064:
065: public static DBCopyPreferenceBean getPreferences() {
066: return _prefs;
067: }
068:
069: public static void unload() {
070: savePrefs();
071: }
072:
073: /**
074: * Save preferences to disk. Always write to the user settings folder, not
075: * the application settings folder.
076: */
077: public static void savePrefs() {
078: try {
079: XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
080: wtr
081: .save(new File(_userSettingsFolder,
082: USER_PREFS_FILE_NAME));
083: } catch (Exception ex) {
084: s_log.error("Error occured writing to preferences file: "
085: + USER_PREFS_FILE_NAME, ex);
086: }
087: }
088:
089: /**
090: * Load from preferences file.
091: */
092: private static void loadPrefs() {
093: File prefFile = null;
094: try {
095: XMLBeanReader doc = new XMLBeanReader();
096:
097: prefFile = PreferenceUtil
098: .getPreferenceFileToReadFrom(plugin);
099:
100: doc.load(prefFile, DBCopyPreferenceBean.class
101: .getClassLoader());
102:
103: Iterator<Object> it = doc.iterator();
104: if (it.hasNext()) {
105: _prefs = (DBCopyPreferenceBean) it.next();
106: }
107: } catch (FileNotFoundException ignore) {
108: s_log.info(USER_PREFS_FILE_NAME + "("
109: + prefFile.getAbsolutePath()
110: + ") not found - will be created");
111: } catch (Exception ex) {
112: s_log.error("Error occured reading from preferences file: "
113: + USER_PREFS_FILE_NAME, ex);
114: }
115: if (_prefs == null) {
116: _prefs = new DBCopyPreferenceBean();
117: }
118:
119: _prefs.setClientName(Version.getApplicationName() + "/"
120: + plugin.getDescriptiveName());
121: _prefs.setClientVersion(Version.getShortVersion() + "/"
122: + plugin.getVersion());
123: }
124:
125: }
|