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 @ 44390

History | View | Annotate | Download (12.2 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
import java.util.HashSet;
31
import java.util.Set;
32
import javax.swing.JButton;
33
34
import javax.swing.JComponent;
35
import javax.swing.JPanel;
36
import javax.swing.SwingUtilities;
37
38
import jwizardcomponent.DefaultJWizardComponents;
39 44390 jjdelcerro
import org.apache.commons.lang3.StringUtils;
40 43925 jjdelcerro
41
import org.gvsig.export.ExportException;
42 44386 omartinez
import org.gvsig.export.ExportLocator;
43 44390 jjdelcerro
import org.gvsig.export.ExportManager;
44 43925 jjdelcerro
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46
47
import org.gvsig.fmap.dal.feature.Feature;
48
import org.gvsig.gui.beans.wizard.WizardPanel;
49
import org.gvsig.gui.beans.wizard.WizardPanelActionListener;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.i18n.I18nManager;
52
import org.gvsig.export.ExportParameters;
53
import org.gvsig.export.ExportProcess;
54
import org.gvsig.export.Filter;
55
import org.gvsig.export.spi.ExportServiceFactory;
56
import org.gvsig.export.swing.ExportSwingLocator;
57
import org.gvsig.export.swing.JExportProcessPanel;
58
import org.gvsig.export.swing.impl.panels.ExportProgressPanel;
59
import org.gvsig.export.swing.impl.panels.SelectFormatPanel;
60
import org.gvsig.export.swing.spi.ExportPanel;
61
import org.gvsig.export.swing.spi.ExportPanels;
62
import org.gvsig.export.swing.spi.ExportPanelsFactory;
63
import org.gvsig.export.swing.spi.ExportPanelsManager;
64 44044 jjdelcerro
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
65 43925 jjdelcerro
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
66 44386 omartinez
import org.gvsig.tools.bookmarksandhistory.History;
67 43925 jjdelcerro
68
/**
69
 * Default implementation for the {@link JExportProcessPanel}.
70
 *
71
 * @author gvSIG Team
72
 * @version $Id$
73
 */
74 44390 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
75 44386 omartinez
public class DefaultJExportProcessPanel
76 43925 jjdelcerro
        extends JPanel
77 44386 omartinez
        implements JExportProcessPanel, WizardPanel {
78 43925 jjdelcerro
79
    private static final Logger LOG = LoggerFactory.getLogger(DefaultJExportProcessPanel.class);
80
81
    private final ExportProcess process;
82
    private final Filter<ExportServiceFactory> serviceFilter;
83
    private final Set<ExportFinishListener> finishListeners;
84
    private WizardPanelWithLogo wizardPanel;
85
    private WizardPanelActionListener wizardListener;
86 44044 jjdelcerro
    private int lastAction;
87 44386 omartinez
88 43925 jjdelcerro
    public DefaultJExportProcessPanel(
89 44386 omartinez
            ExportProcess process,
90 43925 jjdelcerro
            Filter<ExportServiceFactory> serviceFilter
91 44386 omartinez
    ) {
92 43925 jjdelcerro
        this.process = process;
93
        this.serviceFilter = serviceFilter;
94
        this.finishListeners = new HashSet<>();
95
        this.initComponents();
96
    }
97 44386 omartinez
98 43925 jjdelcerro
    private void initComponents() {
99
        I18nManager i18n = ToolsLocator.getI18nManager();
100 44386 omartinez
101 43925 jjdelcerro
        this.wizardPanel = new WizardPanelWithLogo();
102
        this.addPanel(
103 44386 omartinez
                new SelectFormatPanel(
104
                        this,
105
                        this.process.getParameters(),
106
                        this.serviceFilter,
107
                        new ActionListener() {
108 43925 jjdelcerro
                    @Override
109
                    public void actionPerformed(ActionEvent e) {
110 44386 omartinez
                        doAddPanels(((SelectFormatPanel) (e.getSource())).getSelectedService());
111 43925 jjdelcerro
                    }
112
                }
113 44386 omartinez
                )
114
        );
115 43925 jjdelcerro
        this.setLayout(new BorderLayout());
116
        this.add(this.wizardPanel, BorderLayout.CENTER);
117
        this.wizardPanel.setFinishButtonText(i18n.getTranslation("export"));
118
        this.wizardPanel.setWizardListener(this);
119 43968 jjdelcerro
        this.setWizardPanelActionListener(this.getWizardPanelActionListener());
120 43925 jjdelcerro
121
        this.wizardPanel.setNextButtonEnabled(false);
122
        this.wizardPanel.setFinishButtonEnabled(false);
123
        this.setPreferredSize(new Dimension(600, 350));
124
    }
125 44386 omartinez
126 43925 jjdelcerro
    private void addPanel(ExportPanel panel) {
127 44044 jjdelcerro
        OptionPanel optionPanel = new WizardOptionPanelAdapter(this, panel);
128 43925 jjdelcerro
        this.wizardPanel.addOptionPanel(optionPanel);
129
    }
130 44386 omartinez
131 43925 jjdelcerro
    private void doAddPanels(ExportServiceFactory serviceFactory) {
132 44386 omartinez
133 43925 jjdelcerro
        try {
134
            removePreviousPanels();
135 44386 omartinez
            if (this.process.getParameters() != null) {
136 44390 jjdelcerro
                if ( StringUtils.equals(this.process.getParameters().getServiceName(),serviceFactory.getName())) {
137 44386 omartinez
                    this.process.setParameters(null);
138
                }
139
            }
140 43925 jjdelcerro
141 44386 omartinez
            if (this.process.getParameters()==null) {
142
                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 44390 jjdelcerro
            History<ExportParameters> h = exportManager.getHistory();
297 44386 omartinez
            h.add(copyParams);
298 44390 jjdelcerro
        } catch (Exception ex) {
299
            LOG.warn("Not able to create a clone object for export parameters", ex);
300 44386 omartinez
        }
301
302 43925 jjdelcerro
        Thread th = new Thread(
303 44386 omartinez
                new Runnable() {
304
            @Override
305
            public void run() {
306
                try {
307
                    process.run();
308
                    doProcessFinished();
309
                } catch (Exception ex) {
310
                    LOG.warn("Can't export", ex);
311
                    showError(ex);
312 43925 jjdelcerro
                }
313 44386 omartinez
            }
314 44390 jjdelcerro
        },"exportProcess");
315 43925 jjdelcerro
        th.start();
316
    }
317 44386 omartinez
318
    private void doProcessFinished() {
319 43925 jjdelcerro
        fireFinished();
320
    }
321
322
    @Override
323
    public JComponent asJComponent() {
324
        return this;
325
    }
326
327
    private JButton getButton(int button) {
328 44386 omartinez
        switch (button) {
329 43925 jjdelcerro
            case BUTTON_BACK:
330
                return this.wizardPanel.getWizardComponents().getBackButton();
331
            case BUTTON_NEXT:
332
                return this.wizardPanel.getWizardComponents().getNextButton();
333
            case BUTTON_FINISH:
334
                return this.wizardPanel.getWizardComponents().getFinishButton();
335
            case BUTTON_CANCEL:
336
                return this.wizardPanel.getWizardComponents().getCancelButton();
337
        }
338 44386 omartinez
        throw new IllegalArgumentException("Illegal button value (" + button + ").");
339 43925 jjdelcerro
    }
340 44386 omartinez
341 43925 jjdelcerro
    @Override
342
    public void setButtonEnabled(int button, boolean enabled) {
343
        this.getButton(button).setEnabled(enabled);
344
    }
345
346
    @Override
347
    public void setButtonText(int button, String text) {
348
        this.getButton(button).setText(text);
349
    }
350
351
    @Override
352
    public boolean isButtonEnabled(int button) {
353
        return this.getButton(button).isEnabled();
354
    }
355
356
    @Override
357
    public String getButtonText(int button) {
358
        return this.getButton(button).getText();
359
    }
360
361 44044 jjdelcerro
    public void setLastAcction(int action) {
362
        this.lastAction = action;
363
    }
364 44386 omartinez
365 44044 jjdelcerro
    public int getLastAction() {
366
        return this.lastAction;
367
    }
368
369
    @Override
370
    public void nextPanel() {
371
        this.wizardPanel.doAction(WizardPanelWithLogo.ACTION_NEXT);
372
    }
373
374
    @Override
375
    public void previousPanel() {
376
        this.wizardPanel.doAction(WizardPanelWithLogo.ACTION_PREVIOUS);
377
    }
378 44386 omartinez
379 43925 jjdelcerro
}