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 7304 caballero
/* 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 34767 jldominguez
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 31080 nbrodin
import java.awt.event.FocusEvent;
49
import java.awt.event.FocusListener;
50 34767 jldominguez
import java.awt.event.ItemEvent;
51
import java.awt.event.ItemListener;
52 31080 nbrodin
import java.awt.event.KeyEvent;
53
import java.awt.event.KeyListener;
54 34767 jldominguez
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 31080 nbrodin
60 7304 caballero
import javax.swing.DefaultComboBoxModel;
61 34767 jldominguez
import javax.swing.DefaultListModel;
62
import javax.swing.DefaultListSelectionModel;
63
import javax.swing.ImageIcon;
64 7304 caballero
import javax.swing.JComboBox;
65
import javax.swing.JLabel;
66 34767 jldominguez
import javax.swing.JList;
67
import javax.swing.JOptionPane;
68 7304 caballero
import javax.swing.JPanel;
69
import javax.swing.JPasswordField;
70 34767 jldominguez
import javax.swing.JScrollPane;
71 7304 caballero
import javax.swing.JTextField;
72 34767 jldominguez
import javax.swing.ListModel;
73
import javax.swing.ListSelectionModel;
74
import javax.swing.border.TitledBorder;
75
import javax.swing.event.ListSelectionEvent;
76 7304 caballero
77 34767 jldominguez
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 7304 caballero
import com.iver.andami.PluginServices;
85 34767 jldominguez
import com.iver.andami.messages.NotificationManager;
86
import com.iver.andami.ui.mdiManager.IWindow;
87
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension;
88 31080 nbrodin
import com.iver.cit.gvsig.addlayer.AddLayerDialog;
89 34767 jldominguez
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 7304 caballero
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
103 34767 jldominguez
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 7304 caballero
/**
113
 * DOCUMENT ME!
114
 *
115
 * @author Fernando Gonz?lez Cort?s
116 34767 jldominguez
 * @author jldominguez
117 7304 caballero
 */
118 34767 jldominguez
public class DataBaseOpenDialog extends JPanel implements ActionListener {
119 31080 nbrodin
120 34767 jldominguez
        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 7304 caballero
    /**
132
     * This is the default constructor
133
     */
134 34767 jldominguez
    public DataBaseOpenDialog(AddLayerDialog dlg) {
135 7304 caballero
        super();
136 34767 jldominguez
        parent_dialog = dlg;
137 7304 caballero
        initialize();
138
    }
139
140
    /**
141 31080 nbrodin
     * This is the default constructor
142
     */
143
    public DataBaseOpenDialog() {
144
        super();
145
        initialize();
146
    }
147 7304 caballero
148 34767 jldominguez
        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 7304 caballero
168 34767 jldominguez
        private void initialize() {
169
                setLayout(null);
170
                this.setSize(_width, _height);
171 7304 caballero
172 34767 jldominguez
                add(getConnectionPanel(), null);
173
                loadDBDatasourcesCombo();
174
                getDatasourceComboBox().addActionListener(this);
175
                add(getTablesPanel(), null);
176
                add(getFieldsPanel(), null);
177
        }
178 7304 caballero
179 34767 jldominguez
        private JPanel getFieldsPanel() {
180 7304 caballero
181 34767 jldominguez
                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 7304 caballero
196 34767 jldominguez
                return fieldsPanel;
197
        }
198 7304 caballero
199 7907 caballero
200
201
202 34767 jldominguez
        private JScrollPane getFieldsScrollPane() {
203
                if (_fieldsScrollPane == null) {
204
                        _fieldsScrollPane = new JScrollPane();
205
                }
206 7907 caballero
207 34767 jldominguez
                return _fieldsScrollPane;
208
        }
209 7907 caballero
210 34767 jldominguez
        protected void loadDBDatasourcesCombo() {
211
                getDatasourceComboBox().removeAllItems();
212 7907 caballero
213 34767 jldominguez
                getDatasourceComboBox().addItem(new ConnectionWithParams());
214 7907 caballero
215 34767 jldominguez
                ConnectionWithParams[] conn = SingleDBConnectionManager.instance()
216
                .getAllConnections();
217 7907 caballero
218 34767 jldominguez
                if (conn == null) {
219
                        return;
220
                }
221 7907 caballero
222 34767 jldominguez
                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 7907 caballero
232 34767 jldominguez
        public void saveConns() {
233
                SingleVectorialDBConnectionExtension.saveAllToPersistence();
234
        }
235 7907 caballero
236 34767 jldominguez
        protected String getSelectedTable() {
237
                return null;
238
        }
239
240
        /**
241
         * This method initializes namePanel
242 7907 caballero
         *
243 34767 jldominguez
         * @return javax.swing.JPanel
244 7907 caballero
         */
245 34767 jldominguez
        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 7304 caballero
                }
257 34767 jldominguez
258
                return connPanel;
259 7304 caballero
        }
260 34767 jldominguez
261 7304 caballero
        /**
262 34767 jldominguez
         * This method initializes tablesPanel
263 7907 caballero
         *
264 34767 jldominguez
         * @return javax.swing.JPanel
265 7907 caballero
         */
266 34767 jldominguez
        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 7304 caballero
                }
277 34767 jldominguez
278
                return tablesPanel;
279 7304 caballero
        }
280 34767 jldominguez
281 7304 caballero
        /**
282 34767 jldominguez
         * This method initializes tablesScrollPane
283 7907 caballero
         *
284 34767 jldominguez
         * @return javax.swing.JScrollPane
285 7907 caballero
         */
286 34767 jldominguez
        private JScrollPane getTablesScrollPane() {
287
                if (tablesScrollPane == null) {
288
                        tablesScrollPane = new JScrollPane();
289 7304 caballero
                }
290 34767 jldominguez
291
                return tablesScrollPane;
292 7304 caballero
        }
293 34767 jldominguez
294 7304 caballero
        /**
295 34767 jldominguez
         * This method initializes tablesList
296 7907 caballero
         *
297 34767 jldominguez
         * @return javax.swing.JList
298 7907 caballero
         */
299 34767 jldominguez
        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 7304 caballero
                }
306 34767 jldominguez
307
                return tablesList;
308 7304 caballero
        }
309 31080 nbrodin
310 34767 jldominguez
        /**
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 31080 nbrodin
401 34767 jldominguez
                ((TitledBorder) getFieldsPanel().getBorder()).setTitle(
402
                                PluginServices.getText(this, "Fields_for_table") + ": -");
403
                ((DefaultListModel) getFieldsList().getModel()).clear();
404
                getFieldsScrollPane().updateUI();
405
406
                checkFinishable();
407 31080 nbrodin
        }
408
409 34767 jldominguez
        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 31080 nbrodin
                }
425 34767 jldominguez
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 31080 nbrodin
        }
437
438 34767 jldominguez
        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 31080 nbrodin
        }
492
493 34767 jldominguez
        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 31080 nbrodin
        }
504
505 34767 jldominguez
        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 31080 nbrodin
                }
529 34767 jldominguez
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 31080 nbrodin
        }
589 34767 jldominguez
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]