gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.rectangularmatrix / src / main / java / org / gvsig / vectorediting / lib / prov / rectangularmatrix / RectangularMatrixEditingProvider.java @ 359
History | View | Annotate | Download (13.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2015 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.rectangularmatrix; |
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 |
import org.gvsig.fmap.geom.primitive.Point; |
40 |
import org.gvsig.tools.dispose.DisposableIterator; |
41 |
import org.gvsig.tools.dynobject.DynObject; |
42 |
import org.gvsig.tools.exception.BaseException; |
43 |
import org.gvsig.tools.service.spi.ProviderServices; |
44 |
import org.gvsig.tools.visitor.VisitCanceledException; |
45 |
import org.gvsig.tools.visitor.Visitor; |
46 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
47 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
48 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
53 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
54 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
55 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
56 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
59 |
|
60 |
/**
|
61 |
* @author llmarques
|
62 |
*
|
63 |
*/
|
64 |
public class RectangularMatrixEditingProvider extends AbstractEditingProvider |
65 |
implements EditingProvider {
|
66 |
|
67 |
private FeatureStore featureStore;
|
68 |
|
69 |
private Map<EditingServiceParameter, Object> values; |
70 |
|
71 |
private EditingServiceParameter selection;
|
72 |
|
73 |
private EditingServiceParameter columns;
|
74 |
|
75 |
private EditingServiceParameter rows;
|
76 |
|
77 |
private EditingServiceParameter basePoint;
|
78 |
|
79 |
private EditingServiceParameter columnSpacing;
|
80 |
|
81 |
private EditingServiceParameter rowSpacing;
|
82 |
|
83 |
/**
|
84 |
* Default constructor.
|
85 |
*
|
86 |
* @param providerServices
|
87 |
* available services for this provider
|
88 |
* @param parameters
|
89 |
* of this provider
|
90 |
*/
|
91 |
public RectangularMatrixEditingProvider(DynObject parameters,
|
92 |
ProviderServices services) { |
93 |
super(services);
|
94 |
|
95 |
this.featureStore =
|
96 |
(FeatureStore) parameters |
97 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
98 |
|
99 |
this.selection =
|
100 |
new DefaultEditingServiceParameter("selection", "selection", |
101 |
EditingServiceParameter.TYPE.SELECTION); |
102 |
|
103 |
this.basePoint =
|
104 |
new DefaultEditingServiceParameter("indicate_new_point", "indicate_new_point", |
105 |
EditingServiceParameter.TYPE.POSITION); |
106 |
|
107 |
this.columns =
|
108 |
new DefaultEditingServiceParameter("columns_number", "columns_number", |
109 |
EditingServiceParameter.TYPE.VALUE); |
110 |
|
111 |
this.rows =
|
112 |
new DefaultEditingServiceParameter("rows_number", "rows_number", |
113 |
EditingServiceParameter.TYPE.VALUE); |
114 |
|
115 |
this.columnSpacing =
|
116 |
new DefaultEditingServiceParameter("space_between_columns", |
117 |
"spacing_between_columns", EditingServiceParameter.TYPE.VALUE,
|
118 |
EditingServiceParameter.TYPE.POSITION); |
119 |
|
120 |
this.rowSpacing =
|
121 |
new DefaultEditingServiceParameter("space_between_rows", |
122 |
"spacing_between_rows", EditingServiceParameter.TYPE.VALUE,
|
123 |
EditingServiceParameter.TYPE.POSITION); |
124 |
} |
125 |
|
126 |
public EditingServiceParameter next() {
|
127 |
if (values.get(selection) == null) { |
128 |
return selection;
|
129 |
} else if (values.get(columns) == null) { |
130 |
return columns;
|
131 |
} else if (values.get(rows) == null) { |
132 |
return rows;
|
133 |
} else if (values.get(basePoint) == null) { |
134 |
return basePoint;
|
135 |
} else if (values.get(columnSpacing) == null) { |
136 |
return columnSpacing;
|
137 |
} else if (values.get(rowSpacing) == null) { |
138 |
return rowSpacing;
|
139 |
} |
140 |
return null; |
141 |
} |
142 |
|
143 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
144 |
throws DrawServiceException {
|
145 |
|
146 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
147 |
|
148 |
FeatureSelection featureSelection = |
149 |
(FeatureSelection) values.get(selection); |
150 |
Integer columnsValue = (Integer) values.get(columns); |
151 |
Integer rowsValue = (Integer) values.get(rows); |
152 |
Point basePointValue = (Point) values.get(basePoint); |
153 |
Double spacingColumnsValue = (Double) values.get(columnSpacing); |
154 |
Double spacingRowsValue = (Double) values.get(rowSpacing); |
155 |
|
156 |
if (featureSelection != null && columnsValue != null |
157 |
&& rowsValue != null && basePointValue != null) { |
158 |
|
159 |
if (spacingColumnsValue == null) { |
160 |
spacingColumnsValue = |
161 |
mousePosition.getX() - basePointValue.getX(); |
162 |
} |
163 |
|
164 |
if (spacingRowsValue == null) { |
165 |
spacingRowsValue = mousePosition.getY() - basePointValue.getY(); |
166 |
} |
167 |
|
168 |
DisposableIterator it = null;
|
169 |
try {
|
170 |
it = featureSelection.fastIterator(); |
171 |
while (it.hasNext()) {
|
172 |
Feature feature = (Feature) it.next(); |
173 |
Geometry geometry = feature.getDefaultGeometry(); |
174 |
Geometry[][] matrix = |
175 |
createRectangularMatrix(geometry, columnsValue, |
176 |
rowsValue, basePointValue, spacingColumnsValue, |
177 |
spacingRowsValue); |
178 |
|
179 |
for (int i = 0; i < matrix.length; i++) { |
180 |
for (int j = 0; j < matrix[i].length; j++) { |
181 |
drawingStatus.addGeometry(matrix[i][j]); |
182 |
} |
183 |
} |
184 |
} |
185 |
} catch (BaseException e) {
|
186 |
throw new DrawServiceException(e); |
187 |
} finally {
|
188 |
it.dispose(); |
189 |
} |
190 |
} |
191 |
return drawingStatus;
|
192 |
} |
193 |
|
194 |
private Geometry[][] createRectangularMatrix(Geometry geometry, |
195 |
int columnsValue, int rowsValue, Point basePointValue, |
196 |
double spacingColumns, double spacingRows) throws BaseException { |
197 |
|
198 |
Geometry[][] geometryMatrix = new Geometry[columnsValue][rowsValue]; |
199 |
|
200 |
for (int i = 0; i < columnsValue; i++) { |
201 |
|
202 |
for (int j = 0; j < rowsValue; j++) { |
203 |
|
204 |
if (i == 0 && j == 0) { |
205 |
continue;
|
206 |
} |
207 |
|
208 |
AffineTransform at = new AffineTransform(); |
209 |
at.translate(spacingColumns * i, spacingRows * j); |
210 |
Geometry clonedGeoemtry = geometry.cloneGeometry(); |
211 |
clonedGeoemtry.transform(at); |
212 |
geometryMatrix[i][j] = clonedGeoemtry; |
213 |
} |
214 |
} |
215 |
|
216 |
return geometryMatrix;
|
217 |
} |
218 |
|
219 |
public void stop() throws StopServiceException { |
220 |
values.clear(); |
221 |
} |
222 |
|
223 |
public List<EditingServiceParameter> getParameters() { |
224 |
List<EditingServiceParameter> parameters =
|
225 |
new ArrayList<EditingServiceParameter>(); |
226 |
parameters.add(basePoint); |
227 |
parameters.add(columns); |
228 |
parameters.add(rows); |
229 |
parameters.add(columnSpacing); |
230 |
parameters.add(rowSpacing); |
231 |
return parameters;
|
232 |
} |
233 |
|
234 |
public void setValue(Object value) throws InvalidEntryException { |
235 |
EditingServiceParameter parameter = next(); |
236 |
validateAndInsertValue(parameter, value); |
237 |
} |
238 |
|
239 |
private void validateAndInsertValue(EditingServiceParameter parameter, |
240 |
Object value) throws InvalidEntryException { |
241 |
boolean insertedValue = false; |
242 |
|
243 |
if (parameter.equals(selection) && value instanceof FeatureSelection) { |
244 |
FeatureSelection featureSelection = (FeatureSelection) value; |
245 |
if (featureSelection.getSelectedCount() > 0) { |
246 |
values.put(parameter, featureSelection); |
247 |
insertedValue = true;
|
248 |
} |
249 |
} else if (parameter.equals(columns) || parameter.equals(rows)) { |
250 |
|
251 |
if (value instanceof Double) { |
252 |
Double number = (Double) value; |
253 |
values.put(parameter, number.intValue()); |
254 |
insertedValue = true;
|
255 |
} |
256 |
|
257 |
} else if (parameter.equals(basePoint) && value instanceof Point) { |
258 |
|
259 |
values.put(basePoint, value); |
260 |
insertedValue = true;
|
261 |
|
262 |
} else if (parameter.equals(columnSpacing) |
263 |
|| parameter.equals(rowSpacing)) { |
264 |
|
265 |
if (value instanceof Double) { |
266 |
Double doubleValue = (Double) value; |
267 |
values.put(parameter, doubleValue); |
268 |
insertedValue = true;
|
269 |
|
270 |
} else if (value instanceof Point) { |
271 |
|
272 |
Point basePointValue = (Point) values.get(basePoint); |
273 |
Point valuePoint = (Point) value; |
274 |
double distance = 0; |
275 |
if (parameter.equals(columnSpacing)) {
|
276 |
distance = valuePoint.getX() - basePointValue.getX(); |
277 |
values.put(columnSpacing, distance); |
278 |
} else if (parameter.equals(rowSpacing)) { |
279 |
distance = valuePoint.getY() - basePointValue.getY(); |
280 |
values.put(rowSpacing, distance); |
281 |
} |
282 |
insertedValue = true;
|
283 |
} |
284 |
} |
285 |
|
286 |
if (!insertedValue) {
|
287 |
throw new InvalidEntryException(null); |
288 |
} |
289 |
} |
290 |
|
291 |
public Geometry finish() throws FinishServiceException { |
292 |
return null; |
293 |
} |
294 |
|
295 |
public void finishAndStore() throws FinishServiceException { |
296 |
|
297 |
FeatureSelection featureSelection = |
298 |
(FeatureSelection) values.get(selection); |
299 |
final Integer columnsValue = (Integer) values.get(columns); |
300 |
final Integer rowsValue = (Integer) values.get(rows); |
301 |
final Point basePointValue = (Point) values.get(basePoint); |
302 |
final Double spacingColumnsValue = (Double) values.get(columnSpacing); |
303 |
final Double spacingRowsValue = (Double) values.get(rowSpacing); |
304 |
|
305 |
final EditingProviderServices editingProviderServices =
|
306 |
(EditingProviderServices) getProviderServices(); |
307 |
|
308 |
if (featureSelection != null && columnsValue != null |
309 |
&& rowsValue != null && basePointValue != null |
310 |
&& spacingColumnsValue != null && spacingRowsValue != null) { |
311 |
|
312 |
try {
|
313 |
featureSelection.accept(new Visitor() {
|
314 |
|
315 |
public void visit(Object obj) |
316 |
throws VisitCanceledException, BaseException {
|
317 |
|
318 |
Feature feature = (Feature) obj; |
319 |
Geometry geometry = feature.getDefaultGeometry(); |
320 |
Geometry[][] matrix = |
321 |
createRectangularMatrix(geometry, columnsValue, |
322 |
rowsValue, basePointValue, spacingColumnsValue, |
323 |
spacingRowsValue); |
324 |
|
325 |
for (int i = 0; i < matrix.length; i++) { |
326 |
for (int j = 0; j < matrix[i].length; j++) { |
327 |
EditableFeature eFeature = |
328 |
editingProviderServices |
329 |
.getFeatureCopyWithoutPK(featureStore, |
330 |
feature); |
331 |
eFeature.setDefaultGeometry(matrix[i][j]); |
332 |
editingProviderServices |
333 |
.insertFeatureIntoFeatureStore(eFeature, |
334 |
featureStore); |
335 |
} |
336 |
} |
337 |
} |
338 |
}); |
339 |
} catch (BaseException e) {
|
340 |
throw new FinishServiceException(e); |
341 |
} |
342 |
} |
343 |
} |
344 |
|
345 |
public void start() throws StartServiceException, InvalidEntryException { |
346 |
values = new HashMap<EditingServiceParameter, Object>(); |
347 |
FeatureSelection selected = null;
|
348 |
if (featureStore != null && selection == null) { |
349 |
try {
|
350 |
selected = featureStore.getFeatureSelection(); |
351 |
} catch (DataException e) {
|
352 |
throw new StartServiceException(e); |
353 |
} |
354 |
if (selected.getSelectedCount() > 0) { |
355 |
try {
|
356 |
setValue(selected); |
357 |
} catch (InvalidEntryException e) {
|
358 |
throw new InvalidEntryException(e); |
359 |
} |
360 |
} |
361 |
} |
362 |
} |
363 |
|
364 |
public String getName() { |
365 |
return RectangularMatrixEditingProviderFactory.PROVIDER_NAME;
|
366 |
} |
367 |
} |