gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.duplicate / src / main / java / org / gvsig / vectorediting / lib / prov / duplicate / DuplicateEditingProvider.java @ 493
History | View | Annotate | Download (12.4 KB)
1 | 261 | llmarques | /**
|
---|---|---|---|
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.duplicate; |
||
26 | |||
27 | import java.awt.geom.AffineTransform; |
||
28 | import java.util.ArrayList; |
||
29 | import java.util.HashMap; |
||
30 | import java.util.List; |
||
31 | import java.util.Map; |
||
32 | |||
33 | import org.gvsig.fmap.dal.exception.DataException; |
||
34 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
35 | import org.gvsig.fmap.dal.feature.Feature; |
||
36 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
37 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
38 | import org.gvsig.fmap.geom.Geometry; |
||
39 | 371 | fdiaz | import org.gvsig.fmap.geom.GeometryLocator; |
40 | import org.gvsig.fmap.geom.GeometryManager; |
||
41 | import org.gvsig.fmap.geom.aggregate.Aggregate; |
||
42 | import org.gvsig.fmap.geom.aggregate.MultiCurve; |
||
43 | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
||
44 | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
||
45 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
46 | 261 | llmarques | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
47 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
48 | 371 | fdiaz | import org.gvsig.fmap.geom.primitive.Curve; |
49 | import org.gvsig.fmap.geom.primitive.Line; |
||
50 | 261 | llmarques | import org.gvsig.fmap.geom.primitive.Point; |
51 | 371 | fdiaz | import org.gvsig.fmap.geom.primitive.Surface; |
52 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
53 | 261 | llmarques | import org.gvsig.tools.dispose.DisposableIterator; |
54 | import org.gvsig.tools.dynobject.DynObject; |
||
55 | import org.gvsig.tools.exception.BaseException; |
||
56 | import org.gvsig.tools.service.spi.ProviderServices; |
||
57 | import org.gvsig.tools.visitor.VisitCanceledException; |
||
58 | import org.gvsig.tools.visitor.Visitor; |
||
59 | import org.gvsig.vectorediting.lib.api.DrawingStatus; |
||
60 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
||
61 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
||
62 | import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
||
63 | import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
||
64 | import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
||
65 | import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
||
66 | import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
||
67 | import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
||
68 | import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
||
69 | import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
||
70 | import org.gvsig.vectorediting.lib.spi.EditingProvider; |
||
71 | import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
||
72 | 371 | fdiaz | import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
73 | import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
||
74 | 261 | llmarques | import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
75 | |||
76 | /**
|
||
77 | * @author llmarques
|
||
78 | *
|
||
79 | */
|
||
80 | public class DuplicateEditingProvider extends AbstractEditingProvider implements |
||
81 | EditingProvider { |
||
82 | |||
83 | private EditingServiceParameter selection;
|
||
84 | |||
85 | private EditingServiceParameter firstPoint;
|
||
86 | |||
87 | private EditingServiceParameter secondPoint;
|
||
88 | |||
89 | private FeatureStore featureStore;
|
||
90 | |||
91 | private Map<EditingServiceParameter, Object> values; |
||
92 | |||
93 | /**
|
||
94 | * Default constructor.
|
||
95 | *
|
||
96 | * @param providerServices
|
||
97 | * available services for this provider
|
||
98 | * @param parameters
|
||
99 | * of this provider
|
||
100 | */
|
||
101 | public DuplicateEditingProvider(DynObject parameters,
|
||
102 | ProviderServices services) { |
||
103 | super(services);
|
||
104 | |||
105 | this.featureStore =
|
||
106 | (FeatureStore) parameters |
||
107 | .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
||
108 | |||
109 | this.selection =
|
||
110 | new DefaultEditingServiceParameter("selection", "selection", |
||
111 | TYPE.SELECTION); |
||
112 | |||
113 | this.firstPoint =
|
||
114 | new DefaultEditingServiceParameter("first_point", "first_point", |
||
115 | TYPE.POSITION); |
||
116 | |||
117 | this.secondPoint =
|
||
118 | new DefaultEditingServiceParameter("second_point", "second_point", |
||
119 | TYPE.POSITION); |
||
120 | } |
||
121 | |||
122 | public EditingServiceParameter next() {
|
||
123 | if (values.get(selection) == null) { |
||
124 | return selection;
|
||
125 | } else if (values.get(firstPoint) == null) { |
||
126 | return firstPoint;
|
||
127 | } else if (values.get(secondPoint) == null) { |
||
128 | return secondPoint;
|
||
129 | } |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | public DrawingStatus getDrawingStatus(Point mousePosition) |
||
134 | throws DrawServiceException {
|
||
135 | DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
||
136 | |||
137 | 371 | fdiaz | GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
138 | EditingProviderManager editingProviderManager = |
||
139 | EditingProviderLocator.getProviderManager(); |
||
140 | EditingProviderServices editingProviderServices = |
||
141 | (EditingProviderServices) getProviderServices(); |
||
142 | int subtype;
|
||
143 | try {
|
||
144 | subtype = editingProviderServices.getSubType(featureStore); |
||
145 | } catch (DataException e2) {
|
||
146 | throw new DrawServiceException(e2); |
||
147 | } |
||
148 | ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
|
||
149 | ISymbol auxiliarylineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
||
150 | ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
||
151 | ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
||
152 | |||
153 | 261 | llmarques | if (values != null) { |
154 | |||
155 | FeatureSelection featureSelection = |
||
156 | (FeatureSelection) values.get(selection); |
||
157 | Point firstPointValue = (Point) values.get(firstPoint); |
||
158 | |||
159 | if (featureSelection != null && firstPointValue != null) { |
||
160 | 371 | fdiaz | drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
161 | Line auxLine;
|
||
162 | try {
|
||
163 | auxLine = geometryManager.createLine(subtype); |
||
164 | } catch (CreateGeometryException e1) {
|
||
165 | throw new DrawServiceException(e1); |
||
166 | } |
||
167 | auxLine.setPoints(firstPointValue, mousePosition); |
||
168 | drawingStatus.addStatus(auxLine, auxiliarylineSymbolEditing, "");
|
||
169 | |||
170 | 261 | llmarques | DisposableIterator it = null;
|
171 | try {
|
||
172 | it = featureSelection.fastIterator(); |
||
173 | while (it.hasNext()) {
|
||
174 | Feature feature = (Feature) it.next(); |
||
175 | |||
176 | Geometry geometry = |
||
177 | feature.getDefaultGeometry().cloneGeometry(); |
||
178 | |||
179 | AffineTransform at =
|
||
180 | getMoveAffineTransform(firstPointValue, |
||
181 | mousePosition); |
||
182 | geometry.transform(at); |
||
183 | 371 | fdiaz | ISymbol symbol=null;
|
184 | if(geometry instanceof Curve || geometry instanceof MultiCurve){ |
||
185 | symbol = lineSymbolEditing; |
||
186 | } else if(geometry instanceof Surface || geometry instanceof MultiSurface){ |
||
187 | symbol = polygonSymbolEditing; |
||
188 | } else if(geometry instanceof Point || geometry instanceof MultiPoint){ |
||
189 | symbol = auxiliaryPointSymbolEditing; |
||
190 | } |
||
191 | if(geometry instanceof Aggregate){ |
||
192 | int primitivesNumber = ((Aggregate)geometry).getPrimitivesNumber();
|
||
193 | for (int i = 0; i < primitivesNumber; i++) { |
||
194 | drawingStatus.addStatus(((Aggregate)geometry).getPrimitiveAt(i), symbol, "");
|
||
195 | } |
||
196 | } else {
|
||
197 | drawingStatus.addStatus(geometry, symbol, "");
|
||
198 | } |
||
199 | // drawingStatus.addGeometry(geometry);
|
||
200 | 261 | llmarques | } |
201 | } catch (BaseException e) {
|
||
202 | throw new DrawServiceException(e); |
||
203 | } finally {
|
||
204 | it.dispose(); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | return drawingStatus;
|
||
209 | } |
||
210 | |||
211 | private AffineTransform getMoveAffineTransform(Point p1, Point p2) |
||
212 | throws GeometryOperationNotSupportedException,
|
||
213 | GeometryOperationException { |
||
214 | |||
215 | AffineTransform translate =
|
||
216 | AffineTransform.getTranslateInstance(p2.getX() - p1.getX(),
|
||
217 | p2.getY() - p1.getY()); |
||
218 | |||
219 | return translate;
|
||
220 | } |
||
221 | |||
222 | public void stop() throws StopServiceException { |
||
223 | if (values != null) { |
||
224 | values.clear(); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | public List<EditingServiceParameter> getParameters() { |
||
229 | List<EditingServiceParameter> parameters =
|
||
230 | new ArrayList<EditingServiceParameter>(); |
||
231 | parameters.add(selection); |
||
232 | parameters.add(firstPoint); |
||
233 | parameters.add(secondPoint); |
||
234 | return parameters;
|
||
235 | } |
||
236 | |||
237 | public void setValue(Object value) throws InvalidEntryException { |
||
238 | EditingServiceParameter parameter = next(); |
||
239 | validateAndInsertValue(parameter, value); |
||
240 | } |
||
241 | |||
242 | private void validateAndInsertValue(EditingServiceParameter parameter, |
||
243 | Object value) {
|
||
244 | |||
245 | if (parameter == selection) {
|
||
246 | if (value instanceof FeatureSelection) { |
||
247 | if (((FeatureSelection) value).getSelectedCount() > 0) { |
||
248 | values.put(selection, value); |
||
249 | } |
||
250 | } |
||
251 | } else if (parameter == firstPoint || parameter == secondPoint) { |
||
252 | if (value instanceof Point) { |
||
253 | values.put(parameter, value); |
||
254 | return;
|
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | public Geometry finish() throws FinishServiceException { |
||
260 | return null; |
||
261 | } |
||
262 | |||
263 | public void finishAndStore() throws FinishServiceException { |
||
264 | if (values != null) { |
||
265 | |||
266 | final Point p1 = (Point) values.get(firstPoint); |
||
267 | final Point p2 = (Point) values.get(secondPoint); |
||
268 | |||
269 | FeatureSelection featureSelection = |
||
270 | (FeatureSelection) values.get(selection); |
||
271 | |||
272 | try {
|
||
273 | featureSelection.accept(new Visitor() {
|
||
274 | |||
275 | public void visit(Object obj) |
||
276 | throws VisitCanceledException, BaseException {
|
||
277 | Feature feature = (Feature) obj; |
||
278 | |||
279 | EditingProviderServices editingProviderServices = |
||
280 | (EditingProviderServices) getProviderServices(); |
||
281 | |||
282 | EditableFeature editable = |
||
283 | editingProviderServices.getFeatureCopyWithoutPK( |
||
284 | featureStore, feature); |
||
285 | |||
286 | Geometry geometry = |
||
287 | feature.getDefaultGeometry().cloneGeometry(); |
||
288 | geometry.transform(getMoveAffineTransform(p1, p2)); |
||
289 | |||
290 | editable.setDefaultGeometry(geometry); |
||
291 | editingProviderServices.insertFeatureIntoFeatureStore( |
||
292 | editable, featureStore); |
||
293 | } |
||
294 | }); |
||
295 | |||
296 | featureStore.getFeatureSelection().deselectAll(); |
||
297 | } catch (BaseException e) {
|
||
298 | throw new FinishServiceException(e); |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | |||
303 | public void start() throws StartServiceException, InvalidEntryException { |
||
304 | values = new HashMap<EditingServiceParameter, Object>(); |
||
305 | FeatureSelection selected = null;
|
||
306 | if (featureStore != null) { |
||
307 | try {
|
||
308 | selected = featureStore.getFeatureSelection(); |
||
309 | } catch (DataException e) {
|
||
310 | throw new StartServiceException(e); |
||
311 | } |
||
312 | try {
|
||
313 | setValue(selected); |
||
314 | } catch (InvalidEntryException e) {
|
||
315 | throw new InvalidEntryException(e); |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | |||
320 | public String getName() { |
||
321 | return DuplicateEditingProviderFactory.PROVIDER_NAME;
|
||
322 | } |
||
323 | |||
324 | } |