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 @ 47607
History | View | Annotate | Download (25.3 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 java.awt.Color; |
26 |
import java.awt.Cursor; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ItemEvent; |
30 |
import java.io.File; |
31 |
import java.nio.charset.Charset; |
32 |
import java.util.ArrayList; |
33 |
import java.util.Collections; |
34 |
import java.util.List; |
35 |
import java.util.Locale; |
36 |
import javax.swing.AbstractListModel; |
37 |
import javax.swing.DefaultComboBoxModel; |
38 |
import javax.swing.JComponent; |
39 |
import javax.swing.JOptionPane; |
40 |
import javax.swing.SwingUtilities; |
41 |
import javax.swing.event.ChangeEvent; |
42 |
import javax.swing.event.DocumentEvent; |
43 |
import javax.swing.event.DocumentListener; |
44 |
import org.apache.commons.io.FilenameUtils; |
45 |
import org.apache.commons.lang3.StringUtils; |
46 |
import org.cresques.cts.IProjection; |
47 |
import org.gvsig.fmap.dal.DALLocator; |
48 |
import org.gvsig.fmap.dal.DataFactory; |
49 |
import org.gvsig.fmap.dal.DataManager; |
50 |
import org.gvsig.fmap.dal.PersonalDatabaseServerExplorerFactory; |
51 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
52 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
53 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
54 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
55 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
56 |
import org.gvsig.fmap.dal.swing.ProjectionPickerController; |
57 |
import org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel; |
58 |
import static org.gvsig.fmap.dal.swing.featuretype.FeatureTypePanel.MODE_EDIT_ALL; |
59 |
import org.gvsig.tools.ToolsLocator; |
60 |
import org.gvsig.tools.i18n.I18nManager; |
61 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
62 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
63 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
64 |
import org.gvsig.tools.swing.api.pickercontroller.CharsetPickerController; |
65 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
66 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
67 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
68 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
69 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
70 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
71 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
72 |
import org.gvsig.tools.task.SimpleTaskStatus; |
73 |
import org.gvsig.tools.util.LabeledValue; |
74 |
import org.gvsig.tools.util.LabeledValueImpl; |
75 |
import org.gvsig.xml2db.lib.api.Xml2dbLocator; |
76 |
import org.gvsig.xml2db.lib.api.Xml2dbManager; |
77 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLInfo; |
78 |
import org.gvsig.xml2db.lib.api.xmlinfo.XMLTableInfo; |
79 |
import org.gvsig.xml2db.swing.Xml2dbPanel; |
80 |
import org.gvsig.xml2db.swing.impl.Task; |
81 |
import org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons; |
82 |
import static org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons.getPersonalDatabaseServerExplorersComboModel; |
83 |
import org.slf4j.Logger; |
84 |
import org.slf4j.LoggerFactory; |
85 |
|
86 |
/**
|
87 |
*
|
88 |
* @author jjdelcerro
|
89 |
*/
|
90 |
@SuppressWarnings("UseSpecificCatch") |
91 |
public class CreatedbFromXmlPanel |
92 |
extends CreatedbFromXmlPanelView
|
93 |
implements Xml2dbPanel
|
94 |
{ |
95 |
|
96 |
protected static final Logger LOGGER = LoggerFactory.getLogger(CreatedbFromXmlPanel.class); |
97 |
|
98 |
private static final int MODE_NEW = 0; |
99 |
private static final int MODE_OVERWRITE = 1; |
100 |
private static final int MODE_ADD = 2; |
101 |
|
102 |
private PickerController<Locale> pickerLocale; |
103 |
|
104 |
private static class XMLInfoListModel extends AbstractListModel<LabeledValue<XMLTableInfo>> { |
105 |
|
106 |
private final XMLInfo xmlinfo; |
107 |
private final List<LabeledValue<XMLTableInfo>> tables; |
108 |
|
109 |
|
110 |
public XMLInfoListModel() {
|
111 |
this(null); |
112 |
} |
113 |
|
114 |
public XMLInfoListModel(XMLInfo xmlinfo) {
|
115 |
this.xmlinfo = xmlinfo;
|
116 |
if( xmlinfo != null ) { |
117 |
this.tables = new ArrayList<>(xmlinfo.size()); |
118 |
for (XMLTableInfo tableInfo : xmlinfo) {
|
119 |
this.tables.add(new LabeledValueImpl<>(xmlinfo.getNameWithPrefix(tableInfo.getName()),tableInfo)); |
120 |
} |
121 |
} else {
|
122 |
this.tables = null; |
123 |
} |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public LabeledValue<XMLTableInfo> getElementAt(int index) { |
128 |
if( this.tables == null ) { |
129 |
return null; |
130 |
} |
131 |
return tables.get(index);
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public int getSize() { |
136 |
if( this.tables == null ) { |
137 |
return 0; |
138 |
} |
139 |
return this.tables.size(); |
140 |
} |
141 |
|
142 |
|
143 |
} |
144 |
|
145 |
|
146 |
private Dialog dialog; |
147 |
private FilePickerController pickerXMLFile;
|
148 |
private FilePickerController pickerDbfile;
|
149 |
private ProjectionPickerController pickerProjection;
|
150 |
private CharsetPickerController pickerCharset;
|
151 |
private TaskStatusController taskStatusController;
|
152 |
private Task lastTask;
|
153 |
private XMLInfo xmlinfo;
|
154 |
|
155 |
public CreatedbFromXmlPanel() {
|
156 |
this.createComponents();
|
157 |
} |
158 |
|
159 |
private void createComponents() { |
160 |
this.lastTask = Task.FAKE_TASK;
|
161 |
this.xmlinfo = null; |
162 |
this.initComponents();
|
163 |
} |
164 |
|
165 |
private void initComponents() { |
166 |
|
167 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
168 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
169 |
|
170 |
this.taskStatusController = ToolsSwingLocator.getTaskStatusSwingManager().createTaskStatusController(
|
171 |
lblStatusCaption, |
172 |
lblStatusMsg, |
173 |
pbStatus |
174 |
); |
175 |
this.taskStatusController.setVisible(false); |
176 |
|
177 |
this.translate();
|
178 |
|
179 |
this.setTablesModel(null); |
180 |
|
181 |
this.pickerXMLFile = toolsSwingManager.createFilePickerController(
|
182 |
this.txtXMLFile,
|
183 |
this.btnXMLFile,
|
184 |
null,
|
185 |
"Xml2db_xmlFile",
|
186 |
null,
|
187 |
true
|
188 |
|
189 |
); |
190 |
this.pickerXMLFile.addChangeListener((ChangeEvent e) -> { |
191 |
doChangeXMLfile(); |
192 |
}); |
193 |
|
194 |
this.pickerCharset = toolsSwingManager.createCharsetPickerController(
|
195 |
null,
|
196 |
this.cboCharset
|
197 |
); |
198 |
|
199 |
this.pickerProjection = dataSwingManager.createProjectionPickerController(
|
200 |
this.txtProjection,
|
201 |
this.btnProjection
|
202 |
); |
203 |
this.pickerProjection.addChangeListener((ChangeEvent e) -> { |
204 |
setTablesModel(null);
|
205 |
}); |
206 |
|
207 |
this.txtPrefixesForTables.getDocument().addDocumentListener(new DocumentListener() { |
208 |
@Override
|
209 |
public void insertUpdate(DocumentEvent e) { |
210 |
setTablesModel(null);
|
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void removeUpdate(DocumentEvent e) { |
215 |
setTablesModel(null);
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void changedUpdate(DocumentEvent e) { |
220 |
setTablesModel(null);
|
221 |
} |
222 |
}); |
223 |
|
224 |
this.cboDatabaseType.setModel(getPersonalDatabaseServerExplorersComboModel());
|
225 |
|
226 |
this.pickerDbfile = toolsSwingManager.createFilePickerController(
|
227 |
this.txtDatabaseFile,
|
228 |
this.btnDataBaseFile
|
229 |
); |
230 |
this.pickerDbfile.addChangeListener((ChangeEvent e) -> { |
231 |
doChangeDBfile(); |
232 |
}); |
233 |
|
234 |
this.btnAnalizeXML.addActionListener((ActionEvent e) -> { |
235 |
doAnalizeXML(); |
236 |
}); |
237 |
this.btnAnalizeXML.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
238 |
|
239 |
this.btnViewTableStructure.addActionListener((ActionEvent e) -> { |
240 |
doViewTableStructure(); |
241 |
}); |
242 |
this.btnViewTableStructure.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
243 |
|
244 |
this.pickerCharset.set(Charset.defaultCharset()); |
245 |
|
246 |
this.chkRegisterConnection.setSelected(true); |
247 |
|
248 |
this.cboMode.addItemListener((ItemEvent e) -> { |
249 |
if(getMode() == MODE_ADD){
|
250 |
chkRegisterConnection.setSelected(false);
|
251 |
} |
252 |
updateStateComponents(); |
253 |
}); |
254 |
|
255 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
256 |
DefaultComboBoxModel<LabeledValue<Integer>> model = new DefaultComboBoxModel<>(); |
257 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("_New_database"), MODE_NEW)); |
258 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("_Overwrite_database"), MODE_OVERWRITE)); |
259 |
model.addElement(new LabeledValueImpl<>(i18n.getTranslation("_Add_to_existing_database"), MODE_ADD)); |
260 |
this.cboMode.setModel(model);
|
261 |
this.cboMode.setSelectedIndex(0); |
262 |
|
263 |
|
264 |
this.cboCharset.setSelectedIndex(-1); |
265 |
|
266 |
this.pickerLocale = toolsSwingManager.createLocalePickerController(this.cboLocale); |
267 |
this.pickerLocale.set(Locale.getDefault()); |
268 |
this.pickerLocale.addChangeListener((ChangeEvent e) -> { |
269 |
setTablesModel(null);
|
270 |
}); |
271 |
|
272 |
toolsSwingManager.addClearButton(this.txtPrefixesForTables);
|
273 |
|
274 |
SwingUtilities.invokeLater(() -> { updateStateComponents(); });
|
275 |
|
276 |
ToolsSwingUtils.ensureRowsCols(this, 5, 80, 6, 100); |
277 |
} |
278 |
|
279 |
private void translate() { |
280 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
281 |
|
282 |
toolsSwingManager.translate(this.lblCharset);
|
283 |
toolsSwingManager.translate(this.lblRepositoryName);
|
284 |
toolsSwingManager.translate(this.lblCreatedb);
|
285 |
toolsSwingManager.translate(this.lblFileXML);
|
286 |
toolsSwingManager.translate(this.lblDatabaseFile);
|
287 |
toolsSwingManager.translate(this.lblProjection);
|
288 |
toolsSwingManager.translate(this.lblTables);
|
289 |
toolsSwingManager.translate(this.btnAnalizeXML);
|
290 |
toolsSwingManager.translate(this.btnViewTableStructure);
|
291 |
toolsSwingManager.translate(this.lblRegisterConnection);
|
292 |
toolsSwingManager.translate(this.lblMode);
|
293 |
toolsSwingManager.translate(this.lblLocale);
|
294 |
toolsSwingManager.translate(this.lblPrefixesForTables);
|
295 |
toolsSwingManager.translate(this.lblDatabaseType);
|
296 |
|
297 |
} |
298 |
|
299 |
private boolean isProcessing() { |
300 |
return this.lastTask.isProcessing(); |
301 |
} |
302 |
|
303 |
private void updateStateComponents() { |
304 |
if( !SwingUtilities.isEventDispatchThread() ) { |
305 |
SwingUtilities.invokeLater(this::updateStateComponents); |
306 |
return;
|
307 |
} |
308 |
message(null);
|
309 |
if( this.isProcessing() ) { |
310 |
this.pickerXMLFile.setEnabled(false); |
311 |
this.pickerCharset.setEnabled(false); |
312 |
this.pickerProjection.setEnabled(false); |
313 |
this.btnAnalizeXML.setEnabled(false); |
314 |
this.lstTables.setEnabled(false); |
315 |
this.btnViewTableStructure.setEnabled(false); |
316 |
if( this.dialog!=null ) { |
317 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
318 |
} |
319 |
return;
|
320 |
} |
321 |
PersonalDatabaseServerExplorerFactory explorerFactory = this.getExplorerFactory();
|
322 |
|
323 |
this.pickerXMLFile.setEnabled(true); |
324 |
this.pickerCharset.setEnabled(true); |
325 |
this.pickerProjection.setEnabled(true); |
326 |
|
327 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
328 |
File xmlfile = this.pickerXMLFile.get(); |
329 |
if( xmlfile == null || !xmlfile.exists() ) { |
330 |
if( xmlfile==null ) { |
331 |
this.message(i18n.getTranslation("_XML_file_is_required"), JOptionPane.WARNING_MESSAGE); |
332 |
} else {
|
333 |
this.message(i18n.getTranslation("_XML_file_not_exists"), JOptionPane.WARNING_MESSAGE); |
334 |
} |
335 |
this.btnAnalizeXML.setEnabled(false); |
336 |
this.lstTables.setEnabled(false); |
337 |
this.btnViewTableStructure.setEnabled(false); |
338 |
if( this.dialog!=null ) { |
339 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
340 |
} |
341 |
return;
|
342 |
} |
343 |
this.btnAnalizeXML.setEnabled(true); |
344 |
if( this.lstTables.getModel().getSize()<1 ) { |
345 |
if( this.xmlinfo == null ) { |
346 |
this.message(i18n.getTranslation("_Analize_XML_required"), JOptionPane.WARNING_MESSAGE); |
347 |
} else {
|
348 |
this.message(i18n.getTranslation("_No_tables_recognized_in_the_XML"), JOptionPane.WARNING_MESSAGE); |
349 |
} |
350 |
this.btnViewTableStructure.setEnabled(false); |
351 |
if( this.dialog!=null ) { |
352 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
353 |
} |
354 |
return;
|
355 |
} |
356 |
|
357 |
if( this.xmlinfo!=null && this.xmlinfo.hasGeometries() && this.pickerProjection.get()==null ) { |
358 |
this.message(i18n.getTranslation("_Projection_is_required"), JOptionPane.WARNING_MESSAGE); |
359 |
if( this.dialog!=null ) { |
360 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
361 |
} |
362 |
return;
|
363 |
} |
364 |
|
365 |
this.lstTables.setEnabled(true); |
366 |
this.btnViewTableStructure.setEnabled(true); |
367 |
File dbfile = this.pickerDbfile.get(); |
368 |
if( dbfile == null ) { |
369 |
this.message(i18n.getTranslation("_Database_file_is_required"), JOptionPane.WARNING_MESSAGE); |
370 |
if( this.dialog!=null ) { |
371 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
372 |
} |
373 |
return;
|
374 |
} |
375 |
if( explorerFactory.existsdb(dbfile) && this.getMode() == MODE_NEW ) { |
376 |
this.message(i18n.getTranslation("_Database_file_already_exists"), JOptionPane.WARNING_MESSAGE); |
377 |
if( this.dialog!=null ) { |
378 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
379 |
} |
380 |
return;
|
381 |
} |
382 |
if( StringUtils.isBlank(this.txtRepositoryName.getText()) ) { |
383 |
this.message(i18n.getTranslation("_Repository_name_is_required"), JOptionPane.WARNING_MESSAGE); |
384 |
if( this.dialog!=null ) { |
385 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
386 |
} |
387 |
return;
|
388 |
} |
389 |
if( this.dialog!=null ) { |
390 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, true); |
391 |
} |
392 |
this.taskStatusController.setVisible(this.lastTask.needToShowTheStatus()); |
393 |
} |
394 |
|
395 |
@Override
|
396 |
public void setDialog(Dialog dialog) { |
397 |
this.dialog = dialog;
|
398 |
} |
399 |
|
400 |
@Override
|
401 |
public JComponent asJComponent() { |
402 |
return this; |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public void performAction() { |
407 |
doCreatedb(); |
408 |
} |
409 |
|
410 |
private void doCreatedb() { |
411 |
if( this.xmlinfo==null ) { |
412 |
return;
|
413 |
} |
414 |
File dbfile = this.pickerDbfile.get(); |
415 |
if( dbfile==null || dbfile.exists()) { |
416 |
return;
|
417 |
} |
418 |
String s = this.txtRepositoryName.getText(); |
419 |
if( StringUtils.isBlank(s) ) {
|
420 |
s = FilenameUtils.getBaseName(dbfile.getName()); |
421 |
} |
422 |
String repoName = s;
|
423 |
|
424 |
boolean registerConnection = this.chkRegisterConnection.isSelected(); |
425 |
|
426 |
PersonalDatabaseServerExplorerFactory explorerFactory = this.getExplorerFactory();
|
427 |
|
428 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
429 |
|
430 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
431 |
switch(getMode()){
|
432 |
default:
|
433 |
case MODE_NEW:
|
434 |
if( explorerFactory.existsdb(dbfile) ) {
|
435 |
message(i18n.getTranslation("Database already exists"), JOptionPane.WARNING_MESSAGE); |
436 |
return;
|
437 |
} |
438 |
break;
|
439 |
case MODE_OVERWRITE:
|
440 |
break;
|
441 |
case MODE_ADD:
|
442 |
if( !explorerFactory.existsdb(dbfile) ) {
|
443 |
message(i18n.getTranslation("Database not exists"), JOptionPane.WARNING_MESSAGE); |
444 |
return;
|
445 |
} |
446 |
break;
|
447 |
} |
448 |
// if( H2SpatialUtils.existsH2db(dbfile) ) {
|
449 |
// if( !this.chkOverwriteDatabase.isSelected() ) {
|
450 |
// message(i18n.getTranslation("Database already exists"), JOptionPane.WARNING_MESSAGE);
|
451 |
// return;
|
452 |
// }
|
453 |
// }
|
454 |
|
455 |
this.lastTask = new Task( |
456 |
"Xml2db_Creating_database",
|
457 |
i18n.getTranslation("_Create_database"),
|
458 |
this::updateStateComponents,
|
459 |
taskStatusController |
460 |
) { |
461 |
@Override
|
462 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
463 |
try {
|
464 |
JDBCServerExplorerParameters params; |
465 |
switch(getMode()){
|
466 |
default:
|
467 |
case MODE_NEW:
|
468 |
if (explorerFactory.existsdb(dbfile)) {
|
469 |
message(i18n.getTranslation("Database already exists"), JOptionPane.WARNING_MESSAGE); |
470 |
return;
|
471 |
} |
472 |
params = manager.createDatabase(explorerFactory.getName(),dbfile, xmlinfo, taskStatus); |
473 |
break;
|
474 |
case MODE_OVERWRITE:
|
475 |
if( explorerFactory.existsdb(dbfile) ) {
|
476 |
taskStatus.message(i18n.getTranslation("_Removing_existing_database"));
|
477 |
explorerFactory.serverStop();; |
478 |
explorerFactory.removedb(dbfile); |
479 |
explorerFactory.serverStart(); |
480 |
} |
481 |
params = manager.createDatabase(explorerFactory.getName(), dbfile, xmlinfo, taskStatus); |
482 |
break;
|
483 |
case MODE_ADD:
|
484 |
params = manager.addToDatabase(explorerFactory.getName(),dbfile, xmlinfo, taskStatus); |
485 |
break;
|
486 |
} |
487 |
|
488 |
// JDBCServerExplorerParameters params = manager.createDatabase(dbfile, xmlinfo, taskStatus);
|
489 |
if( params == null ) { |
490 |
throw new RuntimeException("Can't create database"); |
491 |
} |
492 |
if( registerConnection ) {
|
493 |
addToConnectionPool(params,repoName); |
494 |
} |
495 |
} catch(Exception ex) { |
496 |
LOGGER.warn("Can't create database",ex);
|
497 |
throw ex;
|
498 |
} |
499 |
} |
500 |
|
501 |
@Override
|
502 |
protected void postTask() { |
503 |
message(null);
|
504 |
} |
505 |
}; |
506 |
|
507 |
this.lastTask.start();
|
508 |
|
509 |
} |
510 |
|
511 |
public void addToConnectionPool(JDBCServerExplorerParameters params, String id) { |
512 |
DataManager dataManager = DALLocator.getDataManager(); |
513 |
dataManager.getDataServerExplorerPool().add(id, params); |
514 |
} |
515 |
|
516 |
private void doAnalizeXML() { |
517 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
518 |
File xmlfile = this.pickerXMLFile.get(); |
519 |
if( xmlfile == null || !xmlfile.exists() ) { |
520 |
return;
|
521 |
} |
522 |
IProjection proj = this.pickerProjection.get();
|
523 |
Charset charset = this.pickerCharset.get(); |
524 |
Locale locale = this.pickerLocale.get(); |
525 |
|
526 |
String tablesPrefix = StringUtils.defaultIfBlank(this.txtPrefixesForTables.getText(), null); |
527 |
|
528 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
529 |
|
530 |
this.lastTask = new Task( |
531 |
"Xml2db_AnalizeXML",
|
532 |
i18n.getTranslation("_Analize_xml"),
|
533 |
this::updateStateComponents,
|
534 |
taskStatusController |
535 |
) { |
536 |
@Override
|
537 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
538 |
try {
|
539 |
XMLInfo xmlinfo = manager.extractStructure(xmlfile, charset, proj, locale, tablesPrefix, taskStatus); |
540 |
if( proj==null && xmlinfo.getSrid()!=null ) { |
541 |
pickerProjection.set(xmlinfo.getSrid()); |
542 |
} |
543 |
if( xmlinfo.getCharset()!=null && charset==null ) { |
544 |
pickerCharset.set(xmlinfo.getCharset()); |
545 |
} |
546 |
setTablesModel(xmlinfo); |
547 |
} catch(Throwable ex) { |
548 |
LOGGER.warn("Can't analize xml",ex);
|
549 |
setTablesModel(null);
|
550 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
551 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
552 |
dialogs.messageDialog( |
553 |
i18n.getTranslation("_Problems_processing_XML_file")
|
554 |
+"\n\n"
|
555 |
+ ex.getLocalizedMessage(), |
556 |
i18n.getTranslation("_Xml2db_Create_database_from_xml"),
|
557 |
JOptionPane.WARNING_MESSAGE
|
558 |
); |
559 |
throw ex;
|
560 |
} |
561 |
} |
562 |
}; |
563 |
this.lastTask.start();
|
564 |
} |
565 |
|
566 |
private void setTablesModel(XMLInfo xmlinfo) { |
567 |
if( !SwingUtilities.isEventDispatchThread() ) { |
568 |
SwingUtilities.invokeLater(() -> {this.setTablesModel(xmlinfo);}); |
569 |
return;
|
570 |
} |
571 |
this.xmlinfo = xmlinfo;
|
572 |
if( xmlinfo == null ) { |
573 |
lstTables.setModel(new XMLInfoListModel());
|
574 |
return;
|
575 |
} |
576 |
lstTables.setModel(new XMLInfoListModel(xmlinfo));
|
577 |
} |
578 |
|
579 |
private void doViewTableStructure() { |
580 |
LabeledValue<XMLTableInfo> x = (LabeledValue<XMLTableInfo>)this.lstTables.getSelectedValue();
|
581 |
if( x == null ) { |
582 |
return;
|
583 |
} |
584 |
// I18nManager i18n = ToolsLocator.getI18nManager();
|
585 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
586 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
587 |
|
588 |
XMLTableInfo tableInfo = x.getValue(); |
589 |
EditableFeatureType featureType = tableInfo.getFeatureType(); |
590 |
|
591 |
FeatureTypePanel panel = dataSwingManager.createFeatureTypePanel(); |
592 |
panel.setMode(MODE_EDIT_ALL); |
593 |
// panel.setRepository(xmlinfo.getRepository());
|
594 |
panel.put(featureType); |
595 |
Dialog theDialog = windowManager.createDialog(
|
596 |
panel.asJComponent(), |
597 |
ToolsSwingUtils.makeTitle("_Xml2db_Column_manager", tableInfo.getName(), featureType.getLabel()),
|
598 |
null,
|
599 |
WindowManager_v2.BUTTONS_OK_CANCEL |
600 |
); |
601 |
theDialog.addActionListener((ActionEvent e) -> {
|
602 |
if( theDialog.getAction()==WindowManager_v2.BUTTON_OK) {
|
603 |
panel.fetch(x.getValue().getFeatureType()); |
604 |
} |
605 |
}); |
606 |
theDialog.show( |
607 |
WindowManager.MODE.WINDOW, |
608 |
Collections.singletonMap("align", GridBagConstraints.CENTER) |
609 |
); |
610 |
} |
611 |
|
612 |
private void doChangeXMLfile() { |
613 |
File xmlfile = this.pickerXMLFile.get(); |
614 |
if( xmlfile != null ) { |
615 |
File dbfile = this.pickerDbfile.get(); |
616 |
if( dbfile == null ) { |
617 |
dbfile = new File(FilenameUtils.removeExtension(xmlfile.getAbsolutePath())); |
618 |
this.pickerDbfile.set(dbfile);
|
619 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(xmlfile.getName()));
|
620 |
} |
621 |
} |
622 |
updateStateComponents(); |
623 |
} |
624 |
|
625 |
private void doChangeDBfile() { |
626 |
File dbfile = this.pickerDbfile.get(); |
627 |
if( dbfile != null ) { |
628 |
String repoName = this.txtRepositoryName.getText(); |
629 |
if( StringUtils.isBlank(repoName) ) {
|
630 |
this.txtRepositoryName.setText(FilenameUtils.getBaseName(dbfile.getName()));
|
631 |
} |
632 |
} |
633 |
updateStateComponents(); |
634 |
} |
635 |
|
636 |
private void message(String msg) { |
637 |
message(msg,JOptionPane.INFORMATION_MESSAGE);
|
638 |
} |
639 |
|
640 |
private void message(String msg, int type) { |
641 |
if( StringUtils.isBlank(msg) ) {
|
642 |
this.lblStatusMsg.setText(""); |
643 |
return;
|
644 |
} |
645 |
if( type == JOptionPane.WARNING_MESSAGE ) { |
646 |
msg = Xml2dbSwingCommons.getHTMLColorTag(Color.RED.darker(), msg);
|
647 |
} |
648 |
this.lblStatusMsg.setText(msg);
|
649 |
this.lblStatusMsg.setVisible(true); |
650 |
} |
651 |
|
652 |
private int getMode() { |
653 |
LabeledValue<Integer> item = (LabeledValue) this.cboMode.getSelectedItem(); |
654 |
if (item == null){ |
655 |
return MODE_NEW;
|
656 |
} |
657 |
return item.getValue();
|
658 |
} |
659 |
|
660 |
private PersonalDatabaseServerExplorerFactory getExplorerFactory() {
|
661 |
return (PersonalDatabaseServerExplorerFactory) this.cboDatabaseType.getSelectedItem(); |
662 |
} |
663 |
} |