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