gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.converttopolyline / src / main / java / org / gvsig / vectorediting / lib / prov / converttopolyline / ConvertToPolylineEditingProvider.java @ 4300
History | View | Annotate | Download (9.1 KB)
1 | 548 | llmarques | /* gvSIG. Desktop Geographic Information System.
|
---|---|---|---|
2 | *
|
||
3 | * Copyright ? 2007-2015 gvSIG Association
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
18 | * MA 02110-1301, USA.
|
||
19 | *
|
||
20 | * For any additional information, do not hesitate to contact us
|
||
21 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
22 | */
|
||
23 | package org.gvsig.vectorediting.lib.prov.converttopolyline; |
||
24 | |||
25 | import java.util.ArrayList; |
||
26 | import java.util.List; |
||
27 | import org.gvsig.fmap.dal.exception.DataException; |
||
28 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
29 | import org.gvsig.fmap.dal.feature.Feature; |
||
30 | import org.gvsig.fmap.dal.feature.FeatureSelection; |
||
31 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
32 | import org.gvsig.fmap.geom.Geometry; |
||
33 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
34 | import org.gvsig.fmap.geom.GeometryLocator; |
||
35 | import org.gvsig.fmap.geom.GeometryManager; |
||
36 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
37 | import org.gvsig.fmap.geom.primitive.Curve; |
||
38 | import org.gvsig.fmap.geom.primitive.Line; |
||
39 | import org.gvsig.fmap.geom.primitive.Point; |
||
40 | import org.gvsig.fmap.geom.type.GeometryType; |
||
41 | import org.gvsig.tools.ToolsLocator; |
||
42 | import org.gvsig.tools.dispose.DisposableIterator; |
||
43 | import org.gvsig.tools.dispose.DisposeUtils; |
||
44 | import org.gvsig.tools.dynobject.DynObject; |
||
45 | import org.gvsig.tools.exception.BaseException; |
||
46 | import org.gvsig.tools.service.spi.ProviderServices; |
||
47 | import org.gvsig.vectorediting.lib.api.DrawingStatus; |
||
48 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
||
49 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
||
50 | import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
||
51 | import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
||
52 | import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
||
53 | import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
||
54 | import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
||
55 | import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
||
56 | import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
||
57 | import org.gvsig.vectorediting.lib.spi.EditingProvider; |
||
58 | import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
||
59 | import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
||
60 | |||
61 | public class ConvertToPolylineEditingProvider extends AbstractEditingProvider |
||
62 | implements EditingProvider {
|
||
63 | |||
64 | 2616 | fdiaz | private final EditingServiceParameter selectionParameter; |
65 | 548 | llmarques | |
66 | private FeatureSelection featureSelection;
|
||
67 | |||
68 | 2616 | fdiaz | private final FeatureStore featureStore; |
69 | 548 | llmarques | |
70 | public ConvertToPolylineEditingProvider(ProviderServices providerServices,
|
||
71 | DynObject parameters) { |
||
72 | super(providerServices);
|
||
73 | |||
74 | this.featureStore =
|
||
75 | (FeatureStore) parameters |
||
76 | .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
||
77 | |||
78 | this.selectionParameter =
|
||
79 | new DefaultEditingServiceParameter("selection", "selection", |
||
80 | TYPE.SELECTION); |
||
81 | } |
||
82 | |||
83 | 2616 | fdiaz | @Override
|
84 | 548 | llmarques | public EditingServiceParameter next() {
|
85 | if (this.featureSelection == null) { |
||
86 | return selectionParameter;
|
||
87 | } |
||
88 | return null; |
||
89 | } |
||
90 | 4300 | fdiaz | |
91 | @Override
|
||
92 | public boolean mustRestartAtFinish() { |
||
93 | return false; |
||
94 | } |
||
95 | 548 | llmarques | |
96 | 2616 | fdiaz | @Override
|
97 | 548 | llmarques | public DrawingStatus getDrawingStatus(Point mousePosition) |
98 | throws DrawServiceException {
|
||
99 | return null; |
||
100 | } |
||
101 | |||
102 | 2616 | fdiaz | @Override
|
103 | 548 | llmarques | public void stop() throws StopServiceException { |
104 | DisposeUtils.disposeQuietly(featureSelection); |
||
105 | featureSelection = null;
|
||
106 | |||
107 | } |
||
108 | |||
109 | 2616 | fdiaz | @Override
|
110 | 548 | llmarques | public List<EditingServiceParameter> getParameters() { |
111 | List<EditingServiceParameter> parameters =
|
||
112 | 2616 | fdiaz | new ArrayList<>(); |
113 | 548 | llmarques | parameters.add(selectionParameter); |
114 | return parameters;
|
||
115 | } |
||
116 | |||
117 | 2204 | fdiaz | @Override
|
118 | public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException { |
||
119 | validateAndInsertValue(parameter, value); |
||
120 | } |
||
121 | |||
122 | 2616 | fdiaz | @Override
|
123 | 548 | llmarques | public void setValue(Object value) throws InvalidEntryException { |
124 | EditingServiceParameter parameter = next(); |
||
125 | validateAndInsertValue(parameter, value); |
||
126 | |||
127 | } |
||
128 | |||
129 | private void validateAndInsertValue(EditingServiceParameter parameter, |
||
130 | Object value) throws InvalidEntryException { |
||
131 | |||
132 | if (parameter == selectionParameter) {
|
||
133 | |||
134 | if (value instanceof FeatureSelection) { |
||
135 | FeatureSelection fSelection = (FeatureSelection) value; |
||
136 | |||
137 | if (fSelection.getSelectedCount() >= 1) { |
||
138 | DisposableIterator it = null;
|
||
139 | |||
140 | try {
|
||
141 | it = fSelection.fastIterator(); |
||
142 | |||
143 | while (it.hasNext()) {
|
||
144 | |||
145 | Feature feature = (Feature) it.next(); |
||
146 | // Gets geometry type of first selected feature
|
||
147 | GeometryType geometryType = |
||
148 | feature.getDefaultGeometry().getGeometryType(); |
||
149 | |||
150 | if (!geometryType.isTypeOf(TYPES.CURVE)
|
||
151 | && !geometryType.isTypeOf(TYPES.MULTICURVE)) { |
||
152 | |||
153 | throw new InvalidEntryException(null); |
||
154 | } |
||
155 | } |
||
156 | DisposeUtils.disposeQuietly(featureSelection); |
||
157 | ToolsLocator.getDisposableManager().bind(fSelection); |
||
158 | featureSelection = fSelection; |
||
159 | } catch (DataException e) {
|
||
160 | throw new InvalidEntryException(e); |
||
161 | } finally {
|
||
162 | 2616 | fdiaz | DisposeUtils.disposeQuietly(it); |
163 | 548 | llmarques | } |
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | 2616 | fdiaz | @Override
|
170 | 548 | llmarques | public Geometry finish() throws FinishServiceException { |
171 | return null; |
||
172 | } |
||
173 | |||
174 | 2616 | fdiaz | @Override
|
175 | 548 | llmarques | public void finishAndStore() throws FinishServiceException { |
176 | |||
177 | if (featureSelection != null) { |
||
178 | |||
179 | GeometryManager geometryManager = |
||
180 | GeometryLocator.getGeometryManager(); |
||
181 | |||
182 | EditingProviderServices editingProviderServices = |
||
183 | (EditingProviderServices) getProviderServices(); |
||
184 | |||
185 | DisposableIterator it = null;
|
||
186 | try {
|
||
187 | it = featureSelection.fastIterator(); |
||
188 | while (it.hasNext()) {
|
||
189 | Feature feature = (Feature) it.next(); |
||
190 | EditableFeature eFeature = feature.getEditable(); |
||
191 | Geometry geometry = feature.getDefaultGeometry(); |
||
192 | |||
193 | if (geometry.getGeometryType().isTypeOf(TYPES.CURVE)) {
|
||
194 | |||
195 | 575 | fdiaz | eFeature.setDefaultGeometry(geometry.toLines().getPrimitiveAt(0));
|
196 | 548 | llmarques | |
197 | 575 | fdiaz | } else if (geometry.getGeometryType().isTypeOf(TYPES.MULTICURVE)) { |
198 | 548 | llmarques | |
199 | 575 | fdiaz | eFeature.setDefaultGeometry(geometry.toLines()); |
200 | 548 | llmarques | } |
201 | DisposeUtils.disposeQuietly(featureSelection); |
||
202 | editingProviderServices.updateFeatureInFeatureStore( |
||
203 | eFeature, featureStore); |
||
204 | } |
||
205 | 575 | fdiaz | |
206 | 548 | llmarques | featureStore.getFeatureSelection().deselectAll(); |
207 | } catch (BaseException e) {
|
||
208 | throw new FinishServiceException(e); |
||
209 | } finally {
|
||
210 | 2616 | fdiaz | DisposeUtils.disposeQuietly(it); |
211 | 548 | llmarques | } |
212 | 575 | fdiaz | |
213 | 548 | llmarques | } |
214 | } |
||
215 | |||
216 | private Line convertCurveToLine(Curve curve) throws CreateGeometryException { |
||
217 | GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
||
218 | Line newLine =
|
||
219 | geometryManager.createLine(curve.getGeometryType().getSubType()); |
||
220 | for (int i = 0; i < curve.getNumVertices(); i++) { |
||
221 | newLine.addVertex(curve.getVertex(i)); |
||
222 | } |
||
223 | return newLine;
|
||
224 | } |
||
225 | |||
226 | 2616 | fdiaz | @Override
|
227 | 548 | llmarques | public void start() throws StartServiceException, InvalidEntryException { |
228 | FeatureSelection selected = null;
|
||
229 | if (featureStore != null) { |
||
230 | try {
|
||
231 | selected = featureStore.getFeatureSelection(); |
||
232 | } catch (DataException e) {
|
||
233 | throw new StartServiceException(e); |
||
234 | } |
||
235 | if (selected.getSelectedCount() >= 1) { |
||
236 | try {
|
||
237 | setValue(selected); |
||
238 | } catch (InvalidEntryException e) {
|
||
239 | throw new InvalidEntryException(e); |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | 2616 | fdiaz | @Override
|
246 | 548 | llmarques | public String getName() { |
247 | return ConvertToPolylineEditingProviderFactory.PROVIDER_NAME;
|
||
248 | } |
||
249 | |||
250 | 2444 | fdiaz | @Override
|
251 | public Object getValue(EditingServiceParameter parameter) { |
||
252 | if(parameter == this.selectionParameter){ |
||
253 | return featureSelection;
|
||
254 | } |
||
255 | return null; |
||
256 | } |
||
257 | 548 | llmarques | } |