0001: package net.sourceforge.squirrel_sql.client.gui;
0002:
0003: /*
0004: * Copyright (C) 2003-2006 Colin Bell
0005: * colbell@users.sourceforge.net
0006: *
0007: * Modifications Copyright (C) 2003-2004 Jason Height
0008: *
0009: * This library is free software; you can redistribute it and/or
0010: * modify it under the terms of the GNU Lesser General Public
0011: * License as published by the Free Software Foundation; either
0012: * version 2.1 of the License, or (at your option) any later version.
0013: *
0014: * This library is distributed in the hope that it will be useful,
0015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0017: * Lesser General Public License for more details.
0018: *
0019: * You should have received a copy of the GNU Lesser General Public
0020: * License along with this library; if not, write to the Free Software
0021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0022: */
0023: import java.awt.Window;
0024: import java.beans.PropertyVetoException;
0025:
0026: import javax.swing.Action;
0027: import javax.swing.JDesktopPane;
0028: import javax.swing.JInternalFrame;
0029: import javax.swing.JMenu;
0030: import javax.swing.JMenuItem;
0031: import javax.swing.RepaintManager;
0032: import javax.swing.SwingUtilities;
0033: import javax.swing.event.EventListenerList;
0034: import javax.swing.event.InternalFrameEvent;
0035: import javax.swing.event.InternalFrameListener;
0036:
0037: import net.sourceforge.squirrel_sql.client.IApplication;
0038: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
0039: import net.sourceforge.squirrel_sql.client.gui.db.*;
0040: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
0041: import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrameWindowState;
0042: import net.sourceforge.squirrel_sql.client.gui.session.BaseSessionInternalFrame;
0043: import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
0044: import net.sourceforge.squirrel_sql.client.gui.session.SQLInternalFrame;
0045: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
0046: import net.sourceforge.squirrel_sql.client.gui.util.ThreadCheckingRepaintManager;
0047: import net.sourceforge.squirrel_sql.client.mainframe.action.*;
0048: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
0049: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
0050: import net.sourceforge.squirrel_sql.client.session.ISession;
0051: import net.sourceforge.squirrel_sql.client.session.SessionManager;
0052: import net.sourceforge.squirrel_sql.client.session.event.SessionAdapter;
0053: import net.sourceforge.squirrel_sql.client.session.event.SessionEvent;
0054: import net.sourceforge.squirrel_sql.client.session.properties.EditWhereColsSheet;
0055: import net.sourceforge.squirrel_sql.client.session.properties.SessionPropertiesSheet;
0056: import net.sourceforge.squirrel_sql.client.session.sqlfilter.SQLFilterSheet;
0057: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
0058: import net.sourceforge.squirrel_sql.fw.gui.WindowState;
0059: import net.sourceforge.squirrel_sql.fw.gui.action.SelectInternalFrameAction;
0060: import net.sourceforge.squirrel_sql.fw.gui.action.SelectInternalFrameCommand;
0061: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
0062: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
0063: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
0064: import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
0065: import net.sourceforge.squirrel_sql.fw.util.StringManager;
0066: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
0067: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
0068: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
0069:
0070: /**
0071: * This class manages the windows for the application.
0072: *
0073: * TODO: Correct these notes
0074: * <p>When a session closes the window manager will ensure that
0075: * all of the windows for that sesion are closed.
0076: * <p>Similarily when a window is closed the windows manager will ensure that
0077: * references to the window are removed for the session.
0078: *
0079: * JASON: Prior to this patch there was some code movement from this class to
0080: * Sessionmanager. The idea being that Sessionmanager was the controller.
0081: * Do we still want to do this? Remember in the future there will probably be
0082: * an SDI as well as MDI version of the windows.
0083: *
0084: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
0085: * @author <A HREF="mailto:jmheight@users.sourceforge.net">Jason Height</A>
0086: */
0087: public class WindowManager {
0088: /** Logger for this class. */
0089: private static final ILogger s_log = LoggerController
0090: .createLogger(WindowManager.class);
0091:
0092: /** Internationalized strings for this class. */
0093: private static final StringManager s_stringMgr = StringManagerFactory
0094: .getStringManager(WindowManager.class);
0095:
0096: /**
0097: * Key to client property stored in internal frame that udentifies the
0098: * internal frame.
0099: */
0100: private static final String MENU = WindowManager.class.getName()
0101: + ".menu";
0102:
0103: /** Application API. */
0104: private final IApplication _app;
0105:
0106: /** Window manager for driver windows. */
0107: private DriverWindowManager _driverWinMgr;
0108:
0109: /** Window manager for aliases windows. */
0110: private AliasWindowManager _aliasWinMgr;
0111:
0112: /** Applications main frame. */
0113: private MainFrame _mainFrame;
0114:
0115: /** Window containing list of database aliases. */
0116: private AliasesListInternalFrame _aliasesListWindow;
0117:
0118: /** Window containing list of JDBC driver definitions. */
0119: private DriversListInternalFrame _driversListWindow;
0120:
0121: /** Window Factory for alias maintenace windows. */
0122: // private final AliasWindowFactory _aliasWinFactory;
0123: /**
0124: * Map of windows(s) that are currently open for a session, keyed by
0125: * session ID.
0126: */
0127: private final SessionWindowsHolder _sessionWindows = new SessionWindowsHolder();
0128:
0129: private final SessionWindowListener _windowListener = new SessionWindowListener();
0130:
0131: // private int _lastSessionIdx = 1;
0132:
0133: // JASON: Mow that multiple object trees exist storing the edit
0134: // where by objectInfo within session won't work. It needs to be objectinfo
0135: // within something else.
0136: // private final Map _editWhereColsSheets = new HashMap();
0137:
0138: private final SessionListener _sessionListener = new SessionListener();
0139:
0140: private EventListenerList _listenerList = new EventListenerList();
0141:
0142: private boolean _sessionClosing = false;
0143:
0144: /**
0145: * Ctor.
0146: *
0147: * @param app Application API.
0148: *
0149: * @throws IllegalArgumentException
0150: * Thrown if <TT>null</TT> <TT>IApplication</TT> passed.
0151: */
0152: public WindowManager(IApplication app) {
0153: super ();
0154: if (app == null) {
0155: throw new IllegalArgumentException("IApplication == null");
0156: }
0157:
0158: if (s_log.isDebugEnabled()) {
0159: RepaintManager
0160: .setCurrentManager(new ThreadCheckingRepaintManager());
0161: }
0162:
0163: _app = app;
0164:
0165: _aliasWinMgr = new AliasWindowManager(_app);
0166: _driverWinMgr = new DriverWindowManager(_app);
0167:
0168: GUIUtils.processOnSwingEventThread(new Runnable() {
0169: public void run() {
0170: initialize();
0171: }
0172: }, true);
0173: }
0174:
0175: /**
0176: * Retrieve applications main frame.
0177: *
0178: * @return Applications main frame.
0179: */
0180: public MainFrame getMainFrame() {
0181: return _mainFrame;
0182: }
0183:
0184: public AliasesListInternalFrame getAliasesListInternalFrame() {
0185: return _aliasesListWindow;
0186: }
0187:
0188: public DriversListInternalFrame getDriversListInternalFrame() {
0189: return _driversListWindow;
0190: }
0191:
0192: public WindowState getAliasesWindowState() {
0193: return new WindowState(_aliasesListWindow);
0194: }
0195:
0196: public WindowState getDriversWindowState() {
0197: return new WindowState(_driversListWindow);
0198: }
0199:
0200: /**
0201: * Get a maintenance sheet for the passed alias. If a maintenance sheet already
0202: * exists it will be brought to the front. If one doesn't exist it will be
0203: * created.
0204: *
0205: * @param alias The alias that user has requested to modify.
0206: *
0207: * @throws IllegalArgumentException
0208: * Thrown if a <TT>null</TT> <TT>ISQLAlias</TT> passed.
0209: */
0210: public void showModifyAliasInternalFrame(final ISQLAlias alias) {
0211: if (alias == null) {
0212: throw new IllegalArgumentException("ISQLAlias == null");
0213: }
0214:
0215: _aliasWinMgr.showModifyAliasInternalFrame(alias);
0216: }
0217:
0218: /**
0219: * Create and show a new maintenance window to allow the user to create a
0220: * new alias.
0221: */
0222: public void showNewAliasInternalFrame() {
0223: _aliasWinMgr.showNewAliasInternalFrame();
0224: }
0225:
0226: /**
0227: * Create and show a new maintenance sheet that will allow the user to create a
0228: * new alias that is a copy of the passed one.
0229: *
0230: * @return The new maintenance sheet.
0231: *
0232: * @throws IllegalArgumentException
0233: * Thrown if a <TT>null</TT> <TT>ISQLAlias</TT> passed.
0234: */
0235: public void showCopyAliasInternalFrame(final SQLAlias alias) {
0236: if (alias == null) {
0237: throw new IllegalArgumentException("ISQLAlias == null");
0238: }
0239:
0240: _aliasWinMgr.showCopyAliasInternalFrame(alias);
0241: }
0242:
0243: /**
0244: * Get a maintenance sheet for the passed driver. If a maintenance sheet
0245: * already exists it will be brought to the front. If one doesn't exist
0246: * it will be created.
0247: *
0248: * @param driver The driver that user has requested to modify.
0249: *
0250: * @throws IllegalArgumentException
0251: * Thrown if a <TT>null</TT> <TT>ISQLDriver</TT> passed.
0252: */
0253: public void showModifyDriverInternalFrame(final ISQLDriver driver) {
0254: if (driver == null) {
0255: throw new IllegalArgumentException("ISQLDriver == null");
0256: }
0257:
0258: _driverWinMgr.showModifyDriverInternalFrame(driver);
0259: }
0260:
0261: /**
0262: * Create and show a new maintenance window to allow the user to create a
0263: * new driver.
0264: */
0265: public void showNewDriverInternalFrame() {
0266: _driverWinMgr.showNewDriverInternalFrame();
0267: }
0268:
0269: /**
0270: * Create and show a new maintenance sheet that will allow the user to
0271: * create a new driver that is a copy of the passed one.
0272: *
0273: * @return The new maintenance sheet.
0274: *
0275: * @throws IllegalArgumentException
0276: * Thrown if a <TT>null</TT> <TT>ISQLDriver</TT> passed.
0277: */
0278: public void showCopyDriverInternalFrame(final ISQLDriver driver) {
0279: if (driver == null) {
0280: throw new IllegalArgumentException("ISQLDriver == null");
0281: }
0282:
0283: _driverWinMgr.showCopyDriverInternalFrame(driver);
0284: }
0285:
0286: /**
0287: * Registers a sheet that is attached to a session. This sheet will
0288: * be automatically closed when the session is closing.
0289: * <p/><b>There is no need to call this method manually.</b> Any
0290: * classes that properly extend BaseSessionInternalFrame will be registered.
0291: */
0292: public synchronized void registerSessionSheet(
0293: BaseSessionInternalFrame sheet) {
0294: //i18n[WindowManager.registerSessionSheet=Registering {0} in WindowManager]
0295: String dbg = s_stringMgr.getString(
0296: "WindowManager.registerSessionSheet", sheet.getClass()
0297: .getName());
0298: s_log.debug(dbg);
0299: final IIdentifier sessionIdentifier = sheet.getSession()
0300: .getIdentifier();
0301:
0302: // Store ptr to newly open window in list of windows per session.
0303: final int idx = _sessionWindows.addFrame(sessionIdentifier,
0304: sheet);
0305:
0306: // For all windows (other than the first one opened) for a session
0307: // add a number on the end of the title to differentiate them in
0308: // menus etc.
0309: if (idx > 1) {
0310: sheet.setTitle(sheet.getTitle() + " (" + idx + ")");
0311: }
0312:
0313: sheet.addInternalFrameListener(_windowListener);
0314: }
0315:
0316: /**
0317: * Adds a listener to the sheets attached to this session <p/>When new
0318: * sheets are constructed, they are automatically added to the session via
0319: * the registerSessionSheet method. <p/>All other listener events fire due
0320: * to interaction with the frame. <p/>The
0321: * InternalFrameListener.internalFrameOpened is a good location to tailor
0322: * the session sheets (ie internal frame) from a plugin. Examples can be
0323: * found in the oracle plugin of how to modify how a session sheet.
0324: */
0325: public void addSessionSheetListener(InternalFrameListener listener) {
0326: if (listener == null) {
0327: throw new IllegalArgumentException(
0328: "InternalFrameListener == null");
0329: }
0330:
0331: _listenerList.add(InternalFrameListener.class, listener);
0332: }
0333:
0334: /**
0335: * Create a new internal frame for the passed session.
0336: *
0337: * @param session Session we are creating internal frame for.
0338: *
0339: * @throws IllegalArgumentException
0340: * Thrown if ISession is passed as null.
0341: */
0342: public synchronized SessionInternalFrame createInternalFrame(
0343: ISession session) {
0344: if (session == null) {
0345: throw new IllegalArgumentException("ISession == null");
0346: }
0347:
0348: final SessionInternalFrame sif = new SessionInternalFrame(
0349: session);
0350:
0351: session.setSessionInternalFrame(sif);
0352: _app.getPluginManager().sessionStarted(session);
0353: _app.getMainFrame().addInternalFrame(sif, true, null);
0354:
0355: // If we don't invokeLater here no Short-Cut-Key is sent
0356: // to the internal frame
0357: // seen under java version "1.4.1_01" and Linux
0358: SwingUtilities.invokeLater(new Runnable() {
0359: public void run() {
0360: sif.setVisible(true);
0361: sif.getObjectTreeAPI().selectRoot();
0362: }
0363: });
0364:
0365: return sif;
0366: }
0367:
0368: // /**
0369: // * A callback method to allow the session we are creating to tell us it has
0370: // * finished it's initialization. It's important that the plugins are only
0371: // * notified that a session has been started, after the session window and
0372: // * it's associated toolbar have been created, and populated with the core
0373: // * toolbar menu-items.
0374: // *
0375: // * @param session the ISession whose SessionPanel has finished it's
0376: // * initialization.
0377: // */
0378: // public void sessionInitComplete(ISession session) {
0379: // _app.getPluginManager().sessionStarted(session);
0380: // }
0381:
0382: /**
0383: * Creates a new SQL View internal frame for the passed session.
0384: *
0385: * @param session Session we are creating internal frame for.
0386: *
0387: * @throws IllegalArgumentException
0388: * Thrown if ISession is passed as null.
0389: */
0390: public synchronized SQLInternalFrame createSQLInternalFrame(
0391: ISession session) {
0392: if (session == null) {
0393: throw new IllegalArgumentException("ISession == null");
0394: }
0395: final SQLInternalFrame sif = new SQLInternalFrame(session);
0396: getMainFrame().addInternalFrame(sif, true, null);
0397:
0398: // If we don't invokeLater here no Short-Cut-Key is sent
0399: // to the internal frame
0400: // seen under java version "1.4.1_01" and Linux
0401: SwingUtilities.invokeLater(new Runnable() {
0402: public void run() {
0403: sif.setVisible(true);
0404: sif.requestFocus();
0405: }
0406: });
0407:
0408: return sif;
0409: }
0410:
0411: /**
0412: * Creates a new Object Tree internal frame for the passed session.
0413: *
0414: * @param session Session we are creating internal frame for.
0415: *
0416: * @throws IllegalArgumentException
0417: * Thrown if ISession is passed as null.
0418: */
0419: public synchronized ObjectTreeInternalFrame createObjectTreeInternalFrame(
0420: ISession session) {
0421: if (session == null) {
0422: throw new IllegalArgumentException("ISession == null");
0423: }
0424: final ObjectTreeInternalFrame oif = new ObjectTreeInternalFrame(
0425: session);
0426: getMainFrame().addInternalFrame(oif, true, null);
0427:
0428: // If we don't invokeLater here no Short-Cut-Key is sent
0429: // to the internal frame
0430: // seen under java version "1.4.1_01" and Linux
0431: SwingUtilities.invokeLater(new Runnable() {
0432: public void run() {
0433: oif.setVisible(true);
0434: oif.getObjectTreeAPI().selectRoot();
0435: }
0436: });
0437:
0438: return oif;
0439: }
0440:
0441: /**
0442: * Get a properties dialog for the passed session. If one already
0443: * exists it will be brought to the front. If one doesn't exist it will be
0444: * created.
0445: *
0446: * @param session The session that user has request property dialog for.
0447: * @param tabNameToSelect The name (title) of the Tab to select. First Tab will be selected
0448: * if tabNameToSelect is null or doesnt match any tab.
0449: *
0450: * @param tabNameToSelect
0451: * @throws IllegalArgumentException
0452: * Thrown if a <TT>null</TT> <TT>ISession</TT> passed.
0453: */
0454: public synchronized void showSessionPropertiesDialog(
0455: ISession session, int tabIndexToSelect) {
0456: if (session == null) {
0457: throw new IllegalArgumentException("ISession == null");
0458: }
0459:
0460: SessionPropertiesSheet propsSheet = getSessionPropertiesDialog(session);
0461: if (propsSheet == null) {
0462: propsSheet = new SessionPropertiesSheet(session);
0463: _app.getMainFrame()
0464: .addInternalFrame(propsSheet, true, null);
0465: positionSheet(propsSheet);
0466: } else {
0467: moveToFront(propsSheet);
0468: }
0469:
0470: propsSheet.selectTabIndex(tabIndexToSelect);
0471: }
0472:
0473: /**
0474: * Get an SQL Filter sheet for the passed data. If one already exists it
0475: * will be brought to the front. If one doesn't exist it will be created.
0476: *
0477: * @param objectTree
0478: * @param objectInfo An instance of a class containing information about
0479: * the database metadata.
0480: *
0481: * @return The filter dialog.
0482: *
0483: * @throws IllegalArgumentException
0484: * Thrown if <tt>null</tt> <tt>ContentsTab</tt>,
0485: * <tt>IObjectTreeAPI</tt>, or <tt>IDatabaseObjectInfo</tt> passed.
0486: */
0487: public synchronized SQLFilterSheet showSQLFilterDialog(
0488: IObjectTreeAPI objectTree, IDatabaseObjectInfo objectInfo) {
0489: if (objectTree == null) {
0490: throw new IllegalArgumentException("IObjectTree == null");
0491: }
0492: if (objectInfo == null) {
0493: throw new IllegalArgumentException(
0494: "IDatabaseObjectInfo == null");
0495: }
0496:
0497: SQLFilterSheet sqlFilterSheet = getSQLFilterSheet(objectTree,
0498: objectInfo);
0499: if (sqlFilterSheet == null) {
0500: sqlFilterSheet = new SQLFilterSheet(objectTree, objectInfo);
0501: _app.getMainFrame().addInternalFrame(sqlFilterSheet, true,
0502: null);
0503: positionSheet(sqlFilterSheet);
0504: } else {
0505: moveToFront(sqlFilterSheet);
0506: }
0507:
0508: return sqlFilterSheet;
0509: }
0510:
0511: /**
0512: * Get a EditWhereCols sheet for the passed session. If one already exists it
0513: * will be brought to the front. If one doesn't exist it will be created.
0514: *
0515: * @param tree Object tree containing the table.
0516: * @param objectInfo An instance of a class containing information about
0517: * the database metadata.
0518: *
0519: * @return The maintenance sheet for the passed session.
0520: */
0521: public synchronized EditWhereColsSheet showEditWhereColsDialog(
0522: IObjectTreeAPI tree, IDatabaseObjectInfo objectInfo) {
0523: if (tree == null) {
0524: throw new IllegalArgumentException("IObjectTreeAPI == null");
0525: }
0526: if (objectInfo == null) {
0527: throw new IllegalArgumentException(
0528: "IDatabaseObjectInfo == null");
0529: }
0530:
0531: ISession session = tree.getSession();
0532: EditWhereColsSheet editWhereColsSheet = getEditWhereColsSheet(
0533: session, objectInfo);
0534: if (editWhereColsSheet == null) {
0535: // JASON: Needs to be done same as the others
0536: editWhereColsSheet = new EditWhereColsSheet(session,
0537: objectInfo);
0538: // Map map = getAllEditWhereColsSheets(tree);
0539: // map.put(objectInfo.getQualifiedName(), editWhereColsSheet);
0540: _app.getMainFrame().addInternalFrame(editWhereColsSheet,
0541: true, null);
0542: // editWhereColsSheet.addInternalFrameListener(_editWhereColsDialogListener);
0543: positionSheet(editWhereColsSheet);
0544: } else {
0545: moveToFront(editWhereColsSheet);
0546: }
0547:
0548: return editWhereColsSheet;
0549: }
0550:
0551: public void moveToFront(final Window win) {
0552: if (win != null) {
0553: GUIUtils.processOnSwingEventThread(new Runnable() {
0554: public void run() {
0555: win.toFront();
0556: win.setVisible(true);
0557: }
0558: });
0559: }
0560: }
0561:
0562: public void moveToFront(final JInternalFrame fr) {
0563: if (fr != null) {
0564: GUIUtils.processOnSwingEventThread(new Runnable() {
0565: public void run() {
0566: fr.moveToFront();
0567: fr.setVisible(true);
0568: try {
0569: fr.setSelected(true);
0570: } catch (PropertyVetoException ex) {
0571: // i18n[WindowManager.error.bringtofront=Error bringing internal frame to the front]
0572: s_log
0573: .error(
0574: s_stringMgr
0575: .getString("WindowManager.error.bringtofront"),
0576: ex);
0577: }
0578: }
0579: });
0580: }
0581: }
0582:
0583: public void activateNextSessionWindow() {
0584: final SessionManager sessMgr = _app.getSessionManager();
0585: final ISession sess = sessMgr.getActiveSession();
0586:
0587: if (sess == null) {
0588: return;
0589: }
0590:
0591: BaseSessionInternalFrame activeSessionWindow = sess
0592: .getActiveSessionWindow();
0593:
0594: if (null == activeSessionWindow) {
0595: throw new IllegalStateException(
0596: "Active Session with no active window ???");
0597: }
0598:
0599: BaseSessionInternalFrame nextSessionWindow = _sessionWindows
0600: .getNextSessionWindow(activeSessionWindow);
0601:
0602: if (false == activeSessionWindow.equals(nextSessionWindow)) {
0603: new SelectInternalFrameCommand(nextSessionWindow).execute();
0604: }
0605: }
0606:
0607: public void activatePreviousSessionWindow() {
0608: final SessionManager sessMgr = _app.getSessionManager();
0609: final ISession sess = sessMgr.getActiveSession();
0610:
0611: if (sess == null) {
0612: return;
0613: }
0614:
0615: BaseSessionInternalFrame activeSessionWindow = sess
0616: .getActiveSessionWindow();
0617:
0618: if (null == activeSessionWindow) {
0619: throw new IllegalStateException(
0620: "Active Session with no active window ???");
0621: }
0622:
0623: BaseSessionInternalFrame previousSessionWindow = _sessionWindows
0624: .getPreviousSessionWindow(activeSessionWindow);
0625:
0626: if (false == activeSessionWindow.equals(previousSessionWindow)) {
0627: new SelectInternalFrameCommand(previousSessionWindow)
0628: .execute();
0629: }
0630: }
0631:
0632: protected void refireSessionSheetOpened(InternalFrameEvent evt) {
0633: // Guaranteed to return a non-null array
0634: Object[] listeners = _listenerList.getListenerList();
0635: // Process the listeners last to first, notifying
0636: // those that are interested in this event
0637: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0638: if (listeners[i] == InternalFrameListener.class) {
0639: ((InternalFrameListener) listeners[i + 1])
0640: .internalFrameOpened(evt);
0641: }
0642: }
0643: }
0644:
0645: protected void refireSessionSheetClosing(InternalFrameEvent evt) {
0646: // Guaranteed to return a non-null array
0647: Object[] listeners = _listenerList.getListenerList();
0648: // Process the listeners last to first, notifying
0649: // those that are interested in this event
0650: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0651: if (listeners[i] == InternalFrameListener.class) {
0652: ((InternalFrameListener) listeners[i + 1])
0653: .internalFrameClosing(evt);
0654: }
0655: }
0656: }
0657:
0658: protected void refireSessionSheetClosed(InternalFrameEvent evt) {
0659: // Guaranteed to return a non-null array
0660: Object[] listeners = _listenerList.getListenerList();
0661: // Process the listeners last to first, notifying
0662: // those that are interested in this event
0663: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0664: if (listeners[i] == InternalFrameListener.class) {
0665: ((InternalFrameListener) listeners[i + 1])
0666: .internalFrameClosed(evt);
0667: }
0668: }
0669: }
0670:
0671: protected void refireSessionSheetIconified(InternalFrameEvent evt) {
0672: // Guaranteed to return a non-null array
0673: Object[] listeners = _listenerList.getListenerList();
0674: // Process the listeners last to first, notifying
0675: // those that are interested in this event
0676: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0677: if (listeners[i] == InternalFrameListener.class) {
0678: ((InternalFrameListener) listeners[i + 1])
0679: .internalFrameIconified(evt);
0680: }
0681: }
0682: }
0683:
0684: protected void refireSessionSheetDeiconified(InternalFrameEvent evt) {
0685: // Guaranteed to return a non-null array
0686: Object[] listeners = _listenerList.getListenerList();
0687: // Process the listeners last to first, notifying
0688: // those that are interested in this event
0689: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0690: if (listeners[i] == InternalFrameListener.class) {
0691: ((InternalFrameListener) listeners[i + 1])
0692: .internalFrameDeiconified(evt);
0693: }
0694: }
0695: }
0696:
0697: protected void refireSessionSheetActivated(InternalFrameEvent evt) {
0698: // Guaranteed to return a non-null array
0699: Object[] listeners = _listenerList.getListenerList();
0700: // Process the listeners last to first, notifying
0701: // those that are interested in this event
0702: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0703: if (listeners[i] == InternalFrameListener.class) {
0704: ((InternalFrameListener) listeners[i + 1])
0705: .internalFrameActivated(evt);
0706: }
0707: }
0708: }
0709:
0710: protected void refireSessionSheetDeactivated(InternalFrameEvent evt) {
0711: // Guaranteed to return a non-null array
0712: Object[] listeners = _listenerList.getListenerList();
0713: // Process the listeners last to first, notifying
0714: // those that are interested in this event
0715: for (int i = listeners.length - 2; i >= 0; i -= 2) {
0716: if (listeners[i] == InternalFrameListener.class) {
0717: ((InternalFrameListener) listeners[i + 1])
0718: .internalFrameDeactivated(evt);
0719: }
0720: }
0721: }
0722:
0723: private SessionPropertiesSheet getSessionPropertiesDialog(
0724: ISession session) {
0725:
0726: BaseSessionInternalFrame[] framesOfSession = _sessionWindows
0727: .getFramesOfSession(session.getIdentifier());
0728:
0729: for (int i = 0; i < framesOfSession.length; i++) {
0730: if (framesOfSession[i] instanceof SessionPropertiesSheet) {
0731: return (SessionPropertiesSheet) framesOfSession[i];
0732: }
0733: }
0734: return null;
0735: }
0736:
0737: private SQLFilterSheet getSQLFilterSheet(IObjectTreeAPI tree,
0738: IDatabaseObjectInfo objectInfo) {
0739: final ISession session = tree.getSession();
0740:
0741: BaseSessionInternalFrame[] framesOfSession = _sessionWindows
0742: .getFramesOfSession(session.getIdentifier());
0743:
0744: for (int i = 0; i < framesOfSession.length; i++) {
0745: if (framesOfSession[i] instanceof SQLFilterSheet) {
0746: final SQLFilterSheet sfs = (SQLFilterSheet) framesOfSession[i];
0747: if (sfs.getObjectTree() == tree
0748: && objectInfo.equals(sfs
0749: .getDatabaseObjectInfo())) {
0750: return sfs;
0751: }
0752: }
0753: }
0754:
0755: return null;
0756: }
0757:
0758: private EditWhereColsSheet getEditWhereColsSheet(ISession session,
0759: IDatabaseObjectInfo objectInfo) {
0760: // final Map map = getAllEditWhereColsSheets(tree);
0761: // return (EditWhereColsSheet)map.get(objectInfo.getQualifiedName());
0762:
0763: BaseSessionInternalFrame[] framesOfSession = _sessionWindows
0764: .getFramesOfSession(session.getIdentifier());
0765:
0766: for (int i = 0; i < framesOfSession.length; i++) {
0767: if (framesOfSession[i] instanceof EditWhereColsSheet) {
0768: final EditWhereColsSheet sfs = (EditWhereColsSheet) framesOfSession[i];
0769: // if (sfs.getObjectTree() == tree &&
0770: // objectInfo.equals(sfs.getDatabaseObjectInfo()))
0771: if (objectInfo.equals(sfs.getDatabaseObjectInfo())) {
0772: return sfs;
0773: }
0774: }
0775: }
0776: return null;
0777: }
0778:
0779: // JASON: FIX THIS
0780: // private Map getAllEditWhereColsSheets(IObjectTreeAPI tree)
0781: // {
0782: // Map map = (Map)_editWhereColsSheets.get(tree.getIdentifier());
0783: // if (map == null)
0784: // {
0785: // map = new HashMap();
0786: // _editWhereColsSheets.put(session.getIdentifier(), map);
0787: // }
0788: // return map;
0789: // }
0790:
0791: private void positionSheet(JInternalFrame jif) {
0792: GUIUtils.centerWithinDesktop(jif);
0793: moveToFront(jif);
0794: }
0795:
0796: private void selectFrontWindow() {
0797: final JDesktopPane desktop = _app.getMainFrame()
0798: .getDesktopPane();
0799: if (desktop != null) {
0800: final JInternalFrame[] jifs = desktop.getAllFrames();
0801: if (jifs != null && jifs.length > 0) {
0802: moveToFront(jifs[0]);
0803: }
0804: }
0805: }
0806:
0807: private void initialize() {
0808: createAliasesListUI();
0809: createDriversListUI();
0810: preLoadActions();
0811: _app.getSessionManager().addSessionListener(_sessionListener);
0812: createMainFrame();
0813: setupFromPreferences();
0814: }
0815:
0816: private void createMainFrame() {
0817: _mainFrame = new MainFrame(_app);
0818: GUIUtils.setMainFrame(_mainFrame);
0819: }
0820:
0821: private void createAliasesListUI() {
0822: final AliasesList al = new AliasesList(_app);
0823:
0824: final ActionCollection actions = _app.getActionCollection();
0825: actions.add(new ModifyAliasAction(_app, al));
0826: actions.add(new DeleteAliasAction(_app, al));
0827: actions.add(new CopyAliasAction(_app, al));
0828: actions.add(new ConnectToAliasAction(_app, al));
0829: actions.add(new CreateAliasAction(_app));
0830: actions.add(new SortAliasesAction(_app, al));
0831: actions.add(new AliasPropertiesAction(_app, al));
0832:
0833: _aliasesListWindow = new AliasesListInternalFrame(_app, al);
0834: }
0835:
0836: private void createDriversListUI() {
0837: final DriversList dl = new DriversList(_app);
0838:
0839: final ActionCollection actions = _app.getActionCollection();
0840: actions.add(new ModifyDriverAction(_app, dl));
0841: actions.add(new DeleteDriverAction(_app, dl));
0842: actions.add(new CopyDriverAction(_app, dl));
0843: actions.add(new CreateDriverAction(_app));
0844: actions.add(new ShowDriverWebsiteAction(_app, dl));
0845:
0846: _driversListWindow = new DriversListInternalFrame(_app, dl);
0847: }
0848:
0849: private void preLoadActions() {
0850: final ActionCollection actions = _app.getActionCollection();
0851: if (actions == null) {
0852: throw new IllegalStateException(
0853: "ActionCollection hasn't been created.");
0854: }
0855:
0856: actions.add(new ViewAliasesAction(_app,
0857: getAliasesListInternalFrame()));
0858: actions.add(new ViewDriversAction(_app,
0859: getDriversListInternalFrame()));
0860:
0861: // IAliasesList al = getAliasesListInternalFrame().getAliasesList();
0862: }
0863:
0864: private void setupFromPreferences() {
0865: final SquirrelPreferences prefs = _app.getSquirrelPreferences();
0866: final MainFrameWindowState ws = prefs.getMainFrameWindowState();
0867:
0868: _mainFrame.addInternalFrame(_driversListWindow, false, null);
0869: WindowState toolWs = ws.getDriversWindowState();
0870: _driversListWindow.setBounds(toolWs.getBounds()
0871: .createRectangle());
0872: _driversListWindow.setVisible(toolWs.isVisible());
0873: try {
0874: _driversListWindow.setSelected(true);
0875: } catch (PropertyVetoException ex) {
0876: // i18n[WindowManager.errorselectingwindow=Error selecting window]
0877: s_log.error(s_stringMgr
0878: .getString("WindowManager.errorselectingwindow"),
0879: ex);
0880: }
0881:
0882: _mainFrame.addInternalFrame(_aliasesListWindow, false, null);
0883: toolWs = ws.getAliasesWindowState();
0884: _aliasesListWindow.setBounds(toolWs.getBounds()
0885: .createRectangle());
0886: if (toolWs.isVisible()) {
0887: _aliasesListWindow.setVisible(true);
0888: try {
0889: _aliasesListWindow.setSelected(true);
0890: } catch (PropertyVetoException ex) {
0891: // i18n[WindowManager.errorselectingwindow=Error selecting window]
0892: s_log
0893: .error(
0894: s_stringMgr
0895: .getString("WindowManager.errorselectingwindow"),
0896: ex);
0897: }
0898: } else {
0899: _aliasesListWindow.setVisible(false);
0900: }
0901: prefs.setMainFrameWindowState(new MainFrameWindowState(this ));
0902: }
0903:
0904: /**
0905: * Retrieve an internal frame for the passed session. Can be <TT>null</TT>
0906: *
0907: * @return an internal frame for the passed session. Can be <TT>null</TT>.
0908: *
0909: * @throws IllegalArgumentException
0910: * Thrown if ISession is passed as null.
0911: */
0912: private JInternalFrame getInternalFrameForSession(ISession session) {
0913: if (session == null) {
0914: throw new IllegalArgumentException("ISession == null");
0915: }
0916:
0917: JInternalFrame firstWindow = null;
0918:
0919: BaseSessionInternalFrame[] framesOfSession = _sessionWindows
0920: .getFramesOfSession(session.getIdentifier());
0921: for (int i = 0; i < framesOfSession.length; i++) {
0922: if (framesOfSession[i] instanceof BaseSessionInternalFrame) {
0923: firstWindow = (BaseSessionInternalFrame) framesOfSession[i];
0924: }
0925: if (framesOfSession[i] instanceof SessionInternalFrame) {
0926: final SessionInternalFrame sif = (SessionInternalFrame) framesOfSession[i];
0927: if (sif.getSession().equals(session)) {
0928: return sif;
0929: }
0930: }
0931: }
0932: return firstWindow;
0933: }
0934:
0935: public BaseSessionInternalFrame[] getAllFramesOfSession(
0936: IIdentifier sessionIdentifier) {
0937: return _sessionWindows.getFramesOfSession(sessionIdentifier);
0938: }
0939:
0940: // JASON: Needs to be done elsewhere
0941: // private synchronized void editWhereColsDialogClosed(EditWhereColsSheet sfs)
0942: // {
0943: // if (sfs != null)
0944: // {
0945: // sfs.removeInternalFrameListener(_editWhereColsDialogListener);
0946: // Map map = getAllEditWhereColsSheets(sfs.getSession());
0947: // String key = sfs.getDatabaseObjectInfo().getQualifiedName();
0948: // if (map.remove(key) == null)
0949: // {
0950: // s_log.error("Unable to find EditWhereColsSheet for " + key);
0951: // }
0952: // }
0953: // }
0954:
0955: // JASON: Do this elsewhere
0956: // private final class EditWhereColsDialogListener extends InternalFrameAdapter
0957: // {
0958: // public void internalFrameClosed(InternalFrameEvent evt)
0959: // {
0960: // EditWhereColsSheet sfs = (EditWhereColsSheet)evt.getInternalFrame();
0961: // WindowManager.this.editWhereColsDialogClosed(sfs);
0962: // }
0963: // }
0964:
0965: private final class SessionWindowListener implements
0966: InternalFrameListener {
0967: public void internalFrameOpened(InternalFrameEvent evt) {
0968: final JInternalFrame jif = evt.getInternalFrame();
0969:
0970: // JASON: Make menu smarter. When second window for the same
0971: // session is added create a hierarchical menu for all windows
0972: // for the session.
0973:
0974: // Add an item to the Windows menu for this window and
0975: // store the menu item back in the internal frame.
0976: final JMenu menu = getMainFrame().getWindowsMenu();
0977: final Action action = new SelectInternalFrameAction(jif);
0978: final JMenuItem menuItem = menu.add(action);
0979: jif.putClientProperty(MENU, menuItem);
0980:
0981: // Enable/Disable actions that require open session frames.
0982: JInternalFrame[] frames = GUIUtils
0983: .getOpenNonToolWindows(getMainFrame()
0984: .getDesktopPane().getAllFrames());
0985: _app.getActionCollection().internalFrameOpenedOrClosed(
0986: frames.length);
0987:
0988: refireSessionSheetOpened(evt);
0989: }
0990:
0991: public void internalFrameClosing(InternalFrameEvent evt) {
0992: refireSessionSheetClosing(evt);
0993: }
0994:
0995: public void internalFrameClosed(InternalFrameEvent evt) {
0996: final JInternalFrame jif = evt.getInternalFrame();
0997:
0998: // Only remove the frame if the entire session is not closing
0999: if (!_sessionClosing) {
1000: // Find the internal Frame in the list of internal frames
1001: // and remove it.
1002: if (jif instanceof BaseSessionInternalFrame) {
1003: final BaseSessionInternalFrame sessionJIF = (BaseSessionInternalFrame) jif;
1004: final IIdentifier sessionID = sessionJIF
1005: .getSession().getIdentifier();
1006: BaseSessionInternalFrame[] sessionSheets = _sessionWindows
1007: .getFramesOfSession(sessionID);
1008:
1009: for (int i = 0; i < sessionSheets.length; i++) {
1010: if (sessionSheets[i] == sessionJIF) {
1011: _sessionWindows
1012: .removeWindow(sessionSheets[i]);
1013: WindowManager.this .selectFrontWindow();
1014: break;
1015: }
1016: }
1017: }
1018: }
1019:
1020: // Remove menu item from Windows menu that relates to this
1021: // internal frame.
1022: final JMenuItem menuItem = (JMenuItem) jif
1023: .getClientProperty(MENU);
1024: if (menuItem != null) {
1025: final JMenu menu = getMainFrame().getWindowsMenu();
1026: if (menu != null) {
1027: menu.remove(menuItem);
1028: }
1029: }
1030:
1031: // Enable/Disable actions that require open session frames.
1032: JInternalFrame[] frames = GUIUtils
1033: .getOpenNonToolWindows(getMainFrame()
1034: .getDesktopPane().getAllFrames());
1035:
1036: _app.getActionCollection().internalFrameOpenedOrClosed(
1037: frames.length);
1038:
1039: refireSessionSheetClosed(evt);
1040: }
1041:
1042: public void internalFrameIconified(InternalFrameEvent e) {
1043: refireSessionSheetIconified(e);
1044: }
1045:
1046: public void internalFrameDeiconified(InternalFrameEvent e) {
1047: refireSessionSheetDeiconified(e);
1048: }
1049:
1050: public void internalFrameActivated(InternalFrameEvent e) {
1051: refireSessionSheetActivated(e);
1052: }
1053:
1054: public void internalFrameDeactivated(InternalFrameEvent e) {
1055: refireSessionSheetDeactivated(e);
1056: }
1057: }
1058:
1059: /**
1060: * Used to update the UI depending on various session events.
1061: */
1062: private final class SessionListener extends SessionAdapter {
1063: /**
1064: * Session has been connected to a database.
1065: */
1066: public void sessionConnected(SessionEvent evt) {
1067: // Add the message handler to the session
1068: evt.getSession()
1069: .setMessageHandler(_app.getMessageHandler());
1070: }
1071:
1072: /**
1073: * A session has been activated.
1074: */
1075: public void sessionActivated(SessionEvent evt) {
1076: final ISession newSession = evt.getSession();
1077:
1078: // Allocate the current session to the actions.
1079: _app.getActionCollection().setCurrentSession(newSession);
1080:
1081: // If the active window isn't for the currently selected session
1082: // then select the main window for the session.
1083: ISession currSession = null;
1084: JInternalFrame sif = getMainFrame().getDesktopPane()
1085: .getSelectedFrame();
1086: if (sif instanceof BaseSessionInternalFrame) {
1087: currSession = ((BaseSessionInternalFrame) sif)
1088: .getSession();
1089: }
1090: if (currSession != newSession) {
1091: sif = getInternalFrameForSession(newSession);
1092: if (sif != null) {
1093: moveToFront(sif);
1094: }
1095: }
1096:
1097: // Make sure that the session menu is enabled.
1098: GUIUtils.processOnSwingEventThread(new Runnable() {
1099: public void run() {
1100: getMainFrame().getSessionMenu().setEnabled(true);
1101: }
1102: });
1103: }
1104:
1105: /**
1106: * A session is being closed.
1107: *
1108: * @param evt Current event.
1109: */
1110: public void sessionClosing(SessionEvent evt) {
1111: getMainFrame().getSessionMenu().setEnabled(false);
1112:
1113: // Clear session info from all actions.
1114: _app.getActionCollection().setCurrentSession(null);
1115:
1116: try {
1117: if (_sessionClosing) {
1118: return;
1119: }
1120:
1121: _sessionClosing = true;
1122: IIdentifier sessionId = evt.getSession()
1123: .getIdentifier();
1124:
1125: BaseSessionInternalFrame[] framesOfSession = _sessionWindows
1126: .getFramesOfSession(sessionId);
1127: for (int i = 0; i < framesOfSession.length; i++) {
1128: if (framesOfSession[i] instanceof SessionInternalFrame) {
1129: // We are in the closing event of the Session main window.
1130: // We don't want to send this event again therefore
1131: // we pass withEvents = false.
1132: framesOfSession[i].closeFrame(false);
1133: } else {
1134: framesOfSession[i].closeFrame(true);
1135: }
1136: }
1137:
1138: _sessionWindows.removeAllWindows(sessionId);
1139:
1140: selectFrontWindow();
1141: } finally {
1142: _sessionClosing = false;
1143: }
1144: }
1145: }
1146: }
|