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 / DefaultEditingManager.java @ 2444
History | View | Annotate | Download (6.18 KB)
1 |
/**
|
---|---|
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 |
*/
|
24 |
|
25 |
package org.gvsig.vectorediting.lib.impl; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import javax.json.JsonArray; |
31 |
import javax.json.JsonObject; |
32 |
import javax.json.JsonValue; |
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
import org.gvsig.andami.PluginServices; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.geom.DataTypes; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.fmap.mapcontext.MapContext; |
42 |
import org.gvsig.json.Json; |
43 |
import org.gvsig.tools.ToolsLocator; |
44 |
import org.gvsig.tools.dataTypes.Coercion; |
45 |
import org.gvsig.tools.dynobject.DynObject; |
46 |
import org.gvsig.tools.service.AbstractManager; |
47 |
import org.gvsig.tools.service.Service; |
48 |
import org.gvsig.tools.service.ServiceException; |
49 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
50 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
51 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
52 |
import org.gvsig.vectorediting.lib.api.EditingService; |
53 |
import org.gvsig.vectorediting.lib.api.EditingServiceInfo; |
54 |
import org.gvsig.vectorediting.lib.api.exceptions.ServiceInformationException; |
55 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
56 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
58 |
|
59 |
public class DefaultEditingManager extends AbstractManager implements |
60 |
EditingManager { |
61 |
|
62 |
private static final Logger logger = LoggerFactory |
63 |
.getLogger(DefaultEditingManager.class); |
64 |
|
65 |
private String clipboardValue; |
66 |
private List<Geometry> geometriesFromClipboard; |
67 |
|
68 |
public DefaultEditingManager() {
|
69 |
super(new DefaultEditingProviderManager()); |
70 |
} |
71 |
|
72 |
public Service getService(DynObject arg0) throws ServiceException { |
73 |
EditingProvider provider = |
74 |
(EditingProvider) EditingProviderLocator.getProviderManager() |
75 |
.createProvider(arg0, new DefaultEditingProviderServices());
|
76 |
return new DefaultEditingService(provider); |
77 |
} |
78 |
|
79 |
public EditingServiceInfo getServiceInfo(String serviceName) |
80 |
throws ServiceInformationException {
|
81 |
return EditingProviderLocator.getProviderManager().getServiceInfo(
|
82 |
serviceName); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public EditingService getEditingService(String name, |
87 |
FeatureStore featureStore, MapContext mapContext) { |
88 |
try {
|
89 |
DynObject params = this.createServiceParameters(name);
|
90 |
params.setDynValue(EditingProviderFactory.FEATURE_STORE_FIELD, |
91 |
featureStore); |
92 |
params.setDynValue(EditingProviderFactory.MAPCONTEXT_FIELD, mapContext); |
93 |
return (EditingService) this.getService(params); |
94 |
} catch (ServiceException e) {
|
95 |
String msg =
|
96 |
String
|
97 |
.format( |
98 |
"Some problem getting %1$s editing service. Seems to name is not correct or there is not factory registered with that name. ",
|
99 |
name); |
100 |
logger.info(msg, e); |
101 |
} |
102 |
|
103 |
return null; |
104 |
} |
105 |
|
106 |
public List<Geometry> getGeometriesFromClipboard(){ |
107 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
108 |
String clipboardValue = manager.getFromClipboard();
|
109 |
|
110 |
if(StringUtils.isNotBlank(clipboardValue)
|
111 |
&& StringUtils.isNotBlank(this.clipboardValue)
|
112 |
&& StringUtils.equals(clipboardValue, this.clipboardValue)){
|
113 |
return this.geometriesFromClipboard; |
114 |
} |
115 |
|
116 |
this.geometriesFromClipboard = new ArrayList<>(); |
117 |
|
118 |
String geometryName = null; |
119 |
try {
|
120 |
JsonArray jsonArray = Json.createArray(clipboardValue); |
121 |
|
122 |
Coercion toGeometry = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.GEOMETRY); |
123 |
|
124 |
for (JsonValue item : jsonArray) {
|
125 |
JsonObject jsonItem = (JsonObject) item; |
126 |
if (StringUtils.isBlank(geometryName)) {
|
127 |
for (Map.Entry<String, JsonValue> field : jsonItem.entrySet()) { |
128 |
Geometry geometry; |
129 |
try {
|
130 |
geometry = (Geometry) toGeometry.coerce(field.getValue().toString().replace("\"", "")); |
131 |
if (geometry != null) { |
132 |
geometryName = field.getKey(); |
133 |
this.geometriesFromClipboard.add(geometry);
|
134 |
break;
|
135 |
} |
136 |
} catch (Exception e) { |
137 |
//DO NOTHING
|
138 |
} |
139 |
} |
140 |
} else {
|
141 |
Geometry geometry; |
142 |
try {
|
143 |
String strGeom = jsonItem.getString(geometryName);
|
144 |
geometry = (Geometry) toGeometry.coerce(strGeom); |
145 |
if (geometry != null) { |
146 |
this.geometriesFromClipboard.add(geometry);
|
147 |
} |
148 |
} catch (Exception e) { |
149 |
//DO NOTHING
|
150 |
// logger.warn("Can't coerce geometry", e);
|
151 |
} |
152 |
} |
153 |
} |
154 |
} catch (Exception ex) { |
155 |
//DO NOTHING
|
156 |
} |
157 |
return this.geometriesFromClipboard; |
158 |
} |
159 |
|
160 |
} |