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 @ 43377
History | View | Annotate | Download (17.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.ActionEvent; |
10 |
import java.awt.event.ActionListener; |
11 |
import java.awt.event.ItemEvent; |
12 |
import java.awt.event.ItemListener; |
13 |
import java.io.File; |
14 |
import java.util.Iterator; |
15 |
import javax.swing.ComboBoxModel; |
16 |
import javax.swing.JComponent; |
17 |
import javax.swing.JLabel; |
18 |
import javax.swing.JTextField; |
19 |
import javax.swing.SwingUtilities; |
20 |
import javax.swing.plaf.basic.BasicHTML; |
21 |
import javax.swing.text.View; |
22 |
import org.apache.commons.lang3.ArrayUtils; |
23 |
import org.apache.commons.lang3.BooleanUtils; |
24 |
import org.apache.commons.lang3.StringUtils; |
25 |
import org.gvsig.fmap.dal.DALLocator; |
26 |
import org.gvsig.fmap.dal.DataFactory; |
27 |
import org.gvsig.fmap.dal.DataManager; |
28 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
29 |
import org.gvsig.fmap.dal.DataServerExplorerPool; |
30 |
import org.gvsig.fmap.dal.DataServerExplorerPoolEntry; |
31 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
32 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
33 |
import org.gvsig.fmap.dal.swing.jdbc.JDBCConnectionPanel; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dynobject.DynClass; |
36 |
import org.gvsig.tools.dynobject.DynField; |
37 |
import org.gvsig.tools.i18n.I18nManager; |
38 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
39 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
|
43 |
// org.gvsig.fmap.mapcontrol.dal.jdbc.JDBCConnectionPanel
|
44 |
public class DefaultJDBCConnectionPanel extends DefaultJDBCConnectionPanelView implements JDBCConnectionPanel{ |
45 |
|
46 |
private static final Logger logger = LoggerFactory.getLogger(DefaultJDBCConnectionPanel.class); |
47 |
private static final long serialVersionUID = -6119489353793596382L; |
48 |
private JDBCServerExplorerParameters forcedParameters;
|
49 |
private ItemListener onChangeConnectorItemListener; |
50 |
private ConnectorFilter connectorFilter=null; |
51 |
|
52 |
@Override
|
53 |
public JComponent asJComponent() { |
54 |
return this; |
55 |
} |
56 |
|
57 |
private static class ServerExplorerParametersComboItem { |
58 |
|
59 |
private JDBCServerExplorerParameters params;
|
60 |
private String label; |
61 |
|
62 |
public ServerExplorerParametersComboItem(String label, JDBCServerExplorerParameters params) { |
63 |
this.params = params;
|
64 |
this.label = label;
|
65 |
} |
66 |
|
67 |
public ServerExplorerParametersComboItem(JDBCServerExplorerParameters params) {
|
68 |
this(params.getExplorerName(), params);
|
69 |
} |
70 |
|
71 |
@Override
|
72 |
public String toString() { |
73 |
return this.label; |
74 |
} |
75 |
|
76 |
public JDBCServerExplorerParameters getParams() {
|
77 |
return this.params; |
78 |
} |
79 |
|
80 |
public String getLabel() { |
81 |
return this.label; |
82 |
} |
83 |
} |
84 |
|
85 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
86 |
public DefaultJDBCConnectionPanel() {
|
87 |
initComponents(); |
88 |
} |
89 |
|
90 |
protected void initComponents() { |
91 |
this.cboConnections.setEditable(true); |
92 |
this.cboConnections.addItemListener(new ItemListener() { |
93 |
@Override
|
94 |
public void itemStateChanged(ItemEvent e) { |
95 |
if(e.getStateChange() != ItemEvent.SELECTED) { |
96 |
return;
|
97 |
} // Lo hago asi para evitar cuelgues durante la depuracion
|
98 |
// al poner puntos de ruptura en un evento de un combo.
|
99 |
SwingUtilities.invokeLater(new Runnable() { |
100 |
|
101 |
@Override
|
102 |
public void run() { |
103 |
onChangeConnection(); |
104 |
} |
105 |
}); |
106 |
} |
107 |
}); |
108 |
this.onChangeConnectorItemListener = new ItemListener() { |
109 |
@Override
|
110 |
public void itemStateChanged(ItemEvent e) { |
111 |
if(e.getStateChange() != ItemEvent.SELECTED) { |
112 |
return;
|
113 |
} |
114 |
// Lo hago asi para evitar cuelgues durante la depuracion
|
115 |
// al poner puntos de ruptura en un evento de un combo.
|
116 |
SwingUtilities.invokeLater(new Runnable() { |
117 |
|
118 |
@Override
|
119 |
public void run() { |
120 |
onChangeConnector(); |
121 |
} |
122 |
}); |
123 |
} |
124 |
}; |
125 |
this.cboConnectors.addItemListener(onChangeConnectorItemListener);
|
126 |
try {
|
127 |
fillConnections(); |
128 |
fillConnectors(); |
129 |
} catch(Throwable th) { |
130 |
// Ignore it to allow use in GUI builders
|
131 |
logger.warn("Can't fill combos",th);
|
132 |
} |
133 |
this.translate();
|
134 |
this.btnDataBaseBrowser.addActionListener(new ActionListener() { |
135 |
|
136 |
@Override
|
137 |
public void actionPerformed(ActionEvent e) { |
138 |
onBrowseDatabase(); |
139 |
} |
140 |
}); |
141 |
adjustPreferedHeight(this.lblFoother,300); |
142 |
} |
143 |
|
144 |
private void adjustPreferedHeight(JLabel label, int prefWidth) { |
145 |
JLabel resizer = new JLabel(); |
146 |
resizer.setText(label.getText()); |
147 |
View view = (View) resizer.getClientProperty(BasicHTML.propertyKey); |
148 |
view.setSize(prefWidth,15);
|
149 |
float w = view.getPreferredSpan(View.X_AXIS); |
150 |
float h = view.getPreferredSpan(View.Y_AXIS); |
151 |
Dimension dim = new Dimension( |
152 |
(int) Math.ceil(w), |
153 |
(int) Math.ceil(h) |
154 |
); |
155 |
label.setPreferredSize(dim); |
156 |
} |
157 |
|
158 |
private void translate() { |
159 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
160 |
|
161 |
this.lblFile.setText(i18nManager.getTranslation("_File")); |
162 |
this.lblConnectionName.setText(i18nManager.getTranslation("_Connection_name")); |
163 |
this.lblConnector.setText(i18nManager.getTranslation("_Driver_type")); |
164 |
this.lblServer.setText(i18nManager.getTranslation("_Host")); |
165 |
this.lblPort.setText(i18nManager.getTranslation("_Port")); |
166 |
this.lblDataBase.setText(i18nManager.getTranslation("_Database")); |
167 |
this.lblUsername.setText(i18nManager.getTranslation("_User")); |
168 |
this.lblPassword.setText(i18nManager.getTranslation("_Password")); |
169 |
this.lblFoother.setText("<html>"+i18nManager.getTranslation("_JDBCConecctionPanel_foother")+"</html>"); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void setServerExplorerParameters(JDBCServerExplorerParameters parameters) { |
174 |
this.forcedParameters = (JDBCServerExplorerParameters) parameters.getCopy();
|
175 |
|
176 |
int indexConnector = this.getIndexOfConnector(parameters); |
177 |
if ( indexConnector >= 0 && this.cboConnectors.getSelectedIndex()!=indexConnector ) { |
178 |
this.cboConnectors.removeItemListener(this.onChangeConnectorItemListener); |
179 |
this.cboConnectors.setSelectedIndex(indexConnector);
|
180 |
this.cboConnectors.addItemListener(onChangeConnectorItemListener);
|
181 |
} |
182 |
|
183 |
this.txtServer.setText(parameters.getHost());
|
184 |
Integer port = parameters.getPort();
|
185 |
if ( port == null ) { |
186 |
this.txtPort.setText(""); |
187 |
} else {
|
188 |
this.txtPort.setText(String.valueOf(port)); |
189 |
} |
190 |
this.txtDataBase.setText(parameters.getDBName());
|
191 |
this.txtUsername.setText(parameters.getUser());
|
192 |
this.txtPassword.setText(parameters.getPassword());
|
193 |
|
194 |
DynClass definition = parameters.getDynClass(); |
195 |
|
196 |
DynField field = definition.getDynField(JDBCServerExplorerParameters.HOST_PARAMTER_NAME); |
197 |
this.txtServer.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
198 |
field = definition.getDynField(JDBCServerExplorerParameters.PORT_PARAMTER_NAME); |
199 |
this.txtPort.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
200 |
field = definition.getDynField(JDBCServerExplorerParameters.DBNAME_PARAMTER_NAME); |
201 |
this.txtDataBase.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
202 |
field = definition.getDynField(JDBCServerExplorerParameters.USER_PARAMTER_NAME); |
203 |
this.txtUsername.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
204 |
field = definition.getDynField(JDBCServerExplorerParameters.PASSWORD_PARAMTER_NAME); |
205 |
this.txtPassword.setEnabled( !( field.isReadOnly() || field.isHidden() ));
|
206 |
if( parameters instanceof FilesystemStoreParameters ) { |
207 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
208 |
if( f==null ) { |
209 |
this.txtFile.setText(""); |
210 |
} else {
|
211 |
this.txtFile.setText(f.getAbsolutePath());
|
212 |
} |
213 |
this.btnDataBaseBrowser.setVisible(true); |
214 |
this.txtFile.setVisible(true); |
215 |
this.lblFile.setVisible(true); |
216 |
} else {
|
217 |
this.btnDataBaseBrowser.setVisible(false); |
218 |
this.txtFile.setVisible(false); |
219 |
this.lblFile.setVisible(false); |
220 |
} |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public JDBCServerExplorerParameters getServerExplorerParameters() {
|
225 |
JDBCServerExplorerParameters params; |
226 |
JDBCServerExplorerParameters connector = this.getConnector();
|
227 |
if( connector==null ) { |
228 |
return null; |
229 |
} |
230 |
if( this.forcedParameters==null ) { |
231 |
params = (JDBCServerExplorerParameters) connector.getCopy(); |
232 |
} else {
|
233 |
params = (JDBCServerExplorerParameters) this.forcedParameters.getCopy();
|
234 |
} |
235 |
String s = this.getServer(); |
236 |
if( s!=null ) { |
237 |
params.setHost(s); |
238 |
} |
239 |
int n = this.getPort(); |
240 |
if( n>0 ) { |
241 |
params.setPort(n); |
242 |
} |
243 |
s = this.getDataBaseName();
|
244 |
if( s!=null ) { |
245 |
params.setDBName(s); |
246 |
} |
247 |
s = this.getUsername();
|
248 |
if( s!=null ) { |
249 |
params.setUser(s); |
250 |
} |
251 |
s = this.getPassword();
|
252 |
if( s!=null ) { |
253 |
params.setPassword(s); |
254 |
} |
255 |
|
256 |
if ( this.getConnectionName() != null ) { |
257 |
DataManager dataManager = DALLocator.getDataManager(); |
258 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
259 |
pool.add(this.getConnectionName(), params);
|
260 |
} |
261 |
return params;
|
262 |
} |
263 |
|
264 |
protected void setConnectionName(String connectionName) { |
265 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
266 |
txtConnections.setText(connectionName); |
267 |
} |
268 |
|
269 |
@Override
|
270 |
public String getConnectionName() { |
271 |
JTextField txtConnections = (JTextField) this.cboConnections.getEditor().getEditorComponent(); |
272 |
String value = txtConnections.getText();
|
273 |
return (String) StringUtils.defaultIfBlank(value, null); |
274 |
} |
275 |
|
276 |
protected JDBCServerExplorerParameters getConnector() {
|
277 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
278 |
JDBCServerExplorerParameters value = item.getParams(); |
279 |
return value;
|
280 |
} |
281 |
|
282 |
protected String getConnectorName() { |
283 |
JDBCServerExplorerParameters value = this.getConnector();
|
284 |
if ( value == null ) { |
285 |
return null; |
286 |
} |
287 |
return (String) StringUtils.defaultIfBlank(value.getExplorerName(), null); |
288 |
} |
289 |
|
290 |
protected String getServer() { |
291 |
return (String) StringUtils.defaultIfBlank(this.txtServer.getText(), null); |
292 |
} |
293 |
|
294 |
protected int getPort() { |
295 |
String svalue = (String) StringUtils.defaultIfBlank(this.txtPort.getText(), null); |
296 |
int ivalue;
|
297 |
try {
|
298 |
ivalue = Integer.parseInt(svalue);
|
299 |
} catch (Exception ex) { |
300 |
ivalue = -1;
|
301 |
} |
302 |
return ivalue;
|
303 |
} |
304 |
|
305 |
protected String getDataBaseName() { |
306 |
return (String) StringUtils.defaultIfBlank(this.txtDataBase.getText(), null); |
307 |
} |
308 |
|
309 |
protected String getUsername() { |
310 |
return (String) StringUtils.defaultIfBlank(this.txtUsername.getText(), null); |
311 |
} |
312 |
|
313 |
protected String getPassword() { |
314 |
return (String) StringUtils.defaultIfBlank(this.txtPassword.getText(), null); |
315 |
} |
316 |
|
317 |
private void onChangeConnector() { |
318 |
ServerExplorerParametersComboItem item = (ServerExplorerParametersComboItem) this.cboConnectors.getSelectedItem();
|
319 |
if( item==null ) { |
320 |
return;
|
321 |
} |
322 |
JDBCServerExplorerParameters connector = item.getParams(); |
323 |
|
324 |
if ( connector == null ) { |
325 |
return;
|
326 |
} |
327 |
this.setServerExplorerParameters(connector);
|
328 |
} |
329 |
|
330 |
private void onChangeConnection() { |
331 |
Object item = this.cboConnections.getSelectedItem(); |
332 |
if ( item instanceof ServerExplorerParametersComboItem ) { |
333 |
JDBCServerExplorerParameters connection = ((ServerExplorerParametersComboItem) item).getParams(); |
334 |
if ( connection == null ) { |
335 |
return;
|
336 |
} |
337 |
this.setServerExplorerParameters(connection);
|
338 |
} |
339 |
} |
340 |
|
341 |
|
342 |
private int getIndexOfConnector(JDBCServerExplorerParameters explorerParameters) { |
343 |
String code = null; |
344 |
try {
|
345 |
code = explorerParameters.toString(); |
346 |
ComboBoxModel model = this.cboConnectors.getModel(); |
347 |
for ( int i = 0; i < model.getSize(); i++ ) { |
348 |
ServerExplorerParametersComboItem x = (ServerExplorerParametersComboItem) model.getElementAt(i); |
349 |
if ( x.getParams()!=null && x.getParams().getExplorerName().equalsIgnoreCase(explorerParameters.getExplorerName()) ) { |
350 |
return i;
|
351 |
} |
352 |
} |
353 |
} catch (Exception ex) { |
354 |
logger.warn("Can't get index of exporer parameter '" + code + "'.", ex); |
355 |
} |
356 |
return -1; |
357 |
} |
358 |
|
359 |
@Override
|
360 |
public void setConnectorFilter(ConnectorFilter connectorFilter) { |
361 |
this.connectorFilter = connectorFilter;
|
362 |
fillConnections(); |
363 |
fillConnectors(); |
364 |
} |
365 |
|
366 |
private void fillConnectors() { |
367 |
DataManager dataManager = DALLocator.getDataManager(); |
368 |
|
369 |
ServerExplorerParametersComboItem last = null;
|
370 |
this.cboConnectors.removeAllItems();
|
371 |
this.cboConnectors.addItem(new ServerExplorerParametersComboItem("",null)); |
372 |
|
373 |
for (DataFactory factory : dataManager.getServerExplorerRegister() ) {
|
374 |
DataServerExplorerParameters params = (DataServerExplorerParameters) factory.createParameters(); |
375 |
if ( params instanceof JDBCServerExplorerParameters ) { |
376 |
if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) params) ) { |
377 |
if( params.getClass() == JDBCServerExplorerParameters.class &&
|
378 |
last == null ) {
|
379 |
// Si es el proveedor generico de JDBC, lo guarda para
|
380 |
// a?adirlo al final del combo.
|
381 |
last = new ServerExplorerParametersComboItem(
|
382 |
factory.getDescription(), |
383 |
(JDBCServerExplorerParameters) params |
384 |
); |
385 |
} else {
|
386 |
this.cboConnectors.addItem(
|
387 |
new ServerExplorerParametersComboItem(
|
388 |
factory.getDescription(), |
389 |
(JDBCServerExplorerParameters) params |
390 |
) |
391 |
); |
392 |
} |
393 |
} |
394 |
} |
395 |
} |
396 |
if( last!=null ) { |
397 |
this.cboConnectors.addItem(last);
|
398 |
} |
399 |
} |
400 |
|
401 |
private void fillConnections() { |
402 |
DataManager dataManager = DALLocator.getDataManager(); |
403 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
404 |
|
405 |
DataServerExplorerParameters params; |
406 |
this.cboConnections.removeAllItems();
|
407 |
Iterator it = pool.iterator();
|
408 |
while ( it.hasNext() ) {
|
409 |
DataServerExplorerPoolEntry entry = (DataServerExplorerPoolEntry) it.next(); |
410 |
if ( entry.getExplorerParameters() instanceof JDBCServerExplorerParameters ) { |
411 |
if( this.connectorFilter==null || this.connectorFilter.accept((JDBCServerExplorerParameters) entry.getExplorerParameters()) ) { |
412 |
JDBCServerExplorerParameters dbParams = (JDBCServerExplorerParameters) entry.getExplorerParameters(); |
413 |
this.cboConnections.addItem(
|
414 |
new ServerExplorerParametersComboItem(entry.getName(), dbParams)
|
415 |
); |
416 |
} |
417 |
} |
418 |
} |
419 |
this.cboConnections.setSelectedIndex(-1); |
420 |
} |
421 |
|
422 |
@Override
|
423 |
public void delete() { |
424 |
String name = this.getConnectionName(); |
425 |
DataManager dataManager = DALLocator.getDataManager(); |
426 |
DataServerExplorerPool pool = dataManager.getDataServerExplorerPool(); |
427 |
|
428 |
pool.remove(name); |
429 |
fillConnections(); |
430 |
} |
431 |
|
432 |
@Override
|
433 |
public void clear() { |
434 |
this.cboConnections.setSelectedIndex(-1); |
435 |
this.cboConnectors.setSelectedIndex(-1); |
436 |
this.txtServer.setText(""); |
437 |
this.txtPort.setText(""); |
438 |
this.txtDataBase.setText(""); |
439 |
this.txtUsername.setText(""); |
440 |
this.txtPassword.setText(""); |
441 |
} |
442 |
|
443 |
private void onBrowseDatabase() { |
444 |
if( this.forcedParameters==null ) { |
445 |
return;
|
446 |
} |
447 |
ThreadSafeDialogsManager dlgmanager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
448 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
449 |
File[] files = dlgmanager.showOpenFileDialog( |
450 |
i18nManager.getTranslation("_Select_database"),
|
451 |
null
|
452 |
); |
453 |
if( ArrayUtils.isEmpty(files) ) {
|
454 |
return;
|
455 |
} |
456 |
((FilesystemStoreParameters)this.forcedParameters).setFile(files[0]); |
457 |
this.txtFile.setText(((FilesystemStoreParameters)(this.forcedParameters)).getFile().getAbsolutePath()); |
458 |
} |
459 |
} |