Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / WizardVectorialDB.java @ 23603

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.data.DataManager;
62
import org.gvsig.fmap.data.DataStoreParameters;
63
import org.gvsig.fmap.data.InitializeException;
64
import org.gvsig.fmap.data.ReadException;
65
import org.gvsig.fmap.data.feature.db.DBExplorer;
66
import org.gvsig.fmap.data.feature.db.DBExplorerParameters;
67
import org.gvsig.fmap.data.feature.db.DBStoreParameters;
68
import org.gvsig.fmap.geom.primitive.Envelope;
69
import org.gvsig.gui.beans.swing.JButton;
70

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

    
78

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

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

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

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

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

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

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

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

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

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

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

    
151
    }
152

    
153
    public void initWizard() {
154
    }
155

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

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

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

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

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

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

    
194
        return namePanel;
195
    }
196

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

    
214
        return tablesPanel;
215
    }
216

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

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

    
234
        return tablesScrollPane;
235
    }
236

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

    
249
        return tablesList;
250
    }
251

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

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

    
270
        return datasourceComboBox;
271
    }
272

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

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

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

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

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

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

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

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

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

    
326

    
327

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

    
339
                    DefaultListModel lmodel = new DefaultListModel();
340

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
417
        return dbButton;
418
    }
419

    
420

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

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

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

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

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

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

    
454
        repaint();
455
    }
456

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

    
463
        settingsPanel = emptySettingsPanel;
464
        fieldsPanel = emptyFieldsPanel;
465

    
466
        repaint();
467
    }
468

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

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

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

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

    
499

    
500
                DBStoreParameters parameters = item.getParameters();
501

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

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

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

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

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

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

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

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

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

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