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 @ 43920
History | View | Annotate | Download (13.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
|
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.awt.Dimension; |
28 |
|
29 |
import javax.swing.ImageIcon; |
30 |
import javax.swing.JOptionPane; |
31 |
|
32 |
import jwizardcomponent.DefaultJWizardComponents; |
33 |
|
34 |
import org.cresques.cts.IProjection; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.exportto.ExporttoService; |
39 |
import org.gvsig.exportto.ExporttoServiceException; |
40 |
import org.gvsig.exportto.ExporttoServiceFinishAction; |
41 |
import org.gvsig.exportto.swing.JExporttoServicePanel; |
42 |
import org.gvsig.exportto.swing.JExporttoServicePanelListener; |
43 |
import org.gvsig.exportto.swing.impl.panel.MessagePanel; |
44 |
import org.gvsig.exportto.swing.impl.wizard.DefaultWizardContainer; |
45 |
import org.gvsig.exportto.swing.impl.wizard.ExportFilterWizard; |
46 |
import org.gvsig.exportto.swing.impl.wizard.ExportTargetProjectionWizard; |
47 |
import org.gvsig.exportto.swing.impl.wizard.ExporterSelectionWizard; |
48 |
import org.gvsig.exportto.swing.impl.wizard.ExporttoProgressWizard; |
49 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProvider; |
50 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderFactory; |
51 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderLocator; |
52 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderManager; |
53 |
import org.gvsig.exportto.swing.spi.options.ExportGeometryOptions; |
54 |
import org.gvsig.exportto.swing.spi.options.ExportOptions; |
55 |
import org.gvsig.fmap.dal.DALLocator; |
56 |
import org.gvsig.fmap.dal.DataManager; |
57 |
import org.gvsig.fmap.dal.exception.DataException; |
58 |
import org.gvsig.fmap.dal.feature.Feature; |
59 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
60 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
61 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
62 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
63 |
import org.gvsig.gui.beans.wizard.WizardPanel; |
64 |
import org.gvsig.gui.beans.wizard.WizardPanelActionListener; |
65 |
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo; |
66 |
import org.gvsig.tools.ToolsLocator; |
67 |
import org.gvsig.tools.evaluator.Evaluator; |
68 |
import org.gvsig.tools.i18n.I18nManager; |
69 |
import org.gvsig.tools.service.ServiceException; |
70 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
71 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
72 |
import org.gvsig.tools.task.SimpleTaskStatus; |
73 |
|
74 |
/**
|
75 |
* Default implementation for the {@link JExporttoServicePanel}.
|
76 |
*
|
77 |
* @author gvSIG Team
|
78 |
* @version $Id$
|
79 |
*/
|
80 |
public class DefaultJExporttoServicePanel extends JExporttoServicePanel |
81 |
implements WizardPanel {
|
82 |
|
83 |
private static final Logger LOG = LoggerFactory |
84 |
.getLogger(DefaultJExporttoServicePanel.class); |
85 |
|
86 |
private static final DataManager DATA_MANAGER = DALLocator.getDataManager(); |
87 |
|
88 |
private static final long serialVersionUID = -7026467582252285238L; |
89 |
|
90 |
private FeatureStore featureStore;
|
91 |
private IProjection projection;
|
92 |
private FLyrVect vectorLayer = null; |
93 |
private DefaultExporttoSwingManager uimanager;
|
94 |
|
95 |
// Used to get the new feature store parameters
|
96 |
private ExporttoSwingProvider exporttoSwingProvider;
|
97 |
|
98 |
// // Provider types that will be displayed
|
99 |
// private int[] providerTypes;
|
100 |
|
101 |
// Wizards used to create the main wizard
|
102 |
private ExporterSelectionWizard exporterSelectionWizard = null; |
103 |
private ExportFilterWizard exportFilterWizard = null; |
104 |
private ExportTargetProjectionWizard exportTargetProjectionWizard = 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 |
@SuppressWarnings("LeakingThisInConstructor") |
132 |
public DefaultJExporttoServicePanel(
|
133 |
DefaultExporttoSwingManager uimanager, |
134 |
FeatureStore featureStore, |
135 |
IProjection projection, |
136 |
ExporttoServiceFinishAction exporttoServiceFinishAction, |
137 |
int[] providerTypes |
138 |
) { |
139 |
this.featureStore = featureStore;
|
140 |
this.projection = projection;
|
141 |
this.uimanager = uimanager;
|
142 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
143 |
// this.providerTypes = providerTypes;
|
144 |
this.status = JOptionPane.UNDEFINED_CONDITION; |
145 |
|
146 |
ImageIcon ii = IconThemeHelper.getImageIcon("wizard-export-to"); |
147 |
wizardPanelWithLogo = new WizardPanelWithLogo(ii);
|
148 |
|
149 |
// Initialize the wizards
|
150 |
exporterSelectionWizard = new ExporterSelectionWizard(this, providerTypes); |
151 |
|
152 |
// Adding the wizards to the main wizard
|
153 |
wizardPanelWithLogo.addOptionPanel(exporterSelectionWizard); |
154 |
|
155 |
// The finish button is called "export"
|
156 |
this.wizardPanelWithLogo.getWizardComponents().getFinishButton()
|
157 |
.setText(uimanager.getTranslation("export"));
|
158 |
|
159 |
// Set the listener for the finish and cancell events
|
160 |
this.wizardPanelWithLogo.setWizardListener(this); |
161 |
|
162 |
this.initComponents();
|
163 |
} |
164 |
|
165 |
private void initComponents() { |
166 |
this.setLayout(new BorderLayout()); |
167 |
this.add(wizardPanelWithLogo, BorderLayout.CENTER); |
168 |
|
169 |
// Disable the nextbutton
|
170 |
setNextButtonEnabled(false);
|
171 |
setExportButtonEnabled(false);
|
172 |
|
173 |
// Set the listener for the finish and cancell events
|
174 |
this.setPreferredSize(new Dimension(700, 350)); |
175 |
} |
176 |
|
177 |
public FeatureStore getFeatureStore() {
|
178 |
return this.featureStore; |
179 |
} |
180 |
|
181 |
public void setNextButtonEnabled(boolean isEnabled) { |
182 |
wizardPanelWithLogo.setNextButtonEnabled(isEnabled); |
183 |
} |
184 |
|
185 |
public void setExportButtonEnabled(boolean isEnabled) { |
186 |
wizardPanelWithLogo.setFinishButtonEnabled(isEnabled); |
187 |
} |
188 |
|
189 |
public void setCancelButtonText(String text) { |
190 |
wizardPanelWithLogo.getWizardComponents().getCancelButton() |
191 |
.setText(text); |
192 |
} |
193 |
|
194 |
/**
|
195 |
* @return the uimanager
|
196 |
*/
|
197 |
public DefaultExporttoSwingManager getExporttoSwingManager() {
|
198 |
return uimanager;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* @param provider
|
203 |
* @throws ServiceException
|
204 |
*/
|
205 |
public void selectExporttoSwingProvider(ExporttoSwingProviderFactory provider) |
206 |
throws ServiceException {
|
207 |
|
208 |
ExporttoSwingProviderManager manager = ExporttoSwingProviderLocator.getManager(); |
209 |
exporttoSwingProvider = manager.createExporttoSwingProvider( |
210 |
provider.getName(), vectorLayer |
211 |
); |
212 |
ExportOptions options = this.exporttoSwingProvider.getOptions();
|
213 |
|
214 |
if( options instanceof ExportGeometryOptions ) { |
215 |
ExportGeometryOptions geomOptions = (ExportGeometryOptions) options; |
216 |
geomOptions.setTargetProjection(projection); |
217 |
exportTargetProjectionWizard = new ExportTargetProjectionWizard(this, geomOptions); |
218 |
exportTargetProjectionWizard.setVectorLayer(vectorLayer); |
219 |
} |
220 |
exportFilterWizard = new ExportFilterWizard(this, options); |
221 |
exporttoProgressWizard = new ExporttoProgressWizard(this, options); |
222 |
|
223 |
DefaultJWizardComponents wizardComponents = wizardPanelWithLogo.getWizardComponents(); |
224 |
for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) { |
225 |
wizardComponents.removeWizardPanel(i); |
226 |
} |
227 |
for (int i = 0; i < exporttoSwingProvider.getPanelCount(); i++) { |
228 |
wizardPanelWithLogo.addOptionPanel( |
229 |
new DefaultWizardContainer(
|
230 |
exporttoSwingProvider.getPanelAt(i) |
231 |
) |
232 |
); |
233 |
} |
234 |
if (exporttoSwingProvider.needsPanelTargetProjection() && exportTargetProjectionWizard!=null ){ |
235 |
wizardPanelWithLogo.addOptionPanel(exportTargetProjectionWizard); |
236 |
} |
237 |
wizardPanelWithLogo.addOptionPanel(exportFilterWizard); |
238 |
wizardPanelWithLogo.addOptionPanel(exporttoProgressWizard); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public Dimension getPreferredSize() { |
243 |
return new Dimension(800, 550); |
244 |
} |
245 |
|
246 |
public void export() throws DataException, ExporttoServiceException { |
247 |
this.lastWizard();
|
248 |
|
249 |
// Create the feature Set...
|
250 |
FeatureSet featureSet = null;
|
251 |
if ( exportFilterWizard.isFullLayerSelected() ) {
|
252 |
featureSet = featureStore.getFeatureSet(); |
253 |
|
254 |
} else if ( exportFilterWizard.isSelectedFeaturesSelected() ) { |
255 |
featureSet = (FeatureSet) featureStore.getSelection(); |
256 |
|
257 |
} else {
|
258 |
Evaluator filter = exportFilterWizard.getFilter(); |
259 |
if( filter!=null ) { |
260 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
261 |
featureQuery.setFilter(filter); |
262 |
featureSet = featureStore.getFeatureSet(featureQuery); |
263 |
} |
264 |
} |
265 |
|
266 |
Export export = new Export(featureSet); |
267 |
export.start();
|
268 |
} |
269 |
|
270 |
public void lastWizard() { |
271 |
this.wizardPanelWithLogo.getWizardComponents().getNextButton()
|
272 |
.getActionListeners()[0].actionPerformed(null); |
273 |
} |
274 |
|
275 |
private class Export extends Thread { |
276 |
|
277 |
private final FeatureSet featureSet; |
278 |
|
279 |
public Export(FeatureSet featureSet) {
|
280 |
super();
|
281 |
this.featureSet = featureSet;
|
282 |
} |
283 |
|
284 |
@Override
|
285 |
public synchronized void run() { |
286 |
ExporttoService exportService; |
287 |
try {
|
288 |
exportService = exporttoSwingProvider.createExporttoService(); |
289 |
} catch(Exception ex) { |
290 |
LOG.warn("Problems exporting layer.", ex);
|
291 |
showError(ex); |
292 |
return;
|
293 |
} |
294 |
exporttoProgressWizard.bind(exportService.getTaskStatus()); |
295 |
exportService.setFinishAction(exporttoServiceFinishAction); |
296 |
String layerName = "(unknow)"; |
297 |
String layerFullName = "(unknow)"; |
298 |
try {
|
299 |
layerName = featureStore.getName(); |
300 |
layerFullName = featureStore.getFullName(); |
301 |
LOG.info("Exporting '"+layerName+"' using '"+exportService.getClass().getName()+"' ("+layerFullName+")."); |
302 |
((SimpleTaskStatus)(exportService.getTaskStatus())).setTitle("Exporting "+layerName+"..."); |
303 |
exportService.export(featureSet); |
304 |
} catch (ExporttoServiceException e) {
|
305 |
LOG.warn("Problems exporting the data (layer="+layerName+").", e); |
306 |
showError(e); |
307 |
} finally {
|
308 |
((SimpleTaskStatus)(exportService.getTaskStatus())).terminate(); |
309 |
} |
310 |
} |
311 |
} |
312 |
|
313 |
private void showError(Exception e) { |
314 |
Feature f = null;
|
315 |
if( e instanceof ExporttoServiceException ) { |
316 |
f = ((ExporttoServiceException)e).getFeature(); |
317 |
} |
318 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
319 |
MessagePanel.showMessage( |
320 |
i18nManager.getTranslation("_Warning"),
|
321 |
i18nManager.getTranslation("_There_have_been_problems_exporting_data"),
|
322 |
e, |
323 |
f |
324 |
); |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void setWizardPanelActionListener( |
329 |
WizardPanelActionListener wizardActionListener) { |
330 |
this.exporttoServicePanelListener = wizardActionListener;
|
331 |
} |
332 |
|
333 |
@Override
|
334 |
public WizardPanelActionListener getWizardPanelActionListener() {
|
335 |
if (exporttoServicePanelListener == null) { |
336 |
exporttoServicePanelListener = |
337 |
new DefaultJExporttoServicePanelListener(this); |
338 |
} |
339 |
return exporttoServicePanelListener;
|
340 |
} |
341 |
|
342 |
@Override
|
343 |
public void setExporttoServicePanelListener( |
344 |
JExporttoServicePanelListener exporttoServicePanelListener) { |
345 |
((DefaultJExporttoServicePanelListener) getWizardPanelActionListener()) |
346 |
.setExporttoServicePanelListener(exporttoServicePanelListener); |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public int getStatus() { |
351 |
return this.status; |
352 |
} |
353 |
|
354 |
public void setStatus(int status) { |
355 |
this.status = status;
|
356 |
if( this.status == JOptionPane.CANCEL_OPTION ) { |
357 |
this.setVisible(false); |
358 |
} |
359 |
} |
360 |
} |