001: package net.sourceforge.squirrel_sql.plugins.exportconfig;
002:
003: /*
004: * Copyright (C) 2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.File;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.Iterator;
025:
026: import javax.swing.JMenu;
027:
028: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
029: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
030: import net.sourceforge.squirrel_sql.fw.util.StringManager;
031: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
032: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
033: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
034:
035: import net.sourceforge.squirrel_sql.client.IApplication;
036: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
037: import net.sourceforge.squirrel_sql.client.plugin.DefaultPlugin;
038: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
039: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
040:
041: import net.sourceforge.squirrel_sql.plugins.exportconfig.action.ExportAliasesAction;
042: import net.sourceforge.squirrel_sql.plugins.exportconfig.action.ExportConfigurationAction;
043: import net.sourceforge.squirrel_sql.plugins.exportconfig.action.ExportDriversAction;
044: import net.sourceforge.squirrel_sql.plugins.exportconfig.action.ExportSettingsAction;
045:
046: /**
047: * Plugin controlling class.
048: *
049: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
050: */
051: public class ExportConfigPlugin extends DefaultPlugin {
052: private static final StringManager s_stringMgr = StringManagerFactory
053: .getStringManager(ExportConfigPlugin.class);
054:
055: /** Logger for this class. */
056: private final static ILogger s_log = LoggerController
057: .createLogger(ExportConfigPlugin.class);
058:
059: /** Name of preferences file. */
060: private static final String USER_PREFS_FILE_NAME = "prefs.xml";
061:
062: /** Folder to store user settings in. */
063: private File _userSettingsFolder;
064:
065: /** Plugin resources. */
066: private PluginResources _resources;
067:
068: /** Export menu. */
069: private JMenu _exportMenu;
070:
071: /** Plugin preferences. */
072: private ExportConfigPreferences _prefs;
073:
074: /**
075: * Return the internal name of this plugin.
076: *
077: * @return the internal name of this plugin.
078: */
079: public String getInternalName() {
080: return "exportconfig";
081: }
082:
083: /**
084: * Return the descriptive name of this plugin.
085: *
086: * @return the descriptive name of this plugin.
087: */
088: public String getDescriptiveName() {
089: return "Export Configuration Plugin";
090: }
091:
092: /**
093: * Returns the current version of this plugin.
094: *
095: * @return the current version of this plugin.
096: */
097: public String getVersion() {
098: return "0.10";
099: }
100:
101: /**
102: * Returns the authors name.
103: *
104: * @return the authors name.
105: */
106: public String getAuthor() {
107: return "Colin Bell";
108: }
109:
110: /**
111: * Returns a comma separated list of other contributors.
112: *
113: * @return Contributors names.
114: */
115: public String getContributors() {
116: return "Rob Manning";
117: }
118:
119: /**
120: * Load this plugin.
121: *
122: * @param app Application API.
123: */
124: public synchronized void load(IApplication app)
125: throws PluginException {
126: super .load(app);
127:
128: // Folder to store user settings.
129: try {
130: _userSettingsFolder = getPluginUserSettingsFolder();
131: } catch (IOException ex) {
132: throw new PluginException(ex);
133: }
134:
135: _resources = new ExportConfigResources(getClass().getName(),
136: this );
137: }
138:
139: /**
140: * Retrieve the name of the change log.
141: *
142: * @return The name of the change log.
143: */
144: public String getChangeLogFileName() {
145: return "changes.txt";
146: }
147:
148: /**
149: * Retrieve the name of the help file.
150: *
151: * @return The nane of the help file.
152: */
153: public String getHelpFileName() {
154: return "readme.html";
155: }
156:
157: /**
158: * Retrieve the name of the licence file.
159: *
160: * @return The nane of the licence file.
161: */
162: public String getLicenceFileName() {
163: return "licence.txt";
164: }
165:
166: /**
167: * Initialize this plugin.
168: */
169: public synchronized void initialize() throws PluginException {
170: super .initialize();
171:
172: final IApplication app = getApplication();
173: final ActionCollection coll = app.getActionCollection();
174:
175: coll.add(new ExportAliasesAction(app, _resources, this ));
176: coll.add(new ExportConfigurationAction(app, _resources, this ));
177: coll.add(new ExportDriversAction(app, _resources, this ));
178: coll.add(new ExportSettingsAction(app, _resources, this ));
179:
180: // Load plugin preferences.
181: loadPrefs();
182:
183: _exportMenu = createExportMenu();
184: app.addToMenu(IApplication.IMenuIDs.PLUGINS_MENU, _exportMenu);
185: }
186:
187: /**
188: * Application is shutting down so save preferences.
189: */
190: public void unload() {
191: savePrefs();
192: super .unload();
193: }
194:
195: /**
196: * Retrieve the plugin preferences.
197: *
198: * @erturn The plugin preferences.
199: */
200: public ExportConfigPreferences getPreferences() {
201: return _prefs;
202: }
203:
204: /**
205: * Load from preferences file.
206: */
207: private void loadPrefs() {
208: try {
209: XMLBeanReader doc = new XMLBeanReader();
210: doc
211: .load(new File(_userSettingsFolder,
212: USER_PREFS_FILE_NAME), getClass()
213: .getClassLoader());
214: final Iterator<?> it = doc.iterator();
215: if (it.hasNext()) {
216: _prefs = (ExportConfigPreferences) it.next();
217: }
218: } catch (FileNotFoundException ignore) {
219: // i18n[exportconfig.fileWillBeCreated={0} not found - will be created]
220: s_log.info(s_stringMgr.getString(
221: "exportconfig.fileWillBeCreated",
222: USER_PREFS_FILE_NAME));
223: } catch (Exception ex) {
224: // i18n[exportconfig.errorCreatingFile=Error occured reading from preferences file: {0}]
225: s_log.error(s_stringMgr.getString(
226: "exportconfig.errorCreatingFile",
227: USER_PREFS_FILE_NAME), ex);
228: }
229: if (_prefs == null) {
230: _prefs = new ExportConfigPreferences();
231: }
232: }
233:
234: /**
235: * Save preferences to disk.
236: */
237: private void savePrefs() {
238: try {
239: XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
240: wtr
241: .save(new File(_userSettingsFolder,
242: USER_PREFS_FILE_NAME));
243: } catch (Exception ex) {
244: // i18n[exportconfig.errorWritingPrefs=Error occured writing to preferences file: {0}]
245: s_log.error(s_stringMgr.getString(
246: "exportconfig.errorWritingPrefs",
247: USER_PREFS_FILE_NAME), ex);
248: }
249: }
250:
251: /**
252: * Create menu containing actions relevant for the object tree.
253: *
254: * @return The menu object.
255: */
256: private JMenu createExportMenu() {
257: final IApplication app = getApplication();
258: final ActionCollection coll = app.getActionCollection();
259:
260: final JMenu exportMenu = _resources
261: .createMenu(ExportConfigResources.IMenuResourceKeys.EXPORT);
262: _resources.addToMenu(coll.get(ExportConfigurationAction.class),
263: exportMenu);
264: _resources.addToMenu(coll.get(ExportAliasesAction.class),
265: exportMenu);
266: _resources.addToMenu(coll.get(ExportDriversAction.class),
267: exportMenu);
268: _resources.addToMenu(coll.get(ExportSettingsAction.class),
269: exportMenu);
270:
271: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU, exportMenu);
272:
273: return exportMenu;
274: }
275: }
|