Index: db/apichanges.xml
===================================================================
diff -u db/apichanges.xml:1.1.4.2 db/apichanges.xml:1.1.4.2.4.3
--- db/apichanges.xml:1.1.4.2 Wed Jan 18 10:53:33 2006
+++ db/apichanges.xml Tue Mar 21 05:13:56 2006
@@ -86,7 +86,22 @@
DatabaseExplorerUIs.connect()
+ method, which populates a JComboBox
with the list of connections
+ from a ConnectionManager
.
+
+ A component which provides database functionality (such as the SQL Editor
+ or a module providing support for data sources) will need to let the user
+ select the a database connection, usually through a combo box.
+ This can be achieved using the
+ DatabaseExplorerUIs.connect()
+ method. The JComboBox
passed to the method will be filled with the list of connections as returned by
+ ConnectionManager.getConnections(), followed by a separator
+ and a New Database Connection item which will display the dialog for adding a new database connection 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.
+ * + * @param combo combo box to be filled with the database connections. + */ + public static void connect(JComboBox combo, ConnectionManager connectionManager) { + connect(combo, connectionManager, null); + } + + private static final void connect(final JComboBox combo, final ConnectionManager connectionManager, DatabaseConnection selectedConnection) { + + combo.setEditable(false); + combo.setRenderer(new DatabaseConnectionListCellRenderer()); + + populate(combo, connectionManager, selectedConnection); + + combo.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent e) { + int keyCode = e.getKeyCode(); + if (KeyEvent.VK_ENTER == keyCode) { + Object selectedItem = combo.getSelectedItem(); + if (selectedItem == NEW_ITEM) { + DatabaseConnection newConn = performCreateDBConnAction(combo); + combo.setPopupVisible(false); + e.consume(); +// if (newConn != null) + populate(combo, connectionManager, newConn); + } + } + } + }); + + combo.addActionListener(new ActionListener() { + + Object previousItem; + int previousIndex = combo.getSelectedIndex(); + + public void actionPerformed(ActionEvent e) { + + Object selectedItem = combo.getSelectedItem(); + // skipping of separator + if (selectedItem == SEPARATOR_ITEM) { + int selectedIndex = combo.getSelectedIndex(); + if (selectedIndex > previousIndex) { + previousIndex = selectedIndex + 1; + previousItem = combo.getItemAt(previousIndex); + } else { + previousIndex = selectedIndex - 1; + previousItem = combo.getItemAt(previousIndex); + } + combo.setSelectedItem(previousItem); + // handling mouse click, see KeyEvent.getKeyModifiersText(e.getModifiers()) + } else if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) { + if (selectedItem == NEW_ITEM) { + DatabaseConnection newConn = performCreateDBConnAction(combo); + combo.setPopupVisible(false); +// if (newConn != null) + populate(combo, connectionManager, newConn); + } + } + } + }); + + } + + private static DatabaseConnection performCreateDBConnAction(JComboBox combo) { + + ConnectionManager.getDefault().showAddConnectionDialog(null); + DatabaseConnection newConn = null; + + // TODO delete above and uncomment all lines when showAddConnectionDialog() will return value + +// DatabaseConnection newConn = ConnectionManager.getDefault().showAddConnectionDialog(null); +// if (newConn == null) { // no new connection created -> select previous item or first item +// DatabaseConnectionComboBoxModel model = (DatabaseConnectionComboBoxModel)combo.getModel(); +// if (model.getPreviousItem() != null) +// combo.setSelectedItem(model.getPreviousItem()); +// else +// combo.setSelectedItem(model.getElementAt(0)); +// } + + return newConn; + } + + private static void populate(final JComboBox combo, ConnectionManager connectionManager, DatabaseConnection selectedConnection) { + + DatabaseConnection[] conns = connectionManager.getConnections(); + + Arrays.sort(conns, new DatabaseConnectionComparator()); + + List items = new LinkedList(Arrays.asList(conns)); + + if (items.size() == 0) + items.add(EMPTY_ITEM); + + items.add(SEPARATOR_ITEM); + items.add(NEW_ITEM); + + DatabaseConnectionComboBoxModel model = new DatabaseConnectionComboBoxModel(items.toArray()); + + combo.setModel(model); + + if (selectedConnection != null) { + combo.setSelectedItem(selectedConnection); + } + } +} Index: db/test/unit/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIsTest.java =================================================================== diff -u /dev/null db/test/unit/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIsTest.java:1.1.2.1 --- /dev/null Tue Mar 21 05:20:44 2006 +++ db/test/unit/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIsTest.java Tue Mar 21 05:13:58 2006 @@ -0,0 +1,74 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.netbeans.api.db.explorer.support; + +import junit.framework.*; +import javax.swing.JComboBox; +import org.netbeans.api.db.explorer.*; +import org.netbeans.modules.db.test.TestBase; + +/** + * + * @author Libor Kotouc + */ +public class DatabaseExplorerUIsTest extends TestBase { + + private DatabaseConnection dbconn1 = null; + private DatabaseConnection dbconn2 = null; + + public DatabaseExplorerUIsTest(String testName) { + super(testName); + } + + private void initConnections() throws Exception { + assertEquals(0, ConnectionManager.getDefault().getConnections().length); + JDBCDriver driver = JDBCDriverManager.getDefault().getDrivers("sun.jdbc.odbc.JdbcOdbcDriver")[0]; + dbconn1 = DatabaseConnection.create(driver, "db", "dbuser", "dbschema", "dbpassword", true); + dbconn2 = DatabaseConnection.create(driver, "database", "user", "schema", "password", true); + ConnectionManager.getDefault().addConnection(dbconn1); + ConnectionManager.getDefault().addConnection(dbconn2); + assertEquals(2, ConnectionManager.getDefault().getConnections().length); + } + + private JComboBox connect() { + JComboBox combo = new JComboBox(); + DatabaseExplorerUIs.connect(combo, ConnectionManager.getDefault()); + return combo; + } + + public void testEmptyComboboxContent() { + JComboBox combo = connect(); + + assertTrue("Wrong number of items in the empty combobox", combo.getItemCount() == 3); + assertTrue("EMPTY_ITEM is not selected by default.", combo.getSelectedItem() == DatabaseExplorerUIs.EMPTY_ITEM); + assertTrue("EMPTY_ITEM must be followed by SEPARATOR_ITEM.", combo.getItemAt(1) == DatabaseExplorerUIs.SEPARATOR_ITEM); + assertTrue("SEPARATOR_ITEM must be followed by NEW_ITEM", combo.getItemAt(2) == DatabaseExplorerUIs.NEW_ITEM); + } + + public void testComboboxWithConnections() throws Exception { + + initConnections(); + + JComboBox combo = connect(); + + assertTrue("Wrong number of items in the combobox", combo.getItemCount() == 4); + assertTrue("Wrong order, db connections aren't alphabetically ordered", combo.getSelectedItem() == dbconn2); + for (int i = 0; i < combo.getItemCount(); i++) { + assertTrue("EMPTY_ITEM mustn't be in the combobox", combo.getItemAt(i) != DatabaseExplorerUIs.EMPTY_ITEM); + } + assertTrue("Second connection must be followed by SEPARATOR_ITEM.", combo.getItemAt(2) == DatabaseExplorerUIs.SEPARATOR_ITEM); + assertTrue("SEPARATOR_ITEM must be followed by NEW_ITEM", combo.getItemAt(3) == DatabaseExplorerUIs.NEW_ITEM); + } + +} Index: ide/golden/public-packages.txt =================================================================== diff -u ide/golden/public-packages.txt:1.38.2.4.2.6 ide/golden/public-packages.txt:1.38.2.4.2.6.2.1 --- ide/golden/public-packages.txt:1.38.2.4.2.6 Sun Mar 5 06:03:25 2006 +++ ide/golden/public-packages.txt Tue Mar 21 05:13:59 2006 @@ -209,6 +209,7 @@ org.apache.xml.serialize org.jdesktop.layout org.netbeans.api.db.explorer +org.netbeans.api.db.explorer.support org.netbeans.api.debugger org.netbeans.api.debugger.jpda org.netbeans.api.debugger.jpda.event