Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / WizardVectorialDB.java @ 24759

History | View | Annotate | Download (16.9 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

    
50
import javax.swing.DefaultListModel;
51
import javax.swing.ImageIcon;
52
import javax.swing.JComboBox;
53
import javax.swing.JOptionPane;
54
import javax.swing.JPanel;
55
import javax.swing.JScrollPane;
56
import javax.swing.ListSelectionModel;
57
import javax.swing.event.ListSelectionEvent;
58
import javax.swing.event.ListSelectionListener;
59

    
60
import org.apache.log4j.Logger;
61
import org.gvsig.fmap.dal.DALLocator;
62
import org.gvsig.fmap.dal.DataManager;
63
import org.gvsig.fmap.dal.DataStoreParameters;
64
import org.gvsig.fmap.dal.exception.InitializeException;
65
import org.gvsig.fmap.dal.exception.ReadException;
66
import org.gvsig.fmap.data.feature.db.DBExplorer;
67
import org.gvsig.fmap.data.feature.db.DBExplorerParameters;
68
import org.gvsig.fmap.data.feature.db.DBStoreParameters;
69
import org.gvsig.fmap.geom.primitive.Envelope;
70
import org.gvsig.gui.beans.swing.JButton;
71

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

    
79

    
80
/**
81
 * Driver-independent GeoDB wizard. Queries the drivers to fill GUI controls.
82
 * Multi-table selection available.
83
 *
84
 * @author jldominguez
85
 *
86
 */
87
public class WizardVectorialDB extends WizardPanel implements ActionListener,
88
    ListSelectionListener {
89
    private static Logger logger = Logger.getLogger(WizardVectorialDB.class.getName());
90

    
91
    private static final String GEODB_WIZARD_TAB_NAME = "GeoDB";
92
    private JPanel namePanel = null;
93
    private JPanel tablesPanel = null;
94
    private JScrollPane tablesScrollPane = null;
95
    private AvailableTablesCheckBoxList tablesList = null;
96
    private JComboBox datasourceComboBox = null;
97
    private UserTableSettingsPanel settingsPanel = null;
98
    private UserSelectedFieldsPanel fieldsPanel = null;
99
    private UserTableSettingsPanel emptySettingsPanel = null;
100
    private UserSelectedFieldsPanel emptyFieldsPanel = null;
101
    private JButton dbButton = null;
102
    private DBExplorerParameters dbExplorerParameters;
103
    private View view = null;
104

    
105
    public WizardVectorialDB() {
106
        super();
107
        initialize();
108
    }
109

    
110
    /**
111
     * This method initializes this
112
     *
113
     * @return void
114
     */
115
    private void initialize() {
116
        setTabName(GEODB_WIZARD_TAB_NAME);
117
        setLayout(null);
118
        setSize(512, 478);
119

    
120
        IWindow iw = PluginServices.getMDIManager().getActiveWindow();
121

    
122
        if (iw == null) {
123
            return;
124
        }
125

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

    
134
        add(getNamePanel(), null);
135
        loadVectorialDBDatasourcesCombo(null);
136

    
137
        add(getTablesPanel(), null);
138
    }
139

    
140
    private void loadVectorialDBDatasourcesCombo(MyExplorer sel) {
141
        getDatasourceComboBox().removeAllItems();
142
        DataManager dm=DALLocator.getDataManager();
143
        String[] names=dm.getRegisters();
144
                if (sel!=null) {
145
                        getDatasourceComboBox().addItem(sel);
146
                }
147

    
148
        if (names == null) {
149
            return;
150
        }
151

    
152
    }
153

    
154
    public void initWizard() {
155
    }
156

    
157
    public void execute() {
158
            SingleVectorialDBConnectionExtension.saveAllToPersistence();
159
    }
160

    
161
    private TablesListItem[] getSelectedTables() {
162
        int count = tablesList.getModel().getSize();
163
        ArrayList resp = new ArrayList();
164

    
165
        for (int i = 0; i < count; i++) {
166
            TablesListItem item = (TablesListItem) tablesList.getModel()
167
                                                             .getElementAt(i);
168

    
169
            if (item.isSelected()) {
170
                resp.add(item);
171
            }
172
        }
173

    
174
        return (TablesListItem[]) resp.toArray(new TablesListItem[0]);
175
    }
176

    
177
    /**
178
     * This method initializes namePanel
179
     *
180
     * @return javax.swing.JPanel
181
     */
182
    private JPanel getNamePanel() {
183
        if (namePanel == null) {
184
            namePanel = new JPanel();
185
            namePanel.setLayout(null);
186
            namePanel.setBounds(new java.awt.Rectangle(5, 5, 501, 51));
187
            namePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
188
                    null, PluginServices.getText(this, "choose_connection"),
189
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
190
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
191
            namePanel.add(getDatasourceComboBox(), null);
192
            namePanel.add(getJdbcButton(), null);
193
        }
194

    
195
        return namePanel;
196
    }
197

    
198
    /**
199
     * This method initializes tablesPanel
200
     *
201
     * @return javax.swing.JPanel
202
     */
203
    private JPanel getTablesPanel() {
204
        if (tablesPanel == null) {
205
            tablesPanel = new JPanel();
206
            tablesPanel.setLayout(new BorderLayout());
207
            tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
208
                    null, PluginServices.getText(this, "choose_table"),
209
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
210
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
211
            tablesPanel.setBounds(new java.awt.Rectangle(5, 55, 246, 191));
212
            tablesPanel.add(getTablesScrollPane(), java.awt.BorderLayout.CENTER);
213
        }
214

    
215
        return tablesPanel;
216
    }
217

    
218
    /**
219
     * This method initializes settingsPanel
220
     *
221
     * @return javax.swing.JPanel
222
     */
223

    
224
    /**
225
     * This method initializes tablesScrollPane
226
     *
227
     * @return javax.swing.JScrollPane
228
     */
229
    private JScrollPane getTablesScrollPane() {
230
        if (tablesScrollPane == null) {
231
            tablesScrollPane = new JScrollPane();
232
            tablesScrollPane.setViewportView(getTablesList());
233
        }
234

    
235
        return tablesScrollPane;
236
    }
237

    
238
    /**
239
     * This method initializes tablesList
240
     *
241
     * @return javax.swing.JList
242
     */
243
    private AvailableTablesCheckBoxList getTablesList() {
244
        if (tablesList == null) {
245
            tablesList = new AvailableTablesCheckBoxList(this);
246
            tablesList.addListSelectionListener(this);
247
            tablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
248
        }
249

    
250
        return tablesList;
251
    }
252

    
253
    /**
254
     * This method initializes layerNameTextField
255
     *
256
     * @return javax.swing.JTextField
257
     */
258

    
259
    /**
260
     * This method initializes jComboBox
261
     *
262
     * @return javax.swing.JComboBox
263
     */
264
    private JComboBox getDatasourceComboBox() {
265
        if (datasourceComboBox == null) {
266
            datasourceComboBox = new JComboBox();
267
            datasourceComboBox.setBounds(new java.awt.Rectangle(10, 20, 446, 21));
268
            datasourceComboBox.addActionListener(this);
269
        }
270

    
271
        return datasourceComboBox;
272
    }
273

    
274
    public void actionPerformed(ActionEvent arg0) {
275
        if (datasourceComboBox.getItemCount()==0) {
276
                        setEmptyPanels();
277
                }
278
            Object src = arg0.getSource();
279

    
280
        if (src == datasourceComboBox) {
281
            Object sel_obj = datasourceComboBox.getSelectedItem();
282

    
283
            if (sel_obj == null) {
284
                return;
285
            }
286
            getDatasourceComboBox().repaint();
287
            return;
288
        }
289

    
290
        if (src == dbButton) {
291
            MyExplorer sel = addNewConnection();
292

    
293
            if (sel != null) {
294
                    dbExplorerParameters=sel.getDbExplorerParameters();
295
                    loadVectorialDBDatasourcesCombo(sel);
296
                getDatasourceComboBox().setSelectedItem(sel);
297

    
298
            }
299
            updateTableList(dbExplorerParameters);
300
        }
301
    }
302

    
303
    private MyExplorer addNewConnection() {
304
            MyExplorer myExplorer=new MyExplorer();
305
        DBExplorerParameters resp = null;
306

    
307
        VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
308
        newco.showDialog();
309

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

    
327

    
328

    
329
    private void updateTableList(DBExplorerParameters src) {
330
          if (src==null) {
331
                  return;
332
          }
333
            DataManager dm = DALLocator.getDataManager();
334
            DBExplorer dbExplorer;
335
            try {
336
                    dbExplorer = (DBExplorer)dm.createDataExplorer(src);
337
                    DBStoreParameters[] parameters = (DBStoreParameters[]) dbExplorer
338
                                        .list();
339

    
340
                    DefaultListModel lmodel = new DefaultListModel();
341

    
342
                    for (int i = 0; i < parameters.length; i++) {
343
                            lmodel.addElement(new TablesListItem(dbExplorer,parameters[i],
344
                                            getMapCtrl(), this));
345
                    }
346

    
347
                    getTablesList().setModel(lmodel);
348
                    getTablesScrollPane().setViewportView(tablesList);
349
                    tablesScrollPane.updateUI();
350
            } catch (InitializeException e) {
351
                    logger.error("While getting table names: " + e.getMessage(), e);
352
                        NotificationManager.showMessageError("While getting table names: "
353
                                        + e.getMessage(), e);
354
                    return;
355
            } catch (ReadException e) {
356
                    logger.error("While getting table names: " + e.getMessage(), e);
357
                        NotificationManager.showMessageError("While getting table names: "
358
                                        + e.getMessage(), e);
359
                    return;
360
            }
361
    }
362

    
363
    public void valueChanged(ListSelectionEvent arg0) {
364
        Object src = arg0.getSource();
365

    
366
        if (src == tablesList) {
367
            TablesListItem selected = (TablesListItem) tablesList.getSelectedValue();
368

    
369
            setSettingsPanels(selected);
370
            checkFinishable();
371
        }
372
    }
373

    
374
    private boolean validFormSettings() {
375
        int count = tablesList.getModel().getSize();
376

    
377
        boolean at_least_one = false;
378
        boolean resp = true;
379

    
380
        for (int i = 0; i < count; i++) {
381
            TablesListItem item = (TablesListItem) tablesList.getModel()
382
                                                             .getElementAt(i);
383

    
384
            if (item.isSelected()) {
385
                at_least_one = true;
386
            }
387

    
388
            if (item.disturbsWizardValidity()) {
389
                resp = false;
390
            }
391
        }
392

    
393
        return (at_least_one && resp);
394
    }
395

    
396
    public void checkFinishable() {
397
        boolean finishable = validFormSettings();
398
        callStateChanged(finishable);
399
    }
400

    
401
    /**
402
     * This method initializes jdbcButton
403
     *
404
     * @return javax.swing.JButton
405
     */
406
    private JButton getJdbcButton() {
407
        if (dbButton == null) {
408
            dbButton = new JButton();
409
            dbButton.addActionListener(this);
410
            dbButton.setToolTipText(PluginServices.getText(this,
411
                    "add_connection"));
412
            dbButton.setBounds(new java.awt.Rectangle(465, 20, 26, 21));
413

    
414
            String _file = createResourceUrl("images/jdbc.png").getFile();
415
            dbButton.setIcon(new ImageIcon(_file));
416
        }
417

    
418
        return dbButton;
419
    }
420

    
421

    
422
    private void showConnectionErrorMessage(String _msg) {
423
        String msg = (_msg.length() > 300) ? "" : (": " + _msg);
424
        String title = PluginServices.getText(this, "connection_error");
425
        JOptionPane.showMessageDialog(this, title + msg, title,
426
            JOptionPane.ERROR_MESSAGE);
427
    }
428

    
429
    private java.net.URL createResourceUrl(String path) {
430
        return getClass().getClassLoader().getResource(path);
431
    }
432

    
433
    public void setSettingsPanels(TablesListItem actTable){
434
        if (actTable == null) {
435
            setEmptyPanels();
436

    
437
            return;
438
        }
439
        String abrev=null;
440
        if (getMapCtrl()!=null){
441
                abrev=getMapCtrl().getViewPort().getProjection()
442
                       .getAbrev();
443
        }
444
        settingsPanel = actTable.getUserTableSettingsPanel(abrev);
445
        fieldsPanel = actTable.getUserSelectedFieldsPanel();
446

    
447
        removeFieldPanels();
448
        add(fieldsPanel);
449
        fieldsPanel.repaint();
450

    
451
        removeSettingsPanels();
452
        add(settingsPanel);
453
        settingsPanel.repaint();
454

    
455
        repaint();
456
    }
457

    
458
    private void setEmptyPanels() {
459
        removeFieldPanels();
460
        add(emptyFieldsPanel);
461
        removeSettingsPanels();
462
        add(emptySettingsPanel);
463

    
464
        settingsPanel = emptySettingsPanel;
465
        fieldsPanel = emptyFieldsPanel;
466

    
467
        repaint();
468
    }
469

    
470
    private void removeFieldPanels() {
471
        for (int i = 0; i < getComponentCount(); i++) {
472
            if (getComponent(i) instanceof UserSelectedFieldsPanel) {
473
                remove(i);
474
            }
475
        }
476
    }
477

    
478
    private void removeSettingsPanels() {
479
        for (int i = 0; i < getComponentCount(); i++) {
480
            if (getComponent(i) instanceof UserTableSettingsPanel) {
481
                remove(i);
482
            }
483
        }
484
    }
485

    
486
        public DataStoreParameters[] getParameters() {
487
                try {
488
            TablesListItem[] selected = getSelectedTables();
489
            int count = selected.length;
490
            DBStoreParameters[] dbParameters = new DBStoreParameters[count];
491
            String strEPSG =null;
492
            if (getMapCtrl()!=null) {
493
                                strEPSG = getMapCtrl().getViewPort().getProjection()
494
                                 .getAbrev();
495
                        }
496

    
497
            for (int i = 0; i < count; i++) {
498
                TablesListItem item = selected[i];
499

    
500

    
501
                DBStoreParameters parameters = item.getParameters();
502

    
503
                UserTableSettingsPanel userTableSettingsPanel=item.getUserTableSettingsPanel(strEPSG);
504
                Envelope _wa = userTableSettingsPanel.getWorkingArea();
505

    
506
                                //                String[] tokens = item.getTableName().split("\\u002E", 2);
507
                                //                String tableName;
508
                                //                String schema = null;
509
                                //                if (tokens.length > 1)
510
                                //                {
511
                                //                        schema = tokens[0];
512
                                //                        tableName = tokens[1];
513
                                //                }
514
                                //                else
515
                                //                {
516
                                //                        tableName = tokens[0];
517
                                //                }
518

    
519
                String fidField = userTableSettingsPanel.getIdFieldName();
520
                String geomField = userTableSettingsPanel.getGeoFieldName();
521
                String[] fields = item.getUserSelectedFieldsPanel()
522
                        .getUserSelectedFields(fidField, geomField);
523

    
524
                                //                parameters.setSchema(schema);
525
                                //                parameters.setTableName(tableName);
526

    
527
                if (userTableSettingsPanel.isSqlActive()) {
528
                    String whereClause = userTableSettingsPanel.getWhereClause();
529
                    parameters.setBaseFilter(whereClause);
530
                }
531
                else {
532
                    parameters.setBaseFilter("");
533
                }
534

    
535
                parameters.setFields(fields);
536
                parameters.setDefaultGeometryField(geomField);
537
                parameters.setFieldsId(new String[] { fidField });
538

    
539
                if (_wa != null) {
540
                   parameters.setWorkingArea(_wa);
541
                }
542

    
543
                parameters.setSRISD(strEPSG);
544
                dbParameters[i]=parameters;
545
            }
546

    
547
            return dbParameters;//layerArrayToGroup(all_layers, groupName);
548
        }
549
        catch (Exception e) {
550
            logger.error("While creating jdbc layer: " + e.getMessage(), e);
551
            NotificationManager.addError("Error al cargar la capa: " +
552
                e.getMessage(), e);
553
        }
554

    
555
        return null;
556
        }
557
} //  @jve:decl-index=0:visual-constraint="10,10"