diff -r 73150d41330b db/apichanges.xml --- a/db/apichanges.xml Mon Feb 25 09:14:12 2008 +0100 +++ b/db/apichanges.xml Tue Feb 26 13:28:42 2008 -0800 @@ -105,6 +105,20 @@ is the proper place. + + + Add ability to remove a database connection + + + + + + Add the ability to remove a database connection from the Database + Explorer connection list + + + + Made Database Explorer API stable diff -r 73150d41330b db/arch.xml --- a/db/arch.xml Mon Feb 25 09:14:12 2008 +0100 +++ b/db/arch.xml Tue Feb 26 13:28:42 2008 -0800 @@ -73,8 +73,8 @@ made subject to such option by the copyr which allows access to the database connections and drivers defined in the Database Explorer. It allows a client to retrieve the - connection list and their properties and to create new connections. - The Database Explorer also manages a list of JDBC drivers used to + connection list and their properties, to create new connections and remove + existing ones. The Database Explorer also manages a list of JDBC drivers used to connect to databases. The API provides access to these drivers and allows to create new and remove existing drivers.

@@ -309,7 +309,14 @@ made subject to such option by the copyr ConnectionManager.showAddConnectionDialog() methods.

- + +

+ A user of this API may want to remove a connection from the list of connections + registered by the Database Explorer. This is done using + ConnectionManager.removeConnection() +

+
+

A component which provides database functionality (such as the SQL Editor) will need to connect to a database. This can be achieved using the diff -r 73150d41330b db/src/org/netbeans/api/db/explorer/ConnectionManager.java --- a/db/src/org/netbeans/api/db/explorer/ConnectionManager.java Mon Feb 25 09:14:12 2008 +0100 +++ b/db/src/org/netbeans/api/db/explorer/ConnectionManager.java Tue Feb 26 13:28:42 2008 -0800 @@ -137,6 +137,22 @@ public final class ConnectionManager { throw new NullPointerException(); } ((RootNodeInfo)RootNode.getInstance().getInfo()).addConnectionNoConnect(dbconn.getDelegate()); + } + + /** + * Remove an existing connection from the Database Explorer. This method + * unregisters the connection from the the explorer so it will + * no longer appear as a connection in the UI, and disconnects the underlying + * JDBC connection if it is connected. + * + * @param dbconn the connection to be removed + */ + public void removeConnection(DatabaseConnection dbconn) throws DatabaseException { + if ( dbconn == null ) { + throw new NullPointerException(); + } + + ((RootNodeInfo)RootNode.getInstance().getInfo()).removeConnection(dbconn.getDelegate()); } /** diff -r 73150d41330b db/src/org/netbeans/modules/db/explorer/DatabaseNodeChildren.java --- a/db/src/org/netbeans/modules/db/explorer/DatabaseNodeChildren.java Mon Feb 25 09:14:12 2008 +0100 +++ b/db/src/org/netbeans/modules/db/explorer/DatabaseNodeChildren.java Tue Feb 26 13:28:42 2008 -0800 @@ -272,6 +272,21 @@ public class DatabaseNodeChildren extend } } + + public void removeSubNode(Node subnode) { + //workaround for issue #31617, children should be initialized if they are not + // getNodes(); + if (isInitialized()) { + synchronized (additionalNodes) { + if (initialized) { + this.remove(new Node[] {subnode}); + } else { + additionalNodes.remove(subnode); + } + } + } + } + public DatabaseNode createSubnode(DatabaseNodeInfo info, boolean addToChildrenFlag) throws DatabaseException { DatabaseNode subnode = createNode(info); if (subnode != null && addToChildrenFlag) { diff -r 73150d41330b db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java --- a/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java Mon Feb 25 09:14:12 2008 +0100 +++ b/db/src/org/netbeans/modules/db/explorer/infos/RootNodeInfo.java Tue Feb 26 13:28:42 2008 -0800 @@ -210,6 +210,28 @@ public class RootNodeInfo extends Databa children.createSubnode(ninfo, true); } + public void removeConnection(DatabaseConnection dbconn) throws DatabaseException { + if ( dbconn == null ) { + throw new NullPointerException(); + } + + ConnectionList.getDefault().remove(dbconn); + + DatabaseNode node = getNode(); + DatabaseNodeChildren children = (DatabaseNodeChildren)node.getChildren(); + Node[] nodes = children.getNodes(); + + for ( Node childNode : nodes ) { + if ( childNode instanceof ConnectionNode ) { + ConnectionNodeInfo connInfo = (ConnectionNodeInfo) + ((ConnectionNode)childNode).getInfo(); + if ( ! connInfo.getDatabaseConnection().equals(dbconn)) { + children.removeSubNode(childNode); + } + } + } + } + public void addConnection(DBConnection cinfo) throws DatabaseException { DatabaseConnection dbconn = (DatabaseConnection)cinfo; getChildren(); // force restore diff -r 73150d41330b db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java --- a/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java Mon Feb 25 09:14:12 2008 +0100 +++ b/db/test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionTest.java Tue Feb 26 13:28:42 2008 -0800 @@ -41,6 +41,7 @@ package org.netbeans.api.db.explorer; +import java.net.URL; import org.netbeans.modules.db.test.TestBase; import org.netbeans.modules.db.test.Util; @@ -60,7 +61,12 @@ public class DatabaseConnectionTest exte } public void testConnectionsRemovedWhenFilesDeleted() throws Exception{ - JDBCDriver driver = JDBCDriverManager.getDefault().getDrivers("sun.jdbc.odbc.JdbcOdbcDriver")[0]; + Util.deleteConnectionFiles(); + Util.deleteDriverFiles(); + + JDBCDriver driver = createDriver(); + assertEquals(1, JDBCDriverManager.getDefault().getDrivers().length); + DatabaseConnection dbconn = DatabaseConnection.create(driver, "database", "user", "schema", "password", true); ConnectionManager.getDefault().addConnection(dbconn); @@ -72,12 +78,45 @@ public class DatabaseConnectionTest exte } public void testSameDatabaseConnectionReturned() throws Exception { + Util.deleteConnectionFiles(); + Util.deleteDriverFiles(); assertEquals(0, ConnectionManager.getDefault().getConnections().length); - JDBCDriver driver = JDBCDriverManager.getDefault().getDrivers("sun.jdbc.odbc.JdbcOdbcDriver")[0]; + JDBCDriver driver = createDriver(); + assertEquals(1, JDBCDriverManager.getDefault().getDrivers().length); + DatabaseConnection dbconn = DatabaseConnection.create(driver, "database", "user", "schema", "password", true); ConnectionManager.getDefault().addConnection(dbconn); + assertTrue(ConnectionManager.getDefault().getConnections().length == 1); assertEquals(dbconn, ConnectionManager.getDefault().getConnections()[0]); } + + public void testDeleteConnection() throws Exception { + Util.deleteConnectionFiles(); + Util.deleteDriverFiles(); + + assertEquals(0, ConnectionManager.getDefault().getConnections().length); + assertEquals(0, JDBCDriverManager.getDefault().getDrivers().length); + + JDBCDriver driver = createDriver(); + + DatabaseConnection dbconn = DatabaseConnection.create( + driver, "jdbc:bar:localhost", + "user", "schema", "password", true); + ConnectionManager.getDefault().addConnection(dbconn); + + assertEquals(1, ConnectionManager.getDefault().getConnections().length); + + ConnectionManager.getDefault().removeConnection(dbconn); + assertEquals(0, ConnectionManager.getDefault().getConnections().length); + } + + private JDBCDriver createDriver() throws Exception { + JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", + "org.bar.BarDriver", new URL[]{ new URL("file://foo/path/foo.jar")}); + JDBCDriverManager.getDefault().addDriver(driver); + + return driver; + } }