svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.impl / src / main / java / org / gvsig / exportto / swing / impl / DefaultJExporttoServicePanel.java @ 41463
History | View | Annotate | Download (12.4 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.exportto.swing.impl; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.net.URL; |
28 |
import java.net.UnknownHostException; |
29 |
import java.util.Iterator; |
30 |
|
31 |
import javax.swing.ImageIcon; |
32 |
import javax.swing.JOptionPane; |
33 |
|
34 |
import jwizardcomponent.DefaultJWizardComponents; |
35 |
|
36 |
import org.cresques.cts.IProjection; |
37 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
import org.gvsig.exportto.ExporttoService; |
41 |
import org.gvsig.exportto.ExporttoServiceException; |
42 |
import org.gvsig.exportto.ExporttoServiceFinishAction; |
43 |
import org.gvsig.exportto.swing.JExporttoServicePanel; |
44 |
import org.gvsig.exportto.swing.JExporttoServicePanelListener; |
45 |
import org.gvsig.exportto.swing.impl.panel.MessagePanel; |
46 |
import org.gvsig.exportto.swing.impl.wizard.DefaultWizardContainer; |
47 |
import org.gvsig.exportto.swing.impl.wizard.ExportFilterWizard; |
48 |
import org.gvsig.exportto.swing.impl.wizard.ExporterSelectionWizard; |
49 |
import org.gvsig.exportto.swing.impl.wizard.ExporttoProgressWizard; |
50 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProvider; |
51 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderFactory; |
52 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderLocator; |
53 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderManager; |
54 |
import org.gvsig.fmap.dal.DALLocator; |
55 |
import org.gvsig.fmap.dal.DataManager; |
56 |
import org.gvsig.fmap.dal.exception.DataException; |
57 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
58 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
59 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
60 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
61 |
import org.gvsig.gui.beans.wizard.WizardPanel; |
62 |
import org.gvsig.gui.beans.wizard.WizardPanelActionListener; |
63 |
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo; |
64 |
import org.gvsig.i18n.Messages; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.evaluator.Evaluator; |
67 |
import org.gvsig.tools.exception.IBaseException; |
68 |
import org.gvsig.tools.i18n.I18nManager; |
69 |
import org.gvsig.tools.service.ServiceException; |
70 |
import org.gvsig.tools.task.SimpleTaskStatus; |
71 |
import org.gvsig.tools.task.TaskStatus; |
72 |
|
73 |
/**
|
74 |
* Default implementation for the {@link JExporttoServicePanel}.
|
75 |
*
|
76 |
* @author gvSIG Team
|
77 |
* @version $Id$
|
78 |
*/
|
79 |
public class DefaultJExporttoServicePanel extends JExporttoServicePanel |
80 |
implements WizardPanel {
|
81 |
|
82 |
private static final Logger LOG = LoggerFactory |
83 |
.getLogger(DefaultJExporttoServicePanel.class); |
84 |
|
85 |
private static final ExporttoSwingProviderManager EXPORTTO_SWING_PROVIDER_MANAGER = |
86 |
ExporttoSwingProviderLocator.getManager(); |
87 |
private static final DataManager DATA_MANAGER = DALLocator.getDataManager(); |
88 |
|
89 |
private static final long serialVersionUID = -7026467582252285238L; |
90 |
|
91 |
private FeatureStore featureStore;
|
92 |
private IProjection projection;
|
93 |
private FLyrVect vectorLayer = null; |
94 |
private DefaultExporttoSwingManager uimanager;
|
95 |
|
96 |
// Used to get the new feature store parameters
|
97 |
private ExporttoSwingProvider exporttoSwingProvider;
|
98 |
|
99 |
// Provider types that will be displayed
|
100 |
private int[] providerTypes; |
101 |
|
102 |
// Wizards used to create the main wizard
|
103 |
private ExporterSelectionWizard exporterSelectionWizard = null; |
104 |
private ExportFilterWizard exportFilterWizard = null; |
105 |
private ExporttoProgressWizard exporttoProgressWizard = null; |
106 |
|
107 |
// Listener for the finish and cancell button
|
108 |
private WizardPanelActionListener exporttoServicePanelListener;
|
109 |
|
110 |
private WizardPanelWithLogo wizardPanelWithLogo = null; |
111 |
|
112 |
// Action to execute at the end of the export process
|
113 |
ExporttoServiceFinishAction exporttoServiceFinishAction; |
114 |
|
115 |
private int status; |
116 |
|
117 |
public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager,
|
118 |
FLyrVect vlayer, |
119 |
ExporttoServiceFinishAction exporttoServiceFinishAction, |
120 |
int[] providerTypes) { |
121 |
|
122 |
this(
|
123 |
uimanager, |
124 |
vlayer.getFeatureStore(), |
125 |
vlayer.getProjection(), |
126 |
exporttoServiceFinishAction, |
127 |
providerTypes); |
128 |
vectorLayer = vlayer; |
129 |
} |
130 |
|
131 |
public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager,
|
132 |
FeatureStore featureStore, IProjection projection, |
133 |
ExporttoServiceFinishAction exporttoServiceFinishAction, |
134 |
int[] providerTypes) { |
135 |
this.featureStore = featureStore;
|
136 |
this.projection = projection;
|
137 |
this.uimanager = uimanager;
|
138 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
139 |
this.providerTypes = providerTypes;
|
140 |
this.status = JOptionPane.UNDEFINED_CONDITION; |
141 |
|
142 |
ImageIcon ii = IconThemeHelper.getImageIcon("wizard-export-to"); |
143 |
wizardPanelWithLogo = new WizardPanelWithLogo(ii);
|
144 |
|
145 |
// Initialize the wizards
|
146 |
exporterSelectionWizard = |
147 |
new ExporterSelectionWizard(this, providerTypes); |
148 |
exportFilterWizard = new ExportFilterWizard(this); |
149 |
exporttoProgressWizard = new ExporttoProgressWizard(this); |
150 |
|
151 |
// Adding the wizards to the main wizard
|
152 |
wizardPanelWithLogo.addOptionPanel(exporterSelectionWizard); |
153 |
|
154 |
// The finish button is called "export"
|
155 |
this.wizardPanelWithLogo.getWizardComponents().getFinishButton()
|
156 |
.setText(uimanager.getTranslation("export"));
|
157 |
|
158 |
this.setLayout(new BorderLayout()); |
159 |
this.add(wizardPanelWithLogo, BorderLayout.CENTER); |
160 |
|
161 |
// Disable the nextbutton
|
162 |
setNextButtonEnabled(false);
|
163 |
setExportButtonEnabled(false);
|
164 |
|
165 |
// Set the listener for the finish and cancell events
|
166 |
this.wizardPanelWithLogo.setWizardListener(this); |
167 |
} |
168 |
|
169 |
public void setNextButtonEnabled(boolean isEnabled) { |
170 |
wizardPanelWithLogo.setNextButtonEnabled(isEnabled); |
171 |
} |
172 |
|
173 |
public void setExportButtonEnabled(boolean isEnabled) { |
174 |
wizardPanelWithLogo.setFinishButtonEnabled(isEnabled); |
175 |
} |
176 |
|
177 |
public void setCancelButtonText(String text) { |
178 |
wizardPanelWithLogo.getWizardComponents().getCancelButton() |
179 |
.setText(text); |
180 |
} |
181 |
|
182 |
/**
|
183 |
* @return the uimanager
|
184 |
*/
|
185 |
public DefaultExporttoSwingManager getExporttoSwingManager() {
|
186 |
return uimanager;
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* @param exporttoSwingProvider
|
191 |
* @throws ServiceException
|
192 |
*/
|
193 |
public void selectExporttoSwingProvider( |
194 |
ExporttoSwingProviderFactory provider) |
195 |
throws ServiceException {
|
196 |
|
197 |
if (vectorLayer == null) { |
198 |
exporttoSwingProvider = |
199 |
EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider( |
200 |
provider.getName(), featureStore, projection); |
201 |
} else {
|
202 |
exporttoSwingProvider = |
203 |
EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider( |
204 |
provider.getName(), vectorLayer); |
205 |
} |
206 |
|
207 |
DefaultJWizardComponents wizardComponents = |
208 |
wizardPanelWithLogo.getWizardComponents(); |
209 |
for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) { |
210 |
wizardComponents.removeWizardPanel(i); |
211 |
} |
212 |
for (int i = 0; i < exporttoSwingProvider.getPanelCount(); i++) { |
213 |
wizardPanelWithLogo.addOptionPanel(new DefaultWizardContainer(
|
214 |
exporttoSwingProvider.getPanelAt(i))); |
215 |
} |
216 |
wizardPanelWithLogo.addOptionPanel(exportFilterWizard); |
217 |
wizardPanelWithLogo.addOptionPanel(exporttoProgressWizard); |
218 |
} |
219 |
|
220 |
public void export() throws DataException, ExporttoServiceException { |
221 |
this.lastWizard();
|
222 |
|
223 |
// Create the feature Set...
|
224 |
FeatureSet featureSet = null;
|
225 |
if (exportFilterWizard.isFullLayerSelected()) {
|
226 |
featureSet = featureStore.getFeatureSet(); |
227 |
} else
|
228 |
if (exportFilterWizard.isSelectedFeaturesSelected()) {
|
229 |
featureSet = (FeatureSet) featureStore.getSelection(); |
230 |
} else {
|
231 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
232 |
Evaluator evaluator = |
233 |
DATA_MANAGER.createExpresion(exportFilterWizard |
234 |
.getFreeFilter()); |
235 |
featureQuery.setFilter(evaluator); |
236 |
featureSet = featureStore.getFeatureSet(featureQuery); |
237 |
} |
238 |
|
239 |
Export export = new Export(featureSet); |
240 |
export.start();
|
241 |
} |
242 |
|
243 |
public void lastWizard() { |
244 |
this.wizardPanelWithLogo.getWizardComponents().getNextButton()
|
245 |
.getActionListeners()[0].actionPerformed(null); |
246 |
} |
247 |
|
248 |
private class Export extends Thread { |
249 |
|
250 |
private FeatureSet featureSet;
|
251 |
|
252 |
public Export(FeatureSet featureSet) {
|
253 |
super();
|
254 |
this.featureSet = featureSet;
|
255 |
} |
256 |
|
257 |
public synchronized void run() { |
258 |
ExporttoService exportService = |
259 |
exporttoSwingProvider.createExporttoService(); |
260 |
exporttoProgressWizard.bind(exportService.getTaskStatus()); |
261 |
exportService.setFinishAction(exporttoServiceFinishAction); |
262 |
try {
|
263 |
exportService.export(featureSet); |
264 |
} catch (ExporttoServiceException e) {
|
265 |
LOG.info("Problems exporting the data", e);
|
266 |
showError(e); |
267 |
} finally {
|
268 |
((SimpleTaskStatus)(exportService.getTaskStatus())).terminate(); |
269 |
} |
270 |
} |
271 |
} |
272 |
|
273 |
private void showError(ExporttoServiceException e) { |
274 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
275 |
Iterator exceptions = e.iterator();
|
276 |
StringBuilder html = new StringBuilder(); |
277 |
|
278 |
html.append("<ul>\n");
|
279 |
while (exceptions.hasNext()) {
|
280 |
Throwable ex = ((Throwable) exceptions.next()); |
281 |
String message = null; |
282 |
if( ex instanceof UnknownHostException ) { |
283 |
message = "Unknown Host " + ex.getMessage();
|
284 |
} else {
|
285 |
message = ex.getMessage(); |
286 |
} |
287 |
if( message!=null ) { |
288 |
html.append("<li>");
|
289 |
if( message.toLowerCase().contains(" create ") ) { |
290 |
message = message.replaceFirst(" create ", "<br>\nCREATE "); |
291 |
message = message.replaceAll(",", ",<br>\n"); |
292 |
} else if( message.toLowerCase().contains(" insert ")) { |
293 |
message = message.replaceFirst(" insert ", "<br>\nINSERT "); |
294 |
message = message.replaceAll(",", ",<br>\n"); |
295 |
} |
296 |
html.append(message); |
297 |
html.append("</li>\n");
|
298 |
} |
299 |
} |
300 |
html.append("</ul>\n");
|
301 |
MessagePanel.showMessage( |
302 |
i18nManager.getTranslation("_Warning"),
|
303 |
i18nManager.getTranslation("_There_have_been_problems_exporting_data"),
|
304 |
html.toString(), |
305 |
e.getFeature() |
306 |
); |
307 |
} |
308 |
|
309 |
public void setWizardPanelActionListener( |
310 |
WizardPanelActionListener wizardActionListener) { |
311 |
this.exporttoServicePanelListener = wizardActionListener;
|
312 |
} |
313 |
|
314 |
public WizardPanelActionListener getWizardPanelActionListener() {
|
315 |
if (exporttoServicePanelListener == null) { |
316 |
exporttoServicePanelListener = |
317 |
new DefaultJExporttoServicePanelListener(this); |
318 |
} |
319 |
return exporttoServicePanelListener;
|
320 |
} |
321 |
|
322 |
@Override
|
323 |
public void setExporttoServicePanelListener( |
324 |
JExporttoServicePanelListener exporttoServicePanelListener) { |
325 |
((DefaultJExporttoServicePanelListener) getWizardPanelActionListener()) |
326 |
.setExporttoServicePanelListener(exporttoServicePanelListener); |
327 |
} |
328 |
|
329 |
@Override
|
330 |
public int getStatus() { |
331 |
return this.status; |
332 |
} |
333 |
|
334 |
public void setStatus(int status) { |
335 |
this.status = status;
|
336 |
} |
337 |
} |