Quantum version 2.4.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / connection / ConnectionUtil.java
1 package com.quantum.util.connection;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5
6 import com.quantum.Messages;
7 import com.quantum.model.Bookmark;
8 import com.quantum.model.ConnectionException;
9 import com.quantum.model.NotConnectedException;
10 import com.quantum.ui.dialog.ExceptionDisplayDialog;
11 import com.quantum.ui.dialog.PasswordDialog;
12 import com.quantum.ui.dialog.SQLExceptionDialog;
13
14 import org.eclipse.swt.widgets.Shell;
15
16 /**
17  * <p>This utility gets a connection from a bookmark, and handles any UI-specific
18  * interactions such as providing messages to the user and/or prompting for a 
19  * password.
20  * 
21  * @author BC Holmes
22  */
23 public class ConnectionUtil {
24
25     public Connection getConnection(Bookmark bookmark, Shell shell) {
26         Connection connection = null;
27         try {
28             connection = bookmark.getConnection();
29         } catch (NotConnectedException e) {
30             connection = connect(bookmark, shell);
31         }
32         return connection;
33     }
34
35     public Connection connect(Bookmark bookmark, Shell shell) {
36         Connection connection = null;
37         try {
38             connection = bookmark.connect(PasswordDialog.createPasswordFinder(shell));
39         } catch (ConnectionException e) {
40                 if (e.getCause() != null && e.getCause() instanceof SQLException) {
41                         SQLExceptionDialog.openException(shell, bookmark, (SQLException) e.getCause());
42                 } else {
43                     ExceptionDisplayDialog.openError(shell, 
44                         Messages.getString(getClass().getName() + ".title"), 
45                         Messages.getString(getClass().getName() + ".message") +
46                                         " (Bookmark:"+bookmark.getName()+")", e);
47                 }
48         }
49         return connection;
50     }
51
52 }