001: /*
002: * ConnectionSelector.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Dimension;
016: import java.awt.EventQueue;
017: import java.awt.Frame;
018: import java.sql.SQLException;
019:
020: import javax.swing.JDialog;
021: import javax.swing.JLabel;
022: import javax.swing.JPanel;
023: import javax.swing.SwingConstants;
024: import javax.swing.border.CompoundBorder;
025: import javax.swing.border.EmptyBorder;
026:
027: import workbench.db.ConnectionMgr;
028: import workbench.db.ConnectionProfile;
029: import workbench.db.WbConnection;
030: import workbench.util.ExceptionUtil;
031: import workbench.gui.WbSwingUtilities;
032: import workbench.gui.profiles.ProfileSelectionDialog;
033: import workbench.interfaces.Connectable;
034: import workbench.log.LogMgr;
035: import workbench.resource.ResourceMgr;
036: import workbench.resource.Settings;
037: import workbench.util.StringUtil;
038: import workbench.util.WbThread;
039:
040: /**
041: *
042: * @author support@sql-workbench.net
043: */
044: public class ConnectionSelector {
045: protected Connectable client;
046: private boolean connectInProgress;
047: protected Frame parent;
048: protected JDialog connectingInfo;
049: protected JLabel connectLabel;
050: private String propertyKey;
051:
052: public ConnectionSelector(Frame parent, Connectable conn) {
053: this .client = conn;
054: this .parent = parent;
055: }
056:
057: public void setPropertyKey(String key) {
058: this .propertyKey = key;
059: }
060:
061: public boolean isConnectInProgress() {
062: return this .connectInProgress;
063: }
064:
065: public void selectConnection() {
066: EventQueue.invokeLater(new Runnable() {
067: public void run() {
068: _selectConnection();
069: }
070: });
071: }
072:
073: protected void _selectConnection() {
074: if (this .isConnectInProgress())
075: return;
076: ProfileSelectionDialog dialog = null;
077: try {
078: WbSwingUtilities.showWaitCursor(this .parent);
079: dialog = new ProfileSelectionDialog(this .parent, true,
080: this .propertyKey);
081: WbSwingUtilities.center(dialog, this .parent);
082: WbSwingUtilities.showDefaultCursor(this .parent);
083: dialog.setVisible(true);
084: ConnectionProfile prof = dialog.getSelectedProfile();
085: boolean cancelled = dialog.isCancelled();
086:
087: if (cancelled || prof == null) {
088: this .client.connectCancelled();
089: } else {
090: this .connectTo(prof, false);
091: }
092: } catch (Throwable th) {
093: LogMgr.logError("ConnectionSelector.selectConnection()",
094: "Error during connect", th);
095: } finally {
096: if (dialog != null)
097: dialog.dispose();
098: }
099: }
100:
101: public void connectTo(final ConnectionProfile aProfile,
102: final boolean showDialogOnError) {
103: if (this .isConnectInProgress())
104: return;
105:
106: Thread t = new WbThread("Connection thread") {
107: public void run() {
108: showConnectingInfo();
109: doConnect(aProfile, showDialogOnError);
110: }
111: };
112: t.start();
113: }
114:
115: public void closeConnectingInfo() {
116: WbSwingUtilities.invoke(new Runnable() {
117: public void run() {
118: if (connectingInfo != null) {
119: connectingInfo.setVisible(false);
120: connectingInfo.dispose();
121: connectingInfo = null;
122: WbSwingUtilities.repaintLater(parent);
123: }
124: }
125: });
126: }
127:
128: public void showDisconnectInfo() {
129: showPopupMessagePanel(ResourceMgr.getString("MsgDisconnecting"));
130: }
131:
132: public void showConnectingInfo() {
133: showPopupMessagePanel(ResourceMgr.getString("MsgConnecting"));
134: }
135:
136: protected void showPopupMessagePanel(final String msg) {
137: WbSwingUtilities.invoke(new Runnable() {
138: public void run() {
139: if (connectingInfo != null) {
140: connectLabel.setText(msg);
141: connectingInfo.pack();
142: WbSwingUtilities.callRepaint(connectingInfo);
143: } else {
144: JPanel p = new JPanel();
145: p.setBorder(new CompoundBorder(WbSwingUtilities
146: .getBevelBorderRaised(), new EmptyBorder(
147: 15, 20, 15, 20)));
148: p.setLayout(new BorderLayout(0, 0));
149: p.setMinimumSize(new Dimension(250, 50));
150: connectLabel = new JLabel(msg);
151: connectLabel.setMinimumSize(new Dimension(200, 50));
152: connectLabel
153: .setHorizontalAlignment(SwingConstants.CENTER);
154: p.add(connectLabel, BorderLayout.CENTER);
155: connectingInfo = new JDialog(parent, false);
156: connectingInfo
157: .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
158: connectingInfo.getContentPane().setLayout(
159: new BorderLayout());
160: connectingInfo.getContentPane().add(p,
161: BorderLayout.CENTER);
162: connectingInfo.setUndecorated(true);
163: connectingInfo.pack();
164: WbSwingUtilities.center(connectingInfo, parent);
165: connectingInfo.setVisible(true);
166: }
167: }
168: });
169: }
170:
171: private void doConnect(final ConnectionProfile aProfile,
172: final boolean showSelectDialogOnError) {
173: if (isConnectInProgress())
174: return;
175:
176: WbConnection conn = null;
177: String error = null;
178:
179: this .setConnectIsInProgress();
180:
181: this .client.connectBegin(aProfile);
182:
183: String id = this .client.getConnectionId(aProfile);
184: try {
185: ConnectionMgr mgr = ConnectionMgr.getInstance();
186:
187: WbSwingUtilities.showWaitCursor(this .parent);
188: conn = mgr.getConnection(aProfile, id);
189: if (this .propertyKey != null) {
190: Settings.getInstance().setProperty(this .propertyKey,
191: aProfile.getName());
192: }
193: } catch (ClassNotFoundException cnf) {
194: conn = null;
195: error = ResourceMgr.getString("ErrDriverNotFound");
196: error = StringUtil.replace(error, "%class%", aProfile
197: .getDriverclass());
198: } catch (SQLException se) {
199: conn = null;
200: StringBuilder logmsg = new StringBuilder(200);
201: logmsg.append(ExceptionUtil.getDisplay(se));
202: SQLException next = se.getNextException();
203: while (next != null) {
204: logmsg.append("\n");
205: logmsg.append(ExceptionUtil.getDisplay(next));
206: next = next.getNextException();
207: }
208: error = logmsg.toString();
209:
210: LogMgr.logError("ConnectionSelector.doConnect()",
211: "SQL Exception when connecting", se);
212: } catch (Throwable e) {
213: conn = null;
214: error = ExceptionUtil.getDisplay(e);
215: } finally {
216: WbSwingUtilities.showDefaultCursor(this .parent);
217: }
218:
219: try {
220: this .closeConnectingInfo();
221:
222: final WbConnection theConnection = conn;
223: final String theError = error;
224:
225: WbSwingUtilities.invoke(new Runnable() {
226: public void run() {
227: if (theConnection != null) {
228: client.connected(theConnection);
229: } else {
230: client.connectFailed(theError);
231: }
232: }
233: });
234:
235: if (conn == null && showSelectDialogOnError) {
236: selectConnection();
237: }
238:
239: } catch (Throwable th) {
240: LogMgr.logError("ConnectionSelector.doConnect()",
241: "Error ending connection process", th);
242: } finally {
243: client.connectEnded();
244: this .clearConnectIsInProgress();
245: }
246: }
247:
248: private void setConnectIsInProgress() {
249: this .connectInProgress = true;
250: }
251:
252: private void clearConnectIsInProgress() {
253: this .connectInProgress = false;
254: }
255:
256: }
|