Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / WizardVectorialDB.java @ 27937

History | View | Annotate | Download (17.5 KB)

1 11971 caballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop 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
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package com.prodevelop.cit.gvsig.vectorialdb.wizard;
44
45 12826 jaume
import java.awt.BorderLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.util.ArrayList;
49 27917 jmvivo
import java.util.Iterator;
50 24760 jmvivo
import java.util.List;
51 11971 caballero
52 12826 jaume
import javax.swing.DefaultListModel;
53
import javax.swing.ImageIcon;
54
import javax.swing.JComboBox;
55
import javax.swing.JOptionPane;
56
import javax.swing.JPanel;
57
import javax.swing.JScrollPane;
58
import javax.swing.ListSelectionModel;
59
import javax.swing.event.ListSelectionEvent;
60
import javax.swing.event.ListSelectionListener;
61
62
import org.apache.log4j.Logger;
63 24759 jmvivo
import org.gvsig.fmap.dal.DALLocator;
64
import org.gvsig.fmap.dal.DataManager;
65
import org.gvsig.fmap.dal.DataStoreParameters;
66 27917 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
67 24759 jmvivo
import org.gvsig.fmap.dal.exception.InitializeException;
68 27917 jmvivo
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
69
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer;
70
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
71 27932 jmvivo
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
72 22035 vcaballero
import org.gvsig.fmap.geom.primitive.Envelope;
73 12826 jaume
import org.gvsig.gui.beans.swing.JButton;
74
75 11971 caballero
import com.iver.andami.PluginServices;
76
import com.iver.andami.messages.NotificationManager;
77
import com.iver.andami.ui.mdiManager.IWindow;
78 15684 vcaballero
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension;
79 11971 caballero
import com.iver.cit.gvsig.gui.WizardPanel;
80
import com.iver.cit.gvsig.project.documents.view.gui.View;
81
82
83
/**
84 14366 jldominguez
 * Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
85 11971 caballero
 * Multi-table selection available.
86
 *
87
 * @author jldominguez
88
 *
89
 */
90
public class WizardVectorialDB extends WizardPanel implements ActionListener,
91
    ListSelectionListener {
92
    private static Logger logger = Logger.getLogger(WizardVectorialDB.class.getName());
93 15684 vcaballero
94 14366 jldominguez
    private static final String GEODB_WIZARD_TAB_NAME = "GeoDB";
95 11971 caballero
    private JPanel namePanel = null;
96
    private JPanel tablesPanel = null;
97
    private JScrollPane tablesScrollPane = null;
98
    private AvailableTablesCheckBoxList tablesList = null;
99
    private JComboBox datasourceComboBox = null;
100
    private UserTableSettingsPanel settingsPanel = null;
101
    private UserSelectedFieldsPanel fieldsPanel = null;
102
    private UserTableSettingsPanel emptySettingsPanel = null;
103
    private UserSelectedFieldsPanel emptyFieldsPanel = null;
104
    private JButton dbButton = null;
105 27917 jmvivo
    private DBServerExplorerParameters dbExplorerParameters;
106 11971 caballero
    private View view = null;
107
108
    public WizardVectorialDB() {
109
        super();
110
        initialize();
111
    }
112
113
    /**
114
     * This method initializes this
115
     *
116
     * @return void
117
     */
118
    private void initialize() {
119 14366 jldominguez
        setTabName(GEODB_WIZARD_TAB_NAME);
120 11971 caballero
        setLayout(null);
121
        setSize(512, 478);
122
123
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
124
125
        if (iw == null) {
126
            return;
127
        }
128
129 22653 vcaballero
        if ((iw instanceof View)) {
130
                 view = (View) iw;
131
             setMapCtrl(view.getMapControl());
132 11971 caballero
        }
133
        emptySettingsPanel = new UserTableSettingsPanel(null, null, "",
134 22653 vcaballero
                         getMapCtrl(), true, this, null);
135 21910 vcaballero
        emptyFieldsPanel = new UserSelectedFieldsPanel(null, true, this);
136 11971 caballero
137
        add(getNamePanel(), null);
138 22035 vcaballero
        loadVectorialDBDatasourcesCombo(null);
139 11971 caballero
140
        add(getTablesPanel(), null);
141
    }
142
143 22128 vcaballero
    private void loadVectorialDBDatasourcesCombo(MyExplorer sel) {
144 11971 caballero
        getDatasourceComboBox().removeAllItems();
145 24563 vcaballero
        DataManager dm=DALLocator.getDataManager();
146 24760 jmvivo
        List names = dm.getStoreProviders();
147 22142 jmvivo
                if (sel!=null) {
148 22128 vcaballero
                        getDatasourceComboBox().addItem(sel);
149 22142 jmvivo
                }
150 22035 vcaballero
151 21893 vcaballero
        if (names == null) {
152 11971 caballero
            return;
153
        }
154
155
    }
156
157
    public void initWizard() {
158
    }
159
160
    public void execute() {
161 15684 vcaballero
            SingleVectorialDBConnectionExtension.saveAllToPersistence();
162 11971 caballero
    }
163
164
    private TablesListItem[] getSelectedTables() {
165
        int count = tablesList.getModel().getSize();
166
        ArrayList resp = new ArrayList();
167
168
        for (int i = 0; i < count; i++) {
169
            TablesListItem item = (TablesListItem) tablesList.getModel()
170
                                                             .getElementAt(i);
171
172
            if (item.isSelected()) {
173
                resp.add(item);
174
            }
175
        }
176
177
        return (TablesListItem[]) resp.toArray(new TablesListItem[0]);
178
    }
179
180
    /**
181
     * This method initializes namePanel
182
     *
183
     * @return javax.swing.JPanel
184
     */
185
    private JPanel getNamePanel() {
186
        if (namePanel == null) {
187
            namePanel = new JPanel();
188
            namePanel.setLayout(null);
189
            namePanel.setBounds(new java.awt.Rectangle(5, 5, 501, 51));
190
            namePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
191
                    null, PluginServices.getText(this, "choose_connection"),
192
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
193
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
194
            namePanel.add(getDatasourceComboBox(), null);
195
            namePanel.add(getJdbcButton(), null);
196
        }
197
198
        return namePanel;
199
    }
200
201
    /**
202
     * This method initializes tablesPanel
203
     *
204
     * @return javax.swing.JPanel
205
     */
206
    private JPanel getTablesPanel() {
207
        if (tablesPanel == null) {
208
            tablesPanel = new JPanel();
209
            tablesPanel.setLayout(new BorderLayout());
210
            tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
211
                    null, PluginServices.getText(this, "choose_table"),
212
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
213
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
214
            tablesPanel.setBounds(new java.awt.Rectangle(5, 55, 246, 191));
215
            tablesPanel.add(getTablesScrollPane(), java.awt.BorderLayout.CENTER);
216
        }
217
218
        return tablesPanel;
219
    }
220
221
    /**
222
     * This method initializes settingsPanel
223
     *
224
     * @return javax.swing.JPanel
225
     */
226
227
    /**
228
     * This method initializes tablesScrollPane
229
     *
230
     * @return javax.swing.JScrollPane
231
     */
232
    private JScrollPane getTablesScrollPane() {
233
        if (tablesScrollPane == null) {
234
            tablesScrollPane = new JScrollPane();
235
            tablesScrollPane.setViewportView(getTablesList());
236
        }
237
238
        return tablesScrollPane;
239
    }
240
241
    /**
242
     * This method initializes tablesList
243
     *
244
     * @return javax.swing.JList
245
     */
246
    private AvailableTablesCheckBoxList getTablesList() {
247
        if (tablesList == null) {
248
            tablesList = new AvailableTablesCheckBoxList(this);
249
            tablesList.addListSelectionListener(this);
250
            tablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
251
        }
252
253
        return tablesList;
254
    }
255
256
    /**
257
     * This method initializes layerNameTextField
258
     *
259
     * @return javax.swing.JTextField
260
     */
261
262
    /**
263
     * This method initializes jComboBox
264
     *
265
     * @return javax.swing.JComboBox
266
     */
267
    private JComboBox getDatasourceComboBox() {
268
        if (datasourceComboBox == null) {
269
            datasourceComboBox = new JComboBox();
270
            datasourceComboBox.setBounds(new java.awt.Rectangle(10, 20, 446, 21));
271
            datasourceComboBox.addActionListener(this);
272
        }
273
274
        return datasourceComboBox;
275
    }
276
277
    public void actionPerformed(ActionEvent arg0) {
278 22142 jmvivo
        if (datasourceComboBox.getItemCount()==0) {
279
                        setEmptyPanels();
280
                }
281 22035 vcaballero
            Object src = arg0.getSource();
282 11971 caballero
283
        if (src == datasourceComboBox) {
284
            Object sel_obj = datasourceComboBox.getSelectedItem();
285
286
            if (sel_obj == null) {
287
                return;
288
            }
289
            getDatasourceComboBox().repaint();
290
            return;
291
        }
292
293
        if (src == dbButton) {
294 22128 vcaballero
            MyExplorer sel = addNewConnection();
295 21910 vcaballero
296
            if (sel != null) {
297 27917 jmvivo
                    dbExplorerParameters=sel.getDbSeverExplorerParameters();
298 22035 vcaballero
                    loadVectorialDBDatasourcesCombo(sel);
299 21910 vcaballero
                getDatasourceComboBox().setSelectedItem(sel);
300 22035 vcaballero
301 21910 vcaballero
            }
302 22035 vcaballero
            updateTableList(dbExplorerParameters);
303 21910 vcaballero
        }
304
    }
305
306 22128 vcaballero
    private MyExplorer addNewConnection() {
307
            MyExplorer myExplorer=new MyExplorer();
308 27917 jmvivo
        DBServerExplorerParameters resp = null;
309 21910 vcaballero
310
        VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
311
        newco.showDialog();
312
313
        if (newco.isOkPressed()) {
314
                try {
315
                                resp=newco.getParameters();
316 27917 jmvivo
                        } catch (DataException e) {
317 21910 vcaballero
                                showConnectionErrorMessage(e.getMessage());
318
                        return null;
319
                        }
320
            SingleVectorialDBConnectionExtension.saveAllToPersistence();
321 22128 vcaballero
            myExplorer.setDbExplorerParameters(resp);
322
            myExplorer.setName(newco.getConnectionName());
323
            return myExplorer;
324 11971 caballero
        }
325 21910 vcaballero
        else {
326
            return null;
327
        }
328 11971 caballero
    }
329
330 21910 vcaballero
331 11971 caballero
332 27917 jmvivo
    private void updateTableList(
333
                        DBServerExplorerParameters dbSeverExplorerParameters2) {
334
                if (dbSeverExplorerParameters2 == null) {
335 22653 vcaballero
                  return;
336
          }
337 24759 jmvivo
            DataManager dm = DALLocator.getDataManager();
338 27917 jmvivo
            DBServerExplorer dbExplorer;
339 22035 vcaballero
            try {
340 27917 jmvivo
                    dbExplorer = (DBServerExplorer) dm
341
                                        .createServerExplorer(dbSeverExplorerParameters2);
342
                    List parameters = dbExplorer
343 23342 jmvivo
                                        .list();
344 22035 vcaballero
345
                    DefaultListModel lmodel = new DefaultListModel();
346 11971 caballero
347 27917 jmvivo
                    Iterator iter = parameters.iterator();
348
                    DBStoreParameters param;
349
                    while (iter.hasNext()) {
350
                            param = (DBStoreParameters) iter.next();
351
                            lmodel.addElement(new TablesListItem(dbExplorer, param,
352 22035 vcaballero
                                            getMapCtrl(), this));
353
                    }
354 11971 caballero
355 22035 vcaballero
                    getTablesList().setModel(lmodel);
356
                    getTablesScrollPane().setViewportView(tablesList);
357
                    tablesScrollPane.updateUI();
358
            } catch (InitializeException e) {
359
                    logger.error("While getting table names: " + e.getMessage(), e);
360 23315 jmvivo
                        NotificationManager.showMessageError("While getting table names: "
361
                                        + e.getMessage(), e);
362 22035 vcaballero
                    return;
363 27917 jmvivo
            } catch (DataException e) {
364 22035 vcaballero
                    logger.error("While getting table names: " + e.getMessage(), e);
365 23315 jmvivo
                        NotificationManager.showMessageError("While getting table names: "
366
                                        + e.getMessage(), e);
367 22035 vcaballero
                    return;
368 27917 jmvivo
            } catch (ValidateDataParametersException e) {
369
                        logger.error("While getting table names: " + e.getMessage(), e);
370
                        NotificationManager.showMessageError("While getting table names: "
371
                                        + e.getMessage(), e);
372
                        return;
373
                }
374 11971 caballero
    }
375
376
    public void valueChanged(ListSelectionEvent arg0) {
377
        Object src = arg0.getSource();
378
379
        if (src == tablesList) {
380
            TablesListItem selected = (TablesListItem) tablesList.getSelectedValue();
381
382 22653 vcaballero
            setSettingsPanels(selected);
383 11971 caballero
            checkFinishable();
384
        }
385
    }
386
387
    private boolean validFormSettings() {
388
        int count = tablesList.getModel().getSize();
389
390
        boolean at_least_one = false;
391
        boolean resp = true;
392
393
        for (int i = 0; i < count; i++) {
394
            TablesListItem item = (TablesListItem) tablesList.getModel()
395
                                                             .getElementAt(i);
396
397
            if (item.isSelected()) {
398
                at_least_one = true;
399
            }
400
401
            if (item.disturbsWizardValidity()) {
402
                resp = false;
403
            }
404
        }
405
406
        return (at_least_one && resp);
407
    }
408
409
    public void checkFinishable() {
410
        boolean finishable = validFormSettings();
411
        callStateChanged(finishable);
412
    }
413
414
    /**
415
     * This method initializes jdbcButton
416
     *
417
     * @return javax.swing.JButton
418
     */
419
    private JButton getJdbcButton() {
420
        if (dbButton == null) {
421
            dbButton = new JButton();
422
            dbButton.addActionListener(this);
423
            dbButton.setToolTipText(PluginServices.getText(this,
424
                    "add_connection"));
425
            dbButton.setBounds(new java.awt.Rectangle(465, 20, 26, 21));
426
427 27937 jmvivo
            // FIXME use standard icon theme
428
                        String _file = createResourceUrl("images/jdbc.png").getFile();
429 11971 caballero
            dbButton.setIcon(new ImageIcon(_file));
430
        }
431
432
        return dbButton;
433
    }
434
435
436
    private void showConnectionErrorMessage(String _msg) {
437
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
438
        String title = PluginServices.getText(this, "connection_error");
439
        JOptionPane.showMessageDialog(this, title + msg, title,
440
            JOptionPane.ERROR_MESSAGE);
441
    }
442
443
    private java.net.URL createResourceUrl(String path) {
444
        return getClass().getClassLoader().getResource(path);
445
    }
446
447 21893 vcaballero
    public void setSettingsPanels(TablesListItem actTable){
448 11971 caballero
        if (actTable == null) {
449
            setEmptyPanels();
450
451
            return;
452
        }
453 22653 vcaballero
        String abrev=null;
454
        if (getMapCtrl()!=null){
455
                abrev=getMapCtrl().getViewPort().getProjection()
456
                       .getAbrev();
457
        }
458
        settingsPanel = actTable.getUserTableSettingsPanel(abrev);
459 11971 caballero
        fieldsPanel = actTable.getUserSelectedFieldsPanel();
460
461
        removeFieldPanels();
462
        add(fieldsPanel);
463
        fieldsPanel.repaint();
464
465
        removeSettingsPanels();
466
        add(settingsPanel);
467
        settingsPanel.repaint();
468
469
        repaint();
470
    }
471
472
    private void setEmptyPanels() {
473
        removeFieldPanels();
474
        add(emptyFieldsPanel);
475
        removeSettingsPanels();
476
        add(emptySettingsPanel);
477
478
        settingsPanel = emptySettingsPanel;
479
        fieldsPanel = emptyFieldsPanel;
480
481
        repaint();
482
    }
483
484
    private void removeFieldPanels() {
485
        for (int i = 0; i < getComponentCount(); i++) {
486
            if (getComponent(i) instanceof UserSelectedFieldsPanel) {
487
                remove(i);
488
            }
489
        }
490
    }
491
492
    private void removeSettingsPanels() {
493
        for (int i = 0; i < getComponentCount(); i++) {
494
            if (getComponent(i) instanceof UserTableSettingsPanel) {
495
                remove(i);
496
            }
497
        }
498
    }
499 22653 vcaballero
500 23342 jmvivo
        public DataStoreParameters[] getParameters() {
501 22653 vcaballero
                try {
502
            TablesListItem[] selected = getSelectedTables();
503
            int count = selected.length;
504 23342 jmvivo
            DBStoreParameters[] dbParameters = new DBStoreParameters[count];
505 22653 vcaballero
            String strEPSG =null;
506 23029 jmvivo
            if (getMapCtrl()!=null) {
507
                                strEPSG = getMapCtrl().getViewPort().getProjection()
508 22653 vcaballero
                                 .getAbrev();
509 23029 jmvivo
                        }
510 22653 vcaballero
511
            for (int i = 0; i < count; i++) {
512
                TablesListItem item = selected[i];
513
514
515 23342 jmvivo
                DBStoreParameters parameters = item.getParameters();
516 22653 vcaballero
517
                UserTableSettingsPanel userTableSettingsPanel=item.getUserTableSettingsPanel(strEPSG);
518
                Envelope _wa = userTableSettingsPanel.getWorkingArea();
519
520 23029 jmvivo
                                //                String[] tokens = item.getTableName().split("\\u002E", 2);
521
                                //                String tableName;
522
                                //                String schema = null;
523
                                //                if (tokens.length > 1)
524
                                //                {
525
                                //                        schema = tokens[0];
526
                                //                        tableName = tokens[1];
527
                                //                }
528
                                //                else
529
                                //                {
530
                                //                        tableName = tokens[0];
531
                                //                }
532 22653 vcaballero
533
                String fidField = userTableSettingsPanel.getIdFieldName();
534
                String geomField = userTableSettingsPanel.getGeoFieldName();
535
                String[] fields = item.getUserSelectedFieldsPanel()
536
                        .getUserSelectedFields(fidField, geomField);
537
538 23029 jmvivo
                                //                parameters.setSchema(schema);
539
                                //                parameters.setTableName(tableName);
540 22653 vcaballero
541
                if (userTableSettingsPanel.isSqlActive()) {
542
                    String whereClause = userTableSettingsPanel.getWhereClause();
543 27932 jmvivo
                    parameters.setInitialFilter(whereClause);
544 22653 vcaballero
                }
545
                else {
546 27932 jmvivo
                    parameters.setInitialFilter("");
547 22653 vcaballero
                }
548
549
                parameters.setFields(fields);
550 27932 jmvivo
                parameters.setDefaultGeometry(geomField);
551
                                parameters.setPkFields(new String[] { fidField });
552 22653 vcaballero
553
                if (_wa != null) {
554
                   parameters.setWorkingArea(_wa);
555
                }
556
557 27932 jmvivo
                parameters.setSRSID(strEPSG);
558 22653 vcaballero
                dbParameters[i]=parameters;
559
            }
560
561
            return dbParameters;//layerArrayToGroup(all_layers, groupName);
562
        }
563
        catch (Exception e) {
564
            logger.error("While creating jdbc layer: " + e.getMessage(), e);
565
            NotificationManager.addError("Error al cargar la capa: " +
566
                e.getMessage(), e);
567
        }
568
569
        return null;
570
        }
571 11971 caballero
} //  @jve:decl-index=0:visual-constraint="10,10"