root / tags / v2_0_0_Build_2056 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / ExploitCADTool.java @ 39004
History | View | Annotate | Download (16.6 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 |
*/
|
22 |
package org.gvsig.editing.gui.cad.tools; |
23 |
|
24 |
import java.awt.event.InputEvent; |
25 |
import java.awt.geom.PathIterator; |
26 |
import java.awt.geom.Point2D; |
27 |
|
28 |
import javax.swing.JOptionPane; |
29 |
|
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
import org.gvsig.andami.PluginServices; |
34 |
import org.gvsig.andami.messages.NotificationManager; |
35 |
import org.gvsig.app.ApplicationLocator; |
36 |
import org.gvsig.editing.CADExtension; |
37 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
38 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
39 |
import org.gvsig.editing.gui.cad.tools.smc.ExploitCADToolContext; |
40 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.exception.ReadException; |
43 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
46 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
47 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
48 |
import org.gvsig.fmap.geom.Geometry; |
49 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
50 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
51 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
52 |
import org.gvsig.tools.dispose.DisposableIterator; |
53 |
|
54 |
//import com.vividsolutions.jts.geom.GeometryCollection;
|
55 |
|
56 |
/**
|
57 |
* Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
|
58 |
* componen, implementado para polil?neas y pol?gonos.
|
59 |
*
|
60 |
* @author Vicente Caballero Navarro
|
61 |
*/
|
62 |
public class ExploitCADTool extends DefaultCADTool { |
63 |
|
64 |
private static Logger logger = LoggerFactory.getLogger(ExploitCADTool.class); |
65 |
|
66 |
protected ExploitCADToolContext _fsm;
|
67 |
|
68 |
/**
|
69 |
* M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
70 |
* carga previa a la utilizaci?n de la herramienta.
|
71 |
*/
|
72 |
public void init() { |
73 |
_fsm = new ExploitCADToolContext(this); |
74 |
} |
75 |
|
76 |
/*
|
77 |
* (non-Javadoc)
|
78 |
*
|
79 |
* @see
|
80 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
81 |
* .layers.FBitSet, double, double)
|
82 |
*/
|
83 |
public void transition(double x, double y, InputEvent event) { |
84 |
_fsm.addPoint(x, y, event); |
85 |
} |
86 |
|
87 |
/*
|
88 |
* (non-Javadoc)
|
89 |
*
|
90 |
* @see
|
91 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
92 |
* .layers.FBitSet, double)
|
93 |
*/
|
94 |
public void transition(double d) { |
95 |
_fsm.addValue(d); |
96 |
} |
97 |
|
98 |
/*
|
99 |
* (non-Javadoc)
|
100 |
*
|
101 |
* @see
|
102 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
103 |
* .layers.FBitSet, java.lang.String)
|
104 |
*/
|
105 |
public void transition(String s) throws CommandException { |
106 |
if (!super.changeCommand(s)) { |
107 |
_fsm.addOption(s); |
108 |
} |
109 |
} |
110 |
|
111 |
/**
|
112 |
* DOCUMENT ME!
|
113 |
*/
|
114 |
public void selection() { |
115 |
FeatureSet selection = null;
|
116 |
try {
|
117 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
118 |
|
119 |
if (selection.getSize() == 0 |
120 |
&& !CADExtension |
121 |
.getCADTool() |
122 |
.getClass() |
123 |
.getName() |
124 |
.equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
125 |
CADExtension.setCADTool("_selection", false); |
126 |
((SelectionCADTool) CADExtension.getCADTool()) |
127 |
.setNextTool("_exploit");
|
128 |
} |
129 |
} catch (ReadException e) {
|
130 |
// TODO Auto-generated catch block
|
131 |
e.printStackTrace(); |
132 |
} catch (DataException e) {
|
133 |
// TODO Auto-generated catch block
|
134 |
e.printStackTrace(); |
135 |
} |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
140 |
* editableFeatureSource que ya estar? creado.
|
141 |
*
|
142 |
* @param x
|
143 |
* par?metro x del punto que se pase en esta transici?n.
|
144 |
* @param y
|
145 |
* par?metro y del punto que se pase en esta transici?n.
|
146 |
*/
|
147 |
public void addPoint(double x, double y, InputEvent event) { |
148 |
} |
149 |
|
150 |
/**
|
151 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
152 |
* encontremos.
|
153 |
*
|
154 |
* @param g
|
155 |
* Graphics sobre el que dibujar.
|
156 |
* @param x
|
157 |
* par?metro x del punto que se pase para dibujar.
|
158 |
* @param y
|
159 |
* par?metro x del punto que se pase para dibujar.
|
160 |
*/
|
161 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
162 |
} |
163 |
|
164 |
public void exploit() { |
165 |
VectorialLayerEdited vle = getVLE(); |
166 |
DisposableIterator iterator = null;
|
167 |
try {
|
168 |
FeatureStore featureStore = vle.getFeatureStore(); |
169 |
iterator = |
170 |
((FeatureSelection) featureStore.getSelection()).iterator(); |
171 |
FeatureSelection newSelection = |
172 |
featureStore.createFeatureSelection(); |
173 |
// ArrayList selectedRowAux = new ArrayList();
|
174 |
featureStore.beginEditingGroup(getName()); |
175 |
|
176 |
while (iterator.hasNext()) {
|
177 |
Feature feature = (Feature) iterator.next(); |
178 |
Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
179 |
featureStore.delete(feature); |
180 |
if (geom instanceof MultiPrimitive) { |
181 |
exploitGeometryCollection(feature, newSelection); |
182 |
} else {
|
183 |
if (geom.getType() == CURVE) {
|
184 |
exploitLine(feature, newSelection); |
185 |
} else
|
186 |
if (geom.getType() == SURFACE) {
|
187 |
exploitPolygon(feature, newSelection); |
188 |
} |
189 |
} |
190 |
|
191 |
} |
192 |
// clearSelection();
|
193 |
featureStore.setSelection(newSelection); |
194 |
featureStore.endEditingGroup(); |
195 |
} catch (DataException e) {
|
196 |
NotificationManager.addError(e.getMessage(), e); |
197 |
} finally {
|
198 |
if (iterator != null) { |
199 |
iterator.dispose(); |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
private void exploitGeometryCollection(Feature feature, |
205 |
FeatureSelection selectedRowAux) { |
206 |
Geometry geom = (feature.getDefaultGeometry()).cloneGeometry(); |
207 |
MultiPrimitive gc = (MultiPrimitive) geom; |
208 |
int numGeoms = gc.getPrimitivesNumber();
|
209 |
FeatureStore featureStore; |
210 |
Feature f = null;
|
211 |
try {
|
212 |
featureStore = getVLE().getFeatureStore(); |
213 |
for (int i = 0; i < numGeoms; i++) { |
214 |
Geometry primitiveGeom = gc.getPrimitiveAt(i); |
215 |
f = insertGeometry(primitiveGeom, feature); |
216 |
/*
|
217 |
EditableFeature eFeature =
|
218 |
featureStore.createNewFeature(feature.getType(), feature);
|
219 |
eFeature.setGeometry(featureStore.getDefaultFeatureType()
|
220 |
.getDefaultGeometryAttributeName(), primitiveGeom);
|
221 |
*/
|
222 |
selectedRowAux.select(f); |
223 |
} |
224 |
} catch (ReadException e) {
|
225 |
// TODO Auto-generated catch block
|
226 |
e.printStackTrace(); |
227 |
} catch (DataException e) {
|
228 |
// TODO Auto-generated catch block
|
229 |
e.printStackTrace(); |
230 |
} |
231 |
refresh(); |
232 |
} |
233 |
|
234 |
private void exploitLine(Feature feature, FeatureSelection selectedRowAux) { |
235 |
GeneralPathX newGp1 = new GeneralPathX();
|
236 |
// DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
|
237 |
// .cloneRow();
|
238 |
// Point2D firstPoint=null;
|
239 |
// Point2D lastPoint=null;
|
240 |
// Object[] attributes=new Object[feature.size()];
|
241 |
// for (int j = 0; j < feature.size(); j++) {
|
242 |
// attributes[j]=feature.get(j);
|
243 |
// }
|
244 |
FeatureStore featureStore; |
245 |
// try {
|
246 |
try {
|
247 |
featureStore = getVLE().getFeatureStore(); |
248 |
|
249 |
// EditableFeature
|
250 |
// eFeature=featureStore.createNewFeature(feature.getType(),
|
251 |
// feature);
|
252 |
PathIterator theIterator =
|
253 |
(feature.getDefaultGeometry()).getPathIterator(null,
|
254 |
geomManager.getFlatness()); |
255 |
double[] theData = new double[6]; |
256 |
int theType;
|
257 |
int numParts = 0; |
258 |
Point2D initialPoint = null; |
259 |
Feature f = null;
|
260 |
|
261 |
while (!theIterator.isDone()) {
|
262 |
theType = theIterator.currentSegment(theData); |
263 |
switch (theType) {
|
264 |
|
265 |
case PathIterator.SEG_MOVETO: |
266 |
numParts++; |
267 |
newGp1 = new GeneralPathX();
|
268 |
// firstPoint=new Point2D.Double(theData[0], theData[1]);
|
269 |
initialPoint = new Point2D.Double(theData[0], theData[1]); |
270 |
newGp1.moveTo(theData[0], theData[1]); |
271 |
// lastPoint=new Point2D.Double(theData[0], theData[1]);
|
272 |
break;
|
273 |
|
274 |
case PathIterator.SEG_LINETO: |
275 |
newGp1.lineTo(theData[0], theData[1]); |
276 |
Geometry geomLine = createCurve(newGp1); |
277 |
// DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
|
278 |
// dfLine.setGeometry(geomLine);
|
279 |
// EditableFeature
|
280 |
// eFeature=featureStore.createNewFeature(feature.getType(),
|
281 |
// feature);
|
282 |
// eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),geomLine);
|
283 |
f = insertGeometry(geomLine, feature); |
284 |
// featureStore.insert(eFeature);
|
285 |
selectedRowAux.select(f); |
286 |
newGp1 = new GeneralPathX();
|
287 |
newGp1.moveTo(theData[0], theData[1]); |
288 |
// lastPoint=new Point2D.Double(theData[0], theData[1]);
|
289 |
break;
|
290 |
case PathIterator.SEG_CLOSE: |
291 |
if (initialPoint != null) { |
292 |
newGp1.lineTo(initialPoint.getX(), initialPoint.getY()); |
293 |
geomLine = createCurve(newGp1); |
294 |
// dfLine = (DefaultFeature) df.cloneRow();
|
295 |
// dfLine.setGeometry(geomLine);
|
296 |
/*
|
297 |
EditableFeature eFeatureClose =
|
298 |
featureStore.createNewFeature(feature.getType(),
|
299 |
feature);
|
300 |
eFeatureClose.setGeometry(featureStore
|
301 |
.getDefaultFeatureType()
|
302 |
.getDefaultGeometryAttributeName(), geomLine);
|
303 |
selectedRowAux.select(eFeatureClose);
|
304 |
*/
|
305 |
|
306 |
f = insertGeometry(geomLine, feature); |
307 |
selectedRowAux.select(f); |
308 |
|
309 |
newGp1 = new GeneralPathX();
|
310 |
newGp1.moveTo(initialPoint.getX(), initialPoint.getY()); |
311 |
} |
312 |
break;
|
313 |
} // end switch
|
314 |
|
315 |
theIterator.next(); |
316 |
} // end while loop
|
317 |
} catch (ReadException e) {
|
318 |
// TODO Auto-generated catch block
|
319 |
e.printStackTrace(); |
320 |
} catch (DataException e) {
|
321 |
// TODO Auto-generated catch block
|
322 |
e.printStackTrace(); |
323 |
} |
324 |
refresh(); |
325 |
} |
326 |
|
327 |
private void exploitPolygon(Feature feature, FeatureSelection selectedRowAux) { |
328 |
GeneralPathX newGp1 = null;
|
329 |
// Object[] attributes=new Object[feature.size()];
|
330 |
// for (int j = 0; j < feature.size(); j++) {
|
331 |
// attributes[j]=feature.get(j);
|
332 |
// }
|
333 |
|
334 |
FeatureStore featureStore; |
335 |
Feature added_feat = null;
|
336 |
try {
|
337 |
featureStore = getVLE().getFeatureStore(); |
338 |
|
339 |
PathIterator theIterator =
|
340 |
(feature.getDefaultGeometry()).getPathIterator(null,
|
341 |
geomManager.getFlatness()); |
342 |
double[] theData = new double[6]; |
343 |
int theType;
|
344 |
int numParts = 0; |
345 |
Point2D initialPoint = null; |
346 |
while (!theIterator.isDone()) {
|
347 |
theType = theIterator.currentSegment(theData); |
348 |
switch (theType) {
|
349 |
|
350 |
case PathIterator.SEG_MOVETO: |
351 |
numParts++; |
352 |
if (newGp1 != null) { |
353 |
Geometry geomLine = createCurve(newGp1); |
354 |
|
355 |
/*
|
356 |
EditableFeature eFeature =
|
357 |
featureStore.createNewFeature(feature.getType(),
|
358 |
feature);
|
359 |
eFeature.setGeometry(featureStore
|
360 |
.getDefaultFeatureType()
|
361 |
.getDefaultGeometryAttributeName(), geomLine);
|
362 |
*/
|
363 |
|
364 |
added_feat = insertGeometry(geomLine, feature); |
365 |
selectedRowAux.select(added_feat); |
366 |
} |
367 |
newGp1 = new GeneralPathX();
|
368 |
initialPoint = new Point2D.Double(theData[0], theData[1]); |
369 |
newGp1.moveTo(theData[0], theData[1]); |
370 |
break;
|
371 |
|
372 |
case PathIterator.SEG_LINETO: |
373 |
newGp1.lineTo(theData[0], theData[1]); |
374 |
// newGp1 = new GeneralPathX();
|
375 |
// newGp1.moveTo(theData[0], theData[1]);
|
376 |
break;
|
377 |
|
378 |
case PathIterator.SEG_QUADTO: |
379 |
newGp1.quadTo(theData[0], theData[1], theData[2], |
380 |
theData[3]);
|
381 |
// newGp1 = new GeneralPathX();
|
382 |
// newGp1.moveTo(theData[0], theData[1]);
|
383 |
break;
|
384 |
case PathIterator.SEG_CUBICTO: |
385 |
newGp1.curveTo(theData[0], theData[1], theData[2], |
386 |
theData[3], theData[4], theData[5]); |
387 |
// newGp1 = new GeneralPathX();
|
388 |
// newGp1.moveTo(theData[0], theData[1]);
|
389 |
break;
|
390 |
case PathIterator.SEG_CLOSE: |
391 |
if (initialPoint != null) { |
392 |
newGp1.lineTo(initialPoint.getX(), initialPoint.getY()); |
393 |
} |
394 |
// System.out.println("aqu?");
|
395 |
// if (isFirstPart)
|
396 |
// newGp1.closePath();
|
397 |
// else
|
398 |
// newGp2.closePath();
|
399 |
break;
|
400 |
} // end switch
|
401 |
|
402 |
theIterator.next(); |
403 |
} // end while loop
|
404 |
if (newGp1 != null) { |
405 |
Geometry geomLine = createCurve(newGp1); |
406 |
/*
|
407 |
EditableFeature eFeature =
|
408 |
featureStore.createNewFeature(feature.getType(),
|
409 |
feature);
|
410 |
eFeature.setGeometry(featureStore
|
411 |
.getDefaultFeatureType()
|
412 |
.getDefaultGeometryAttributeName(), geomLine);
|
413 |
*/
|
414 |
|
415 |
added_feat = insertGeometry(geomLine, feature); |
416 |
selectedRowAux.select(added_feat); |
417 |
} |
418 |
} catch (Throwable e) { |
419 |
logger.info("While doing exploit of polygon.", e);
|
420 |
ApplicationLocator.getManager().message( |
421 |
"_Unable_to_exploit_polygon", JOptionPane.ERROR_MESSAGE); |
422 |
// TODO Auto-generated catch block
|
423 |
// e.printStackTrace();
|
424 |
} |
425 |
refresh(); |
426 |
} |
427 |
|
428 |
/**
|
429 |
* Add a diferent option.
|
430 |
*
|
431 |
* @param s
|
432 |
* Diferent option.
|
433 |
*/
|
434 |
public void addOption(String s) { |
435 |
} |
436 |
|
437 |
/*
|
438 |
* (non-Javadoc)
|
439 |
*
|
440 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
441 |
*/
|
442 |
public void addValue(double d) { |
443 |
|
444 |
} |
445 |
|
446 |
public String getName() { |
447 |
return PluginServices.getText(this, "exploit_"); |
448 |
} |
449 |
|
450 |
public String toString() { |
451 |
return "_exploit"; |
452 |
} |
453 |
|
454 |
@Override
|
455 |
protected int[] getSupportedGeometryTypes() { |
456 |
return new int[] { CURVE, SURFACE }; |
457 |
} |
458 |
|
459 |
} |