001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.util;
043:
044: import java.util.ArrayList;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.HashSet;
049: import java.util.List;
050: import java.util.Set;
051: import javax.swing.AbstractListModel;
052: import javax.swing.ComboBoxModel;
053: import javax.swing.JComboBox;
054: import org.netbeans.api.db.explorer.JDBCDriverManager;
055: import org.netbeans.api.db.explorer.JDBCDriver;
056: import org.netbeans.modules.db.explorer.driver.JDBCDriverSupport;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Andrei Badea
062: */
063: public final class DatabaseExplorerInternalUIs {
064:
065: private DatabaseExplorerInternalUIs() {
066: }
067:
068: public static void connect(JComboBox comboBox,
069: JDBCDriverManager driverManager) {
070: connect(comboBox, driverManager, null);
071: }
072:
073: public static void connect(JComboBox comboBox,
074: JDBCDriverManager driverManager, String driverClass) {
075: DataComboBoxSupport
076: .connect(comboBox, new DriverDataComboBoxModel(
077: driverManager, driverClass),
078: driverClass == null);
079: }
080:
081: private static final class DriverDataComboBoxModel implements
082: DataComboBoxModel {
083:
084: private final JDBCDriverManager driverManager;
085: private final DriverComboBoxModel comboBoxModel;
086:
087: public DriverDataComboBoxModel(JDBCDriverManager driverManager,
088: String driverClass) {
089: this .driverManager = driverManager;
090: this .comboBoxModel = new DriverComboBoxModel(driverManager,
091: driverClass);
092: }
093:
094: public String getItemTooltipText(Object item) {
095: return ((JDBCDriver) item).toString();
096: }
097:
098: public String getItemDisplayName(Object item) {
099: return ((JDBCDriver) item).getDisplayName();
100: }
101:
102: public void newItemActionPerformed() {
103: Set oldDrivers = new HashSet(Arrays.asList(driverManager
104: .getDrivers()));
105: driverManager.showAddDriverDialog();
106:
107: // try to find the new driver
108: JDBCDriver[] newDrivers = driverManager.getDrivers();
109: if (newDrivers.length == oldDrivers.size()) {
110: // no new driver, so...
111: return;
112: }
113: for (int i = 0; i < newDrivers.length; i++) {
114: if (!oldDrivers.contains(newDrivers[i])) {
115: comboBoxModel.addSelectedDriver(newDrivers[i]);
116: break;
117: }
118: }
119: }
120:
121: public String getNewItemDisplayName() {
122: return NbBundle.getMessage(
123: DatabaseExplorerInternalUIs.class, "LBL_NewDriver");
124: }
125:
126: public ComboBoxModel getListModel() {
127: return comboBoxModel;
128: }
129: }
130:
131: private static final class DriverComboBoxModel extends
132: AbstractListModel implements ComboBoxModel {
133:
134: private final JDBCDriverManager driverManager;
135: private final List driverList; // must be ArrayList
136:
137: private Object selectedItem; // can be anything, not just a database driver
138:
139: public DriverComboBoxModel(JDBCDriverManager driverManager,
140: String driverClass) {
141: this .driverManager = driverManager;
142:
143: driverList = new ArrayList();
144: JDBCDriver[] drivers;
145: if (driverClass != null) {
146: drivers = driverManager.getDrivers(driverClass);
147: } else {
148: drivers = driverManager.getDrivers();
149: }
150: for (int i = 0; i < drivers.length; i++) {
151: if (JDBCDriverSupport.isAvailable(drivers[i])) {
152: driverList.add(drivers[i]);
153: }
154: }
155: Collections.sort(driverList, new DriverComparator());
156: }
157:
158: public void setSelectedItem(Object anItem) {
159: selectedItem = anItem;
160: }
161:
162: public Object getElementAt(int index) {
163: return driverList.get(index);
164: }
165:
166: public int getSize() {
167: return driverList.size();
168: }
169:
170: public Object getSelectedItem() {
171: return selectedItem;
172: }
173:
174: public void addSelectedDriver(JDBCDriver dbconn) {
175: selectedItem = dbconn;
176: driverList.add(dbconn);
177: Collections.sort(driverList, new DriverComparator());
178: fireContentsChanged(this , 0, driverList.size());
179: }
180: }
181:
182: private static final class DriverComparator implements Comparator {
183:
184: public boolean equals(Object that) {
185: return that instanceof DriverComparator;
186: }
187:
188: public int compare(Object driver1, Object driver2) {
189: if (driver1 == null) {
190: return driver2 == null ? 0 : -1;
191: } else {
192: if (driver2 == null) {
193: return 1;
194: }
195: }
196:
197: String dispName1 = ((JDBCDriver) driver1).getDisplayName();
198: String dispName2 = ((JDBCDriver) driver2).getDisplayName();
199: if (dispName1 == null) {
200: return dispName2 == null ? 0 : -1;
201: } else {
202: return dispName2 == null ? 1 : dispName1
203: .compareToIgnoreCase(dispName2);
204: }
205: }
206: }
207: }
|