1 19 20 package org.netbeans.api.db.explorer.support; 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.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 48 public final class DatabaseExplorerUIs { 49 50 private DatabaseExplorerUIs() { 51 } 52 53 66 public static void connect(JComboBox 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 getItemTooltipText(Object item) { 81 return ((DatabaseConnection)item).toString(); 82 } 83 84 public String getItemDisplayName(Object item) { 85 return ((DatabaseConnection)item).getDisplayName(); 86 } 87 88 public void newItemActionPerformed() { 89 Set oldConnections = new HashSet (Arrays.asList(connectionManager.getConnections())); 90 connectionManager.showAddConnectionDialog(null); 91 92 DatabaseConnection[] newConnections = connectionManager.getConnections(); 94 if (newConnections.length == oldConnections.size()) { 95 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 getNewItemDisplayName() { 107 return NbBundle.getMessage(DatabaseExplorerUIs.class, "LBL_NewDbConnection"); 108 } 109 110 public ComboBoxModel getListModel() { 111 return comboBoxModel; 112 } 113 } 114 115 private static final class ConnectionComboBoxModel extends AbstractListModel implements ComboBoxModel { 116 117 private final ConnectionManager connectionManager; 118 private final List connectionList; 120 private Object selectedItem; 122 public ConnectionComboBoxModel(ConnectionManager connectionManager) { 123 this.connectionManager = connectionManager; 124 125 connectionList = new ArrayList (); 126 connectionList.addAll(Arrays.asList(connectionManager.getConnections())); 127 Collections.sort(connectionList, new ConnectionComparator()); 128 } 129 130 public void setSelectedItem(Object anItem) { 131 selectedItem = anItem; 132 } 133 134 public Object getElementAt(int index) { 135 return connectionList.get(index); 136 } 137 138 public int getSize() { 139 return connectionList.size(); 140 } 141 142 public Object 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 { 155 156 public boolean equals(Object that) { 157 return that instanceof ConnectionComparator; 158 } 159 160 public int compare(Object dbconn1, Object 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 dispName1 = ((DatabaseConnection)dbconn1).getDisplayName(); 170 String 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 |