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 / export / jdbc / swing / panels / SelectPkPanel.java @ 43968
History | View | Annotate | Download (7.37 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.export.jdbc.swing.panels; |
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.export.jdbc.service.ExportJDBCParameters; |
32 |
import org.gvsig.export.swing.JExportProcessPanel; |
33 |
import org.gvsig.export.swing.spi.ExportPanel; |
34 |
import org.gvsig.export.swing.spi.ExportPanelValidationException; |
35 |
|
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.tools.ToolsLocator; |
38 |
import org.gvsig.tools.i18n.I18nManager; |
39 |
|
40 |
/**
|
41 |
* @author gvSIG Team
|
42 |
* @version $Id$
|
43 |
*
|
44 |
*/
|
45 |
public class SelectPkPanel |
46 |
extends SelectPkPanelLayout
|
47 |
implements ExportPanel
|
48 |
{ |
49 |
|
50 |
private static final long serialVersionUID = 2652404227373508779L; |
51 |
|
52 |
private final ExportJDBCParameters parameters; |
53 |
private final JExportProcessPanel processPanel; |
54 |
|
55 |
public SelectPkPanel(
|
56 |
JExportProcessPanel processPanel, |
57 |
ExportJDBCParameters parameters |
58 |
) { |
59 |
this.processPanel = processPanel;
|
60 |
this.parameters = parameters;
|
61 |
initComponents(); |
62 |
} |
63 |
|
64 |
private void initComponents() { |
65 |
this.rdoCreatePrimaryNewKey.addActionListener(new ActionListener() { |
66 |
@Override
|
67 |
public void actionPerformed(ActionEvent e) { |
68 |
onSelect(); |
69 |
} |
70 |
}); |
71 |
this.rdoUseExistingFieldAsPrimariKey.addActionListener(new ActionListener() { |
72 |
@Override
|
73 |
public void actionPerformed(ActionEvent e) { |
74 |
onSelect(); |
75 |
} |
76 |
}); |
77 |
this.rdoDoNotCreatePrimaryKey.addActionListener(new ActionListener() { |
78 |
@Override
|
79 |
public void actionPerformed(ActionEvent e) { |
80 |
onSelect(); |
81 |
} |
82 |
}); |
83 |
if( this.hasSourcePrimaryKey() ) { |
84 |
this.rdoDoNotCreatePrimaryKey.setSelected(true); |
85 |
} else {
|
86 |
this.rdoCreatePrimaryNewKey.setSelected(true); |
87 |
} |
88 |
this.translate();
|
89 |
} |
90 |
|
91 |
private void translate() { |
92 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
93 |
|
94 |
this.lblHeader.setText("<html>"+i18nManager.getTranslation("_Primary_key_header")+"</html>"); |
95 |
this.lblQuestion.setText(i18nManager.getTranslation("_Desea_crear_una_clave_primaria")); |
96 |
this.rdoCreatePrimaryNewKey.setText(i18nManager.getTranslation("_Generar_una_clave_primaria_con_un_serial")); |
97 |
this.lblPrimaryKeyName.setText(i18nManager.getTranslation("_Indique_el_nombre_a_usar_para_la_clave_primaria")); |
98 |
this.rdoUseExistingFieldAsPrimariKey.setText(i18nManager.getTranslation("_Utilizar_un_campo_existente_como_clave_primaria")); |
99 |
this.lblSelectFieldAsPrimaryKey.setText(i18nManager.getTranslation("_Seleccione_el_campo_a_usar_como_clave_primaria")); |
100 |
this.rdoDoNotCreatePrimaryKey.setText(i18nManager.getTranslation("_No_hacer_nada_en_relacion_a_la_creacion_de_la_clave_primaria")); |
101 |
} |
102 |
|
103 |
|
104 |
private void onSelect() { |
105 |
if (this.rdoCreatePrimaryNewKey.isSelected()) { |
106 |
this.txtPrimaryKeyName.setEnabled(true); |
107 |
this.lstFields.setEnabled(false); |
108 |
} else if (this.rdoUseExistingFieldAsPrimariKey.isSelected()) { |
109 |
this.txtPrimaryKeyName.setEnabled(false); |
110 |
this.lstFields.setEnabled(true); |
111 |
} else if (this.rdoDoNotCreatePrimaryKey.isSelected()) { |
112 |
this.txtPrimaryKeyName.setEnabled(false); |
113 |
this.lstFields.setEnabled(false); |
114 |
} |
115 |
} |
116 |
|
117 |
public String getPrimaryKeyName() { |
118 |
String pkname = null; |
119 |
if (this.rdoCreatePrimaryNewKey.isSelected()) { |
120 |
pkname = this.txtPrimaryKeyName.getText();
|
121 |
|
122 |
} else if (this.rdoUseExistingFieldAsPrimariKey.isSelected()) { |
123 |
pkname = (String) this.lstFields.getSelectedValue(); |
124 |
} |
125 |
if (StringUtils.isBlank(pkname)) {
|
126 |
return null; |
127 |
} |
128 |
return pkname.trim();
|
129 |
} |
130 |
|
131 |
@Override
|
132 |
public String getIdPanel() { |
133 |
return this.getClass().getCanonicalName(); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public String getTitlePanel() { |
138 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
139 |
return i18nManager.getTranslation("_Primary_key"); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void previousPanel() { |
144 |
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void nextPanel() { |
149 |
this.parameters.setPrimaryKey(this.getPrimaryKeyName()); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public boolean validatePanel() throws ExportPanelValidationException { |
154 |
return true; |
155 |
} |
156 |
|
157 |
private boolean hasSourcePrimaryKey() { |
158 |
FeatureType featureType = this.parameters.getSourceFeatureType();
|
159 |
return ! ArrayUtils.isEmpty(featureType.getPrimaryKey());
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void enterPanel() { |
164 |
this.fillListOfFields();
|
165 |
if( !this.parameters.canCreatetable() ) { |
166 |
this.rdoCreatePrimaryNewKey.setEnabled(false); |
167 |
this.rdoUseExistingFieldAsPrimariKey.setEnabled(false); |
168 |
this.rdoDoNotCreatePrimaryKey.setEnabled(false); |
169 |
|
170 |
this.rdoCreatePrimaryNewKey.setSelected(false); |
171 |
this.rdoUseExistingFieldAsPrimariKey.setSelected(false); |
172 |
this.rdoDoNotCreatePrimaryKey.setSelected(false); |
173 |
} else {
|
174 |
this.rdoCreatePrimaryNewKey.setEnabled(true); |
175 |
this.rdoUseExistingFieldAsPrimariKey.setEnabled(true); |
176 |
this.rdoDoNotCreatePrimaryKey.setEnabled(true); |
177 |
|
178 |
if (! this.rdoCreatePrimaryNewKey.isSelected() |
179 |
&& ! this.rdoUseExistingFieldAsPrimariKey.isSelected()
|
180 |
&& ! this.rdoDoNotCreatePrimaryKey.isSelected()) {
|
181 |
if( this.hasSourcePrimaryKey() ) { |
182 |
this.rdoDoNotCreatePrimaryKey.setSelected(true); |
183 |
} else {
|
184 |
this.rdoCreatePrimaryNewKey.setSelected(true); |
185 |
} |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public JComponent asJComponent() { |
192 |
return this; |
193 |
} |
194 |
|
195 |
private void fillListOfFields() { |
196 |
try {
|
197 |
FeatureType sourceFeatureType = this.parameters.getSourceFeatureType();
|
198 |
DefaultListModel model = new DefaultListModel(); |
199 |
for (int i = 0; i < sourceFeatureType.size(); i++) { |
200 |
model.addElement(sourceFeatureType.getAttributeDescriptor(i).getName()); |
201 |
} |
202 |
this.lstFields.setModel(model);
|
203 |
} catch (Exception ex) { |
204 |
throw new RuntimeException(ex); |
205 |
} |
206 |
} |
207 |
} |