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 / export / swing / impl / DefaultJExportProcessPanel.java @ 44397
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.export.swing.impl; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
import java.util.Date; |
31 |
import java.util.HashSet; |
32 |
import java.util.Set; |
33 |
import javax.swing.JButton; |
34 |
|
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JPanel; |
37 |
import javax.swing.SwingUtilities; |
38 |
|
39 |
import jwizardcomponent.DefaultJWizardComponents; |
40 |
import org.apache.commons.lang3.StringUtils; |
41 |
|
42 |
import org.gvsig.export.ExportException; |
43 |
import org.gvsig.export.ExportLocator; |
44 |
import org.gvsig.export.ExportManager; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
import org.gvsig.fmap.dal.feature.Feature; |
49 |
import org.gvsig.gui.beans.wizard.WizardPanel; |
50 |
import org.gvsig.gui.beans.wizard.WizardPanelActionListener; |
51 |
import org.gvsig.tools.ToolsLocator; |
52 |
import org.gvsig.tools.i18n.I18nManager; |
53 |
import org.gvsig.export.ExportParameters; |
54 |
import org.gvsig.export.ExportProcess; |
55 |
import org.gvsig.export.Filter; |
56 |
import org.gvsig.export.spi.ExportServiceFactory; |
57 |
import org.gvsig.export.swing.ExportSwingLocator; |
58 |
import org.gvsig.export.swing.JExportProcessPanel; |
59 |
import org.gvsig.export.swing.impl.panels.ExportProgressPanel; |
60 |
import org.gvsig.export.swing.impl.panels.SelectFormatPanel; |
61 |
import org.gvsig.export.swing.spi.ExportPanel; |
62 |
import org.gvsig.export.swing.spi.ExportPanels; |
63 |
import org.gvsig.export.swing.spi.ExportPanelsFactory; |
64 |
import org.gvsig.export.swing.spi.ExportPanelsManager; |
65 |
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo; |
66 |
import org.gvsig.gui.beans.wizard.panel.OptionPanel; |
67 |
import org.gvsig.tools.bookmarksandhistory.History; |
68 |
|
69 |
/**
|
70 |
* Default implementation for the {@link JExportProcessPanel}.
|
71 |
*
|
72 |
* @author gvSIG Team
|
73 |
* @version $Id$
|
74 |
*/
|
75 |
@SuppressWarnings("UseSpecificCatch") |
76 |
public class DefaultJExportProcessPanel |
77 |
extends JPanel |
78 |
implements JExportProcessPanel, WizardPanel {
|
79 |
|
80 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultJExportProcessPanel.class); |
81 |
|
82 |
private final ExportProcess process; |
83 |
private final Filter<ExportServiceFactory> serviceFilter; |
84 |
private final Set<ExportFinishListener> finishListeners; |
85 |
private WizardPanelWithLogo wizardPanel;
|
86 |
private WizardPanelActionListener wizardListener;
|
87 |
private int lastAction; |
88 |
|
89 |
public DefaultJExportProcessPanel(
|
90 |
ExportProcess process, |
91 |
Filter<ExportServiceFactory> serviceFilter
|
92 |
) { |
93 |
this.process = process;
|
94 |
this.serviceFilter = serviceFilter;
|
95 |
this.finishListeners = new HashSet<>(); |
96 |
this.initComponents();
|
97 |
} |
98 |
|
99 |
private void initComponents() { |
100 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
101 |
|
102 |
this.wizardPanel = new WizardPanelWithLogo(); |
103 |
this.addPanel(
|
104 |
new SelectFormatPanel(
|
105 |
this,
|
106 |
this.process.getParameters(),
|
107 |
this.serviceFilter,
|
108 |
new ActionListener() { |
109 |
@Override
|
110 |
public void actionPerformed(ActionEvent e) { |
111 |
doAddPanels(((SelectFormatPanel) (e.getSource())).getSelectedService()); |
112 |
} |
113 |
} |
114 |
) |
115 |
); |
116 |
this.setLayout(new BorderLayout()); |
117 |
this.add(this.wizardPanel, BorderLayout.CENTER); |
118 |
this.wizardPanel.setFinishButtonText(i18n.getTranslation("export")); |
119 |
this.wizardPanel.setWizardListener(this); |
120 |
this.setWizardPanelActionListener(this.getWizardPanelActionListener()); |
121 |
|
122 |
this.wizardPanel.setNextButtonEnabled(false); |
123 |
this.wizardPanel.setFinishButtonEnabled(false); |
124 |
this.setPreferredSize(new Dimension(600, 350)); |
125 |
} |
126 |
|
127 |
private void addPanel(ExportPanel panel) { |
128 |
OptionPanel optionPanel = new WizardOptionPanelAdapter(this, panel); |
129 |
this.wizardPanel.addOptionPanel(optionPanel);
|
130 |
} |
131 |
|
132 |
private void doAddPanels(ExportServiceFactory serviceFactory) { |
133 |
|
134 |
try {
|
135 |
removePreviousPanels(); |
136 |
if (this.process.getParameters() != null) { |
137 |
// Check parameters are for the actual serviceFactory, if not, reset.
|
138 |
if ( !StringUtils.equals(this.process.getParameters().getServiceName(),serviceFactory.getName())) { |
139 |
this.process.setOutputFormat(serviceFactory.getName());
|
140 |
} |
141 |
} else if (this.process.getParameters()==null) { |
142 |
this.process.setOutputFormat(serviceFactory.getName());
|
143 |
} |
144 |
|
145 |
|
146 |
ExportParameters parameters = this.process.getParameters();
|
147 |
|
148 |
ExportPanelsManager panelsManager = ExportSwingLocator.getExportPanelsManager(); |
149 |
ExportPanelsFactory panelsFactory = panelsManager.getPanelsFactory(serviceFactory.getName()); |
150 |
ExportPanels panels = panelsFactory.createPanels(this, parameters);
|
151 |
for (ExportPanel panel : panels) {
|
152 |
this.addPanel(panel);
|
153 |
} |
154 |
if (parameters.needsSelectTargetProjection()) {
|
155 |
if (!panels.contains(ExportPanelsManager.PANEL_SELECT_TARGET_PROJECTION)) {
|
156 |
this.addPanel(panelsManager.createStandardPanel(
|
157 |
ExportPanelsManager.PANEL_SELECT_TARGET_PROJECTION, |
158 |
this,
|
159 |
parameters |
160 |
) |
161 |
); |
162 |
} |
163 |
} |
164 |
if (!panels.contains(ExportPanelsManager.PANEL_SELECT_FILTER)) {
|
165 |
this.addPanel(panelsManager.createStandardPanel(
|
166 |
ExportPanelsManager.PANEL_SELECT_FILTER, |
167 |
this,
|
168 |
parameters |
169 |
) |
170 |
); |
171 |
} |
172 |
this.addPanel(new ExportProgressPanel(this, parameters)); |
173 |
|
174 |
} catch (ExportException ex) { |
175 |
LOG.warn("Can't add panels to wizard.", ex);
|
176 |
showError(ex); |
177 |
} |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public ExportProcess getProcess() {
|
182 |
return this.process; |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public ExportParameters getParameters() {
|
187 |
return this.process.getParameters(); |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public void setWizardPanelActionListener( |
192 |
WizardPanelActionListener wizardListener |
193 |
) { |
194 |
this.wizardListener = wizardListener;
|
195 |
} |
196 |
|
197 |
@Override
|
198 |
public WizardPanelActionListener getWizardPanelActionListener() {
|
199 |
if (wizardListener == null) { |
200 |
wizardListener = new WizardPanelActionListener() {
|
201 |
@Override
|
202 |
public void finish(WizardPanel wizardPanel) { |
203 |
doExport(); |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public void cancel(WizardPanel wizardPanel) { |
208 |
doCancel(); |
209 |
} |
210 |
}; |
211 |
} |
212 |
return wizardListener;
|
213 |
} |
214 |
|
215 |
private void removePreviousPanels() { |
216 |
DefaultJWizardComponents wizardComponents = wizardPanel.getWizardComponents(); |
217 |
for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) { |
218 |
wizardComponents.removeWizardPanel(i); |
219 |
} |
220 |
} |
221 |
|
222 |
private void lastWizard() { |
223 |
this.wizardPanel.getWizardComponents().getNextButton()
|
224 |
.getActionListeners()[0].actionPerformed(null); |
225 |
} |
226 |
|
227 |
private void showError(Exception e) { |
228 |
Feature f = null;
|
229 |
if (e instanceof ExportException) { |
230 |
f = ((ExportException) e).getFeature();
|
231 |
} |
232 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
233 |
MessagePanel.showMessage( |
234 |
i18nManager.getTranslation("_Warning"),
|
235 |
i18nManager.getTranslation("_There_have_been_problems_exporting_data"),
|
236 |
e, |
237 |
f |
238 |
); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void addFinishListener(ExportFinishListener finishListener) { |
243 |
this.finishListeners.add(finishListener);
|
244 |
} |
245 |
|
246 |
private void fireCancelled() { |
247 |
if (!SwingUtilities.isEventDispatchThread()) { |
248 |
SwingUtilities.invokeLater(new Runnable() { |
249 |
@Override
|
250 |
public void run() { |
251 |
fireCancelled(); |
252 |
} |
253 |
}); |
254 |
} |
255 |
for (ExportFinishListener listener : this.finishListeners) { |
256 |
try {
|
257 |
listener.cancelled(this);
|
258 |
} catch (Exception ex) { |
259 |
|
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
private void fireFinished() { |
265 |
if (!SwingUtilities.isEventDispatchThread()) { |
266 |
SwingUtilities.invokeLater(new Runnable() { |
267 |
@Override
|
268 |
public void run() { |
269 |
fireFinished(); |
270 |
} |
271 |
}); |
272 |
return;
|
273 |
} |
274 |
for (ExportFinishListener listener : this.finishListeners) { |
275 |
try {
|
276 |
listener.finished(this);
|
277 |
} catch (Exception ex) { |
278 |
|
279 |
} |
280 |
} |
281 |
} |
282 |
|
283 |
private void doCancel() { |
284 |
this.setVisible(false); |
285 |
this.fireCancelled();
|
286 |
} |
287 |
|
288 |
private void doExport() { |
289 |
lastWizard(); |
290 |
ExportParameters params = this.process.getParameters();
|
291 |
ExportManager exportManager = ExportLocator.getManager(); |
292 |
|
293 |
ExportParameters copyParams = null;
|
294 |
try {
|
295 |
copyParams = (ExportParameters) params.clone(); |
296 |
copyParams.setCreationDate(new Date()); |
297 |
History<ExportParameters> h = exportManager.getHistory(); |
298 |
h.add(copyParams); |
299 |
} catch (Exception ex) { |
300 |
LOG.warn("Not able to create a clone object for export parameters", ex);
|
301 |
} |
302 |
|
303 |
Thread th = new Thread( |
304 |
new Runnable() { |
305 |
@Override
|
306 |
public void run() { |
307 |
try {
|
308 |
process.run(); |
309 |
doProcessFinished(); |
310 |
} catch (Exception ex) { |
311 |
LOG.warn("Can't export", ex);
|
312 |
showError(ex); |
313 |
} |
314 |
} |
315 |
},"exportProcess");
|
316 |
th.start(); |
317 |
} |
318 |
|
319 |
private void doProcessFinished() { |
320 |
fireFinished(); |
321 |
} |
322 |
|
323 |
@Override
|
324 |
public JComponent asJComponent() { |
325 |
return this; |
326 |
} |
327 |
|
328 |
private JButton getButton(int button) { |
329 |
switch (button) {
|
330 |
case BUTTON_BACK:
|
331 |
return this.wizardPanel.getWizardComponents().getBackButton(); |
332 |
case BUTTON_NEXT:
|
333 |
return this.wizardPanel.getWizardComponents().getNextButton(); |
334 |
case BUTTON_FINISH:
|
335 |
return this.wizardPanel.getWizardComponents().getFinishButton(); |
336 |
case BUTTON_CANCEL:
|
337 |
return this.wizardPanel.getWizardComponents().getCancelButton(); |
338 |
} |
339 |
throw new IllegalArgumentException("Illegal button value (" + button + ")."); |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public void setButtonEnabled(int button, boolean enabled) { |
344 |
this.getButton(button).setEnabled(enabled);
|
345 |
} |
346 |
|
347 |
@Override
|
348 |
public void setButtonText(int button, String text) { |
349 |
this.getButton(button).setText(text);
|
350 |
} |
351 |
|
352 |
@Override
|
353 |
public boolean isButtonEnabled(int button) { |
354 |
return this.getButton(button).isEnabled(); |
355 |
} |
356 |
|
357 |
@Override
|
358 |
public String getButtonText(int button) { |
359 |
return this.getButton(button).getText(); |
360 |
} |
361 |
|
362 |
public void setLastAcction(int action) { |
363 |
this.lastAction = action;
|
364 |
} |
365 |
|
366 |
public int getLastAction() { |
367 |
return this.lastAction; |
368 |
} |
369 |
|
370 |
@Override
|
371 |
public void nextPanel() { |
372 |
this.wizardPanel.doAction(WizardPanelWithLogo.ACTION_NEXT);
|
373 |
} |
374 |
|
375 |
@Override
|
376 |
public void previousPanel() { |
377 |
this.wizardPanel.doAction(WizardPanelWithLogo.ACTION_PREVIOUS);
|
378 |
} |
379 |
|
380 |
} |