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 / importxml2db / ImportXML2dbPanel.java @ 47472
History | View | Annotate | Download (10.8 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.importxml2db; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.io.File; |
28 |
import java.nio.charset.Charset; |
29 |
import javax.swing.ImageIcon; |
30 |
import javax.swing.JComponent; |
31 |
import javax.swing.JOptionPane; |
32 |
import javax.swing.SwingUtilities; |
33 |
import javax.swing.event.ChangeEvent; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
36 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
37 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
38 |
import org.gvsig.tools.ToolsLibrary; |
39 |
import org.gvsig.tools.ToolsLocator; |
40 |
import org.gvsig.tools.i18n.I18nManager; |
41 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
42 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
44 |
import org.gvsig.tools.swing.api.pickercontroller.CharsetPickerController; |
45 |
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController; |
46 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
47 |
import org.gvsig.tools.swing.api.task.TaskStatusController; |
48 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
49 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
50 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
51 |
import org.gvsig.tools.task.SimpleTaskStatus; |
52 |
import org.gvsig.xml2db.lib.api.Xml2dbLocator; |
53 |
import org.gvsig.xml2db.lib.api.Xml2dbManager; |
54 |
import org.gvsig.xml2db.swing.Xml2dbPanel; |
55 |
import org.gvsig.xml2db.swing.impl.Xml2dbSwingCommons; |
56 |
import org.gvsig.xml2db.swing.impl.createdbfromxml.CreatedbFromXmlPanel; |
57 |
import org.gvsig.xml2db.swing.impl.Task; |
58 |
import org.slf4j.Logger; |
59 |
import org.slf4j.LoggerFactory; |
60 |
|
61 |
/**
|
62 |
*
|
63 |
* @author jjdelcerro
|
64 |
*/
|
65 |
public class ImportXML2dbPanel |
66 |
extends ImportXML2dbPanelView
|
67 |
implements Xml2dbPanel
|
68 |
{ |
69 |
protected static final Logger LOGGER = LoggerFactory.getLogger(CreatedbFromXmlPanel.class); |
70 |
|
71 |
|
72 |
private Dialog dialog; |
73 |
private FilePickerController pickerXMLFile;
|
74 |
private PickerController<JDBCServerExplorerParameters> pickerConnection;
|
75 |
private TaskStatusController taskStatusController;
|
76 |
private Task lastTask;
|
77 |
private CharsetPickerController pickerCharset;
|
78 |
|
79 |
public ImportXML2dbPanel() {
|
80 |
this.createComponents();
|
81 |
} |
82 |
|
83 |
private void createComponents() { |
84 |
this.lastTask = Task.FAKE_TASK;
|
85 |
this.initComponents();
|
86 |
} |
87 |
|
88 |
private void initComponents() { |
89 |
|
90 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
91 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
92 |
|
93 |
this.taskStatusController = ToolsSwingLocator.getTaskStatusSwingManager().createTaskStatusController(
|
94 |
lblStatusCaption, |
95 |
lblStatusMsg, |
96 |
pbStatus |
97 |
); |
98 |
this.taskStatusController.setVisible(false); |
99 |
|
100 |
this.translate();
|
101 |
|
102 |
this.pickerXMLFile = toolsSwingManager.createFilePickerController(
|
103 |
this.txtXMLFile,
|
104 |
this.btnXMLFile,
|
105 |
null,
|
106 |
"Xml2db_xmlFile",
|
107 |
null,
|
108 |
true
|
109 |
|
110 |
); |
111 |
this.pickerXMLFile.addChangeListener((ChangeEvent e) -> { |
112 |
updateStateComponents(); |
113 |
}); |
114 |
|
115 |
this.pickerCharset = toolsSwingManager.createCharsetPickerController(
|
116 |
null,
|
117 |
this.cboCharset
|
118 |
); |
119 |
this.pickerCharset.set(null); |
120 |
|
121 |
this.btnDetectCharset.addActionListener((ActionEvent e) -> { |
122 |
doDetectCharset(); |
123 |
}); |
124 |
|
125 |
this.pickerConnection = dataSwingManager.createJDBCConnectionPickerController(
|
126 |
this.cboConnection,
|
127 |
this.btnConnection
|
128 |
); |
129 |
this.pickerConnection.addChangeListener((ChangeEvent e) -> { |
130 |
updateStateComponents(); |
131 |
}); |
132 |
this.chkConnectToRepository.setSelected(true); |
133 |
|
134 |
toolsSwingManager.addClearButton(this.txtPrefixesForTables);
|
135 |
|
136 |
ToolsSwingUtils.ensureRowsCols(this, 8, 80, 10, 100); |
137 |
} |
138 |
|
139 |
private void translate() { |
140 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
141 |
|
142 |
toolsSwingManager.translate(lblDatabase); |
143 |
toolsSwingManager.translate(lblFileXML); |
144 |
toolsSwingManager.translate(lblCharset); |
145 |
toolsSwingManager.translate(lblConnectToRepository); |
146 |
toolsSwingManager.translate(btnDetectCharset); |
147 |
toolsSwingManager.translate(lblPrefixesForTables); |
148 |
} |
149 |
|
150 |
private void updateStateComponents() { |
151 |
if( !SwingUtilities.isEventDispatchThread() ) { |
152 |
SwingUtilities.invokeLater(this::updateStateComponents); |
153 |
return;
|
154 |
} |
155 |
message(null);
|
156 |
if( this.isProcessing() ) { |
157 |
this.pickerXMLFile.setEnabled(false); |
158 |
this.pickerCharset.setEnabled(false); |
159 |
this.pickerConnection.setEnabled(false); |
160 |
this.btnDetectCharset.setEnabled(false); |
161 |
if( this.dialog!=null ) { |
162 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
163 |
} |
164 |
return;
|
165 |
} |
166 |
this.pickerXMLFile.setEnabled(true); |
167 |
this.pickerCharset.setEnabled(true); |
168 |
this.pickerConnection.setEnabled(true); |
169 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
170 |
File xmlfile = this.pickerXMLFile.get(); |
171 |
if( xmlfile == null || !xmlfile.exists() ) { |
172 |
this.btnDetectCharset.setEnabled(false); |
173 |
if( xmlfile==null ) { |
174 |
this.message(i18n.getTranslation("_XML_file_is_required"), JOptionPane.WARNING_MESSAGE); |
175 |
} else {
|
176 |
this.message(i18n.getTranslation("_XML_file_not_exists"), JOptionPane.WARNING_MESSAGE); |
177 |
} |
178 |
if( this.dialog!=null ) { |
179 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
180 |
} |
181 |
return;
|
182 |
} |
183 |
this.btnDetectCharset.setEnabled(true); |
184 |
JDBCServerExplorerParameters conn = this.pickerConnection.get();
|
185 |
if( conn==null ) { |
186 |
this.message(i18n.getTranslation("_A_database_connection_is_required"), JOptionPane.WARNING_MESSAGE); |
187 |
if( this.dialog!=null ) { |
188 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, false); |
189 |
} |
190 |
return;
|
191 |
} |
192 |
if( this.dialog!=null ) { |
193 |
this.dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, true); |
194 |
} |
195 |
this.taskStatusController.setVisible(this.lastTask.needToShowTheStatus()); |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public void setDialog(Dialog dialog) { |
200 |
this.dialog = dialog;
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public JComponent asJComponent() { |
205 |
return this; |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public void performAction() { |
210 |
doImportXML(); |
211 |
} |
212 |
|
213 |
private void doImportXML() { |
214 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
215 |
File xmlfile = this.pickerXMLFile.get(); |
216 |
if( xmlfile == null || !xmlfile.exists() ) { |
217 |
return;
|
218 |
} |
219 |
JDBCServerExplorerParameters dbparams = this.pickerConnection.get();
|
220 |
if( dbparams==null ) { |
221 |
return;
|
222 |
} |
223 |
Charset charset = this.pickerCharset.get(); |
224 |
boolean connectToRepository = this.chkConnectToRepository.isSelected(); |
225 |
|
226 |
String tablesPrefix = StringUtils.defaultIfBlank(this.txtPrefixesForTables.getText(), null); |
227 |
|
228 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
229 |
|
230 |
this.lastTask = new Task( |
231 |
"Xml2db_copyXmml2Db",
|
232 |
i18n.getTranslation("_Copying_XML_data"),
|
233 |
this::updateStateComponents,
|
234 |
taskStatusController |
235 |
) { |
236 |
@Override
|
237 |
protected void task(SimpleTaskStatus taskStatus) throws Throwable{ |
238 |
try {
|
239 |
manager.copyXml2Db(xmlfile, charset, dbparams, tablesPrefix, taskStatus); |
240 |
if( connectToRepository) {
|
241 |
DataSwingManager dataSwingManager = DALSwingLocator.getDataSwingManager(); |
242 |
dataSwingManager.connectToDatabaseWorkspace(dbparams); |
243 |
} |
244 |
taskStatus.terminate(); |
245 |
} catch(Exception ex) { |
246 |
LOGGER.warn("Can't import xml",ex);
|
247 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
248 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
249 |
dialogs.messageDialog( |
250 |
i18n.getTranslation("_Problems_processing_XML_file")
|
251 |
+"\n\n"
|
252 |
+ ex.getLocalizedMessage(), |
253 |
i18n.getTranslation("_Xml2db_Import_xml_data_to_a_existing_database"),
|
254 |
JOptionPane.WARNING_MESSAGE
|
255 |
); |
256 |
throw ex;
|
257 |
} |
258 |
} |
259 |
}; |
260 |
this.lastTask.start();
|
261 |
} |
262 |
|
263 |
private void message(String msg) { |
264 |
message(msg,JOptionPane.INFORMATION_MESSAGE);
|
265 |
} |
266 |
|
267 |
private void message(String msg, int type) { |
268 |
if( StringUtils.isBlank(msg) ) {
|
269 |
this.lblStatusMsg.setText(""); |
270 |
return;
|
271 |
} |
272 |
if( type == JOptionPane.WARNING_MESSAGE ) { |
273 |
msg = Xml2dbSwingCommons.getHTMLColorTag(Color.RED.darker(), msg);
|
274 |
} |
275 |
this.lblStatusMsg.setText(msg);
|
276 |
this.lblStatusMsg.setVisible(true); |
277 |
} |
278 |
|
279 |
private boolean isProcessing() { |
280 |
return this.lastTask.isProcessing(); |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public ImageIcon loadImage(String imageName) { |
285 |
return ToolsSwingUtils.loadImage(this, imageName); |
286 |
} |
287 |
|
288 |
private void doDetectCharset() { |
289 |
Xml2dbManager manager = Xml2dbLocator.getXml2dbManager(); |
290 |
this.pickerCharset.set(manager.detectCharset(this.pickerXMLFile.get())); |
291 |
} |
292 |
} |