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