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
/**
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
import org.apache.commons.lang3.StringUtils;
40

    
41
import org.gvsig.export.ExportException;
42
import org.gvsig.export.ExportLocator;
43
import org.gvsig.export.ExportManager;
44
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
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
65
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
66
import org.gvsig.tools.bookmarksandhistory.History;
67

    
68
/**
69
 * Default implementation for the {@link JExportProcessPanel}.
70
 *
71
 * @author gvSIG Team
72
 * @version $Id$
73
 */
74
@SuppressWarnings("UseSpecificCatch")
75
public class DefaultJExportProcessPanel
76
        extends JPanel
77
        implements JExportProcessPanel, WizardPanel {
78

    
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
    private int lastAction;
87

    
88
    public DefaultJExportProcessPanel(
89
            ExportProcess process,
90
            Filter<ExportServiceFactory> serviceFilter
91
    ) {
92
        this.process = process;
93
        this.serviceFilter = serviceFilter;
94
        this.finishListeners = new HashSet<>();
95
        this.initComponents();
96
    }
97

    
98
    private void initComponents() {
99
        I18nManager i18n = ToolsLocator.getI18nManager();
100

    
101
        this.wizardPanel = new WizardPanelWithLogo();
102
        this.addPanel(
103
                new SelectFormatPanel(
104
                        this,
105
                        this.process.getParameters(),
106
                        this.serviceFilter,
107
                        new ActionListener() {
108
                    @Override
109
                    public void actionPerformed(ActionEvent e) {
110
                        doAddPanels(((SelectFormatPanel) (e.getSource())).getSelectedService());
111
                    }
112
                }
113
                )
114
        );
115
        this.setLayout(new BorderLayout());
116
        this.add(this.wizardPanel, BorderLayout.CENTER);
117
        this.wizardPanel.setFinishButtonText(i18n.getTranslation("export"));
118
        this.wizardPanel.setWizardListener(this);
119
        this.setWizardPanelActionListener(this.getWizardPanelActionListener());
120

    
121
        this.wizardPanel.setNextButtonEnabled(false);
122
        this.wizardPanel.setFinishButtonEnabled(false);
123
        this.setPreferredSize(new Dimension(600, 350));
124
    }
125

    
126
    private void addPanel(ExportPanel panel) {
127
        OptionPanel optionPanel = new WizardOptionPanelAdapter(this, panel);
128
        this.wizardPanel.addOptionPanel(optionPanel);
129
    }
130

    
131
    private void doAddPanels(ExportServiceFactory serviceFactory) {
132

    
133
        try {
134
            removePreviousPanels();
135
            if (this.process.getParameters() != null) {
136
                if ( StringUtils.equals(this.process.getParameters().getServiceName(),serviceFactory.getName())) {
137
                    this.process.setParameters(null);
138
                }
139
            }
140
            
141
            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
            History<ExportParameters> h = exportManager.getHistory();
297
            h.add(copyParams);
298
        } catch (Exception ex) {
299
            LOG.warn("Not able to create a clone object for export parameters", ex);
300
        }
301

    
302
        Thread th = new Thread(
303
                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
                }
313
            }
314
        },"exportProcess");
315
        th.start();
316
    }
317

    
318
    private void doProcessFinished() {
319
        fireFinished();
320
    }
321

    
322
    @Override
323
    public JComponent asJComponent() {
324
        return this;
325
    }
326

    
327
    private JButton getButton(int button) {
328
        switch (button) {
329
            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
        throw new IllegalArgumentException("Illegal button value (" + button + ").");
339
    }
340

    
341
    @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
    public void setLastAcction(int action) {
362
        this.lastAction = action;
363
    }
364

    
365
    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

    
379
}