001: package net.sourceforge.squirrel_sql.plugins.userscript;
002:
003: /*
004: * Copyright (C) 2004 Gerd Wagner
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 net.sourceforge.squirrel_sql.client.IApplication;
022: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
023: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
024: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
025: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
026: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
027: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
028: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
029: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
030: import net.sourceforge.squirrel_sql.client.session.ISession;
031: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
032: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
033: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
034: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
035: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
036: import net.sourceforge.squirrel_sql.plugins.userscript.kernel.UserScriptAdmin;
037:
038: import javax.swing.*;
039: import java.net.URLClassLoader;
040: import java.util.Hashtable;
041:
042: /**
043: * The SQL Script plugin class.
044: */
045: public class UserScriptPlugin extends DefaultSessionPlugin {
046:
047: private interface IMenuResourceKeys {
048: String USER_SCRIPT = "userscript";
049: }
050:
051: /**
052: * Logger for this class.
053: */
054: @SuppressWarnings("unused")
055: private static ILogger s_log = LoggerController
056: .createLogger(UserScriptPlugin.class);
057:
058: private PluginResources _resources;
059:
060: private Hashtable<IIdentifier, UserScriptAdmin> _userScriptAdminsBySessionId = new Hashtable<IIdentifier, UserScriptAdmin>();
061: private URLClassLoader m_userScriptClassLoader;
062:
063: /**
064: * Return the internal name of this plugin.
065: *
066: * @return the internal name of this plugin.
067: */
068: public String getInternalName() {
069: return "userscript";
070: }
071:
072: /**
073: * Return the descriptive name of this plugin.
074: *
075: * @return the descriptive name of this plugin.
076: */
077: public String getDescriptiveName() {
078: return "User Scripts Plugin";
079: }
080:
081: /**
082: * Returns the current version of this plugin.
083: *
084: * @return the current version of this plugin.
085: */
086: public String getVersion() {
087: return "0.01";
088: }
089:
090: /**
091: * Returns the authors name.
092: *
093: * @return the authors name.
094: */
095: public String getAuthor() {
096: return "Gerd Wagner";
097: }
098:
099: /**
100: * Returns the name of the change log for the plugin. This should
101: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
102: * directory.
103: *
104: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
105: * a change log.
106: */
107: public String getChangeLogFileName() {
108: return "changes.txt";
109: }
110:
111: /**
112: * Returns the name of the Help file for the plugin. This should
113: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
114: * directory.
115: *
116: * @return the Help file name or <TT>null</TT> if plugin doesn't have
117: * a help file.
118: */
119: public String getHelpFileName() {
120: return "readme.txt";
121: }
122:
123: /**
124: * Returns the name of the Licence file for the plugin. This should
125: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
126: * directory.
127: *
128: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
129: * a licence file.
130: */
131: public String getLicenceFileName() {
132: return "licence.txt";
133: }
134:
135: /**
136: * Initialize this plugin.
137: */
138: public synchronized void initialize() throws PluginException {
139: super .initialize();
140: IApplication app = getApplication();
141:
142: _resources = new PluginResources(
143: "net.sourceforge.squirrel_sql.plugins.userscript.userscript",
144: this );
145:
146: ActionCollection coll = app.getActionCollection();
147: coll.add(new UserScriptAction(app, _resources, this ));
148: coll.add(new UserScriptSQLAction(app, _resources, this ));
149:
150: createMenu();
151: }
152:
153: /**
154: * Application is shutting down so save data.
155: */
156: public void unload() {
157: super .unload();
158: }
159:
160: public boolean allowsSessionStartedInBackground() {
161: return true;
162: }
163:
164: /**
165: * Called when a session started. Add commands to popup menu
166: * in object tree.
167: *
168: * @param session The session that is starting.
169: * @return <TT>true</TT> to indicate that this plugin is
170: * applicable to passed session.
171: */
172: public PluginSessionCallback sessionStarted(final ISession session) {
173:
174: GUIUtils.processOnSwingEventThread(new Runnable() {
175: public void run() {
176: ActionCollection coll = getApplication()
177: .getActionCollection();
178: IObjectTreeAPI api = session.getSessionInternalFrame()
179: .getObjectTreeAPI();
180:
181: api.addToPopup(DatabaseObjectType.TABLE, coll
182: .get(UserScriptAction.class));
183: api.addToPopup(DatabaseObjectType.PROCEDURE, coll
184: .get(UserScriptAction.class));
185: api.addToPopup(DatabaseObjectType.SESSION, coll
186: .get(UserScriptAction.class));
187: }
188: });
189:
190: UserScriptAdmin adm = new UserScriptAdmin(this , session);
191: _userScriptAdminsBySessionId.put(session.getIdentifier(), adm);
192:
193: PluginSessionCallback ret = new PluginSessionCallback() {
194: public void sqlInternalFrameOpened(
195: SQLInternalFrame sqlInternalFrame, ISession sess) {
196: // TODO
197: // Plugin supports only the main session window
198: }
199:
200: public void objectTreeInternalFrameOpened(
201: ObjectTreeInternalFrame objectTreeInternalFrame,
202: ISession sess) {
203: // TODO
204: // Plugin supports only the main session window
205: }
206: };
207: return ret;
208: }
209:
210: private void createMenu() {
211: IApplication app = getApplication();
212: ActionCollection coll = app.getActionCollection();
213:
214: JMenu menu = _resources
215: .createMenu(IMenuResourceKeys.USER_SCRIPT);
216:
217: _resources.addToMenu(coll.get(UserScriptSQLAction.class), menu);
218:
219: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU, menu);
220: }
221:
222: public UserScriptAdmin getUserScriptAdmin(ISession session) {
223: return _userScriptAdminsBySessionId
224: .get(session.getIdentifier());
225: }
226:
227: public URLClassLoader getUserScriptClassLoader() {
228: return m_userScriptClassLoader;
229: }
230:
231: public void setUserScriptClassLoader(URLClassLoader urlClassLoader) {
232: m_userScriptClassLoader = urlClassLoader;
233: }
234:
235: }
|