gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.circlecr / src / main / java / org / gvsig / vectorediting / lib / prov / circlecr / CircleCREditingProvider.java @ 333
History | View | Annotate | Download (8.38 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.circlecr; |
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.EditingServiceParameter; |
45 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
46 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
47 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
48 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
50 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
51 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
52 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
53 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
54 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
55 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
56 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
58 |
|
59 |
public class CircleCREditingProvider extends AbstractEditingProvider implements |
60 |
EditingProvider { |
61 |
|
62 |
protected EditingServiceParameter center;
|
63 |
|
64 |
protected EditingServiceParameter radius;
|
65 |
|
66 |
protected Map<EditingServiceParameter, Object> values; |
67 |
|
68 |
protected FeatureStore featureStore;
|
69 |
|
70 |
public CircleCREditingProvider(ProviderServices providerServices,
|
71 |
DynObject parameters) { |
72 |
super(providerServices);
|
73 |
|
74 |
this.featureStore =
|
75 |
(FeatureStore) parameters |
76 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
77 |
|
78 |
this.center =
|
79 |
new DefaultEditingServiceParameter("center", "center", |
80 |
TYPE.POSITION); |
81 |
|
82 |
this.radius =
|
83 |
new DefaultEditingServiceParameter("radius", "radius", |
84 |
TYPE.POSITION, TYPE.VALUE); |
85 |
} |
86 |
|
87 |
public DefaultDrawingStatus getDrawingStatus(Point mousePosition) |
88 |
throws DrawServiceException {
|
89 |
DefaultDrawingStatus geometries = new DefaultDrawingStatus();
|
90 |
EditingProviderManager editingProviderManager = |
91 |
EditingProviderLocator.getProviderManager(); |
92 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
93 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
94 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
95 |
|
96 |
if ((values != null) && (values.get(center) != null) |
97 |
&& (values.get(radius) == null)) {
|
98 |
Point centerPoint = (Point) values.get(center); |
99 |
EditingProviderServices editingProviderServices = |
100 |
(EditingProviderServices) getProviderServices(); |
101 |
|
102 |
try {
|
103 |
int subtype = editingProviderServices.getSubType(featureStore);
|
104 |
Curve line = |
105 |
editingProviderServices.createLine(centerPoint, |
106 |
mousePosition, subtype); |
107 |
Circle circle = editingProviderServices.createCircle(centerPoint, mousePosition.distance(centerPoint), subtype); |
108 |
geometries.addStatus(centerPoint, auxiliaryPointSymbolEditing, "");
|
109 |
geometries.addStatus(line, auxiliaryLineSymbolEditing, "");
|
110 |
geometries.addStatus(circle, polygonSymbolEditing, "");
|
111 |
|
112 |
} catch (Exception e) { |
113 |
throw new DrawServiceException(e); |
114 |
} |
115 |
|
116 |
return geometries;
|
117 |
} |
118 |
|
119 |
return null; |
120 |
|
121 |
} |
122 |
|
123 |
public EditingServiceParameter next() {
|
124 |
if (values.get(center) == null) { |
125 |
return this.center; |
126 |
} else if (values.get(radius) == null) { |
127 |
return this.radius; |
128 |
} |
129 |
return null; |
130 |
} |
131 |
|
132 |
public void stop() { |
133 |
values.clear(); |
134 |
} |
135 |
|
136 |
private void validateAndInsertValue(EditingServiceParameter param, |
137 |
Object value) throws InvalidEntryException { |
138 |
if (param == center) {
|
139 |
if (value instanceof Point) { |
140 |
values.put(param, value); |
141 |
return;
|
142 |
} |
143 |
} else if (param == radius) { |
144 |
|
145 |
if (value instanceof Point) { |
146 |
Double radiusValue = null; |
147 |
|
148 |
try {
|
149 |
radiusValue = |
150 |
((Point) value).distance((Point) values.get(center)); |
151 |
} catch (Exception e) { |
152 |
throw new InvalidEntryException(e); |
153 |
} |
154 |
|
155 |
if (radiusValue != null) { |
156 |
values.put(param, radiusValue); |
157 |
return;
|
158 |
} |
159 |
|
160 |
} else if (value instanceof Double) { |
161 |
|
162 |
if ((((Double) value) - 0.01) > 0) { |
163 |
values.put(param, value); |
164 |
return;
|
165 |
} |
166 |
|
167 |
} |
168 |
} |
169 |
throw new InvalidEntryException(null); |
170 |
} |
171 |
|
172 |
public List<EditingServiceParameter> getParameters() { |
173 |
List<EditingServiceParameter> list =
|
174 |
new ArrayList<EditingServiceParameter>(); |
175 |
list.add(center); |
176 |
list.add(radius); |
177 |
return list;
|
178 |
} |
179 |
|
180 |
public void setValue(Object value) throws InvalidEntryException { |
181 |
EditingServiceParameter param = next(); |
182 |
validateAndInsertValue(param, value); |
183 |
} |
184 |
|
185 |
public void finishAndStore() throws FinishServiceException { |
186 |
EditingProviderServices editingProviderServices = |
187 |
(EditingProviderServices) getProviderServices(); |
188 |
Geometry geometry = finish(); |
189 |
editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
190 |
featureStore); |
191 |
} |
192 |
|
193 |
public Geometry finish() throws FinishServiceException { |
194 |
Point centerPoint = (Point) values.get(center); |
195 |
double radiusValue = (Double) values.get(radius); |
196 |
EditingProviderServices editingProviderServices = |
197 |
(EditingProviderServices) getProviderServices(); |
198 |
|
199 |
try {
|
200 |
int subtype = editingProviderServices.getSubType(featureStore);
|
201 |
GeometryType storeGeomType = |
202 |
editingProviderServices.getGeomType(featureStore); |
203 |
Geometry geometry = |
204 |
editingProviderServices.createCircle(centerPoint, radiusValue, |
205 |
subtype); |
206 |
|
207 |
if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
208 |
MultiSurface multisurface; |
209 |
multisurface = |
210 |
GeometryLocator.getGeometryManager().createMultiSurface( |
211 |
storeGeomType.getSubType()); |
212 |
multisurface.addSurface((Circle) geometry); |
213 |
return multisurface;
|
214 |
} |
215 |
|
216 |
return geometry;
|
217 |
} catch (Exception e) { |
218 |
throw new FinishServiceException(e); |
219 |
} |
220 |
} |
221 |
|
222 |
public void start() throws StartServiceException { |
223 |
this.values = new HashMap<EditingServiceParameter, Object>(); |
224 |
} |
225 |
|
226 |
public String getName() { |
227 |
return CircleCREditingProviderFactory.PROVIDER_NAME;
|
228 |
} |
229 |
|
230 |
} |