root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / DataBaseOpenDialog.java @ 37942
History | View | Annotate | Download (22.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.project.documents.table.gui; |
42 |
|
43 |
import java.awt.BorderLayout; |
44 |
import java.awt.Component; |
45 |
import java.awt.Dimension; |
46 |
import java.awt.event.ActionEvent; |
47 |
import java.awt.event.ActionListener; |
48 |
import java.awt.event.FocusEvent; |
49 |
import java.awt.event.FocusListener; |
50 |
import java.awt.event.ItemEvent; |
51 |
import java.awt.event.ItemListener; |
52 |
import java.awt.event.KeyEvent; |
53 |
import java.awt.event.KeyListener; |
54 |
import java.awt.geom.Rectangle2D; |
55 |
import java.sql.DatabaseMetaData; |
56 |
import java.sql.ResultSet; |
57 |
import java.sql.SQLException; |
58 |
import java.sql.Types; |
59 |
import java.util.ArrayList; |
60 |
|
61 |
import javax.swing.DefaultComboBoxModel; |
62 |
import javax.swing.DefaultListModel; |
63 |
import javax.swing.DefaultListSelectionModel; |
64 |
import javax.swing.ImageIcon; |
65 |
import javax.swing.JComboBox; |
66 |
import javax.swing.JLabel; |
67 |
import javax.swing.JList; |
68 |
import javax.swing.JOptionPane; |
69 |
import javax.swing.JPanel; |
70 |
import javax.swing.JPasswordField; |
71 |
import javax.swing.JScrollPane; |
72 |
import javax.swing.JTextField; |
73 |
import javax.swing.ListModel; |
74 |
import javax.swing.ListSelectionModel; |
75 |
import javax.swing.border.TitledBorder; |
76 |
import javax.swing.event.ListSelectionEvent; |
77 |
|
78 |
import org.apache.log4j.Logger; |
79 |
import org.cresques.cts.IProjection; |
80 |
import org.gvsig.gui.beans.swing.JButton; |
81 |
|
82 |
import com.hardcode.driverManager.Driver; |
83 |
import com.hardcode.driverManager.DriverLoadException; |
84 |
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver; |
85 |
import com.iver.andami.PluginServices; |
86 |
import com.iver.andami.messages.NotificationManager; |
87 |
import com.iver.andami.ui.mdiManager.IWindow; |
88 |
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension; |
89 |
import com.iver.cit.gvsig.addlayer.AddLayerDialog; |
90 |
import com.iver.cit.gvsig.fmap.MapContext; |
91 |
import com.iver.cit.gvsig.fmap.core.ICanReproject; |
92 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC; |
93 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
94 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
95 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
96 |
import com.iver.cit.gvsig.fmap.drivers.IConnection; |
97 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver; |
98 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver; |
99 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams; |
100 |
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleDBConnectionManager; |
101 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
102 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
103 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
104 |
import com.iver.cit.gvsig.project.documents.view.gui.BaseView; |
105 |
import com.iver.utiles.swing.JPasswordDlg; |
106 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.AvailableTablesCheckBoxList; |
107 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.DBConnectionParamsDialog; |
108 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.TablesListItem; |
109 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.TablesListItemSimple; |
110 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.UserSelectedFieldsPanel; |
111 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.UserTableSettingsPanel; |
112 |
import com.prodevelop.cit.gvsig.vectorialdb.wizard.WizardVectorialDB; |
113 |
/**
|
114 |
* DOCUMENT ME!
|
115 |
*
|
116 |
* @author Fernando Gonz?lez Cort?s
|
117 |
* @author jldominguez
|
118 |
*/
|
119 |
public class DataBaseOpenDialog extends JPanel implements ActionListener { |
120 |
|
121 |
private AddLayerDialog parent_dialog = null; |
122 |
|
123 |
private int _width = 500; |
124 |
private int _height = 480; |
125 |
private int _margin = 6; |
126 |
private int _conn_height = 55; |
127 |
private int _conn_button_height = 21; |
128 |
private int _conn_button_width = 26; |
129 |
|
130 |
|
131 |
|
132 |
/**
|
133 |
* This is the default constructor
|
134 |
*/
|
135 |
public DataBaseOpenDialog(AddLayerDialog dlg) {
|
136 |
super();
|
137 |
parent_dialog = dlg; |
138 |
initialize(); |
139 |
} |
140 |
|
141 |
/**
|
142 |
* This is the default constructor
|
143 |
*/
|
144 |
public DataBaseOpenDialog() {
|
145 |
super();
|
146 |
initialize(); |
147 |
} |
148 |
|
149 |
private static Logger logger = Logger.getLogger(WizardVectorialDB.class.getName()); |
150 |
protected IConnection conex = null; |
151 |
private ConnectionWithParams selectedDataSource = null; |
152 |
private JPanel connPanel = null; |
153 |
private JPanel tablesPanel = null; |
154 |
private JScrollPane tablesScrollPane = null; |
155 |
private JScrollPane _fieldsScrollPane = null; |
156 |
|
157 |
private AvailableTablesList tablesList = null; |
158 |
private JList _fieldsList = null; |
159 |
|
160 |
private JComboBox datasourceComboBox = null; |
161 |
// private UserTableSettingsPanel settingsPanel = null;
|
162 |
private JPanel fieldsPanel = null; |
163 |
// private UserTableSettingsPanel emptySettingsPanel = null;
|
164 |
// private UserSelectedFieldsPanel emptyFieldsPanel = null;
|
165 |
private JButton dbButton = null; |
166 |
// private BaseView view = null;
|
167 |
|
168 |
|
169 |
private void initialize() { |
170 |
setLayout(null);
|
171 |
this.setSize(_width, _height);
|
172 |
|
173 |
add(getConnectionPanel(), null);
|
174 |
loadDBDatasourcesCombo(); |
175 |
getDatasourceComboBox().addActionListener(this);
|
176 |
add(getTablesPanel(), null);
|
177 |
add(getFieldsPanel(), null);
|
178 |
} |
179 |
|
180 |
private JPanel getFieldsPanel() { |
181 |
|
182 |
if (fieldsPanel == null) { |
183 |
fieldsPanel = new JPanel(); |
184 |
fieldsPanel.setLayout(new BorderLayout()); |
185 |
fieldsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
186 |
null, PluginServices.getText(this, "choose_table"), |
187 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
188 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
189 |
fieldsPanel.setBounds(new java.awt.Rectangle(
|
190 |
(_width-3*_margin)/2+2*_margin, |
191 |
_conn_height+2*_margin,
|
192 |
(_width-3*_margin)/2, |
193 |
_height-_conn_height-3*_margin));
|
194 |
fieldsPanel.add(getFieldsScrollPane(), java.awt.BorderLayout.CENTER); |
195 |
} |
196 |
|
197 |
return fieldsPanel;
|
198 |
} |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
private JScrollPane getFieldsScrollPane() { |
204 |
if (_fieldsScrollPane == null) { |
205 |
_fieldsScrollPane = new JScrollPane(); |
206 |
} |
207 |
|
208 |
return _fieldsScrollPane;
|
209 |
} |
210 |
|
211 |
protected void loadDBDatasourcesCombo() { |
212 |
getDatasourceComboBox().removeAllItems(); |
213 |
|
214 |
getDatasourceComboBox().addItem(new ConnectionWithParams());
|
215 |
|
216 |
ConnectionWithParams[] conn = SingleDBConnectionManager.instance()
|
217 |
.getAllConnections(); |
218 |
|
219 |
if (conn == null) { |
220 |
return;
|
221 |
} |
222 |
|
223 |
Driver _drv = null; |
224 |
for (int i = 0; i < conn.length; i++) { |
225 |
|
226 |
_drv = SingleDBConnectionManager.getInstanceFromName(conn[i].getDrvName()); |
227 |
if (_drv != null && _drv instanceof AlphanumericDBDriver) { |
228 |
getDatasourceComboBox().addItem(conn[i]); |
229 |
} |
230 |
} |
231 |
} |
232 |
|
233 |
public void saveConns() { |
234 |
SingleVectorialDBConnectionExtension.saveAllToPersistence(); |
235 |
} |
236 |
|
237 |
protected String getSelectedTable() { |
238 |
return null; |
239 |
} |
240 |
|
241 |
/**
|
242 |
* This method initializes namePanel
|
243 |
*
|
244 |
* @return javax.swing.JPanel
|
245 |
*/
|
246 |
private JPanel getConnectionPanel() { |
247 |
if (connPanel == null) { |
248 |
connPanel = new JPanel(); |
249 |
connPanel.setLayout(null);
|
250 |
connPanel.setBounds(new java.awt.Rectangle(_margin, _margin, _width-2*_margin, _conn_height)); |
251 |
connPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
252 |
null, PluginServices.getText(this, "choose_connection"), |
253 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
254 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
255 |
connPanel.add(getDatasourceComboBox(), null);
|
256 |
connPanel.add(getJdbcButton(), null);
|
257 |
} |
258 |
|
259 |
return connPanel;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* This method initializes tablesPanel
|
264 |
*
|
265 |
* @return javax.swing.JPanel
|
266 |
*/
|
267 |
private JPanel getTablesPanel() { |
268 |
if (tablesPanel == null) { |
269 |
tablesPanel = new JPanel(); |
270 |
tablesPanel.setLayout(new BorderLayout()); |
271 |
tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder( |
272 |
null, PluginServices.getText(this, "choose_table"), |
273 |
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, |
274 |
javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
275 |
tablesPanel.setBounds(new java.awt.Rectangle(_margin, _conn_height+2*_margin, (_width-3*_margin)/2, _height-3*_margin-_conn_height)); |
276 |
tablesPanel.add(getTablesScrollPane(), java.awt.BorderLayout.CENTER); |
277 |
} |
278 |
|
279 |
return tablesPanel;
|
280 |
} |
281 |
|
282 |
/**
|
283 |
* This method initializes tablesScrollPane
|
284 |
*
|
285 |
* @return javax.swing.JScrollPane
|
286 |
*/
|
287 |
private JScrollPane getTablesScrollPane() { |
288 |
if (tablesScrollPane == null) { |
289 |
tablesScrollPane = new JScrollPane(); |
290 |
} |
291 |
|
292 |
return tablesScrollPane;
|
293 |
} |
294 |
|
295 |
/**
|
296 |
* This method initializes tablesList
|
297 |
*
|
298 |
* @return javax.swing.JList
|
299 |
*/
|
300 |
private AvailableTablesList getTablesList() {
|
301 |
if (tablesList == null) { |
302 |
tablesList = new AvailableTablesList(this); |
303 |
DefaultListModel lmodel = new DefaultListModel(); |
304 |
tablesList.setModel(lmodel); |
305 |
// tablesList.addListSelectionListener(this);
|
306 |
} |
307 |
|
308 |
return tablesList;
|
309 |
} |
310 |
|
311 |
/**
|
312 |
* This method initializes jComboBox
|
313 |
*
|
314 |
* @return javax.swing.JComboBox
|
315 |
*/
|
316 |
protected JComboBox getDatasourceComboBox() { |
317 |
if (datasourceComboBox == null) { |
318 |
datasourceComboBox = new JComboBox(); |
319 |
datasourceComboBox.setBounds(new java.awt.Rectangle(
|
320 |
_margin, |
321 |
3*_margin,
|
322 |
_width-5*_margin-_conn_button_width, _conn_button_height));
|
323 |
} |
324 |
|
325 |
return datasourceComboBox;
|
326 |
} |
327 |
|
328 |
public void actionPerformed(ActionEvent arg0) { |
329 |
Object src = arg0.getSource();
|
330 |
|
331 |
if (src == datasourceComboBox) {
|
332 |
Object sel_obj = datasourceComboBox.getSelectedItem();
|
333 |
|
334 |
if (sel_obj == null) { |
335 |
return;
|
336 |
} |
337 |
|
338 |
if (!(sel_obj instanceof ConnectionWithParams)) { |
339 |
return;
|
340 |
} |
341 |
|
342 |
selectedDataSource = (ConnectionWithParams) sel_obj; |
343 |
setEmptyPanels(); |
344 |
|
345 |
if (selectedDataSource.isNull()) {
|
346 |
// empty conn selected
|
347 |
try {
|
348 |
updateTableList(selectedDataSource); |
349 |
checkFinishable(); |
350 |
} catch (SQLException e) { } |
351 |
return;
|
352 |
} |
353 |
|
354 |
if (!selectedDataSource.isConnected()) {
|
355 |
if (!tryToConnect(selectedDataSource)) {
|
356 |
datasourceComboBox.setSelectedIndex(0);
|
357 |
|
358 |
return;
|
359 |
} |
360 |
} |
361 |
|
362 |
try {
|
363 |
updateTableList(selectedDataSource); |
364 |
checkFinishable(); |
365 |
} catch (SQLException e) { |
366 |
JOptionPane.showMessageDialog(this, |
367 |
PluginServices.getText(this, "Error_getting_table_names") + ": " + e.getMessage(), |
368 |
PluginServices.getText(this, "Error"), |
369 |
JOptionPane.ERROR_MESSAGE);
|
370 |
setEmptyPanels(); |
371 |
return;
|
372 |
} |
373 |
|
374 |
|
375 |
if (tablesList.getModel().getSize() == 0) { |
376 |
JOptionPane.showMessageDialog(
|
377 |
this,
|
378 |
PluginServices.getText(this, "No_tables_listed_maybe_schema_case_issue"), |
379 |
PluginServices.getText(this, "Error"), |
380 |
JOptionPane.WARNING_MESSAGE);
|
381 |
return;
|
382 |
} |
383 |
|
384 |
|
385 |
return;
|
386 |
} |
387 |
|
388 |
if (src == dbButton) {
|
389 |
ConnectionWithParams sel = addNewConnection(); |
390 |
|
391 |
if (sel != null) { |
392 |
loadDBDatasourcesCombo(); |
393 |
getDatasourceComboBox().setSelectedItem(sel); |
394 |
} |
395 |
} |
396 |
} |
397 |
|
398 |
private void setEmptyPanels() { |
399 |
((DefaultListModel) getTablesList().getModel()).clear();
|
400 |
getTablesScrollPane().updateUI(); |
401 |
|
402 |
((TitledBorder) getFieldsPanel().getBorder()).setTitle(
|
403 |
PluginServices.getText(this, "Fields_for_table") + ": -"); |
404 |
((DefaultListModel) getFieldsList().getModel()).clear();
|
405 |
getFieldsScrollPane().updateUI(); |
406 |
|
407 |
checkFinishable(); |
408 |
} |
409 |
|
410 |
private boolean tryToConnect(ConnectionWithParams _cwp) { |
411 |
JPasswordDlg dlg = new JPasswordDlg();
|
412 |
dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
|
413 |
String strMessage = PluginServices.getText(this, "conectar_jdbc"); |
414 |
String strPassword = PluginServices.getText(this, "password"); |
415 |
dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " + |
416 |
_cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() + |
417 |
", " + _cwp.getUser() + "]. " + strPassword + "?"); |
418 |
|
419 |
dlg.setVisible(true);
|
420 |
|
421 |
String clave = dlg.getPassword();
|
422 |
|
423 |
if (clave == null) { |
424 |
return false; |
425 |
} |
426 |
|
427 |
try {
|
428 |
_cwp.connect(clave); |
429 |
} |
430 |
catch (DBException e) {
|
431 |
showConnectionErrorMessage(e.getMessage()); |
432 |
|
433 |
return false; |
434 |
} |
435 |
|
436 |
return true; |
437 |
} |
438 |
|
439 |
private void updateTableList(ConnectionWithParams src) throws SQLException { |
440 |
if (src.isNull()) {
|
441 |
getTablesList().setModel(new DefaultListModel()); |
442 |
getTablesScrollPane().setViewportView(tablesList); |
443 |
getTablesScrollPane().updateUI(); |
444 |
|
445 |
return;
|
446 |
} |
447 |
|
448 |
conex = src.getConnection(); |
449 |
ConnectionJDBC jdbc_conn = null;
|
450 |
DefaultListModel lmodel = new DefaultListModel(); |
451 |
|
452 |
if (conex instanceof ConnectionJDBC) { |
453 |
jdbc_conn = (ConnectionJDBC) conex; |
454 |
} else {
|
455 |
getTablesList().setModel(lmodel); |
456 |
getTablesScrollPane().setViewportView(tablesList); |
457 |
getTablesScrollPane().updateUI(); |
458 |
throw new SQLException("Unknown connection type: " + conex.getClass().getName()); |
459 |
} |
460 |
|
461 |
// FJP: We don't want system tables, but linked tables yes
|
462 |
String[] ret_types = {"TABLE", "VIEW", "SYNONYM" /* odbc */ }; // SYSTEM |
463 |
ResultSet rs = null; |
464 |
|
465 |
try {
|
466 |
|
467 |
// DatabaseMetaData dmd = jdbc_conn.getConnection().getMetaData();
|
468 |
// ResultSet rs2 = dmd.getTables(null, null, "%", null);
|
469 |
// while (rs2.next())
|
470 |
// System.out.println(rs2.getString(3) + " " + rs2.getString(4));
|
471 |
// rs2 = dmd.getTables(null, null, null, null);
|
472 |
// int colcnt = rs2.getMetaData().getColumnCount();
|
473 |
// while (rs2.next()) {
|
474 |
// for (int i=0; i<colcnt; i++) {
|
475 |
// System.out.println(rs2.getMetaData().getColumnName(i+1) + " : " + rs2.getString(i+1));
|
476 |
// }
|
477 |
// }
|
478 |
|
479 |
rs = jdbc_conn.getConnection().getMetaData().getTables( |
480 |
null,
|
481 |
src.getSchema().length() == 0 ? null : src.getSchema(), |
482 |
null,
|
483 |
ret_types); |
484 |
} catch (SQLException e1) { |
485 |
throw e1;
|
486 |
} |
487 |
|
488 |
String _n = ""; |
489 |
String _t = ""; |
490 |
String _s = ""; |
491 |
|
492 |
while (rs.next()) {
|
493 |
_s = rs.getString(2);
|
494 |
_n = rs.getString(3);
|
495 |
_n = (_s == null || _s.length()==0) ? _n : _s + "." + _n; |
496 |
_t = rs.getString(4);
|
497 |
lmodel.addElement( new TablesListItemSimple(_n, _t, src.getConnection()) );
|
498 |
} |
499 |
rs.close(); |
500 |
|
501 |
getTablesList().setModel(lmodel); |
502 |
getTablesScrollPane().setViewportView(tablesList); |
503 |
getTablesScrollPane().updateUI(); |
504 |
|
505 |
|
506 |
} |
507 |
|
508 |
private boolean validFormSettings() { |
509 |
|
510 |
int count = tablesList.getModel().getSize();
|
511 |
for (int i = 0; i < count; i++) { |
512 |
TablesListItemSimple item = (TablesListItemSimple) tablesList.getModel().getElementAt(i); |
513 |
if (item.isSelected()) {
|
514 |
return true; |
515 |
} |
516 |
} |
517 |
return false; |
518 |
} |
519 |
|
520 |
public void checkFinishable() { |
521 |
boolean finishable = validFormSettings();
|
522 |
parent_dialog.getJPanel().getOkButton().setEnabled(finishable); |
523 |
} |
524 |
|
525 |
/**
|
526 |
* This method initializes jdbcButton
|
527 |
*
|
528 |
* @return javax.swing.JButton
|
529 |
*/
|
530 |
private JButton getJdbcButton() { |
531 |
if (dbButton == null) { |
532 |
dbButton = new JButton(); |
533 |
dbButton.addActionListener(this);
|
534 |
dbButton.setToolTipText(PluginServices.getText(this,
|
535 |
"add_connection"));
|
536 |
dbButton.setBounds(new java.awt.Rectangle(
|
537 |
_width-3*_margin-_conn_button_width,
|
538 |
3*_margin,
|
539 |
_conn_button_width, _conn_button_height)); |
540 |
|
541 |
String _file = createResourceUrl("images/jdbc.png").getFile(); |
542 |
dbButton.setIcon(new ImageIcon(_file)); |
543 |
} |
544 |
|
545 |
return dbButton;
|
546 |
} |
547 |
|
548 |
private ConnectionWithParams addNewConnection() {
|
549 |
ConnectionWithParams resp = null;
|
550 |
|
551 |
DBConnectionParamsDialog newco = new DBConnectionParamsDialog();
|
552 |
newco.showDialog(); |
553 |
|
554 |
if (newco.isOkPressed()) {
|
555 |
String _drvname = newco.getConnectionDriverName();
|
556 |
String _host = newco.getConnectionServerUrl();
|
557 |
String _port = newco.getConnectionPort();
|
558 |
String _dbname = newco.getConnectionDBName();
|
559 |
String _user = newco.getConnectionUser();
|
560 |
String _pw = newco.getConnectionPassword();
|
561 |
String _sche = newco.getConnectionSchema();
|
562 |
String _conn_usr_name = newco.getConnectionName();
|
563 |
|
564 |
boolean hasToBeCon = newco.hasToBeConnected();
|
565 |
|
566 |
try {
|
567 |
resp = SingleDBConnectionManager.instance() |
568 |
.getConnection(_drvname, |
569 |
_user, _pw, _conn_usr_name, _host, _port, _dbname, _sche, |
570 |
hasToBeCon); |
571 |
} |
572 |
catch (DBException e) {
|
573 |
showConnectionErrorMessage(e.getMessage()); |
574 |
|
575 |
return null; |
576 |
} |
577 |
SingleVectorialDBConnectionExtension.saveAllToPersistence(); |
578 |
return resp;
|
579 |
} |
580 |
else {
|
581 |
return null; |
582 |
} |
583 |
} |
584 |
|
585 |
private void showConnectionErrorMessage(String _msg) { |
586 |
String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
587 |
String title = PluginServices.getText(this, "connection_error"); |
588 |
JOptionPane.showMessageDialog(this, title + msg, title, |
589 |
JOptionPane.ERROR_MESSAGE);
|
590 |
} |
591 |
|
592 |
private java.net.URL createResourceUrl(String path) { |
593 |
return getClass().getClassLoader().getResource(path);
|
594 |
} |
595 |
|
596 |
public String getDriverName() { |
597 |
if (selectedDataSource == null) { |
598 |
logger.error("CWP is NULL (?)");
|
599 |
return null; |
600 |
} else {
|
601 |
return selectedDataSource.getDrvName();
|
602 |
} |
603 |
} |
604 |
|
605 |
public String getPort() { |
606 |
if (selectedDataSource == null) { |
607 |
logger.error("CWP is NULL (?)");
|
608 |
return null; |
609 |
} else {
|
610 |
return selectedDataSource.getPort();
|
611 |
} |
612 |
} |
613 |
|
614 |
public String getHost() { |
615 |
if (selectedDataSource == null) { |
616 |
logger.error("CWP is NULL (?)");
|
617 |
return null; |
618 |
} else {
|
619 |
return selectedDataSource.getHost();
|
620 |
} |
621 |
} |
622 |
|
623 |
public String getDataBase() { |
624 |
if (selectedDataSource == null) { |
625 |
logger.error("CWP is NULL (?)");
|
626 |
return null; |
627 |
} else {
|
628 |
return selectedDataSource.getDb();
|
629 |
} |
630 |
} |
631 |
|
632 |
public String getUser() { |
633 |
if (selectedDataSource == null) { |
634 |
logger.error("CWP is NULL (?)");
|
635 |
return null; |
636 |
} else {
|
637 |
return selectedDataSource.getUser();
|
638 |
} |
639 |
} |
640 |
|
641 |
public String getPassword() { |
642 |
if (selectedDataSource == null) { |
643 |
logger.error("CWP is NULL (?)");
|
644 |
return null; |
645 |
} else {
|
646 |
return selectedDataSource.getPw();
|
647 |
} |
648 |
} |
649 |
|
650 |
public String getTable() { |
651 |
ListModel lm = getTablesList().getModel();
|
652 |
int sz = lm.getSize();
|
653 |
TablesListItemSimple item = null;
|
654 |
for (int i=0; i<sz; i++) { |
655 |
if (lm.getElementAt(i) instanceof TablesListItemSimple) { |
656 |
item = (TablesListItemSimple) lm.getElementAt(i); |
657 |
if (item.isSelected()) {
|
658 |
return quoteTableName(item.getTableName());
|
659 |
} |
660 |
} |
661 |
} |
662 |
logger.error("NO check box selected in table list (?)");
|
663 |
return ""; |
664 |
} |
665 |
|
666 |
public boolean setActingTable(TablesListItemSimple acttab) { |
667 |
|
668 |
String drvn = selectedDataSource.getDrvName();
|
669 |
IConnection iconn = selectedDataSource.getConnection(); |
670 |
|
671 |
Driver drv = SingleDBConnectionManager.getInstanceFromName(drvn);
|
672 |
String tn = ""; |
673 |
if (drv instanceof AlphanumericDBDriver && iconn instanceof ConnectionJDBC) { |
674 |
ConnectionJDBC conn = (ConnectionJDBC) iconn; |
675 |
AlphanumericDBDriver alpha_drv = (AlphanumericDBDriver) drv; |
676 |
|
677 |
tn = quoteTableName(acttab.getTableName()); |
678 |
|
679 |
String _sql = "SELECT * FROM " + tn + " WHERE (0=1)"; |
680 |
FieldDescription[] fd = null; |
681 |
try {
|
682 |
alpha_drv.open(conn.getConnection(), _sql); |
683 |
fd = alpha_drv.getTableDefinition().getFieldsDesc(); |
684 |
} catch (Exception ex) { |
685 |
logger.error("While getting fields: " + ex.getMessage());
|
686 |
JOptionPane.showMessageDialog(this, |
687 |
PluginServices.getText(this, "Error_while_getting_table_fields") + ": " + ex.getMessage(), |
688 |
PluginServices.getText(this, "Error"), |
689 |
JOptionPane.ERROR_MESSAGE);
|
690 |
return false; |
691 |
} |
692 |
|
693 |
int fld_cnt = fd.length;
|
694 |
((DefaultListModel) getFieldsList().getModel()).clear();
|
695 |
String ft = "Other"; |
696 |
for (int i=0; i<fld_cnt; i++) { |
697 |
String fn = fd[i].getFieldName();
|
698 |
|
699 |
try {
|
700 |
ft = typeToString(fd[i].getFieldType()); |
701 |
} catch (Exception ex) { |
702 |
ft = "Other";
|
703 |
} |
704 |
|
705 |
((DefaultListModel) getFieldsList().getModel()).addElement(
|
706 |
new FieldListItem(fn,ft));
|
707 |
} |
708 |
this.getFieldsScrollPane().setViewportView(getFieldsList());
|
709 |
getFieldsScrollPane().updateUI(); |
710 |
|
711 |
((TitledBorder) getFieldsPanel().getBorder()).setTitle(
|
712 |
PluginServices.getText(this, "Fields_for_table") + ": " + tn); |
713 |
|
714 |
try {
|
715 |
alpha_drv.close(); |
716 |
} catch (Exception ex) { |
717 |
logger.error("Unreported error while closing temp driver: " + ex.getMessage());
|
718 |
} |
719 |
|
720 |
alpha_drv = null;
|
721 |
return true; |
722 |
} else {
|
723 |
logger.error("Unexpacted driver or conn class, drv : " + drv.getClass().getName());
|
724 |
logger.error("Unexpacted driver or conn class, conn: " + iconn.getClass().getName());
|
725 |
return false; |
726 |
} |
727 |
|
728 |
} |
729 |
|
730 |
|
731 |
public static String typeToString(int sqlType) { |
732 |
switch (sqlType) {
|
733 |
case Types.BIGINT: |
734 |
case Types.INTEGER: |
735 |
case Types.SMALLINT: |
736 |
case Types.TINYINT: |
737 |
return "Integer"; |
738 |
|
739 |
case Types.NUMERIC: |
740 |
return "Numeric"; |
741 |
|
742 |
case Types.BIT: |
743 |
case Types.BOOLEAN: |
744 |
return "Boolean"; |
745 |
|
746 |
case Types.CHAR: |
747 |
case Types.VARCHAR: |
748 |
case Types.LONGVARCHAR: |
749 |
return "String"; |
750 |
|
751 |
case Types.DATE: |
752 |
return "Date"; |
753 |
|
754 |
case Types.FLOAT: |
755 |
case Types.DOUBLE: |
756 |
case Types.DECIMAL: |
757 |
case Types.REAL: |
758 |
return "Double"; |
759 |
|
760 |
case Types.BINARY: |
761 |
case Types.VARBINARY: |
762 |
case Types.LONGVARBINARY: |
763 |
return "Binary"; |
764 |
|
765 |
case Types.TIMESTAMP: |
766 |
return "Timestamp"; |
767 |
|
768 |
case Types.TIME: |
769 |
return "Time"; |
770 |
|
771 |
case Types.OTHER: |
772 |
default:
|
773 |
throw new RuntimeException("Type not recognized: " + sqlType); |
774 |
} |
775 |
} |
776 |
|
777 |
private JList getFieldsList() { |
778 |
if (_fieldsList == null) { |
779 |
_fieldsList = new JList(); |
780 |
_fieldsList.setSelectionModel(new NoSelectionModel());
|
781 |
DefaultListModel lmodel = new DefaultListModel(); |
782 |
_fieldsList.setModel(lmodel); |
783 |
} |
784 |
return _fieldsList;
|
785 |
} |
786 |
|
787 |
private class FieldListItem extends JLabel { |
788 |
|
789 |
private String name = ""; |
790 |
private String type = ""; |
791 |
|
792 |
public FieldListItem(String _n, String _t) { |
793 |
name = _n; |
794 |
type = _t; |
795 |
} |
796 |
|
797 |
public String toString() { |
798 |
if (name == null || name.length() == 0) { |
799 |
return "[-]"; |
800 |
} else {
|
801 |
return (type == null || type.length() == 0) ? name : (name+" [" + type + "]"); |
802 |
} |
803 |
} |
804 |
|
805 |
} |
806 |
|
807 |
private class NoSelectionModel extends DefaultListSelectionModel { |
808 |
public NoSelectionModel() {
|
809 |
} |
810 |
public void addSelectionInterval(int a, int b) { } |
811 |
public boolean isSelectedIndex(int a) { return false; } |
812 |
public boolean isSelectionEmpty() { return true; } |
813 |
} |
814 |
|
815 |
public void clearFieldsList() { |
816 |
((DefaultListModel) getFieldsList().getModel()).clear();
|
817 |
getFieldsScrollPane().updateUI(); |
818 |
} |
819 |
|
820 |
|
821 |
public static String quoteTableName(String _tn) { |
822 |
|
823 |
String[] pointspl = _tn.split("\\."); |
824 |
String resp = ""; |
825 |
for (int i=0; i<pointspl.length; i++) { |
826 |
if (i!=0) { |
827 |
resp = resp + ".";
|
828 |
} |
829 |
resp = resp + "\"" + pointspl[i] + "\""; |
830 |
} |
831 |
return resp;
|
832 |
} |
833 |
|
834 |
|
835 |
|
836 |
} |
837 |
|
838 |
|
839 |
// [eiel-gestion-conexiones]
|