Lines 41-69
Link Here
|
41 |
* Version 2 license, then the option applies only if the new code is |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
43 |
*/ |
44 |
|
|
|
45 |
package org.netbeans.api.db.explorer.support; |
44 |
package org.netbeans.api.db.explorer.support; |
46 |
|
45 |
|
47 |
import java.util.ArrayList; |
46 |
import java.util.ArrayList; |
48 |
import java.util.Arrays; |
47 |
import java.util.Arrays; |
49 |
import java.util.Collections; |
48 |
import java.util.Collections; |
50 |
import java.util.Comparator; |
49 |
import java.util.Comparator; |
51 |
import java.util.HashSet; |
|
|
52 |
import java.util.List; |
50 |
import java.util.List; |
53 |
import java.util.Set; |
|
|
54 |
import javax.swing.AbstractListModel; |
51 |
import javax.swing.AbstractListModel; |
55 |
import javax.swing.ComboBoxModel; |
52 |
import javax.swing.ComboBoxModel; |
56 |
import javax.swing.JComboBox; |
53 |
import javax.swing.JComboBox; |
|
|
54 |
import javax.swing.SwingUtilities; |
55 |
import org.netbeans.api.db.explorer.ConnectionListener; |
57 |
import org.netbeans.api.db.explorer.ConnectionManager; |
56 |
import org.netbeans.api.db.explorer.ConnectionManager; |
58 |
import org.netbeans.api.db.explorer.DatabaseConnection; |
57 |
import org.netbeans.api.db.explorer.DatabaseConnection; |
59 |
import org.netbeans.modules.db.util.DataComboBoxModel; |
58 |
import org.netbeans.modules.db.util.DataComboBoxModel; |
60 |
import org.netbeans.modules.db.util.DataComboBoxSupport; |
59 |
import org.netbeans.modules.db.util.DataComboBoxSupport; |
61 |
import org.openide.util.NbBundle; |
60 |
import org.openide.util.NbBundle; |
|
|
61 |
import org.openide.util.WeakListeners; |
62 |
|
62 |
|
63 |
/** |
63 |
/** |
64 |
* This class contains utility methods for working with and/or displaying |
64 |
* This class contains utility methods for working with and/or displaying |
65 |
* database connections in the UI. Currently it provides a method for |
65 |
* database connections in the UI. Currently it provides a method for populating |
66 |
* populating a combo box with the list of database connections from |
66 |
* a combo box with the list of database connections from |
67 |
* {@link ConnectionManager}. |
67 |
* {@link ConnectionManager}. |
68 |
* |
68 |
* |
69 |
* @author Andrei Badea |
69 |
* @author Andrei Badea |
Lines 77-90
Link Here
|
77 |
|
77 |
|
78 |
/** |
78 |
/** |
79 |
* Populates and manages the contents of the passed combo box. The combo box |
79 |
* Populates and manages the contents of the passed combo box. The combo box |
80 |
* contents consists of the database connections defined in |
80 |
* contents consists of the database connections defined in the passes |
81 |
* the passes instance of {@link ConnectionManager} and a Add Database Connection |
81 |
* instance of {@link ConnectionManager} and a Add Database Connection item |
82 |
* item which displays the New Database Connection dialog when selected. |
82 |
* which displays the New Database Connection dialog when selected. |
83 |
* |
83 |
* |
84 |
* <p>This method may cause the replacement of the combo box model, |
84 |
* <p>This method may cause the replacement of the combo box model, thus the |
85 |
* thus the caller is recommended to register a |
85 |
* caller is recommended to register a |
86 |
* {@link java.beans.PropertyChangeListener} on the combo box when |
86 |
* {@link java.beans.PropertyChangeListener} on the combo box when it needs |
87 |
* it needs to check the combo box content when it changes.</p> |
87 |
* to check the combo box content when it changes.</p> |
88 |
* |
88 |
* |
89 |
* @param comboBox combo box to be filled with the database connections. |
89 |
* @param comboBox combo box to be filled with the database connections. |
90 |
*/ |
90 |
*/ |
Lines 94-137
Link Here
|
94 |
|
94 |
|
95 |
private static final class ConnectionDataComboBoxModel implements DataComboBoxModel { |
95 |
private static final class ConnectionDataComboBoxModel implements DataComboBoxModel { |
96 |
|
96 |
|
97 |
private final ConnectionManager connectionManager; |
|
|
98 |
private final ConnectionComboBoxModel comboBoxModel; |
97 |
private final ConnectionComboBoxModel comboBoxModel; |
99 |
|
98 |
|
100 |
public ConnectionDataComboBoxModel(ConnectionManager connectionManager) { |
99 |
public ConnectionDataComboBoxModel(ConnectionManager connectionManager) { |
101 |
this.connectionManager = connectionManager; |
|
|
102 |
this.comboBoxModel = new ConnectionComboBoxModel(connectionManager); |
100 |
this.comboBoxModel = new ConnectionComboBoxModel(connectionManager); |
103 |
} |
101 |
} |
104 |
|
102 |
|
|
|
103 |
@Override |
105 |
public String getItemTooltipText(Object item) { |
104 |
public String getItemTooltipText(Object item) { |
106 |
return ((DatabaseConnection)item).toString(); |
105 |
return ((DatabaseConnection) item).toString(); |
107 |
} |
106 |
} |
108 |
|
107 |
|
|
|
108 |
@Override |
109 |
public String getItemDisplayName(Object item) { |
109 |
public String getItemDisplayName(Object item) { |
110 |
return ((DatabaseConnection)item).getDisplayName(); |
110 |
return ((DatabaseConnection) item).getDisplayName(); |
111 |
} |
111 |
} |
112 |
|
112 |
|
|
|
113 |
@Override |
113 |
public void newItemActionPerformed() { |
114 |
public void newItemActionPerformed() { |
114 |
Set oldConnections = new HashSet(Arrays.asList(connectionManager.getConnections())); |
|
|
115 |
connectionManager.showAddConnectionDialog(null); |
116 |
|
117 |
// try to find the new connection |
118 |
DatabaseConnection[] newConnections = connectionManager.getConnections(); |
119 |
if (newConnections.length == oldConnections.size()) { |
120 |
// no new connection, so... |
121 |
return; |
122 |
} |
115 |
} |
123 |
for (int i = 0; i < newConnections.length; i++) { |
|
|
124 |
if (!oldConnections.contains(newConnections[i])) { |
125 |
comboBoxModel.addSelectedConnection(newConnections[i]); |
126 |
break; |
127 |
} |
128 |
} |
129 |
} |
130 |
|
116 |
|
|
|
117 |
@Override |
131 |
public String getNewItemDisplayName() { |
118 |
public String getNewItemDisplayName() { |
132 |
return NbBundle.getMessage(DatabaseExplorerUIs.class, "LBL_NewDbConnection"); |
119 |
return NbBundle.getMessage(DatabaseExplorerUIs.class, "LBL_NewDbConnection"); |
133 |
} |
120 |
} |
134 |
|
121 |
|
|
|
122 |
@Override |
135 |
public ComboBoxModel getListModel() { |
123 |
public ComboBoxModel getListModel() { |
136 |
return comboBoxModel; |
124 |
return comboBoxModel; |
137 |
} |
125 |
} |
Lines 140-187
Link Here
|
140 |
private static final class ConnectionComboBoxModel extends AbstractListModel implements ComboBoxModel { |
128 |
private static final class ConnectionComboBoxModel extends AbstractListModel implements ComboBoxModel { |
141 |
|
129 |
|
142 |
private final ConnectionManager connectionManager; |
130 |
private final ConnectionManager connectionManager; |
143 |
private final List connectionList; // must be ArrayList |
131 |
private final List connectionList = new ArrayList(); |
144 |
|
|
|
145 |
private Object selectedItem; // can be anything, not just a database connection |
132 |
private Object selectedItem; // can be anything, not just a database connection |
|
|
133 |
private ConnectionListener cl = new ConnectionListener() { |
134 |
@Override |
135 |
public void connectionsChanged() { |
136 |
updateConnectionList(); |
137 |
} |
138 |
}; |
146 |
|
139 |
|
147 |
public ConnectionComboBoxModel(ConnectionManager connectionManager) { |
140 |
public ConnectionComboBoxModel(ConnectionManager connectionManager) { |
148 |
this.connectionManager = connectionManager; |
141 |
this.connectionManager = connectionManager; |
|
|
142 |
connectionManager.addConnectionListener(WeakListeners.create(ConnectionListener.class, cl, connectionManager)); |
143 |
updateConnectionList(); |
144 |
} |
149 |
|
145 |
|
150 |
connectionList = new ArrayList(); |
146 |
private void updateConnectionList() { |
|
|
147 |
Runnable r = new Runnable() { |
148 |
@Override |
149 |
public void run() { |
150 |
int oldLength = connectionList.size(); |
151 |
connectionList.clear(); |
151 |
connectionList.addAll(Arrays.asList(connectionManager.getConnections())); |
152 |
connectionList.addAll(Arrays.asList(connectionManager.getConnections())); |
152 |
Collections.sort(connectionList, new ConnectionComparator()); |
153 |
Collections.sort(connectionList, new ConnectionComparator()); |
|
|
154 |
fireContentsChanged(this, 0, Math.max(connectionList.size(), oldLength)); |
153 |
} |
155 |
} |
|
|
156 |
}; |
154 |
|
157 |
|
|
|
158 |
if (SwingUtilities.isEventDispatchThread()) { |
159 |
r.run(); |
160 |
} else { |
161 |
SwingUtilities.invokeLater(r); |
162 |
} |
163 |
} |
164 |
|
165 |
@Override |
155 |
public void setSelectedItem(Object anItem) { |
166 |
public void setSelectedItem(Object anItem) { |
156 |
selectedItem = anItem; |
167 |
selectedItem = anItem; |
157 |
} |
168 |
} |
158 |
|
169 |
|
|
|
170 |
@Override |
159 |
public Object getElementAt(int index) { |
171 |
public Object getElementAt(int index) { |
160 |
return connectionList.get(index); |
172 |
return connectionList.get(index); |
161 |
} |
173 |
} |
162 |
|
174 |
|
|
|
175 |
@Override |
163 |
public int getSize() { |
176 |
public int getSize() { |
164 |
return connectionList.size(); |
177 |
return connectionList.size(); |
165 |
} |
178 |
} |
166 |
|
179 |
|
|
|
180 |
@Override |
167 |
public Object getSelectedItem() { |
181 |
public Object getSelectedItem() { |
168 |
return selectedItem; |
182 |
return selectedItem; |
169 |
} |
183 |
} |
170 |
|
|
|
171 |
public void addSelectedConnection(DatabaseConnection dbconn) { |
172 |
selectedItem = dbconn; |
173 |
connectionList.add(dbconn); |
174 |
Collections.sort(connectionList, new ConnectionComparator()); |
175 |
fireContentsChanged(this, 0, connectionList.size()); |
176 |
} |
184 |
} |
177 |
} |
|
|
178 |
|
185 |
|
179 |
private static final class ConnectionComparator implements Comparator { |
186 |
private static final class ConnectionComparator implements Comparator { |
180 |
|
187 |
@Override |
181 |
public boolean equals(Object that) { |
188 |
public boolean equals(Object that) { |
182 |
return that instanceof ConnectionComparator; |
189 |
return that instanceof ConnectionComparator; |
183 |
} |
190 |
} |
184 |
|
191 |
|
|
|
192 |
@Override |
185 |
public int compare(Object dbconn1, Object dbconn2) { |
193 |
public int compare(Object dbconn1, Object dbconn2) { |
186 |
if (dbconn1 == null) { |
194 |
if (dbconn1 == null) { |
187 |
return dbconn2 == null ? 0 : -1; |
195 |
return dbconn2 == null ? 0 : -1; |
Lines 191-198
Link Here
|
191 |
} |
199 |
} |
192 |
} |
200 |
} |
193 |
|
201 |
|
194 |
String dispName1 = ((DatabaseConnection)dbconn1).getDisplayName(); |
202 |
String dispName1 = ((DatabaseConnection) dbconn1).getDisplayName(); |
195 |
String dispName2 = ((DatabaseConnection)dbconn2).getDisplayName(); |
203 |
String dispName2 = ((DatabaseConnection) dbconn2).getDisplayName(); |
196 |
if (dispName1 == null) { |
204 |
if (dispName1 == null) { |
197 |
return dispName2 == null ? 0 : -1; |
205 |
return dispName2 == null ? 0 : -1; |
198 |
} else { |
206 |
} else { |