# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /home/matthias/NetBeansProjects/core-main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: db/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIs.java --- db/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIs.java Base (BASE) +++ db/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIs.java Locally Modified (Based On LOCAL) @@ -41,29 +41,29 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. */ - package org.netbeans.api.db.explorer.support; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; -import java.util.HashSet; import java.util.List; -import java.util.Set; import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; +import javax.swing.SwingUtilities; +import org.netbeans.api.db.explorer.ConnectionListener; import org.netbeans.api.db.explorer.ConnectionManager; import org.netbeans.api.db.explorer.DatabaseConnection; import org.netbeans.modules.db.util.DataComboBoxModel; import org.netbeans.modules.db.util.DataComboBoxSupport; import org.openide.util.NbBundle; +import org.openide.util.WeakListeners; /** * This class contains utility methods for working with and/or displaying - * database connections in the UI. Currently it provides a method for - * populating a combo box with the list of database connections from + * database connections in the UI. Currently it provides a method for populating + * a combo box with the list of database connections from * {@link ConnectionManager}. * * @author Andrei Badea @@ -77,14 +77,14 @@ /** * Populates and manages the contents of the passed combo box. The combo box - * contents consists of the database connections defined in - * the passes instance of {@link ConnectionManager} and a Add Database Connection - * item which displays the New Database Connection dialog when selected. + * contents consists of the database connections defined in the passes + * instance of {@link ConnectionManager} and a Add Database Connection item + * which displays the New Database Connection dialog when selected. * - *

This method may cause the replacement of the combo box model, - * thus the caller is recommended to register a - * {@link java.beans.PropertyChangeListener} on the combo box when - * it needs to check the combo box content when it changes.

+ *

This method may cause the replacement of the combo box model, thus the + * caller is recommended to register a + * {@link java.beans.PropertyChangeListener} on the combo box when it needs + * to check the combo box content when it changes.

* * @param comboBox combo box to be filled with the database connections. */ @@ -94,44 +94,32 @@ private static final class ConnectionDataComboBoxModel implements DataComboBoxModel { - private final ConnectionManager connectionManager; private final ConnectionComboBoxModel comboBoxModel; public ConnectionDataComboBoxModel(ConnectionManager connectionManager) { - this.connectionManager = connectionManager; this.comboBoxModel = new ConnectionComboBoxModel(connectionManager); } + @Override public String getItemTooltipText(Object item) { - return ((DatabaseConnection)item).toString(); + return ((DatabaseConnection) item).toString(); } + @Override public String getItemDisplayName(Object item) { - return ((DatabaseConnection)item).getDisplayName(); + return ((DatabaseConnection) item).getDisplayName(); } + @Override public void newItemActionPerformed() { - Set oldConnections = new HashSet(Arrays.asList(connectionManager.getConnections())); - connectionManager.showAddConnectionDialog(null); - - // try to find the new connection - DatabaseConnection[] newConnections = connectionManager.getConnections(); - if (newConnections.length == oldConnections.size()) { - // no new connection, so... - return; } - for (int i = 0; i < newConnections.length; i++) { - if (!oldConnections.contains(newConnections[i])) { - comboBoxModel.addSelectedConnection(newConnections[i]); - break; - } - } - } + @Override public String getNewItemDisplayName() { return NbBundle.getMessage(DatabaseExplorerUIs.class, "LBL_NewDbConnection"); } + @Override public ComboBoxModel getListModel() { return comboBoxModel; } @@ -140,48 +128,68 @@ private static final class ConnectionComboBoxModel extends AbstractListModel implements ComboBoxModel { private final ConnectionManager connectionManager; - private final List connectionList; // must be ArrayList - + private final List connectionList = new ArrayList(); private Object selectedItem; // can be anything, not just a database connection + private ConnectionListener cl = new ConnectionListener() { + @Override + public void connectionsChanged() { + updateConnectionList(); + } + }; public ConnectionComboBoxModel(ConnectionManager connectionManager) { this.connectionManager = connectionManager; + connectionManager.addConnectionListener(WeakListeners.create(ConnectionListener.class, cl, connectionManager)); + updateConnectionList(); + } - connectionList = new ArrayList(); + private void updateConnectionList() { + Runnable r = new Runnable() { + @Override + public void run() { + int oldLength = connectionList.size(); + connectionList.clear(); connectionList.addAll(Arrays.asList(connectionManager.getConnections())); Collections.sort(connectionList, new ConnectionComparator()); + fireContentsChanged(this, 0, Math.max(connectionList.size(), oldLength)); } + }; + if (SwingUtilities.isEventDispatchThread()) { + r.run(); + } else { + SwingUtilities.invokeLater(r); + } + } + + @Override public void setSelectedItem(Object anItem) { selectedItem = anItem; } + @Override public Object getElementAt(int index) { return connectionList.get(index); } + @Override public int getSize() { return connectionList.size(); } + @Override public Object getSelectedItem() { return selectedItem; } - - public void addSelectedConnection(DatabaseConnection dbconn) { - selectedItem = dbconn; - connectionList.add(dbconn); - Collections.sort(connectionList, new ConnectionComparator()); - fireContentsChanged(this, 0, connectionList.size()); } - } private static final class ConnectionComparator implements Comparator { - + @Override public boolean equals(Object that) { return that instanceof ConnectionComparator; } + @Override public int compare(Object dbconn1, Object dbconn2) { if (dbconn1 == null) { return dbconn2 == null ? 0 : -1; @@ -191,8 +199,8 @@ } } - String dispName1 = ((DatabaseConnection)dbconn1).getDisplayName(); - String dispName2 = ((DatabaseConnection)dbconn2).getDisplayName(); + String dispName1 = ((DatabaseConnection) dbconn1).getDisplayName(); + String dispName2 = ((DatabaseConnection) dbconn2).getDisplayName(); if (dispName1 == null) { return dispName2 == null ? 0 : -1; } else { Index: db/test/unit/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIsTest.java --- db/test/unit/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIsTest.java Base (BASE) +++ db/test/unit/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIsTest.java Locally Modified (Based On LOCAL) @@ -41,7 +41,6 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. */ - package org.netbeans.api.db.explorer.support; import javax.swing.JComboBox; @@ -64,6 +63,10 @@ private void initConnections() throws Exception { JDBCDriver driver = Util.createDummyDriver(); + DatabaseConnection[] connections = ConnectionManager.getDefault().getConnections(); + for (DatabaseConnection dc : connections) { + ConnectionManager.getDefault().removeConnection(dc); + } assertEquals(0, ConnectionManager.getDefault().getConnections().length); dbconn1 = DatabaseConnection.create(driver, "db", "dbuser", "dbschema", "dbpassword", true); dbconn2 = DatabaseConnection.create(driver, "database", "user", "schema", "password", true); @@ -93,4 +96,28 @@ assertSame(dbconn2, combo.getItemAt(0)); assertSame(dbconn1, combo.getItemAt(1)); } + + public void testComboboxChangingConnections() throws Exception { + initConnections(); + JComboBox combo = connect(); + + assertTrue("Wrong number of items in the combobox", combo.getItemCount() == 3); + + assertSame(dbconn2, combo.getItemAt(0)); + assertSame(dbconn1, combo.getItemAt(1)); + + DatabaseConnection dc = DatabaseConnection.create(Util.createDummyDriver(), "dc1", "user", "schema", "password", true); + ConnectionManager.getDefault().addConnection(dc); + + assertTrue("Wrong number of items in the combobox", combo.getItemCount() == 4); + + assertSame(dc, combo.getItemAt(2)); + + ConnectionManager.getDefault().removeConnection(dc); + + assertTrue("Wrong number of items in the combobox", combo.getItemCount() == 3); + + assertSame(dbconn2, combo.getItemAt(0)); + assertSame(dbconn1, combo.getItemAt(1)); } +}