Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / BreakCADTool.java @ 30335

History | View | Annotate | Download (24.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.editing.gui.cad.tools;
42

    
43
import java.awt.Image;
44
import java.awt.event.InputEvent;
45
import java.awt.geom.PathIterator;
46
import java.awt.geom.Point2D;
47
import java.awt.geom.Rectangle2D;
48
import java.util.ArrayList;
49

    
50
import org.gvsig.andami.PluginServices;
51
import org.gvsig.andami.messages.NotificationManager;
52
import org.gvsig.editing.CADExtension;
53
import org.gvsig.editing.gui.cad.DefaultCADTool;
54
import org.gvsig.editing.gui.cad.exception.CommandException;
55
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext;
56
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
57
import org.gvsig.editing.layers.VectorialLayerEdited;
58
import org.gvsig.fmap.dal.exception.DataException;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.fmap.dal.feature.EditableFeature;
61
import org.gvsig.fmap.dal.feature.Feature;
62
import org.gvsig.fmap.dal.feature.FeatureSet;
63
import org.gvsig.fmap.dal.feature.FeatureStore;
64
import org.gvsig.fmap.geom.Geometry;
65
import org.gvsig.fmap.geom.operation.Draw;
66
import org.gvsig.fmap.geom.operation.DrawOperationContext;
67
import org.gvsig.fmap.geom.operation.GeometryOperationException;
68
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
69
import org.gvsig.fmap.geom.primitive.GeneralPathX;
70
import org.gvsig.fmap.geom.util.Converter;
71
import org.gvsig.fmap.mapcontext.ViewPort;
72
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
73
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
74

    
75

    
76
/**
77
 * Recorta una polil?nea en dos partes.
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class BreakCADTool extends DefaultCADTool {
82
        protected BreakCADToolContext _fsm;
83
        protected Point2D firstPoint;
84
        protected Point2D secondPoint;
85
        protected Feature rowEdited;
86

    
87
        /**
88
         * Crea un nuevo PolylineCADTool.
89
         */
90
        public BreakCADTool() {
91
        }
92

    
93
        /**
94
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
95
         * carga previa a la utilizaci?n de la herramienta.
96
         */
97
        public void init() {
98
                _fsm = new BreakCADToolContext(this);
99
                firstPoint = null;
100
                secondPoint = null;
101
        }
102

    
103
        /*
104
         * (non-Javadoc)
105
         *
106
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
107
         *      double, double)
108
         */
109
        public void transition(double x, double y, InputEvent event) {
110
                _fsm.addPoint(x, y, event);
111
        }
112

    
113
        /*
114
         * (non-Javadoc)
115
         *
116
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
117
         *      double)
118
         */
119
        public void transition(double d) {
120
                _fsm.addValue(d);
121
        }
122

    
123
        /*
124
         * (non-Javadoc)
125
         *
126
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
127
         *      java.lang.String)
128
         */
129
        public void transition(String s) throws CommandException {
130
                if (!super.changeCommand(s)) {
131
                        _fsm.addOption(s);
132
                }
133
        }
134

    
135
        /**
136
         * DOCUMENT ME!
137
         */
138
        public void selection() {
139
                FeatureSet selection = null;
140
                try {
141
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
142

    
143
                        if (selection.getSize() == 0
144
                                        && !CADExtension
145
                                                        .getCADTool()
146
                                                        .getClass()
147
                                                        .getName()
148
                                                        .equals(
149
                                                                        "com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
150
                                CADExtension.setCADTool("_selection", false);
151
                                ((SelectionCADTool) CADExtension.getCADTool())
152
                                                .setNextTool("_break");
153
                        }
154
                } catch (ReadException e) {
155
                        // TODO Auto-generated catch block
156
                        e.printStackTrace();
157
                } catch (DataException e) {
158
                        // TODO Auto-generated catch block
159
                        e.printStackTrace();
160
                }
161
        }
162

    
163
        /**
164
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
165
         * editableFeatureSource que ya estar? creado.
166
         *
167
         * @param x
168
         *            par?metro x del punto que se pase en esta transici?n.
169
         * @param y
170
         *            par?metro y del punto que se pase en esta transici?n.
171
         */
172
        public void addPoint(double x, double y, InputEvent event) {
173
                BreakCADToolState actualState = (BreakCADToolState) _fsm
174
                                .getPreviousState();
175
                String status = actualState.getName();
176

    
177
                if (status.equals("Break.FirstPoint")) {
178
                        // if (rowEdited!=null &&
179
                        // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
180
                        // Point2D.Double(x,y)))
181
                        firstPoint = new Point2D.Double(x, y);
182

    
183
                } else if (status.equals("Break.SecondPoint")) {
184
                        // if (rowEdited !=null &&
185
                        // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
186
                        // Point2D.Double(x,y))){
187
                        secondPoint = new Point2D.Double(x, y);
188
                        try {
189
                                // IGeometry
190
                                // geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
191
                                // if (geom instanceof FGeometryCollection) {
192
                                // breakGeometryGC(rowEdited);
193
                                // }else {
194
                                breakGeometry(rowEdited);
195
                                // }
196
                        } catch (ReadException e) {
197
                                NotificationManager.addError(e.getMessage(), e);
198
                        }
199
                }
200
                // }
201
        }
202

    
203
        /*
204
         * private void breakGeometryGC(DefaultRowEdited dre) throws IOException,
205
         * DriverIOException { GeneralPathX newGp1 = new GeneralPathX();
206
         * GeneralPathX newGp2 = new GeneralPathX(); FGeometryCollection
207
         * gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
208
         * IGeometry[] geoms=gc.getGeometries(); for (int i = 0;i<geoms.length;i++) {
209
         * PathIterator theIterator=geoms[i].getPathIterator(null); double[] theData =
210
         * new double[6]; boolean isFirstPart=true; boolean isCut=false; int
211
         * theType; int numParts = 0;
212
         *
213
         * Point2D previous=null;
214
         *
215
         * while (!theIterator.isDone()) { theType =
216
         * theIterator.currentSegment(theData); switch (theType) {
217
         *
218
         * case PathIterator.SEG_MOVETO: numParts++; previous=new
219
         * Point2D.Double(theData[0], theData[1]); if (isFirstPart)
220
         * newGp1.moveTo(theData[0], theData[1]); else newGp2.moveTo(theData[0],
221
         * theData[1]); break;
222
         *
223
         * case PathIterator.SEG_LINETO: if (previous!=null){ GeneralPathX gpx=new
224
         * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
225
         * gpx.lineTo(theData[0], theData[1]); IGeometry
226
         * geom=ShapeFactory.createPolyline2D(gpx); Point2D
227
         * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
228
         * (intersects(geom,p1)){ isFirstPart=false;
229
         * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
230
         * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
231
         * (isFirstPart) newGp1.lineTo(theData[0], theData[1]); else
232
         * newGp2.lineTo(theData[0], theData[1]); break;
233
         *
234
         * case PathIterator.SEG_QUADTO: if (previous!=null){ GeneralPathX gpx=new
235
         * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
236
         * gpx.quadTo(theData[0], theData[1],theData[2], theData[3]); IGeometry
237
         * geom=ShapeFactory.createPolyline2D(gpx); Point2D
238
         * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
239
         * (intersects(geom,p1)){ isFirstPart=false;
240
         * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
241
         * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
242
         * (isFirstPart) newGp1.quadTo(theData[0], theData[1],theData[2],
243
         * theData[3]); else newGp2.quadTo(theData[0], theData[1],theData[2],
244
         * theData[3]);
245
         *
246
         * break;
247
         *
248
         * case PathIterator.SEG_CUBICTO: if (previous!=null){ GeneralPathX gpx=new
249
         * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
250
         * gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4],
251
         * theData[5]); IGeometry geom=ShapeFactory.createPolyline2D(gpx); Point2D
252
         * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
253
         * (intersects(geom,p1)){ isFirstPart=false;
254
         * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
255
         * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
256
         * (isFirstPart) newGp1.curveTo(theData[0], theData[1],theData[2],
257
         * theData[3],theData[4], theData[5]); else newGp2.curveTo(theData[0],
258
         * theData[1],theData[2], theData[3],theData[4], theData[5]);
259
         *
260
         * break;
261
         *
262
         * case PathIterator.SEG_CLOSE: //if (isFirstPart) // newGp1.closePath();
263
         * //else // newGp2.closePath(); break; } //end switch
264
         *
265
         * theIterator.next(); } //end while loop
266
         *
267
         * if (isCut) { IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
268
         * IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
269
         * VectorialLayerEdited vle = getVLE(); VectorialEditableAdapter vea =
270
         * vle.getVEA(); ArrayList selectedRow = vle.getSelectedRow();
271
         * vea.startComplexRow(); vea.removeRow(dre.getIndex(), getName(),
272
         * EditionEvent.GRAPHIC); int num = vea.getRowCount(); if (gc.isClosed()) {
273
         * ArrayList geomsAux1 = new ArrayList(); geomsAux1.add(geom2); for (int k =
274
         * i + 1; k < geoms.length; k++) { geomsAux1.add(geoms[k]); } for (int k =
275
         * 0; k < i; k++) { geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
276
         *
277
         * DefaultFeature df1 = new DefaultFeature( new
278
         * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
279
         * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
280
         * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
281
         *
282
         * clearSelection(); selectedRow.add(new DefaultRowEdited(df1,
283
         * IRowEdited.STATUS_ADDED, index1)); vea.endComplexRow(); return; }else {
284
         *
285
         * ArrayList geomsAux1 = new ArrayList(); for (int k = 0; k < i; k++) {
286
         * geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
287
         *
288
         * ArrayList geomsAux2 = new ArrayList(); geomsAux2.add(geom2); for (int k =
289
         * i + 1; k < geoms.length; k++) { geomsAux2.add(geoms[k]); }
290
         *
291
         * DefaultFeature df1 = new DefaultFeature( new
292
         * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
293
         * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
294
         * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
295
         * DefaultFeature df2 = new DefaultFeature( new
296
         * FGeometryCollection((IGeometry[]) geomsAux2 .toArray(new IGeometry[0])),
297
         * dre .getAttributes(), String.valueOf(num + 1)); int index2 =
298
         * vea.addRow(df2, PluginServices.getText(this, "parte2"),
299
         * EditionEvent.GRAPHIC); clearSelection(); selectedRow.add(new
300
         * DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, index2));
301
         * selectedRow.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
302
         * index1));
303
         *
304
         * vea.endComplexRow(); return; }
305
         *  } } }
306
         */
307

    
308
        private void breakGeometry(Feature dre) throws ReadException {
309
                breakGeom(dre);
310
        }
311

    
312
        private void breakGeom(Feature dre) throws ReadException {
313
                GeneralPathX newGp1 = new GeneralPathX();
314
                GeneralPathX newGp2 = new GeneralPathX();
315
                Geometry geomAux = (Geometry) rowEdited.getDefaultGeometry();
316
                PathIterator theIterator = geomAux.getPathIterator(null,
317
                                geomManager.getFlatness());
318
                Point2D[] pointsOrdered = getOrderPoints(geomAux);
319
                double[] theData = new double[6];
320
                boolean isFirstPart = true;
321
                boolean intersectsP2 = false;
322
                int theType;
323
                int numParts = 0;
324
                boolean isBreaked = false;
325
                Point2D previous = null;
326

    
327
                while (!theIterator.isDone()) {
328
                        theType = theIterator.currentSegment(theData);
329
                        switch (theType) {
330

    
331
                        case PathIterator.SEG_MOVETO:
332
                                numParts++;
333

    
334
                                previous = new Point2D.Double(theData[0], theData[1]);
335

    
336
                                if (isFirstPart)
337
                                        newGp1.moveTo(theData[0], theData[1]);
338
                                else
339
                                        newGp2.moveTo(theData[0], theData[1]);
340
                                break;
341

    
342
                        case PathIterator.SEG_LINETO:
343

    
344
                                if (previous != null) {
345
                                        GeneralPathX gpx = new GeneralPathX();
346
                                        gpx.moveTo(previous.getX(), previous.getY());
347
                                        gpx.lineTo(theData[0], theData[1]);
348
                                        Geometry geom = createCurve(gpx);
349
                                        Point2D p1 = pointsOrdered[0];
350
                                        Point2D p2 = pointsOrdered[1];
351

    
352
                                        if (intersects(geom, p1) && !isBreaked) {
353
                                                isFirstPart = false;
354
                                                newGp1.lineTo(p1.getX(), p1.getY());
355
                                        }
356
                                        if (intersects(geom, p2) && !isBreaked) {
357
                                                isBreaked = true;
358
                                                intersectsP2 = true;
359
                                                newGp2.moveTo(p2.getX(), p2.getY());
360
                                        }
361
                                }
362
                                previous = new Point2D.Double(theData[0], theData[1]);
363
                                if (isFirstPart)
364
                                        newGp1.lineTo(theData[0], theData[1]);
365
                                else if (intersectsP2) {
366
                                        newGp2.lineTo(theData[0], theData[1]);
367
                                }
368

    
369
                                break;
370

    
371
                        case PathIterator.SEG_QUADTO:
372
                                if (previous != null) {
373
                                        GeneralPathX gpx = new GeneralPathX();
374
                                        gpx.moveTo(previous.getX(), previous.getY());
375
                                        gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
376
                                        Geometry geom = createCurve(gpx);
377
                                        Point2D p1 = pointsOrdered[0];
378
                                        Point2D p2 = pointsOrdered[1];
379
                                        if (intersects(geom, p1) && !isBreaked) {
380
                                                isFirstPart = false;
381
                                                newGp1.lineTo(p1.getX(), p1.getY());
382
                                        }
383
                                        if (intersects(geom, p2) && !isBreaked) {
384
                                                isBreaked = true;
385
                                                intersectsP2 = true;
386
                                                newGp2.moveTo(p2.getX(), p2.getY());
387

    
388
                                        }
389
                                }
390
                                previous = new Point2D.Double(theData[0], theData[1]);
391
                                if (isFirstPart)
392
                                        newGp1.quadTo(theData[0], theData[1], theData[2],
393
                                                        theData[3]);
394
                                else
395
                                        newGp2.quadTo(theData[0], theData[1], theData[2],
396
                                                        theData[3]);
397

    
398
                                break;
399

    
400
                        case PathIterator.SEG_CUBICTO:
401
                                if (previous != null) {
402
                                        GeneralPathX gpx = new GeneralPathX();
403
                                        gpx.moveTo(previous.getX(), previous.getY());
404
                                        gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
405
                                                        theData[4], theData[5]);
406
                                        Geometry geom = createCurve(gpx);
407
                                        Point2D p1 = pointsOrdered[0];
408
                                        Point2D p2 = pointsOrdered[1];
409
                                        if (intersects(geom, p1) && !isBreaked) {
410
                                                isFirstPart = false;
411
                                                newGp1.lineTo(p1.getX(), p1.getY());
412
                                        }
413
                                        if (intersects(geom, p2) && !isBreaked) {
414
                                                isBreaked = true;
415
                                                intersectsP2 = true;
416
                                                newGp2.moveTo(p2.getX(), p2.getY());
417

    
418
                                        }
419
                                }
420
                                previous = new Point2D.Double(theData[0], theData[1]);
421
                                if (isFirstPart)
422
                                        newGp1.curveTo(theData[0], theData[1], theData[2],
423
                                                        theData[3], theData[4], theData[5]);
424
                                else
425
                                        newGp2.curveTo(theData[0], theData[1], theData[2],
426
                                                        theData[3], theData[4], theData[5]);
427

    
428
                                break;
429

    
430
                        case PathIterator.SEG_CLOSE:
431
                                // if (isFirstPart)
432
                                // newGp1.closePath();
433
                                // else
434
                                // newGp2.closePath();
435
                                break;
436
                        } // end switch
437

    
438
                        theIterator.next();
439
                } // end while loop
440
                GeneralPathX gpx = new GeneralPathX();
441
                gpx.append(geomAux.getInternalShape().getPathIterator(null), true);
442
                VectorialLayerEdited vle = getVLE();
443
                FeatureStore featureStore = ((FLyrVect) vle.getLayer())
444
                                .getFeatureStore();
445
                // VectorialEditableAdapter vea = vle.getVEA();
446
                ArrayList selectedRowAux = new ArrayList();
447
                try {
448
                        featureStore.beginEditingGroup(getName());
449

    
450
                        featureStore.delete(dre);
451
                        // vea.startComplexRow();
452
                        if (gpx.isClosed()) {
453

    
454
                                newGp2.append(newGp1.getPathIterator(null, geomManager.getFlatness()),
455
                                                true);
456
                                Geometry geom1 = createCurve(newGp2);
457

    
458
                                // Feature dfLine1 = dre.cloneRow();
459
                                EditableFeature eFeature = featureStore.createNewFeature(dre
460
                                                .getType(), dre);
461
                                eFeature.setGeometry(featureStore.getDefaultFeatureType()
462
                                                .getDefaultGeometryAttributeName(), geom1);
463
                                selectedRowAux.add(eFeature);
464

    
465
                        } else {
466
                                Geometry geom1 = createCurve(newGp1);
467
                                Geometry geom2 = createCurve(newGp2);
468

    
469
                                // DefaultFeature dfLine1 = (DefaultFeature)
470
                                // dre.getLinkedRow().cloneRow();
471
                                EditableFeature eFeature = featureStore.createNewFeature(dre
472
                                                .getType(), dre);
473
                                eFeature.setGeometry(featureStore.getDefaultFeatureType()
474
                                                .getDefaultGeometryAttributeName(), geom2);
475

    
476
                                selectedRowAux.add(eFeature);
477

    
478
                                // dre.editing();
479
                                // dre.setGeometry(geom2);
480
                                // DefaultFeature dfLine2 = (DefaultFeature)
481
                                // dre.getLinkedRow().cloneRow();
482
                                // dfLine2.setGeometry(geom2);
483
                                // int indexLine2 = addGeometry(dre);
484

    
485
                                // selectedRowAux.add(new DefaultRowEdited(dfLine2,
486
                                // IRowEdited.STATUS_ADDED, indexLine2));
487

    
488
                        }
489

    
490
                        // vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
491
                        // vea.endComplexRow(getName());
492
                        featureStore.endEditingGroup();
493
                } catch (DataException e) {
494
                        // TODO Auto-generated catch block
495
                        e.printStackTrace();
496
                }
497
                // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
498
                // selectedRowAux);
499

    
500
        }
501

    
502
        private Point2D[] getOrderPoints(Geometry geomAux) {
503
                PathIterator theIterator = geomAux.getPathIterator(null,
504
                                geomManager.getFlatness());
505
                double[] theData = new double[6];
506
                Point2D previous = null;
507
                ArrayList points = new ArrayList();
508
                boolean isFirstPointBreak = false;
509
                boolean isSecondPointBreak = false;
510
                while (!theIterator.isDone()) {
511
                        int theType = theIterator.currentSegment(theData);
512
                        switch (theType) {
513

    
514
                        case PathIterator.SEG_MOVETO:
515
                                previous = new Point2D.Double(theData[0], theData[1]);
516
                                break;
517

    
518
                        case PathIterator.SEG_LINETO:
519

    
520
                                if (previous != null) {
521
                                        GeneralPathX gpx = new GeneralPathX();
522
                                        gpx.moveTo(previous.getX(), previous.getY());
523
                                        gpx.lineTo(theData[0], theData[1]);
524
                                        Geometry geom = createCurve(gpx);
525
                                        boolean intersectFirst = intersects(geom, firstPoint);
526
                                        boolean intersectSecond = intersects(geom, secondPoint);
527
                                        if (intersectFirst && intersectSecond && !isFirstPointBreak) {
528
                                                isFirstPointBreak = true;
529
                                                isSecondPointBreak = true;
530
                                                points.add(getNearPoint(previous));
531
                                                points.add(getDistantPoint(previous));
532
                                                return (Point2D[]) points.toArray(new Point2D[0]);
533
                                        } else if (intersectFirst && !isFirstPointBreak) {
534
                                                isFirstPointBreak = true;
535
                                                points.add(firstPoint);
536
                                        } else if (intersectSecond && !isSecondPointBreak) {
537
                                                isSecondPointBreak = true;
538
                                                points.add(secondPoint);
539
                                        }
540
                                }
541
                                previous = new Point2D.Double(theData[0], theData[1]);
542
                                break;
543

    
544
                        case PathIterator.SEG_QUADTO:
545
                                if (previous != null) {
546
                                        GeneralPathX gpx = new GeneralPathX();
547
                                        gpx.moveTo(previous.getX(), previous.getY());
548
                                        gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
549
                                        Geometry geom = createCurve(gpx);
550
                                        boolean intersectFirst = intersects(geom, firstPoint);
551
                                        boolean intersectSecond = intersects(geom, secondPoint);
552
                                        if (intersectFirst && intersectSecond && !isFirstPointBreak) {
553
                                                isFirstPointBreak = true;
554
                                                isSecondPointBreak = true;
555
                                                points.add(getNearPoint(previous));
556
                                                points.add(getDistantPoint(previous));
557
                                                return (Point2D[]) points.toArray(new Point2D[0]);
558
                                        } else if (intersectFirst && !isFirstPointBreak) {
559
                                                isFirstPointBreak = true;
560
                                                points.add(firstPoint);
561
                                        } else if (intersectSecond && !isSecondPointBreak) {
562
                                                isSecondPointBreak = true;
563
                                                points.add(secondPoint);
564
                                        }
565
                                }
566
                                previous = new Point2D.Double(theData[0], theData[1]);
567

    
568
                                break;
569

    
570
                        case PathIterator.SEG_CUBICTO:
571
                                if (previous != null) {
572
                                        GeneralPathX gpx = new GeneralPathX();
573
                                        gpx.moveTo(previous.getX(), previous.getY());
574
                                        gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
575
                                                        theData[4], theData[5]);
576
                                        Geometry geom = createCurve(gpx);
577
                                        boolean intersectFirst = intersects(geom, firstPoint);
578
                                        boolean intersectSecond = intersects(geom, secondPoint);
579
                                        if (intersectFirst && intersectSecond && !isFirstPointBreak) {
580
                                                isFirstPointBreak = true;
581
                                                isSecondPointBreak = true;
582
                                                points.add(getNearPoint(previous));
583
                                                points.add(getDistantPoint(previous));
584
                                                return (Point2D[]) points.toArray(new Point2D[0]);
585
                                        } else if (intersectFirst && !isFirstPointBreak) {
586
                                                isFirstPointBreak = true;
587
                                                points.add(firstPoint);
588
                                        } else if (intersectSecond && !isSecondPointBreak) {
589
                                                isSecondPointBreak = true;
590
                                                points.add(secondPoint);
591
                                        }
592
                                }
593
                                previous = new Point2D.Double(theData[0], theData[1]);
594

    
595
                                break;
596

    
597
                        case PathIterator.SEG_CLOSE:
598
                                // if (isFirstPart)
599
                                // newGp1.closePath();
600
                                // else
601
                                // newGp2.closePath();
602
                                break;
603
                        } // end switch
604

    
605
                        theIterator.next();
606
                } // end while loop
607

    
608
                return (Point2D[]) points.toArray(new Point2D[0]);
609
        }
610

    
611
        private Point2D getDistantPoint(Point2D previous) {
612
                if (firstPoint.distance(previous) > secondPoint.distance(previous)) {
613
                        return firstPoint;
614
                }
615
                return secondPoint;
616
        }
617

    
618
        private Point2D getNearPoint(Point2D previous) {
619
                if (firstPoint.distance(previous) <= secondPoint.distance(previous)) {
620
                        return firstPoint;
621
                }
622
                return secondPoint;
623
        }
624

    
625
        /**
626
         * M?todo para dibujar la lo necesario para el estado en el que nos
627
         * encontremos.
628
         *
629
         * @param g
630
         *            Graphics sobre el que dibujar.
631
         * @param x
632
         *            par?metro x del punto que se pase para dibujar.
633
         * @param y
634
         *            par?metro x del punto que se pase para dibujar.
635
         */
636
        public void drawOperation(MapControlDrawer renderer, double x, double y) {
637
                VectorialLayerEdited vle = getVLE();
638
                FeatureSet selection = null;
639
                try {
640
                        selection = (FeatureSet) vle.getFeatureStore().getSelection();
641

    
642
                        ViewPort vp = CADExtension.getEditionManager().getMapControl()
643
                                        .getViewPort();
644
                        if (selection.getSize() == 1) {
645
                                if (firstPoint != null) {
646
                                        Geometry g1 = createCircle(createPoint(firstPoint), vp
647
                                                        .toMapDistance(3));
648
                                        Geometry g2 = createCircle(createPoint(firstPoint), vp
649
                                                        .toMapDistance(5));
650

    
651
                                        renderer.draw(g1, mapControlManager.getAxisReferenceSymbol());                
652
                                        renderer.draw(g2, mapControlManager.getAxisReferenceSymbol());
653
                                }
654
                                rowEdited = (Feature) selection.iterator().next();
655
                                Geometry geom = ((Geometry) rowEdited.getDefaultGeometry())
656
                                                .cloneGeometry();
657
                                if (intersects(geom, new Point2D.Double(x, y))) {                                        
658
                                        renderer.draw(geom, mapControlManager.getGeometrySelectionSymbol());                                
659
                                }
660
                        }else{
661
                        if (!vle.getLayer().isVisible())
662
                                return;
663
                        try{
664
                                Image imgSel = vle.getSelectionImage();
665
                                renderer.drawImage(imgSel, 0, 0);
666
                                
667
                                Image imgHand = vle.getHandlersImage();
668
                                renderer.drawImage(imgHand, 0, 0);
669
                        }catch (Exception e) {
670
                        }
671
                }
672
                } catch (ReadException e1) {
673
                        // TODO Auto-generated catch block
674
                        e1.printStackTrace();
675
                } catch (DataException e) {
676
                        // TODO Auto-generated catch block
677
                        e.printStackTrace();
678
                }
679
        }
680

    
681
        public boolean intersects(double x, double y) {
682
                Point2D p = new Point2D.Double(x, y);
683
                VectorialLayerEdited vle = getVLE();
684
                FeatureSet selection = null;
685
                try {
686
                        selection = (FeatureSet) vle.getFeatureStore().getSelection();
687

    
688
                        if (selection.getSize() == 1) {
689
                                rowEdited = (Feature) selection.iterator().next();
690
                                Geometry g = ((Geometry) rowEdited.getDefaultGeometry())
691
                                                .cloneGeometry();
692
                                return intersects(g, p);
693
                        }
694
                } catch (ReadException e) {
695
                        // TODO Auto-generated catch block
696
                        e.printStackTrace();
697
                } catch (DataException e) {
698
                        // TODO Auto-generated catch block
699
                        e.printStackTrace();
700
                }
701
                return false;
702
        }
703

    
704
        private boolean intersects(Geometry geom, Point2D p) {
705
                double tol = 1;
706
                tol = CADExtension.getEditionManager().getMapControl().getViewPort()
707
                                .toMapDistance((int) tol);
708
                Rectangle2D r = new Rectangle2D.Double(p.getX() - tol / 2, p.getY()
709
                                - tol / 2, tol, tol);
710
                return (geom.intersects(r) && !geom.contains(r));
711
        }
712

    
713
        /**
714
         * Add a diferent option.
715
         *
716
         * @param s
717
         *            Diferent option.
718
         */
719
        public void addOption(String s) {
720
                if (s.equals(PluginServices.getText(this, "cancel")) || s.equals("c")
721
                                || s.equals("C")) {
722
                        init();
723
                }
724
        }
725

    
726
        /*
727
         * (non-Javadoc)
728
         *
729
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
730
         */
731
        public void addValue(double d) {
732
        }
733

    
734
        public String getName() {
735
                return PluginServices.getText(this, "break_");
736
        }
737

    
738
        public String toString() {
739
                return "_break";
740
        }
741

    
742
        public boolean isApplicable(int shapeType) {
743
                switch (shapeType) {
744
                case Geometry.TYPES.GEOMETRY:
745
                case Geometry.TYPES.CURVE:
746
                        return true;
747
                }
748
                return false;
749
        }
750

    
751
}