Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / WizardDB.java @ 38608

History | View | Annotate | Download (18.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

    
28
package org.gvsig.geodb.vectorialdb.wizard;
29

    
30
import java.awt.BorderLayout;
31
import java.awt.Window;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.util.ArrayList;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.Map.Entry;
39

    
40
import javax.swing.DefaultListModel;
41
import javax.swing.ImageIcon;
42
import javax.swing.JComboBox;
43
import javax.swing.JOptionPane;
44
import javax.swing.JPanel;
45
import javax.swing.JScrollPane;
46
import javax.swing.ListSelectionModel;
47
import javax.swing.event.ListSelectionEvent;
48
import javax.swing.event.ListSelectionListener;
49

    
50
import org.apache.commons.collections.map.HashedMap;
51
import org.cresques.cts.IProjection;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
import org.gvsig.andami.IconThemeHelper;
56
import org.gvsig.andami.PluginServices;
57
import org.gvsig.andami.PluginsLocator;
58
import org.gvsig.andami.PluginsManager;
59
import org.gvsig.andami.messages.NotificationManager;
60
import org.gvsig.app.ApplicationLocator;
61
import org.gvsig.app.ApplicationManager;
62
import org.gvsig.app.gui.WizardPanel;
63
import org.gvsig.app.prepareAction.PrepareContext;
64
import org.gvsig.app.project.Project;
65
import org.gvsig.app.project.ProjectManager;
66
import org.gvsig.app.project.documents.table.TableDocument;
67
import org.gvsig.app.project.documents.table.TableManager;
68
import org.gvsig.fmap.dal.DALLocator;
69
import org.gvsig.fmap.dal.DataManager;
70
import org.gvsig.fmap.dal.DataStoreParameters;
71
import org.gvsig.fmap.dal.exception.DataException;
72
import org.gvsig.fmap.dal.exception.InitializeException;
73
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
74
import org.gvsig.fmap.dal.feature.FeatureStore;
75
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer;
76
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorerParameters;
77
import org.gvsig.fmap.dal.store.db.DBStoreParameters;
78
import org.gvsig.fmap.mapcontext.layers.FLayer;
79
import org.gvsig.fmap.mapcontrol.MapControl;
80
import org.gvsig.geodb.ExtDB_Spatial;
81
import org.gvsig.gui.beans.swing.JButton;
82
import org.gvsig.tools.dynobject.DynObject;
83

    
84

    
85
public class WizardDB extends WizardPanel implements ActionListener,
86
                ListSelectionListener {
87

    
88
        /**
89
     * 
90
     */
91
    private static final long serialVersionUID = -7045762275505941695L;
92
    private static final String WIZARD_TAB_NAME = "DB";
93
        private static Logger logger = LoggerFactory.getLogger(WizardDB.class
94
                        .getName());
95

    
96
    private JPanel namePanel = null;
97
        private JPanel tablesPanel = null;
98
        private JScrollPane tablesScrollPane = null;
99
        private AvailableTablesCheckBoxList tablesList = null;
100
        private JComboBox datasourceComboBox = null;
101
        private UserSelectedFieldsPanel fieldsPanel = null;
102
        private UserSelectedFieldsPanel emptyFieldsPanel = null;
103
        private JButton dbButton = null;
104
        private DBServerExplorerParameters dbExplorerParameters;
105

    
106
    private UserTableSettingsPanel settingsPanel = null;
107
        protected UserTableSettingsPanel emptySettingsPanel = null;
108
        private PrepareContext prepareDSContext;
109

    
110
    public WizardDB() {
111
                super();
112
                initialize();
113
        }
114

    
115

    
116
        protected void initialize() {
117
                setTabName(WIZARD_TAB_NAME);
118
                setLayout(null);
119
                setSize(512, 478);
120

    
121

    
122

    
123
                emptyFieldsPanel = new UserSelectedFieldsPanel(null, true, this);
124
                add(emptyFieldsPanel);
125

    
126
        add(getNamePanel(), null);
127
                loadVectorialDBDatasourcesCombo(null);
128

    
129
                add(getTablesPanel(), null);
130

    
131
        emptySettingsPanel = createSettingsPanel(null);
132
        add(emptySettingsPanel);
133

    
134
        }
135

    
136

    
137
    @SuppressWarnings("rawtypes")
138
    private void loadVectorialDBDatasourcesCombo(MyExplorer sel) {
139
        
140
        PluginsManager manager = PluginsLocator.getManager();
141
        DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties();
142
        Map connections = (Map) values.getDynValue("db_connections");
143
        if (connections != null){
144
            Iterator it = connections.entrySet().iterator();
145
            getDatasourceComboBox().removeAllItems();
146
            getDatasourceComboBox().addItem("");
147
            while (it.hasNext()){
148
                Map.Entry entry = (Entry) it.next();
149
                MyExplorer myExplorer = new MyExplorer();
150
                myExplorer.setDbExplorerParameters((DBServerExplorerParameters) entry.getValue());
151
                myExplorer.setName((String) entry.getKey());
152
                getDatasourceComboBox().addItem(myExplorer);
153
                if(sel!=null && sel.getName().equalsIgnoreCase(myExplorer.getName())){
154
                    getDatasourceComboBox().setSelectedItem(myExplorer);
155
                }
156
            }
157
        } else {
158
            connections = new HashedMap();
159
            values.setDynValue("db_connections", connections);
160
        }
161
        }
162

    
163
        public void initWizard() {
164
        }
165

    
166
    @Override
167
    public void execute() {
168
        executeWizard();
169
    }
170

    
171
    @Override
172
    public Object executeWizard() {
173
                TablesListItem[] tables = getSelectedTables();
174

    
175
                DataManager man = DALLocator.getDataManager();
176
                FeatureStore store;
177
                
178
                String docName;
179
                TableDocument document;
180
                Project project = ProjectManager.getInstance().getCurrentProject();
181

    
182
                ApplicationManager appGvSIGMan = ApplicationLocator.getManager();
183
                PrepareContext context = this.getPrepareDataStoreContext();
184
                DBStoreParameters storeParams;
185
        List<TableDocument> tabledocs =
186
            new ArrayList<TableDocument>(tables.length);
187
                for (TablesListItem table : tables) {
188
                        storeParams = getParameterForTable(table);
189

    
190
                        try {
191
                                storeParams = (DBStoreParameters) appGvSIGMan
192
                                                .prepareOpenDataStoreParameters(storeParams, context);
193
                        } catch (Exception e2) {
194
                                NotificationManager.addError(e2);
195
                                continue;
196
                        }
197

    
198
                        UserTableSettingsPanel userTableSettingsPanel = table
199
                                        .getUserTableSettingsPanel();
200

    
201
                        docName = userTableSettingsPanel.getUserLayerName();
202
                        try {
203
                                store = (FeatureStore) man.openStore(storeParams.getDataStoreName(), storeParams);
204
                        } catch (Exception e) {
205
                                NotificationManager.addError(e);
206
                return null;
207
                        }
208

    
209
                        try {
210
                                appGvSIGMan.pepareOpenDataSource(
211
                                                store, context);
212
                        } catch (Exception e) {
213
                                NotificationManager.addError(e);
214
                                store.dispose();
215
                return null;
216
                        }
217

    
218
                        document = (TableDocument) ProjectManager.getInstance().createDocument(TableManager.TYPENAME, docName);
219
                        document.setStore(store);
220
            // project.add(document);
221
            tabledocs.add(document);
222
                }
223
        return tabledocs;
224
        }
225

    
226
        protected DBStoreParameters getParameterForTable(TablesListItem table) {
227
                DBStoreParameters parameters = table.getParameters();
228

    
229
                UserTableSettingsPanel userTableSettingsPanel = table
230
                                .getUserTableSettingsPanel();
231

    
232

    
233

    
234
                String fidField = userTableSettingsPanel.getIdFieldName();
235
                //IF is a multiple PK, remove the {} symbols
236
                if (fidField.startsWith("{") && fidField.endsWith("}")) {
237
                        fidField = fidField.substring(1, fidField.length()-1);
238
                }
239
                String[] pkFields = fidField.split(",");              
240
                    
241
                String[] fields = table.getUserSelectedFieldsPanel()
242
                                .getUserSelectedFields(pkFields, null);
243

    
244
                if (userTableSettingsPanel.isSqlActive()) {
245
                        String whereClause = userTableSettingsPanel
246
                                        .getWhereClause();
247
                        parameters.setBaseFilter(whereClause);
248
                } else {
249
                        parameters.setBaseFilter("");
250
                }
251

    
252
                parameters.setFields(fields);
253

    
254

    
255
                return parameters;
256

    
257
        }
258

    
259
        @SuppressWarnings({ "rawtypes", "unchecked" })
260
    protected TablesListItem[] getSelectedTables() {
261
                int count = tablesList.getModel().getSize();
262
                ArrayList resp = new ArrayList();
263

    
264
                for (int i = 0; i < count; i++) {
265
                        TablesListItem item = (TablesListItem) tablesList.getModel()
266
                                        .getElementAt(i);
267

    
268
                        if (item.isSelected()) {
269
                                resp.add(item);
270
                        }
271
                }
272

    
273
                return (TablesListItem[]) resp.toArray(new TablesListItem[0]);
274
        }
275

    
276
        /**
277
         * This method initializes namePanel
278
         *
279
         * @return javax.swing.JPanel
280
         */
281
        private JPanel getNamePanel() {
282
                if (namePanel == null) {
283
                        namePanel = new JPanel();
284
                        namePanel.setLayout(null);
285
                        namePanel.setBounds(new java.awt.Rectangle(5, 5, 501, 51));
286
                        namePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
287
                                        null, PluginServices.getText(this, "choose_connection"),
288
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
289
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
290
                                        null));
291
                        namePanel.add(getDatasourceComboBox(), null);
292
                        namePanel.add(getJdbcButton(), null);
293
                }
294

    
295
                return namePanel;
296
        }
297

    
298
        /**
299
         * This method initializes tablesPanel
300
         *
301
         * @return javax.swing.JPanel
302
         */
303
        private JPanel getTablesPanel() {
304
                if (tablesPanel == null) {
305
                        tablesPanel = new JPanel();
306
                        tablesPanel.setLayout(new BorderLayout());
307
                        tablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
308
                                        null, PluginServices.getText(this, "choose_table"),
309
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
310
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null,
311
                                        null));
312
                        tablesPanel.setBounds(new java.awt.Rectangle(5, 55, 246, 166));
313
                        tablesPanel
314
                                        .add(getTablesScrollPane(), java.awt.BorderLayout.CENTER);
315
                }
316

    
317
                return tablesPanel;
318
        }
319

    
320
        /**
321
         * This method initializes settingsPanel
322
         *
323
         * @return javax.swing.JPanel
324
         */
325

    
326
        /**
327
         * This method initializes tablesScrollPane
328
         *
329
         * @return javax.swing.JScrollPane
330
         */
331
        private JScrollPane getTablesScrollPane() {
332
                if (tablesScrollPane == null) {
333
                        tablesScrollPane = new JScrollPane();
334
                        tablesScrollPane.setViewportView(getTablesList());
335
                }
336

    
337
                return tablesScrollPane;
338
        }
339

    
340
        /**
341
         * This method initializes tablesList
342
         *
343
         * @return javax.swing.JList
344
         */
345
        protected AvailableTablesCheckBoxList getTablesList() {
346
                if (tablesList == null) {
347
                        tablesList = new AvailableTablesCheckBoxList(this);
348
                        tablesList.addListSelectionListener(this);
349
                        tablesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
350
                }
351

    
352
                return tablesList;
353
        }
354

    
355
        /**
356
         * This method initializes layerNameTextField
357
         *
358
         * @return javax.swing.JTextField
359
         */
360

    
361
        /**
362
         * This method initializes jComboBox
363
         *
364
         * @return javax.swing.JComboBox
365
         */
366
        private JComboBox getDatasourceComboBox() {
367
                if (datasourceComboBox == null) {
368
                        datasourceComboBox = new JComboBox();
369
                        datasourceComboBox
370
                                        .setBounds(new java.awt.Rectangle(10, 20, 446, 21));
371
                        datasourceComboBox.addActionListener(this);
372
                }
373

    
374
                return datasourceComboBox;
375
        }
376

    
377
        public void actionPerformed(ActionEvent arg0) {
378
                if (datasourceComboBox.getItemCount() == 0) {
379
                        setEmptyPanels();
380
                }
381
                Object src = arg0.getSource();
382

    
383
                if (src == datasourceComboBox) {
384
            Object selected = datasourceComboBox.getSelectedItem();
385
            if (selected instanceof MyExplorer) {
386
                MyExplorer sel_obj = (MyExplorer) selected;
387

    
388
                if (sel_obj == null) {
389
                    return;
390
                }
391
                getDatasourceComboBox().repaint();
392
                dbExplorerParameters = sel_obj.getDbSeverExplorerParameters();
393
            }
394

    
395
                } else if (src == dbButton) {
396
                        MyExplorer sel = addNewConnection();
397

    
398
                        if (sel != null) {
399
                                dbExplorerParameters = sel.getDbSeverExplorerParameters();
400
                                loadVectorialDBDatasourcesCombo(sel);
401
                                getDatasourceComboBox().setSelectedItem(sel);
402

    
403
                        }
404
                }
405
                
406
                updateTableList(dbExplorerParameters);
407
        }
408

    
409
        @SuppressWarnings({ "unchecked", "rawtypes" })
410
    private MyExplorer addNewConnection() {
411
                MyExplorer myExplorer = new MyExplorer();
412
                DBServerExplorerParameters resp = null;
413

    
414
                VectorialDBConnectionParamsDialog newco = new VectorialDBConnectionParamsDialog();
415
                newco.showDialog();
416

    
417
                if (newco.isOkPressed()) {
418
                        try {
419
                                resp = newco.getParameters();
420
                        } catch (Exception e) {
421
                                showConnectionErrorMessage(e.getMessage());
422
                                return null;
423
                        }
424
                        PluginsManager manager = PluginsLocator.getManager();
425
            DynObject values = manager.getPlugin(ExtDB_Spatial.class).getPluginProperties();
426
            Map connections = (Map) values.getDynValue("db_connections");
427
            if(connections == null){
428
                connections = new HashedMap();
429
                values.setDynValue("db_connections", connections);
430
            }
431
            connections.put(newco.getConnectionName(), resp);
432
            
433
//                        SingleVectorialDBConnectionExtension.saveAllToPersistence();
434
                        myExplorer.setDbExplorerParameters(resp);
435
                        myExplorer.setName(newco.getConnectionName());
436
                        return myExplorer;
437
                } else {
438
                        return null;
439
                }
440
        }
441

    
442
        protected TablesListItem createTabeListItem(DBServerExplorer dbExplorer,
443
                        DBStoreParameters param) {
444
                return new TablesListItem(dbExplorer, param, this);
445
        }
446

    
447
        @SuppressWarnings("rawtypes")
448
    private void updateTableList(
449
                        DBServerExplorerParameters dbSeverExplorerParameters2) {
450
                if (dbSeverExplorerParameters2 == null) {
451
                        return;
452
                }
453
                DataManager dm = DALLocator.getDataManager();
454
                DBServerExplorer dbExplorer;
455
                try {
456
                        dbExplorer = (DBServerExplorer) dm.openServerExplorer(dbSeverExplorerParameters2.getExplorerName(), dbSeverExplorerParameters2);
457

    
458
                        // TODO: use the mode to filter tables with geometries when opening
459
                        // a view document
460
                        List parameters = dbExplorer.list();
461

    
462
                        DefaultListModel lmodel = new DefaultListModel();
463

    
464
                        Iterator iter = parameters.iterator();
465
                        DBStoreParameters param;
466
                        int count = 0;
467
                        while (iter.hasNext()) {
468
                                param = (DBStoreParameters) iter.next();
469
                                lmodel.addElement(createTabeListItem(dbExplorer, param));
470
                                count++;
471
                        }
472

    
473
                        getTablesList().setModel(lmodel);
474
                        getTablesScrollPane().setViewportView(tablesList);
475
                        tablesScrollPane.updateUI();
476
                } catch (InitializeException e) {
477
                        logger.error("While getting table names: " + e.getMessage(), e);
478
                        NotificationManager.showMessageError("While getting table names: "
479
                                        + e.getMessage(), e);
480
                        return;
481
                } catch (DataException e) {
482
                        logger.error("While getting table names: " + e.getMessage(), e);
483
                        NotificationManager.showMessageError("While getting table names: "
484
                                        + e.getMessage(), e);
485
                        return;
486
                } catch (ValidateDataParametersException e) {
487
                        logger.error("While getting table names: " + e.getMessage(), e);
488
                        NotificationManager.showMessageError("While getting table names: "
489
                                        + e.getMessage(), e);
490
                        return;
491
                }
492
        }
493

    
494
        public void valueChanged(ListSelectionEvent arg0) {
495
                Object src = arg0.getSource();
496

    
497
                if (src == tablesList) {
498
                        TablesListItem selected = (TablesListItem) tablesList
499
                                        .getSelectedValue();
500

    
501
                        setSettingsPanels(selected);
502
                        checkFinishable();
503
                }
504
        }
505

    
506
        public boolean areSettingsValid() {
507
                int count = tablesList.getModel().getSize();
508

    
509
                boolean at_least_one = false;
510
                boolean resp = true;
511

    
512
                for (int i = 0; i < count; i++) {
513
                        TablesListItem item = (TablesListItem) tablesList.getModel()
514
                                        .getElementAt(i);
515

    
516
                        if (item.isSelected()) {
517
                                at_least_one = true;
518
                        }
519

    
520
                        if (item.disturbsWizardValidity()) {
521
                                resp = false;
522
                        }
523
                }
524

    
525
                return (at_least_one && resp);
526
        }
527

    
528
        public void checkFinishable() {
529
                boolean finishable = areSettingsValid();
530
                callStateChanged(finishable);
531
        }
532

    
533
        /**
534
         * This method initializes jdbcButton
535
         *
536
         * @return javax.swing.JButton
537
         */
538
        private JButton getJdbcButton() {
539
                if (dbButton == null) {
540
                        dbButton = new JButton();
541
                        dbButton.addActionListener(this);
542
                        dbButton.setToolTipText(PluginServices.getText(this,
543
                                        "add_connection"));
544
                        dbButton.setBounds(new java.awt.Rectangle(465, 20, 26, 21));
545

    
546
                        dbButton.setIcon(IconThemeHelper.getImageIcon("geodb-connection-add"));
547
                }
548

    
549
                return dbButton;
550
        }
551

    
552
        private void showConnectionErrorMessage(String _msg) {
553
                String msg = (_msg.length() > 300) ? "" : (": " + _msg);
554
                String title = PluginServices.getText(this, "connection_error");
555
                JOptionPane.showMessageDialog(this, title + msg, title,
556
                                JOptionPane.ERROR_MESSAGE);
557
        }
558

    
559
        private java.net.URL createResourceUrl(String path) {
560
                return getClass().getClassLoader().getResource(path);
561
        }
562

    
563
        public void setSettingsPanels(TablesListItem actTable) {
564
                if (actTable == null) {
565
                        setEmptyPanels();
566

    
567
                        return;
568
                }
569
                fieldsPanel = actTable.getUserSelectedFieldsPanel();
570

    
571
                removeFieldPanels();
572
                add(fieldsPanel);
573
                fieldsPanel.repaint();
574
                removeSettingsPanels();
575
                add(emptySettingsPanel);
576

    
577
                settingsPanel = createSettingsPanel(actTable);
578

    
579
                removeSettingsPanels();
580
                add(settingsPanel);
581
                settingsPanel.repaint();
582

    
583

    
584
                repaint();
585
        }
586

    
587

    
588

    
589
        protected UserTableSettingsPanel createSettingsPanel(TablesListItem actTable) {
590
                if (actTable == null) {
591
                        return new UserTableSettingsPanel(null, "", true, this, null);
592
                }
593

    
594
                return actTable.getUserTableSettingsPanel();
595
        }
596

    
597
        protected void setEmptyPanels() {
598
                removeFieldPanels();
599
                add(emptyFieldsPanel);
600
                fieldsPanel = emptyFieldsPanel;
601

    
602
                repaint();
603
        }
604

    
605
        private void removeFieldPanels() {
606
                for (int i = 0; i < getComponentCount(); i++) {
607
                        if (getComponent(i) instanceof UserSelectedFieldsPanel) {
608
                                remove(i);
609
                        }
610
                }
611
        }
612

    
613
        public DataStoreParameters[] getParameters() {
614
                try {
615
                        TablesListItem[] selected = getSelectedTables();
616
                        int count = selected.length;
617
                        DBStoreParameters[] dbParameters = new DBStoreParameters[count];
618

    
619
                        for (int i = 0; i < count; i++) {
620
                                TablesListItem item = selected[i];
621

    
622

    
623
                                dbParameters[i] = getParameterForTable(item);
624
                        }
625

    
626
                        return dbParameters;// layerArrayToGroup(all_layers, groupName);
627
                } catch (Exception e) {
628
                        logger.error("While creating jdbc layer: " + e.getMessage(), e);
629
                        NotificationManager.addError("Error al cargar la capa: "
630
                                        + e.getMessage(), e);
631
                }
632

    
633
                return null;
634
        }
635

    
636
        /**
637
         * This method process the errors found in a layer
638
         *
639
         * @param lyr
640
         * @param mapControl
641
         */
642

    
643
        protected void processErrorsOfLayer(FLayer lyr, MapControl mapControl) {
644
                // List errors = lyr.getErrors();
645
                // wp.callError(null);
646
                mapControl.getMapContext().callNewErrorEvent(null);
647
        }
648

    
649
        private void removeSettingsPanels() {
650
                for (int i = 0; i < getComponentCount(); i++) {
651
                        if (getComponent(i) instanceof UserTableSettingsPanel) {
652
                                remove(i);
653
                        }
654
                }
655
        }
656

    
657
        protected PrepareContext getPrepareDataStoreContext() {
658
                if (this.prepareDSContext == null) {
659
                        this.prepareDSContext = new PrepareContext() {
660
                                public Window getOwnerWindow() {
661
                                        return null;
662
                                }
663

    
664
                                public IProjection getViewProjection() {
665
                                        return null;
666
                                }
667

    
668
                        };
669
                }
670
                return this.prepareDSContext;
671
        }
672

    
673
        @Override
674
        public void close() {
675
                // Nothing to do
676
        }
677

    
678
} // @jve:decl-index=0:visual-constraint="10,10"