svn-gvsig-desktop / 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 @ 37780
History | View | Annotate | Download (8.35 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.Iterator; |
26 |
import java.util.List; |
27 |
|
28 |
import org.cresques.cts.IProjection; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProvider; |
33 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderFactory; |
34 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderManager; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.i18n.Messages; |
37 |
import org.gvsig.tools.ToolsLocator; |
38 |
import org.gvsig.tools.dynobject.DynObject; |
39 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
40 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
41 |
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension; |
42 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
43 |
import org.gvsig.tools.service.Service; |
44 |
import org.gvsig.tools.service.ServiceException; |
45 |
import org.gvsig.tools.service.spi.AbstractProviderManager; |
46 |
import org.gvsig.tools.service.spi.Provider; |
47 |
import org.gvsig.tools.service.spi.ProviderFactory; |
48 |
import org.gvsig.tools.service.spi.ProviderServices; |
49 |
|
50 |
/**
|
51 |
* @author gvSIG Team
|
52 |
* @version $Id$
|
53 |
*
|
54 |
*/
|
55 |
public class DefaultExporttoSwingProviderManager extends |
56 |
AbstractProviderManager implements ExporttoSwingProviderManager {
|
57 |
|
58 |
private static final Logger LOG = LoggerFactory |
59 |
.getLogger(DefaultExporttoSwingProviderManager.class); |
60 |
|
61 |
private static final String PROVIDERS_NAME = "Exportto.swing.providers"; |
62 |
private static final String PROVIDERS_DESCRIPTION = |
63 |
"Exportto swing providers";
|
64 |
|
65 |
private static final String PARAMETER_FEATURESTORE = "FeatureStore"; |
66 |
private static final String PARAMETER_PROJECTION = "Projection"; |
67 |
|
68 |
public DefaultExporttoSwingProviderManager() {
|
69 |
super();
|
70 |
} |
71 |
|
72 |
public ProviderServices createProviderServices(Service service) {
|
73 |
return new DefaultExporttoSwingProviderServices(); |
74 |
} |
75 |
|
76 |
public List<String> getProviderNames() { |
77 |
return getProviderNames(new int[0]); |
78 |
} |
79 |
|
80 |
public List<String> getProviderNames(int[] providerTypes) { |
81 |
ExtensionPointManager extensionPointManager = |
82 |
ToolsLocator.getExtensionPointManager(); |
83 |
ExtensionPoint extensionPoint = |
84 |
extensionPointManager.get(this.getRegistryKey());
|
85 |
List<String> providers = new ArrayList<String>(); |
86 |
if (extensionPoint != null) { |
87 |
@SuppressWarnings("unchecked") |
88 |
Iterator<Extension> it = extensionPoint.iterator();
|
89 |
while (it.hasNext()) {
|
90 |
Extension extension = it.next(); |
91 |
try {
|
92 |
ExporttoSwingProviderFactory exporttoSwingProviderFactory = |
93 |
(ExporttoSwingProviderFactory) extension.create(); |
94 |
if (providerTypes.length == 0) { |
95 |
providers.add(exporttoSwingProviderFactory.getName()); |
96 |
} else {
|
97 |
boolean areSupported = true; |
98 |
for (int i = 0; i < providerTypes.length; i++) { |
99 |
if (!exporttoSwingProviderFactory
|
100 |
.support(providerTypes[i])) { |
101 |
areSupported = false;
|
102 |
break;
|
103 |
} |
104 |
} |
105 |
if (areSupported) {
|
106 |
providers.add(exporttoSwingProviderFactory |
107 |
.getName()); |
108 |
} |
109 |
} |
110 |
} catch (InstantiationException e) { |
111 |
LOG.error( |
112 |
"Not possible to create the exportto provider factory",
|
113 |
e); |
114 |
} catch (IllegalAccessException e) { |
115 |
LOG.error( |
116 |
"Not possible to create the exportto provider factory",
|
117 |
e); |
118 |
} catch (ServiceException e) {
|
119 |
LOG.error( |
120 |
"Not possible to check if the provider support a format",
|
121 |
e); |
122 |
} |
123 |
} |
124 |
} |
125 |
return providers;
|
126 |
} |
127 |
|
128 |
protected String getRegistryDescription() { |
129 |
return PROVIDERS_DESCRIPTION;
|
130 |
} |
131 |
|
132 |
protected String getRegistryKey() { |
133 |
return PROVIDERS_NAME;
|
134 |
} |
135 |
|
136 |
public ExporttoSwingProvider createExporttoSwingProvider(
|
137 |
String providerName, FeatureStore featureStore, IProjection projection)
|
138 |
throws ServiceException {
|
139 |
DynObject serviceParameters = createServiceParameters(providerName); |
140 |
serviceParameters.setDynValue(PARAMETER_FEATURESTORE, featureStore); |
141 |
try {
|
142 |
serviceParameters.setDynValue(PARAMETER_PROJECTION, projection); |
143 |
} catch (DynFieldNotFoundException e) {
|
144 |
LOG.info("This provider doesn't accept projection", e);
|
145 |
} |
146 |
Provider provider =
|
147 |
createProvider(serviceParameters, |
148 |
new DefaultExporttoSwingProviderServices());
|
149 |
if (!(provider instanceof ExporttoSwingProvider)) { |
150 |
throw new NotExporttoSwingProviderException(providerName); |
151 |
} |
152 |
return (ExporttoSwingProvider) provider;
|
153 |
} |
154 |
|
155 |
public boolean support(String providerName, int providerType) |
156 |
throws ServiceException {
|
157 |
ExtensionPointManager epm = ToolsLocator.getExtensionPointManager(); |
158 |
ExtensionPoint ep = epm.get(this.getRegistryKey());
|
159 |
ExporttoSwingProviderFactory exporttoSwingProviderFactory = null;
|
160 |
|
161 |
if (ep != null) { |
162 |
try {
|
163 |
exporttoSwingProviderFactory = |
164 |
((ExporttoSwingProviderFactory) ep.create(providerName)); |
165 |
return exporttoSwingProviderFactory.support(providerType);
|
166 |
} catch (InstantiationException e) { |
167 |
LOG.error( |
168 |
"Not possible to create the exportto provider factory", e);
|
169 |
} catch (IllegalAccessException e) { |
170 |
LOG.error( |
171 |
"Not possible to create the exportto provider factory", e);
|
172 |
} |
173 |
} |
174 |
return false; |
175 |
} |
176 |
|
177 |
public void addProviderFactory(ProviderFactory providerFactory) { |
178 |
if (providerFactory == null) { |
179 |
throw new IllegalArgumentException( |
180 |
"ProviderFactory cannot be null.");
|
181 |
} |
182 |
|
183 |
if (!(providerFactory instanceof ExporttoSwingProviderFactory)) { |
184 |
throw new IllegalArgumentException(providerFactory.getName() |
185 |
+ " must implement the ExporttoSwingProviderFactory interface");
|
186 |
} |
187 |
super.addProviderFactory(providerFactory);
|
188 |
} |
189 |
|
190 |
public String getDescription(String providerName) { |
191 |
if (providerName == null) { |
192 |
return Messages.getText("exporer_to"); |
193 |
} |
194 |
ExtensionPointManager extensionPointManager = |
195 |
ToolsLocator.getExtensionPointManager(); |
196 |
ExtensionPoint extensionPoint = |
197 |
extensionPointManager.get(this.getRegistryKey());
|
198 |
try {
|
199 |
Object object = extensionPoint.create(providerName);
|
200 |
if (object != null) { |
201 |
return Messages.getText(((ExporttoSwingProviderFactory) object)
|
202 |
.getDescription()); |
203 |
} |
204 |
} catch (InstantiationException e) { |
205 |
LOG.error("Not possible to get the exportto provider factory", e);
|
206 |
} catch (IllegalAccessException e) { |
207 |
LOG.error("Not possible to get the exportto provider factory", e);
|
208 |
} |
209 |
return Messages.getText("exporer_to_format", |
210 |
new String[] { providerName }); |
211 |
} |
212 |
} |