KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > util > DatabaseExplorerInternalUIs


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.modules.db.util;
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.JDBCDriverManager;
33 import org.netbeans.api.db.explorer.JDBCDriver;
34 import org.netbeans.modules.db.explorer.driver.JDBCDriverSupport;
35 import org.openide.util.NbBundle;
36
37 /**
38  *
39  * @author Andrei Badea
40  */

41 public final class DatabaseExplorerInternalUIs {
42
43     private DatabaseExplorerInternalUIs() {
44     }
45
46     public static void connect(JComboBox JavaDoc comboBox, JDBCDriverManager driverManager) {
47         connect(comboBox, driverManager, null);
48     }
49
50     public static void connect(JComboBox JavaDoc comboBox, JDBCDriverManager driverManager, String JavaDoc driverClass) {
51         DataComboBoxSupport.connect(comboBox, new DriverDataComboBoxModel(driverManager, driverClass), driverClass == null);
52     }
53
54     private static final class DriverDataComboBoxModel implements DataComboBoxModel {
55
56         private final JDBCDriverManager driverManager;
57         private final DriverComboBoxModel comboBoxModel;
58
59         public DriverDataComboBoxModel(JDBCDriverManager driverManager, String JavaDoc driverClass) {
60             this.driverManager = driverManager;
61             this.comboBoxModel = new DriverComboBoxModel(driverManager, driverClass);
62         }
63
64         public String JavaDoc getItemTooltipText(Object JavaDoc item) {
65             return ((JDBCDriver)item).toString();
66         }
67
68         public String JavaDoc getItemDisplayName(Object JavaDoc item) {
69             return ((JDBCDriver)item).getDisplayName();
70         }
71
72         public void newItemActionPerformed() {
73             Set JavaDoc oldDrivers = new HashSet JavaDoc(Arrays.asList(driverManager.getDrivers()));
74             driverManager.showAddDriverDialog();
75
76             // try to find the new driver
77
JDBCDriver[] newDrivers = driverManager.getDrivers();
78             if (newDrivers.length == oldDrivers.size()) {
79                 // no new driver, so...
80
return;
81             }
82             for (int i = 0; i < newDrivers.length; i++) {
83                 if (!oldDrivers.contains(newDrivers[i])) {
84                     comboBoxModel.addSelectedDriver(newDrivers[i]);
85                     break;
86                 }
87             }
88         }
89
90         public String JavaDoc getNewItemDisplayName() {
91             return NbBundle.getMessage(DatabaseExplorerInternalUIs.class, "LBL_NewDriver");
92         }
93
94         public ComboBoxModel JavaDoc getListModel() {
95             return comboBoxModel;
96         }
97     }
98
99     private static final class DriverComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
100
101         private final JDBCDriverManager driverManager;
102         private final List JavaDoc driverList; // must be ArrayList
103

104         private Object JavaDoc selectedItem; // can be anything, not just a database driver
105

106         public DriverComboBoxModel(JDBCDriverManager driverManager, String JavaDoc driverClass) {
107             this.driverManager = driverManager;
108
109             driverList = new ArrayList JavaDoc();
110             JDBCDriver[] drivers;
111             if (driverClass != null) {
112                 drivers = driverManager.getDrivers(driverClass);
113             } else {
114                 drivers = driverManager.getDrivers();
115             }
116             for (int i = 0; i < drivers.length; i++) {
117                 if (JDBCDriverSupport.isAvailable(drivers[i])) {
118                     driverList.add(drivers[i]);
119                 }
120             }
121             Collections.sort(driverList, new DriverComparator());
122         }
123
124         public void setSelectedItem(Object JavaDoc anItem) {
125             selectedItem = anItem;
126         }
127
128         public Object JavaDoc getElementAt(int index) {
129             return driverList.get(index);
130         }
131
132         public int getSize() {
133             return driverList.size();
134         }
135
136         public Object JavaDoc getSelectedItem() {
137             return selectedItem;
138         }
139
140         public void addSelectedDriver(JDBCDriver dbconn) {
141             selectedItem = dbconn;
142             driverList.add(dbconn);
143             Collections.sort(driverList, new DriverComparator());
144             fireContentsChanged(this, 0, driverList.size());
145         }
146     }
147
148     private static final class DriverComparator implements Comparator JavaDoc {
149
150         public boolean equals(Object JavaDoc that) {
151             return that instanceof DriverComparator;
152         }
153
154         public int compare(Object JavaDoc driver1, Object JavaDoc driver2) {
155             if (driver1 == null) {
156                 return driver2 == null ? 0 : -1;
157             } else {
158                 if (driver2 == null) {
159                     return 1;
160                 }
161             }
162
163             String JavaDoc dispName1 = ((JDBCDriver)driver1).getDisplayName();
164             String JavaDoc dispName2 = ((JDBCDriver)driver2).getDisplayName();
165             if (dispName1 == null) {
166                 return dispName2 == null ? 0 : -1;
167             } else {
168                 return dispName2 == null ? 1 : dispName1.compareToIgnoreCase(dispName2);
169             }
170         }
171     }
172 }
173
Popular Tags