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

History | View | Annotate | Download (11.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.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

    
40
import org.gvsig.export.ExportException;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.gui.beans.wizard.WizardPanel;
46
import org.gvsig.gui.beans.wizard.WizardPanelActionListener;
47
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.gvsig.export.ExportParameters;
51
import org.gvsig.export.ExportProcess;
52
import org.gvsig.export.Filter;
53
import org.gvsig.export.spi.ExportServiceFactory;
54
import org.gvsig.export.swing.ExportSwingLocator;
55
import org.gvsig.export.swing.JExportProcessPanel;
56
import org.gvsig.export.swing.impl.panels.ExportProgressPanel;
57
import org.gvsig.export.swing.impl.panels.SelectFormatPanel;
58
import org.gvsig.export.swing.spi.ExportPanel;
59
import org.gvsig.export.swing.spi.ExportPanels;
60
import org.gvsig.export.swing.spi.ExportPanelsFactory;
61
import org.gvsig.export.swing.spi.ExportPanelsManager;
62
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
63
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
64

    
65
/**
66
 * Default implementation for the {@link JExportProcessPanel}.
67
 *
68
 * @author gvSIG Team
69
 * @version $Id$
70
 */
71
public class DefaultJExportProcessPanel 
72
        extends JPanel
73
        implements JExportProcessPanel, WizardPanel 
74
    {
75

    
76
    private static final Logger LOG = LoggerFactory.getLogger(DefaultJExportProcessPanel.class);
77

    
78
    private final ExportProcess process;
79
    private final Filter<ExportServiceFactory> serviceFilter;
80
    private final Set<ExportFinishListener> finishListeners;
81
    private WizardPanelWithLogo wizardPanel;
82
    private WizardPanelActionListener wizardListener;
83
    private int lastAction;
84
    
85
    
86
    public DefaultJExportProcessPanel(
87
            ExportProcess process, 
88
            Filter<ExportServiceFactory> serviceFilter
89
        ) {
90
        this.process = process;
91
        this.serviceFilter = serviceFilter;
92
        this.finishListeners = new HashSet<>();
93
        this.initComponents();
94
    }
95
    
96
    private void initComponents() {
97
        I18nManager i18n = ToolsLocator.getI18nManager();
98
        
99
        this.wizardPanel = new WizardPanelWithLogo();
100
        this.addPanel(
101
            new SelectFormatPanel(
102
                this,
103
                this.process.getParameters(), 
104
                this.serviceFilter, 
105
                new ActionListener() {
106
                    @Override
107
                    public void actionPerformed(ActionEvent e) {
108
                        doAddPanels(((SelectFormatPanel)(e.getSource())).getSelectedService());
109
                    }
110
                }
111
            )
112
        );        
113
        this.setLayout(new BorderLayout());
114
        this.add(this.wizardPanel, BorderLayout.CENTER);
115
        this.wizardPanel.setFinishButtonText(i18n.getTranslation("export"));
116
        this.wizardPanel.setWizardListener(this);
117
        this.setWizardPanelActionListener(this.getWizardPanelActionListener());
118

    
119
        this.wizardPanel.setNextButtonEnabled(false);
120
        this.wizardPanel.setFinishButtonEnabled(false);
121
        this.setPreferredSize(new Dimension(600, 350));
122
    }
123
    
124
    private void addPanel(ExportPanel panel) {
125
        OptionPanel optionPanel = new WizardOptionPanelAdapter(this, panel);
126
        this.wizardPanel.addOptionPanel(optionPanel);
127
    }
128
    
129
    private void doAddPanels(ExportServiceFactory serviceFactory) {
130
        
131
        try {
132
            removePreviousPanels();
133
            
134
            this.process.setOutputFormat(serviceFactory.getName());
135
            ExportParameters parameters = this.process.getParameters();
136
            
137
            ExportPanelsManager panelsManager = ExportSwingLocator.getExportPanelsManager();
138
            ExportPanelsFactory panelsFactory = panelsManager.getPanelsFactory(serviceFactory.getName());
139
            ExportPanels panels = panelsFactory.createPanels(this, parameters);
140
            for (ExportPanel panel : panels) {
141
                this.addPanel(panel);
142
            }
143
            if( parameters.needsSelectTargetProjection() ) {
144
                if( ! panels.contains(ExportPanelsManager.PANEL_SELECT_TARGET_PROJECTION) ) {
145
                    this.addPanel( panelsManager.createStandardPanel(
146
                            ExportPanelsManager.PANEL_SELECT_TARGET_PROJECTION,
147
                            this,
148
                            parameters
149
                        )
150
                    );
151
                }
152
            }
153
            if( ! panels.contains(ExportPanelsManager.PANEL_SELECT_FILTER) ) {
154
                this.addPanel( panelsManager.createStandardPanel(
155
                        ExportPanelsManager.PANEL_SELECT_FILTER,
156
                        this,
157
                        parameters
158
                    )
159
                );
160
            }
161
            this.addPanel(new ExportProgressPanel(this, parameters));
162

    
163
        } catch (ExportException ex) {
164
            LOG.warn("Can't add panels to wizard.",ex);
165
            showError(ex);
166
        }
167
    }
168
    
169
    @Override
170
    public ExportProcess getProcess() {
171
        return this.process;
172
    }
173

    
174
    @Override
175
    public ExportParameters getParameters() {
176
        return this.process.getParameters();
177
    }
178
    
179
    @Override
180
    public void setWizardPanelActionListener(
181
            WizardPanelActionListener wizardListener
182
        ) {
183
        this.wizardListener = wizardListener;
184
    }
185

    
186
    @Override
187
    public WizardPanelActionListener getWizardPanelActionListener() {
188
        if (wizardListener == null) {
189
            wizardListener = new WizardPanelActionListener() {
190
                @Override
191
                public void finish(WizardPanel wizardPanel) {
192
                    doExport();
193
                }
194

    
195
                @Override
196
                public void cancel(WizardPanel wizardPanel) {
197
                    doCancel();
198
                }
199
            };
200
        }
201
        return wizardListener;
202
    }
203

    
204
    private void removePreviousPanels() {
205
        DefaultJWizardComponents wizardComponents = wizardPanel.getWizardComponents();
206
        for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) {
207
            wizardComponents.removeWizardPanel(i);
208
        }        
209
    }
210
    
211
    private void lastWizard() {
212
        this.wizardPanel.getWizardComponents().getNextButton()
213
            .getActionListeners()[0].actionPerformed(null);
214
    }    
215

    
216
    private void showError(Exception e) {
217
        Feature f = null;
218
        if( e instanceof ExportException ) {
219
            f = ((ExportException)e).getFeature();
220
        }
221
        I18nManager i18nManager = ToolsLocator.getI18nManager();
222
        MessagePanel.showMessage(
223
                i18nManager.getTranslation("_Warning"),
224
                i18nManager.getTranslation("_There_have_been_problems_exporting_data"),
225
                e,
226
                f
227
        );
228
    }
229
    
230
    @Override
231
    public void addFinishListener(ExportFinishListener finishListener) {
232
        this.finishListeners.add(finishListener);
233
    }
234

    
235
    private void fireCancelled() {
236
        if( ! SwingUtilities.isEventDispatchThread() ) {
237
            SwingUtilities.invokeLater(new Runnable() {
238
                @Override
239
                public void run() {
240
                    fireCancelled();
241
                }
242
            });
243
        }
244
        for (ExportFinishListener listener : this.finishListeners) {
245
            try {
246
                listener.cancelled(this);
247
            } catch(Exception ex) {
248
                
249
            }
250
        }
251
    }
252
    
253
    private void fireFinished() {
254
        if( ! SwingUtilities.isEventDispatchThread() ) {
255
            SwingUtilities.invokeLater(new Runnable() {
256
                @Override
257
                public void run() {
258
                    fireFinished();
259
                }
260
            });
261
            return;
262
        }
263
        for (ExportFinishListener listener : this.finishListeners) {
264
            try {
265
                listener.finished(this);
266
            } catch(Exception ex) {
267
                
268
            }
269
        }
270
    }
271
    
272
    private void doCancel() {
273
        this.setVisible(false);
274
        this.fireCancelled();
275
    }
276
    
277
    private void doExport() {
278
        lastWizard();
279
        Thread th = new Thread(
280
            new Runnable() {
281
                @Override
282
                public void run() {
283
                    try {
284
                        process.run();
285
                        doProcessFinished();
286
                    } catch(Exception ex) {
287
                        LOG.warn("Can't export",ex);
288
                        showError(ex);
289
                    }
290
                }
291
            },
292
            "exportProcess"
293
        );
294
        th.start();
295
    }
296
    
297
    private void doProcessFinished() {  
298
        fireFinished();
299
    }
300

    
301
    @Override
302
    public JComponent asJComponent() {
303
        return this;
304
    }
305

    
306
    private JButton getButton(int button) {
307
        switch(button) {
308
            case BUTTON_BACK:
309
                return this.wizardPanel.getWizardComponents().getBackButton();
310
            case BUTTON_NEXT:
311
                return this.wizardPanel.getWizardComponents().getNextButton();
312
            case BUTTON_FINISH:
313
                return this.wizardPanel.getWizardComponents().getFinishButton();
314
            case BUTTON_CANCEL:
315
                return this.wizardPanel.getWizardComponents().getCancelButton();
316
        }
317
        throw new IllegalArgumentException("Illegal button value ("+button+").");
318
    }
319
    
320
    @Override
321
    public void setButtonEnabled(int button, boolean enabled) {
322
        this.getButton(button).setEnabled(enabled);
323
    }
324

    
325
    @Override
326
    public void setButtonText(int button, String text) {
327
        this.getButton(button).setText(text);
328
    }
329

    
330
    @Override
331
    public boolean isButtonEnabled(int button) {
332
        return this.getButton(button).isEnabled();
333
    }
334

    
335
    @Override
336
    public String getButtonText(int button) {
337
        return this.getButton(button).getText();
338
    }
339

    
340
    public void setLastAcction(int action) {
341
        this.lastAction = action;
342
    }
343
    
344
    public int getLastAction() {
345
        return this.lastAction;
346
    }
347

    
348
    @Override
349
    public void nextPanel() {
350
        this.wizardPanel.doAction(WizardPanelWithLogo.ACTION_NEXT);
351
    }
352

    
353
    @Override
354
    public void previousPanel() {
355
        this.wizardPanel.doAction(WizardPanelWithLogo.ACTION_PREVIOUS);
356
    }
357
    
358
}