001: /*
002: * Copyright (C) 2006 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.sqlscript.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 SQLScriptPreferencesManager {
036:
037: /** Logger for this class. */
038: private final static ILogger s_log = LoggerController
039: .createLogger(SQLScriptPreferencesManager.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 SQLScriptPreferenceBean _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 SQLScriptPreferenceBean getPreferences() {
066: return _prefs;
067: }
068:
069: public static void unload() {
070: savePrefs();
071: }
072:
073: /**
074: * Save preferences to disk.
075: */
076: public static void savePrefs() {
077: try {
078: XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
079: wtr
080: .save(new File(_userSettingsFolder,
081: USER_PREFS_FILE_NAME));
082: } catch (Exception ex) {
083: s_log.error("Error occured writing to preferences file: "
084: + USER_PREFS_FILE_NAME, ex);
085: }
086: }
087:
088: /**
089: * Load from preferences file.
090: */
091: private static void loadPrefs() {
092: try {
093: XMLBeanReader doc = new XMLBeanReader();
094:
095: File prefFile = PreferenceUtil
096: .getPreferenceFileToReadFrom(plugin);
097:
098: doc.load(prefFile, SQLScriptPreferenceBean.class
099: .getClassLoader());
100:
101: Iterator<?> it = doc.iterator();
102: if (it.hasNext()) {
103: _prefs = (SQLScriptPreferenceBean) it.next();
104: }
105: } catch (FileNotFoundException ignore) {
106: s_log.info(USER_PREFS_FILE_NAME
107: + " not found - will be created");
108: } catch (Exception ex) {
109: s_log.error("Error occured reading from preferences file: "
110: + USER_PREFS_FILE_NAME, ex);
111: }
112: if (_prefs == null) {
113: _prefs = new SQLScriptPreferenceBean();
114: }
115:
116: _prefs.setClientName(Version.getApplicationName() + "/"
117: + plugin.getDescriptiveName());
118: _prefs.setClientVersion(Version.getShortVersion() + "/"
119: + plugin.getVersion());
120: }
121:
122: }
|