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 / SelectTableNamePanel.java @ 41598
History | View | Annotate | Download (14.2 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 java.awt.event.ComponentEvent; |
28 |
import java.awt.event.ComponentListener; |
29 |
import java.awt.event.ContainerEvent; |
30 |
import java.awt.event.ContainerListener; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
|
34 |
import javax.swing.DefaultListModel; |
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JOptionPane; |
37 |
import javax.swing.ListModel; |
38 |
import javax.swing.SwingUtilities; |
39 |
import javax.swing.event.AncestorEvent; |
40 |
import javax.swing.event.AncestorListener; |
41 |
import org.apache.commons.lang3.StringUtils; |
42 |
import org.gvsig.app.ApplicationLocator; |
43 |
import org.gvsig.app.ApplicationManager; |
44 |
import org.gvsig.exportto.swing.ExporttoSwingLocator; |
45 |
import org.gvsig.exportto.swing.ExporttoSwingManager; |
46 |
import org.gvsig.exportto.swing.prov.jdbc.ExporttoJDBCOptions; |
47 |
import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException; |
48 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel; |
49 |
import org.gvsig.fmap.dal.DALLocator; |
50 |
import org.gvsig.fmap.dal.DataManager; |
51 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
52 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
53 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.i18n.I18nManager; |
56 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
57 |
import org.gvsig.tools.task.SimpleTaskStatus; |
58 |
import org.slf4j.Logger; |
59 |
import org.slf4j.LoggerFactory; |
60 |
|
61 |
/**
|
62 |
* @author gvSIG Team
|
63 |
* @version $Id$
|
64 |
*
|
65 |
*/
|
66 |
public class SelectTableNamePanel extends SelectTableNamePanelLayout implements ExporttoSwingProviderPanel { |
67 |
|
68 |
private static final Logger logger = LoggerFactory.getLogger(SelectTableNamePanel.class); |
69 |
|
70 |
private static final long serialVersionUID = 6269512983586358017L; |
71 |
private final ExporttoJDBCOptions provider; |
72 |
|
73 |
private FillTablesListTask task = null; |
74 |
|
75 |
private static class TableItem { |
76 |
|
77 |
private JDBCStoreParameters params;
|
78 |
private String label; |
79 |
|
80 |
public TableItem(String label, JDBCStoreParameters params) { |
81 |
this.params = params;
|
82 |
this.label = label;
|
83 |
} |
84 |
|
85 |
public TableItem(JDBCStoreParameters params) {
|
86 |
this(params.getSchema() + "." + params.getTable(), params); |
87 |
} |
88 |
|
89 |
public String toString() { |
90 |
return this.label; |
91 |
} |
92 |
|
93 |
public JDBCStoreParameters getParams() {
|
94 |
return this.params; |
95 |
} |
96 |
} |
97 |
|
98 |
public SelectTableNamePanel(ExporttoJDBCOptions provider) {
|
99 |
this.provider = provider;
|
100 |
initComponents(); |
101 |
this.addAncestorListener(new AncestorListener() { |
102 |
|
103 |
public void ancestorAdded(AncestorEvent ae) { |
104 |
} |
105 |
|
106 |
public void ancestorRemoved(AncestorEvent ae) { |
107 |
cancelTask(); |
108 |
} |
109 |
|
110 |
public void ancestorMoved(AncestorEvent ae) { |
111 |
} |
112 |
}); |
113 |
} |
114 |
|
115 |
private void initComponents() { |
116 |
this.rdoCreateTable.addActionListener(new ActionListener() { |
117 |
public void actionPerformed(ActionEvent e) { |
118 |
onChangeRadioSelecion(); |
119 |
} |
120 |
}); |
121 |
this.rdoInsert.addActionListener(new ActionListener() { |
122 |
public void actionPerformed(ActionEvent e) { |
123 |
onChangeRadioSelecion(); |
124 |
} |
125 |
}); |
126 |
this.rdoCreateTable.setSelected(true); |
127 |
this.rdoInsert.setEnabled(false); |
128 |
this.lstTables.setEnabled(false); |
129 |
try {
|
130 |
this.txtTableName.setText(this.provider.getSource().getName()); |
131 |
} catch (Exception ex) { |
132 |
logger.warn("Can't set the default value for the table name", ex);
|
133 |
} |
134 |
|
135 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
136 |
this.lblHeader.setText(i18nManager.getTranslation("_Indique_donde_desea_insertar_los_datos")); |
137 |
this.lblWarningUseExistingTable.setText(
|
138 |
"<html>\n"
|
139 |
+ i18nManager.getTranslation("_Los_datos_se_insertaran_usando_los_nombres_de_columna_que_coincidan_con_la_tabla_origen_dejandose_al_valor_por_defecto_para_los_que_no_haya_valores_en_la_tabla_origen")
|
140 |
+ "\n</html>"
|
141 |
); |
142 |
this.rdoInsert.setText(i18nManager.getTranslation("_Insertar_registros_en_una_tabla_existente")); |
143 |
this.lblSelectTableName.setText(i18nManager.getTranslation("_Seleccione_la_tabla_a_usar")); |
144 |
this.rdoCreateTable.setText(i18nManager.getTranslation("_Crear_una_tabla_nueva")); |
145 |
this.lblSchema.setText(i18nManager.getTranslation("_Indique_el_esquema_en_el_que_desea_crear_la_tabla")); |
146 |
this.lblTableName.setText(i18nManager.getTranslation("_Indique_el_nombre_de_la_tabla")); |
147 |
} |
148 |
|
149 |
private void cancelTask() { |
150 |
if (task != null) { |
151 |
task.cancelRequest(); |
152 |
task.getSimpleTaskStatus().remove(); |
153 |
task = null;
|
154 |
} |
155 |
} |
156 |
|
157 |
public boolean canCreateTable() { |
158 |
return this.rdoCreateTable.isSelected(); |
159 |
} |
160 |
|
161 |
public String getSchema() { |
162 |
if (this.canCreateTable()) { |
163 |
return StringUtils.defaultIfBlank(this.txtSchema.getText(), null); |
164 |
} |
165 |
TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
166 |
JDBCStoreParameters tableParameter = item.getParams(); |
167 |
if (tableParameter == null) { |
168 |
return null; |
169 |
} |
170 |
return tableParameter.getSchema();
|
171 |
} |
172 |
|
173 |
public String getTableName() { |
174 |
if (this.canCreateTable()) { |
175 |
return StringUtils.defaultIfBlank(this.txtTableName.getText(), null); |
176 |
} |
177 |
TableItem item = (TableItem) this.lstTables.getSelectedValue();
|
178 |
if (item == null) { |
179 |
return null; |
180 |
} |
181 |
JDBCStoreParameters tableParameter = item.getParams(); |
182 |
|
183 |
if (tableParameter == null) { |
184 |
return null; |
185 |
} |
186 |
return tableParameter.getTable();
|
187 |
} |
188 |
|
189 |
public String getPanelTitle() { |
190 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
191 |
return i18nManager.getTranslation("_Tablename"); |
192 |
} |
193 |
|
194 |
public boolean isValidPanel() throws ExporttoPanelValidationException { |
195 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
196 |
String tablename = this.getTableName(); |
197 |
if (tablename == null) { |
198 |
throw new ExporttoPanelValidationException( |
199 |
i18nManager.getTranslation( |
200 |
"_The_name_of_table_cannot_be_empty"
|
201 |
) |
202 |
); |
203 |
} |
204 |
String schema = this.getSchema(); |
205 |
if (schema == null) { |
206 |
throw new ExporttoPanelValidationException( |
207 |
i18nManager.getTranslation( |
208 |
"_The_name_of_schema_cannot_be_empty"
|
209 |
) |
210 |
); |
211 |
} |
212 |
if (this.rdoCreateTable.isSelected()) { |
213 |
String tablename_tr = tablename;
|
214 |
if( this.provider.getTranslateIdentifiersToLowerCase() ) { |
215 |
tablename_tr = tablename_tr.toLowerCase(); |
216 |
} |
217 |
if( this.provider.getRemoveSpacesInIdentifiers() ) { |
218 |
tablename_tr = StringUtils.normalizeSpace(tablename_tr).replace(" ", "_"); |
219 |
} |
220 |
if( !tablename_tr.equals(tablename) ) {
|
221 |
ApplicationManager application = ApplicationLocator.getManager(); |
222 |
String msg = i18nManager.getTranslation(
|
223 |
"Ha_utilizado_espacios_en_blanco_o_mayusculas_en_el_nombre_de_la_tabla_Desea_que_se_corrija_de_forma_automatica"
|
224 |
); |
225 |
int resp = application.confirmDialog(
|
226 |
"Ha utilizado espacios en blanco o mayusculas en el nombre de la tabla.\n? Desea que se corrija de forma automatica ?",
|
227 |
i18nManager.getTranslation("_Warning"),
|
228 |
JOptionPane.YES_NO_OPTION,
|
229 |
JOptionPane.WARNING_MESSAGE
|
230 |
); |
231 |
if( resp != JOptionPane.YES_OPTION ) { |
232 |
msg = i18nManager.getTranslation( |
233 |
"El_nombre_de_tabla_contiene_caracteres no_validos"
|
234 |
); |
235 |
throw new ExporttoPanelValidationException(msg); |
236 |
} |
237 |
tablename = tablename_tr; |
238 |
this.txtTableName.setText(tablename);
|
239 |
} |
240 |
ListModel model = this.lstTables.getModel(); |
241 |
for (int i = 0; i < model.getSize(); i++) { |
242 |
TableItem item = (TableItem) model.getElementAt(i); |
243 |
if (schema.equals(item.getParams().getSchema())
|
244 |
&& tablename.equals(item.getParams().getTable())) { |
245 |
String msg = i18nManager.getTranslation(
|
246 |
"_La_tabla_{0}_{1}_ya_existe_en_la_base_de_datos_Seleccione_la_opcion_de_insertar_registros_en_una_tabla_existente_para_a?adir_los_datos_a_esta_o_indique_otro_nombre",
|
247 |
new String[]{schema, tablename} |
248 |
); |
249 |
throw new ExporttoPanelValidationException(msg); |
250 |
} |
251 |
} |
252 |
} |
253 |
return true; |
254 |
} |
255 |
|
256 |
public void enterPanel() { |
257 |
this.fillTablesList();
|
258 |
} |
259 |
|
260 |
public JComponent asJComponent() { |
261 |
return this; |
262 |
} |
263 |
|
264 |
public void onChangeRadioSelecion() { |
265 |
if (this.rdoCreateTable.isSelected()) { |
266 |
this.txtSchema.setEnabled(true); |
267 |
this.txtTableName.setEnabled(true); |
268 |
this.lstTables.setEnabled(false); |
269 |
} else {
|
270 |
this.txtSchema.setEnabled(false); |
271 |
this.txtTableName.setEnabled(false); |
272 |
this.lstTables.setEnabled(true); |
273 |
} |
274 |
} |
275 |
|
276 |
private void fillTablesList() { |
277 |
|
278 |
JDBCServerExplorerParameters explorerParameters = this.provider.getExplorerParameters();
|
279 |
if (explorerParameters == null) { |
280 |
return;
|
281 |
} |
282 |
cancelTask(); |
283 |
this.task = new FillTablesListTask(); |
284 |
task.setDaemon(true);
|
285 |
task.start(); |
286 |
} |
287 |
|
288 |
private class FillTablesListTask extends AbstractMonitorableTask { |
289 |
|
290 |
public FillTablesListTask() {
|
291 |
super("Export"); |
292 |
} |
293 |
|
294 |
protected SimpleTaskStatus getSimpleTaskStatus() {
|
295 |
return (SimpleTaskStatus) this.getTaskStatus(); |
296 |
} |
297 |
|
298 |
public void run() { |
299 |
|
300 |
JDBCServerExplorerParameters explorerParameters = provider.getExplorerParameters(); |
301 |
if (provider.getExplorerParameters() == null) { |
302 |
return;
|
303 |
} |
304 |
final SimpleTaskStatus status = this.getSimpleTaskStatus(); |
305 |
try {
|
306 |
status.setAutoremove(true);
|
307 |
|
308 |
DataManager dataManager = DALLocator.getDataManager(); |
309 |
|
310 |
this.getSimpleTaskStatus().message("Connecting server"); |
311 |
explorerParameters.setShowInformationDBTables(false);
|
312 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
313 |
explorerParameters.getExplorerName(), |
314 |
explorerParameters |
315 |
); |
316 |
|
317 |
this.getSimpleTaskStatus().message("Retrieving tables"); |
318 |
final List<JDBCStoreParameters> tables = explorer.list(); |
319 |
|
320 |
this.getSimpleTaskStatus().message("Add tables"); |
321 |
|
322 |
SwingUtilities.invokeAndWait(new Runnable() { |
323 |
public void run() { |
324 |
DefaultListModel lmodel = new DefaultListModel(); |
325 |
Iterator<JDBCStoreParameters> it = tables.iterator();
|
326 |
while (it.hasNext()) {
|
327 |
if (status.isCancelled()) {
|
328 |
status.cancel(); |
329 |
break;
|
330 |
} |
331 |
JDBCStoreParameters table = it.next(); |
332 |
lmodel.addElement(new TableItem(table));
|
333 |
} |
334 |
lstTables.setModel(lmodel); |
335 |
//lstTables.setEnabled(true);
|
336 |
rdoInsert.setEnabled(true);
|
337 |
} |
338 |
}); |
339 |
|
340 |
status.message("finish");
|
341 |
status.terminate(); |
342 |
|
343 |
} catch (final Exception ex) { |
344 |
logger.warn("Fail to fill tables list", ex);
|
345 |
if (status.isCancellationRequested()) {
|
346 |
status.cancel(); |
347 |
} |
348 |
if (status.isRunning()) {
|
349 |
try {
|
350 |
SwingUtilities.invokeAndWait(new Runnable() { |
351 |
public void run() { |
352 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
353 |
ExporttoSwingManager manager = ExporttoSwingLocator.getSwingManager(); |
354 |
|
355 |
manager.showMessage( |
356 |
i18nManager.getTranslation("_Warning"),
|
357 |
i18nManager.getTranslation("_There_have_been_problems_filling_data_in_panel")
|
358 |
+ " (" + getPanelTitle() + ")", |
359 |
ex, |
360 |
null
|
361 |
); |
362 |
} |
363 |
}); |
364 |
} catch (Exception ex2) { |
365 |
logger.warn("Can't show error message", ex2);
|
366 |
} |
367 |
} |
368 |
} finally {
|
369 |
status.terminate(); |
370 |
status.remove(); |
371 |
} |
372 |
|
373 |
} |
374 |
|
375 |
} |
376 |
|
377 |
} |