Revision 41488 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

View differences:

SelectPkPanel.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 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 3
9
 * of the License, or (at your option) any later version.
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 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.
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 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.
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.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.exportto.swing.prov.jdbc.panel;
25 24

  
26 25
import java.awt.event.ActionEvent;
27 26
import java.awt.event.ActionListener;
27
import javax.swing.DefaultListModel;
28 28
import javax.swing.JComponent;
29 29
import org.apache.commons.lang3.StringUtils;
30 30
import org.gvsig.exportto.swing.prov.jdbc.ExporttoJDBCProvider;
......
32 32
import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException;
33 33
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel;
34 34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
35 36
import org.gvsig.fmap.dal.feature.FeatureType;
36 37
import org.gvsig.tools.ToolsLocator;
37 38
import org.gvsig.tools.i18n.I18nManager;
......
39 40
/**
40 41
 * @author gvSIG Team
41 42
 * @version $Id$
42
 * 
43
 *
43 44
 */
44 45
public class SelectPkPanel extends SelectPkPanelLayout implements ExporttoSwingProviderPanel {
45 46

  
46 47
    private static final long serialVersionUID = 2652404227373508779L;
47
    
48
    private FeatureType featType = null;
48

  
49 49
    private ExporttoJDBCProvider provider;
50
    
50

  
51 51
    public SelectPkPanel(ExporttoJDBCProvider provider) {
52 52
        this.provider = provider;
53 53
    }
54
    
55
    public SelectPkPanel(ExporttoJDBCProvider provider,FeatureType ft) {
56
        this(provider);
57
        featType = ft;
58
    }
59
    
54

  
60 55
    protected void initComponents() {
61
        this.chkCreatePrimaryKey.addActionListener(new ActionListener() {
56
        this.rdoCreatePrimaryNewKey.addActionListener(new ActionListener() {
62 57
            public void actionPerformed(ActionEvent e) {
63
                onChangeCreatePrimaryKey();
58
                onSelect();
64 59
            }
65 60
        });
61
        this.rdoUseExistingFieldAsPrimariKey.addActionListener(new ActionListener() {
62
            public void actionPerformed(ActionEvent e) {
63
                onSelect();
64
            }
65
        });
66
        this.rdoDoNotCreatePrimaryKey.addActionListener(new ActionListener() {
67
            public void actionPerformed(ActionEvent e) {
68
                onSelect();
69
            }
70
        });
66 71
        this.translateControls();
67 72
    }
68
    
69
    private void onChangeCreatePrimaryKey() {
70
        this.txtPrimaryKeyName.setEnabled(this.chkCreatePrimaryKey.isSelected());
73

  
74
    private void onSelect() {
75
        if (this.rdoCreatePrimaryNewKey.isSelected()) {
76
            this.txtPrimaryKeyName.setEnabled(true);
77
            this.lstFields.setEnabled(false);
78
        } else if (this.rdoUseExistingFieldAsPrimariKey.isSelected()) {
79
            this.txtPrimaryKeyName.setEnabled(false);
80
            this.lstFields.setEnabled(true);
81
        } else if (this.rdoDoNotCreatePrimaryKey.isSelected()) {
82
            this.txtPrimaryKeyName.setEnabled(false);
83
            this.lstFields.setEnabled(false);
84
        }
71 85
    }
72
    
86

  
73 87
    protected void translateControls() {
74 88
        I18nManager i18nManager = ToolsLocator.getI18nManager();
75
        
89

  
76 90
    }
77 91

  
78
    public String getPrimaryKeyName() {        
79
        if( !this.chkCreatePrimaryKey.isSelected() ) {
80
            return null;
92
    public String getPrimaryKeyName() {
93
        String pkname = null;
94
        if (this.rdoCreatePrimaryNewKey.isSelected()) {
95
            pkname = this.txtPrimaryKeyName.getText();
96

  
97
        } else if (this.rdoUseExistingFieldAsPrimariKey.isSelected()) {
98
            pkname = (String) this.lstFields.getSelectedValue();
81 99
        }
82
        String pkname = this.txtPrimaryKeyName.getText();
83
        if( StringUtils.isBlank(pkname) ) {
100
        if (StringUtils.isBlank(pkname)) {
84 101
            return null;
85 102
        }
86 103
        return pkname.trim();
......
92 109
    }
93 110

  
94 111
    public boolean isValidPanel() throws ExporttoPanelValidationException {
95
        String pkname = this.getPrimaryKeyName();
96
        if (isFieldName(pkname, this.featType)) {
97
            I18nManager i18nManager = ToolsLocator.getI18nManager();
98
            throw new ExporttoPanelValidationException(
99
                i18nManager.getTranslation("_Field_name_already_exists"));
100
        }
101 112
        return true;
102 113
    }
103 114

  
104
    private boolean isFieldName(String name, FeatureType ft) {
105
        if (ft == null || name == null) {
106
            return false;
107
        }
108
        FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
109
        for (int i=0; i<atts.length; i++) {
110
            if (atts[i].getName().equalsIgnoreCase(name)) {
111
                return true;
112
            }
113
        }
114
        return false;
115
    public void enterPanel() {
116
        this.fillListOfFields();
115 117
    }
116 118

  
117 119
    public JComponent asJComponent() {
118 120
        return this;
119 121
    }
120
    
121
    
122

  
123
    private void fillListOfFields() {
124
        try {
125
            FeatureStore source = this.provider.getSource();
126
            FeatureType sourceFeatureType = source.getDefaultFeatureType();
127
            DefaultListModel model = new DefaultListModel();
128
            for (int i = 0; i < sourceFeatureType.size(); i++) {
129
                model.addElement(sourceFeatureType.getAttributeDescriptor(i).getName());
130
            }
131
            this.lstFields.setModel(model);
132
        } catch (Exception ex) {
133
            throw new RuntimeException(ex);
134
        }
135
    }
122 136
}

Also available in: Unified diff