Source Code Cross Referenced for ConnectionSelector.java in  » Database-Client » SQL-Workbench » workbench » gui » components » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » SQL Workbench » workbench.gui.components 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.