001: package net.sourceforge.squirrel_sql.client.action;
002:
003: /*
004: * Copyright (C) 2001-2006 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * Modifications Copyright (C) 2003-2004 Jason Height
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: import java.awt.event.KeyEvent;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import javax.swing.Action;
029: import javax.swing.JInternalFrame;
030: import javax.swing.KeyStroke;
031:
032: import net.sourceforge.squirrel_sql.client.IApplication;
033: import net.sourceforge.squirrel_sql.client.gui.session.BaseSessionInternalFrame;
034: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
035: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
036: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
037: import net.sourceforge.squirrel_sql.client.mainframe.action.AboutAction;
038: import net.sourceforge.squirrel_sql.client.mainframe.action.CascadeAction;
039: import net.sourceforge.squirrel_sql.client.mainframe.action.CloseAllSessionsAction;
040: import net.sourceforge.squirrel_sql.client.mainframe.action.DisplayPluginSummaryAction;
041: import net.sourceforge.squirrel_sql.client.mainframe.action.DumpApplicationAction;
042: import net.sourceforge.squirrel_sql.client.mainframe.action.ExitAction;
043: import net.sourceforge.squirrel_sql.client.mainframe.action.GlobalPreferencesAction;
044: import net.sourceforge.squirrel_sql.client.mainframe.action.InstallDefaultDriversAction;
045: import net.sourceforge.squirrel_sql.client.mainframe.action.MaximizeAction;
046: import net.sourceforge.squirrel_sql.client.mainframe.action.NewSessionPropertiesAction;
047: import net.sourceforge.squirrel_sql.client.mainframe.action.SavePreferencesAction;
048: import net.sourceforge.squirrel_sql.client.mainframe.action.ShowLoadedDriversOnlyAction;
049: import net.sourceforge.squirrel_sql.client.mainframe.action.TileAction;
050: import net.sourceforge.squirrel_sql.client.mainframe.action.TileHorizontalAction;
051: import net.sourceforge.squirrel_sql.client.mainframe.action.TileVerticalAction;
052: import net.sourceforge.squirrel_sql.client.mainframe.action.ViewHelpAction;
053: import net.sourceforge.squirrel_sql.client.mainframe.action.ViewLogsAction;
054: import net.sourceforge.squirrel_sql.client.session.ISession;
055: import net.sourceforge.squirrel_sql.client.session.action.*;
056: import net.sourceforge.squirrel_sql.fw.util.StringManager;
057: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
058: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
059: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
060:
061: /**
062: * This class represents a collection of <TT>Action</CODE> objects for the
063: * application.
064: *
065: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
066: */
067: public class ActionCollection {
068: /** Logger for this class. */
069: private static ILogger s_log;
070:
071: /** Application API. */
072: private IApplication _app;
073:
074: /** Collection of all Actions keyed by class name. */
075: private final Map<String, Action> _actionColl = new HashMap<String, Action>();
076:
077: /** Internationalized strings for this class. */
078: private static final StringManager s_stringMgr = StringManagerFactory
079: .getStringManager(ActionCollection.class);
080:
081: /**
082: * Ctor. Disable all actions that are not valid when the
083: * application is first initialised.
084: *
085: * @param app Application API.
086: *
087: * @throws IllegalArgumentException
088: * Thrown if <TT>null</TT> <TT>IApplication</TT> passed.
089: */
090: public ActionCollection(IApplication app) {
091: super ();
092: if (app == null) {
093: throw new IllegalArgumentException("IApplication == null");
094: }
095: if (s_log == null) {
096: s_log = LoggerController.createLogger(getClass());
097: }
098: _app = app;
099: preloadActions();
100: enableInternalFrameOptions(false);
101: }
102:
103: /**
104: * Add an <TT>Action</TT> to this collection. Normally <TT>get</TT> will
105: * do this "on demand" but this function can be used when
106: * there is no default ctor for the <TT>Action</TT>.
107: *
108: * @param action <TT>Action</TT> to be added.
109: *
110: * @throws IllegalArgumentException
111: * If a <TT>null</TT> <TT>Action</TT> passed.
112: */
113: public void add(Action action) {
114: if (action == null) {
115: throw new IllegalArgumentException("Action == null");
116: }
117: _actionColl.put(action.getClass().getName(), action);
118: }
119:
120: /**
121: * Returns the instance of the passed <TT>Action</TT> class that is stored
122: * in this collection.
123: *
124: * @param actionClass The <TT>Class</TT> of the <TT>Action</TT>
125: * required. Because the instance is created
126: * using <TT>newInstance()</TT> this <TT>Class</TT>
127: * must have a default ctor.
128: *
129: * @throws IllegalArgumentException Thrown if a null action class passed.
130: */
131: public synchronized Action get(Class<? extends Action> actionClass) {
132: if (actionClass == null) {
133: throw new IllegalArgumentException(
134: "null Action Class passed.");
135: }
136:
137: return get(actionClass.getName());
138: }
139:
140: /**
141: * Returns the instance of the passed <TT>Action</TT> class name that is
142: * stored in this collection.
143: *
144: * @param actionClass The <TT>Class</TT> of the <TT>Action</TT>
145: * required. Because the instance is created
146: * using <TT>newInstance()</TT> this <TT>Class</TT>
147: * must have a default ctor.
148: *
149: * @throws IllegalArgumentException Thrown if a null action class passed.
150: */
151: public synchronized Action get(String actionClassName) {
152: if (actionClassName == null) {
153: throw new IllegalArgumentException(
154: "null Action Class Name passed.");
155: }
156:
157: Action action = _actionColl.get(actionClassName);
158: if (action == null) {
159: // i18n[ActionCollection.actionNotFound=Action {0} not found in ActionCollection.]
160: String errMsg = s_stringMgr.getString(
161: "ActionCollection.actionNotFound", actionClassName);
162: s_log.error(errMsg);
163: action = createAction(actionClassName);
164: }
165: return action;
166: }
167:
168: /**
169: * Emable/Disable the instance of the passed <TT>Action</TT> class that is
170: * stored in this collection. If one isn't in this collection then an instance
171: * of <TT>actionClass</TT> will be created and stored.
172: *
173: * @param actionClass The <TT>Class</TT> of the <TT>Action</TT>
174: * to be enabled/disabled. Because the instance
175: * is created using <TT>newInstance()</TT> this
176: * <TT>Class</TT> must have a default ctor.
177: * @param enable If <TT>true</TT> then enable else disable
178: * the action.
179: *
180: * @throws IllegalArgumentException Thrown if a null action class passed.
181: */
182: @SuppressWarnings("unchecked")
183: public void enableAction(Class actionClass, boolean enable)
184: throws IllegalArgumentException {
185: if (actionClass == null) {
186: throw new IllegalArgumentException(
187: "null Action Class passed.");
188: }
189:
190: final Action action = get(actionClass);
191: if (action != null) {
192: action.setEnabled(enable);
193: }
194: }
195:
196: /**
197: * This function should be called whenever an internal frame is
198: * opened or closed. It enables/disabled actions that are only
199: * applicable to an internal frame.
200: *
201: * @param nbrInternalFramesOpen The count of the internal frames open.
202: */
203: public void internalFrameOpenedOrClosed(int nbrInternalFramesOpen) {
204: enableInternalFrameOptions(nbrInternalFramesOpen > 0);
205: }
206:
207: /**
208: * This function should be called whenever an internal frame is
209: * deactivated.
210: *
211: * JASON: Should this be in Sessionmanager or SessionWindowmanager?
212: *
213: * @param frame The <TT>JInternalFrame</TT> deactivated.
214: */
215: public void deactivationChanged(JInternalFrame frame) {
216: final boolean isSQLFrame = (frame instanceof SQLInternalFrame);
217: final boolean isTreeFrame = (frame instanceof ObjectTreeInternalFrame);
218: final boolean isSessionInternalFrame = (frame instanceof SessionInternalFrame);
219:
220: for (Iterator<Action> it = actions(); it.hasNext();) {
221: final Action act = it.next();
222:
223: if (act instanceof ISessionAction) {
224: ((ISessionAction) act).setSession(null);
225: }
226:
227: if (isSQLFrame && (act instanceof ISQLPanelAction)) {
228: ((ISQLPanelAction) act).setSQLPanel(null);
229: }
230: if (isTreeFrame && (act instanceof IObjectTreeAction)) {
231: ((IObjectTreeAction) act).setObjectTree(null);
232: }
233: if ((isSessionInternalFrame)
234: && (act instanceof ISQLPanelAction)) {
235: ((ISQLPanelAction) act).setSQLPanel(null);
236: }
237: if ((isSessionInternalFrame)
238: && (act instanceof IObjectTreeAction)) {
239: ((IObjectTreeAction) act).setObjectTree(null);
240: }
241: }
242: }
243:
244: /**
245: * This function should be called whenever an internal frame is
246: * activated.
247: *
248: * JASON: Should this be in Sessionmanager or SessionWindowmanager?
249: *
250: * @param frame The <TT>JInternalFrame</TT> activated.
251: */
252: public synchronized void activationChanged(JInternalFrame frame) {
253: final boolean isSQLFrame = (frame instanceof SQLInternalFrame);
254: final boolean isTreeFrame = (frame instanceof ObjectTreeInternalFrame);
255: final boolean isSessionInternalFrame = (frame instanceof SessionInternalFrame);
256:
257: ISession session = null;
258: if (frame instanceof BaseSessionInternalFrame) {
259: session = ((BaseSessionInternalFrame) frame).getSession();
260: }
261:
262: for (Iterator<Action> it = actions(); it.hasNext();) {
263: final Action act = it.next();
264: if (act instanceof ISessionAction) {
265: ((ISessionAction) act).setSession(session);
266: }
267: if (isSQLFrame && (act instanceof ISQLPanelAction)) {
268: ((ISQLPanelAction) act)
269: .setSQLPanel(((SQLInternalFrame) frame)
270: .getSQLPanel().getSQLPanelAPI());
271: }
272: if (isTreeFrame && (act instanceof IObjectTreeAction)) {
273: ((IObjectTreeAction) act)
274: .setObjectTree(((ObjectTreeInternalFrame) frame)
275: .getObjectTreePanel());
276: }
277:
278: if ((isSessionInternalFrame)
279: && (act instanceof ISQLPanelAction)) {
280: SessionInternalFrame sif = (SessionInternalFrame) frame;
281: if (sif.getSessionPanel().isSQLTabSelected()) {
282: ((ISQLPanelAction) act).setSQLPanel(sif
283: .getSessionPanel().getSQLPaneAPI());
284: } else {
285: ((ISQLPanelAction) act).setSQLPanel(null);
286: }
287: }
288: if ((isSessionInternalFrame)
289: && (act instanceof IObjectTreeAction)) {
290: SessionInternalFrame sif = (SessionInternalFrame) frame;
291: if (sif.getSessionPanel().isObjectTreeTabSelected()) {
292: ((IObjectTreeAction) act)
293: .setObjectTree(((SessionInternalFrame) frame)
294: .getSessionPanel()
295: .getObjectTreePanel());
296: } else {
297: ((IObjectTreeAction) act).setObjectTree(null);
298: }
299: }
300: }
301: }
302:
303: /**
304: * Apply these action keys to the actions currently loaded.
305: *
306: * actionkeys Action keys to load.
307: *
308: * @throws IllegalArgumentException
309: * Thrown if <TT>null</TT> <TT>ActionKeys[]</TT> passed.
310: */
311: public synchronized void loadActionKeys(ActionKeys[] actionKeys) {
312: if (actionKeys == null) {
313: throw new IllegalArgumentException(
314: "null ActionKeys[] passed");
315: }
316:
317: for (int i = 0; i < actionKeys.length; ++i) {
318: final ActionKeys ak = actionKeys[i];
319: final Action action = get(ak.getActionClassName());
320: if (action != null) {
321: final String accel = ak.getAccelerator();
322: if (accel != null && accel.length() > 0) {
323: action.putValue(Action.ACCELERATOR_KEY, KeyStroke
324: .getKeyStroke(accel));
325: }
326:
327: final int mnemonic = ak.getMnemonic();
328: if (mnemonic != KeyEvent.VK_UNDEFINED) {
329: action.putValue(Action.MNEMONIC_KEY, Integer
330: .valueOf(mnemonic));
331: }
332: }
333: }
334: }
335:
336: /**
337: * Return an <TT>Iterator</TT> over this collection.
338: */
339: public Iterator<Action> actions() {
340: return _actionColl.values().iterator();
341: }
342:
343: /**
344: * Specify the current session for actions.
345: *
346: * @param session The current session. Can be <tt>null</tt>.
347: */
348: public synchronized void setCurrentSession(ISession session) {
349: for (Iterator<Action> it = actions(); it.hasNext();) {
350: final Action act = it.next();
351: if (act instanceof ISessionAction) {
352: ((ISessionAction) act).setSession(session);
353: }
354: }
355: }
356:
357: /**
358: * Create a new instance of <TT>actionCassName</TT> and store in this
359: * collection.
360: *
361: * @param actionClass The name of the <TT>Class</TT> of the <TT>Action</TT>
362: * required. Because the instance is created
363: * using <TT>newInstance()</TT> this <TT>Class</TT>
364: * must have a default ctor.
365: */
366: private Action createAction(String actionClassName) {
367: Action action = null;
368: try {
369: // i18n[ActionCollection.createActionInfo=Attempting to load action class - {0}]
370: String msg = s_stringMgr.getString(
371: "ActionCollection.createActionInfo",
372: actionClassName);
373: s_log.info(msg);
374: action = (Action) Class.forName(actionClassName)
375: .newInstance();
376: _actionColl.put(actionClassName, action);
377: } catch (Exception ex) {
378: // i18n[ActionCollection.createActionError=Error occured creating Action: {0}]
379: String msg = s_stringMgr.getString(
380: "ActionCollection.createActionError",
381: actionClassName);
382: s_log.error(msg, ex);
383: }
384: return action;
385: }
386:
387: /**
388: * Enable/disable actions that are valid only if an internal frame
389: * is open.
390: */
391: private void enableInternalFrameOptions(boolean enable) {
392: enableAction(CascadeAction.class, enable);
393: enableAction(MaximizeAction.class, enable);
394: enableAction(TileAction.class, enable);
395: enableAction(TileHorizontalAction.class, enable);
396: enableAction(TileVerticalAction.class, enable);
397: enableAction(CloseAllSessionsAction.class, enable);
398: }
399:
400: /**
401: * Load actions.
402: */
403: private void preloadActions() {
404: add(new AboutAction(_app));
405: add(new CascadeAction(_app));
406: add(new ToolsPopupAction(_app));
407: add(new CloseAllSessionsAction(_app));
408: add(new CloseAllSQLResultTabsAction(_app));
409: add(new CloseAllSQLResultTabsButCurrentAction(_app));
410: add(new CloseCurrentSQLResultTabAction(_app));
411: add(new ToggleCurrentSQLResultTabStickyAction(_app));
412: add(new CloseAllSQLResultWindowsAction(_app));
413: add(new ViewObjectAtCursorInObjectTreeAction(_app));
414: add(new CloseSessionAction(_app));
415: add(new CommitAction(_app));
416: add(new CopyQualifiedObjectNameAction(_app));
417: add(new CopySimpleObjectNameAction(_app));
418: add(new DisplayPluginSummaryAction(_app));
419: //add(new DropSelectedTablesAction(_app));
420: add(new DeleteSelectedTablesAction(_app));
421: add(new DumpApplicationAction(_app));
422: add(new SavePreferencesAction(_app));
423: add(new DumpSessionAction(_app));
424: add(new ExecuteSqlAction(_app));
425: add(new ExitAction(_app));
426: add(new FileNewAction(_app));
427: add(new FileOpenAction(_app));
428: add(new FileAppendAction(_app));
429: add(new FileSaveAction(_app));
430: add(new FileSaveAsAction(_app));
431: add(new FilePrintAction(_app));
432: add(new FileCloseAction(_app));
433: add(new GlobalPreferencesAction(_app));
434: add(new GotoNextResultsTabAction(_app));
435: add(new GotoPreviousResultsTabAction(_app));
436: add(new InstallDefaultDriversAction(_app));
437: add(new MaximizeAction(_app));
438: add(new NewObjectTreeAction(_app));
439: add(new NewSQLWorksheetAction(_app));
440: add(new NewSessionPropertiesAction(_app));
441: add(new NextSessionAction(_app));
442: add(new PreviousSessionAction(_app));
443: add(new ReconnectAction(_app));
444: add(new RefreshSchemaInfoAction(_app));
445: add(new RefreshObjectTreeItemAction(_app));
446: add(new RollbackAction(_app));
447: add(new SessionPropertiesAction(_app));
448: add(new FilterObjectsAction(_app));
449: add(new SetDefaultCatalogAction(_app));
450: add(new ShowLoadedDriversOnlyAction(_app));
451: add(new ShowNativeSQLAction(_app));
452: add(new SQLFilterAction(_app));
453: add(new EditWhereColsAction(_app));
454: add(new TileAction(_app));
455: add(new TileHorizontalAction(_app));
456: add(new TileVerticalAction(_app));
457: add(new ToggleAutoCommitAction(_app));
458: add(new ViewHelpAction(_app));
459: add(new ViewLogsAction(_app));
460: add(new PreviousSqlAction(_app));
461: add(new NextSqlAction(_app));
462: add(new SelectSqlAction(_app));
463: add(new OpenSqlHistoryAction(_app));
464: }
465:
466: }
|