001: package net.sourceforge.squirrel_sql.plugins.sqlval;
002:
003: /*
004: * Copyright (C) 2002-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.gui.GUIUtils;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
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: import net.sourceforge.squirrel_sql.plugins.sqlval.action.ConnectAction;
036: import net.sourceforge.squirrel_sql.plugins.sqlval.action.DisconnectAction;
037: import net.sourceforge.squirrel_sql.plugins.sqlval.action.ValidateSQLAction;
038:
039: import net.sourceforge.squirrel_sql.client.IApplication;
040: import net.sourceforge.squirrel_sql.client.Version;
041: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
042: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
043: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
044: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
045: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
046: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
047: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
048: import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
049: import net.sourceforge.squirrel_sql.client.session.ISession;
050: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
051: import net.sourceforge.squirrel_sql.client.session.event.ISQLPanelListener;
052: import net.sourceforge.squirrel_sql.client.session.event.SQLPanelAdapter;
053: import net.sourceforge.squirrel_sql.client.session.event.SQLPanelEvent;
054:
055: /**
056: * This plugin provides an interface to the SQL Validation web service provided
057: * by Mimer SQL. See http://sqlvalidator.mimer.com/ for more information.
058: *
059: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
060: */
061: public class SQLValidatorPlugin extends DefaultSessionPlugin {
062: /** Logger for this class. */
063: private static final ILogger s_log = LoggerController
064: .createLogger(SQLValidatorPlugin.class);
065:
066: /** Internationalized strings for this class. */
067: private static final StringManager s_stringMgr = StringManagerFactory
068: .getStringManager(SQLValidatorPlugin.class);
069:
070: private interface IMenuResourceKeys {
071: String SQLVAL = "sqlval";
072: }
073:
074: /** Name of preferences file. */
075: private static final String USER_PREFS_FILE_NAME = "prefs.xml";
076:
077: private static final String PREFS_KEY = "sessionprefs";
078:
079: /** Plugin settings. */
080: private WebServicePreferences _prefs;
081:
082: /** Folder to store user settings in. */
083: private File _userSettingsFolder;
084:
085: /** Resources for this plugin. */
086: private PluginResources _resources;
087:
088: /** Listener to the SQL panel. */
089: private ISQLPanelListener _lis = new SQLPanelListener();
090:
091: /**
092: * Return the internal name of this plugin.
093: *
094: * @return the internal name of this plugin.
095: */
096: public String getInternalName() {
097: return "sqlval";
098: }
099:
100: /**
101: * Return the descriptive name of this plugin.
102: *
103: * @return the descriptive name of this plugin.
104: */
105: public String getDescriptiveName() {
106: // i18n[sqlval.descriptivename=SQL Validator plugin]
107: return s_stringMgr.getString("sqlval.descriptivename");
108: }
109:
110: /**
111: * Returns the current version of this plugin.
112: *
113: * @return the current version of this plugin.
114: */
115: public String getVersion() {
116: return "0.13";
117: }
118:
119: /**
120: * Returns the authors name.
121: *
122: * @return the authors name.
123: */
124: public String getAuthor() {
125: return "Colin Bell";
126: }
127:
128: /**
129: * Returns a comma separated list of other contributors.
130: *
131: * @return Contributors names.
132: */
133: public String getContributors() {
134: return "Olof Edlund";
135: }
136:
137: /**
138: * Returns the name of the change log for the plugin. This should
139: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
140: * directory.
141: *
142: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
143: * a change log.
144: */
145: public String getChangeLogFileName() {
146: return "changes.txt";
147: }
148:
149: /**
150: * Returns the name of the Help file for the plugin. This should
151: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
152: * directory.
153: *
154: * @return the Help file name or <TT>null</TT> if plugin doesn't have
155: * a help file.
156: */
157: public String getHelpFileName() {
158: return "readme.html";
159: }
160:
161: /**
162: * Returns the name of the Licence file for the plugin. This should
163: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
164: * directory.
165: *
166: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
167: * a licence file.
168: */
169: public String getLicenceFileName() {
170: return "licence.txt";
171: }
172:
173: /**
174: * Initialize this plugin.
175: */
176: public synchronized void initialize() throws PluginException {
177: super .initialize();
178:
179: _resources = new PluginResources(getClass().getName(), this );
180:
181: // Folder to store user settings.
182: try {
183: _userSettingsFolder = getPluginUserSettingsFolder();
184: } catch (IOException ex) {
185: throw new PluginException(ex);
186: }
187:
188: // Load plugin preferences.
189: loadPrefs();
190:
191: // Add menu.
192: final IApplication app = getApplication();
193: final ActionCollection coll = app.getActionCollection();
194: coll.add(new ConnectAction(app, _resources, _prefs, this ));
195: coll.add(new DisconnectAction(app, _resources, _prefs, this ));
196: coll.add(new ValidateSQLAction(app, _resources, _prefs, this ));
197: createMenu();
198: }
199:
200: /**
201: * Application is shutting down so save preferences.
202: */
203: public void unload() {
204: savePrefs();
205: super .unload();
206: }
207:
208: /**
209: * Called when a session started. The session sheet doesn't
210: * exist at this point.
211: *
212: * @param session The session that is starting.
213: */
214: public void sessionCreated(final ISession session) {
215: super .sessionCreated(session);
216: final WebServiceSessionProperties props = new WebServiceSessionProperties(
217: _prefs);
218: session.getApplication().getThreadPool().addTask(
219: new Runnable() {
220: public void run() {
221: props.setSQLConnection(session
222: .getSQLConnection());
223: GUIUtils
224: .processOnSwingEventThread(new Runnable() {
225: public void run() {
226: session
227: .putPluginObject(
228: SQLValidatorPlugin.this ,
229: PREFS_KEY,
230: props);
231: }
232: });
233: }
234: });
235: }
236:
237: /**
238: * Called when a session started.
239: *
240: * @param session The session that is starting.
241: *
242: * @return <TT>true</TT> if plugin is applicable to passed
243: * session else <TT>false</TT>.
244: */
245: public PluginSessionCallback sessionStarted(ISession session) {
246: session.getSessionInternalFrame().getSQLPanelAPI()
247: .addSQLPanelListener(_lis);
248: setupSQLEntryArea(session);
249:
250: PluginSessionCallback ret = new PluginSessionCallback() {
251: public void sqlInternalFrameOpened(
252: SQLInternalFrame sqlInternalFrame, ISession sess) {
253: // TODO
254: // Plugin supports only the main session window
255: }
256:
257: public void objectTreeInternalFrameOpened(
258: ObjectTreeInternalFrame objectTreeInternalFrame,
259: ISession sess) {
260: // TODO
261: // Plugin supports only the main session window
262: }
263: };
264: return ret;
265: }
266:
267: /**
268: * Called when a session shutdown.
269: *
270: * @param session The session that is ending.
271: */
272: public void sessionEnding(ISession session) {
273: session.getSessionInternalFrame().getSQLPanelAPI()
274: .removeSQLPanelListener(_lis);
275: WebServiceSessionProperties wssp = getWebServiceSessionProperties(session);
276: if (wssp != null) {
277: WebServiceSession wss = wssp.getWebServiceSession();
278: if (wss != null) {
279: wss.close();
280: }
281: }
282: session.removePluginObject(this , PREFS_KEY);
283: super .sessionEnding(session);
284: }
285:
286: /**
287: * Create panel for the Global Properties dialog.
288: *
289: * @return properties panel.
290: */
291: public IGlobalPreferencesPanel[] getGlobalPreferencePanels() {
292: return new IGlobalPreferencesPanel[] { new ValidatorGlobalPreferencesTab(
293: _prefs), };
294: }
295:
296: PluginResources getResources() {
297: return _resources;
298: }
299:
300: public WebServiceSessionProperties getWebServiceSessionProperties(
301: ISession session) {
302: if (session == null) {
303: throw new IllegalArgumentException("ISession == null");
304: }
305: return (WebServiceSessionProperties) session.getPluginObject(
306: this , PREFS_KEY);
307: }
308:
309: private void setupSQLEntryArea(ISession session) {
310: final ISQLPanelAPI api = session.getSessionInternalFrame()
311: .getSQLPanelAPI();
312: final ActionCollection coll = getApplication()
313: .getActionCollection();
314: api.addToSQLEntryAreaMenu(coll.get(ValidateSQLAction.class));
315: }
316:
317: /**
318: * Load from preferences file.
319: */
320: private void loadPrefs() {
321: try {
322: XMLBeanReader doc = new XMLBeanReader();
323: doc
324: .load(new File(_userSettingsFolder,
325: USER_PREFS_FILE_NAME), getClass()
326: .getClassLoader());
327: Iterator<?> it = doc.iterator();
328: if (it.hasNext()) {
329: _prefs = (WebServicePreferences) it.next();
330: }
331: } catch (FileNotFoundException ignore) {
332: s_log.info(USER_PREFS_FILE_NAME
333: + " not found - will be created");
334: } catch (Exception ex) {
335: s_log.error("Error occured reading from preferences file: "
336: + USER_PREFS_FILE_NAME, ex);
337: }
338: if (_prefs == null) {
339: _prefs = new WebServicePreferences();
340: }
341:
342: _prefs.setClientName(Version.getApplicationName() + "/"
343: + getDescriptiveName());
344: _prefs.setClientVersion(Version.getShortVersion() + "/"
345: + getVersion());
346: }
347:
348: /**
349: * Save preferences to disk.
350: */
351: private void savePrefs() {
352: try {
353: XMLBeanWriter wtr = new XMLBeanWriter(_prefs);
354: wtr
355: .save(new File(_userSettingsFolder,
356: USER_PREFS_FILE_NAME));
357: } catch (Exception ex) {
358: s_log.error("Error occured writing to preferences file: "
359: + USER_PREFS_FILE_NAME, ex);
360: }
361: }
362:
363: private void createMenu() {
364: final IApplication app = getApplication();
365: final ActionCollection coll = app.getActionCollection();
366:
367: final JMenu menu = _resources
368: .createMenu(IMenuResourceKeys.SQLVAL);
369: _resources.addToMenu(coll.get(ConnectAction.class), menu);
370: _resources.addToMenu(coll.get(DisconnectAction.class), menu);
371: _resources.addToMenu(coll.get(ValidateSQLAction.class), menu);
372:
373: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU, menu);
374: }
375:
376: private class SQLPanelListener extends SQLPanelAdapter {
377: public void sqlEntryAreaReplaced(SQLPanelEvent evt) {
378: setupSQLEntryArea(evt.getSession());
379: }
380: }
381: }
|