Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.newlayer / org.gvsig.newlayer.prov / org.gvsig.newlayer.prov.jdbc / src / main / java / org / gvsig / newlayer / prov / jdbc / panels / SelectTableNamePanel.java @ 45564

History | View | Annotate | Download (5.86 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.newlayer.prov.jdbc.panels;
24

    
25
import javax.swing.JComponent;
26
import org.apache.commons.lang3.StringUtils;
27
import org.gvsig.fmap.dal.DALLocator;
28
import org.gvsig.fmap.dal.DataManager;
29
import org.gvsig.fmap.dal.SQLBuilder;
30
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
31
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
32
import org.gvsig.tools.ToolsLocator;
33
import org.gvsig.tools.i18n.I18nManager;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
import org.gvsig.newlayer.NewLayerProviderPanel;
37
import org.gvsig.newlayer.NewLayerWizard;
38
import static org.gvsig.newlayer.NewLayerWizard.BUTTON_NEXT;
39
import org.gvsig.newlayer.prov.jdbc.NewLayerJDBCProviderParameters;
40
import org.gvsig.newlayer.spi.NewLayerPanelValidationException;
41

    
42
/**
43
 * @author gvSIG Team
44
 *
45
 */
46
@SuppressWarnings("UseSpecificCatch")
47
public class SelectTableNamePanel
48
        extends SelectTableNamePanelLayout
49
        implements NewLayerProviderPanel {
50

    
51
    private static final Logger LOGGER = LoggerFactory.getLogger(SelectTableNamePanel.class);
52
    
53
    private final NewLayerJDBCProviderParameters parameters;
54
    private final NewLayerWizard wizard;
55
    private SQLBuilder sqlbuilder;
56

    
57
    @SuppressWarnings("OverridableMethodCallInConstructor")
58
    public SelectTableNamePanel(NewLayerWizard wizard, NewLayerJDBCProviderParameters parameters) {
59
        this.parameters = parameters;
60
        this.wizard = wizard;
61
        initComponents();
62
    }
63

    
64
    private void initComponents() {
65
        try {
66
            this.txtTableName.setText(this.parameters.getTableName());
67
        } catch (Exception ex) {
68
            LOGGER.warn("Can't set the default value for the table name", ex);
69
        }
70

    
71
        I18nManager i18nManager = ToolsLocator.getI18nManager();
72
        this.lblHeader.setText(i18nManager.getTranslation("_Indique_donde_desea_insertar_los_datos"));
73
        this.lblSchema.setText(i18nManager.getTranslation("_Indique_el_esquema_en_el_que_desea_crear_la_tabla"));
74
        this.lblTableName.setText(i18nManager.getTranslation("_Indique_el_nombre_de_la_tabla"));
75
    }
76

    
77
    public String getSchema() {
78
            return StringUtils.defaultIfBlank(this.txtSchema.getText(), null);
79
    }
80

    
81
    public String getTableName() {
82
            return StringUtils.defaultIfBlank(this.txtTableName.getText(), null);
83
    }
84

    
85
    @Override
86
    public String getIdPanel() {
87
        return this.getClass().getCanonicalName();
88
    }
89

    
90
    @Override
91
    public String getTitlePanel() {
92
        I18nManager i18nManager = ToolsLocator.getI18nManager();
93
        return i18nManager.getTranslation("_Tablename");
94
    }
95

    
96
    @Override
97
    public void previousPanel() {
98

    
99
    }
100

    
101
    @Override
102
    public void nextPanel() {
103
        this.parameters.setSchema(this.getSchema());
104
        this.parameters.setTableName(this.getTableName());
105
    }
106

    
107
    @Override
108
    public boolean validatePanel() throws NewLayerPanelValidationException {
109
        I18nManager i18nManager = ToolsLocator.getI18nManager();
110
        String tablename = this.getTableName();
111
        if (tablename == null) {
112
            throw new NewLayerPanelValidationException(
113
                    i18nManager.getTranslation(
114
                            "_The_name_of_table_cannot_be_empty"
115
                    )
116
            );
117
        }
118
        String schema = this.getSchema();
119
        if (sqlbuilder.support_schemas()) {
120
            if (schema == null) {
121
                throw new NewLayerPanelValidationException(
122
                        i18nManager.getTranslation(
123
                                "_The_name_of_schema_cannot_be_empty"
124
                        )
125
                );
126
            }
127
        }
128
        this.parameters.setSchema(schema);
129
        this.parameters.setTableName(tablename);
130
        return true;
131
    }
132

    
133
    @Override
134
    public void enterPanel() {
135
        JDBCServerExplorerParameters explorerParameters = parameters.getExplorerParameters();
136
        if (explorerParameters == null) {
137
            this.wizard.setButtonEnabled(BUTTON_NEXT, false);
138
            return;
139
        }
140
        this.wizard.setButtonEnabled(BUTTON_NEXT, true);
141
        try {
142
            DataManager dataManager = DALLocator.getDataManager();
143
            JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
144
                    explorerParameters.getExplorerName(),
145
                    explorerParameters
146
            );
147
            this.sqlbuilder = explorer.createSQLBuilder();
148
            if( this.getSchema()==null ) {
149
                this.txtSchema.setText(this.sqlbuilder.default_schema());
150
            }
151
        } catch (Exception ex) {
152
            throw new RuntimeException("Can't retrieve the sqlbuilder", ex);
153
        }
154

    
155
        // set values from params
156
        if (this.parameters.getSchema() != null) {
157
            this.txtSchema.setText((String) this.parameters.getSchema());
158
            this.txtTableName.setText(this.parameters.getTableName());
159
        }
160
    }
161

    
162
    @Override
163
    public JComponent asJComponent() {
164
        return this;
165
    }
166

    
167
}