001: package net.sourceforge.squirrel_sql.plugins.sqlscript;
002:
003: /*
004: * Copyright (C) 2001 Johan Compagner
005: * jcompagner@j-com.nl
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; 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.IOException;
023:
024: import javax.swing.JMenu;
025:
026: import net.sourceforge.squirrel_sql.client.IApplication;
027: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
028: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
029: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
030: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
031: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
032: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
033: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
034: import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
035: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
036: import net.sourceforge.squirrel_sql.client.session.ISession;
037: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
038: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
039: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
040: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
041: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferencesManager;
042: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferencesTab;
043: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateDataScriptAction;
044: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateDataScriptOfCurrentSQLAction;
045: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateSelectScriptAction;
046: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateTableOfCurrentSQLAction;
047: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateTableScriptAction;
048: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.CreateTemplateDataScriptAction;
049: import net.sourceforge.squirrel_sql.plugins.sqlscript.table_script.DropTableScriptAction;
050:
051: /**
052: * The SQL Script plugin class.
053: */
054: public class SQLScriptPlugin extends DefaultSessionPlugin {
055: private interface IMenuResourceKeys {
056: String SCRIPTS = "scripts";
057: }
058:
059: /** Logger for this class. */
060: private static ILogger s_log = LoggerController
061: .createLogger(SQLScriptPlugin.class);
062:
063: /** The app folder for this plugin. */
064: private File _pluginAppFolder;
065:
066: /** Folder to store user settings in. */
067: private File _userSettingsFolder;
068:
069: private PluginResources _resources;
070:
071: /**
072: * Return the internal name of this plugin.
073: *
074: * @return the internal name of this plugin.
075: */
076: public String getInternalName() {
077: return "sqlscript";
078: }
079:
080: /**
081: * Return the descriptive name of this plugin.
082: *
083: * @return the descriptive name of this plugin.
084: */
085: public String getDescriptiveName() {
086: return "SQL Scripts Plugin";
087: }
088:
089: /**
090: * Returns the current version of this plugin.
091: *
092: * @return the current version of this plugin.
093: */
094: public String getVersion() {
095: return "1.2";
096: }
097:
098: /**
099: * Returns the authors name.
100: *
101: * @return the authors name.
102: */
103: public String getAuthor() {
104: return "Johan Compagner";
105: }
106:
107: /**
108: * Returns the name of the change log for the plugin. This should
109: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
110: * directory.
111: *
112: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
113: * a change log.
114: */
115: public String getChangeLogFileName() {
116: return "changes.txt";
117: }
118:
119: /**
120: * Returns the name of the Help file for the plugin. This should
121: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
122: * directory.
123: *
124: * @return the Help file name or <TT>null</TT> if plugin doesn't have
125: * a help file.
126: */
127: public String getHelpFileName() {
128: return "readme.html";
129: }
130:
131: /**
132: * Returns the name of the Licence file for the plugin. This should
133: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
134: * directory.
135: *
136: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
137: * a licence file.
138: */
139: public String getLicenceFileName() {
140: return "licence.txt";
141: }
142:
143: /**
144: * @return Comma separated list of contributors.
145: */
146: public String getContributors() {
147: return "Gerd Wagner, John Murga, Rob Manning";
148: }
149:
150: /**
151: * Create preferences panel for the Global Preferences dialog.
152: *
153: * @return Preferences panel.
154: */
155: public IGlobalPreferencesPanel[] getGlobalPreferencePanels() {
156: SQLScriptPreferencesTab tab = new SQLScriptPreferencesTab();
157: return new IGlobalPreferencesPanel[] { tab };
158: }
159:
160: /**
161: * Initialize this plugin.
162: */
163: public synchronized void initialize() throws PluginException {
164: super .initialize();
165: IApplication app = getApplication();
166:
167: //PluginManager pmgr = app.getPluginManager();
168:
169: // Folder within plugins folder that belongs to this
170: // plugin.
171: try {
172: _pluginAppFolder = getPluginAppSettingsFolder();
173: } catch (IOException ex) {
174: throw new PluginException(ex);
175: }
176:
177: // Folder to store user settings.
178: try {
179: _userSettingsFolder = getPluginUserSettingsFolder();
180: } catch (IOException ex) {
181: throw new PluginException(ex);
182: }
183:
184: _resources = new SQLPluginResources(
185: "net.sourceforge.squirrel_sql.plugins.sqlscript.sqlscript",
186: this );
187:
188: ActionCollection coll = app.getActionCollection();
189: coll.add(new CreateTableScriptAction(app, _resources, this ));
190: coll.add(new CreateSelectScriptAction(app, _resources, this ));
191: coll.add(new DropTableScriptAction(app, _resources, this ));
192: coll.add(new CreateDataScriptAction(app, _resources, this ));
193: coll.add(new CreateTemplateDataScriptAction(app, _resources,
194: this ));
195: coll.add(new CreateDataScriptOfCurrentSQLAction(app,
196: _resources, this ));
197: coll.add(new CreateTableOfCurrentSQLAction(app, _resources,
198: this ));
199: createMenu();
200:
201: SQLScriptPreferencesManager.initialize(this );
202: }
203:
204: /**
205: * Application is shutting down so save data.
206: */
207: public void unload() {
208: super .unload();
209: SQLScriptPreferencesManager.unload();
210: }
211:
212: public boolean allowsSessionStartedInBackground() {
213: return true;
214: }
215:
216: /**
217: * Called when a session started. Add commands to popup menu
218: * in object tree.
219: *
220: * @param session The session that is starting.
221: *
222: * @return <TT>true</TT> to indicate that this plugin is
223: * applicable to passed session.
224: */
225: public PluginSessionCallback sessionStarted(final ISession session) {
226: GUIUtils.processOnSwingEventThread(new Runnable() {
227: public void run() {
228: addActionsToPopup(session);
229: }
230: });
231:
232: PluginSessionCallback ret = new PluginSessionCallback() {
233: public void sqlInternalFrameOpened(
234: SQLInternalFrame sqlInternalFrame, ISession sess) {
235: ActionCollection coll = sess.getApplication()
236: .getActionCollection();
237: sqlInternalFrame.addSeparatorToToolbar();
238: sqlInternalFrame.addToToolbar(coll
239: .get(CreateTableOfCurrentSQLAction.class));
240:
241: sqlInternalFrame.addToToolsPopUp("sql2table", coll
242: .get(CreateTableOfCurrentSQLAction.class));
243: sqlInternalFrame.addToToolsPopUp("sql2ins", coll
244: .get(CreateDataScriptOfCurrentSQLAction.class));
245: }
246:
247: public void objectTreeInternalFrameOpened(
248: ObjectTreeInternalFrame objectTreeInternalFrame,
249: ISession sess) {
250: ActionCollection coll = sess.getApplication()
251: .getActionCollection();
252: objectTreeInternalFrame.getObjectTreeAPI().addToPopup(
253: DatabaseObjectType.TABLE,
254: coll.get(CreateTableScriptAction.class));
255: objectTreeInternalFrame.getObjectTreeAPI().addToPopup(
256: DatabaseObjectType.TABLE,
257: coll.get(CreateSelectScriptAction.class));
258: objectTreeInternalFrame.getObjectTreeAPI().addToPopup(
259: DatabaseObjectType.TABLE,
260: coll.get(DropTableScriptAction.class));
261: objectTreeInternalFrame.getObjectTreeAPI().addToPopup(
262: DatabaseObjectType.TABLE,
263: coll.get(CreateDataScriptAction.class));
264: objectTreeInternalFrame.getObjectTreeAPI().addToPopup(
265: DatabaseObjectType.TABLE,
266: coll.get(CreateTemplateDataScriptAction.class));
267: }
268: };
269:
270: return ret;
271: }
272:
273: private void addActionsToPopup(ISession session) {
274: ActionCollection coll = getApplication().getActionCollection();
275:
276: //IObjectTreeAPI api = session.getObjectTreeAPI(this);
277: IObjectTreeAPI api = FrameWorkAcessor.getObjectTreeAPI(session,
278: this );
279:
280: JMenu menu = _resources.createMenu(IMenuResourceKeys.SCRIPTS);
281:
282: api.addToPopup(DatabaseObjectType.TABLE, getTableMenu(true));
283: api.addToPopup(DatabaseObjectType.VIEW, getTableMenu(false));
284:
285: session.addSeparatorToToolbar();
286: session.addToToolbar(coll
287: .get(CreateTableOfCurrentSQLAction.class));
288:
289: session.getSessionInternalFrame().addToToolsPopUp("sql2table",
290: coll.get(CreateTableOfCurrentSQLAction.class));
291: session.getSessionInternalFrame().addToToolsPopUp("sql2ins",
292: coll.get(CreateDataScriptOfCurrentSQLAction.class));
293: }
294:
295: private void createMenu() {
296: IApplication app = getApplication();
297: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU,
298: getSessionMenu());
299: }
300:
301: private JMenu getSessionMenu() {
302: IApplication app = getApplication();
303: ActionCollection coll = app.getActionCollection();
304:
305: JMenu menu = _resources.createMenu(IMenuResourceKeys.SCRIPTS);
306: _resources.addToMenu(coll.get(CreateDataScriptAction.class),
307: menu);
308: _resources.addToMenu(coll
309: .get(CreateTemplateDataScriptAction.class), menu);
310: _resources.addToMenu(coll.get(CreateTableScriptAction.class),
311: menu);
312: _resources.addToMenu(coll.get(CreateSelectScriptAction.class),
313: menu);
314: _resources.addToMenu(coll.get(DropTableScriptAction.class),
315: menu);
316: _resources.addToMenu(coll
317: .get(CreateDataScriptOfCurrentSQLAction.class), menu);
318: _resources.addToMenu(coll
319: .get(CreateTableOfCurrentSQLAction.class), menu);
320: return menu;
321: }
322:
323: private JMenu getTableMenu(boolean includeDrop) {
324: IApplication app = getApplication();
325: ActionCollection coll = app.getActionCollection();
326:
327: JMenu menu = _resources.createMenu(IMenuResourceKeys.SCRIPTS);
328: _resources.addToMenu(coll.get(CreateDataScriptAction.class),
329: menu);
330: _resources.addToMenu(coll
331: .get(CreateTemplateDataScriptAction.class), menu);
332: _resources.addToMenu(coll.get(CreateTableScriptAction.class),
333: menu);
334: _resources.addToMenu(coll.get(CreateSelectScriptAction.class),
335: menu);
336: if (includeDrop) {
337: _resources.addToMenu(coll.get(DropTableScriptAction.class),
338: menu);
339: }
340: return menu;
341: }
342:
343: public Object getExternalService() {
344: return new SQLScriptExternalService(this);
345: }
346:
347: }
|