svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / jdbc / DefaultJDBCConnectionPanel.java @ 43020
History | View | Annotate | Download (12.6 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.swing.impl.jdbc; |
7 |
|
8 |
import java.awt.Dimension; |
9 |
import java.awt.event.ItemEvent; |
10 |
import java.awt.event.ItemListener; |
11 |
import java.util.Iterator; |
12 |
import java.util.List; |
13 |
import javax.swing.ComboBoxModel; |
14 |
import javax.swing.JComponent; |
15 |
import javax.swing.JLabel; |
16 |
import javax.swing.JTextField; |
17 |
import javax.swing.plaf.basic.BasicHTML; |
18 |
import javax.swing.text.View; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.gvsig.fmap.dal.DALLocator; |
21 |
import org.gvsig.fmap.dal.DataFactory; |
22 |
import org.gvsig.fmap.dal.DataManager; |
23 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
24 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
25 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
26 |
import org.gvsig.fmap.dal.exception.DataException; |
27 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
28 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.i18n.I18nManager; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
// org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
35 |
public class DefaultJDBCConnectionPanel extends DefaultJDBCConnectionPanelView implements JDBCConnectionPanel{ |
36 |
|
37 |
private static final Logger |
38 |
logger = LoggerFactory.getLogger(DefaultJDBCConnectionPanel.class); |
39 |
private JDBCServerExplorerParameters forcedParameters;
|
40 |
|
41 |
@Override
|
42 |
public JComponent asJComponent() { |
43 |
return this; |
44 |
} |
45 |
|
46 |
private static class ServerExplorerParametersComboItem { |
47 |
|
48 |
private JDBCServerExplorerParameters params;
|
49 |
private String label; |
50 |
|
51 |
public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) { |
52 |
this.params = params;
|
53 |
this.label = label;
|
54 |
} |
55 |
|
56 |
public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
|
57 |
this(params.getExplorerName(), params);
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public String toString() { |
62 |
return this.label; |
63 |
} |
64 |
|
65 |
public JDBCServerExplorerParameters getParams() {
|
66 |
return this.params; |
67 |
} |
68 |
|
69 |
public String getLabel() { |
70 |
return this.label; |
71 |
} |
72 |
} |
73 |
|
74 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
75 |
public DefaultJDBCConnectionPanel() {
|
76 |
initComponents(); |
77 |
} |
78 |
|
79 |
protected void initComponents() { |
80 |
this.cboConnections.setEditable(true); |
81 |
this.cboConnections.addItemListener(new ItemListener() { |
82 |
@Override
|
83 |
public void itemStateChanged(ItemEvent e) { |
84 |
onChangeConnection(); |
85 |
} |
86 |
}); |
87 |
this.cboConnectors.addItemListener(new ItemListener() { |
88 |
@Override
|
89 |
public void itemStateChanged(ItemEvent e) { |
90 |
onChangeConnector(); |
91 |
} |
92 |
}); |
93 |
try {
|
94 |
fillConnections(); |
95 |
fillConnectors(); |
96 |
} catch(Throwable th) { |
97 |
// Ignore it to allow use in GUI builders
|
98 |
logger.warn("Can't fill combos",th);
|
99 |
} |
100 |
this.translate();
|
101 |
adjustPreferedHeight(this.lblFoother,300); |
102 |
} |
103 |
|
104 |
private void adjustPreferedHeight(JLabel label, int prefWidth) { |
105 |
JLabel resizer = new JLabel(); |
106 |
resizer.setText(label.getText()); |
107 |
View view = (View) resizer.getClientProperty(BasicHTML.propertyKey); |
108 |
view.setSize(prefWidth,15);
|
109 |
float w = view.getPreferredSpan(View.X_AXIS); |
110 |
float h = view.getPreferredSpan(View.Y_AXIS); |
111 |
Dimension dim = new Dimension( |
112 |
(int) Math.ceil(w), |
113 |
(int) Math.ceil(h) |
114 |
); |
115 |
label.setPreferredSize(dim); |
116 |
} |
117 |
|
118 |
private void translate() { |
119 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
120 |
|
121 |
this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
122 |
this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
123 |
this.lblServer.setText(i18nManager.getTranslation("_Host")); |
124 |
this.lblPort.setText(i18nManager.getTranslation("_Port")); |
125 |
this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
126 |
this.lblUsername.setText(i18nManager.getTranslation("_User")); |
127 |
this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
128 |
this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
133 |
this.forcedParameters = (JDBCServerExplorerParameters) parameters.getCopy();
|
134 |
|
135 |
int indexConnector = this.getIndexOfConnector(parameters); |
136 |
if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
137 |
this.cboConnectors.setSelectedIndex(indexConnector);
|
138 |
} |
139 |
|
140 |
this.txtServer.setText(parameters.getHost());
|
141 |
Integer port = parameters.getPort();
|
142 |
if ( port == null ) { |
143 |
this.txtPort.setText(""); |
144 |
} else {
|
145 |
this.txtPort.setText(String.valueOf(port)); |
146 |
} |
147 |
this.txtDataBase.setText(parameters.getDBName());
|
148 |
this.txtUsername.setText(parameters.getUser());
|
149 |
this.txtPassword.setText(parameters.getPassword());
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
154 |
JDBCServerExplorerParameters params; |
155 |
JDBCServerExplorerParameters connector = this.getConnector();
|
156 |
if( this.forcedParameters==null ) { |
157 |
params = (JDBCServerExplorerParameters) connector.getCopy(); |
158 |
} else {
|
159 |
params = (JDBCServerExplorerParameters) this.forcedParameters.getCopy();
|
160 |
} |
161 |
String s = this.getServer(); |
162 |
if( s!=null ) { |
163 |
params.setHost(s); |
164 |
} |
165 |
int n = this.getPort(); |
166 |
if( n>0 ) { |
167 |
params.setPort(n); |
168 |
} |
169 |
s = this.getDataBaseName();
|
170 |
if( s!=null ) { |
171 |
params.setDBName(s); |
172 |
} |
173 |
s = this.getUsername();
|
174 |
if( s!=null ) { |
175 |
params.setUser(s); |
176 |
} |
177 |
s = this.getPassword();
|
178 |
if( s!=null ) { |
179 |
params.setPassword(s); |
180 |
} |
181 |
|
182 |
if ( this.getConnectionName() != null ) { |
183 |
DataManager dataManager = DALLocator.getDataManager(); |
184 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
185 |
pool.add(this.getConnectionName(), params);
|
186 |
} |
187 |
return params;
|
188 |
} |
189 |
|
190 |
protected void setConnectionName(String connectionName) { |
191 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
192 |
txtConnections.setText(connectionName); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public String getConnectionName() { |
197 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
198 |
String value = txtConnections.getText();
|
199 |
return (String) StringUtils.defaultIfBlank(value, null); |
200 |
} |
201 |
|
202 |
protected JDBCServerExplorerParameters getConnector() {
|
203 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
204 |
JDBCServerExplorerParameters value = item.getParams(); |
205 |
return value;
|
206 |
} |
207 |
|
208 |
protected String getConnectorName() { |
209 |
JDBCServerExplorerParameters value = this.getConnector();
|
210 |
if ( value == null ) { |
211 |
return null; |
212 |
} |
213 |
return (String) StringUtils.defaultIfBlank(value.getExplorerName(), null); |
214 |
} |
215 |
|
216 |
protected String getServer() { |
217 |
return (String) StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
218 |
} |
219 |
|
220 |
protected int getPort() { |
221 |
String svalue = (String) StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
222 |
int ivalue;
|
223 |
try {
|
224 |
ivalue = Integer.parseInt(svalue);
|
225 |
} catch (Exception ex) { |
226 |
ivalue = -1;
|
227 |
} |
228 |
return ivalue;
|
229 |
} |
230 |
|
231 |
protected String getDataBaseName() { |
232 |
return (String) StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
233 |
} |
234 |
|
235 |
protected String getUsername() { |
236 |
return (String) StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
237 |
} |
238 |
|
239 |
protected String getPassword() { |
240 |
return (String) StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
241 |
} |
242 |
|
243 |
private void onChangeConnector() { |
244 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
245 |
if( item==null ) { |
246 |
return;
|
247 |
} |
248 |
JDBCServerExplorerParameters connector = item.getParams(); |
249 |
|
250 |
if ( connector == null ) { |
251 |
return;
|
252 |
} |
253 |
this.setServerExplorerParameters(connector);
|
254 |
} |
255 |
|
256 |
private void onChangeConnection() { |
257 |
Object item = this.cboConnections.getSelectedItem(); |
258 |
if ( item instanceof ServerExplorerParametersComboItem ) { |
259 |
JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams(); |
260 |
if ( connection == null ) { |
261 |
return;
|
262 |
} |
263 |
this.setServerExplorerParameters(connection);
|
264 |
} |
265 |
} |
266 |
|
267 |
|
268 |
private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
269 |
String code = null; |
270 |
try {
|
271 |
code = explorerParameters.toString(); |
272 |
ComboBoxModel model = this.cboConnectors.getModel(); |
273 |
for ( int i = 0; i < model.getSize(); i++ ) { |
274 |
ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i); |
275 |
if ( x.getLabel().equalsIgnoreCase(explorerParameters.getExplorerName()) ) {
|
276 |
return i;
|
277 |
} |
278 |
} |
279 |
} catch (Exception ex) { |
280 |
logger.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
281 |
} |
282 |
return -1; |
283 |
} |
284 |
|
285 |
private void fillConnectors() { |
286 |
DataManager dataManager = DALLocator.getDataManager(); |
287 |
|
288 |
ServerExplorerParametersComboItem last = null;
|
289 |
|
290 |
for (DataFactory factory : dataManager.getServerExplorerRegister() ) {
|
291 |
DataServerExplorerParameters params = (DataServerExplorerParameters) factory.createParameters(); |
292 |
if ( params instanceof JDBCServerExplorerParameters ) { |
293 |
if( params.getClass() == JDBCServerExplorerParameters.class &&
|
294 |
last == null ) {
|
295 |
// Si es el proveedor generico de JDBC, lo guarda para
|
296 |
// a?adirlo al final del combo.
|
297 |
last = new ServerExplorerParametersComboItem(
|
298 |
factory.getDescription(), |
299 |
(JDBCServerExplorerParameters) params |
300 |
); |
301 |
} else {
|
302 |
this.cboConnectors.addItem(
|
303 |
new ServerExplorerParametersComboItem(
|
304 |
factory.getDescription(), |
305 |
(JDBCServerExplorerParameters) params |
306 |
) |
307 |
); |
308 |
} |
309 |
} |
310 |
} |
311 |
if( last!=null ) { |
312 |
this.cboConnectors.addItem(last);
|
313 |
} |
314 |
} |
315 |
|
316 |
private void fillConnections() { |
317 |
DataManager dataManager = DALLocator.getDataManager(); |
318 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
319 |
|
320 |
DataServerExplorerParameters params; |
321 |
|
322 |
Iterator it = pool.iterator();
|
323 |
while ( it.hasNext() ) {
|
324 |
DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) it.next(); |
325 |
if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
326 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
327 |
this.cboConnections.addItem(
|
328 |
new ServerExplorerParametersComboItem(entry.getName(), dbParams)
|
329 |
); |
330 |
} |
331 |
} |
332 |
this.cboConnections.setSelectedIndex(-1); |
333 |
} |
334 |
|
335 |
@Override
|
336 |
public void delete() { |
337 |
String name = this.getConnectionName(); |
338 |
DataManager dataManager = DALLocator.getDataManager(); |
339 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
340 |
|
341 |
pool.remove(name); |
342 |
} |
343 |
|
344 |
@Override
|
345 |
public void clear() { |
346 |
this.cboConnections.setSelectedIndex(-1); |
347 |
this.cboConnectors.setSelectedIndex(-1); |
348 |
this.txtServer.setText(""); |
349 |
this.txtPort.setText(""); |
350 |
this.txtDataBase.setText(""); |
351 |
this.txtUsername.setText(""); |
352 |
this.txtPassword.setText(""); |
353 |
} |
354 |
} |