001: /*
002: * Copyright (C) 2003 Joseph Mocker
003: * mock-sf@misfit.dhs.org
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package net.sourceforge.squirrel_sql.plugins.sqlbookmark;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.Properties;
030:
031: import javax.swing.JComponent;
032: import javax.swing.JMenu;
033: import javax.swing.JMenuItem;
034: import javax.swing.JSeparator;
035:
036: import net.sourceforge.squirrel_sql.client.IApplication;
037: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
038: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
039: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
040: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
041: import net.sourceforge.squirrel_sql.client.plugin.DefaultSessionPlugin;
042: import net.sourceforge.squirrel_sql.client.plugin.PluginException;
043: import net.sourceforge.squirrel_sql.client.plugin.PluginResources;
044: import net.sourceforge.squirrel_sql.client.plugin.PluginSessionCallback;
045: import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
046: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
047: import net.sourceforge.squirrel_sql.client.session.ISession;
048: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
049: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
050: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
051:
052: /**
053: * Main entry into the SQL Bookmark plugin.
054: * <p/>
055: * This plugin allows you to maintain a set of frequently used SQL
056: * scripts for easy playback. There is also a parameter replacement
057: * syntax available for the SQL files.
058: *
059: * @author Joseph Mocker
060: */
061: public class SQLBookmarkPlugin extends DefaultSessionPlugin {
062:
063: /** Logger for this class. */
064: private final static ILogger s_log = LoggerController
065: .createLogger(SQLBookmarkPlugin.class);
066:
067: private ArrayList<ISQLPanelAPI> _sqlPanelAPIsListeningForBookmarks = new ArrayList<ISQLPanelAPI>();
068:
069: private static final String BOOKMARKS_PROPS_FILE = "bookmarks.properties";
070: static final String BOOKMARK_PROP_DEFAULT_MARKS_IN_POPUP = "squirrelMarksInPopup";
071: private Properties _boomarkProps;
072:
073: private interface IMenuResourceKeys {
074: String BOOKMARKS = "bookmarks";
075: }
076:
077: private static String RESOURCE_PATH = "net.sourceforge.squirrel_sql.plugins.sqlbookmark.sqlbookmark";
078:
079: private static ILogger logger = LoggerController
080: .createLogger(SQLBookmarkPlugin.class);
081:
082: /**
083: * The app folder for this plugin.
084: */
085: private File pluginAppFolder;
086:
087: private PluginResources resources;
088:
089: /**
090: * The bookmark menu
091: */
092: private JMenu menu;
093:
094: /**
095: * All the current bookmarkManager
096: */
097: private BookmarkManager bookmarkManager;
098:
099: /**
100: * Returns the plugin version.
101: *
102: * @return the plugin version.
103: */
104: public String getVersion() {
105: return "2.0.1";
106: }
107:
108: /**
109: * Returns the authors name.
110: *
111: * @return the authors name.
112: */
113: public String getAuthor() {
114: return "Joseph Mocker";
115: }
116:
117: public String getContributors() {
118: return "Gerd Wagner";
119: }
120:
121: /**
122: * Return the internal name of this plugin.
123: *
124: * @return the internal name of this plugin.
125: */
126: public String getInternalName() {
127: return "sqlbookmark";
128: }
129:
130: /**
131: * Return the descriptive name of this plugin.
132: *
133: * @return the descriptive name of this plugin.
134: */
135: public String getDescriptiveName() {
136: return "SQL Bookmark Plugin";
137: }
138:
139: /**
140: * Returns the name of the Help file for the plugin.
141: *
142: * @return the help file name.
143: */
144: public String getHelpFileName() {
145: return "readme.html";
146: }
147:
148: /**
149: * Returns the name of the Help file for the plugin.
150: *
151: * @return the license file name.
152: */
153: public String getLicenceFileName() {
154: return "licence.txt";
155: }
156:
157: /**
158: * Returns the name of the change log for the plugin. This should
159: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
160: * directory.
161: *
162: * @return the changelog file name or <TT>null</TT> if plugin doesn't have
163: * a change log.
164: */
165: public String getChangeLogFileName() {
166: return "changes.txt";
167: }
168:
169: /**
170: * Return the plugin resources. Used by other classes.
171: *
172: * @return plugin resources.
173: */
174: protected PluginResources getResources() {
175: return resources;
176: }
177:
178: /**
179: * Get and return a string from the plugin resources.
180: *
181: * @param name name of the resource string to return.
182: * @return resource string.
183: */
184: protected String getResourceString(String name) {
185: return resources.getString(name);
186: }
187:
188: /**
189: * Returns a handle to the current bookmark manager.
190: *
191: * @return the bookmark manager.
192: */
193: BookmarkManager getBookmarkManager() {
194: return bookmarkManager;
195: }
196:
197: /**
198: * Set the bookmark manager.
199: *
200: * @param bookmarks new manager to register.
201: */
202: protected void setBookmarkManager(BookmarkManager bookmarks) {
203: this .bookmarkManager = bookmarks;
204: }
205:
206: public Object getExternalService() {
207: return new BoomarksExternalServiceImpl(this );
208: }
209:
210: /**
211: * Initialize this plugin.
212: */
213: public synchronized void initialize() throws PluginException {
214: super .initialize();
215:
216: IApplication app = getApplication();
217:
218: // Folder within plugins folder that belongs to this
219: // plugin.
220: try {
221: pluginAppFolder = getPluginAppSettingsFolder();
222: } catch (IOException ex) {
223: throw new PluginException(ex);
224: }
225:
226: // Load resources such as menu items, etc...
227: resources = new SQLBookmarkResources(RESOURCE_PATH, this );
228:
229: bookmarkManager = new BookmarkManager(this );
230: // Load plugin preferences.
231: try {
232: bookmarkManager.load();
233: } catch (IOException e) {
234: if (!(e instanceof FileNotFoundException)) {
235: logger.error("Problem loading bookmarkManager", e);
236: }
237: }
238:
239: ActionCollection coll = app.getActionCollection();
240: coll.add(new AddBookmarkAction(app, resources, this ));
241: coll.add(new EditBookmarksAction(app, resources, this ));
242: coll.add(new RunBookmarkAction(app, resources, this ));
243: createMenu();
244:
245: rebuildMenu();
246: }
247:
248: public boolean allowsSessionStartedInBackground() {
249: return true;
250: }
251:
252: public PluginSessionCallback sessionStarted(final ISession session) {
253: GUIUtils.processOnSwingEventThread(new Runnable() {
254: public void run() {
255: addBookmarkAction(session);
256: }
257: });
258:
259: PluginSessionCallback ret = new PluginSessionCallback() {
260: public void sqlInternalFrameOpened(
261: SQLInternalFrame sqlInternalFrame, ISession sess) {
262: ActionCollection coll = getApplication()
263: .getActionCollection();
264: sqlInternalFrame.addSeparatorToToolbar();
265: sqlInternalFrame.addToToolbar(coll
266: .get(AddBookmarkAction.class));
267: sqlInternalFrame.addToToolbar(coll
268: .get(EditBookmarksAction.class));
269: sqlInternalFrame.addToToolsPopUp("bookmarkadd", coll
270: .get(AddBookmarkAction.class));
271: sqlInternalFrame.addToToolsPopUp("bookmarkedit", coll
272: .get(EditBookmarksAction.class));
273:
274: ISQLPanelAPI sqlPaneAPI = sqlInternalFrame
275: .getSQLPanelAPI();
276: CompleteBookmarkAction cba = new CompleteBookmarkAction(
277: sess.getApplication(), resources, sqlPaneAPI
278: .getSQLEntryPanel(),
279: SQLBookmarkPlugin.this );
280: JMenuItem item = sqlPaneAPI.addToSQLEntryAreaMenu(cba);
281: resources.configureMenuItem(cba, item);
282: JComponent comp = sqlPaneAPI.getSQLEntryPanel()
283: .getTextComponent();
284: comp.registerKeyboardAction(cba, resources
285: .getKeyStroke(cba),
286: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
287: sqlInternalFrame.addToToolsPopUp("bookmarkselect", cba);
288: }
289:
290: public void objectTreeInternalFrameOpened(
291: ObjectTreeInternalFrame objectTreeInternalFrame,
292: ISession sess) {
293: }
294: };
295: return ret;
296: }
297:
298: private void addBookmarkAction(ISession session) {
299: ActionCollection coll = getApplication().getActionCollection();
300: session.addSeparatorToToolbar();
301: session.addToToolbar(coll.get(AddBookmarkAction.class));
302: session.addToToolbar(coll.get(EditBookmarksAction.class));
303: session.getSessionInternalFrame().addToToolsPopUp(
304: "bookmarkadd", coll.get(AddBookmarkAction.class));
305: session.getSessionInternalFrame().addToToolsPopUp(
306: "bookmarkedit", coll.get(EditBookmarksAction.class));
307:
308: ISQLPanelAPI sqlPaneAPI = session.getSessionInternalFrame()
309: .getSQLPanelAPI();
310: CompleteBookmarkAction cba = new CompleteBookmarkAction(session
311: .getApplication(), resources, sqlPaneAPI
312: .getSQLEntryPanel(), SQLBookmarkPlugin.this );
313: JMenuItem item = sqlPaneAPI.addToSQLEntryAreaMenu(cba);
314: resources.configureMenuItem(cba, item);
315: JComponent comp = sqlPaneAPI.getSQLEntryPanel()
316: .getTextComponent();
317: comp.registerKeyboardAction(cba, resources.getKeyStroke(cba),
318: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
319: session.getSessionInternalFrame().addToToolsPopUp(
320: "bookmarkselect", cba);
321: }
322:
323: /**
324: * Rebuild the Sessions->Bookmarks menu
325: */
326: protected void rebuildMenu() {
327: ActionCollection coll = getApplication().getActionCollection();
328:
329: menu.removeAll();
330: resources.addToMenu(coll.get(AddBookmarkAction.class), menu);
331: menu.add(new JSeparator());
332:
333: for (Iterator<Bookmark> i = bookmarkManager.iterator(); i
334: .hasNext();) {
335: Object o = i.next();
336: Bookmark bookmark = (Bookmark) o;
337:
338: addBookmarkItem(bookmark);
339: }
340:
341: String defaultMarksInPopup = getBookmarkProperties()
342: .getProperty(
343: SQLBookmarkPlugin.BOOKMARK_PROP_DEFAULT_MARKS_IN_POPUP,
344: "" + false);
345:
346: if (Boolean.valueOf(defaultMarksInPopup).booleanValue()) {
347: Bookmark[] defaultBookmarks = DefaultBookmarksFactory
348: .getDefaultBookmarks();
349:
350: for (int i = 0; i < defaultBookmarks.length; i++) {
351: addBookmarkItem(defaultBookmarks[i]);
352: }
353: }
354:
355: }
356:
357: /**
358: * Create the initial Sessions->Bookmark menu
359: */
360: private void createMenu() {
361: IApplication app = getApplication();
362:
363: menu = resources.createMenu(IMenuResourceKeys.BOOKMARKS);
364:
365: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU, menu);
366: }
367:
368: /**
369: * Add new bookmark to Sessions->Bookmark menu
370: *
371: * @param bookmark the bookmark to add.
372: */
373: protected void addBookmarkItem(Bookmark bookmark) {
374: IApplication app = getApplication();
375: ActionCollection coll = app.getActionCollection();
376:
377: SquirrelAction action = (SquirrelAction) coll
378: .get(RunBookmarkAction.class);
379:
380: JMenuItem item = new JMenuItem(action);
381: item.setText(bookmark.getName());
382:
383: menu.add(item);
384: }
385:
386: /**
387: * Create and return a preferences object.
388: *
389: * @return The global preferences object.
390: */
391: public IGlobalPreferencesPanel[] getGlobalPreferencePanels() {
392: return new IGlobalPreferencesPanel[] { new SQLBookmarkPreferencesController(
393: this ) };
394: }
395:
396: public void addSQLPanelAPIListeningForBookmarks(ISQLPanelAPI sqlApi) {
397: if (false == _sqlPanelAPIsListeningForBookmarks
398: .contains(sqlApi)) {
399: _sqlPanelAPIsListeningForBookmarks.add(sqlApi);
400: }
401: }
402:
403: public void removeSQLPanelAPIListeningForBookmarks(
404: ISQLPanelAPI sqlApi) {
405: _sqlPanelAPIsListeningForBookmarks.remove(sqlApi);
406: }
407:
408: public void removeALLSQLPanelsAPIListeningForBookmarks() {
409: _sqlPanelAPIsListeningForBookmarks = new ArrayList<ISQLPanelAPI>();
410: }
411:
412: public ISQLPanelAPI[] getSQLPanelAPIsListeningForBookmarks() {
413: return _sqlPanelAPIsListeningForBookmarks
414: .toArray(new ISQLPanelAPI[_sqlPanelAPIsListeningForBookmarks
415: .size()]);
416: }
417:
418: Properties getBookmarkProperties() {
419: FileInputStream fis = null;
420: try {
421: if (null == _boomarkProps) {
422: File usf = getPluginUserSettingsFolder();
423: File boomarkPropsFile = new File(usf,
424: BOOKMARKS_PROPS_FILE);
425:
426: if (false == boomarkPropsFile.exists()) {
427: _boomarkProps = new Properties();
428: } else {
429: fis = new FileInputStream(boomarkPropsFile);
430: _boomarkProps = new Properties();
431: _boomarkProps.load(fis);
432: }
433: }
434: return _boomarkProps;
435: } catch (IOException e) {
436: throw new RuntimeException(e);
437: } finally {
438: if (fis != null) {
439: try {
440: fis.close();
441: } catch (IOException ex) {
442: s_log.error("Unable to close output stream: "
443: + ex.getMessage(), ex);
444: }
445: }
446: }
447: }
448:
449: void saveBookmarkProperties() {
450: FileOutputStream fos = null;
451: try {
452: if (null == _boomarkProps) {
453: return;
454: }
455:
456: File usf = getPluginUserSettingsFolder();
457: File boomarkPropsFile = new File(usf, BOOKMARKS_PROPS_FILE);
458: fos = new FileOutputStream(boomarkPropsFile);
459: _boomarkProps.store(fos, "Bookmark properties");
460: } catch (IOException e) {
461: throw new RuntimeException(e);
462: } finally {
463: if (fos != null) {
464: try {
465: fos.close();
466: } catch (IOException ex) {
467: s_log.error("Unable to close output stream: "
468: + ex.getMessage(), ex);
469: }
470: }
471: }
472: }
473:
474: }
|