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 @ 2109
History | View | Annotate | Download (8.38 KB)
1 | 159 | llmarques | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 41 | fdiaz | *
|
4 | 159 | llmarques | * 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 | 28 | llmarques | */
|
24 | 159 | llmarques | |
25 | 47 | fdiaz | package org.gvsig.vectorediting.lib.prov.circlecr; |
26 | 28 | llmarques | |
27 | 38 | llmarques | import java.util.ArrayList; |
28 | 28 | llmarques | import java.util.HashMap; |
29 | import java.util.List; |
||
30 | import java.util.Map; |
||
31 | |||
32 | 45 | llmarques | import org.gvsig.fmap.dal.feature.FeatureStore; |
33 | 28 | llmarques | import org.gvsig.fmap.geom.Geometry; |
34 | 55 | fdiaz | import org.gvsig.fmap.geom.GeometryLocator; |
35 | 46 | fdiaz | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
36 | import org.gvsig.fmap.geom.primitive.Arc; |
||
37 | 28 | llmarques | import org.gvsig.fmap.geom.primitive.Circle; |
38 | 55 | fdiaz | import org.gvsig.fmap.geom.primitive.Curve; |
39 | 37 | llmarques | import org.gvsig.fmap.geom.primitive.Point; |
40 | 46 | fdiaz | import org.gvsig.fmap.geom.type.GeometryType; |
41 | 333 | fdiaz | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
42 | 28 | llmarques | import org.gvsig.tools.dynobject.DynObject; |
43 | import org.gvsig.tools.service.spi.ProviderServices; |
||
44 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
||
45 | 44 | llmarques | import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
46 | 61 | llmarques | import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
47 | import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
||
48 | 62 | llmarques | import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
49 | 63 | fdiaz | import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
50 | 28 | llmarques | import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
51 | 55 | fdiaz | import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
52 | 28 | llmarques | import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
53 | import org.gvsig.vectorediting.lib.spi.EditingProvider; |
||
54 | import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
||
55 | 333 | fdiaz | import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
56 | import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
||
57 | 28 | llmarques | import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
58 | |||
59 | 47 | fdiaz | public class CircleCREditingProvider extends AbstractEditingProvider implements |
60 | 227 | llmarques | EditingProvider { |
61 | 28 | llmarques | |
62 | 193 | llmarques | protected EditingServiceParameter center;
|
63 | 65 | fdiaz | |
64 | 193 | llmarques | protected EditingServiceParameter radius;
|
65 | 28 | llmarques | |
66 | 136 | llmarques | protected Map<EditingServiceParameter, Object> values; |
67 | 28 | llmarques | |
68 | 136 | llmarques | protected FeatureStore featureStore;
|
69 | 28 | llmarques | |
70 | 136 | llmarques | public CircleCREditingProvider(ProviderServices providerServices,
|
71 | DynObject parameters) { |
||
72 | super(providerServices);
|
||
73 | 193 | llmarques | |
74 | 136 | llmarques | this.featureStore =
|
75 | (FeatureStore) parameters |
||
76 | 227 | llmarques | .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
77 | 193 | llmarques | |
78 | this.center =
|
||
79 | 228 | llmarques | new DefaultEditingServiceParameter("center", "center", |
80 | 193 | llmarques | TYPE.POSITION); |
81 | |||
82 | this.radius =
|
||
83 | 228 | llmarques | new DefaultEditingServiceParameter("radius", "radius", |
84 | 193 | llmarques | TYPE.POSITION, TYPE.VALUE); |
85 | 136 | llmarques | } |
86 | 28 | llmarques | |
87 | 136 | llmarques | public DefaultDrawingStatus getDrawingStatus(Point mousePosition) |
88 | throws DrawServiceException {
|
||
89 | DefaultDrawingStatus geometries = new DefaultDrawingStatus();
|
||
90 | 333 | fdiaz | 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 | 227 | llmarques | if ((values != null) && (values.get(center) != null) |
97 | && (values.get(radius) == null)) {
|
||
98 | 136 | llmarques | Point centerPoint = (Point) values.get(center); |
99 | 178 | llmarques | EditingProviderServices editingProviderServices = |
100 | (EditingProviderServices) getProviderServices(); |
||
101 | |||
102 | 136 | llmarques | try {
|
103 | 178 | llmarques | int subtype = editingProviderServices.getSubType(featureStore);
|
104 | 136 | llmarques | Curve line = |
105 | 227 | llmarques | editingProviderServices.createLine(centerPoint, |
106 | mousePosition, subtype); |
||
107 | 333 | fdiaz | 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 | 55 | fdiaz | |
112 | 136 | llmarques | } catch (Exception e) { |
113 | throw new DrawServiceException(e); |
||
114 | } |
||
115 | 61 | llmarques | |
116 | 136 | llmarques | return geometries;
|
117 | } |
||
118 | 28 | llmarques | |
119 | 136 | llmarques | return null; |
120 | 28 | llmarques | |
121 | 136 | llmarques | } |
122 | 28 | llmarques | |
123 | 136 | llmarques | 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 | 28 | llmarques | } |
131 | 136 | llmarques | |
132 | public void stop() { |
||
133 | values.clear(); |
||
134 | 28 | llmarques | } |
135 | |||
136 | 136 | llmarques | 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 | 28 | llmarques | |
145 | 136 | llmarques | 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 | 227 | llmarques | if ((((Double) value) - 0.01) > 0) { |
163 | 136 | llmarques | values.put(param, value); |
164 | return;
|
||
165 | } |
||
166 | |||
167 | 193 | llmarques | } |
168 | 56 | llmarques | } |
169 | 136 | llmarques | throw new InvalidEntryException(null); |
170 | 56 | llmarques | } |
171 | 47 | fdiaz | |
172 | 136 | llmarques | 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 | 28 | llmarques | |
180 | 136 | llmarques | public void setValue(Object value) throws InvalidEntryException { |
181 | EditingServiceParameter param = next(); |
||
182 | validateAndInsertValue(param, value); |
||
183 | } |
||
184 | 28 | llmarques | |
185 | 136 | llmarques | public void finishAndStore() throws FinishServiceException { |
186 | EditingProviderServices editingProviderServices = |
||
187 | (EditingProviderServices) getProviderServices(); |
||
188 | Geometry geometry = finish(); |
||
189 | editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
||
190 | featureStore); |
||
191 | } |
||
192 | 80 | llmarques | |
193 | 136 | llmarques | 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 | 174 | llmarques | |
199 | 136 | llmarques | try {
|
200 | 178 | llmarques | int subtype = editingProviderServices.getSubType(featureStore);
|
201 | 136 | llmarques | GeometryType storeGeomType = |
202 | editingProviderServices.getGeomType(featureStore); |
||
203 | 174 | llmarques | Geometry geometry = |
204 | editingProviderServices.createCircle(centerPoint, radiusValue, |
||
205 | 178 | llmarques | subtype); |
206 | 174 | llmarques | |
207 | 136 | llmarques | 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 | 174 | llmarques | |
216 | 136 | llmarques | return geometry;
|
217 | } catch (Exception e) { |
||
218 | throw new FinishServiceException(e); |
||
219 | } |
||
220 | 56 | llmarques | } |
221 | 136 | llmarques | |
222 | public void start() throws StartServiceException { |
||
223 | this.values = new HashMap<EditingServiceParameter, Object>(); |
||
224 | 56 | llmarques | } |
225 | 28 | llmarques | |
226 | 136 | llmarques | public String getName() { |
227 | return CircleCREditingProviderFactory.PROVIDER_NAME;
|
||
228 | } |
||
229 | 28 | llmarques | |
230 | } |