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 @@ First initial release of the Database Explorer API. - + + + + Addded an utility method to fill a combo box with database connections + + + + + + This change adds the DatabaseExplorerUIs.connect() + method, which populates a JComboBox with the list of connections + from a ConnectionManager. + + + + Index: db/arch.xml =================================================================== diff -u db/arch.xml:1.3.2.2.2.1 db/arch.xml:1.3.2.2.2.1.2.2 --- db/arch.xml:1.3.2.2.2.1 Thu Feb 9 05:01:11 2006 +++ db/arch.xml Tue Mar 21 05:13:57 2006 @@ -271,6 +271,18 @@ method.

+ +

+ 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. +

+
Index: db/nbproject/project.properties =================================================================== diff -u db/nbproject/project.properties:1.8.2.3.2.1 db/nbproject/project.properties:1.8.2.3.2.1.2.1 --- db/nbproject/project.properties:1.8.2.3.2.1 Thu Feb 9 05:01:19 2006 +++ db/nbproject/project.properties Fri Mar 17 07:45:51 2006 @@ -13,7 +13,7 @@ javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=1.17.0 +spec.version.base=1.18.0 extra.module.files=modules/ext/ddl.jar Index: db/nbproject/project.xml =================================================================== diff -u db/nbproject/project.xml:1.11.2.1.2.1 db/nbproject/project.xml:1.11.2.1.2.1.2.1 --- db/nbproject/project.xml:1.11.2.1.2.1 Thu Feb 9 05:01:20 2006 +++ db/nbproject/project.xml Tue Mar 21 05:13:57 2006 @@ -126,6 +126,7 @@ org.netbeans.api.db.explorer + org.netbeans.api.db.explorer.support org.netbeans.spi.db.explorer Index: db/src/org/netbeans/api/db/explorer/Bundle.properties =================================================================== diff -u /dev/null db/src/org/netbeans/api/db/explorer/Bundle.properties:1.1.2.2 --- /dev/null Tue Mar 21 05:20:43 2006 +++ db/src/org/netbeans/api/db/explorer/Bundle.properties Wed Mar 15 07:31:41 2006 @@ -0,0 +1,12 @@ +# 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. + +LBL_NewDbConnection=New Database Connection... Index: db/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIs.java =================================================================== diff -u /dev/null db/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIs.java:1.1.2.1 --- /dev/null Tue Mar 21 05:20:43 2006 +++ db/src/org/netbeans/api/db/explorer/support/DatabaseExplorerUIs.java Tue Mar 21 05:13:58 2006 @@ -0,0 +1,285 @@ +/* + * 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 java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.util.Arrays; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; +import javax.swing.AbstractListModel; +import javax.swing.ComboBoxModel; +import javax.swing.DefaultListCellRenderer; +import javax.swing.JComboBox; +import javax.swing.JList; +import javax.swing.JSeparator; +import org.netbeans.api.db.explorer.*; +import org.openide.util.NbBundle; + +/** + * 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 + * {@link ConnectionManager}. + * + * @author Libor Kotouc + * + * @since 1.18 + */ +public final class DatabaseExplorerUIs { + + private static final class Separator extends JSeparator { + Separator() { + setPreferredSize(new Dimension(getWidth(), 1)); + setForeground(Color.BLACK); + } + } + + /** Not private because used in tests. */ + static final Separator SEPARATOR_ITEM = new Separator(); + + /** Not private because used in tests. */ + static final Object NEW_ITEM = new Object() { + public String toString() { + return NbBundle.getMessage(DatabaseExplorerUIs.class, "LBL_NewDbConnection"); // NOI18N + } + }; + + /** Not private because used in tests. */ + static final Object EMPTY_ITEM = new Object() { + public String toString() { + return " "; // NOI18N + } + }; + + private static class DatabaseConnectionComboBoxModel extends AbstractListModel implements ComboBoxModel { + + private Object[] items; + private Object selectedItem; + private Object previousItem; + + DatabaseConnectionComboBoxModel(Object[] items) { + assert(items.length > 0); + this.items = items; + selectedItem = this.items[0]; + } + + public void setSelectedItem(Object anItem) { + assert(anItem != null); + previousItem = selectedItem; + selectedItem = anItem; + } + + public Object getSelectedItem() { + return selectedItem; + } + + public Object getElementAt(int index) { + return items[index]; + } + + public int getSize() { + return items.length; + } + + Object getPreviousItem() { + return previousItem; + } + + } + + private static class DatabaseConnectionListCellRenderer extends DefaultListCellRenderer { + + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + + if (isSelected) { + setBackground(list.getSelectionBackground()); + setForeground(list.getSelectionForeground()); + } else { + setBackground(list.getBackground()); + setForeground(list.getForeground()); + } + + if (value instanceof DatabaseConnection) { + DatabaseConnection conn = (DatabaseConnection) value; + setText(conn != null ? conn.getDisplayName() : ""); + setToolTipText(conn.toString()); + } + else + if (value == SEPARATOR_ITEM) { + return SEPARATOR_ITEM; + } + else { + if (value != null) + setText(value.toString()); + setToolTipText(""); // NOI18N + } + + return this; + } + + } + + private static class DatabaseConnectionComparator implements Comparator { + + public int compare(Object o1, Object o2) { + DatabaseConnection ds1 = (DatabaseConnection)o1; + DatabaseConnection ds2 = (DatabaseConnection)o2; + + if (ds1 == null) { + return ds2 == null ? 0 : -1; + } + else { + if (ds2 == null) { + return 1; + } + else { + String dispName1 = ds1.getDisplayName(); + String dispName2 = ds2.getDisplayName(); + if (dispName1 == null) { + return dispName2 == null ? 0 : -1; + } + else { + return dispName2 == null ? 1 : dispName1.compareToIgnoreCase(dispName2); + } + } + } + } + } + + private DatabaseExplorerUIs() { + } + + /** + * 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. + * + *

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