svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.xml2db / org.gvsig.xml2db.swing / org.gvsig.xml2db.swing.impl / src / main / java / org / gvsig / xml2db / swing / impl / createdbfromxml / CreatedbFromXmlPanel.java @ 47283
History | View | Annotate | Download (17.5 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright (c) 2007-2023 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.xml2db.swing.impl.createdbfromxml; |
24 |
|
25 |
import org.gvsig.xml2db.swing.impl.Task; |
26 |
import java.awt.Color; |
27 |
import java.awt.Cursor; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.io.File; |
31 |
import java.util.ArrayList; |
32 |
import java.util.List; |
33 |
import javax.swing.AbstractListModel; |
34 |
import javax.swing.JComponent; |
35 |
import org.cresques.cts.IProjection; |
36 |
import java.nio.charset.Charset; |
37 |
import java.util.Collections; |
38 |
import javax.swing.JOptionPane; |
39 |
import javax.swing.SwingUtilities; |
40 |
import javax.swing.event.ChangeEvent; |
41 |
import org.apache.commons.io.FilenameUtils; |
42 |
import org.apache.commons.lang3.StringUtils; |
43 |
import org.gvsig.fmap.dal.DALLocator; |
44 |
import org.gvsig.fmap.dal.DataManager; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
46 |
import org.gvsig.fmap.dal.store.h2.H2SpatialUtils; |
47 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
48 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
49 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
50 |
import org.gvsig.fmap.dal.swing.ProjectionPickerController; |
51 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel; |
52 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
53 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
54 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
55 |
import org.gvsig.tools.swing.api.pickercontroller.CharsetPickerController; |
56 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
57 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
58 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
59 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
60 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
61 |
import org.gvsig.tools.task.SimpleTaskStatus; |
62 |
import org.gvsig.tools.util.LabeledValue; |
63 |
import org.gvsig.tools.util.LabeledValueImpl; |
64 |
import org.gvsig.xml2db.lib.api.Xml2dbLocator; |
65 |
import org.gvsig.xml2db.lib.api.Xml2dbManager; |
66 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLInfo; |
67 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLTableInfo; |
68 |
import org.gvsig.xml2db.swing.Xml2dbPanel; |
69 |
import org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons; |
70 |
import org.slf4j.Logger; |
71 |
import org.slf4j.LoggerFactory; |
72 |
|
73 |
/**
|
74 |
*
|
75 |
* @author jjdelcerro
|
76 |
*/
|
77 |
@SuppressWarnings("UseSpecificCatch") |
78 |
public class CreatedbFromXmlPanel |
79 |
extends CreatedbFromXmlPanelView
|
80 |
implements Xml2dbPanel
|
81 |
{ |
82 |
|
83 |
protected static final Logger LOGGER = LoggerFactory.getLogger(CreatedbFromXmlPanel.class); |
84 |
|
85 |
private static class XMLInfoListModel extends AbstractListModel<LabeledValue<XMLTableInfo>> { |
86 |
|
87 |
private final XMLInfo xmlinfo; |
88 |
private final List<LabeledValue<XMLTableInfo>> tables; |
89 |
|
90 |
|
91 |
public XMLInfoListModel() {
|
92 |
this(null); |
93 |
} |
94 |
|
95 |
public XMLInfoListModel(XMLInfo xmlinfo) {
|
96 |
this.xmlinfo = xmlinfo;
|
97 |
if( xmlinfo != null ) { |
98 |
this.tables = new ArrayList<>(xmlinfo.size()); |
99 |
for (XMLTableInfo tableInfo : xmlinfo) {
|
100 |
this.tables.add(new LabeledValueImpl<>(tableInfo.getName(),tableInfo)); |
101 |
} |
102 |
} else {
|
103 |
this.tables = null; |
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public LabeledValue<XMLTableInfo> getElementAt(int index) { |
109 |
if( this.tables == null ) { |
110 |
return null; |
111 |
} |
112 |
return tables.get(index);
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public int getSize() { |
117 |
if( this.tables == null ) { |
118 |
return 0; |
119 |
} |
120 |
return this.tables.size(); |
121 |
} |
122 |
|
123 |
|
124 |
} |
125 |
|
126 |
|
127 |
private Dialog dialog; |
128 |
private FilePickerController pickerXMLFile;
|
129 |
private FilePickerController pickerDbfile;
|
130 |
private ProjectionPickerController pickerProjection;
|
131 |
private CharsetPickerController pickerCharset;
|
132 |
private TaskStatusController taskStatusController;
|
133 |
private Task lastTask;
|
134 |
private XMLInfo xmlinfo;
|
135 |
|
136 |
public CreatedbFromXmlPanel() {
|
137 |
this.createComponents();
|
138 |
} |
139 |
|
140 |
private void createComponents() { |
141 |
this.lastTask = Task.FAKE_TASK;
|
142 |
this.xmlinfo = null; |
143 |
this.initComponents();
|
144 |
} |
145 |
|
146 |
private void initComponents() { |
147 |
|
148 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
149 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
150 |
|
151 |
this.taskStatusController = ToolsSwingLocator.getTaskStatusSwingManager().createTaskStatusController(
|
152 |
lblStatusCaption, |
153 |
lblStatusMsg, |
154 |
pbStatus |
155 |
); |
156 |
this.taskStatusController.setVisible(false); |
157 |
|
158 |
this.translate();
|
159 |
|
160 |
this.setTablesModel(null); |
161 |
|
162 |
this.pickerXMLFile = toolsSwingManager.createFilePickerController(
|
163 |
this.txtXMLFile,
|
164 |
this.btnXMLFile
|
165 |
); |
166 |
this.pickerXMLFile.addChangeListener((ChangeEvent e) -> { |
167 |
updateStateComponents(); |
168 |
}); |
169 |
|
170 |
this.pickerCharset = toolsSwingManager.createCharsetPickerController(
|
171 |
null,
|
172 |
this.cboCharset
|
173 |
); |
174 |
|
175 |
this.pickerProjection = dataSwingManager.createProjectionPickerController(
|
176 |
this.txtProjection,
|
177 |
this.btnProjection
|
178 |
); |
179 |
|
180 |
this.pickerDbfile = toolsSwingManager.createFilePickerController(
|
181 |
this.txtDatabaseFile,
|
182 |
this.btnDataBaseFile
|
183 |
); |
184 |
this.pickerDbfile.addChangeListener((ChangeEvent e) -> { |
185 |
doChangeDBfile(); |
186 |
}); |
187 |
|
188 |
this.btnAnalizeXML.addActionListener((ActionEvent e) -> { |
189 |
doAnalizeXML(); |
190 |
}); |
191 |
this.btnAnalizeXML.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
192 |
|
193 |
this.btnViewTableStructure.addActionListener((ActionEvent e) -> { |
194 |
doViewTableStructure(); |
195 |
}); |
196 |
this.btnViewTableStructure.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
197 |
|
198 |
this.pickerCharset.set(Charset.defaultCharset()); |
199 |
|
200 |
this.chkRegisterConnection.setSelected(true); |
201 |
|
202 |
this.cboCharset.setSelectedIndex(-1); |
203 |
|
204 |
SwingUtilities.invokeLater(() -> { updateStateComponents(); });
|
205 |
|
206 |
ToolsSwingUtils.ensureRowsCols(this, 4, 80, 5, 100); |
207 |
} |
208 |
|
209 |
private void translate() { |
210 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
211 |
|
212 |
toolsSwingManager.translate(this.lblCharset);
|
213 |
toolsSwingManager.translate(this.lblRepositoryName);
|
214 |
toolsSwingManager.translate(this.lblCreatedb);
|
215 |
toolsSwingManager.translate(this.lblFileXML);
|
216 |
toolsSwingManager.translate(this.lblDatabaseFile);
|
217 |
toolsSwingManager.translate(this.lblProjection);
|
218 |
toolsSwingManager.translate(this.lblTables);
|
219 |
toolsSwingManager.translate(this.btnAnalizeXML);
|
220 |
toolsSwingManager.translate(this.btnViewTableStructure);
|
221 |
toolsSwingManager.translate(this.lblRegisterConnection);
|
222 |
} |
223 |
|
224 |
private boolean isProcessing() { |
225 |
return this.lastTask.isProcessing(); |
226 |
} |
227 |
|
228 |
private void updateStateComponents() { |
229 |
if( !SwingUtilities.isEventDispatchThread() ) { |
230 |
SwingUtilities.invokeLater(this::updateStateComponents); |
231 |
return;
|
232 |
} |
233 |
message(null);
|
234 |
if( this.isProcessing() ) { |
235 |
this.pickerXMLFile.setEnabled(false); |
236 |
this.pickerCharset.setEnabled(false); |
237 |
this.pickerProjection.setEnabled(false); |
238 |
this.btnAnalizeXML.setEnabled(false); |
239 |
this.lstTables.setEnabled(false); |
240 |
this.btnViewTableStructure.setEnabled(false); |
241 |
if( this.dialog!=null ) { |
242 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, false); |
243 |
} |
244 |
return;
|
245 |
} |
246 |
this.pickerXMLFile.setEnabled(true); |
247 |
this.pickerCharset.setEnabled(true); |
248 |
this.pickerProjection.setEnabled(true); |
249 |
|
250 |
File xmlfile = this.pickerXMLFile.get(); |
251 |
if( xmlfile == null || !xmlfile.exists() ) { |
252 |
if( xmlfile==null ) { |
253 |
this.message("_XML_file_is_required", JOptionPane.WARNING_MESSAGE); |
254 |
} else {
|
255 |
this.message("_XML_file_not_exists", JOptionPane.WARNING_MESSAGE); |
256 |
} |
257 |
this.btnAnalizeXML.setEnabled(false); |
258 |
this.lstTables.setEnabled(false); |
259 |
this.btnViewTableStructure.setEnabled(false); |
260 |
if( this.dialog!=null ) { |
261 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, false); |
262 |
} |
263 |
return;
|
264 |
} |
265 |
this.btnAnalizeXML.setEnabled(true); |
266 |
if( this.lstTables.getModel().getSize()<1 ) { |
267 |
if( this.xmlinfo == null ) { |
268 |
this.message("_Analize_XML_required", JOptionPane.WARNING_MESSAGE); |
269 |
} else {
|
270 |
this.message("_No_tables_recognized_in_the_XML", JOptionPane.WARNING_MESSAGE); |
271 |
} |
272 |
this.btnViewTableStructure.setEnabled(false); |
273 |
if( this.dialog!=null ) { |
274 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, false); |
275 |
} |
276 |
return;
|
277 |
} |
278 |
this.lstTables.setEnabled(true); |
279 |
this.btnViewTableStructure.setEnabled(true); |
280 |
File dbfile = this.pickerDbfile.get(); |
281 |
if( dbfile == null || H2SpatialUtils.existsH2db(dbfile) ) { |
282 |
if( dbfile == null ) { |
283 |
this.message("_Database_file_is_required", JOptionPane.WARNING_MESSAGE); |
284 |
} else {
|
285 |
this.message("_Database_file_already_exists", JOptionPane.WARNING_MESSAGE); |
286 |
} |
287 |
if( this.dialog!=null ) { |
288 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, false); |
289 |
} |
290 |
return;
|
291 |
} |
292 |
if( StringUtils.isBlank(this.txtRepositoryName.getText()) ) { |
293 |
this.message("_Repository_name_is_required", JOptionPane.WARNING_MESSAGE); |
294 |
if( this.dialog!=null ) { |
295 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, false); |
296 |
} |
297 |
return;
|
298 |
} |
299 |
if( this.dialog!=null ) { |
300 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTONS_OK, true); |
301 |
} |
302 |
this.taskStatusController.setVisible(this.lastTask.needToShowTheStatus()); |
303 |
} |
304 |
|
305 |
@Override
|
306 |
public void setDialog(Dialog dialog) { |
307 |
this.dialog = dialog;
|
308 |
} |
309 |
|
310 |
@Override
|
311 |
public JComponent asJComponent() { |
312 |
return this; |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void performAction() { |
317 |
doCreatedb(); |
318 |
} |
319 |
|
320 |
private void doCreatedb() { |
321 |
if( this.xmlinfo==null ) { |
322 |
return;
|
323 |
} |
324 |
File dbfile = this.pickerDbfile.get(); |
325 |
if( dbfile==null || dbfile.exists()) { |
326 |
return;
|
327 |
} |
328 |
String s = this.txtRepositoryName.getText(); |
329 |
if( StringUtils.isBlank(s) ) {
|
330 |
s = FilenameUtils.getBaseName(dbfile.getName()); |
331 |
} |
332 |
String repoName = s;
|
333 |
|
334 |
boolean registerConnection = this.chkRegisterConnection.isSelected(); |
335 |
|
336 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
337 |
|
338 |
this.lastTask = new Task( |
339 |
"Xml2db_Creating_database",
|
340 |
"Create databse",
|
341 |
this::updateStateComponents,
|
342 |
taskStatusController |
343 |
) { |
344 |
@Override
|
345 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
346 |
try {
|
347 |
JDBCServerExplorerParameters params = manager.createDatabase(dbfile, xmlinfo, taskStatus); |
348 |
if( params == null ) { |
349 |
throw new RuntimeException("Can't create database"); |
350 |
} |
351 |
if( registerConnection ) {
|
352 |
addToConnectionPool(params,repoName); |
353 |
} |
354 |
} catch(Exception ex) { |
355 |
LOGGER.warn("Can't create database",ex);
|
356 |
throw ex;
|
357 |
} |
358 |
} |
359 |
|
360 |
@Override
|
361 |
protected void postTask() { |
362 |
message(null);
|
363 |
} |
364 |
}; |
365 |
|
366 |
this.lastTask.start();
|
367 |
|
368 |
} |
369 |
|
370 |
public void addToConnectionPool(JDBCServerExplorerParameters params, String id) { |
371 |
DataManager dataManager = DALLocator.getDataManager(); |
372 |
dataManager.getDataServerExplorerPool().add(id, params); |
373 |
} |
374 |
|
375 |
private void doAnalizeXML() { |
376 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
377 |
File xmlfile = this.pickerXMLFile.get(); |
378 |
if( xmlfile == null || !xmlfile.exists() ) { |
379 |
return;
|
380 |
} |
381 |
IProjection proj = this.pickerProjection.get();
|
382 |
Charset charset = this.pickerCharset.get(); |
383 |
|
384 |
this.lastTask = new Task( |
385 |
"Xml2db_AnalizeXML",
|
386 |
"Analize XML",
|
387 |
this::updateStateComponents,
|
388 |
taskStatusController |
389 |
) { |
390 |
@Override
|
391 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
392 |
try {
|
393 |
XMLInfo xmlinfo = manager.extractStructure(xmlfile, charset, proj, taskStatus); |
394 |
setTablesModel(xmlinfo); |
395 |
if( proj==null && xmlinfo.getSrid()!=null ) { |
396 |
pickerProjection.set(xmlinfo.getSrid()); |
397 |
} |
398 |
if( charset==null ) { |
399 |
pickerCharset.set(xmlinfo.getCharset()); |
400 |
} |
401 |
} catch(Exception ex) { |
402 |
LOGGER.warn("Can't analize xml",ex);
|
403 |
setTablesModel(null);
|
404 |
throw ex;
|
405 |
} |
406 |
} |
407 |
}; |
408 |
this.lastTask.start();
|
409 |
} |
410 |
|
411 |
private void setTablesModel(XMLInfo xmlinfo) { |
412 |
if( !SwingUtilities.isEventDispatchThread() ) { |
413 |
SwingUtilities.invokeLater(() -> {this.setTablesModel(xmlinfo);}); |
414 |
return;
|
415 |
} |
416 |
this.xmlinfo = xmlinfo;
|
417 |
if( xmlinfo == null ) { |
418 |
lstTables.setModel(new XMLInfoListModel());
|
419 |
return;
|
420 |
} |
421 |
lstTables.setModel(new XMLInfoListModel(xmlinfo));
|
422 |
} |
423 |
|
424 |
private void doViewTableStructure() { |
425 |
LabeledValue<XMLTableInfo> x = (LabeledValue<XMLTableInfo>)this.lstTables.getSelectedValue();
|
426 |
if( x == null ) { |
427 |
return;
|
428 |
} |
429 |
// I18nManager i18n = ToolsLocator.getI18nManager();
|
430 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
431 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
432 |
|
433 |
XMLTableInfo tableInfo = x.getValue(); |
434 |
EditableFeatureType featureType = tableInfo.getFeatureType(); |
435 |
|
436 |
FeatureTypePanel panel = dataSwingManager.createFeatureTypePanel(); |
437 |
panel.put(featureType); |
438 |
Dialog theDialog = windowManager.createDialog(
|
439 |
panel.asJComponent(), |
440 |
ToolsSwingUtils.makeTitle("_Xml2db_Column_manager", tableInfo.getName(), featureType.getLabel()),
|
441 |
null,
|
442 |
WindowManager_v2.BUTTONS_OK_CANCEL |
443 |
); |
444 |
theDialog.addActionListener((ActionEvent e) -> {
|
445 |
if( theDialog.getAction()==WindowManager_v2.BUTTON_OK) {
|
446 |
panel.fetch(x.getValue().getFeatureType()); |
447 |
} |
448 |
}); |
449 |
theDialog.show( |
450 |
WindowManager.MODE.WINDOW, |
451 |
Collections.singletonMap("align", GridBagConstraints.CENTER) |
452 |
); |
453 |
} |
454 |
|
455 |
private void doChangeXMLfile() { |
456 |
File xmlfile = this.pickerXMLFile.get(); |
457 |
if( xmlfile != null ) { |
458 |
File dbfile = this.pickerDbfile.get(); |
459 |
if( dbfile == null ) { |
460 |
|
461 |
} |
462 |
} else {
|
463 |
updateStateComponents(); |
464 |
} |
465 |
|
466 |
} |
467 |
|
468 |
private void doChangeDBfile() { |
469 |
File dbfile = this.pickerDbfile.get(); |
470 |
if( dbfile != null ) { |
471 |
String repoName = this.txtRepositoryName.getText(); |
472 |
if( StringUtils.isBlank(repoName) ) {
|
473 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(dbfile.getName()));
|
474 |
} |
475 |
} |
476 |
updateStateComponents(); |
477 |
} |
478 |
|
479 |
private void message(String msg) { |
480 |
message(msg,JOptionPane.INFORMATION_MESSAGE);
|
481 |
} |
482 |
|
483 |
private void message(String msg, int type) { |
484 |
if( StringUtils.isBlank(msg) ) {
|
485 |
this.lblStatusMsg.setText(""); |
486 |
return;
|
487 |
} |
488 |
if( type == JOptionPane.WARNING_MESSAGE ) { |
489 |
msg = Xml2dbSwingCommons.getHTMLColorTag(Color.RED.darker(), msg);
|
490 |
} |
491 |
this.lblStatusMsg.setText(msg);
|
492 |
this.lblStatusMsg.setVisible(true); |
493 |
} |
494 |
|
495 |
} |