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