Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.impl / src / main / java / org / gvsig / exportto / swing / impl / DefaultJExporttoServicePanel.java @ 37887

History | View | Annotate | Download (10.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.exportto.swing.impl;
23

    
24
import java.awt.BorderLayout;
25
import java.net.URL;
26

    
27
import javax.swing.ImageIcon;
28
import javax.swing.JOptionPane;
29

    
30
import jwizardcomponent.DefaultJWizardComponents;
31

    
32
import org.cresques.cts.IProjection;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
import org.gvsig.exportto.ExporttoService;
37
import org.gvsig.exportto.ExporttoServiceException;
38
import org.gvsig.exportto.ExporttoServiceFinishAction;
39
import org.gvsig.exportto.swing.JExporttoServicePanel;
40
import org.gvsig.exportto.swing.JExporttoServicePanelListener;
41
import org.gvsig.exportto.swing.impl.wizard.DefaultWizardContainer;
42
import org.gvsig.exportto.swing.impl.wizard.ExportFilterWizard;
43
import org.gvsig.exportto.swing.impl.wizard.ExporterSelectionWizard;
44
import org.gvsig.exportto.swing.impl.wizard.ExporttoProgressWizard;
45
import org.gvsig.exportto.swing.spi.ExporttoSwingProvider;
46
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderFactory;
47
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderLocator;
48
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderManager;
49
import org.gvsig.fmap.dal.DALLocator;
50
import org.gvsig.fmap.dal.DataManager;
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.FeatureQuery;
53
import org.gvsig.fmap.dal.feature.FeatureSet;
54
import org.gvsig.fmap.dal.feature.FeatureStore;
55
import org.gvsig.gui.beans.wizard.WizardPanel;
56
import org.gvsig.gui.beans.wizard.WizardPanelActionListener;
57
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
58
import org.gvsig.tools.evaluator.Evaluator;
59
import org.gvsig.tools.service.ServiceException;
60
import org.gvsig.tools.task.TaskStatus;
61

    
62
/**
63
 * Default implementation for the {@link JExporttoServicePanel}.
64
 * 
65
 * @author gvSIG Team
66
 * @version $Id$
67
 */
68
public class DefaultJExporttoServicePanel extends JExporttoServicePanel
69
    implements WizardPanel {
70

    
71
    private static final Logger LOG = LoggerFactory
72
        .getLogger(DefaultJExporttoServicePanel.class);
73

    
74
    private static final ExporttoSwingProviderManager EXPORTTO_SWING_PROVIDER_MANAGER =
75
        ExporttoSwingProviderLocator.getManager();
76
    private static final DataManager DATA_MANAGER = DALLocator.getDataManager();
77

    
78
    private static final long serialVersionUID = -7026467582252285238L;
79

    
80
    private FeatureStore featureStore;
81
    private IProjection projection;
82
    private DefaultExporttoSwingManager uimanager;
83

    
84
    // Used to get the new feature store parameters
85
    private ExporttoSwingProvider exporttoSwingProvider;
86

    
87
    // Provider types that will be displayed
88
    private int[] providerTypes;
89

    
90
    // Wizards used to create the main wizard
91
    private ExporterSelectionWizard exporterSelectionWizard = null;
92
    private ExportFilterWizard exportFilterWizard = null;
93
    private ExporttoProgressWizard exporttoProgressWizard = null;
94

    
95
    // Listener for the finish and cancell button
96
    private WizardPanelActionListener exporttoServicePanelListener;
97

    
98
    private WizardPanelWithLogo wizardPanelWithLogo = null;
99

    
100
    // Action to execute at the end of the export process
101
    ExporttoServiceFinishAction exporttoServiceFinishAction;
102

    
103
    private int status;
104

    
105
    public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager,
106
        FeatureStore featureStore, IProjection projection,
107
        ExporttoServiceFinishAction exporttoServiceFinishAction,
108
        int[] providerTypes) {
109
        this.featureStore = featureStore;
110
        this.projection = projection;
111
        this.uimanager = uimanager;
112
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
113
        this.providerTypes = providerTypes;
114
        this.status = JOptionPane.UNDEFINED_CONDITION;
115

    
116
        URL iconURL =
117
            getClass().getClassLoader().getResource(
118
                "org/gvsig/exportto/swing/impl/images/exporttoicon.png");
119
        ImageIcon icon = new ImageIcon(iconURL);
120
        wizardPanelWithLogo = new WizardPanelWithLogo(icon);
121

    
122
        // Initialize the wizards
123
        exporterSelectionWizard =
124
            new ExporterSelectionWizard(this, providerTypes);
125
        exportFilterWizard = new ExportFilterWizard(this);
126
        exporttoProgressWizard = new ExporttoProgressWizard(this);
127

    
128
        // Adding the wizards to the main wizard
129
        wizardPanelWithLogo.addOptionPanel(exporterSelectionWizard);
130

    
131
        // The finish button is called "export"
132
        this.wizardPanelWithLogo.getWizardComponents().getFinishButton()
133
            .setText(uimanager.getTranslation("export"));
134

    
135
        this.setLayout(new BorderLayout());
136
        this.add(wizardPanelWithLogo, BorderLayout.CENTER);
137

    
138
        // Disable the nextbutton
139
        setNextButtonEnabled(false);
140
        setExportButtonEnabled(false);
141

    
142
        // Set the listener for the finish and cancell events
143
        this.wizardPanelWithLogo.setWizardListener(this);
144
    }
145

    
146
    public void setNextButtonEnabled(boolean isEnabled) {
147
        wizardPanelWithLogo.setNextButtonEnabled(isEnabled);
148
    }
149

    
150
    public void setExportButtonEnabled(boolean isEnabled) {
151
        wizardPanelWithLogo.setFinishButtonEnabled(isEnabled);
152
    }
153

    
154
    public void setCancelButtonText(String text) {
155
        wizardPanelWithLogo.getWizardComponents().getCancelButton()
156
            .setText(text);
157
    }
158

    
159
    /**
160
     * @return the uimanager
161
     */
162
    public DefaultExporttoSwingManager getExporttoSwingManager() {
163
        return uimanager;
164
    }
165

    
166
    /**
167
     * @param exporttoSwingProvider
168
     * @throws ServiceException
169
     */
170
    public void selectExporttoSwingProvider(
171
        ExporttoSwingProviderFactory provider)
172
        throws ServiceException {
173
        exporttoSwingProvider =
174
            EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider(
175
                provider.getName(), featureStore, projection);
176

    
177
        DefaultJWizardComponents wizardComponents =
178
            wizardPanelWithLogo.getWizardComponents();
179
        for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) {
180
            wizardComponents.removeWizardPanel(i);
181
        }
182
        for (int i = 0; i < exporttoSwingProvider.getPanelCount(); i++) {
183
            wizardPanelWithLogo.addOptionPanel(new DefaultWizardContainer(
184
                exporttoSwingProvider.getPanelAt(i)));
185
        }
186
        wizardPanelWithLogo.addOptionPanel(exportFilterWizard);
187
        wizardPanelWithLogo.addOptionPanel(exporttoProgressWizard);
188
    }
189

    
190
    public void export() throws DataException, ExporttoServiceException {
191
        this.lastWizard();
192

    
193
        // Create the feature Set...
194
        FeatureSet featureSet = null;
195
        if (exportFilterWizard.isFullLayerSelected()) {
196
            featureSet = featureStore.getFeatureSet();
197
        } else
198
            if (exportFilterWizard.isSelectedFeaturesSelected()) {
199
                featureSet = (FeatureSet) featureStore.getSelection();
200
            } else {
201
                FeatureQuery featureQuery = featureStore.createFeatureQuery();
202
                Evaluator evaluator =
203
                    DATA_MANAGER.createExpresion(exportFilterWizard
204
                        .getFreeFilter());
205
                featureQuery.setFilter(evaluator);
206
                featureSet = featureStore.getFeatureSet(featureQuery);
207
            }
208

    
209
        Export export = new Export(featureSet);
210
        export.start();
211
    }
212

    
213
    public void lastWizard() {
214
        this.wizardPanelWithLogo.getWizardComponents().getNextButton()
215
            .getActionListeners()[0].actionPerformed(null);
216
    }
217

    
218
    private class Export extends Thread {
219

    
220
        private FeatureSet featureSet;
221

    
222
        public Export(FeatureSet featureSet) {
223
            super();
224
            this.featureSet = featureSet;
225
        }
226

    
227
        public synchronized void run() {
228
            ExporttoService exportService =
229
                exporttoSwingProvider.createExporttoService();
230
            exporttoProgressWizard.bind(exportService.getTaskStatus());
231
            exportService.setFinishAction(exporttoServiceFinishAction);
232
            try {
233
                exportService.export(featureSet);
234
            } catch (ExporttoServiceException e) {
235
                LOG.error("Error exporting the store", e);
236
                showError(e, exportService.getTaskStatus());
237
            }
238
        }
239
    }
240

    
241
    private void showError(ExporttoServiceException e, TaskStatus taskStatus) {
242
        String message = e.getMessage();
243
        if (e.getCause() != null) {
244
            message = e.getCause().getMessage();
245
        }
246
        JOptionPane.showMessageDialog(exporttoProgressWizard, message);
247
    }
248

    
249
    public void setWizardPanelActionListener(
250
        WizardPanelActionListener wizardActionListener) {
251
        this.exporttoServicePanelListener = wizardActionListener;
252
    }
253

    
254
    public WizardPanelActionListener getWizardPanelActionListener() {
255
        if (exporttoServicePanelListener == null) {
256
            exporttoServicePanelListener =
257
                new DefaultJExporttoServicePanelListener(this);
258
        }
259
        return exporttoServicePanelListener;
260
    }
261

    
262
    @Override
263
    public void setExporttoServicePanelListener(
264
        JExporttoServicePanelListener exporttoServicePanelListener) {
265
        ((DefaultJExporttoServicePanelListener) getWizardPanelActionListener())
266
            .setExporttoServicePanelListener(exporttoServicePanelListener);
267
    }
268

    
269
    @Override
270
    public int getStatus() {
271
        return this.status;
272
    }
273

    
274
    public void setStatus(int status) {
275
        this.status = status;
276
    }
277
}