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
/* 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
import java.awt.BorderLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.util.ArrayList;
49
import java.util.Iterator;
50
import java.util.List;
51

    
52
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
import org.gvsig.fmap.dal.DALLocator;
64
import org.gvsig.fmap.dal.DataManager;
65
import org.gvsig.fmap.dal.DataStoreParameters;
66
import org.gvsig.fmap.dal.exception.DataException;
67
import org.gvsig.fmap.dal.exception.InitializeException;
68
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
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
72
import org.gvsig.fmap.geom.primitive.Envelope;
73
import org.gvsig.gui.beans.swing.JButton;
74

    
75
import com.iver.andami.PluginServices;
76
import com.iver.andami.messages.NotificationManager;
77
import com.iver.andami.ui.mdiManager.IWindow;
78
import com.iver.cit.gvsig.SingleVectorialDBConnectionExtension;
79
import com.iver.cit.gvsig.gui.WizardPanel;
80
import com.iver.cit.gvsig.project.documents.view.gui.View;
81

    
82

    
83
/**
84
 * Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
85
 * 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

    
94
    private static final String GEODB_WIZARD_TAB_NAME = "GeoDB";
95
    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
    private DBServerExplorerParameters dbExplorerParameters;
106
    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
        setTabName(GEODB_WIZARD_TAB_NAME);
120
        setLayout(null);
121
        setSize(512, 478);
122

    
123
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
124

    
125
        if (iw == null) {
126
            return;
127
        }
128

    
129
        if ((iw instanceof View)) {
130
                 view = (View) iw;
131
             setMapCtrl(view.getMapControl());
132
        }
133
        emptySettingsPanel = new UserTableSettingsPanel(null, null, "",
134
                         getMapCtrl(), true, this, null);
135
        emptyFieldsPanel = new UserSelectedFieldsPanel(null, true, this);
136

    
137
        add(getNamePanel(), null);
138
        loadVectorialDBDatasourcesCombo(null);
139

    
140
        add(getTablesPanel(), null);
141
    }
142

    
143
    private void loadVectorialDBDatasourcesCombo(MyExplorer sel) {
144
        getDatasourceComboBox().removeAllItems();
145
        DataManager dm=DALLocator.getDataManager();
146
        List names = dm.getStoreProviders();
147
                if (sel!=null) {
148
                        getDatasourceComboBox().addItem(sel);
149
                }
150

    
151
        if (names == null) {
152
            return;
153
        }
154

    
155
    }
156

    
157
    public void initWizard() {
158
    }
159

    
160
    public void execute() {
161
            SingleVectorialDBConnectionExtension.saveAllToPersistence();
162
    }
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
        if (datasourceComboBox.getItemCount()==0) {
279
                        setEmptyPanels();
280
                }
281
            Object src = arg0.getSource();
282

    
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
            MyExplorer sel = addNewConnection();
295

    
296
            if (sel != null) {
297
                    dbExplorerParameters=sel.getDbSeverExplorerParameters();
298
                    loadVectorialDBDatasourcesCombo(sel);
299
                getDatasourceComboBox().setSelectedItem(sel);
300

    
301
            }
302
            updateTableList(dbExplorerParameters);
303
        }
304
    }
305

    
306
    private MyExplorer addNewConnection() {
307
            MyExplorer myExplorer=new MyExplorer();
308
        DBServerExplorerParameters resp = null;
309

    
310
        VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
311
        newco.showDialog();
312

    
313
        if (newco.isOkPressed()) {
314
                try {
315
                                resp=newco.getParameters();
316
                        } catch (DataException e) {
317
                                showConnectionErrorMessage(e.getMessage());
318
                        return null;
319
                        }
320
            SingleVectorialDBConnectionExtension.saveAllToPersistence();
321
            myExplorer.setDbExplorerParameters(resp);
322
            myExplorer.setName(newco.getConnectionName());
323
            return myExplorer;
324
        }
325
        else {
326
            return null;
327
        }
328
    }
329

    
330

    
331

    
332
    private void updateTableList(
333
                        DBServerExplorerParameters dbSeverExplorerParameters2) {
334
                if (dbSeverExplorerParameters2 == null) {
335
                  return;
336
          }
337
            DataManager dm = DALLocator.getDataManager();
338
            DBServerExplorer dbExplorer;
339
            try {
340
                    dbExplorer = (DBServerExplorer) dm
341
                                        .createServerExplorer(dbSeverExplorerParameters2);
342
                    List parameters = dbExplorer
343
                                        .list();
344

    
345
                    DefaultListModel lmodel = new DefaultListModel();
346

    
347
                    Iterator iter = parameters.iterator();
348
                    DBStoreParameters param;
349
                    while (iter.hasNext()) {
350
                            param = (DBStoreParameters) iter.next();
351
                            lmodel.addElement(new TablesListItem(dbExplorer, param,
352
                                            getMapCtrl(), this));
353
                    }
354

    
355
                    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
                        NotificationManager.showMessageError("While getting table names: "
361
                                        + e.getMessage(), e);
362
                    return;
363
            } catch (DataException e) {
364
                    logger.error("While getting table names: " + e.getMessage(), e);
365
                        NotificationManager.showMessageError("While getting table names: "
366
                                        + e.getMessage(), e);
367
                    return;
368
            } 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
    }
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
            setSettingsPanels(selected);
383
            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
            // FIXME use standard icon theme
428
                        String _file = createResourceUrl("images/jdbc.png").getFile();
429
            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
    public void setSettingsPanels(TablesListItem actTable){
448
        if (actTable == null) {
449
            setEmptyPanels();
450

    
451
            return;
452
        }
453
        String abrev=null;
454
        if (getMapCtrl()!=null){
455
                abrev=getMapCtrl().getViewPort().getProjection()
456
                       .getAbrev();
457
        }
458
        settingsPanel = actTable.getUserTableSettingsPanel(abrev);
459
        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

    
500
        public DataStoreParameters[] getParameters() {
501
                try {
502
            TablesListItem[] selected = getSelectedTables();
503
            int count = selected.length;
504
            DBStoreParameters[] dbParameters = new DBStoreParameters[count];
505
            String strEPSG =null;
506
            if (getMapCtrl()!=null) {
507
                                strEPSG = getMapCtrl().getViewPort().getProjection()
508
                                 .getAbrev();
509
                        }
510

    
511
            for (int i = 0; i < count; i++) {
512
                TablesListItem item = selected[i];
513

    
514

    
515
                DBStoreParameters parameters = item.getParameters();
516

    
517
                UserTableSettingsPanel userTableSettingsPanel=item.getUserTableSettingsPanel(strEPSG);
518
                Envelope _wa = userTableSettingsPanel.getWorkingArea();
519

    
520
                                //                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

    
533
                String fidField = userTableSettingsPanel.getIdFieldName();
534
                String geomField = userTableSettingsPanel.getGeoFieldName();
535
                String[] fields = item.getUserSelectedFieldsPanel()
536
                        .getUserSelectedFields(fidField, geomField);
537

    
538
                                //                parameters.setSchema(schema);
539
                                //                parameters.setTableName(tableName);
540

    
541
                if (userTableSettingsPanel.isSqlActive()) {
542
                    String whereClause = userTableSettingsPanel.getWhereClause();
543
                    parameters.setInitialFilter(whereClause);
544
                }
545
                else {
546
                    parameters.setInitialFilter("");
547
                }
548

    
549
                parameters.setFields(fields);
550
                parameters.setDefaultGeometry(geomField);
551
                                parameters.setPkFields(new String[] { fidField });
552

    
553
                if (_wa != null) {
554
                   parameters.setWorkingArea(_wa);
555
                }
556

    
557
                parameters.setSRSID(strEPSG);
558
                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
} //  @jve:decl-index=0:visual-constraint="10,10"