001: package net.sourceforge.squirrel_sql.plugins.mysql;
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 javax.swing.JMenu;
022:
023: import net.sourceforge.squirrel_sql.client.IApplication;
024: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
025: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
026: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
027: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
028: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
029: import net.sourceforge.squirrel_sql.client.plugin.PluginQueryTokenizerPreferencesManager;
030: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
031: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
032: import net.sourceforge.squirrel_sql.client.plugin.gui.PluginGlobalPreferencesTab;
033: import net.sourceforge.squirrel_sql.client.plugin.gui.PluginQueryTokenizerPreferencesPanel;
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.client.session.mainpanel.objecttree.expanders.TableWithChildNodesExpander;
038: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.DatabaseObjectInfoTab;
039: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
040: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
041: import net.sourceforge.squirrel_sql.fw.preferences.IQueryTokenizerPreferenceBean;
042: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
043: import net.sourceforge.squirrel_sql.fw.util.StringManager;
044: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
045: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
046: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
047: import net.sourceforge.squirrel_sql.plugins.mysql.action.AlterTableAction;
048: import net.sourceforge.squirrel_sql.plugins.mysql.action.AnalyzeTableAction;
049: import net.sourceforge.squirrel_sql.plugins.mysql.action.CheckTableAction;
050: import net.sourceforge.squirrel_sql.plugins.mysql.action.CopyTableAction;
051: import net.sourceforge.squirrel_sql.plugins.mysql.action.CreateDatabaseAction;
052: import net.sourceforge.squirrel_sql.plugins.mysql.action.CreateMysqlTableScriptAction;
053: import net.sourceforge.squirrel_sql.plugins.mysql.action.DropDatabaseAction;
054: import net.sourceforge.squirrel_sql.plugins.mysql.action.ExplainSelectTableAction;
055: import net.sourceforge.squirrel_sql.plugins.mysql.action.ExplainTableAction;
056: import net.sourceforge.squirrel_sql.plugins.mysql.action.OptimizeTableAction;
057: import net.sourceforge.squirrel_sql.plugins.mysql.action.RenameTableAction;
058: import net.sourceforge.squirrel_sql.plugins.mysql.expander.MysqlTableTriggerExtractorImpl;
059: import net.sourceforge.squirrel_sql.plugins.mysql.expander.SessionExpander;
060: import net.sourceforge.squirrel_sql.plugins.mysql.expander.UserParentExpander;
061: import net.sourceforge.squirrel_sql.plugins.mysql.prefs.MysqlPreferenceBean;
062: import net.sourceforge.squirrel_sql.plugins.mysql.tab.DatabaseStatusTab;
063: import net.sourceforge.squirrel_sql.plugins.mysql.tab.MysqlProcedureSourceTab;
064: import net.sourceforge.squirrel_sql.plugins.mysql.tab.MysqlTriggerDetailsTab;
065: import net.sourceforge.squirrel_sql.plugins.mysql.tab.MysqlTriggerSourceTab;
066: import net.sourceforge.squirrel_sql.plugins.mysql.tab.MysqlViewSourceTab;
067: import net.sourceforge.squirrel_sql.plugins.mysql.tab.OpenTablesTab;
068: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ProcessesTab;
069: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowColumnsTab;
070: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowIndexesTab;
071: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowLogsTab;
072: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowMasterLogsTab;
073: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowMasterStatusTab;
074: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowSlaveStatusTab;
075: import net.sourceforge.squirrel_sql.plugins.mysql.tab.ShowVariablesTab;
076: import net.sourceforge.squirrel_sql.plugins.mysql.tab.TableStatusTab;
077: import net.sourceforge.squirrel_sql.plugins.mysql.tab.UserGrantsTab;
078: import net.sourceforge.squirrel_sql.plugins.mysql.tokenizer.MysqlQueryTokenizer;
079:
080: /**
081: * MySQL plugin class.
082: *
083: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
084: */
085: public class MysqlPlugin extends DefaultSessionPlugin {
086: /**
087: * Internationalized strings for this class.
088: */
089: private static final StringManager s_stringMgr = StringManagerFactory
090: .getStringManager(MysqlPlugin.class);
091:
092: /** Logger for this class. */
093: @SuppressWarnings("unused")
094: private final static ILogger s_log = LoggerController
095: .createLogger(MysqlPlugin.class);
096:
097: /** Plugin resources. */
098: private PluginResources _resources;
099:
100: /** API for the Obejct Tree. */
101: private IObjectTreeAPI _treeAPI;
102:
103: /** MySQL menu. */
104: private JMenu _mySQLMenu;
105:
106: /** manages our query tokenizing preferences */
107: private PluginQueryTokenizerPreferencesManager _prefsManager = null;
108:
109: interface i18n {
110: // i18n[MysqlPlugin.title=MySQL]
111: String title = s_stringMgr.getString("MysqlPlugin.title");
112:
113: // i18n[MysqlPlugin.hint=Preferences for MySQL]
114: String hint = s_stringMgr.getString("MysqlPlugin.hint");
115:
116: //i18n[MysqlPlugin.showProcedureSource=Show procedure source]
117: String SHOW_PROCEDURE_SOURCE = s_stringMgr
118: .getString("MysqlPlugin.showProcedureSource");
119:
120: //i18n[MysqlPlugin.showTriggerSource=Show trigger source]
121: String SHOW_TRIGGER_SOURCE = s_stringMgr
122: .getString("MysqlPlugin.showTriggerSource");
123:
124: //i18n[MysqlPlugin.showViewSource=Show view source]
125: String SHOW_VIEW_SOURCE = s_stringMgr
126: .getString("MysqlPlugin.showViewSource");
127: }
128:
129: /**
130: * Return the internal name of this plugin.
131: *
132: * @return the internal name of this plugin.
133: */
134: public String getInternalName() {
135: return "mysql";
136: }
137:
138: /**
139: * Return the descriptive name of this plugin.
140: *
141: * @return the descriptive name of this plugin.
142: */
143: public String getDescriptiveName() {
144: return "MySQL Plugin";
145: }
146:
147: /**
148: * Returns the current version of this plugin.
149: *
150: * @return the current version of this plugin.
151: */
152: public String getVersion() {
153: return "0.33";
154: }
155:
156: /**
157: * Returns the authors name.
158: *
159: * @return the authors name.
160: */
161: public String getAuthor() {
162: return "Colin Bell";
163: }
164:
165: /**
166: * Load this plugin.
167: *
168: * @param app Application API.
169: */
170: public synchronized void load(IApplication app)
171: throws PluginException {
172: super .load(app);
173:
174: // Folder to store user settings.
175: // try
176: // {
177: // _userSettingsFolder = getPluginUserSettingsFolder();
178: // }
179: // catch (IOException ex)
180: // {
181: // throw new PluginException(ex);
182: // }
183:
184: _resources = new MysqlResources(getClass().getName(), this );
185: }
186:
187: /**
188: * Retrieve the name of the change log.
189: *
190: * @return The name of the change log.
191: */
192: public String getChangeLogFileName() {
193: return "changes.txt";
194: }
195:
196: /**
197: * Retrieve the name of the help file.
198: *
199: * @return The nane of the help file.
200: */
201: public String getHelpFileName() {
202: return "readme.html";
203: }
204:
205: /**
206: * Retrieve the name of the licence file.
207: *
208: * @return The nane of the licence file.
209: */
210: public String getLicenceFileName() {
211: return "licence.txt";
212: }
213:
214: /**
215: * Create panel for the Global Properties dialog.
216: *
217: * @return properties panel.
218: */
219: public IGlobalPreferencesPanel[] getGlobalPreferencePanels() {
220: PluginQueryTokenizerPreferencesPanel _prefsPanel = new PluginQueryTokenizerPreferencesPanel(
221: _prefsManager, "MySQL");
222:
223: PluginGlobalPreferencesTab tab = new PluginGlobalPreferencesTab(
224: _prefsPanel);
225:
226: tab.setHint(i18n.hint);
227: tab.setTitle(i18n.title);
228:
229: return new IGlobalPreferencesPanel[] { tab };
230: }
231:
232: /**
233: * Initialize this plugin.
234: */
235: public synchronized void initialize() throws PluginException {
236: super .initialize();
237:
238: final IApplication app = getApplication();
239: final ActionCollection coll = app.getActionCollection();
240:
241: coll.add(new AnalyzeTableAction(app, _resources, this ));
242: coll
243: .add(new CreateMysqlTableScriptAction(app, _resources,
244: this ));
245: coll.add(new CheckTableAction.ChangedCheckTableAction(app,
246: _resources, this ));
247: coll.add(new CheckTableAction.ExtendedCheckTableAction(app,
248: _resources, this ));
249: coll.add(new CheckTableAction.FastCheckTableAction(app,
250: _resources, this ));
251: coll.add(new CheckTableAction.MediumCheckTableAction(app,
252: _resources, this ));
253: coll.add(new CheckTableAction.QuickCheckTableAction(app,
254: _resources, this ));
255: coll.add(new ExplainSelectTableAction(app, _resources, this ));
256: coll.add(new ExplainTableAction(app, _resources, this ));
257: coll.add(new OptimizeTableAction(app, _resources, this ));
258: coll.add(new RenameTableAction(app, _resources, this ));
259:
260: coll.add(new CreateDatabaseAction(app, _resources, this ));
261: coll.add(new DropDatabaseAction(app, _resources, this ));
262: coll.add(new AlterTableAction(app, _resources, this ));
263: // coll.add(new CreateTableAction(app, _resources, this));
264: coll.add(new CopyTableAction(app, _resources, this ));
265:
266: _mySQLMenu = createFullMysqlMenu();
267: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU, _mySQLMenu);
268: super .registerSessionMenu(_mySQLMenu);
269:
270: _prefsManager = new PluginQueryTokenizerPreferencesManager();
271: _prefsManager.initialize(this , new MysqlPreferenceBean());
272: }
273:
274: /**
275: * Application is shutting down so save preferences.
276: */
277: public void unload() {
278: super .unload();
279: }
280:
281: public boolean allowsSessionStartedInBackground() {
282: return true;
283: }
284:
285: /**
286: * Session has been started. If this is a MySQL session
287: * then setup MySQL tabs etc.
288: *
289: * @param session Session that has started.
290: *
291: * @return <TT>true</TT> if session is MySQL in which case this plugin
292: * is interested in it.
293: */
294: public PluginSessionCallback sessionStarted(final ISession session) {
295: if (!isPluginSession(session)) {
296: return null;
297: }
298:
299: GUIUtils.processOnSwingEventThread(new Runnable() {
300: public void run() {
301: updateTreeApi(session);
302: }
303: });
304:
305: installMysqlQueryTokenizer(session);
306:
307: PluginSessionCallback ret = new PluginSessionCallback() {
308: public void sqlInternalFrameOpened(
309: SQLInternalFrame sqlInternalFrame, ISession sess) {
310: // TODO
311: // Plugin supports only the main session window
312: }
313:
314: public void objectTreeInternalFrameOpened(
315: ObjectTreeInternalFrame objectTreeInternalFrame,
316: ISession sess) {
317: // TODO
318: // Plugin supports only the main session window
319: }
320: };
321: return ret;
322: }
323:
324: @Override
325: protected boolean isPluginSession(ISession session) {
326: return DialectFactory.isMySQL(session.getMetaData());
327: }
328:
329: /**
330: * Determines from the user's preference whether or not to install the
331: * custom query tokenizer, and if so configure installs it.
332: *
333: * @param session the session to install the custom query tokenizer in.
334: */
335: private void installMysqlQueryTokenizer(ISession session) {
336:
337: IQueryTokenizerPreferenceBean _prefs = _prefsManager
338: .getPreferences();
339:
340: if (_prefs.isInstallCustomQueryTokenizer()) {
341: session.setQueryTokenizer(new MysqlQueryTokenizer(_prefs));
342: }
343:
344: }
345:
346: private void updateTreeApi(ISession session) {
347: _treeAPI = session.getSessionInternalFrame().getObjectTreeAPI();
348: final ActionCollection coll = getApplication()
349: .getActionCollection();
350:
351: // Show users in the object tee.
352: _treeAPI.addExpander(DatabaseObjectType.SESSION,
353: new SessionExpander());
354: _treeAPI.addExpander(IObjectTypes.USER_PARENT,
355: new UserParentExpander(this ));
356:
357: // Tabs to add to the database node.
358: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
359: new DatabaseStatusTab());
360: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
361: new ProcessesTab());
362: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
363: new ShowVariablesTab());
364: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
365: new ShowLogsTab());
366: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
367: new ShowMasterStatusTab());
368: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
369: new ShowMasterLogsTab());
370: _treeAPI.addDetailTab(DatabaseObjectType.SESSION,
371: new ShowSlaveStatusTab());
372:
373: // Tabs to add to the catalog nodes.
374: _treeAPI.addDetailTab(DatabaseObjectType.CATALOG,
375: new OpenTablesTab());
376: _treeAPI.addDetailTab(DatabaseObjectType.CATALOG,
377: new TableStatusTab());
378:
379: // Tabs to add to the table nodes.
380: _treeAPI.addDetailTab(DatabaseObjectType.TABLE,
381: new ShowColumnsTab());
382: _treeAPI.addDetailTab(DatabaseObjectType.TABLE,
383: new ShowIndexesTab());
384:
385: // Tabs to add to the user nodes.
386: _treeAPI.addDetailTab(DatabaseObjectType.USER,
387: new UserGrantsTab());
388:
389: // Options in popup menu.
390: _treeAPI.addToPopup(coll.get(CreateDatabaseAction.class));
391:
392: // _treeAPI.addToPopup(DatabaseObjectType.SESSION, coll.get(CreateTableAction.class));
393: // _treeAPI.addToPopup(DatabaseObjectType.CATALOG, coll.get(CreateTableAction.class));
394: _treeAPI.addToPopup(DatabaseObjectType.CATALOG, coll
395: .get(DropDatabaseAction.class));
396:
397: _treeAPI.addToPopup(DatabaseObjectType.TABLE,
398: createMysqlTableMenu());
399:
400: updateTreeApiForMysql5(session);
401: }
402:
403: private void updateTreeApiForMysql5(ISession session) {
404: if (!DialectFactory.isMySQL5(session.getMetaData())) {
405: return;
406: }
407: String stmtSep = session.getQueryTokenizer()
408: .getSQLStatementSeparator();
409:
410: MysqlProcedureSourceTab procSourceTab = new MysqlProcedureSourceTab(
411: i18n.SHOW_PROCEDURE_SOURCE);
412: _treeAPI.addDetailTab(DatabaseObjectType.PROCEDURE,
413: procSourceTab);
414:
415: // Tab to add to view nodes.
416: MysqlViewSourceTab viewSourceTab = new MysqlViewSourceTab(
417: i18n.SHOW_VIEW_SOURCE, stmtSep);
418: _treeAPI.addDetailTab(DatabaseObjectType.VIEW, viewSourceTab);
419:
420: // Show triggers for tables
421: TableWithChildNodesExpander trigExp = new TableWithChildNodesExpander();
422: trigExp
423: .setTableTriggerExtractor(new MysqlTableTriggerExtractorImpl());
424: _treeAPI.addExpander(DatabaseObjectType.TABLE, trigExp);
425:
426: // tabs for triggers
427: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
428: new DatabaseObjectInfoTab());
429: _treeAPI.addDetailTab(DatabaseObjectType.TRIGGER,
430: new MysqlTriggerDetailsTab());
431: MysqlTriggerSourceTab trigSourceTab = new MysqlTriggerSourceTab(
432: i18n.SHOW_TRIGGER_SOURCE, stmtSep);
433: _treeAPI
434: .addDetailTab(DatabaseObjectType.TRIGGER, trigSourceTab);
435:
436: }
437:
438: /**
439: * Create menu containing actions relevant for table nodes in the object
440: * tree.
441: *
442: * @return The menu object.
443: */
444: private JMenu createMysqlTableMenu() {
445: final IApplication app = getApplication();
446: final ActionCollection coll = app.getActionCollection();
447:
448: final JMenu mysqlMenu = _resources
449: .createMenu(MysqlResources.IMenuResourceKeys.MYSQL);
450:
451: _resources.addToMenu(coll
452: .get(CreateMysqlTableScriptAction.class), mysqlMenu);
453:
454: _resources.addToMenu(coll.get(AnalyzeTableAction.class),
455: mysqlMenu);
456: _resources.addToMenu(coll.get(ExplainTableAction.class),
457: mysqlMenu);
458: _resources.addToMenu(coll.get(ExplainSelectTableAction.class),
459: mysqlMenu);
460: _resources.addToMenu(coll.get(OptimizeTableAction.class),
461: mysqlMenu);
462:
463: final JMenu checkTableMenu = _resources
464: .createMenu(MysqlResources.IMenuResourceKeys.CHECK_TABLE);
465: _resources.addToMenu(coll
466: .get(CheckTableAction.ChangedCheckTableAction.class),
467: checkTableMenu);
468: _resources.addToMenu(coll
469: .get(CheckTableAction.ExtendedCheckTableAction.class),
470: checkTableMenu);
471: _resources.addToMenu(coll
472: .get(CheckTableAction.FastCheckTableAction.class),
473: checkTableMenu);
474: _resources.addToMenu(coll
475: .get(CheckTableAction.MediumCheckTableAction.class),
476: checkTableMenu);
477: _resources.addToMenu(coll
478: .get(CheckTableAction.QuickCheckTableAction.class),
479: checkTableMenu);
480: mysqlMenu.add(checkTableMenu);
481:
482: _resources.addToMenu(coll.get(AlterTableAction.class),
483: mysqlMenu);
484: _resources
485: .addToMenu(coll.get(CopyTableAction.class), mysqlMenu);
486: _resources.addToMenu(coll.get(RenameTableAction.class),
487: mysqlMenu);
488:
489: return mysqlMenu;
490: }
491:
492: /**
493: * Create menu containing all MYSQL actions.
494: *
495: * @return The menu object.
496: */
497: private JMenu createFullMysqlMenu() {
498: final IApplication app = getApplication();
499: final ActionCollection coll = app.getActionCollection();
500:
501: final JMenu mysqlMenu = _resources
502: .createMenu(MysqlResources.IMenuResourceKeys.MYSQL);
503:
504: _resources.addToMenu(coll.get(CreateDatabaseAction.class),
505: mysqlMenu);
506: // _resources.addToMenu(coll.get(DropDatabaseAction.class), mysqlMenu);
507:
508: _resources.addToMenu(coll
509: .get(CreateMysqlTableScriptAction.class), mysqlMenu);
510: // _resources.addToMenu(coll.get(CreateTableAction.class), mysqlMenu);
511:
512: _resources.addToMenu(coll.get(AnalyzeTableAction.class),
513: mysqlMenu);
514: _resources.addToMenu(coll.get(ExplainTableAction.class),
515: mysqlMenu);
516: _resources.addToMenu(coll.get(ExplainSelectTableAction.class),
517: mysqlMenu);
518: _resources.addToMenu(coll.get(OptimizeTableAction.class),
519: mysqlMenu);
520:
521: final JMenu checkTableMenu = _resources
522: .createMenu(MysqlResources.IMenuResourceKeys.CHECK_TABLE);
523: _resources.addToMenu(coll
524: .get(CheckTableAction.ChangedCheckTableAction.class),
525: checkTableMenu);
526: _resources.addToMenu(coll
527: .get(CheckTableAction.ExtendedCheckTableAction.class),
528: checkTableMenu);
529: _resources.addToMenu(coll
530: .get(CheckTableAction.FastCheckTableAction.class),
531: checkTableMenu);
532: _resources.addToMenu(coll
533: .get(CheckTableAction.MediumCheckTableAction.class),
534: checkTableMenu);
535: _resources.addToMenu(coll
536: .get(CheckTableAction.QuickCheckTableAction.class),
537: checkTableMenu);
538: mysqlMenu.add(checkTableMenu);
539:
540: return mysqlMenu;
541: }
542:
543: }
|