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 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41488 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 41488 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 41488 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 41488 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.exportto.swing.prov.jdbc.panel; |
||
24 | |||
25 | 41486 | jjdelcerro | import java.awt.event.ActionEvent; |
26 | import java.awt.event.ActionListener; |
||
27 | 41488 | jjdelcerro | import javax.swing.DefaultListModel; |
28 | 41486 | jjdelcerro | import javax.swing.JComponent; |
29 | 43397 | jjdelcerro | import org.apache.commons.lang3.ArrayUtils; |
30 | 41486 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
31 | 41598 | jjdelcerro | import org.gvsig.exportto.swing.prov.jdbc.ExporttoJDBCOptions; |
32 | 41127 | jldominguez | |
33 | 40435 | jjdelcerro | import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException; |
34 | 41486 | jjdelcerro | import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel; |
35 | 41127 | jldominguez | import org.gvsig.fmap.dal.feature.FeatureType; |
36 | 41486 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
37 | import org.gvsig.tools.i18n.I18nManager; |
||
38 | 40435 | jjdelcerro | |
39 | /**
|
||
40 | * @author gvSIG Team
|
||
41 | * @version $Id$
|
||
42 | 41488 | jjdelcerro | *
|
43 | 40435 | jjdelcerro | */
|
44 | 41486 | jjdelcerro | public class SelectPkPanel extends SelectPkPanelLayout implements ExporttoSwingProviderPanel { |
45 | 40435 | jjdelcerro | |
46 | private static final long serialVersionUID = 2652404227373508779L; |
||
47 | 41488 | jjdelcerro | |
48 | 43920 | jjdelcerro | private final ExporttoJDBCOptions options; |
49 | 41488 | jjdelcerro | |
50 | 43920 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
51 | public SelectPkPanel(ExporttoJDBCOptions options) {
|
||
52 | this.options = options;
|
||
53 | 41492 | jjdelcerro | initComponents(); |
54 | 41127 | jldominguez | } |
55 | 41488 | jjdelcerro | |
56 | 41486 | jjdelcerro | protected void initComponents() { |
57 | 41488 | jjdelcerro | this.rdoCreatePrimaryNewKey.addActionListener(new ActionListener() { |
58 | 43397 | jjdelcerro | @Override
|
59 | 41486 | jjdelcerro | public void actionPerformed(ActionEvent e) { |
60 | 41488 | jjdelcerro | onSelect(); |
61 | 41486 | jjdelcerro | } |
62 | }); |
||
63 | 41488 | jjdelcerro | this.rdoUseExistingFieldAsPrimariKey.addActionListener(new ActionListener() { |
64 | 43397 | jjdelcerro | @Override
|
65 | 41488 | jjdelcerro | public void actionPerformed(ActionEvent e) { |
66 | onSelect(); |
||
67 | } |
||
68 | }); |
||
69 | this.rdoDoNotCreatePrimaryKey.addActionListener(new ActionListener() { |
||
70 | 43397 | jjdelcerro | @Override
|
71 | 41488 | jjdelcerro | public void actionPerformed(ActionEvent e) { |
72 | onSelect(); |
||
73 | } |
||
74 | }); |
||
75 | 43397 | jjdelcerro | if( this.hasSourcePrimaryKey() ) { |
76 | this.rdoDoNotCreatePrimaryKey.setSelected(true); |
||
77 | } else {
|
||
78 | this.rdoCreatePrimaryNewKey.setSelected(true); |
||
79 | } |
||
80 | 41728 | jjdelcerro | this.translate();
|
81 | 41486 | jjdelcerro | } |
82 | 41728 | jjdelcerro | |
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 | 41488 | jjdelcerro | |
95 | 41728 | jjdelcerro | |
96 | 41488 | jjdelcerro | 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 | 41486 | jjdelcerro | } |
108 | 41488 | jjdelcerro | |
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 | 41127 | jldominguez | } |
117 | 41488 | jjdelcerro | if (StringUtils.isBlank(pkname)) {
|
118 | 41486 | jjdelcerro | return null; |
119 | } |
||
120 | return pkname.trim();
|
||
121 | 41127 | jldominguez | } |
122 | |||
123 | 43397 | jjdelcerro | @Override
|
124 | 40435 | jjdelcerro | public String getPanelTitle() { |
125 | 41486 | jjdelcerro | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
126 | return i18nManager.getTranslation("_Primary_key"); |
||
127 | 40435 | jjdelcerro | } |
128 | |||
129 | 43397 | jjdelcerro | @Override
|
130 | 40435 | jjdelcerro | public boolean isValidPanel() throws ExporttoPanelValidationException { |
131 | 43920 | jjdelcerro | this.options.setPrimaryKey(this.getPrimaryKeyName()); |
132 | 40435 | jjdelcerro | return true; |
133 | } |
||
134 | 43397 | jjdelcerro | |
135 | private boolean hasSourcePrimaryKey() { |
||
136 | 43920 | jjdelcerro | FeatureType featureType = this.options.getSourceFeatureType();
|
137 | return ! ArrayUtils.isEmpty(featureType.getPrimaryKey());
|
||
138 | 43397 | jjdelcerro | } |
139 | 41127 | jldominguez | |
140 | 43397 | jjdelcerro | @Override
|
141 | 41488 | jjdelcerro | public void enterPanel() { |
142 | this.fillListOfFields();
|
||
143 | 43920 | jjdelcerro | if( !this.options.canCreatetable() ) { |
144 | 41654 | jjdelcerro | 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 | 43397 | jjdelcerro | if( this.hasSourcePrimaryKey() ) { |
160 | this.rdoDoNotCreatePrimaryKey.setSelected(true); |
||
161 | } else {
|
||
162 | this.rdoCreatePrimaryNewKey.setSelected(true); |
||
163 | } |
||
164 | 41654 | jjdelcerro | } |
165 | } |
||
166 | 41127 | jldominguez | } |
167 | 41486 | jjdelcerro | |
168 | 43397 | jjdelcerro | @Override
|
169 | 41486 | jjdelcerro | public JComponent asJComponent() { |
170 | return this; |
||
171 | } |
||
172 | 41488 | jjdelcerro | |
173 | private void fillListOfFields() { |
||
174 | try {
|
||
175 | 43920 | jjdelcerro | FeatureType sourceFeatureType = this.options.getSourceFeatureType();
|
176 | 41488 | jjdelcerro | 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 | 40435 | jjdelcerro | } |