001: package net.sourceforge.squirrel_sql.plugins.editextras;
002:
003: /*
004: * Copyright (C) 2003 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: import javax.swing.Action;
021: import javax.swing.JMenu;
022: import javax.swing.JMenuItem;
023:
024: import net.sourceforge.squirrel_sql.client.IApplication;
025: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
026: import net.sourceforge.squirrel_sql.client.gui.session.BaseSessionInternalFrame;
027: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
028: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
029: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
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.session.ISQLPanelAPI;
035: import net.sourceforge.squirrel_sql.client.session.ISession;
036: import net.sourceforge.squirrel_sql.client.session.event.ISQLPanelListener;
037: import net.sourceforge.squirrel_sql.client.session.event.SQLPanelAdapter;
038: import net.sourceforge.squirrel_sql.client.session.event.SQLPanelEvent;
039: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
040: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
041: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
042:
043: /**
044: * The plugin class.
045: *
046: * @author Gerd Wagner
047: */
048: public class EditExtrasPlugin extends DefaultSessionPlugin {
049: /** Logger for this class. */
050: private final static ILogger s_log = LoggerController
051: .createLogger(EditExtrasPlugin.class);
052:
053: private interface IMenuResourceKeys {
054: String MENU = "editextras";
055: }
056:
057: /** Name of file to store user prefs in. */
058: static final String USER_PREFS_FILE_NAME = "prefs.xml";
059:
060: /** Resources for this plugin. */
061: private Resources _resources;
062:
063: /** Listener to the SQL panel. */
064: private ISQLPanelListener _lis = new SQLPanelListener();
065:
066: /**
067: * Return the internal name of this plugin.
068: *
069: * @return the internal name of this plugin.
070: */
071: public String getInternalName() {
072: return "editextras";
073: }
074:
075: /**
076: * Return the descriptive name of this plugin.
077: *
078: * @return the descriptive name of this plugin.
079: */
080: public String getDescriptiveName() {
081: return "SQL Entry Area Enhancements";
082: }
083:
084: /**
085: * Returns the current version of this plugin.
086: *
087: * @return the current version of this plugin.
088: */
089: public String getVersion() {
090: return "1.0.1";
091: }
092:
093: /**
094: * Returns the authors name.
095: *
096: * @return the authors name.
097: */
098: public String getAuthor() {
099: return "Gerd Wagner";
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: public String getChangeLogFileName() {
111: return "changes.txt";
112: }
113:
114: /**
115: * Returns the name of the Help file for the plugin. This should
116: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
117: * directory.
118: *
119: * @return the Help file name or <TT>null</TT> if plugin doesn't have
120: * a help file.
121: */
122: public String getHelpFileName() {
123: return "readme.html";
124: }
125:
126: /**
127: * Returns the name of the Licence file for the plugin. This should
128: * be a text or HTML file residing in the <TT>getPluginAppSettingsFolder</TT>
129: * directory.
130: *
131: * @return the Licence file name or <TT>null</TT> if plugin doesn't have
132: * a licence file.
133: */
134: public String getLicenceFileName() {
135: return "licence.txt";
136: }
137:
138: /**
139: * Called on application startup after application started.
140: */
141: public void initialize() throws PluginException {
142: super .initialize();
143:
144: final IApplication app = getApplication();
145:
146: // Load resources.
147: _resources = new Resources(this );
148:
149: createMenu();
150: }
151:
152: public boolean allowsSessionStartedInBackground() {
153: return true;
154: }
155:
156: /**
157: * Session has been started.
158: *
159: * @param session Session that has started.
160: */
161: public PluginSessionCallback sessionStarted(ISession session) {
162: ISQLPanelAPI sqlPanelAPI = FrameWorkAcessor.getSQLPanelAPI(
163: session, this );
164: initEditExtras(sqlPanelAPI);
165:
166: PluginSessionCallback ret = new PluginSessionCallback() {
167: public void sqlInternalFrameOpened(
168: final SQLInternalFrame sqlInternalFrame,
169: final ISession sess) {
170: initEditExtras(sqlInternalFrame.getSQLPanelAPI());
171: }
172:
173: public void objectTreeInternalFrameOpened(
174: ObjectTreeInternalFrame objectTreeInternalFrame,
175: ISession sess) {
176: }
177: };
178:
179: return ret;
180: }
181:
182: private void initEditExtras(final ISQLPanelAPI sqlPanelAPI) {
183: GUIUtils.processOnSwingEventThread(new Runnable() {
184: public void run() {
185: sqlPanelAPI.addSQLPanelListener(_lis);
186: createSQLEntryAreaPopMenuItems(sqlPanelAPI);
187:
188: ActionCollection actions = getApplication()
189: .getActionCollection();
190: sqlPanelAPI.addToToolsPopUp("quote", actions
191: .get(InQuotesAction.class));
192: sqlPanelAPI.addToToolsPopUp("unquote", actions
193: .get(RemoveQuotesAction.class));
194: sqlPanelAPI.addToToolsPopUp("quotesb", actions
195: .get(ConvertToStringBufferAction.class));
196: sqlPanelAPI.addToToolsPopUp("format", actions
197: .get(FormatSQLAction.class));
198: sqlPanelAPI.addToToolsPopUp("date", actions
199: .get(EscapeDateAction.class));
200: sqlPanelAPI.addToToolsPopUp("sqlcut", actions
201: .get(CutSqlAction.class));
202: sqlPanelAPI.addToToolsPopUp("sqlcopy", actions
203: .get(CopySqlAction.class));
204: }
205: });
206: }
207:
208: /**
209: * Called when a session shutdown.
210: *
211: * @param session The session that is ending.
212: */
213: public void sessionEnding(ISession session) {
214: BaseSessionInternalFrame[] frames = session.getApplication()
215: .getWindowManager().getAllFramesOfSession(
216: session.getIdentifier());
217:
218: for (int i = 0; i < frames.length; i++) {
219: if (frames[i] instanceof SQLInternalFrame) {
220: ((SQLInternalFrame) frames[i]).getSQLPanelAPI()
221: .removeSQLPanelListener(_lis);
222: }
223:
224: if (frames[i] instanceof SessionInternalFrame) {
225: ((SessionInternalFrame) frames[i]).getSQLPanelAPI()
226: .removeSQLPanelListener(_lis);
227: }
228: }
229:
230: super .sessionEnding(session);
231: }
232:
233: /**
234: * Retrieve plugins resources.
235: *
236: * @return Plugins resources.
237: */
238: public PluginResources getResources() {
239: return _resources;
240: }
241:
242: private void createMenu() {
243: IApplication app = getApplication();
244: ActionCollection coll = app.getActionCollection();
245:
246: JMenu menu = _resources.createMenu(IMenuResourceKeys.MENU);
247: app.addToMenu(IApplication.IMenuIDs.SESSION_MENU, menu);
248:
249: Action act = new InQuotesAction(app, this );
250: coll.add(act);
251: _resources.addToMenu(act, menu);
252:
253: act = new RemoveQuotesAction(app, this );
254: coll.add(act);
255: _resources.addToMenu(act, menu);
256:
257: act = new RemoveNewLinesAction(app, this );
258: coll.add(act);
259: _resources.addToMenu(act, menu);
260:
261: act = new ConvertToStringBufferAction(app, this );
262: coll.add(act);
263: _resources.addToMenu(act, menu);
264:
265: act = new FormatSQLAction(app, this );
266: coll.add(act);
267: _resources.addToMenu(act, menu);
268:
269: act = new EscapeDateAction(getApplication(), _resources);
270: coll.add(act);
271: _resources.addToMenu(act, menu);
272:
273: act = new CutSqlAction(getApplication(), _resources);
274: coll.add(act);
275: _resources.addToMenu(act, menu);
276:
277: act = new CopySqlAction(getApplication(), _resources);
278: coll.add(act);
279: _resources.addToMenu(act, menu);
280:
281: }
282:
283: private void createSQLEntryAreaPopMenuItems(ISQLPanelAPI api) {
284: JMenuItem mnu;
285:
286: ActionCollection actions = getApplication()
287: .getActionCollection();
288: api.addToSQLEntryAreaMenu(actions.get(InQuotesAction.class));
289: api
290: .addToSQLEntryAreaMenu(actions
291: .get(RemoveQuotesAction.class));
292: api.addToSQLEntryAreaMenu(actions
293: .get(ConvertToStringBufferAction.class));
294:
295: // To make the shortcut visible in the popup
296: mnu = api.addToSQLEntryAreaMenu(actions
297: .get(FormatSQLAction.class));
298: _resources.configureMenuItem(
299: actions.get(FormatSQLAction.class), mnu);
300:
301: mnu = api.addToSQLEntryAreaMenu(actions
302: .get(RemoveNewLinesAction.class));
303: _resources.configureMenuItem(actions
304: .get(RemoveNewLinesAction.class), mnu);
305:
306: api.addToSQLEntryAreaMenu(actions.get(EscapeDateAction.class));
307:
308: mnu = api
309: .addToSQLEntryAreaMenu(actions.get(CutSqlAction.class));
310: _resources.configureMenuItem(actions.get(CutSqlAction.class),
311: mnu);
312:
313: mnu = api.addToSQLEntryAreaMenu(actions
314: .get(CopySqlAction.class));
315: _resources.configureMenuItem(actions.get(CopySqlAction.class),
316: mnu);
317:
318: }
319:
320: private class SQLPanelListener extends SQLPanelAdapter {
321: public void sqlEntryAreaReplaced(SQLPanelEvent evt) {
322: createSQLEntryAreaPopMenuItems(evt.getSQLPanel());
323: }
324: }
325:
326: public Object getExternalService() {
327: return new EditExtrasExternalServiceImpl();
328: }
329: }
|