1 19 20 package org.netbeans.api.db.explorer; 21 22 import javax.swing.SwingUtilities ; 23 import org.netbeans.lib.ddl.DBConnection; 24 import org.netbeans.modules.db.explorer.ConnectionList; 25 import org.netbeans.modules.db.explorer.actions.ConnectUsingDriverAction; 26 import org.netbeans.modules.db.explorer.infos.RootNodeInfo; 27 import org.netbeans.modules.db.explorer.nodes.RootNode; 28 import org.openide.ErrorManager; 29 import org.openide.util.Mutex; 30 31 52 public final class ConnectionManager { 53 54 57 private static ConnectionManager DEFAULT; 58 59 62 public static synchronized ConnectionManager getDefault() { 63 if (DEFAULT == null) { 64 DEFAULT = new ConnectionManager(); 65 } 66 return DEFAULT; 67 } 68 69 74 public DatabaseConnection[] getConnections() { 75 DBConnection[] conns = ConnectionList.getDefault().getConnections(); 76 DatabaseConnection[] dbconns = new DatabaseConnection[conns.length]; 77 for (int i = 0; i < conns.length; i++) { 78 dbconns[i] = ((org.netbeans.modules.db.explorer.DatabaseConnection)conns[i]).getDatabaseConnection(); 79 } 80 return dbconns; 81 } 82 83 90 public DatabaseConnection getConnection(String name) { 91 if (name == null) { 92 throw new NullPointerException (); 93 } 94 DBConnection[] conns = ConnectionList.getDefault().getConnections(); 95 for (int i = 0; i < conns.length; i++) { 96 DatabaseConnection dbconn = ((org.netbeans.modules.db.explorer.DatabaseConnection)conns[i]).getDatabaseConnection(); 97 if (name.equals(dbconn.getName())) { 98 return dbconn; 99 } 100 } 101 return null; 102 } 103 104 113 public void addConnection(DatabaseConnection dbconn) throws DatabaseException { 114 if (dbconn == null) { 115 throw new NullPointerException (); 116 } 117 ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnectionNoConnect(dbconn.getDelegate()); 118 } 119 120 126 public void showAddConnectionDialog(JDBCDriver driver) { 127 showAddConnectionDialog(driver, null, null, null); 128 } 129 130 140 public void showAddConnectionDialog(JDBCDriver driver, final String databaseUrl) { 141 showAddConnectionDialog(driver, databaseUrl, null, null); 142 } 143 144 160 public void showAddConnectionDialog(final JDBCDriver driver, final String databaseUrl, final String user, final String password) { 161 Mutex.EVENT.readAccess(new Runnable () { 162 public void run() { 163 new ConnectUsingDriverAction.NewConnectionDialogDisplayer().showDialog(driver, databaseUrl, user, password); 164 } 165 }); 166 } 167 168 183 public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver) { 184 return showAddConnectionDialogFromEventThread(driver, null, null, null); 185 } 186 187 203 public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver, String databaseUrl) { 204 return showAddConnectionDialogFromEventThread(driver, databaseUrl, null, null); 205 } 206 207 225 public DatabaseConnection showAddConnectionDialogFromEventThread(JDBCDriver driver, String databaseUrl, String user, String password) { 226 if (!SwingUtilities.isEventDispatchThread()) { 227 throw new IllegalStateException ("The current thread is not the event dispatching thread."); } 229 org.netbeans.modules.db.explorer.DatabaseConnection internalDBConn = new ConnectUsingDriverAction.NewConnectionDialogDisplayer().showDialog(driver, databaseUrl, user, password); 230 if (internalDBConn != null) { 231 return internalDBConn.getDatabaseConnection(); 232 } 233 return null; 234 } 235 236 248 public void showConnectionDialog(DatabaseConnection dbconn) { 249 if (dbconn == null) { 250 throw new NullPointerException (); 251 } 252 if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) { 253 throw new IllegalStateException ("This connection is not added to the ConnectionManager."); } 255 dbconn.getDelegate().showConnectionDialog(); 256 } 257 258 268 public void disconnect(DatabaseConnection dbconn) { 269 if (dbconn == null) { 270 throw new NullPointerException (); 271 } 272 if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) { 273 throw new IllegalStateException ("This connection is not added to the ConnectionManager."); } 275 try { 276 dbconn.getDelegate().disconnect(); 277 } catch (DatabaseException e) { 278 ErrorManager.getDefault().notify(e); 280 } 281 } 282 283 293 public void selectConnectionInExplorer(DatabaseConnection dbconn) { 294 if (dbconn == null) { 295 throw new NullPointerException (); 296 } 297 if (!ConnectionList.getDefault().contains(dbconn.getDelegate())) { 298 throw new IllegalStateException ("This connection is not added to the ConnectionManager."); } 300 dbconn.getDelegate().selectInExplorer(); 301 } 302 303 306 public void addConnectionListener(ConnectionListener listener) { 307 ConnectionList.getDefault().addConnectionListener(listener); 308 } 309 310 313 public void removeConnectionListener(ConnectionListener listener) { 314 ConnectionList.getDefault().removeConnectionListener(listener); 315 } 316 } 317 | Popular Tags |