gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.circle3p / src / main / java / org / gvsig / vectorediting / lib / prov / circle3p / Circle3PEditingProvider.java @ 575
History | View | Annotate | Download (11.8 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.circle3p; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
|
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.GeometryManager; |
36 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
37 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
38 |
import org.gvsig.fmap.geom.primitive.Circle; |
39 |
import org.gvsig.fmap.geom.primitive.Curve; |
40 |
import org.gvsig.fmap.geom.primitive.Point; |
41 |
import org.gvsig.fmap.geom.type.GeometryType; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
43 |
import org.gvsig.tools.dynobject.DynObject; |
44 |
import org.gvsig.tools.service.spi.ProviderServices; |
45 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
46 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
47 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
48 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
53 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
54 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
55 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
56 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
59 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
60 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
61 |
|
62 |
public class Circle3PEditingProvider extends AbstractEditingProvider implements |
63 |
EditingProvider { |
64 |
|
65 |
protected FeatureStore featureStore;
|
66 |
|
67 |
protected EditingServiceParameter firstPoint;
|
68 |
|
69 |
protected EditingServiceParameter secondPoint;
|
70 |
|
71 |
protected EditingServiceParameter thirdPoint;
|
72 |
|
73 |
protected Map<EditingServiceParameter, Object> values; |
74 |
|
75 |
/**
|
76 |
* Default constructor.
|
77 |
*
|
78 |
* @param services
|
79 |
* available services to this provider
|
80 |
* @param parameters
|
81 |
* to create this provider
|
82 |
*/
|
83 |
public Circle3PEditingProvider(ProviderServices services,
|
84 |
DynObject parameters) { |
85 |
super(services);
|
86 |
|
87 |
this.featureStore =
|
88 |
(FeatureStore) parameters |
89 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
90 |
|
91 |
this.firstPoint =
|
92 |
new DefaultEditingServiceParameter("first_point", "first_point", |
93 |
TYPE.POSITION); |
94 |
|
95 |
this.secondPoint =
|
96 |
new DefaultEditingServiceParameter("second_point", "second_point", |
97 |
TYPE.POSITION); |
98 |
|
99 |
this.thirdPoint =
|
100 |
new DefaultEditingServiceParameter("third_point", "third_point", |
101 |
TYPE.POSITION); |
102 |
} |
103 |
|
104 |
public EditingServiceParameter next() {
|
105 |
if (values != null) { |
106 |
|
107 |
if (values.get(firstPoint) == null) { |
108 |
return firstPoint;
|
109 |
} else if (values.get(secondPoint) == null) { |
110 |
return secondPoint;
|
111 |
} else if (values.get(thirdPoint) == null) { |
112 |
return thirdPoint;
|
113 |
} |
114 |
} |
115 |
return null; |
116 |
} |
117 |
|
118 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
119 |
throws DrawServiceException {
|
120 |
|
121 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
122 |
EditingProviderManager editingProviderManager = |
123 |
EditingProviderLocator.getProviderManager(); |
124 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
125 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
126 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
127 |
|
128 |
if (values != null) { |
129 |
|
130 |
Point firstPointValue = (Point) values.get(firstPoint); |
131 |
Point secondPointValue = (Point) values.get(secondPoint); |
132 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
133 |
|
134 |
EditingProviderServices editingProviderServices = |
135 |
(EditingProviderServices) getProviderServices(); |
136 |
|
137 |
if (firstPointValue != null && secondPointValue == null) { |
138 |
|
139 |
try {
|
140 |
|
141 |
int subtype =
|
142 |
editingProviderServices.getSubType(featureStore); |
143 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
144 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
145 |
|
146 |
Curve line = |
147 |
editingProviderServices.createLine(firstPointValue, |
148 |
mousePosition, subtype); |
149 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
150 |
|
151 |
} catch (Exception e) { |
152 |
throw new DrawServiceException(e); |
153 |
} |
154 |
|
155 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue == null) { |
156 |
|
157 |
try {
|
158 |
|
159 |
int subtype =
|
160 |
editingProviderServices.getSubType(featureStore); |
161 |
|
162 |
Curve line1 = |
163 |
editingProviderServices.createLine(firstPointValue, |
164 |
secondPointValue, subtype); |
165 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
166 |
|
167 |
Curve line2 = |
168 |
editingProviderServices.createLine(secondPointValue, |
169 |
mousePosition, subtype); |
170 |
drawingStatus.addStatus(line2, auxiliaryLineSymbolEditing, "");
|
171 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
172 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
173 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
174 |
|
175 |
Point center =
|
176 |
editingProviderServices.getCenter(firstPointValue, |
177 |
secondPointValue, mousePosition, subtype); |
178 |
double radius = center.distance(firstPointValue);
|
179 |
|
180 |
addTemporaryGeometryToDrawingStatus(drawingStatus, subtype, center, radius); |
181 |
drawingStatus.addStatus(center, auxiliaryPointSymbolEditing, "");
|
182 |
|
183 |
} catch (Exception e) { |
184 |
throw new DrawServiceException(e); |
185 |
} |
186 |
} |
187 |
} |
188 |
return drawingStatus;
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* @param drawingStatus
|
193 |
* @param subtype
|
194 |
* @param center
|
195 |
* @param radius
|
196 |
* @throws CreateGeometryException
|
197 |
*/
|
198 |
protected void addTemporaryGeometryToDrawingStatus(DefaultDrawingStatus drawingStatus, int subtype, Point center, double radius) |
199 |
throws CreateGeometryException {
|
200 |
EditingProviderManager editingProviderManager = |
201 |
EditingProviderLocator.getProviderManager(); |
202 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
203 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
204 |
Circle circle = (Circle)geomManager.create(Geometry.TYPES.CIRCLE, subtype); |
205 |
circle.setPoints(center, radius); |
206 |
drawingStatus.addStatus(circle, polygonSymbolEditing, "");
|
207 |
} |
208 |
|
209 |
public void stop() throws StopServiceException { |
210 |
if (values != null) { |
211 |
values.clear(); |
212 |
} |
213 |
} |
214 |
|
215 |
public List<EditingServiceParameter> getParameters() { |
216 |
List<EditingServiceParameter> parameters =
|
217 |
new ArrayList<EditingServiceParameter>(); |
218 |
parameters.add(firstPoint); |
219 |
parameters.add(secondPoint); |
220 |
parameters.add(thirdPoint); |
221 |
return parameters;
|
222 |
} |
223 |
|
224 |
public void setValue(Object value) throws InvalidEntryException { |
225 |
EditingServiceParameter parameter = next(); |
226 |
validateAndInsertValue(parameter, value); |
227 |
} |
228 |
|
229 |
private void validateAndInsertValue(EditingServiceParameter parameter, |
230 |
Object value) throws InvalidEntryException { |
231 |
if (parameter == firstPoint) {
|
232 |
if (value instanceof Point) { |
233 |
values.put(parameter, value); |
234 |
return;
|
235 |
} |
236 |
} else if (parameter == secondPoint) { |
237 |
if (value instanceof Point) { |
238 |
values.put(parameter, value); |
239 |
return;
|
240 |
} |
241 |
} else if (parameter == thirdPoint) { |
242 |
if (value instanceof Point) { |
243 |
values.put(parameter, value); |
244 |
return;
|
245 |
} |
246 |
} |
247 |
throw new InvalidEntryException(null); |
248 |
} |
249 |
|
250 |
public Geometry finish() throws FinishServiceException { |
251 |
if (values != null) { |
252 |
|
253 |
Point firstPointValue = (Point) values.get(firstPoint); |
254 |
Point secondPointValue = (Point) values.get(secondPoint); |
255 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
256 |
|
257 |
EditingProviderServices editingProviderServices = |
258 |
(EditingProviderServices) getProviderServices(); |
259 |
|
260 |
try {
|
261 |
|
262 |
int subtype = editingProviderServices.getSubType(featureStore);
|
263 |
|
264 |
Point center =
|
265 |
editingProviderServices.getCenter(firstPointValue, |
266 |
secondPointValue, thirdPointValue, subtype); |
267 |
double radius = center.distance(firstPointValue);
|
268 |
|
269 |
Circle circle = |
270 |
editingProviderServices.createCircle(center, radius, |
271 |
subtype); |
272 |
|
273 |
GeometryType geomType = |
274 |
editingProviderServices.getGeomType(featureStore); |
275 |
|
276 |
if (geomType.isTypeOf(MULTISURFACE)) {
|
277 |
|
278 |
MultiSurface multiSurface; |
279 |
multiSurface = |
280 |
GeometryLocator.getGeometryManager() |
281 |
.createMultiSurface(geomType.getSubType()); |
282 |
multiSurface.addSurface(circle); |
283 |
return multiSurface;
|
284 |
} |
285 |
|
286 |
return circle;
|
287 |
} catch (Exception e) { |
288 |
throw new FinishServiceException(e); |
289 |
} |
290 |
} |
291 |
throw new FinishServiceException(null); |
292 |
} |
293 |
|
294 |
public void finishAndStore() throws FinishServiceException { |
295 |
Geometry circle = finish(); |
296 |
EditingProviderServices editingProviderServices = |
297 |
(EditingProviderServices) getProviderServices(); |
298 |
editingProviderServices.insertGeometryIntoFeatureStore(circle, |
299 |
featureStore); |
300 |
} |
301 |
|
302 |
public void start() throws StartServiceException { |
303 |
values = new HashMap<EditingServiceParameter, Object>(); |
304 |
|
305 |
} |
306 |
|
307 |
public String getName() { |
308 |
return Circle3PEditingProviderFactory.PROVIDER_NAME;
|
309 |
} |
310 |
|
311 |
} |