gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.spline / src / main / java / org / gvsig / vectorediting / lib / prov / spline / SplineEditingProvider.java @ 2204
History | View | Annotate | Download (10.3 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.prov.spline; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.LinkedHashMap; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
36 |
import org.gvsig.fmap.geom.GeometryManager; |
37 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
38 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.geom.primitive.Spline; |
40 |
import org.gvsig.fmap.geom.type.GeometryType; |
41 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.dynobject.DynObject; |
44 |
import org.gvsig.tools.exception.BaseException; |
45 |
import org.gvsig.tools.i18n.I18nManager; |
46 |
import org.gvsig.tools.service.spi.ProviderServices; |
47 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
48 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
53 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
54 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
55 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
56 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
59 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
60 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
61 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
62 |
|
63 |
/**
|
64 |
* @author llmarques
|
65 |
*
|
66 |
*/
|
67 |
public class SplineEditingProvider extends AbstractEditingProvider implements |
68 |
EditingProvider { |
69 |
|
70 |
protected EditingServiceParameter points;
|
71 |
|
72 |
protected List<Point> values; |
73 |
|
74 |
protected Map<String, String> options; |
75 |
|
76 |
protected boolean closeSpline; |
77 |
|
78 |
protected boolean finishSpline; |
79 |
|
80 |
protected FeatureStore featureStore;
|
81 |
|
82 |
/**
|
83 |
* Default constructor.
|
84 |
*
|
85 |
* @param parameters
|
86 |
* of service.
|
87 |
* @param providerServices
|
88 |
* available services. See {@link EditingProviderServices}.
|
89 |
*/
|
90 |
public SplineEditingProvider(ProviderServices services, DynObject parameters) {
|
91 |
super(services);
|
92 |
|
93 |
this.featureStore =
|
94 |
(FeatureStore) parameters |
95 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
96 |
|
97 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
98 |
options = new LinkedHashMap<String, String>(); |
99 |
options.put(i18nManager.getTranslation("key_close"), "close_spline"); |
100 |
options.put(i18nManager.getTranslation("key_finish"), "finish"); |
101 |
options.put(i18nManager.getTranslation("key_remove_last_point"), "remove_last_point"); |
102 |
|
103 |
|
104 |
EditingProviderServices editingProviderServices = |
105 |
(EditingProviderServices) getProviderServices(); |
106 |
|
107 |
String consoleMsg =
|
108 |
editingProviderServices.makeConsoleMessage("indicate_new_point",
|
109 |
options); |
110 |
|
111 |
this.points =
|
112 |
new DefaultEditingServiceParameter("insert_point", consoleMsg, |
113 |
options, EditingServiceParameter.TYPE.LIST_POSITIONS, |
114 |
EditingServiceParameter.TYPE.OPTION); |
115 |
|
116 |
this.closeSpline = false; |
117 |
|
118 |
this.finishSpline = false; |
119 |
} |
120 |
|
121 |
public EditingServiceParameter next() {
|
122 |
if (finishSpline) {
|
123 |
return null; |
124 |
} |
125 |
return points;
|
126 |
} |
127 |
|
128 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
129 |
throws DrawServiceException {
|
130 |
|
131 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
132 |
EditingProviderManager editingProviderManager = |
133 |
EditingProviderLocator.getProviderManager(); |
134 |
ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
|
135 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
136 |
|
137 |
|
138 |
if (values != null) { |
139 |
|
140 |
if (values.size() >= 1) { |
141 |
for (Iterator iterator = values.iterator(); iterator.hasNext();) { |
142 |
Point point = (Point) iterator.next(); |
143 |
drawingStatus.addStatus(point, auxiliaryPointSymbolEditing, "");
|
144 |
} |
145 |
|
146 |
EditingProviderServices editingProviderServices = |
147 |
(EditingProviderServices) getProviderServices(); |
148 |
|
149 |
try {
|
150 |
int subtype =
|
151 |
editingProviderServices.getSubType(featureStore); |
152 |
Spline spline = |
153 |
editingProviderServices.createSpline(values, subtype); |
154 |
|
155 |
spline.addVertex(mousePosition); |
156 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
157 |
drawingStatus.addStatus(spline, lineSymbolEditing, "");
|
158 |
|
159 |
// drawingStatus.addGeometry(spline);
|
160 |
} catch (BaseException e) {
|
161 |
throw new DrawServiceException(e); |
162 |
} |
163 |
} |
164 |
} |
165 |
return drawingStatus;
|
166 |
} |
167 |
|
168 |
public void stop() throws StopServiceException { |
169 |
values.clear(); |
170 |
} |
171 |
|
172 |
public List<EditingServiceParameter> getParameters() { |
173 |
List<EditingServiceParameter> parameters =
|
174 |
new ArrayList<EditingServiceParameter>(); |
175 |
parameters.add(points); |
176 |
return parameters;
|
177 |
} |
178 |
|
179 |
@Override
|
180 |
public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException { |
181 |
validateAndInsertValue(parameter, value); |
182 |
} |
183 |
|
184 |
public void setValue(Object value) throws InvalidEntryException { |
185 |
EditingServiceParameter parameter = next(); |
186 |
validateAndInsertValue(parameter, value); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Checks if value is valid to parameter received as parameter.
|
191 |
*
|
192 |
* @param param
|
193 |
* of value
|
194 |
* @param value
|
195 |
* to e inserted
|
196 |
* @throws InvalidEntryException
|
197 |
* If values is no valid throw an InvalidEntryException
|
198 |
*/
|
199 |
private void validateAndInsertValue(EditingServiceParameter parameter, |
200 |
Object value) throws InvalidEntryException { |
201 |
|
202 |
if (parameter == points) {
|
203 |
|
204 |
if (value instanceof Point) { |
205 |
values.add((Point) value);
|
206 |
return;
|
207 |
|
208 |
} else if (value instanceof String) { |
209 |
|
210 |
String option = (String) value; |
211 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
212 |
|
213 |
if (values.size() > 0) { |
214 |
if (option.equalsIgnoreCase(i18nManager
|
215 |
.getTranslation("key_remove_last_point"))) {
|
216 |
values.remove(values.size() - 1);
|
217 |
return;
|
218 |
} |
219 |
} |
220 |
if (values.size() > 1) { |
221 |
if (option.equalsIgnoreCase(i18nManager
|
222 |
.getTranslation("key_close"))
|
223 |
|| option.equalsIgnoreCase(i18nManager |
224 |
.getTranslation("key_finish"))) {
|
225 |
|
226 |
if (option.equalsIgnoreCase(i18nManager
|
227 |
.getTranslation("key_close"))) {
|
228 |
this.closeSpline = true; |
229 |
} |
230 |
|
231 |
this.finishSpline = true; |
232 |
return;
|
233 |
} |
234 |
} |
235 |
} |
236 |
} |
237 |
throw new InvalidEntryException(null); |
238 |
} |
239 |
|
240 |
public Geometry finish() throws FinishServiceException { |
241 |
|
242 |
if (values != null) { |
243 |
|
244 |
EditingProviderServices editingProviderServices = |
245 |
(EditingProviderServices) getProviderServices(); |
246 |
try {
|
247 |
|
248 |
if (closeSpline) {
|
249 |
values.add(values.get(0));
|
250 |
} |
251 |
|
252 |
int subtype = editingProviderServices.getSubType(featureStore);
|
253 |
Spline spline = |
254 |
editingProviderServices.createSpline(values, subtype); |
255 |
|
256 |
GeometryType geomType = |
257 |
editingProviderServices.getGeomType(featureStore); |
258 |
|
259 |
if (geomType.isTypeOf(MULTICURVE)) {
|
260 |
|
261 |
GeometryManager geometryManager = |
262 |
GeometryLocator.getGeometryManager(); |
263 |
MultiCurve multiCurve = |
264 |
geometryManager.createMultiCurve(geomType.getSubType()); |
265 |
multiCurve.addCurve(spline); |
266 |
return multiCurve;
|
267 |
|
268 |
} else {
|
269 |
return spline;
|
270 |
} |
271 |
} catch (BaseException e) {
|
272 |
throw new FinishServiceException(e); |
273 |
} |
274 |
} |
275 |
return null; |
276 |
} |
277 |
|
278 |
public void finishAndStore() throws FinishServiceException { |
279 |
Geometry geometry = this.finish();
|
280 |
EditingProviderServices editingProviderServices = |
281 |
(EditingProviderServices) getProviderServices(); |
282 |
editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
283 |
featureStore); |
284 |
} |
285 |
|
286 |
public void start() throws StartServiceException { |
287 |
values = new ArrayList<Point>(); |
288 |
|
289 |
closeSpline = false;
|
290 |
finishSpline = false;
|
291 |
} |
292 |
|
293 |
public String getName() { |
294 |
return SplineEditingProviderFactory.PROVIDER_NAME;
|
295 |
} |
296 |
|
297 |
} |