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

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

    
30
import org.cresques.cts.IProjection;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

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

    
53
/**
54
 * @author gvSIG Team
55
 * @version $Id$
56
 * 
57
 */
58
public class DefaultExporttoSwingProviderManager extends
59
    AbstractProviderManager implements ExporttoSwingProviderManager {
60

    
61
    private static final Logger LOG = LoggerFactory
62
        .getLogger(DefaultExporttoSwingProviderManager.class);
63

    
64
    private static final String PROVIDERS_NAME = "Exportto.swing.providers";
65
    private static final String PROVIDERS_DESCRIPTION =
66
        "Exportto swing providers";
67

    
68
    private static final String PARAMETER_FEATURESTORE = "FeatureStore";
69
    private static final String PARAMETER_PROJECTION = "Projection";
70

    
71
    private Map<ExporttoSwingProviderFactory, Boolean> providerStatus;
72

    
73
    public DefaultExporttoSwingProviderManager() {
74
        super();
75
        providerStatus = new HashMap<ExporttoSwingProviderFactory, Boolean>();
76
    }
77

    
78
    public ProviderServices createProviderServices(Service service) {
79
        return new DefaultExporttoSwingProviderServices();
80
    }
81

    
82
    public List<String> getProviderNames() {
83
        return getProviderNames(new int[0]);
84
    }
85

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

    
134
    public List<ExporttoSwingProviderFactory> getProviderFactories() {
135
        return getProviderFactories(null);
136
    }
137

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

    
187
    protected String getRegistryDescription() {
188
        return PROVIDERS_DESCRIPTION;
189
    }
190

    
191
    protected String getRegistryKey() {
192
        return PROVIDERS_NAME;
193
    }
194

    
195
    public ExporttoSwingProvider createExporttoSwingProvider(
196
        String providerName, FeatureStore featureStore, IProjection projection)
197
        throws ServiceException {
198
        DynObject serviceParameters = createServiceParameters(providerName);
199
        serviceParameters.setDynValue(PARAMETER_FEATURESTORE, featureStore);
200
        try {
201
            serviceParameters.setDynValue(PARAMETER_PROJECTION, projection);
202
        } catch (DynFieldNotFoundException e) {
203
            LOG.info("This provider doesn't accept projection", e);
204
        }
205
        Provider provider =
206
            createProvider(serviceParameters,
207
                new DefaultExporttoSwingProviderServices());
208
        if (!(provider instanceof ExporttoSwingProvider)) {
209
            throw new NotExporttoSwingProviderException(providerName);
210
        }
211
        return (ExporttoSwingProvider) provider;
212
    }
213

    
214
    public boolean support(String providerName, int providerType)
215
        throws ServiceException {
216
        ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
217
        ExtensionPoint ep = epm.get(this.getRegistryKey());
218
        ExporttoSwingProviderFactory exporttoSwingProviderFactory = null;
219

    
220
        if (ep != null) {
221
            try {
222
                exporttoSwingProviderFactory =
223
                    ((ExporttoSwingProviderFactory) ep.create(providerName));
224
                return exporttoSwingProviderFactory.support(providerType);
225
            } catch (InstantiationException e) {
226
                LOG.error(
227
                    "Not possible to create the exportto provider factory", e);
228
            } catch (IllegalAccessException e) {
229
                LOG.error(
230
                    "Not possible to create the exportto provider factory", e);
231
            }
232
        }
233
        return false;
234
    }
235

    
236
    public void addProviderFactory(ProviderFactory providerFactory) {
237
        if (providerFactory == null) {
238
            throw new IllegalArgumentException(
239
                "ProviderFactory cannot be null.");
240
        }
241

    
242
        if (!(providerFactory instanceof ExporttoSwingProviderFactory)) {
243
            throw new IllegalArgumentException(providerFactory.getName()
244
                + " must implement the ExporttoSwingProviderFactory interface");
245
        }
246
        super.addProviderFactory(providerFactory);
247
    }
248

    
249
    public String getDescription(String providerName) {
250
        try {
251
            return getExporttoSwingProviderFactory(providerName)
252
                .getDescription();
253
        } catch (ServiceException e) {
254
            throw new RuntimeException(e);
255
        }
256
    }
257

    
258
    public boolean isProviderEnabled(ExporttoSwingProviderFactory factory) {
259
        Boolean status = providerStatus.get(factory);
260
        return status == null ? true : status.booleanValue();
261
    }
262

    
263
    public void enableProvider(ExporttoSwingProviderFactory factory, boolean value) {
264
        providerStatus.put(factory, Boolean.valueOf(value));
265
    }
266

    
267
    public ExporttoSwingProviderFactory getExporttoSwingProviderFactory(
268
        String name) throws ServiceException {
269
        return (ExporttoSwingProviderFactory) getProviderFactory(name);
270
    }
271

    
272
    public ExporttoSwingPreferencesComponent createExporttoSwingProvidersPreferences() {
273
        return new DefaultExporttoPreferencesComponent();
274
    }
275
}