001: package net.sourceforge.squirrel_sql.plugins.sqlparam;
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: import java.util.HashMap;
021: import java.util.Hashtable;
022: import java.util.Map;
023:
024: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
025: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
026: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
027: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
028: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
029: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
030: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
031: import net.sourceforge.squirrel_sql.client.session.ISession;
032: import net.sourceforge.squirrel_sql.client.session.event.ISQLExecutionListener;
033: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
034: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
035: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
036:
037: /**
038: * The plugin class.
039: *
040: * @author Thorsten Mürell
041: */
042: public class SQLParamPlugin extends DefaultSessionPlugin {
043: private final static ILogger log = LoggerController
044: .createLogger(SQLParamPlugin.class);
045:
046: private Resources resources;
047: // private static final String PREFS_FILE_NAME = "sqlparamprefs.xml";
048: Map<String, String> cache;
049:
050: /**
051: * Remember which sqlpanelapis we've registered listeners with so that we
052: * can unregister them when it's time to unload.
053: */
054: HashMap<ISQLPanelAPI, ISQLExecutionListener> panelListenerMap = new HashMap<ISQLPanelAPI, ISQLExecutionListener>();
055:
056: /**
057: * Return the internal name of this plugin.
058: *
059: * @return the internal name of this plugin.
060: */
061: public String getInternalName() {
062: return "sqlparam";
063: }
064:
065: /**
066: * Return the descriptive name of this plugin.
067: *
068: * @return the descriptive name of this plugin.
069: */
070: public String getDescriptiveName() {
071: return "SQL Parametrisation";
072: }
073:
074: /**
075: * Returns the current version of this plugin.
076: *
077: * @return the current version of this plugin.
078: */
079: public String getVersion() {
080: return "1.0";
081: }
082:
083: /**
084: * Returns a comma separated list of other contributors.
085: *
086: * @return Contributors names.
087: */
088: @Override
089: public String getContributors() {
090: return "";
091: }
092:
093: /**
094: * Returns the authors name.
095: *
096: * @return the authors name.
097: */
098: public String getAuthor() {
099: return "Thorsten Mürell";
100: }
101:
102: /**
103: * Returns the name of the change log for the plugin. This should
104: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
105: * directory.
106: *
107: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
108: * a change log.
109: */
110: @Override
111: public String getChangeLogFileName() {
112: return null;
113: }
114:
115: /**
116: * Returns the name of the Help file for the plugin. This should
117: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
118: * directory.
119: *
120: * @return the Help file name or <TT>null</TT> if plugin doesn't have
121: * a help file.
122: */
123: @Override
124: public String getHelpFileName() {
125: return "readme.html";
126: }
127:
128: /**
129: * Returns the name of the Licence file for the plugin. This should
130: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
131: * directory.
132: *
133: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
134: * a licence file.
135: */
136: @Override
137: public String getLicenceFileName() {
138: return "licence.txt";
139: }
140:
141: /**
142: * Called on application startup after application started.
143: */
144: @Override
145: public void initialize() throws PluginException {
146: resources = new Resources(this );
147: }
148:
149: /**
150: * Called on plugin unloading.
151: */
152: @Override
153: public void unload() {
154: for (ISQLPanelAPI api : panelListenerMap.keySet()) {
155: api.removeSQLExecutionListener(panelListenerMap.get(api));
156: }
157: }
158:
159: /**
160: *
161: */
162: @Override
163: public boolean allowsSessionStartedInBackground() {
164: return true;
165: }
166:
167: /**
168: * Called on session creating by callback.
169: *
170: * @param session The session
171: */
172: @Override
173: public void sessionCreated(ISession session) {
174: cache = new Hashtable<String, String>();
175: }
176:
177: /**
178: * Returns the value cache.
179: *
180: * @return A map caching parameters to old values
181: */
182: public Map<String, String> getCache() {
183: return cache;
184: }
185:
186: /**
187: * Session has been started.
188: *
189: * @param session Session that has started.
190: * @return The callback to start on session events.
191: */
192: public PluginSessionCallback sessionStarted(final ISession session) {
193: log.info("Initializing plugin");
194: ISQLPanelAPI sqlPaneAPI = session.getSessionSheet()
195: .getSQLPaneAPI();
196:
197: initSQLParam(sqlPaneAPI, session);
198:
199: PluginSessionCallback ret = new PluginSessionCallback() {
200: public void sqlInternalFrameOpened(
201: final SQLInternalFrame sqlInternalFrame,
202: final ISession sess) {
203: initSQLParam(sqlInternalFrame.getSQLPanelAPI(), sess);
204: }
205:
206: public void objectTreeInternalFrameOpened(
207: ObjectTreeInternalFrame objectTreeInternalFrame,
208: ISession sess) {
209: // Nothing to do if object tree is opened.
210: }
211:
212: };
213:
214: return ret;
215: }
216:
217: /**
218: * This method is called on session closing an needs to free resources.
219: *
220: * @param session the session to be closed
221: */
222: @Override
223: public void sessionEnding(ISession session) {
224: ISQLPanelAPI sqlPaneAPI = session.getSessionSheet()
225: .getSQLPaneAPI();
226: ISQLExecutionListener listener = panelListenerMap
227: .remove(sqlPaneAPI);
228: sqlPaneAPI.removeSQLExecutionListener(listener);
229: }
230:
231: private void initSQLParam(final ISQLPanelAPI sqlPaneAPI,
232: final ISession session) {
233: final SQLParamPlugin plugin = this ;
234:
235: GUIUtils.processOnSwingEventThread(new Runnable() {
236: public void run() {
237: log.info("Adding SQL execution listener.");
238: ISQLExecutionListener listener = new SQLParamExecutionListener(
239: plugin, session);
240: sqlPaneAPI.addSQLExecutionListener(listener);
241: panelListenerMap.put(sqlPaneAPI, listener);
242: }
243:
244: });
245: }
246:
247: /**
248: * Retrieve plugins resources.
249: *
250: * @return Plugins resources.
251: */
252: public PluginResources getResources() {
253: return resources;
254: }
255:
256: }
|