001: /*
002: * Copyright (C) 2007 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.client.plugin;
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.preferences.IQueryTokenizerPreferenceBean;
031: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
032: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
033: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
034: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
035:
036: /**
037: * This is intended to be used by plugins that have custom IQueryTokenizers,
038: * which require some sort of preference management (loading/storing preference
039: * beans to XML file and vice-versa). In reality this functionality isn't
040: * specific to IQueryTokenizer preferences, so this could be further generalized.
041: * There is no need at the present time to do so.
042: *
043: * @author manningr
044: */
045: public class PluginQueryTokenizerPreferencesManager {
046:
047: /** Logger for this class. */
048: private final static ILogger s_log = LoggerController
049: .createLogger(PluginQueryTokenizerPreferencesManager.class);
050:
051: /** Name of preferences file. */
052: private static final String USER_PREFS_FILE_NAME = "prefs.xml";
053:
054: /** Folder to store user settings in. */
055: private File _userSettingsFolder;
056:
057: /** The bean we will be loading from / storing to */
058: private IQueryTokenizerPreferenceBean _prefs = null;
059:
060: /** Tells us which file to store the preference bean in */
061: private IPlugin plugin = null;
062:
063: /** whether or not we've been initialized */
064: private boolean _initialized = false;
065:
066: /**
067: * Constructor.
068: */
069: public PluginQueryTokenizerPreferencesManager() {
070: /* Do Nothing */
071: }
072:
073: /**
074: * Initializes this preference manager. This must be done prior to calling
075: * other methods.
076: *
077: * @param thePlugin which plugin we are handling preferences for.
078: * @param defaultPrefsBean the bean to use if no preference file currently
079: * exists.
080: * @throws PluginException
081: */
082: public void initialize(IPlugin thePlugin,
083: IQueryTokenizerPreferenceBean defaultPrefsBean)
084: throws PluginException {
085: if (thePlugin == null) {
086: throw new IllegalArgumentException(
087: "IPlugin arguement cannot be null");
088: }
089: if (defaultPrefsBean == null) {
090: throw new IllegalArgumentException(
091: "IQueryTokenizerPreferenceBean arguement cannot be null");
092: }
093: plugin = thePlugin;
094:
095: // Folder to store user settings.
096: try {
097: _userSettingsFolder = plugin.getPluginUserSettingsFolder();
098: } catch (IOException ex) {
099: throw new PluginException(ex);
100: }
101: _prefs = defaultPrefsBean;
102: loadPrefs();
103: _initialized = true;
104: }
105:
106: /**
107: * Returns the preferences bean that this class manages.
108: *
109: * @return an implementation instance of IQueryTokenizerPreferenceBean
110: */
111: public IQueryTokenizerPreferenceBean getPreferences() {
112: if (!_initialized) {
113: throw new IllegalStateException(
114: "initialize() must be called first");
115: }
116: return _prefs;
117: }
118:
119: /**
120: * Saves the preferences
121: */
122: public void unload() {
123: savePrefs();
124: }
125:
126: /**
127: * Save preferences to disk.
128: */
129: public void savePrefs() {
130: if (!_initialized) {
131: throw new IllegalStateException(
132: "initialize() must be called first");
133: }
134: try {
135: XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
136: wtr
137: .save(new File(_userSettingsFolder,
138: USER_PREFS_FILE_NAME));
139: } catch (Exception ex) {
140: s_log.error("Error occured writing to preferences file: "
141: + USER_PREFS_FILE_NAME, ex);
142: }
143: }
144:
145: /**
146: * Load from preferences file.
147: */
148: private void loadPrefs() {
149: try {
150: XMLBeanReader doc = new XMLBeanReader();
151:
152: File prefFile = PreferenceUtil
153: .getPreferenceFileToReadFrom(plugin);
154:
155: doc.load(prefFile, _prefs.getClass().getClassLoader());
156:
157: Iterator<Object> it = doc.iterator();
158:
159: if (it.hasNext()) {
160: _prefs = (IQueryTokenizerPreferenceBean) it.next();
161: }
162: } catch (FileNotFoundException ignore) {
163: s_log.info(USER_PREFS_FILE_NAME
164: + " not found - will be created");
165: } catch (Exception ex) {
166: s_log.error("Error occured reading from preferences file: "
167: + USER_PREFS_FILE_NAME, ex);
168: }
169:
170: _prefs.setClientName(Version.getApplicationName() + "/"
171: + plugin.getDescriptiveName());
172: _prefs.setClientVersion(Version.getShortVersion() + "/"
173: + plugin.getVersion());
174: }
175:
176: }
|