KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > db > explorer > support > DatabaseExplorerUIs


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.db.explorer.support;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.AbstractListModel JavaDoc;
30 import javax.swing.ComboBoxModel JavaDoc;
31 import javax.swing.JComboBox JavaDoc;
32 import org.netbeans.api.db.explorer.ConnectionManager;
33 import org.netbeans.api.db.explorer.DatabaseConnection;
34 import org.netbeans.modules.db.util.DataComboBoxModel;
35 import org.netbeans.modules.db.util.DataComboBoxSupport;
36 import org.openide.util.NbBundle;
37
38 /**
39  * This class contains utility methods for working with and/or displaying
40  * database connections in the UI. Currently it provides a method for
41  * populating a combo box with the list of database connections from
42  * {@link ConnectionManager}.
43  *
44  * @author Andrei Badea
45  *
46  * @since 1.18
47  */

48 public final class DatabaseExplorerUIs {
49
50     private DatabaseExplorerUIs() {
51     }
52
53     /**
54      * Populates and manages the contents of the passed combo box. The combo box
55      * contents consists of the database connections defined in
56      * the passes instance of {@link ConnectionManager} and a Add Database Connection
57      * item which displays the New Database Connection dialog when selected.
58      *
59      * <p>This method may cause the replacement of the combo box model,
60      * thus the caller is recommended to register a
61      * {@link java.beans.PropertyChangeListener} on the combo box when
62      * it needs to check the combo box content when it changes.</p>
63      *
64      * @param comboBox combo box to be filled with the database connections.
65      */

66     public static void connect(JComboBox JavaDoc comboBox, ConnectionManager connectionManager) {
67         DataComboBoxSupport.connect(comboBox, new ConnectionDataComboBoxModel(connectionManager));
68     }
69
70     private static final class ConnectionDataComboBoxModel implements DataComboBoxModel {
71
72         private final ConnectionManager connectionManager;
73         private final ConnectionComboBoxModel comboBoxModel;
74
75         public ConnectionDataComboBoxModel(ConnectionManager connectionManager) {
76             this.connectionManager = connectionManager;
77             this.comboBoxModel = new ConnectionComboBoxModel(connectionManager);
78         }
79
80         public String JavaDoc getItemTooltipText(Object JavaDoc item) {
81             return ((DatabaseConnection)item).toString();
82         }
83
84         public String JavaDoc getItemDisplayName(Object JavaDoc item) {
85             return ((DatabaseConnection)item).getDisplayName();
86         }
87
88         public void newItemActionPerformed() {
89             Set JavaDoc oldConnections = new HashSet JavaDoc(Arrays.asList(connectionManager.getConnections()));
90             connectionManager.showAddConnectionDialog(null);
91
92             // try to find the new connection
93
DatabaseConnection[] newConnections = connectionManager.getConnections();
94             if (newConnections.length == oldConnections.size()) {
95                 // no new connection, so...
96
return;
97             }
98             for (int i = 0; i < newConnections.length; i++) {
99                 if (!oldConnections.contains(newConnections[i])) {
100                     comboBoxModel.addSelectedConnection(newConnections[i]);
101                     break;
102                 }
103             }
104         }
105
106         public String JavaDoc getNewItemDisplayName() {
107             return NbBundle.getMessage(DatabaseExplorerUIs.class, "LBL_NewDbConnection");
108         }
109
110         public ComboBoxModel JavaDoc getListModel() {
111             return comboBoxModel;
112         }
113     }
114
115     private static final class ConnectionComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
116
117         private final ConnectionManager connectionManager;
118         private final List JavaDoc connectionList; // must be ArrayList
119

120         private Object JavaDoc selectedItem; // can be anything, not just a database connection
121

122         public ConnectionComboBoxModel(ConnectionManager connectionManager) {
123             this.connectionManager = connectionManager;
124
125             connectionList = new ArrayList JavaDoc();
126             connectionList.addAll(Arrays.asList(connectionManager.getConnections()));
127             Collections.sort(connectionList, new ConnectionComparator());
128         }
129
130         public void setSelectedItem(Object JavaDoc anItem) {
131             selectedItem = anItem;
132         }
133
134         public Object JavaDoc getElementAt(int index) {
135             return connectionList.get(index);
136         }
137
138         public int getSize() {
139             return connectionList.size();
140         }
141
142         public Object JavaDoc getSelectedItem() {
143             return selectedItem;
144         }
145
146         public void addSelectedConnection(DatabaseConnection dbconn) {
147             selectedItem = dbconn;
148             connectionList.add(dbconn);
149             Collections.sort(connectionList, new ConnectionComparator());
150             fireContentsChanged(this, 0, connectionList.size());
151         }
152     }
153
154     private static final class ConnectionComparator implements Comparator JavaDoc {
155
156         public boolean equals(Object JavaDoc that) {
157             return that instanceof ConnectionComparator;
158         }
159
160         public int compare(Object JavaDoc dbconn1, Object JavaDoc dbconn2) {
161             if (dbconn1 == null) {
162                 return dbconn2 == null ? 0 : -1;
163             } else {
164                 if (dbconn2 == null) {
165                     return 1;
166                 }
167             }
168
169             String JavaDoc dispName1 = ((DatabaseConnection)dbconn1).getDisplayName();
170             String JavaDoc dispName2 = ((DatabaseConnection)dbconn2).getDisplayName();
171             if (dispName1 == null) {
172                 return dispName2 == null ? 0 : -1;
173             } else {
174                 return dispName2 == null ? 1 : dispName1.compareToIgnoreCase(dispName2);
175             }
176         }
177     }
178 }
179
Popular Tags