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 / SelectPkPanel.java @ 43920

History | View | Annotate | Download (7.01 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 javax.swing.DefaultListModel;
28
import javax.swing.JComponent;
29
import org.apache.commons.lang3.ArrayUtils;
30
import org.apache.commons.lang3.StringUtils;
31
import org.gvsig.exportto.swing.prov.jdbc.ExporttoJDBCOptions;
32

    
33
import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException;
34
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.i18n.I18nManager;
38

    
39
/**
40
 * @author gvSIG Team
41
 * @version $Id$
42
 *
43
 */
44
public class SelectPkPanel extends SelectPkPanelLayout implements ExporttoSwingProviderPanel {
45

    
46
    private static final long serialVersionUID = 2652404227373508779L;
47

    
48
    private final ExporttoJDBCOptions options;
49

    
50
    @SuppressWarnings("OverridableMethodCallInConstructor")
51
    public SelectPkPanel(ExporttoJDBCOptions options) {
52
        this.options = options;
53
        initComponents();
54
    }
55

    
56
    protected void initComponents() {
57
        this.rdoCreatePrimaryNewKey.addActionListener(new ActionListener() {
58
            @Override
59
            public void actionPerformed(ActionEvent e) {
60
                onSelect();
61
            }
62
        });
63
        this.rdoUseExistingFieldAsPrimariKey.addActionListener(new ActionListener() {
64
            @Override
65
            public void actionPerformed(ActionEvent e) {
66
                onSelect();
67
            }
68
        });
69
        this.rdoDoNotCreatePrimaryKey.addActionListener(new ActionListener() {
70
            @Override
71
            public void actionPerformed(ActionEvent e) {
72
                onSelect();
73
            }
74
        });
75
        if( this.hasSourcePrimaryKey() ) {
76
            this.rdoDoNotCreatePrimaryKey.setSelected(true);
77
        } else {
78
            this.rdoCreatePrimaryNewKey.setSelected(true);
79
        }        
80
        this.translate();
81
    }
82
    
83
    private void translate() {
84
        I18nManager i18nManager = ToolsLocator.getI18nManager();
85
        
86
        this.lblHeader.setText("<html>"+i18nManager.getTranslation("_Primary_key_header")+"</html>");
87
        this.lblQuestion.setText(i18nManager.getTranslation("_Desea_crear_una_clave_primaria"));
88
        this.rdoCreatePrimaryNewKey.setText(i18nManager.getTranslation("_Generar_una_clave_primaria_con_un_serial"));
89
        this.lblPrimaryKeyName.setText(i18nManager.getTranslation("_Indique_el_nombre_a_usar_para_la_clave_primaria"));
90
        this.rdoUseExistingFieldAsPrimariKey.setText(i18nManager.getTranslation("_Utilizar_un_campo_existente_como_clave_primaria"));
91
        this.lblSelectFieldAsPrimaryKey.setText(i18nManager.getTranslation("_Seleccione_el_campo_a_usar_como_clave_primaria"));
92
        this.rdoDoNotCreatePrimaryKey.setText(i18nManager.getTranslation("_No_hacer_nada_en_relacion_a_la_creacion_de_la_clave_primaria"));
93
    }
94

    
95

    
96
    private void onSelect() {
97
        if (this.rdoCreatePrimaryNewKey.isSelected()) {
98
            this.txtPrimaryKeyName.setEnabled(true);
99
            this.lstFields.setEnabled(false);
100
        } else if (this.rdoUseExistingFieldAsPrimariKey.isSelected()) {
101
            this.txtPrimaryKeyName.setEnabled(false);
102
            this.lstFields.setEnabled(true);
103
        } else if (this.rdoDoNotCreatePrimaryKey.isSelected()) {
104
            this.txtPrimaryKeyName.setEnabled(false);
105
            this.lstFields.setEnabled(false);
106
        }
107
    }
108

    
109
    public String getPrimaryKeyName() {
110
        String pkname = null;
111
        if (this.rdoCreatePrimaryNewKey.isSelected()) {
112
            pkname = this.txtPrimaryKeyName.getText();
113

    
114
        } else if (this.rdoUseExistingFieldAsPrimariKey.isSelected()) {
115
            pkname = (String) this.lstFields.getSelectedValue();
116
        }
117
        if (StringUtils.isBlank(pkname)) {
118
            return null;
119
        }
120
        return pkname.trim();
121
    }
122

    
123
    @Override
124
    public String getPanelTitle() {
125
        I18nManager i18nManager = ToolsLocator.getI18nManager();
126
        return i18nManager.getTranslation("_Primary_key");
127
    }
128

    
129
    @Override
130
    public boolean isValidPanel() throws ExporttoPanelValidationException {
131
        this.options.setPrimaryKey(this.getPrimaryKeyName());
132
        return true;
133
    }
134
    
135
    private boolean hasSourcePrimaryKey() {
136
        FeatureType featureType = this.options.getSourceFeatureType();
137
        return ! ArrayUtils.isEmpty(featureType.getPrimaryKey());
138
    }
139

    
140
    @Override
141
    public void enterPanel() {
142
        this.fillListOfFields();
143
        if( !this.options.canCreatetable() ) {
144
            this.rdoCreatePrimaryNewKey.setEnabled(false);
145
            this.rdoUseExistingFieldAsPrimariKey.setEnabled(false);
146
            this.rdoDoNotCreatePrimaryKey.setEnabled(false);
147
        
148
            this.rdoCreatePrimaryNewKey.setSelected(false);
149
            this.rdoUseExistingFieldAsPrimariKey.setSelected(false);
150
            this.rdoDoNotCreatePrimaryKey.setSelected(false);
151
        } else {
152
            this.rdoCreatePrimaryNewKey.setEnabled(true);
153
            this.rdoUseExistingFieldAsPrimariKey.setEnabled(true);
154
            this.rdoDoNotCreatePrimaryKey.setEnabled(true);
155
            
156
            if (! this.rdoCreatePrimaryNewKey.isSelected()
157
                && ! this.rdoUseExistingFieldAsPrimariKey.isSelected()
158
                && ! this.rdoDoNotCreatePrimaryKey.isSelected()) {
159
                if( this.hasSourcePrimaryKey() ) {
160
                    this.rdoDoNotCreatePrimaryKey.setSelected(true);
161
                } else {
162
                    this.rdoCreatePrimaryNewKey.setSelected(true);
163
                }
164
            }
165
        }
166
    }
167

    
168
    @Override
169
    public JComponent asJComponent() {
170
        return this;
171
    }
172

    
173
    private void fillListOfFields() {
174
        try {
175
            FeatureType sourceFeatureType = this.options.getSourceFeatureType();
176
            DefaultListModel model = new DefaultListModel();
177
            for (int i = 0; i < sourceFeatureType.size(); i++) {
178
                model.addElement(sourceFeatureType.getAttributeDescriptor(i).getName());
179
            }
180
            this.lstFields.setModel(model);
181
        } catch (Exception ex) {
182
            throw new RuntimeException(ex);
183
        }
184
    }
185
}