Statistics
| Revision:

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 43925 jjdelcerro
/**
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 44395 omartinez
import java.util.Date;
31 43925 jjdelcerro
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 44390 jjdelcerro
import org.apache.commons.lang3.StringUtils;
41 43925 jjdelcerro
42
import org.gvsig.export.ExportException;
43 44386 omartinez
import org.gvsig.export.ExportLocator;
44 44390 jjdelcerro
import org.gvsig.export.ExportManager;
45 43925 jjdelcerro
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 44044 jjdelcerro
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
66 43925 jjdelcerro
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
67 44386 omartinez
import org.gvsig.tools.bookmarksandhistory.History;
68 43925 jjdelcerro
69
/**
70
 * Default implementation for the {@link JExportProcessPanel}.
71
 *
72
 * @author gvSIG Team
73
 * @version $Id$
74
 */
75 44390 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
76 44386 omartinez
public class DefaultJExportProcessPanel
77 43925 jjdelcerro
        extends JPanel
78 44386 omartinez
        implements JExportProcessPanel, WizardPanel {
79 43925 jjdelcerro
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 44044 jjdelcerro
    private int lastAction;
88 44386 omartinez
89 43925 jjdelcerro
    public DefaultJExportProcessPanel(
90 44386 omartinez
            ExportProcess process,
91 43925 jjdelcerro
            Filter<ExportServiceFactory> serviceFilter
92 44386 omartinez
    ) {
93 43925 jjdelcerro
        this.process = process;
94
        this.serviceFilter = serviceFilter;
95
        this.finishListeners = new HashSet<>();
96
        this.initComponents();
97
    }
98 44386 omartinez
99 43925 jjdelcerro
    private void initComponents() {
100
        I18nManager i18n = ToolsLocator.getI18nManager();
101 44386 omartinez
102 43925 jjdelcerro
        this.wizardPanel = new WizardPanelWithLogo();
103
        this.addPanel(
104 44386 omartinez
                new SelectFormatPanel(
105
                        this,
106
                        this.process.getParameters(),
107
                        this.serviceFilter,
108
                        new ActionListener() {
109 43925 jjdelcerro
                    @Override
110
                    public void actionPerformed(ActionEvent e) {
111 44386 omartinez
                        doAddPanels(((SelectFormatPanel) (e.getSource())).getSelectedService());
112 43925 jjdelcerro
                    }
113
                }
114 44386 omartinez
                )
115
        );
116 43925 jjdelcerro
        this.setLayout(new BorderLayout());
117
        this.add(this.wizardPanel, BorderLayout.CENTER);
118
        this.wizardPanel.setFinishButtonText(i18n.getTranslation("export"));
119
        this.wizardPanel.setWizardListener(this);
120 43968 jjdelcerro
        this.setWizardPanelActionListener(this.getWizardPanelActionListener());
121 43925 jjdelcerro
122
        this.wizardPanel.setNextButtonEnabled(false);
123
        this.wizardPanel.setFinishButtonEnabled(false);
124
        this.setPreferredSize(new Dimension(600, 350));
125
    }
126 44386 omartinez
127 43925 jjdelcerro
    private void addPanel(ExportPanel panel) {
128 44044 jjdelcerro
        OptionPanel optionPanel = new WizardOptionPanelAdapter(this, panel);
129 43925 jjdelcerro
        this.wizardPanel.addOptionPanel(optionPanel);
130
    }
131 44386 omartinez
132 43925 jjdelcerro
    private void doAddPanels(ExportServiceFactory serviceFactory) {
133 44386 omartinez
134 43925 jjdelcerro
        try {
135
            removePreviousPanels();
136 44386 omartinez
            if (this.process.getParameters() != null) {
137 44395 omartinez
                // 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 44386 omartinez
                }
141 44395 omartinez
            } else if (this.process.getParameters()==null) {
142 44386 omartinez
                this.process.setOutputFormat(serviceFactory.getName());
143
            }
144
145
146 43925 jjdelcerro
            ExportParameters parameters = this.process.getParameters();
147 44386 omartinez
148 43925 jjdelcerro
            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 44386 omartinez
            if (parameters.needsSelectTargetProjection()) {
155
                if (!panels.contains(ExportPanelsManager.PANEL_SELECT_TARGET_PROJECTION)) {
156
                    this.addPanel(panelsManager.createStandardPanel(
157 43925 jjdelcerro
                            ExportPanelsManager.PANEL_SELECT_TARGET_PROJECTION,
158
                            this,
159
                            parameters
160 44386 omartinez
                    )
161 43925 jjdelcerro
                    );
162
                }
163
            }
164 44386 omartinez
            if (!panels.contains(ExportPanelsManager.PANEL_SELECT_FILTER)) {
165
                this.addPanel(panelsManager.createStandardPanel(
166 43925 jjdelcerro
                        ExportPanelsManager.PANEL_SELECT_FILTER,
167
                        this,
168
                        parameters
169 44386 omartinez
                )
170 43925 jjdelcerro
                );
171
            }
172
            this.addPanel(new ExportProgressPanel(this, parameters));
173
174
        } catch (ExportException ex) {
175 44386 omartinez
            LOG.warn("Can't add panels to wizard.", ex);
176 43925 jjdelcerro
            showError(ex);
177
        }
178
    }
179 44386 omartinez
180 43925 jjdelcerro
    @Override
181
    public ExportProcess getProcess() {
182
        return this.process;
183
    }
184
185
    @Override
186
    public ExportParameters getParameters() {
187
        return this.process.getParameters();
188
    }
189 44386 omartinez
190 43925 jjdelcerro
    @Override
191
    public void setWizardPanelActionListener(
192
            WizardPanelActionListener wizardListener
193 44386 omartinez
    ) {
194 43925 jjdelcerro
        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 44386 omartinez
        }
220 43925 jjdelcerro
    }
221 44386 omartinez
222 43925 jjdelcerro
    private void lastWizard() {
223
        this.wizardPanel.getWizardComponents().getNextButton()
224 44386 omartinez
                .getActionListeners()[0].actionPerformed(null);
225
    }
226 43925 jjdelcerro
227
    private void showError(Exception e) {
228
        Feature f = null;
229 44386 omartinez
        if (e instanceof ExportException) {
230
            f = ((ExportException) e).getFeature();
231 43925 jjdelcerro
        }
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 44386 omartinez
241 43925 jjdelcerro
    @Override
242
    public void addFinishListener(ExportFinishListener finishListener) {
243
        this.finishListeners.add(finishListener);
244
    }
245
246
    private void fireCancelled() {
247 44386 omartinez
        if (!SwingUtilities.isEventDispatchThread()) {
248 43925 jjdelcerro
            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 44386 omartinez
            } catch (Exception ex) {
259
260 43925 jjdelcerro
            }
261
        }
262
    }
263 44386 omartinez
264 43925 jjdelcerro
    private void fireFinished() {
265 44386 omartinez
        if (!SwingUtilities.isEventDispatchThread()) {
266 43925 jjdelcerro
            SwingUtilities.invokeLater(new Runnable() {
267
                @Override
268
                public void run() {
269
                    fireFinished();
270
                }
271
            });
272 44071 omartinez
            return;
273 43925 jjdelcerro
        }
274
        for (ExportFinishListener listener : this.finishListeners) {
275
            try {
276
                listener.finished(this);
277 44386 omartinez
            } catch (Exception ex) {
278
279 43925 jjdelcerro
            }
280
        }
281
    }
282 44386 omartinez
283 43925 jjdelcerro
    private void doCancel() {
284
        this.setVisible(false);
285
        this.fireCancelled();
286
    }
287 44386 omartinez
288 43925 jjdelcerro
    private void doExport() {
289
        lastWizard();
290 44386 omartinez
        ExportParameters params = this.process.getParameters();
291 44390 jjdelcerro
        ExportManager exportManager = ExportLocator.getManager();
292 44386 omartinez
293
        ExportParameters copyParams = null;
294
        try {
295
            copyParams = (ExportParameters) params.clone();
296 44395 omartinez
            copyParams.setCreationDate(new Date());
297 44390 jjdelcerro
            History<ExportParameters> h = exportManager.getHistory();
298 44386 omartinez
            h.add(copyParams);
299 44390 jjdelcerro
        } catch (Exception ex) {
300
            LOG.warn("Not able to create a clone object for export parameters", ex);
301 44386 omartinez
        }
302
303 43925 jjdelcerro
        Thread th = new Thread(
304 44386 omartinez
                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 43925 jjdelcerro
                }
314 44386 omartinez
            }
315 44390 jjdelcerro
        },"exportProcess");
316 43925 jjdelcerro
        th.start();
317
    }
318 44386 omartinez
319
    private void doProcessFinished() {
320 43925 jjdelcerro
        fireFinished();
321
    }
322
323
    @Override
324
    public JComponent asJComponent() {
325
        return this;
326
    }
327
328
    private JButton getButton(int button) {
329 44386 omartinez
        switch (button) {
330 43925 jjdelcerro
            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 44386 omartinez
        throw new IllegalArgumentException("Illegal button value (" + button + ").");
340 43925 jjdelcerro
    }
341 44386 omartinez
342 43925 jjdelcerro
    @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 44044 jjdelcerro
    public void setLastAcction(int action) {
363
        this.lastAction = action;
364
    }
365 44386 omartinez
366 44044 jjdelcerro
    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 44386 omartinez
380 43925 jjdelcerro
}