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 / DefaultExporttoSwingProviderManager.java @ 40931

History | View | Annotate | Download (12.8 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.util.ArrayList;
27
import java.util.HashMap;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.Map;
31

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

    
37
import org.gvsig.exportto.swing.impl.preferences.DefaultExporttoPreferencesComponent;
38
import org.gvsig.exportto.swing.preferences.ExporttoSwingPreferencesComponent;
39
import org.gvsig.exportto.swing.spi.ExporttoSwingProvider;
40
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderFactory;
41
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderManager;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.dynobject.DynObject;
46
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
47
import org.gvsig.tools.extensionpoint.ExtensionPoint;
48
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
49
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
50
import org.gvsig.tools.service.Service;
51
import org.gvsig.tools.service.ServiceException;
52
import org.gvsig.tools.service.spi.AbstractProviderManager;
53
import org.gvsig.tools.service.spi.Provider;
54
import org.gvsig.tools.service.spi.ProviderFactory;
55
import org.gvsig.tools.service.spi.ProviderServices;
56

    
57
/**
58
 * @author gvSIG Team
59
 * @version $Id$
60
 * 
61
 */
62
public class DefaultExporttoSwingProviderManager extends
63
    AbstractProviderManager implements ExporttoSwingProviderManager {
64

    
65
    private static final Logger LOG = LoggerFactory
66
        .getLogger(DefaultExporttoSwingProviderManager.class);
67

    
68
    private static final String PROVIDERS_NAME = "Exportto.swing.providers";
69
    private static final String PROVIDERS_DESCRIPTION =
70
        "Exportto swing providers";
71

    
72
    private static final String PARAMETER_FEATURESTORE = "FeatureStore";
73
    private static final String PARAMETER_PROJECTION = "Projection";
74
    private static final String PARAMETER_LAYER = "FLyrVect";
75

    
76
    private Map<ExporttoSwingProviderFactory, Boolean> providerStatus;
77

    
78
    public DefaultExporttoSwingProviderManager() {
79
        super();
80
        providerStatus = new HashMap<ExporttoSwingProviderFactory, Boolean>();
81
    }
82

    
83
    public ProviderServices createProviderServices(Service service) {
84
        return new DefaultExporttoSwingProviderServices();
85
    }
86

    
87
    public List<String> getProviderNames() {
88
        return getProviderNames(new int[0]);
89
    }
90

    
91
    public List<String> getProviderNames(int[] providerTypes) {
92
        ExtensionPointManager extensionPointManager =
93
            ToolsLocator.getExtensionPointManager();
94
        ExtensionPoint extensionPoint =
95
            extensionPointManager.get(this.getRegistryKey());
96
        List<String> providers = new ArrayList<String>();
97
        if (extensionPoint != null) {
98
            @SuppressWarnings("unchecked")
99
            Iterator<Extension> it = extensionPoint.iterator();
100
            while (it.hasNext()) {
101
                Extension extension = it.next();
102
                try {
103
                    ExporttoSwingProviderFactory exporttoSwingProviderFactory =
104
                        (ExporttoSwingProviderFactory) extension.create();
105
                    if (providerTypes.length == 0) {
106
                        providers.add(exporttoSwingProviderFactory.getName());
107
                    } else {
108
                        boolean areSupported = true;
109
                        for (int i = 0; i < providerTypes.length; i++) {
110
                            if (!exporttoSwingProviderFactory
111
                                .support(providerTypes[i])) {
112
                                areSupported = false;
113
                                break;
114
                            }
115
                        }
116
                        if (areSupported) {
117
                            providers.add(exporttoSwingProviderFactory
118
                                .getName());
119
                        }
120
                    }
121
                } catch (InstantiationException e) {
122
                    LOG.error(
123
                        "Not possible to create the exportto provider factory",
124
                        e);
125
                } catch (IllegalAccessException e) {
126
                    LOG.error(
127
                        "Not possible to create the exportto provider factory",
128
                        e);
129
                } catch (ServiceException e) {
130
                    LOG.error(
131
                        "Not possible to check if the provider support a format",
132
                        e);
133
                }
134
            }
135
        }
136
        return providers;
137
    }
138

    
139
    public List<ExporttoSwingProviderFactory> getProviderFactories() {
140
        return getProviderFactories(null);
141
    }
142

    
143
    public List<ExporttoSwingProviderFactory> getProviderFactories(
144
        int[] providerTypes) {
145
        ExtensionPointManager extensionPointManager =
146
            ToolsLocator.getExtensionPointManager();
147
        ExtensionPoint extensionPoint =
148
            extensionPointManager.get(this.getRegistryKey());
149
        List<ExporttoSwingProviderFactory> providers =
150
            new ArrayList<ExporttoSwingProviderFactory>();
151
        if (extensionPoint != null) {
152
            @SuppressWarnings("unchecked")
153
            Iterator<Extension> it = extensionPoint.iterator();
154
            while (it.hasNext()) {
155
                Extension extension = it.next();
156
                try {
157
                    ExporttoSwingProviderFactory exporttoSwingProviderFactory =
158
                        (ExporttoSwingProviderFactory) extension.create();
159
                    if (providerTypes == null || providerTypes.length == 0) {
160
                        providers.add(exporttoSwingProviderFactory);
161
                    } else {
162
                        boolean areSupported = true;
163
                        for (int i = 0; i < providerTypes.length; i++) {
164
                            if (!exporttoSwingProviderFactory
165
                                .support(providerTypes[i])) {
166
                                areSupported = false;
167
                                break;
168
                            }
169
                        }
170
                        if (areSupported) {
171
                            providers.add(exporttoSwingProviderFactory);
172
                        }
173
                    }
174
                } catch (InstantiationException e) {
175
                    LOG.error(
176
                        "Not possible to create the exportto provider factory",
177
                        e);
178
                } catch (IllegalAccessException e) {
179
                    LOG.error(
180
                        "Not possible to create the exportto provider factory",
181
                        e);
182
                } catch (ServiceException e) {
183
                    LOG.error(
184
                        "Not possible to check if the provider support a format",
185
                        e);
186
                }
187
            }
188
        }
189
        return providers;
190
    }
191

    
192
    protected String getRegistryDescription() {
193
        return PROVIDERS_DESCRIPTION;
194
    }
195

    
196
    protected String getRegistryKey() {
197
        return PROVIDERS_NAME;
198
    }
199

    
200
    public ExporttoSwingProvider createExporttoSwingProvider(
201
        String providerName, FeatureStore featureStore, IProjection projection)
202
        throws ServiceException {
203
        DynObject serviceParameters = createServiceParameters(providerName);
204
        serviceParameters.setDynValue(PARAMETER_FEATURESTORE, featureStore);
205
        try {
206
            serviceParameters.setDynValue(PARAMETER_PROJECTION, projection);
207
        } catch (DynFieldNotFoundException e) {
208
            LOG.info("This provider doesn't accept projection", e);
209
        }
210
        Provider provider =
211
            createProvider(serviceParameters,
212
                new DefaultExporttoSwingProviderServices());
213
        if (!(provider instanceof ExporttoSwingProvider)) {
214
            throw new NotExporttoSwingProviderException(providerName);
215
        }
216
        return (ExporttoSwingProvider) provider;
217
    }
218
    
219
    
220
    public ExporttoSwingProvider createExporttoSwingProvider(
221
        String providerName, FLyrVect vlayer)
222
        throws ServiceException {
223
        
224
        FeatureStore fstore = vlayer.getFeatureStore();
225
        
226
        IProjection proj = null;
227
        ICoordTrans ct = vlayer.getCoordTrans();
228
        if (ct == null) {
229
            // Layer is not reprojected on the fly
230
            proj = vlayer.getProjection(); 
231
        } else {
232
            /*
233
             * If there is on-the-fly reprojection, then
234
             * the projection of the export process is the
235
             * destination projection of the transformation:
236
             */
237
            proj = ct.getPDest();
238
        }
239
            
240
        DynObject serviceParameters = createServiceParameters(providerName);
241
        serviceParameters.setDynValue(PARAMETER_FEATURESTORE, fstore);
242
        try {
243
            serviceParameters.setDynValue(PARAMETER_PROJECTION, proj);
244
        } catch (DynFieldNotFoundException e) {
245
            LOG.info("This provider doesn't accept projection", e);
246
        }
247
        try {
248
            serviceParameters.setDynValue(PARAMETER_LAYER, vlayer);
249
        } catch (DynFieldNotFoundException e) {
250
            LOG.info("This provider doesn't accept layer", e);
251
        }
252
        // ===========================================================
253
        Provider provider =
254
            createProvider(serviceParameters,
255
                new DefaultExporttoSwingProviderServices());
256
        if (!(provider instanceof ExporttoSwingProvider)) {
257
            throw new NotExporttoSwingProviderException(providerName);
258
        }
259
        return (ExporttoSwingProvider) provider;
260
    }
261

    
262
    
263
    
264
    
265

    
266
    public boolean support(String providerName, int providerType)
267
        throws ServiceException {
268
        ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
269
        ExtensionPoint ep = epm.get(this.getRegistryKey());
270
        ExporttoSwingProviderFactory exporttoSwingProviderFactory = null;
271

    
272
        if (ep != null) {
273
            try {
274
                exporttoSwingProviderFactory =
275
                    ((ExporttoSwingProviderFactory) ep.create(providerName));
276
                return exporttoSwingProviderFactory.support(providerType);
277
            } catch (InstantiationException e) {
278
                LOG.error(
279
                    "Not possible to create the exportto provider factory", e);
280
            } catch (IllegalAccessException e) {
281
                LOG.error(
282
                    "Not possible to create the exportto provider factory", e);
283
            }
284
        }
285
        return false;
286
    }
287

    
288
    public void addProviderFactory(ProviderFactory providerFactory) {
289
        if (providerFactory == null) {
290
            throw new IllegalArgumentException(
291
                "ProviderFactory cannot be null.");
292
        }
293

    
294
        if (!(providerFactory instanceof ExporttoSwingProviderFactory)) {
295
            throw new IllegalArgumentException(providerFactory.getName()
296
                + " must implement the ExporttoSwingProviderFactory interface");
297
        }
298
        super.addProviderFactory(providerFactory);
299
    }
300

    
301
    public String getDescription(String providerName) {
302
        try {
303
            return getExporttoSwingProviderFactory(providerName)
304
                .getDescription();
305
        } catch (ServiceException e) {
306
            throw new RuntimeException(e);
307
        }
308
    }
309

    
310
    public boolean isProviderEnabled(ExporttoSwingProviderFactory factory) {
311
        Boolean status = providerStatus.get(factory);
312
        return status == null ? true : status.booleanValue();
313
    }
314

    
315
    public void enableProvider(ExporttoSwingProviderFactory factory, boolean value) {
316
        providerStatus.put(factory, Boolean.valueOf(value));
317
    }
318

    
319
    public ExporttoSwingProviderFactory getExporttoSwingProviderFactory(
320
        String name) throws ServiceException {
321
        return (ExporttoSwingProviderFactory) getProviderFactory(name);
322
    }
323

    
324
    public ExporttoSwingPreferencesComponent createExporttoSwingProvidersPreferences() {
325
        return new DefaultExporttoPreferencesComponent();
326
    }
327
}