001: package net.sourceforge.squirrel_sql.client.gui.db;
002:
003: import net.sourceforge.squirrel_sql.client.IApplication;
004: import net.sourceforge.squirrel_sql.client.mainframe.action.ConnectToAliasCommand;
005: import net.sourceforge.squirrel_sql.client.session.ISession;
006: import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
007: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
008: import net.sourceforge.squirrel_sql.fw.sql.WrappedSQLException;
009: import net.sourceforge.squirrel_sql.fw.gui.ErrorDialog;
010: import net.sourceforge.squirrel_sql.fw.util.StringManager;
011: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
012: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
013: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
014:
015: import javax.swing.*;
016: import java.sql.SQLException;
017:
018: public class ConnectToAliasCallBack implements ICompletionCallback {
019: private static final StringManager s_stringMgr = StringManagerFactory
020: .getStringManager(ConnectToAliasCallBack.class);
021:
022: private static final ILogger s_log = LoggerController
023: .createLogger(ConnectToAliasCallBack.class);
024:
025: private final IApplication _app;
026: private final SQLAlias _sqlAlias;
027:
028: public ConnectToAliasCallBack(IApplication app, SQLAlias alias) {
029: super ();
030: if (app == null) {
031: throw new IllegalArgumentException("IApplication == null");
032: }
033: if (alias == null) {
034: throw new IllegalArgumentException("ISQLAlias == null");
035: }
036: _app = app;
037: _sqlAlias = alias;
038: }
039:
040: /**
041: * @see CompletionCallback#connected(net.sourceforge.squirrel_sql.fw.sql.SQLConnection)
042: */
043: public void connected(ISQLConnection conn) {
044: // Empty.
045: }
046:
047: /**
048: * @see CompletionCallback#sessionCreated(net.sourceforge.squirrel_sql.client.session.ISession)
049: */
050: public void sessionCreated(ISession session) {
051: // Empty.
052: }
053:
054: /**
055: * @see CompletionCallback#errorOccured(Throwable)
056: */
057: public void errorOccured(Throwable th) {
058: if (th instanceof WrappedSQLException) {
059: th = ((WrappedSQLException) th).getSQLExeption();
060: }
061:
062: if (th instanceof SQLException) {
063: String msg = th.getMessage();
064: if (msg == null || msg.length() == 0) {
065: msg = s_stringMgr
066: .getString("ConnectToAliasCommand.error.cantopen");
067: }
068: msg = _sqlAlias.getName() + ": " + msg;
069: showErrorDialog(msg, th);
070: } else if (th instanceof ClassNotFoundException) {
071: String msg = s_stringMgr.getString(
072: "ConnectToAliasCommand.error.driver", _sqlAlias
073: .getName());
074: showErrorDialog(msg, th);
075: } else if (th instanceof NoClassDefFoundError) {
076: String msg = s_stringMgr.getString(
077: "ConnectToAliasCommand.error.driver", _sqlAlias
078: .getName());
079: s_log.error(msg, th);
080: showErrorDialog(msg, th);
081: } else {
082: String msg = s_stringMgr.getString(
083: "ConnectToAliasCommand.error.unexpected", _sqlAlias
084: .getName());
085: s_log.debug(th.getClass().getName());
086: s_log.error(msg, th);
087: showErrorDialog(msg, th);
088: }
089: }
090:
091: protected IApplication getApplication() {
092: return _app;
093: }
094:
095: protected void showErrorDialog(final String msg, final Throwable th) {
096: synchronized (this ) {
097: SwingUtilities.invokeLater(new Runnable() {
098: public void run() {
099: new ErrorDialog(_app.getMainFrame(), msg, th)
100: .setVisible(true);
101: }
102: });
103: }
104: }
105:
106: public SQLAlias getAlias() {
107: return _sqlAlias;
108: }
109:
110: }
|