001: package net.sourceforge.squirrel_sql.client.gui.mainframe;
002:
003: /*
004: * Copyright (C) 2001-2004 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 java.awt.Component;
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024:
025: import javax.swing.Action;
026: import javax.swing.JCheckBoxMenuItem;
027: import javax.swing.JDesktopPane;
028: import javax.swing.JMenu;
029: import javax.swing.JMenuBar;
030:
031: import net.sourceforge.squirrel_sql.fw.gui.action.IHasJDesktopPane;
032: import net.sourceforge.squirrel_sql.fw.gui.IToggleAction;
033: import net.sourceforge.squirrel_sql.fw.util.Resources;
034: import net.sourceforge.squirrel_sql.fw.util.SystemProperties;
035: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
036: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
037:
038: import net.sourceforge.squirrel_sql.client.IApplication;
039: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
040: import net.sourceforge.squirrel_sql.client.session.action.CloseAllSQLResultTabsButCurrentAction;
041: import net.sourceforge.squirrel_sql.client.mainframe.action.*;
042: import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
043: import net.sourceforge.squirrel_sql.client.plugin.PluginManager;
044: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
045: import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
046: import net.sourceforge.squirrel_sql.client.session.action.*;
047:
048: /**
049: * Menu bar for <CODE>MainFrame</CODE>.
050: *
051: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
052: */
053: @SuppressWarnings("serial")
054: final class MainFrameMenuBar extends JMenuBar {
055:
056: public interface IMenuIDs {
057: int PLUGINS_MENU = 1;
058: int SESSION_MENU = 2;
059: }
060:
061: /** Logger for this class. */
062: private static final ILogger s_log = LoggerController
063: .createLogger(MainFrameMenuBar.class);
064:
065: private final IApplication _app;
066: // private final JMenu _editMenu;
067: private final JMenu _pluginsMenu;
068: private final JMenu _sessionMenu;
069: private final JMenu _windowsMenu;
070:
071: private JMenu _aliasesMenu;
072: private JMenu _driversMenu;
073:
074: private final ActionCollection _actions;
075:
076: private JCheckBoxMenuItem _showLoadedDriversOnlyItem;
077:
078: /** Listener to changes to application properties. */
079: private SquirrelPropertiesListener _propsLis;
080:
081: private final boolean _osxPluginLoaded;
082:
083: /**
084: * Ctor.
085: */
086: MainFrameMenuBar(IApplication app, JDesktopPane desktopPane,
087: ActionCollection actions) {
088: super ();
089: if (app == null) {
090: throw new IllegalArgumentException(
091: "Null IApplication passed");
092: }
093:
094: if (desktopPane == null) {
095: throw new IllegalArgumentException(
096: "Null JDesktopPane passed");
097: }
098:
099: if (actions == null) {
100: throw new IllegalArgumentException(
101: "Null ActionCollection passed");
102: }
103:
104: Resources rsrc = app.getResources();
105:
106: if (rsrc == null) {
107: throw new IllegalStateException(
108: "No Resources object in IApplication");
109: }
110:
111: _actions = actions;
112:
113: _app = app;
114: _osxPluginLoaded = isOsxPluginLoaded();
115:
116: add(createOsxFileMenu(rsrc));
117: add(_driversMenu = createDriversMenu(rsrc));
118: add(_aliasesMenu = createAliasesMenu(rsrc));
119: add(_pluginsMenu = createPluginsMenu(rsrc));
120: add(_sessionMenu = createSessionMenu(rsrc));
121: add(_windowsMenu = createWindowsMenu(rsrc, desktopPane));
122: add(createHelpMenu(rsrc));
123: }
124:
125: /**
126: * Component has been added to its parent so setup required listeners.
127: */
128: public void addNotify() {
129: super .addNotify();
130: propertiesChanged(null);
131: if (_propsLis == null) {
132: _propsLis = new SquirrelPropertiesListener();
133: _app.getSquirrelPreferences().addPropertyChangeListener(
134: _propsLis);
135: }
136: }
137:
138: /**
139: * Component has been removed from its parent so remove required listeners.
140: */
141: public void removeNotify() {
142: super .removeNotify();
143: if (_propsLis != null) {
144: _app.getSquirrelPreferences().removePropertyChangeListener(
145: _propsLis);
146: _propsLis = null;
147: }
148: }
149:
150: JMenu getWindowsMenu() {
151: return _windowsMenu;
152: }
153:
154: JMenu getSessionMenu() {
155: return _sessionMenu;
156: }
157:
158: void addToMenu(int menuId, JMenu menu) {
159: if (menu == null) {
160: throw new IllegalArgumentException("Null JMenu passed");
161: }
162:
163: switch (menuId) {
164: case IMenuIDs.PLUGINS_MENU: {
165: _pluginsMenu.add(menu);
166: break;
167: }
168:
169: case IMenuIDs.SESSION_MENU: {
170: _sessionMenu.add(menu);
171: break;
172: }
173:
174: default: {
175: throw new IllegalArgumentException(
176: "Invalid menuId passed: " + menuId);
177: }
178: }
179: }
180:
181: void addToMenu(int menuId, Action action) {
182: if (action == null) {
183: throw new IllegalArgumentException("Null Action passed");
184: }
185:
186: switch (menuId) {
187: case IMenuIDs.PLUGINS_MENU: {
188: _pluginsMenu.add(action);
189: break;
190: }
191:
192: case IMenuIDs.SESSION_MENU: {
193: _sessionMenu.add(action);
194: break;
195: }
196:
197: default: {
198: throw new IllegalArgumentException(
199: "Invalid menuId passed: " + menuId);
200: }
201: }
202: }
203:
204: /**
205: * Add a component to the end of the menu.
206: *
207: * @param menuId Defines the menu to add the component to. @see IMenuIDs
208: * @param comp Component to add to menu.
209: *
210: * @throws IllegalArgumentException if null <TT>Component</TT> passed or
211: * an invalid <TT>menuId</TT> passed.
212: */
213: void addToMenu(int menuId, Component comp) {
214: if (comp == null) {
215: throw new IllegalArgumentException("Component == null");
216: }
217:
218: switch (menuId) {
219: case IMenuIDs.PLUGINS_MENU: {
220: _pluginsMenu.add(comp);
221: break;
222: }
223:
224: case IMenuIDs.SESSION_MENU: {
225: _sessionMenu.add(comp);
226: break;
227: }
228:
229: default: {
230: throw new IllegalArgumentException(
231: "Invalid menuId passed: " + menuId);
232: }
233:
234: }
235: }
236:
237: private JMenu createOsxFileMenu(Resources rsrc) {
238: JMenu menu = rsrc
239: .createMenu(SquirrelResources.IMenuResourceKeys.OSX_FILE);
240: if (!_osxPluginLoaded) {
241: addToMenu(rsrc, GlobalPreferencesAction.class, menu);
242: }
243: addToMenu(rsrc, NewSessionPropertiesAction.class, menu);
244: menu.addSeparator();
245: addToMenu(rsrc, DumpApplicationAction.class, menu);
246: addToMenu(rsrc, SavePreferencesAction.class, menu);
247: menu.addSeparator();
248: if (!_osxPluginLoaded) {
249: addToMenu(rsrc, ExitAction.class, menu);
250: }
251:
252: return menu;
253: }
254:
255: public void setEnabledAliasesMenu(boolean b) {
256: _aliasesMenu.setEnabled(b);
257: }
258:
259: public void setEnabledDriversMenu(boolean b) {
260: _driversMenu.setEnabled(b);
261: }
262:
263: private JMenu createSessionMenu(Resources rsrc) {
264: JMenu menu = rsrc
265: .createMenu(SquirrelResources.IMenuResourceKeys.SESSION);
266: addToMenu(rsrc, SessionPropertiesAction.class, menu);
267: addToMenu(rsrc, DumpSessionAction.class, menu);
268: menu.addSeparator();
269: addToMenu(rsrc, ToolsPopupAction.class, menu);
270: addToMenu(rsrc, RefreshSchemaInfoAction.class, menu);
271: addToMenu(rsrc, ExecuteSqlAction.class, menu);
272: menu.add(createTransactionMenu(rsrc));
273: addToMenu(rsrc, SQLFilterAction.class, menu);
274: menu.addSeparator();
275: addToMenu(rsrc, ViewObjectAtCursorInObjectTreeAction.class,
276: menu);
277: menu.addSeparator();
278: menu.add(createFileMenu(rsrc));
279: menu.addSeparator();
280: addToMenu(rsrc, GotoPreviousResultsTabAction.class, menu);
281: addToMenu(rsrc, GotoNextResultsTabAction.class, menu);
282: addToMenu(rsrc, ToggleCurrentSQLResultTabStickyAction.class,
283: menu);
284: menu.addSeparator();
285: addToMenu(rsrc, ShowNativeSQLAction.class, menu);
286: menu.addSeparator();
287: addToMenu(rsrc, ReconnectAction.class, menu);
288: addToMenu(rsrc, CloseSessionAction.class, menu);
289: menu.add(createSQLResultsCloseMenu(rsrc));
290: menu.addSeparator();
291: addToMenu(rsrc, PreviousSessionAction.class, menu);
292: addToMenu(rsrc, NextSessionAction.class, menu);
293: menu.addSeparator();
294: addToMenu(rsrc, PreviousSqlAction.class, menu);
295: addToMenu(rsrc, NextSqlAction.class, menu);
296: addToMenu(rsrc, SelectSqlAction.class, menu);
297: menu.addSeparator();
298: addToMenu(rsrc, EditWhereColsAction.class, menu);
299: menu.addSeparator();
300: addToMenu(rsrc, NewSQLWorksheetAction.class, menu);
301: addToMenu(rsrc, NewObjectTreeAction.class, menu);
302: menu.addSeparator();
303:
304: menu.setEnabled(false);
305: return menu;
306: }
307:
308: private JMenu createPluginsMenu(Resources rsrc) {
309: JMenu menu = rsrc
310: .createMenu(SquirrelResources.IMenuResourceKeys.PLUGINS);
311: addToMenu(rsrc, DisplayPluginSummaryAction.class, menu);
312: menu.addSeparator();
313: return menu;
314: }
315:
316: private JMenu createAliasesMenu(Resources rsrc) {
317: JMenu menu = rsrc
318: .createMenu(SquirrelResources.IMenuResourceKeys.ALIASES);
319: addToMenu(rsrc, ConnectToAliasAction.class, menu);
320: menu.addSeparator();
321: addToMenu(rsrc, CreateAliasAction.class, menu);
322: menu.addSeparator();
323: addToMenu(rsrc, ModifyAliasAction.class, menu);
324: addToMenu(rsrc, DeleteAliasAction.class, menu);
325: addToMenu(rsrc, CopyAliasAction.class, menu);
326: addToMenu(rsrc, AliasPropertiesAction.class, menu);
327: menu.addSeparator();
328: addToMenu(rsrc, SortAliasesAction.class, menu);
329: return menu;
330: }
331:
332: private JMenu createDriversMenu(Resources rsrc) {
333: JMenu menu = rsrc
334: .createMenu(SquirrelResources.IMenuResourceKeys.DRIVERS);
335: addToMenu(rsrc, CreateDriverAction.class, menu);
336: menu.addSeparator();
337: addToMenu(rsrc, ModifyDriverAction.class, menu);
338: addToMenu(rsrc, DeleteDriverAction.class, menu);
339: addToMenu(rsrc, CopyDriverAction.class, menu);
340: addToMenu(rsrc, ShowDriverWebsiteAction.class, menu);
341: menu.addSeparator();
342: addToMenu(rsrc, InstallDefaultDriversAction.class, menu);
343: menu.addSeparator();
344: _showLoadedDriversOnlyItem = addToMenuAsCheckBoxMenuItem(rsrc,
345: ShowLoadedDriversOnlyAction.class, menu);
346: return menu;
347: }
348:
349: private JMenu createWindowsMenu(Resources rsrc,
350: JDesktopPane desktopPane) {
351: JMenu menu = rsrc
352: .createMenu(SquirrelResources.IMenuResourceKeys.WINDOWS);
353: addToMenu(rsrc, ViewAliasesAction.class, menu);
354: addToMenu(rsrc, ViewDriversAction.class, menu);
355: addToMenu(rsrc, ViewLogsAction.class, menu);
356: menu.addSeparator();
357: addDesktopPaneActionToMenu(rsrc, TileAction.class, menu,
358: desktopPane);
359: addDesktopPaneActionToMenu(rsrc, TileHorizontalAction.class,
360: menu, desktopPane);
361: addDesktopPaneActionToMenu(rsrc, TileVerticalAction.class,
362: menu, desktopPane);
363: addDesktopPaneActionToMenu(rsrc, CascadeAction.class, menu,
364: desktopPane);
365: addDesktopPaneActionToMenu(rsrc, MaximizeAction.class, menu,
366: desktopPane);
367: menu.addSeparator();
368: addToMenu(rsrc, CloseAllSessionsAction.class, menu);
369: menu.addSeparator();
370: return menu;
371: }
372:
373: private JMenu createHelpMenu(Resources rsrc) {
374: JMenu menu = rsrc
375: .createMenu(SquirrelResources.IMenuResourceKeys.HELP);
376: addToMenu(rsrc, ViewHelpAction.class, menu);
377:
378: menu.addSeparator();
379: if (!_osxPluginLoaded) {
380: addToMenu(rsrc, AboutAction.class, menu);
381: }
382:
383: return menu;
384: }
385:
386: private JMenu createSQLResultsCloseMenu(Resources rsrc) {
387: JMenu menu = rsrc
388: .createMenu(SquirrelResources.IMenuResourceKeys.CLOSE_ALL_SQL_RESULTS);
389: addToMenu(rsrc, CloseAllSQLResultTabsAction.class, menu);
390: addToMenu(rsrc, CloseCurrentSQLResultTabAction.class, menu);
391: addToMenu(rsrc, CloseAllSQLResultTabsButCurrentAction.class,
392: menu);
393: addToMenu(rsrc, CloseAllSQLResultWindowsAction.class, menu);
394: return menu;
395: }
396:
397: private Component createFileMenu(Resources rsrc) {
398: JMenu menu = rsrc
399: .createMenu(SquirrelResources.IMenuResourceKeys.FILE);
400: addToMenu(rsrc, FileSaveAction.class, menu);
401: addToMenu(rsrc, FileSaveAsAction.class, menu);
402: addToMenu(rsrc, FileOpenAction.class, menu);
403: addToMenu(rsrc, FileNewAction.class, menu);
404: addToMenu(rsrc, FileAppendAction.class, menu);
405: addToMenu(rsrc, FilePrintAction.class, menu);
406: addToMenu(rsrc, FileCloseAction.class, menu);
407: return menu;
408: }
409:
410: private Component createTransactionMenu(Resources rsrc) {
411: JMenu menu = rsrc
412: .createMenu(SquirrelResources.IMenuResourceKeys.TRANSACTION);
413: addToMenuAsCheckBoxMenuItem(rsrc, ToggleAutoCommitAction.class,
414: menu);
415: addToMenu(rsrc, CommitAction.class, menu);
416: addToMenu(rsrc, RollbackAction.class, menu);
417: return menu;
418: }
419:
420: @SuppressWarnings("unchecked")
421: private Action addDesktopPaneActionToMenu(Resources rsrc,
422: Class actionClass, JMenu menu, JDesktopPane desktopPane) {
423: Action act = addToMenu(rsrc, actionClass, menu);
424: if (act != null) {
425: if (act instanceof IHasJDesktopPane) {
426: ((IHasJDesktopPane) act).setJDesktopPane(desktopPane);
427: } else {
428: s_log
429: .error("Tryimg to add non IHasJDesktopPane ("
430: + actionClass.getName()
431: + ") in MainFrameMenuBar.addDesktopPaneActionToMenu");
432: }
433: }
434: return act;
435: }
436:
437: @SuppressWarnings("unchecked")
438: private Action addToMenu(Resources rsrc, Class actionClass,
439: JMenu menu) {
440: Action act = _actions.get(actionClass);
441: if (act != null) {
442: rsrc.addToMenu(act, menu);
443: } else {
444: s_log.error("Could not retrieve instance of "
445: + actionClass.getName()
446: + ") in MainFrameMenuBar.addToMenu");
447: }
448:
449: return act;
450: }
451:
452: @SuppressWarnings("unchecked")
453: private JCheckBoxMenuItem addToMenuAsCheckBoxMenuItem(
454: Resources rsrc, Class actionClass, JMenu menu) {
455: Action act = _actions.get(actionClass);
456: if (act != null) {
457: JCheckBoxMenuItem mnu = rsrc.addToMenuAsCheckBoxMenuItem(
458: act, menu);
459: if (act instanceof IToggleAction) {
460: ((IToggleAction) act).getToggleComponentHolder()
461: .addToggleableComponent(mnu);
462: }
463: return mnu;
464: }
465: s_log.error("Could not retrieve instance of "
466: + actionClass.getName()
467: + ") in MainFrameMenuBar.addToMenu");
468: return null;
469: }
470:
471: /**
472: * Application properties have changed so update this object.
473: *
474: * @param propName Name of property that has changed or <TT>null</TT>
475: * if multiple properties have changed.
476: */
477: private void propertiesChanged(String propName) {
478: if (propName == null
479: || propName
480: .equals(SquirrelPreferences.IPropertyNames.SHOW_LOADED_DRIVERS_ONLY)) {
481: boolean show = _app.getSquirrelPreferences()
482: .getShowLoadedDriversOnly();
483: _showLoadedDriversOnlyItem.setSelected(show);
484: }
485: }
486:
487: // TODO: This is a nasty quick hack. Needs an API to do this.
488: private boolean isOsxPluginLoaded() {
489: if (SystemProperties.isRunningOnOSX()) {
490: final PluginManager mgr = _app.getPluginManager();
491: PluginInfo[] ar = mgr.getPluginInformation();
492: for (int i = 0; i < ar.length; ++i) {
493: if (ar[i].getInternalName().equals("macosx")) {
494: return ar[i].isLoaded();
495: }
496: }
497: }
498: return false;
499: }
500:
501: /**
502: * Listener for changes to Squirrel Properties.
503: */
504: private class SquirrelPropertiesListener implements
505: PropertyChangeListener {
506: public void propertyChange(PropertyChangeEvent evt) {
507: final String propName = evt != null ? evt.getPropertyName()
508: : null;
509: propertiesChanged(propName);
510: }
511: }
512: }
|