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 / exportto / swing / impl / DefaultJExporttoServicePanel.java @ 41437

History | View | Annotate | Download (11 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.exportto.swing.impl;
25

    
26
import java.awt.BorderLayout;
27
import java.net.URL;
28

    
29
import javax.swing.ImageIcon;
30
import javax.swing.JOptionPane;
31

    
32
import jwizardcomponent.DefaultJWizardComponents;
33

    
34
import org.cresques.cts.IProjection;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

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

    
66
/**
67
 * Default implementation for the {@link JExporttoServicePanel}.
68
 * 
69
 * @author gvSIG Team
70
 * @version $Id$
71
 */
72
public class DefaultJExporttoServicePanel extends JExporttoServicePanel
73
    implements WizardPanel {
74

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

    
78
    private static final ExporttoSwingProviderManager EXPORTTO_SWING_PROVIDER_MANAGER =
79
        ExporttoSwingProviderLocator.getManager();
80
    private static final DataManager DATA_MANAGER = DALLocator.getDataManager();
81

    
82
    private static final long serialVersionUID = -7026467582252285238L;
83

    
84
    private FeatureStore featureStore;
85
    private IProjection projection;
86
    private FLyrVect vectorLayer = null;
87
    private DefaultExporttoSwingManager uimanager;
88

    
89
    // Used to get the new feature store parameters
90
    private ExporttoSwingProvider exporttoSwingProvider;
91

    
92
    // Provider types that will be displayed
93
    private int[] providerTypes;
94

    
95
    // Wizards used to create the main wizard
96
    private ExporterSelectionWizard exporterSelectionWizard = null;
97
    private ExportFilterWizard exportFilterWizard = null;
98
    private ExporttoProgressWizard exporttoProgressWizard = null;
99

    
100
    // Listener for the finish and cancell button
101
    private WizardPanelActionListener exporttoServicePanelListener;
102

    
103
    private WizardPanelWithLogo wizardPanelWithLogo = null;
104

    
105
    // Action to execute at the end of the export process
106
    ExporttoServiceFinishAction exporttoServiceFinishAction;
107

    
108
    private int status;
109

    
110
    public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager,
111
        FLyrVect vlayer,
112
        ExporttoServiceFinishAction exporttoServiceFinishAction,
113
        int[] providerTypes) {
114
        
115
        this(
116
            uimanager,
117
            vlayer.getFeatureStore(),
118
            vlayer.getProjection(),
119
            exporttoServiceFinishAction,
120
            providerTypes);
121
        vectorLayer = vlayer;
122
    }
123
    
124
    public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager,
125
        FeatureStore featureStore, IProjection projection,
126
        ExporttoServiceFinishAction exporttoServiceFinishAction,
127
        int[] providerTypes) {
128
        this.featureStore = featureStore;
129
        this.projection = projection;
130
        this.uimanager = uimanager;
131
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
132
        this.providerTypes = providerTypes;
133
        this.status = JOptionPane.UNDEFINED_CONDITION;
134

    
135
        ImageIcon ii = IconThemeHelper.getImageIcon("wizard-export-to"); 
136
        wizardPanelWithLogo = new WizardPanelWithLogo(ii);
137

    
138
        // Initialize the wizards
139
        exporterSelectionWizard =
140
            new ExporterSelectionWizard(this, providerTypes);
141
        exportFilterWizard = new ExportFilterWizard(this);
142
        exporttoProgressWizard = new ExporttoProgressWizard(this);
143

    
144
        // Adding the wizards to the main wizard
145
        wizardPanelWithLogo.addOptionPanel(exporterSelectionWizard);
146

    
147
        // The finish button is called "export"
148
        this.wizardPanelWithLogo.getWizardComponents().getFinishButton()
149
            .setText(uimanager.getTranslation("export"));
150

    
151
        this.setLayout(new BorderLayout());
152
        this.add(wizardPanelWithLogo, BorderLayout.CENTER);
153

    
154
        // Disable the nextbutton
155
        setNextButtonEnabled(false);
156
        setExportButtonEnabled(false);
157

    
158
        // Set the listener for the finish and cancell events
159
        this.wizardPanelWithLogo.setWizardListener(this);
160
    }
161

    
162
    public void setNextButtonEnabled(boolean isEnabled) {
163
        wizardPanelWithLogo.setNextButtonEnabled(isEnabled);
164
    }
165

    
166
    public void setExportButtonEnabled(boolean isEnabled) {
167
        wizardPanelWithLogo.setFinishButtonEnabled(isEnabled);
168
    }
169

    
170
    public void setCancelButtonText(String text) {
171
        wizardPanelWithLogo.getWizardComponents().getCancelButton()
172
            .setText(text);
173
    }
174

    
175
    /**
176
     * @return the uimanager
177
     */
178
    public DefaultExporttoSwingManager getExporttoSwingManager() {
179
        return uimanager;
180
    }
181

    
182
    /**
183
     * @param exporttoSwingProvider
184
     * @throws ServiceException
185
     */
186
    public void selectExporttoSwingProvider(
187
        ExporttoSwingProviderFactory provider)
188
        throws ServiceException {
189
        
190
        if (vectorLayer == null) {
191
            exporttoSwingProvider =
192
                EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider(
193
                    provider.getName(), featureStore, projection);
194
        } else {
195
            exporttoSwingProvider =
196
                EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider(
197
                    provider.getName(), vectorLayer);
198
        }
199

    
200
        DefaultJWizardComponents wizardComponents =
201
            wizardPanelWithLogo.getWizardComponents();
202
        for (int i = wizardComponents.getWizardPanelList().size() - 1; i >= 1; i--) {
203
            wizardComponents.removeWizardPanel(i);
204
        }
205
        for (int i = 0; i < exporttoSwingProvider.getPanelCount(); i++) {
206
            wizardPanelWithLogo.addOptionPanel(new DefaultWizardContainer(
207
                exporttoSwingProvider.getPanelAt(i)));
208
        }
209
        wizardPanelWithLogo.addOptionPanel(exportFilterWizard);
210
        wizardPanelWithLogo.addOptionPanel(exporttoProgressWizard);
211
    }
212

    
213
    public void export() throws DataException, ExporttoServiceException {
214
        this.lastWizard();
215

    
216
        // Create the feature Set...
217
        FeatureSet featureSet = null;
218
        if (exportFilterWizard.isFullLayerSelected()) {
219
            featureSet = featureStore.getFeatureSet();
220
        } else
221
            if (exportFilterWizard.isSelectedFeaturesSelected()) {
222
                featureSet = (FeatureSet) featureStore.getSelection();
223
            } else {
224
                FeatureQuery featureQuery = featureStore.createFeatureQuery();
225
                Evaluator evaluator =
226
                    DATA_MANAGER.createExpresion(exportFilterWizard
227
                        .getFreeFilter());
228
                featureQuery.setFilter(evaluator);
229
                featureSet = featureStore.getFeatureSet(featureQuery);
230
            }
231

    
232
        Export export = new Export(featureSet);
233
        export.start();
234
    }
235

    
236
    public void lastWizard() {
237
        this.wizardPanelWithLogo.getWizardComponents().getNextButton()
238
            .getActionListeners()[0].actionPerformed(null);
239
    }
240

    
241
    private class Export extends Thread {
242

    
243
        private FeatureSet featureSet;
244

    
245
        public Export(FeatureSet featureSet) {
246
            super();
247
            this.featureSet = featureSet;
248
        }
249

    
250
        public synchronized void run() {
251
            ExporttoService exportService =
252
                exporttoSwingProvider.createExporttoService();
253
            exporttoProgressWizard.bind(exportService.getTaskStatus());
254
            exportService.setFinishAction(exporttoServiceFinishAction);
255
            try {
256
                exportService.export(featureSet);
257
            } catch (ExporttoServiceException e) {
258
                LOG.info("Error exporting the store", e);
259
                showError(e, exportService.getTaskStatus());
260
            }
261
        }
262
    }
263

    
264
    private void showError(ExporttoServiceException e, TaskStatus taskStatus) {
265
        String message = e.getMessage();        
266
//        if (e.getCause() != null) {
267
//            message = e.getCause().getMessage();
268
//        }
269
        message = e.getMessageStack();
270
        
271
        if (message != null && message.startsWith("_")) {
272
            message = Messages.getText(message);
273
        }
274
        JOptionPane.showMessageDialog(exporttoProgressWizard, message);
275
    }
276

    
277
    public void setWizardPanelActionListener(
278
        WizardPanelActionListener wizardActionListener) {
279
        this.exporttoServicePanelListener = wizardActionListener;
280
    }
281

    
282
    public WizardPanelActionListener getWizardPanelActionListener() {
283
        if (exporttoServicePanelListener == null) {
284
            exporttoServicePanelListener =
285
                new DefaultJExporttoServicePanelListener(this);
286
        }
287
        return exporttoServicePanelListener;
288
    }
289

    
290
    @Override
291
    public void setExporttoServicePanelListener(
292
        JExporttoServicePanelListener exporttoServicePanelListener) {
293
        ((DefaultJExporttoServicePanelListener) getWizardPanelActionListener())
294
            .setExporttoServicePanelListener(exporttoServicePanelListener);
295
    }
296

    
297
    @Override
298
    public int getStatus() {
299
        return this.status;
300
    }
301

    
302
    public void setStatus(int status) {
303
        this.status = status;
304
    }
305
}