1 19 20 package org.netbeans.modules.db.util; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Set ; 29 import javax.swing.AbstractListModel ; 30 import javax.swing.ComboBoxModel ; 31 import javax.swing.JComboBox ; 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 41 public final class DatabaseExplorerInternalUIs { 42 43 private DatabaseExplorerInternalUIs() { 44 } 45 46 public static void connect(JComboBox comboBox, JDBCDriverManager driverManager) { 47 connect(comboBox, driverManager, null); 48 } 49 50 public static void connect(JComboBox comboBox, JDBCDriverManager driverManager, String 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 driverClass) { 60 this.driverManager = driverManager; 61 this.comboBoxModel = new DriverComboBoxModel(driverManager, driverClass); 62 } 63 64 public String getItemTooltipText(Object item) { 65 return ((JDBCDriver)item).toString(); 66 } 67 68 public String getItemDisplayName(Object item) { 69 return ((JDBCDriver)item).getDisplayName(); 70 } 71 72 public void newItemActionPerformed() { 73 Set oldDrivers = new HashSet (Arrays.asList(driverManager.getDrivers())); 74 driverManager.showAddDriverDialog(); 75 76 JDBCDriver[] newDrivers = driverManager.getDrivers(); 78 if (newDrivers.length == oldDrivers.size()) { 79 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 getNewItemDisplayName() { 91 return NbBundle.getMessage(DatabaseExplorerInternalUIs.class, "LBL_NewDriver"); 92 } 93 94 public ComboBoxModel getListModel() { 95 return comboBoxModel; 96 } 97 } 98 99 private static final class DriverComboBoxModel extends AbstractListModel implements ComboBoxModel { 100 101 private final JDBCDriverManager driverManager; 102 private final List driverList; 104 private Object selectedItem; 106 public DriverComboBoxModel(JDBCDriverManager driverManager, String driverClass) { 107 this.driverManager = driverManager; 108 109 driverList = new ArrayList (); 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 anItem) { 125 selectedItem = anItem; 126 } 127 128 public Object getElementAt(int index) { 129 return driverList.get(index); 130 } 131 132 public int getSize() { 133 return driverList.size(); 134 } 135 136 public Object 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 { 149 150 public boolean equals(Object that) { 151 return that instanceof DriverComparator; 152 } 153 154 public int compare(Object driver1, Object 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 dispName1 = ((JDBCDriver)driver1).getDisplayName(); 164 String 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 |