Statistics
| Revision:

root / 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 @ 38609

History | View | Annotate | Download (9.96 KB)

1 34981 jpiera
/* 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 37887 cordinyana
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderFactory;
47 34981 jpiera
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 37780 cordinyana
public class DefaultJExporttoServicePanel extends JExporttoServicePanel
69
    implements WizardPanel {
70 34981 jpiera
71 37780 cordinyana
    private static final Logger LOG = LoggerFactory
72
        .getLogger(DefaultJExporttoServicePanel.class);
73 34981 jpiera
74 37780 cordinyana
    private static final ExporttoSwingProviderManager EXPORTTO_SWING_PROVIDER_MANAGER =
75 34981 jpiera
        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 37780 cordinyana
    // Used to get the new feature store parameters
85 34981 jpiera
    private ExporttoSwingProvider exporttoSwingProvider;
86 37780 cordinyana
87
    // Provider types that will be displayed
88 35745 jpiera
    private int[] providerTypes;
89 34981 jpiera
90 37780 cordinyana
    // Wizards used to create the main wizard
91 34981 jpiera
    private ExporterSelectionWizard exporterSelectionWizard = null;
92
    private ExportFilterWizard exportFilterWizard = null;
93
    private ExporttoProgressWizard exporttoProgressWizard = null;
94
95 37780 cordinyana
    // Listener for the finish and cancell button
96 34981 jpiera
    private WizardPanelActionListener exporttoServicePanelListener;
97
98
    private WizardPanelWithLogo wizardPanelWithLogo = null;
99
100 37780 cordinyana
    // Action to execute at the end of the export process
101 34981 jpiera
    ExporttoServiceFinishAction exporttoServiceFinishAction;
102 37144 fdiaz
103 37780 cordinyana
    private int status;
104
105
    public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager,
106
        FeatureStore featureStore, IProjection projection,
107
        ExporttoServiceFinishAction exporttoServiceFinishAction,
108
        int[] providerTypes) {
109 34981 jpiera
        this.featureStore = featureStore;
110
        this.projection = projection;
111
        this.uimanager = uimanager;
112
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
113 35745 jpiera
        this.providerTypes = providerTypes;
114 37144 fdiaz
        this.status = JOptionPane.UNDEFINED_CONDITION;
115 34981 jpiera
116 38608 jjdelcerro
        wizardPanelWithLogo = new WizardPanelWithLogo(IconThemeHelper.getImageIcon("wizard-export-to"));
117 34981 jpiera
118 37780 cordinyana
        // Initialize the wizards
119
        exporterSelectionWizard =
120
            new ExporterSelectionWizard(this, providerTypes);
121 34981 jpiera
        exportFilterWizard = new ExportFilterWizard(this);
122
        exporttoProgressWizard = new ExporttoProgressWizard(this);
123
124 37780 cordinyana
        // Adding the wizards to the main wizard
125 34981 jpiera
        wizardPanelWithLogo.addOptionPanel(exporterSelectionWizard);
126
127 37780 cordinyana
        // The finish button is called "export"
128
        this.wizardPanelWithLogo.getWizardComponents().getFinishButton()
129
            .setText(uimanager.getTranslation("export"));
130 34981 jpiera
131
        this.setLayout(new BorderLayout());
132 37780 cordinyana
        this.add(wizardPanelWithLogo, BorderLayout.CENTER);
133 34981 jpiera
134 37780 cordinyana
        // Disable the nextbutton
135 34981 jpiera
        setNextButtonEnabled(false);
136
        setExportButtonEnabled(false);
137
138 37780 cordinyana
        // Set the listener for the finish and cancell events
139 34981 jpiera
        this.wizardPanelWithLogo.setWizardListener(this);
140
    }
141
142
    public void setNextButtonEnabled(boolean isEnabled) {
143
        wizardPanelWithLogo.setNextButtonEnabled(isEnabled);
144
    }
145
146
    public void setExportButtonEnabled(boolean isEnabled) {
147
        wizardPanelWithLogo.setFinishButtonEnabled(isEnabled);
148 37780 cordinyana
    }
149 34981 jpiera
150 37780 cordinyana
    public void setCancelButtonText(String text) {
151
        wizardPanelWithLogo.getWizardComponents().getCancelButton()
152
            .setText(text);
153 34981 jpiera
    }
154
155
    /**
156
     * @return the uimanager
157
     */
158
    public DefaultExporttoSwingManager getExporttoSwingManager() {
159
        return uimanager;
160
    }
161
162
    /**
163
     * @param exporttoSwingProvider
164 37780 cordinyana
     * @throws ServiceException
165 34981 jpiera
     */
166 37887 cordinyana
    public void selectExporttoSwingProvider(
167
        ExporttoSwingProviderFactory provider)
168 37780 cordinyana
        throws ServiceException {
169
        exporttoSwingProvider =
170
            EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider(
171 37887 cordinyana
                provider.getName(), featureStore, projection);
172 34981 jpiera
173 37780 cordinyana
        DefaultJWizardComponents wizardComponents =
174
            wizardPanelWithLogo.getWizardComponents();
175
        for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) {
176 34981 jpiera
            wizardComponents.removeWizardPanel(i);
177
        }
178 37780 cordinyana
        for (int i = 0; i < exporttoSwingProvider.getPanelCount(); i++) {
179
            wizardPanelWithLogo.addOptionPanel(new DefaultWizardContainer(
180
                exporttoSwingProvider.getPanelAt(i)));
181
        }
182
        wizardPanelWithLogo.addOptionPanel(exportFilterWizard);
183 34981 jpiera
        wizardPanelWithLogo.addOptionPanel(exporttoProgressWizard);
184
    }
185
186 37780 cordinyana
    public void export() throws DataException, ExporttoServiceException {
187 34981 jpiera
        this.lastWizard();
188
189 37780 cordinyana
        // Create the feature Set...
190 34981 jpiera
        FeatureSet featureSet = null;
191 37780 cordinyana
        if (exportFilterWizard.isFullLayerSelected()) {
192 34981 jpiera
            featureSet = featureStore.getFeatureSet();
193 37780 cordinyana
        } else
194
            if (exportFilterWizard.isSelectedFeaturesSelected()) {
195
                featureSet = (FeatureSet) featureStore.getSelection();
196
            } else {
197
                FeatureQuery featureQuery = featureStore.createFeatureQuery();
198
                Evaluator evaluator =
199
                    DATA_MANAGER.createExpresion(exportFilterWizard
200
                        .getFreeFilter());
201
                featureQuery.setFilter(evaluator);
202
                featureSet = featureStore.getFeatureSet(featureQuery);
203
            }
204 34981 jpiera
205
        Export export = new Export(featureSet);
206
        export.start();
207 37780 cordinyana
    }
208 34981 jpiera
209
    public void lastWizard() {
210 37780 cordinyana
        this.wizardPanelWithLogo.getWizardComponents().getNextButton()
211
            .getActionListeners()[0].actionPerformed(null);
212 34981 jpiera
    }
213
214 37780 cordinyana
    private class Export extends Thread {
215
216 34981 jpiera
        private FeatureSet featureSet;
217
218
        public Export(FeatureSet featureSet) {
219
            super();
220
            this.featureSet = featureSet;
221
        }
222
223
        public synchronized void run() {
224 37780 cordinyana
            ExporttoService exportService =
225
                exporttoSwingProvider.createExporttoService();
226 34981 jpiera
            exporttoProgressWizard.bind(exportService.getTaskStatus());
227
            exportService.setFinishAction(exporttoServiceFinishAction);
228 37780 cordinyana
            try {
229
                exportService.export(featureSet);
230
            } catch (ExporttoServiceException e) {
231 34981 jpiera
                LOG.error("Error exporting the store", e);
232 37780 cordinyana
                showError(e, exportService.getTaskStatus());
233 34981 jpiera
            }
234
        }
235
    }
236
237 37780 cordinyana
    private void showError(ExporttoServiceException e, TaskStatus taskStatus) {
238 34981 jpiera
        String message = e.getMessage();
239 37780 cordinyana
        if (e.getCause() != null) {
240 34981 jpiera
            message = e.getCause().getMessage();
241
        }
242
        JOptionPane.showMessageDialog(exporttoProgressWizard, message);
243
    }
244
245 37780 cordinyana
    public void setWizardPanelActionListener(
246
        WizardPanelActionListener wizardActionListener) {
247
        this.exporttoServicePanelListener = wizardActionListener;
248 34981 jpiera
    }
249
250
    public WizardPanelActionListener getWizardPanelActionListener() {
251
        if (exporttoServicePanelListener == null) {
252 37780 cordinyana
            exporttoServicePanelListener =
253
                new DefaultJExporttoServicePanelListener(this);
254 34981 jpiera
        }
255
        return exporttoServicePanelListener;
256
    }
257
258
    @Override
259
    public void setExporttoServicePanelListener(
260
        JExporttoServicePanelListener exporttoServicePanelListener) {
261 37780 cordinyana
        ((DefaultJExporttoServicePanelListener) getWizardPanelActionListener())
262
            .setExporttoServicePanelListener(exporttoServicePanelListener);
263 34981 jpiera
    }
264 37780 cordinyana
265 37144 fdiaz
    @Override
266
    public int getStatus() {
267 37780 cordinyana
        return this.status;
268 37144 fdiaz
    }
269 37780 cordinyana
270 37144 fdiaz
    public void setStatus(int status) {
271 37780 cordinyana
        this.status = status;
272 37144 fdiaz
    }
273 34981 jpiera
}