Index: apichanges.xml
===================================================================
RCS file: /cvs/db/apichanges.xml,v
retrieving revision 1.1.4.2
retrieving revision 1.1.4.2.4.2
diff -u -r1.1.4.2 -r1.1.4.2.4.2
--- apichanges.xml 18 Jan 2006 18:53:33 -0000 1.1.4.2
+++ apichanges.xml 17 Mar 2006 16:31:38 -0000 1.1.4.2.4.2
@@ -86,7 +86,22 @@
DatabaseConnectionUIHelper.connect()
+ method, which populates a JComboBox
with the list of connections
+ from 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
+ DatabaseConnectionUIHelper.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) { + connect(combo, null); + } + + private static final void connect(final JComboBox combo, DatabaseConnection selectedConnection) { + + combo.setEditable(false); + combo.setRenderer(new DatabaseConnectionListCellRenderer()); + + populate(combo, 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, 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, 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, DatabaseConnection selectedConnection) { + + DatabaseConnection[] conns = ConnectionManager.getDefault().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: test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionUIHelperTest.java =================================================================== RCS file: test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionUIHelperTest.java diff -N test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionUIHelperTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/unit/src/org/netbeans/api/db/explorer/DatabaseConnectionUIHelperTest.java 17 Mar 2006 16:06:40 -0000 1.1.2.3 @@ -0,0 +1,73 @@ +/* + * 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; + +import junit.framework.*; +import javax.swing.JComboBox; +import org.netbeans.modules.db.test.TestBase; + +/** + * + * @author Libor Kotouc + */ +public class DatabaseConnectionUIHelperTest extends TestBase { + + private DatabaseConnection dbconn1 = null; + private DatabaseConnection dbconn2 = null; + + public DatabaseConnectionUIHelperTest(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(); + DatabaseConnectionUIHelper.connect(combo); + 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() == DatabaseConnectionUIHelper.EMPTY_ITEM); + assertTrue("EMPTY_ITEM must be followed by SEPARATOR_ITEM.", combo.getItemAt(1) == DatabaseConnectionUIHelper.SEPARATOR_ITEM); + assertTrue("SEPARATOR_ITEM must be followed by NEW_ITEM", combo.getItemAt(2) == DatabaseConnectionUIHelper.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) != DatabaseConnectionUIHelper.EMPTY_ITEM); + } + assertTrue("Second connection must be followed by SEPARATOR_ITEM.", combo.getItemAt(2) == DatabaseConnectionUIHelper.SEPARATOR_ITEM); + assertTrue("SEPARATOR_ITEM must be followed by NEW_ITEM", combo.getItemAt(3) == DatabaseConnectionUIHelper.NEW_ITEM); + } + +}