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