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

History | View | Annotate | Download (9.44 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.ExporttoSwingProviderLocator;
47
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderManager;
48
import org.gvsig.fmap.dal.DALLocator;
49
import org.gvsig.fmap.dal.DataManager;
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.feature.FeatureQuery;
52
import org.gvsig.fmap.dal.feature.FeatureSet;
53
import org.gvsig.fmap.dal.feature.FeatureStore;
54
import org.gvsig.gui.beans.wizard.WizardPanel;
55
import org.gvsig.gui.beans.wizard.WizardPanelActionListener;
56
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
57
import org.gvsig.tools.evaluator.Evaluator;
58
import org.gvsig.tools.service.ServiceException;
59
import org.gvsig.tools.task.TaskStatus;
60

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

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

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

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

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

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

    
86
    //Wizards used to create the main wizard
87
    private ExporterSelectionWizard exporterSelectionWizard = null;
88
    private ExportFilterWizard exportFilterWizard = null;
89
    private ExporttoProgressWizard exporttoProgressWizard = null;
90

    
91
    //Listener for the finish and cancell button
92
    private WizardPanelActionListener exporttoServicePanelListener;
93

    
94
    private WizardPanelWithLogo wizardPanelWithLogo = null;
95

    
96
    //Action to execute at the end of the export process
97
    ExporttoServiceFinishAction exporttoServiceFinishAction;
98

    
99
    public DefaultJExporttoServicePanel(DefaultExporttoSwingManager uimanager, FeatureStore featureStore, IProjection projection,
100
        ExporttoServiceFinishAction exporttoServiceFinishAction) {
101
        this.featureStore = featureStore;
102
        this.projection = projection;
103
        this.uimanager = uimanager;
104
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
105

    
106
        URL iconURL = getClass().getClassLoader().getResource("org/gvsig/exportto/swing/impl/images/exporttoicon.png");
107
        ImageIcon icon = new ImageIcon(iconURL);
108
        wizardPanelWithLogo = new WizardPanelWithLogo(icon);       
109

    
110
        //Initialize the wizards
111
        exporterSelectionWizard = new ExporterSelectionWizard(this);
112
        exportFilterWizard = new ExportFilterWizard(this);
113
        exporttoProgressWizard = new ExporttoProgressWizard(this);
114

    
115
        //Adding the wizards to the main wizard
116
        wizardPanelWithLogo.addOptionPanel(exporterSelectionWizard);
117

    
118
        //The finish button is called "export"
119
        this.wizardPanelWithLogo.getWizardComponents().getFinishButton().setText(uimanager.getTranslation("export"));
120

    
121
        this.setLayout(new BorderLayout());
122
        this.add(wizardPanelWithLogo, BorderLayout.CENTER); 
123

    
124
        //Disable the nextbutton
125
        setNextButtonEnabled(false);
126
        setExportButtonEnabled(false);
127

    
128
        //Set the listener for the finish and cancell events
129
        this.wizardPanelWithLogo.setWizardListener(this);
130
    }
131

    
132
    public void setNextButtonEnabled(boolean isEnabled) {
133
        wizardPanelWithLogo.setNextButtonEnabled(isEnabled);
134
    }
135

    
136
    public void setExportButtonEnabled(boolean isEnabled) {
137
        wizardPanelWithLogo.setFinishButtonEnabled(isEnabled);
138
    } 
139

    
140
    public void setCancelButtonText(String text){
141
        wizardPanelWithLogo.getWizardComponents().getCancelButton().setText(text);
142
    }
143

    
144

    
145
    /**
146
     * @return the uimanager
147
     */
148
    public DefaultExporttoSwingManager getExporttoSwingManager() {
149
        return uimanager;
150
    }
151

    
152

    
153
    /**
154
     * @param exporttoSwingProvider
155
     * @throws ServiceException 
156
     */
157
    public void selectExporttoSwingProvider(String providerName) throws ServiceException {        
158
        exporttoSwingProvider = 
159
            EXPORTTO_SWING_PROVIDER_MANAGER.createExporttoSwingProvider(providerName, featureStore, projection);
160

    
161
        DefaultJWizardComponents wizardComponents = wizardPanelWithLogo.getWizardComponents();              
162
        for (int i=wizardComponents.getWizardPanelList().size()-1 ; i>=1 ; i--){
163
            wizardComponents.removeWizardPanel(i);
164
        }
165
        for (int i=0 ; i<exporttoSwingProvider.getPanelCount() ; i++){
166
            wizardPanelWithLogo.addOptionPanel(new DefaultWizardContainer(exporttoSwingProvider.getPanelAt(i)));
167
        } 
168
        wizardPanelWithLogo.addOptionPanel(exportFilterWizard);     
169
        wizardPanelWithLogo.addOptionPanel(exporttoProgressWizard);
170
    }
171

    
172
    public void export() throws DataException, ExporttoServiceException{
173
        this.lastWizard();
174

    
175
        //Create the feature Set...
176
        FeatureSet featureSet = null;
177
        if (exportFilterWizard.isFullLayerSelected()){
178
            featureSet = featureStore.getFeatureSet();
179
        }else if (exportFilterWizard.isSelectedFeaturesSelected()){
180
            featureSet = (FeatureSet)featureStore.getSelection();
181
        }else{
182
            FeatureQuery featureQuery = featureStore.createFeatureQuery();
183
            Evaluator evaluator = DATA_MANAGER.createExpresion(exportFilterWizard.getFreeFilter());
184
            featureQuery.setFilter(evaluator);            
185
            featureSet = featureStore.getFeatureSet(featureQuery);
186
        }
187

    
188
        Export export = new Export(featureSet);
189
        export.start();
190
    } 
191

    
192
    public void lastWizard() {
193
        this.wizardPanelWithLogo.getWizardComponents().getNextButton().getActionListeners()[0].actionPerformed(null);
194
    }
195

    
196
    private class Export extends Thread{
197
        private FeatureSet featureSet;
198

    
199
        public Export(FeatureSet featureSet) {
200
            super();
201
            this.featureSet = featureSet;
202
        }
203

    
204
        public synchronized void run() {
205
            ExporttoService exportService = exporttoSwingProvider.createExporttoService();
206
            exporttoProgressWizard.bind(exportService.getTaskStatus());
207
            exportService.setFinishAction(exporttoServiceFinishAction);
208
            try {             
209
                exportService.export(featureSet);                
210
            } catch (ExporttoServiceException e) {             
211
                LOG.error("Error exporting the store", e);
212
                showError(e, exportService.getTaskStatus());              
213
            }
214
        }
215
    }
216

    
217
    private void showError(ExporttoServiceException e, TaskStatus taskStatus){        
218
        String message = e.getMessage();
219
        if (e.getCause() != null){
220
            message = e.getCause().getMessage();
221
        }
222
        JOptionPane.showMessageDialog(exporttoProgressWizard, message);
223
    }
224

    
225
    public void setWizardPanelActionListener(WizardPanelActionListener wizardActionListener) {
226
        this.exporttoServicePanelListener = wizardActionListener;    
227
    }
228

    
229
    public WizardPanelActionListener getWizardPanelActionListener() {
230
        if (exporttoServicePanelListener == null) {
231
            exporttoServicePanelListener = new DefaultJExporttoServicePanelListener(this);
232
        }
233
        return exporttoServicePanelListener;
234
    }
235

    
236
    @Override
237
    public void setExporttoServicePanelListener(
238
        JExporttoServicePanelListener exporttoServicePanelListener) {
239
        ((DefaultJExporttoServicePanelListener)getWizardPanelActionListener()).
240
        setExporttoServicePanelListener(exporttoServicePanelListener);
241
    }
242
}