Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.jdbc / src / main / java / org / gvsig / exportto / swing / prov / jdbc / panel / SelectTableNamePanel.java @ 41488

History | View | Annotate | Download (6.44 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.exportto.swing.prov.jdbc.panel;
24

    
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.util.Iterator;
28
import java.util.List;
29

    
30
import javax.swing.DefaultListModel;
31
import javax.swing.JComponent;
32
import javax.swing.JOptionPane;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.exportto.swing.prov.jdbc.ExporttoJDBCProvider;
35
import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException;
36
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel;
37
import org.gvsig.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.DataManager;
39
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
40
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
41
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.i18n.I18nManager;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47
/**
48
 * @author gvSIG Team
49
 * @version $Id$
50
 *
51
 */
52
public class SelectTableNamePanel extends SelectTableNamePanelLayout implements ExporttoSwingProviderPanel {
53

    
54
    private static final Logger logger = LoggerFactory.getLogger(SelectTableNamePanel.class);
55
            
56
    private static final long serialVersionUID = 6269512983586358017L;
57
    private final ExporttoJDBCProvider provider;
58

    
59
    private static class TableItem {
60

    
61
        private JDBCStoreParameters params;
62
        private String label;
63

    
64
        public TableItem(String label, JDBCStoreParameters params) {
65
            this.params = params;
66
            this.label = label;
67
        }
68

    
69
        public TableItem(JDBCStoreParameters params) {
70
            this(params.getSchema() + "." + params.getTable(), params);
71
        }
72

    
73
        public String toString() {
74
            return this.label;
75
        }
76

    
77
        public JDBCStoreParameters getParams() {
78
            return this.params;
79
        }
80
    }
81

    
82
    public SelectTableNamePanel(ExporttoJDBCProvider provider) {
83
        this.provider = provider;
84
        initComponents();
85
        
86
    }
87

    
88
    private void initComponents() {
89
        this.rdoCreateTable.addActionListener(new ActionListener() {
90
            public void actionPerformed(ActionEvent e) {
91
                onChangeRadioSelecion();
92
            }
93
        });
94
        this.rdoInsert.addActionListener(new ActionListener() {
95
            public void actionPerformed(ActionEvent e) {
96
                onChangeRadioSelecion();
97
            }
98
        });
99
        this.rdoCreateTable.setSelected(true);
100
    }
101

    
102
    public boolean canCreateTable() {
103
        return this.rdoCreateTable.isSelected();
104
    }
105

    
106
    public String getSchema() {
107
        if (this.canCreateTable()) {
108
            return StringUtils.defaultIfBlank(this.txtSchema.getText(), null);
109
        }
110
        JDBCStoreParameters table = (JDBCStoreParameters) this.lstTables.getSelectedValue();
111
        if( table == null ) {
112
            return null;
113
        }
114
        return table.getSchema();
115
    }
116

    
117
    public String getTableName() {
118
        if (this.canCreateTable()) {
119
            return StringUtils.defaultIfBlank(this.txtSchema.getText(), null);
120
        }
121
        JDBCStoreParameters table = (JDBCStoreParameters) this.lstTables.getSelectedValue();
122
        if( table == null ) {
123
            return null;
124
        }
125
        return table.getTable();
126
    }
127

    
128
    public String getPanelTitle() {
129
        I18nManager i18nManager = ToolsLocator.getI18nManager();
130
        return i18nManager.getTranslation("intro_tablename");
131
    }
132

    
133
    public boolean isValidPanel() throws ExporttoPanelValidationException {
134
        if (this.getTableName() == null) {
135
            throw new ExporttoPanelValidationException(
136
                    "The table cannot be empty");
137
        }
138
        if (this.getSchema() == null) {
139
            throw new ExporttoPanelValidationException(
140
                    "The schema cannot be empty");
141
        }
142
        return true;
143
    }
144
    
145
    public void enterPanel() {
146
        this.fillTablesList();
147
    }
148
    
149
    public JComponent asJComponent() {
150
        return this;
151
    }
152

    
153
    public void onChangeRadioSelecion() {
154
        if (this.rdoCreateTable.isSelected()) {
155
            this.txtSchema.setEnabled(true);
156
            this.txtTableName.setEnabled(true);
157
            this.lstTables.setEnabled(false);
158
        } else {
159
            this.txtSchema.setEnabled(false);
160
            this.txtTableName.setEnabled(false);
161
            this.lstTables.setEnabled(true);
162
        }
163
    }
164

    
165
    private void fillTablesList() {
166
        JDBCServerExplorerParameters explorerParameters = this.provider.getExplorerParameters();
167
        if (explorerParameters == null) {
168
            return;
169
        }
170
        DefaultListModel lmodel = new DefaultListModel();
171
        DataManager dataManager = DALLocator.getDataManager();
172
        try {
173
            explorerParameters.setShowInformationDBTables(false);
174
            JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
175
                    explorerParameters.getExplorerName(),
176
                    explorerParameters
177
            );
178
            
179
            List<JDBCStoreParameters> tables = explorer.list();
180
            Iterator<JDBCStoreParameters> it = tables.iterator();
181
            while (it.hasNext()) {
182
                JDBCStoreParameters table = it.next();
183
                lmodel.addElement(new TableItem(table));
184
            }
185
            this.lstTables.setModel(lmodel);
186
        } catch (Exception ex) {
187
            // Esto es llamado desde el enterPanel que ya atrapa los errores
188
            // volcandolos al log y presentandolos al usuario adecuadamente.
189
            throw new RuntimeException(ex);
190
        }
191
    }
192

    
193
}