Statistics
| Revision:

root / branches / gvSIG_1.11.0_Mejoras_gvSIG-EIEL / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / DataBaseOpenDialog.java @ 34767

History | View | Annotate | Download (21.6 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.ResultSet;
56
import java.sql.SQLException;
57
import java.sql.Types;
58
import java.util.ArrayList;
59

    
60
import javax.swing.DefaultComboBoxModel;
61
import javax.swing.DefaultListModel;
62
import javax.swing.DefaultListSelectionModel;
63
import javax.swing.ImageIcon;
64
import javax.swing.JComboBox;
65
import javax.swing.JLabel;
66
import javax.swing.JList;
67
import javax.swing.JOptionPane;
68
import javax.swing.JPanel;
69
import javax.swing.JPasswordField;
70
import javax.swing.JScrollPane;
71
import javax.swing.JTextField;
72
import javax.swing.ListModel;
73
import javax.swing.ListSelectionModel;
74
import javax.swing.border.TitledBorder;
75
import javax.swing.event.ListSelectionEvent;
76

    
77
import org.apache.log4j.Logger;
78
import org.cresques.cts.IProjection;
79
import org.gvsig.gui.beans.swing.JButton;
80

    
81
import com.hardcode.driverManager.Driver;
82
import com.hardcode.driverManager.DriverLoadException;
83
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver;
84
import com.iver.andami.PluginServices;
85
import com.iver.andami.messages.NotificationManager;
86
import com.iver.andami.ui.mdiManager.IWindow;
87
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension;
88
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
89
import com.iver.cit.gvsig.fmap.MapContext;
90
import com.iver.cit.gvsig.fmap.core.ICanReproject;
91
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC;
92
import com.iver.cit.gvsig.fmap.drivers.DBException;
93
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
94
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
95
import com.iver.cit.gvsig.fmap.drivers.IConnection;
96
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
97
import com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver;
98
import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams;
99
import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleDBConnectionManager;
100
import com.iver.cit.gvsig.fmap.layers.FLayer;
101
import com.iver.cit.gvsig.fmap.layers.FLayers;
102
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
103
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
104
import com.iver.utiles.swing.JPasswordDlg;
105
import com.prodevelop.cit.gvsig.vectorialdb.wizard.AvailableTablesCheckBoxList;
106
import com.prodevelop.cit.gvsig.vectorialdb.wizard.DBConnectionParamsDialog;
107
import com.prodevelop.cit.gvsig.vectorialdb.wizard.TablesListItem;
108
import com.prodevelop.cit.gvsig.vectorialdb.wizard.TablesListItemSimple;
109
import com.prodevelop.cit.gvsig.vectorialdb.wizard.UserSelectedFieldsPanel;
110
import com.prodevelop.cit.gvsig.vectorialdb.wizard.UserTableSettingsPanel;
111
import com.prodevelop.cit.gvsig.vectorialdb.wizard.WizardVectorialDB;
112
/**
113
 * DOCUMENT ME!
114
 *
115
 * @author Fernando Gonz?lez Cort?s
116
 * @author jldominguez
117
 */
118
public class DataBaseOpenDialog extends JPanel implements ActionListener {
119
        
120
        private AddLayerDialog parent_dialog = null;
121
        
122
        private int _width = 500;
123
        private int _height = 480;
124
        private int _margin = 6;
125
        private int _conn_height = 55;
126
        private int _conn_button_height = 21;
127
        private int _conn_button_width = 26;
128
        
129
        
130
        
131
    /**
132
     * This is the default constructor
133
     */
134
    public DataBaseOpenDialog(AddLayerDialog dlg) {
135
        super();
136
        parent_dialog = dlg;
137
        initialize();
138
    }
139

    
140
    /**
141
     * This is the default constructor
142
     */
143
    public DataBaseOpenDialog() {
144
        super();
145
        initialize();
146
    }
147

    
148
        private static Logger logger = Logger.getLogger(WizardVectorialDB.class.getName());
149
        protected IConnection conex = null;
150
        private ConnectionWithParams selectedDataSource = null;
151
        private JPanel connPanel = null;
152
        private JPanel tablesPanel = null;
153
        private JScrollPane tablesScrollPane = null;
154
        private JScrollPane _fieldsScrollPane = null;
155
        
156
        private AvailableTablesList tablesList = null;
157
        private JList _fieldsList = null;
158
        
159
        private JComboBox datasourceComboBox = null;
160
        // private UserTableSettingsPanel settingsPanel = null;
161
        private JPanel fieldsPanel = null;
162
        // private UserTableSettingsPanel emptySettingsPanel = null;
163
        // private UserSelectedFieldsPanel emptyFieldsPanel = null;
164
        private JButton dbButton = null;
165
        // private BaseView view = null;
166
        
167

    
168
        private void initialize() {
169
                setLayout(null);
170
                this.setSize(_width, _height);
171

    
172
                add(getConnectionPanel(), null);
173
                loadDBDatasourcesCombo();
174
                getDatasourceComboBox().addActionListener(this);
175
                add(getTablesPanel(), null);
176
                add(getFieldsPanel(), null);
177
        }
178

    
179
        private JPanel getFieldsPanel() {
180

    
181
                if (fieldsPanel == null) {
182
                        fieldsPanel = new JPanel();
183
                        fieldsPanel.setLayout(new BorderLayout());
184
                        fieldsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
185
                                        null, PluginServices.getText(this, "choose_table"),
186
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
187
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
188
                        fieldsPanel.setBounds(new java.awt.Rectangle(
189
                                        (_width-3*_margin)/2+2*_margin,
190
                                        _conn_height+2*_margin,
191
                                        (_width-3*_margin)/2,
192
                                        _height-_conn_height-3*_margin));
193
                        fieldsPanel.add(getFieldsScrollPane(), java.awt.BorderLayout.CENTER);
194
                }
195

    
196
                return fieldsPanel;
197
        }
198

    
199

    
200

    
201

    
202
        private JScrollPane getFieldsScrollPane() {
203
                if (_fieldsScrollPane == null) {
204
                        _fieldsScrollPane = new JScrollPane();
205
                }
206

    
207
                return _fieldsScrollPane;
208
        }
209

    
210
        protected void loadDBDatasourcesCombo() {
211
                getDatasourceComboBox().removeAllItems();
212

    
213
                getDatasourceComboBox().addItem(new ConnectionWithParams());
214

    
215
                ConnectionWithParams[] conn = SingleDBConnectionManager.instance()
216
                .getAllConnections();
217

    
218
                if (conn == null) {
219
                        return;
220
                }
221

    
222
                Driver _drv = null;
223
                for (int i = 0; i < conn.length; i++) {
224
                        
225
                        _drv = SingleDBConnectionManager.getInstanceFromName(conn[i].getDrvName());
226
                        if (_drv != null && _drv instanceof AlphanumericDBDriver) {
227
                                getDatasourceComboBox().addItem(conn[i]);
228
                        }
229
                }
230
        }
231

    
232
        public void saveConns() {
233
                SingleVectorialDBConnectionExtension.saveAllToPersistence();
234
        }
235

    
236
        protected String getSelectedTable() {
237
                return null;
238
        }
239

    
240
        /**
241
         * This method initializes namePanel
242
         *
243
         * @return javax.swing.JPanel
244
         */
245
        private JPanel getConnectionPanel() {
246
                if (connPanel == null) {
247
                        connPanel = new JPanel();
248
                        connPanel.setLayout(null);
249
                        connPanel.setBounds(new java.awt.Rectangle(_margin, _margin, _width-2*_margin, _conn_height));
250
                        connPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
251
                                        null, PluginServices.getText(this, "choose_connection"),
252
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
253
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
254
                        connPanel.add(getDatasourceComboBox(), null);
255
                        connPanel.add(getJdbcButton(), null);
256
                }
257

    
258
                return connPanel;
259
        }
260

    
261
        /**
262
         * This method initializes tablesPanel
263
         *
264
         * @return javax.swing.JPanel
265
         */
266
        private JPanel getTablesPanel() {
267
                if (tablesPanel == null) {
268
                        tablesPanel = new JPanel();
269
                        tablesPanel.setLayout(new BorderLayout());
270
                        tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
271
                                        null, PluginServices.getText(this, "choose_table"),
272
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
273
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
274
                        tablesPanel.setBounds(new java.awt.Rectangle(_margin, _conn_height+2*_margin, (_width-3*_margin)/2, _height-3*_margin-_conn_height));
275
                        tablesPanel.add(getTablesScrollPane(), java.awt.BorderLayout.CENTER);
276
                }
277

    
278
                return tablesPanel;
279
        }
280

    
281
        /**
282
         * This method initializes tablesScrollPane
283
         *
284
         * @return javax.swing.JScrollPane
285
         */
286
        private JScrollPane getTablesScrollPane() {
287
                if (tablesScrollPane == null) {
288
                        tablesScrollPane = new JScrollPane();
289
                }
290

    
291
                return tablesScrollPane;
292
        }
293

    
294
        /**
295
         * This method initializes tablesList
296
         *
297
         * @return javax.swing.JList
298
         */
299
        private AvailableTablesList getTablesList() {
300
                if (tablesList == null) {
301
                        tablesList = new AvailableTablesList(this);
302
                        DefaultListModel lmodel = new DefaultListModel();
303
                        tablesList.setModel(lmodel);
304
                        // tablesList.addListSelectionListener(this);
305
                }
306

    
307
                return tablesList;
308
        }
309

    
310
        /**
311
         * This method initializes jComboBox
312
         *
313
         * @return javax.swing.JComboBox
314
         */
315
        protected JComboBox getDatasourceComboBox() {
316
                if (datasourceComboBox == null) {
317
                        datasourceComboBox = new JComboBox();
318
                        datasourceComboBox.setBounds(new java.awt.Rectangle(
319
                                        _margin,
320
                                        3*_margin,
321
                                        _width-5*_margin-_conn_button_width, _conn_button_height));
322
                }
323

    
324
                return datasourceComboBox;
325
        }
326

    
327
        public void actionPerformed(ActionEvent arg0) {
328
                Object src = arg0.getSource();
329

    
330
                if (src == datasourceComboBox) {
331
                        Object sel_obj = datasourceComboBox.getSelectedItem();
332

    
333
                        if (sel_obj == null) {
334
                                return;
335
                        }
336

    
337
                        if (!(sel_obj instanceof ConnectionWithParams)) {
338
                                return;
339
                        }
340

    
341
                        selectedDataSource = (ConnectionWithParams) sel_obj;
342
                        setEmptyPanels();
343

    
344
                        if (selectedDataSource.isNull()) {
345
                                // empty conn selected
346
                                try {
347
                                        updateTableList(selectedDataSource);
348
                                        checkFinishable();
349
                                } catch (SQLException e) { }
350
                                return;
351
                        }
352
                        
353
                        if (!selectedDataSource.isConnected()) {
354
                                if (!tryToConnect(selectedDataSource)) {
355
                                        datasourceComboBox.setSelectedIndex(0);
356

    
357
                                        return;
358
                                }
359
                        }
360
                        
361
                        try {
362
                                updateTableList(selectedDataSource);
363
                                checkFinishable();
364
                        } catch (SQLException e) {
365
                                JOptionPane.showMessageDialog(this,
366
                                                PluginServices.getText(this, "Error_getting_table_names") + ": " + e.getMessage(),
367
                                                PluginServices.getText(this, "Error"),
368
                                                JOptionPane.ERROR_MESSAGE);
369
                                setEmptyPanels();
370
                                return;
371
                        }
372

    
373

    
374
                        if (tablesList.getModel().getSize() == 0) {
375
                                JOptionPane.showMessageDialog(
376
                                                this,
377
                                                PluginServices.getText(this, "No_tables_listed_maybe_schema_case_issue"),
378
                                                PluginServices.getText(this, "Error"),
379
                                                JOptionPane.WARNING_MESSAGE);
380
                                return;
381
                        }
382

    
383
                                        
384
                        return;
385
                }
386

    
387
                if (src == dbButton) {
388
                        ConnectionWithParams sel = addNewConnection();
389

    
390
                        if (sel != null) {
391
                                loadDBDatasourcesCombo();
392
                                getDatasourceComboBox().setSelectedItem(sel);
393
                        }
394
                }
395
        }
396

    
397
        private void setEmptyPanels() {
398
                ((DefaultListModel) getTablesList().getModel()).clear();
399
                getTablesScrollPane().updateUI();
400
                
401
                ((TitledBorder) getFieldsPanel().getBorder()).setTitle(
402
                                PluginServices.getText(this, "Fields_for_table") + ": -");
403
                ((DefaultListModel) getFieldsList().getModel()).clear();
404
                getFieldsScrollPane().updateUI();
405
                
406
                checkFinishable();
407
        }
408

    
409
        private boolean tryToConnect(ConnectionWithParams _cwp) {
410
                JPasswordDlg dlg = new JPasswordDlg();
411
                dlg.setLocationRelativeTo((Component)PluginServices.getMainFrame());
412
                String strMessage = PluginServices.getText(this, "conectar_jdbc");
413
                String strPassword = PluginServices.getText(this, "password");
414
                dlg.setMessage(strMessage + " [" + _cwp.getDrvName() + ", " +
415
                                _cwp.getHost() + ", " + _cwp.getPort() + ", " + _cwp.getDb() +
416
                                ", " + _cwp.getUser() + "]. " + strPassword + "?");
417

    
418
                dlg.setVisible(true);
419

    
420
                String clave = dlg.getPassword();
421

    
422
                if (clave == null) {
423
                        return false;
424
                }
425

    
426
                try {
427
                        _cwp.connect(clave);
428
                }
429
                catch (DBException e) {
430
                        showConnectionErrorMessage(e.getMessage());
431

    
432
                        return false;
433
                }
434

    
435
                return true;
436
        }
437

    
438
        private void updateTableList(ConnectionWithParams src) throws SQLException {
439
                if (src.isNull()) {
440
                        getTablesList().setModel(new DefaultListModel());
441
                        getTablesScrollPane().setViewportView(tablesList);
442
                        getTablesScrollPane().updateUI();
443

    
444
                        return;
445
                }
446

    
447
                conex = src.getConnection();
448
                ConnectionJDBC jdbc_conn = null;
449
                DefaultListModel lmodel = new DefaultListModel();
450

    
451
                if (conex instanceof ConnectionJDBC) {
452
                        jdbc_conn = (ConnectionJDBC) conex; 
453
                } else {
454
                        getTablesList().setModel(lmodel);
455
                        getTablesScrollPane().setViewportView(tablesList);
456
                        getTablesScrollPane().updateUI();
457
                        throw new SQLException("Unknown connection type: " + conex.getClass().getName());
458
                }
459
                
460
                String[] ret_types = {"TABLE", "VIEW"};
461
                ResultSet rs = null;
462

    
463
                try {
464
                        rs = jdbc_conn.getConnection().getMetaData().getTables(
465
                                        null,
466
                                        src.getSchema().length() == 0 ? null : src.getSchema(),
467
                                        null,
468
                                        ret_types);
469
                } catch (SQLException e1) {
470
                        throw e1;
471
                }
472
                
473
                String _n = "";
474
                String _t = "";
475
                String _s = "";
476
                
477
                while (rs.next()) {
478
                        _s = rs.getString(2);
479
                        _n = rs.getString(3);
480
                        _n = (_s == null || _s.length()==0) ? _n : _s + "." + _n; 
481
                        _t = rs.getString(4);
482
                        lmodel.addElement( new TablesListItemSimple(_n, _t, src.getConnection()) );
483
                }
484
                rs.close();
485
                
486
                getTablesList().setModel(lmodel);
487
                getTablesScrollPane().setViewportView(tablesList);
488
                getTablesScrollPane().updateUI();
489
                
490
                
491
        }
492

    
493
        private boolean validFormSettings() {
494
                
495
                int count = tablesList.getModel().getSize();
496
                for (int i = 0; i < count; i++) {
497
                        TablesListItemSimple item = (TablesListItemSimple) tablesList.getModel().getElementAt(i);
498
                        if (item.isSelected()) {
499
                                return true;
500
                        }
501
                }
502
                return false;
503
        }
504

    
505
        public void checkFinishable() {
506
                boolean finishable = validFormSettings();
507
                parent_dialog.getJPanel().getOkButton().setEnabled(finishable);
508
        }
509

    
510
        /**
511
         * This method initializes jdbcButton
512
         *
513
         * @return javax.swing.JButton
514
         */
515
         private JButton getJdbcButton() {
516
                if (dbButton == null) {
517
                        dbButton = new JButton();
518
                        dbButton.addActionListener(this);
519
                        dbButton.setToolTipText(PluginServices.getText(this,
520
                                        "add_connection"));
521
                        dbButton.setBounds(new java.awt.Rectangle(
522
                                        _width-3*_margin-_conn_button_width,
523
                                        3*_margin,
524
                                        _conn_button_width, _conn_button_height));
525

    
526
                        String _file = createResourceUrl("images/jdbc.png").getFile();
527
                        dbButton.setIcon(new ImageIcon(_file));
528
                }
529

    
530
                return dbButton;
531
         }
532

    
533
         private ConnectionWithParams addNewConnection() {
534
                 ConnectionWithParams resp = null;
535

    
536
                 DBConnectionParamsDialog newco = new DBConnectionParamsDialog();
537
                 newco.showDialog();
538

    
539
                 if (newco.isOkPressed()) {
540
                         String _drvname = newco.getConnectionDriverName();
541
                         String _host = newco.getConnectionServerUrl();
542
                         String _port = newco.getConnectionPort();
543
                         String _dbname = newco.getConnectionDBName();
544
                         String _user = newco.getConnectionUser();
545
                         String _pw = newco.getConnectionPassword();
546
                         String _sche = newco.getConnectionSchema();
547
                         String _conn_usr_name = newco.getConnectionName();
548

    
549
                         boolean hasToBeCon = newco.hasToBeConnected();
550

    
551
                         try {
552
                                 resp = SingleDBConnectionManager.instance()
553
                                 .getConnection(_drvname,
554
                                                 _user, _pw, _conn_usr_name, _host, _port, _dbname, _sche,
555
                                                 hasToBeCon);
556
                         }
557
                         catch (DBException e) {
558
                                 showConnectionErrorMessage(e.getMessage());
559

    
560
                                 return null;
561
                         }
562
                         SingleVectorialDBConnectionExtension.saveAllToPersistence();
563
                         return resp;
564
                 }
565
                 else {
566
                         return null;
567
                 }
568
         }
569

    
570
         private void showConnectionErrorMessage(String _msg) {
571
                 String msg = (_msg.length() > 300) ? "" : (": " + _msg);
572
                 String title = PluginServices.getText(this, "connection_error");
573
                 JOptionPane.showMessageDialog(this, title + msg, title,
574
                                 JOptionPane.ERROR_MESSAGE);
575
         }
576

    
577
         private java.net.URL createResourceUrl(String path) {
578
                 return getClass().getClassLoader().getResource(path);
579
         }
580

    
581
        public String getDriverName() {
582
                if (selectedDataSource == null) {
583
                        logger.error("CWP is NULL (?)");
584
                        return null;
585
                } else {
586
                        return selectedDataSource.getDrvName();
587
                }
588
        }
589

    
590
        public String getPort() {
591
                if (selectedDataSource == null) {
592
                        logger.error("CWP is NULL (?)");
593
                        return null;
594
                } else {
595
                        return selectedDataSource.getPort();
596
                }
597
        }
598

    
599
        public String getHost() {
600
                if (selectedDataSource == null) {
601
                        logger.error("CWP is NULL (?)");
602
                        return null;
603
                } else {
604
                        return selectedDataSource.getHost();
605
                }
606
        }
607

    
608
        public String getDataBase() {
609
                if (selectedDataSource == null) {
610
                        logger.error("CWP is NULL (?)");
611
                        return null;
612
                } else {
613
                        return selectedDataSource.getDb();
614
                }
615
        }
616

    
617
        public String getUser() {
618
                if (selectedDataSource == null) {
619
                        logger.error("CWP is NULL (?)");
620
                        return null;
621
                } else {
622
                        return selectedDataSource.getUser();
623
                }
624
        }
625

    
626
        public String getPassword() {
627
                if (selectedDataSource == null) {
628
                        logger.error("CWP is NULL (?)");
629
                        return null;
630
                } else {
631
                        return selectedDataSource.getPw();
632
                }
633
        }
634

    
635
        public String getTable() {
636
                ListModel lm = getTablesList().getModel();
637
                int sz = lm.getSize();
638
                TablesListItemSimple item = null;
639
                for (int i=0; i<sz; i++) {
640
                        if (lm.getElementAt(i) instanceof TablesListItemSimple) {
641
                                item = (TablesListItemSimple) lm.getElementAt(i);
642
                                if (item.isSelected()) {
643
                                        return item.getTableName();
644
                                }
645
                        }
646
                }
647
                logger.error("NO check box selected in table list (?)");
648
                return "";
649
        }
650

    
651
        public boolean setActingTable(TablesListItemSimple acttab) {
652

    
653
                String drvn = selectedDataSource.getDrvName();
654
                IConnection iconn = selectedDataSource.getConnection();
655
                
656
                Driver drv = SingleDBConnectionManager.getInstanceFromName(drvn);
657
                String tn = "";
658
                if (drv instanceof AlphanumericDBDriver && iconn instanceof ConnectionJDBC) {
659
                        ConnectionJDBC conn = (ConnectionJDBC) iconn;
660
                        AlphanumericDBDriver alpha_drv = (AlphanumericDBDriver) drv;
661
                        
662
                        tn = acttab.getTableName();
663

    
664
                        String _sql = "SELECT * FROM " + tn + " WHERE (0=1)";
665
                        FieldDescription[] fd = null;
666
                        try {
667
                                alpha_drv.open(conn.getConnection(), _sql);
668
                                fd = alpha_drv.getTableDefinition().getFieldsDesc();
669
                        } catch (Exception ex) {
670
                                logger.error("While getting fields: " + ex.getMessage());
671
                                JOptionPane.showMessageDialog(this,
672
                                                PluginServices.getText(this, "Error_while_getting_table_fields") + ": " + ex.getMessage(),
673
                                                PluginServices.getText(this, "Error"),
674
                                                JOptionPane.ERROR_MESSAGE);
675
                                return false;
676
                        }
677
                        
678
                        int fld_cnt = fd.length;
679
                        ((DefaultListModel) getFieldsList().getModel()).clear();
680
                        String ft = "Other";
681
                        for (int i=0; i<fld_cnt; i++) {
682
                                String fn = fd[i].getFieldName();
683
                                
684
                                try {
685
                                        ft = typeToString(fd[i].getFieldType());
686
                                } catch (Exception ex) {
687
                                        ft = "Other";
688
                                }
689
                                
690
                                ((DefaultListModel) getFieldsList().getModel()).addElement(
691
                                                new FieldListItem(fn,ft));
692
                        }
693
                        this.getFieldsScrollPane().setViewportView(getFieldsList());
694
                        getFieldsScrollPane().updateUI();
695
                        
696
                        ((TitledBorder) getFieldsPanel().getBorder()).setTitle(
697
                                        PluginServices.getText(this, "Fields_for_table") + ": " + tn);
698
                        
699
                        try {
700
                                alpha_drv.close();
701
                        } catch (Exception ex) {
702
                                logger.error("Unreported error while closing temp driver: " + ex.getMessage());
703
                        }
704
                        
705
                        alpha_drv = null;
706
                        return true;
707
                } else {
708
                        logger.error("Unexpacted driver or conn class, drv : " + drv.getClass().getName());
709
                        logger.error("Unexpacted driver or conn class, conn: " + iconn.getClass().getName());
710
                        return false;
711
                }
712
                
713
        }
714
        
715
                
716
        public static String typeToString(int sqlType) {
717
                switch (sqlType) {
718
                case Types.BIGINT:
719
                case Types.INTEGER:
720
                case Types.SMALLINT:
721
                case Types.TINYINT:
722
                        return "Integer";
723

    
724
                case Types.NUMERIC:
725
                        return "Numeric";
726

    
727
                case Types.BIT:
728
                case Types.BOOLEAN:
729
                        return "Boolean";
730

    
731
                case Types.CHAR:
732
                case Types.VARCHAR:
733
                case Types.LONGVARCHAR:
734
                        return "String";
735

    
736
                case Types.DATE:
737
                        return "Date";
738

    
739
                case Types.FLOAT:
740
                case Types.DOUBLE:
741
                case Types.DECIMAL:
742
                case Types.REAL:
743
                        return "Double";
744

    
745
                case Types.BINARY:
746
                case Types.VARBINARY:
747
                case Types.LONGVARBINARY:
748
                        return "Binary";
749

    
750
                case Types.TIMESTAMP:
751
                        return "Timestamp";
752

    
753
                case Types.TIME:
754
                        return "Time";
755

    
756
                case Types.OTHER:
757
                default:
758
                        throw new RuntimeException("Type not recognized: " + sqlType);
759
                }
760
        }
761

    
762
        private JList getFieldsList() {
763
                if (_fieldsList == null) {
764
                        _fieldsList = new JList();
765
                        _fieldsList.setSelectionModel(new NoSelectionModel());
766
                        DefaultListModel lmodel = new DefaultListModel();
767
                        _fieldsList.setModel(lmodel);
768
                }
769
                return _fieldsList;
770
        }
771
        
772
        private class FieldListItem extends JLabel {
773
                
774
                private String name = "";
775
                private String type = "";
776
                
777
                public FieldListItem(String _n, String _t) {
778
                        name = _n;
779
                        type = _t;
780
                }
781
                
782
                public String toString() {
783
                        if (name == null || name.length() == 0) {
784
                                return "[-]";
785
                        } else {
786
                                return (type == null || type.length() == 0) ? name : (name+" [" + type + "]");
787
                        }
788
                }
789
                
790
        }
791
        
792
        private class NoSelectionModel extends DefaultListSelectionModel {
793
                public NoSelectionModel() {
794
                }
795
                public void addSelectionInterval(int a, int b) {  }
796
                public boolean isSelectedIndex(int a) { return false; }
797
                public boolean isSelectionEmpty() { return true; }
798
        }
799

    
800
        public void clearFieldsList() {
801
                ((DefaultListModel) getFieldsList().getModel()).clear();
802
                getFieldsScrollPane().updateUI();
803
        }
804

    
805
    
806
    
807

    
808
} 
809

    
810

    
811
// [eiel-gestion-conexiones]