gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.impl / src / main / java / org / gvsig / vectorediting / lib / impl / DefaultEditingProviderManager.java @ 2608
History | View | Annotate | Download (12.1 KB)
1 | 159 | llmarques | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright ? 2007-2014 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 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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 4 | llmarques | */
|
24 | 159 | llmarques | |
25 | 4 | llmarques | package org.gvsig.vectorediting.lib.impl; |
26 | |||
27 | 114 | llmarques | import java.net.URL; |
28 | 2608 | fdiaz | import java.util.ArrayList; |
29 | 319 | fdiaz | import java.util.HashMap; |
30 | 377 | fdiaz | import java.util.Iterator; |
31 | 2608 | fdiaz | import java.util.List; |
32 | 319 | fdiaz | import java.util.Map; |
33 | 377 | fdiaz | import java.util.Properties; |
34 | 2608 | fdiaz | import java.util.function.Predicate; |
35 | import javax.json.JsonArray; |
||
36 | import javax.json.JsonObject; |
||
37 | import javax.json.JsonValue; |
||
38 | import org.apache.commons.lang3.StringUtils; |
||
39 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
40 | import org.gvsig.fmap.dal.feature.Feature; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
42 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
43 | import org.gvsig.fmap.geom.DataTypes; |
||
44 | import org.gvsig.fmap.geom.Geometry; |
||
45 | import org.gvsig.fmap.geom.GeometryCoercionContext; |
||
46 | import org.gvsig.fmap.geom.GeometryLocator; |
||
47 | import org.gvsig.fmap.geom.GeometryUtils; |
||
48 | import org.gvsig.fmap.geom.type.GeometryType; |
||
49 | 319 | fdiaz | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
50 | 2608 | fdiaz | import org.gvsig.json.Json; |
51 | import org.gvsig.tools.ToolsLocator; |
||
52 | import org.gvsig.tools.dataTypes.Coercion; |
||
53 | import org.gvsig.tools.dataTypes.DataTypeUtils; |
||
54 | 17 | llmarques | import org.gvsig.tools.dynobject.DynObject; |
55 | 4 | llmarques | import org.gvsig.tools.service.Service; |
56 | 17 | llmarques | import org.gvsig.tools.service.ServiceException; |
57 | 4 | llmarques | import org.gvsig.tools.service.spi.AbstractProviderManager; |
58 | 17 | llmarques | import org.gvsig.tools.service.spi.Provider; |
59 | import org.gvsig.tools.service.spi.ProviderFactory; |
||
60 | 4 | llmarques | import org.gvsig.tools.service.spi.ProviderServices; |
61 | 114 | llmarques | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
62 | 2608 | fdiaz | import org.gvsig.tools.swing.api.ToolsSwingManager; |
63 | 114 | llmarques | import org.gvsig.tools.swing.icontheme.IconTheme; |
64 | 29 | llmarques | import org.gvsig.vectorediting.lib.api.EditingServiceInfo; |
65 | 89 | llmarques | import org.gvsig.vectorediting.lib.api.exceptions.ServiceInformationException; |
66 | 19 | llmarques | import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
67 | 4 | llmarques | import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
68 | 2608 | fdiaz | import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
69 | import org.slf4j.Logger; |
||
70 | import org.slf4j.LoggerFactory; |
||
71 | 4 | llmarques | |
72 | public class DefaultEditingProviderManager extends AbstractProviderManager |
||
73 | 227 | llmarques | implements EditingProviderManager {
|
74 | 4 | llmarques | |
75 | 114 | llmarques | private static final Logger logger = LoggerFactory |
76 | .getLogger(DefaultEditingProviderManager.class); |
||
77 | 4 | llmarques | |
78 | 2608 | fdiaz | Map<String, ISymbol> symbols = new HashMap<>(); |
79 | 319 | fdiaz | |
80 | 2608 | fdiaz | Map<String, Map<String, String>> defaultValues = new HashMap<>(); |
81 | |||
82 | private String clipboardValue; |
||
83 | private List<Geometry> geometriesFromClipboard; |
||
84 | private List<EditableFeature> featuresFromClipboard; |
||
85 | 377 | fdiaz | |
86 | 2608 | fdiaz | |
87 | 114 | llmarques | public ProviderServices createProviderServices(Service service) {
|
88 | // TODO Auto-generated method stub
|
||
89 | return null; |
||
90 | } |
||
91 | 4 | llmarques | |
92 | 114 | llmarques | @Override
|
93 | protected String getRegistryKey() { |
||
94 | // TODO Auto-generated method stub
|
||
95 | return null; |
||
96 | } |
||
97 | 4 | llmarques | |
98 | 114 | llmarques | @Override
|
99 | protected String getRegistryDescription() { |
||
100 | // TODO Auto-generated method stub
|
||
101 | return null; |
||
102 | } |
||
103 | 29 | llmarques | |
104 | 114 | llmarques | @Override
|
105 | public Provider createProvider(DynObject serviceParameters, |
||
106 | ProviderServices providerServices) throws ServiceException {
|
||
107 | String providerName =
|
||
108 | (String) serviceParameters
|
||
109 | 227 | llmarques | .getDynValue(EditingProviderFactory.PROVIDER_NAME_FIELD); |
110 | 114 | llmarques | ProviderFactory factory = getProviderFactory(providerName); |
111 | return factory == null ? null : factory.create(serviceParameters, |
||
112 | providerServices); |
||
113 | 89 | llmarques | } |
114 | 114 | llmarques | |
115 | public EditingServiceInfo getServiceInfo(String name) |
||
116 | throws ServiceInformationException {
|
||
117 | EditingProviderFactory factory; |
||
118 | try {
|
||
119 | factory = (EditingProviderFactory) getProviderFactory(name); |
||
120 | return (factory == null ? null : factory.getServiceInfo()); |
||
121 | } catch (Exception e) { |
||
122 | throw new ServiceInformationException("Can't get service info of " |
||
123 | + name, e); |
||
124 | } |
||
125 | 89 | llmarques | } |
126 | 114 | llmarques | |
127 | public void registerIcon(String group, String name, ClassLoader loader, |
||
128 | String provider) {
|
||
129 | String resourceName;
|
||
130 | IconTheme iconTheme = |
||
131 | ToolsSwingLocator.getIconThemeManager().getCurrent(); |
||
132 | 227 | llmarques | if ((group == null) || (group.trim().length() == 0)) { |
133 | 114 | llmarques | resourceName = "images/" + name + ".png"; |
134 | } else {
|
||
135 | resourceName = "images/" + group + "/" + name + ".png"; |
||
136 | } |
||
137 | |||
138 | URL resource = null; |
||
139 | try {
|
||
140 | resource = loader.getResource(resourceName); |
||
141 | iconTheme.registerDefault(provider, group, name, null, resource);
|
||
142 | } catch (Throwable e) { |
||
143 | logger.info("Can't register icon '" + name + "'.", e); |
||
144 | } |
||
145 | |||
146 | } |
||
147 | |||
148 | 319 | fdiaz | |
149 | public void registerSymbol(String name, ISymbol symbol) { |
||
150 | symbols.put(name, symbol); |
||
151 | } |
||
152 | |||
153 | public ISymbol getSymbol(String name) { |
||
154 | return symbols.get(name);
|
||
155 | } |
||
156 | 377 | fdiaz | |
157 | public void registerDefaultValues(String provider, Map values) { |
||
158 | if (!this.defaultValues.containsKey(provider)) { |
||
159 | this.defaultValues.put(provider, values);
|
||
160 | } |
||
161 | } |
||
162 | |||
163 | public void registerDefaultValues(String provider, ClassLoader loader) { |
||
164 | if (!this.defaultValues.containsKey(provider)) { |
||
165 | String resourceName =
|
||
166 | "defaultvalues/" + provider + "/defaultvalues.properties"; |
||
167 | HashMap<String, String> providerDefaultValues = |
||
168 | new HashMap<String, String>(); |
||
169 | |||
170 | URL resource = null; |
||
171 | try {
|
||
172 | resource = loader.getResource(resourceName); |
||
173 | |||
174 | Properties properties = new Properties(); |
||
175 | properties.load(resource.openStream()); |
||
176 | |||
177 | for (Iterator it = properties.keySet().iterator(); it.hasNext();) { |
||
178 | String key = (String) it.next(); |
||
179 | providerDefaultValues.put(key, (String) properties.get(key));
|
||
180 | } |
||
181 | this.defaultValues.put(provider, providerDefaultValues);
|
||
182 | } catch (Throwable e) { |
||
183 | logger.info("Can't register default values from '" + provider
|
||
184 | + "'.", e);
|
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | 2608 | fdiaz | @Override
|
190 | 377 | fdiaz | public Map<String, String> getDefaultValues(String name) { |
191 | return this.defaultValues.get(name); |
||
192 | } |
||
193 | 2608 | fdiaz | |
194 | @Override
|
||
195 | public List<Geometry> getGeometriesFromClipboard(){ |
||
196 | ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
||
197 | String clipboardValue = manager.getFromClipboard();
|
||
198 | |||
199 | if(StringUtils.isNotBlank(clipboardValue)
|
||
200 | && StringUtils.isNotBlank(this.clipboardValue)
|
||
201 | && StringUtils.equals(clipboardValue, this.clipboardValue)){
|
||
202 | return this.geometriesFromClipboard; |
||
203 | } |
||
204 | |||
205 | this.geometriesFromClipboard = new ArrayList<>(); |
||
206 | |||
207 | String geometryName = null; |
||
208 | try {
|
||
209 | JsonArray jsonArray = Json.createArray(clipboardValue); |
||
210 | 377 | fdiaz | |
211 | 2608 | fdiaz | Coercion toGeometry = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.GEOMETRY); |
212 | |||
213 | for (JsonValue item : jsonArray) {
|
||
214 | JsonObject jsonItem = (JsonObject) item; |
||
215 | if (StringUtils.isBlank(geometryName)) {
|
||
216 | for (Map.Entry<String, JsonValue> field : jsonItem.entrySet()) { |
||
217 | Geometry geometry; |
||
218 | try {
|
||
219 | geometry = (Geometry) toGeometry.coerce(field.getValue().toString().replace("\"", "")); |
||
220 | if (geometry != null) { |
||
221 | geometryName = field.getKey(); |
||
222 | this.geometriesFromClipboard.add(geometry);
|
||
223 | break;
|
||
224 | } |
||
225 | } catch (Exception e) { |
||
226 | //DO NOTHING
|
||
227 | } |
||
228 | } |
||
229 | } else {
|
||
230 | Geometry geometry; |
||
231 | try {
|
||
232 | String strGeom = jsonItem.getString(geometryName);
|
||
233 | geometry = (Geometry) toGeometry.coerce(strGeom); |
||
234 | if (geometry != null) { |
||
235 | this.geometriesFromClipboard.add(geometry);
|
||
236 | } |
||
237 | } catch (Exception e) { |
||
238 | //DO NOTHING
|
||
239 | // logger.warn("Can't coerce geometry", e);
|
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | } catch (Exception ex) { |
||
244 | //DO NOTHING
|
||
245 | } |
||
246 | return this.geometriesFromClipboard; |
||
247 | } |
||
248 | |||
249 | |||
250 | @Override
|
||
251 | public List<EditableFeature> getFeaturesFromClipboard(EditingProviderServices providerServices, FeatureStore store) { |
||
252 | ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
||
253 | String clipboardValue = manager.getFromClipboard();
|
||
254 | |||
255 | if(StringUtils.isNotBlank(clipboardValue)
|
||
256 | && StringUtils.isNotBlank(this.clipboardValue)
|
||
257 | && StringUtils.equals(clipboardValue, this.clipboardValue)){
|
||
258 | return this.featuresFromClipboard; |
||
259 | } |
||
260 | |||
261 | this.featuresFromClipboard = new ArrayList<>(); |
||
262 | |||
263 | GeometryType storeGeomType = store.getDefaultFeatureTypeQuietly().getDefaultGeometryAttribute().getGeomType(); |
||
264 | GeometryCoercionContext context = GeometryLocator.getGeometryManager().createGeometryCoercionContext(); |
||
265 | context.setGeometryType(storeGeomType); |
||
266 | context.setMode(GeometryCoercionContext.MODE_ONERROR_NULL); |
||
267 | Coercion toGeometry = DataTypeUtils.getCoercion(DataTypes.GEOMETRY); |
||
268 | |||
269 | |||
270 | String geometryName = null; |
||
271 | try {
|
||
272 | JsonArray jsonArray = Json.createArray(clipboardValue); |
||
273 | |||
274 | for (JsonValue item : jsonArray) {
|
||
275 | JsonObject jsonItem = (JsonObject) item; |
||
276 | EditableFeature feat = providerServices.createNewFeature(store); |
||
277 | feat.copyFrom(jsonItem, (FeatureAttributeDescriptor t) -> !t.isPrimaryKey()); |
||
278 | Geometry geometry = feat.getDefaultGeometry(); |
||
279 | if(geometry == null){ |
||
280 | if (StringUtils.isBlank(geometryName)) {
|
||
281 | for (Map.Entry<String, JsonValue> field : jsonItem.entrySet()) { |
||
282 | try {
|
||
283 | geometry = (Geometry) toGeometry.coerce(field.getValue().toString().replace("\"", ""), context); |
||
284 | if (geometry != null) { |
||
285 | geometryName = field.getKey(); |
||
286 | break;
|
||
287 | } |
||
288 | } catch (Exception e) { |
||
289 | //DO NOTHING
|
||
290 | } |
||
291 | } |
||
292 | } else {
|
||
293 | try {
|
||
294 | String strGeom = jsonItem.getString(geometryName);
|
||
295 | geometry = (Geometry) toGeometry.coerce(strGeom, context); |
||
296 | } catch (Exception e) { |
||
297 | //DO NOTHING
|
||
298 | } |
||
299 | } |
||
300 | |||
301 | } else {
|
||
302 | geometry = (Geometry) toGeometry.coerce(geometry,context); |
||
303 | } |
||
304 | |||
305 | if (geometry != null) { |
||
306 | feat.setDefaultGeometry(geometry); |
||
307 | this.featuresFromClipboard.add(feat);
|
||
308 | } |
||
309 | } |
||
310 | } catch (Exception ex) { |
||
311 | //DO NOTHING
|
||
312 | } |
||
313 | return this.featuresFromClipboard; |
||
314 | } |
||
315 | |||
316 | 4 | llmarques | } |