Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / BreakCADTool.java @ 39573

History | View | Annotate | Download (28.2 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
import java.awt.geom.Rectangle2D;
28
import java.util.ArrayList;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.editing.CADExtension;
33
import org.gvsig.editing.gui.cad.exception.CommandException;
34
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext;
35
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
36
import org.gvsig.editing.layers.VectorialLayerEdited;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.geom.Geometry;
44
import org.gvsig.fmap.geom.primitive.GeneralPathX;
45
import org.gvsig.fmap.mapcontext.ViewPort;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
48
import org.gvsig.tools.dispose.DisposableIterator;
49

    
50
/**
51
 * Recorta una polil?nea en dos partes.
52
 * 
53
 * @author Vicente Caballero Navarro
54
 */
55
public class BreakCADTool extends AbstractCurveCADTool {
56

    
57
    protected BreakCADToolContext _fsm;
58
    protected Point2D firstPoint;
59
    protected Point2D secondPoint;
60
    protected Feature rowEdited;
61

    
62
    /**
63
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
64
     * carga previa a la utilizaci?n de la herramienta.
65
     */
66
    public void init() {
67
        _fsm = new BreakCADToolContext(this);
68
        firstPoint = null;
69
        secondPoint = null;
70
    }
71

    
72
    public void transition(double x, double y, InputEvent event) {
73
        _fsm.addPoint(x, y, event);
74
    }
75

    
76
    public void transition(double d) {
77
        _fsm.addValue(d);
78
    }
79

    
80
    public void transition(String s) throws CommandException {
81
        if (!super.changeCommand(s)) {
82
            _fsm.addOption(s);
83
        }
84
    }
85

    
86
    public void selection() {
87
        FeatureSet selection = null;
88
        try {
89
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
90

    
91
            if (selection.getSize() == 0
92
                && !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
93
                
94
                CADExtension.setCADTool("_selection", false);
95
                ((SelectionCADTool) CADExtension.getCADTool())
96
                    .setNextTool("_break");
97
            }
98
        } catch (ReadException e) {
99
            // TODO Auto-generated catch block
100
            e.printStackTrace();
101
        } catch (DataException e) {
102
            // TODO Auto-generated catch block
103
            e.printStackTrace();
104
        }
105
    }
106

    
107
    /**
108
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
109
     * editableFeatureSource que ya estar? creado.
110
     * 
111
     * @param x
112
     *            par?metro x del punto que se pase en esta transici?n.
113
     * @param y
114
     *            par?metro y del punto que se pase en esta transici?n.
115
     */
116
    public void addPoint(double x, double y, InputEvent event) {
117
        BreakCADToolState actualState =
118
            (BreakCADToolState) _fsm.getPreviousState();
119
        String status = actualState.getName();
120

    
121
        if (status.equals("Break.FirstPoint")) {
122
            // if (rowEdited!=null &&
123
            // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
124
            // Point2D.Double(x,y)))
125
            firstPoint = new Point2D.Double(x, y);
126

    
127
        } else
128
            if (status.equals("Break.SecondPoint")) {
129
                // if (rowEdited !=null &&
130
                // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
131
                // Point2D.Double(x,y))){
132
                secondPoint = new Point2D.Double(x, y);
133
                try {
134
                    // IGeometry
135
                    // geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
136
                    // if (geom instanceof FGeometryCollection) {
137
                    // breakGeometryGC(rowEdited);
138
                    // }else {
139
                    breakGeometry(rowEdited);
140
                    // }
141
                } catch (ReadException e) {
142
                    NotificationManager.addError(e.getMessage(), e);
143
                }
144
            }
145
        // }
146
    }
147

    
148
    /*
149
     * private void breakGeometryGC(DefaultRowEdited dre) throws IOException,
150
     * DriverIOException { GeneralPathX newGp1 = new GeneralPathX();
151
     * GeneralPathX newGp2 = new GeneralPathX(); FGeometryCollection
152
     * gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).
153
     * getGeometry();
154
     * IGeometry[] geoms=gc.getGeometries(); for (int i = 0;i<geoms.length;i++)
155
     * {
156
     * PathIterator theIterator=geoms[i].getPathIterator(null); double[] theData
157
     * =
158
     * new double[6]; boolean isFirstPart=true; boolean isCut=false; int
159
     * theType; int numParts = 0;
160
     * 
161
     * Point2D previous=null;
162
     * 
163
     * while (!theIterator.isDone()) { theType =
164
     * theIterator.currentSegment(theData); switch (theType) {
165
     * 
166
     * case PathIterator.SEG_MOVETO: numParts++; previous=new
167
     * Point2D.Double(theData[0], theData[1]); if (isFirstPart)
168
     * newGp1.moveTo(theData[0], theData[1]); else newGp2.moveTo(theData[0],
169
     * theData[1]); break;
170
     * 
171
     * case PathIterator.SEG_LINETO: if (previous!=null){ GeneralPathX gpx=new
172
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
173
     * gpx.lineTo(theData[0], theData[1]); IGeometry
174
     * geom=ShapeFactory.createPolyline2D(gpx); Point2D
175
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
176
     * (intersects(geom,p1)){ isFirstPart=false;
177
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
178
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
179
     * (isFirstPart) newGp1.lineTo(theData[0], theData[1]); else
180
     * newGp2.lineTo(theData[0], theData[1]); break;
181
     * 
182
     * case PathIterator.SEG_QUADTO: if (previous!=null){ GeneralPathX gpx=new
183
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
184
     * gpx.quadTo(theData[0], theData[1],theData[2], theData[3]); IGeometry
185
     * geom=ShapeFactory.createPolyline2D(gpx); Point2D
186
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
187
     * (intersects(geom,p1)){ isFirstPart=false;
188
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
189
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
190
     * (isFirstPart) newGp1.quadTo(theData[0], theData[1],theData[2],
191
     * theData[3]); else newGp2.quadTo(theData[0], theData[1],theData[2],
192
     * theData[3]);
193
     * 
194
     * break;
195
     * 
196
     * case PathIterator.SEG_CUBICTO: if (previous!=null){ GeneralPathX gpx=new
197
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
198
     * gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4],
199
     * theData[5]); IGeometry geom=ShapeFactory.createPolyline2D(gpx); Point2D
200
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
201
     * (intersects(geom,p1)){ isFirstPart=false;
202
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
203
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
204
     * (isFirstPart) newGp1.curveTo(theData[0], theData[1],theData[2],
205
     * theData[3],theData[4], theData[5]); else newGp2.curveTo(theData[0],
206
     * theData[1],theData[2], theData[3],theData[4], theData[5]);
207
     * 
208
     * break;
209
     * 
210
     * case PathIterator.SEG_CLOSE: //if (isFirstPart) // newGp1.closePath();
211
     * //else // newGp2.closePath(); break; } //end switch
212
     * 
213
     * theIterator.next(); } //end while loop
214
     * 
215
     * if (isCut) { IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
216
     * IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
217
     * VectorialLayerEdited vle = getVLE(); VectorialEditableAdapter vea =
218
     * vle.getVEA(); ArrayList selectedRow = vle.getSelectedRow();
219
     * vea.startComplexRow(); vea.removeRow(dre.getIndex(), getName(),
220
     * EditionEvent.GRAPHIC); int num = vea.getRowCount(); if (gc.isClosed()) {
221
     * ArrayList geomsAux1 = new ArrayList(); geomsAux1.add(geom2); for (int k =
222
     * i + 1; k < geoms.length; k++) { geomsAux1.add(geoms[k]); } for (int k =
223
     * 0; k < i; k++) { geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
224
     * 
225
     * DefaultFeature df1 = new DefaultFeature( new
226
     * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
227
     * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
228
     * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
229
     * 
230
     * clearSelection(); selectedRow.add(new DefaultRowEdited(df1,
231
     * IRowEdited.STATUS_ADDED, index1)); vea.endComplexRow(); return; }else {
232
     * 
233
     * ArrayList geomsAux1 = new ArrayList(); for (int k = 0; k < i; k++) {
234
     * geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
235
     * 
236
     * ArrayList geomsAux2 = new ArrayList(); geomsAux2.add(geom2); for (int k =
237
     * i + 1; k < geoms.length; k++) { geomsAux2.add(geoms[k]); }
238
     * 
239
     * DefaultFeature df1 = new DefaultFeature( new
240
     * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
241
     * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
242
     * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
243
     * DefaultFeature df2 = new DefaultFeature( new
244
     * FGeometryCollection((IGeometry[]) geomsAux2 .toArray(new IGeometry[0])),
245
     * dre .getAttributes(), String.valueOf(num + 1)); int index2 =
246
     * vea.addRow(df2, PluginServices.getText(this, "parte2"),
247
     * EditionEvent.GRAPHIC); clearSelection(); selectedRow.add(new
248
     * DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, index2));
249
     * selectedRow.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
250
     * index1));
251
     * 
252
     * vea.endComplexRow(); return; }
253
     * } } }
254
     */
255

    
256
    private void breakGeometry(Feature dre) throws ReadException {
257
        breakGeom(dre);
258
    }
259

    
260
    private void breakGeom(Feature dre) throws ReadException {
261
        GeneralPathX newGp1 = new GeneralPathX();
262
        GeneralPathX newGp2 = new GeneralPathX();
263
        Geometry geomAux = (Geometry) rowEdited.getDefaultGeometry();
264
        PathIterator theIterator =
265
            geomAux.getPathIterator(null, geomManager.getFlatness());
266
        Point2D[] pointsOrdered = getOrderPoints(geomAux);
267
        double[] theData = new double[6];
268
        boolean isFirstPart = true;
269
        boolean intersectsP2 = false;
270
        int theType;
271
        int numParts = 0;
272
        boolean isBreaked = false;
273
        Point2D previous = null;
274

    
275
        while (!theIterator.isDone()) {
276
            theType = theIterator.currentSegment(theData);
277
            switch (theType) {
278

    
279
            case PathIterator.SEG_MOVETO:
280
                numParts++;
281

    
282
                previous = new Point2D.Double(theData[0], theData[1]);
283

    
284
                if (isFirstPart)
285
                    newGp1.moveTo(theData[0], theData[1]);
286
                else
287
                    newGp2.moveTo(theData[0], theData[1]);
288
                break;
289

    
290
            case PathIterator.SEG_LINETO:
291

    
292
                if (previous != null) {
293
                    GeneralPathX gpx = new GeneralPathX();
294
                    gpx.moveTo(previous.getX(), previous.getY());
295
                    gpx.lineTo(theData[0], theData[1]);
296
                    Geometry geom = createCurve(gpx);
297
                    Point2D p1 = pointsOrdered[0];
298
                    Point2D p2 = pointsOrdered[1];
299

    
300
                    if (intersects(geom, p1) && !isBreaked) {
301
                        isFirstPart = false;
302
                        newGp1.lineTo(p1.getX(), p1.getY());
303
                    }
304
                    if (intersects(geom, p2) && !isBreaked) {
305
                        isBreaked = true;
306
                        intersectsP2 = true;
307
                        newGp2.moveTo(p2.getX(), p2.getY());
308
                    }
309
                }
310
                previous = new Point2D.Double(theData[0], theData[1]);
311
                if (isFirstPart)
312
                    newGp1.lineTo(theData[0], theData[1]);
313
                else
314
                    if (intersectsP2) {
315
                        newGp2.lineTo(theData[0], theData[1]);
316
                    }
317

    
318
                break;
319

    
320
            case PathIterator.SEG_QUADTO:
321
                if (previous != null) {
322
                    GeneralPathX gpx = new GeneralPathX();
323
                    gpx.moveTo(previous.getX(), previous.getY());
324
                    gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
325
                    Geometry geom = createCurve(gpx);
326
                    Point2D p1 = pointsOrdered[0];
327
                    Point2D p2 = pointsOrdered[1];
328
                    if (intersects(geom, p1) && !isBreaked) {
329
                        isFirstPart = false;
330
                        newGp1.lineTo(p1.getX(), p1.getY());
331
                    }
332
                    if (intersects(geom, p2) && !isBreaked) {
333
                        isBreaked = true;
334
                        intersectsP2 = true;
335
                        newGp2.moveTo(p2.getX(), p2.getY());
336

    
337
                    }
338
                }
339
                previous = new Point2D.Double(theData[0], theData[1]);
340
                if (isFirstPart)
341
                    newGp1.quadTo(theData[0], theData[1], theData[2],
342
                        theData[3]);
343
                else
344
                    newGp2.quadTo(theData[0], theData[1], theData[2],
345
                        theData[3]);
346

    
347
                break;
348

    
349
            case PathIterator.SEG_CUBICTO:
350
                if (previous != null) {
351
                    GeneralPathX gpx = new GeneralPathX();
352
                    gpx.moveTo(previous.getX(), previous.getY());
353
                    gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
354
                        theData[4], theData[5]);
355
                    Geometry geom = createCurve(gpx);
356
                    Point2D p1 = pointsOrdered[0];
357
                    Point2D p2 = pointsOrdered[1];
358
                    if (intersects(geom, p1) && !isBreaked) {
359
                        isFirstPart = false;
360
                        newGp1.lineTo(p1.getX(), p1.getY());
361
                    }
362
                    if (intersects(geom, p2) && !isBreaked) {
363
                        isBreaked = true;
364
                        intersectsP2 = true;
365
                        newGp2.moveTo(p2.getX(), p2.getY());
366

    
367
                    }
368
                }
369
                previous = new Point2D.Double(theData[0], theData[1]);
370
                if (isFirstPart)
371
                    newGp1.curveTo(theData[0], theData[1], theData[2],
372
                        theData[3], theData[4], theData[5]);
373
                else
374
                    newGp2.curveTo(theData[0], theData[1], theData[2],
375
                        theData[3], theData[4], theData[5]);
376

    
377
                break;
378

    
379
            case PathIterator.SEG_CLOSE:
380
                // if (isFirstPart)
381
                // newGp1.closePath();
382
                // else
383
                // newGp2.closePath();
384
                break;
385
            } // end switch
386

    
387
            theIterator.next();
388
        } // end while loop
389
        GeneralPathX gpx = new GeneralPathX();
390
        gpx.append(geomAux.getInternalShape().getPathIterator(null), true);
391
        VectorialLayerEdited vle = getVLE();
392
        FeatureStore featureStore =
393
            ((FLyrVect) vle.getLayer()).getFeatureStore();
394
        // VectorialEditableAdapter vea = vle.getVEA();
395
        ArrayList selectedRowAux = new ArrayList();
396
        try {
397
            featureStore.beginEditingGroup(getName());
398

    
399
            featureStore.delete(dre);
400
            // vea.startComplexRow();
401
            if (gpx.isClosed()) {
402

    
403
                newGp2.append(
404
                    newGp1.getPathIterator(null, geomManager.getFlatness()),
405
                    true);
406
                Geometry geom1 = createCurve(newGp2);
407

    
408
                // Feature dfLine1 = dre.cloneRow();
409
                EditableFeature eFeature =
410
                    featureStore.createNewFeature(dre.getType(), dre);
411
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
412
                    .getDefaultGeometryAttributeName(), geom1);
413
                selectedRowAux.add(eFeature);
414

    
415
            } else {
416
                Geometry geom1 = createCurve(newGp1);
417
                Geometry geom2 = createCurve(newGp2);
418

    
419
                // DefaultFeature dfLine1 = (DefaultFeature)
420
                // dre.getLinkedRow().cloneRow();
421
                EditableFeature eFeature =
422
                    featureStore.createNewFeature(dre.getType(), dre);
423
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
424
                    .getDefaultGeometryAttributeName(), geom2);
425

    
426
                selectedRowAux.add(eFeature);
427

    
428
                // dre.editing();
429
                // dre.setGeometry(geom2);
430
                // DefaultFeature dfLine2 = (DefaultFeature)
431
                // dre.getLinkedRow().cloneRow();
432
                // dfLine2.setGeometry(geom2);
433
                // int indexLine2 = addGeometry(dre);
434

    
435
                // selectedRowAux.add(new DefaultRowEdited(dfLine2,
436
                // IRowEdited.STATUS_ADDED, indexLine2));
437

    
438
            }
439

    
440
            // vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
441
            // vea.endComplexRow(getName());
442
            featureStore.endEditingGroup();
443
        } catch (DataException e) {
444
            // TODO Auto-generated catch block
445
            e.printStackTrace();
446
        }
447
        // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
448
        // selectedRowAux);
449

    
450
    }
451

    
452
    private Point2D[] getOrderPoints(Geometry geomAux) {
453
        PathIterator theIterator =
454
            geomAux.getPathIterator(null, geomManager.getFlatness());
455
        double[] theData = new double[6];
456
        Point2D previous = null;
457
        ArrayList points = new ArrayList();
458
        boolean isFirstPointBreak = false;
459
        boolean isSecondPointBreak = false;
460
        while (!theIterator.isDone()) {
461
            int theType = theIterator.currentSegment(theData);
462
            switch (theType) {
463

    
464
            case PathIterator.SEG_MOVETO:
465
                previous = new Point2D.Double(theData[0], theData[1]);
466
                break;
467

    
468
            case PathIterator.SEG_LINETO:
469

    
470
                if (previous != null) {
471
                    GeneralPathX gpx = new GeneralPathX();
472
                    gpx.moveTo(previous.getX(), previous.getY());
473
                    gpx.lineTo(theData[0], theData[1]);
474
                    Geometry geom = createCurve(gpx);
475
                    boolean intersectFirst = intersects(geom, firstPoint);
476
                    boolean intersectSecond = intersects(geom, secondPoint);
477
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
478
                        isFirstPointBreak = true;
479
                        isSecondPointBreak = true;
480
                        points.add(getNearPoint(previous));
481
                        points.add(getDistantPoint(previous));
482
                        return (Point2D[]) points.toArray(new Point2D[0]);
483
                    } else
484
                        if (intersectFirst && !isFirstPointBreak) {
485
                            isFirstPointBreak = true;
486
                            points.add(firstPoint);
487
                        } else
488
                            if (intersectSecond && !isSecondPointBreak) {
489
                                isSecondPointBreak = true;
490
                                points.add(secondPoint);
491
                            }
492
                }
493
                previous = new Point2D.Double(theData[0], theData[1]);
494
                break;
495

    
496
            case PathIterator.SEG_QUADTO:
497
                if (previous != null) {
498
                    GeneralPathX gpx = new GeneralPathX();
499
                    gpx.moveTo(previous.getX(), previous.getY());
500
                    gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
501
                    Geometry geom = createCurve(gpx);
502
                    boolean intersectFirst = intersects(geom, firstPoint);
503
                    boolean intersectSecond = intersects(geom, secondPoint);
504
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
505
                        isFirstPointBreak = true;
506
                        isSecondPointBreak = true;
507
                        points.add(getNearPoint(previous));
508
                        points.add(getDistantPoint(previous));
509
                        return (Point2D[]) points.toArray(new Point2D[0]);
510
                    } else
511
                        if (intersectFirst && !isFirstPointBreak) {
512
                            isFirstPointBreak = true;
513
                            points.add(firstPoint);
514
                        } else
515
                            if (intersectSecond && !isSecondPointBreak) {
516
                                isSecondPointBreak = true;
517
                                points.add(secondPoint);
518
                            }
519
                }
520
                previous = new Point2D.Double(theData[0], theData[1]);
521

    
522
                break;
523

    
524
            case PathIterator.SEG_CUBICTO:
525
                if (previous != null) {
526
                    GeneralPathX gpx = new GeneralPathX();
527
                    gpx.moveTo(previous.getX(), previous.getY());
528
                    gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
529
                        theData[4], theData[5]);
530
                    Geometry geom = createCurve(gpx);
531
                    boolean intersectFirst = intersects(geom, firstPoint);
532
                    boolean intersectSecond = intersects(geom, secondPoint);
533
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
534
                        isFirstPointBreak = true;
535
                        isSecondPointBreak = true;
536
                        points.add(getNearPoint(previous));
537
                        points.add(getDistantPoint(previous));
538
                        return (Point2D[]) points.toArray(new Point2D[0]);
539
                    } else
540
                        if (intersectFirst && !isFirstPointBreak) {
541
                            isFirstPointBreak = true;
542
                            points.add(firstPoint);
543
                        } else
544
                            if (intersectSecond && !isSecondPointBreak) {
545
                                isSecondPointBreak = true;
546
                                points.add(secondPoint);
547
                            }
548
                }
549
                previous = new Point2D.Double(theData[0], theData[1]);
550

    
551
                break;
552

    
553
            case PathIterator.SEG_CLOSE:
554
                // if (isFirstPart)
555
                // newGp1.closePath();
556
                // else
557
                // newGp2.closePath();
558
                break;
559
            } // end switch
560

    
561
            theIterator.next();
562
        } // end while loop
563

    
564
        return (Point2D[]) points.toArray(new Point2D[0]);
565
    }
566

    
567
    private Point2D getDistantPoint(Point2D previous) {
568
        if (firstPoint.distance(previous) > secondPoint.distance(previous)) {
569
            return firstPoint;
570
        }
571
        return secondPoint;
572
    }
573

    
574
    private Point2D getNearPoint(Point2D previous) {
575
        if (firstPoint.distance(previous) <= secondPoint.distance(previous)) {
576
            return firstPoint;
577
        }
578
        return secondPoint;
579
    }
580

    
581
    /**
582
     * M?todo para dibujar la lo necesario para el estado en el que nos
583
     * encontremos.
584
     * 
585
     * @param g
586
     *            Graphics sobre el que dibujar.
587
     * @param x
588
     *            par?metro x del punto que se pase para dibujar.
589
     * @param y
590
     *            par?metro x del punto que se pase para dibujar.
591
     */
592
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
593
        VectorialLayerEdited vle = getVLE();
594
        FeatureSet selection = null;
595
        DisposableIterator iterator = null;
596
        try {
597
            selection = (FeatureSet) vle.getFeatureStore().getSelection();
598

    
599
            ViewPort vp =
600
                CADExtension.getEditionManager().getMapControl().getViewPort();
601
            if (selection.getSize() == 1) {
602
                if (firstPoint != null) {
603
                    Geometry g1 =
604
                        createCircle(createPoint(firstPoint),
605
                            vp.toMapDistance(3));
606
                    Geometry g2 =
607
                        createCircle(createPoint(firstPoint),
608
                            vp.toMapDistance(5));
609

    
610
                    renderer.draw(g1,
611
                        mapControlManager.getAxisReferenceSymbol());
612
                    renderer.draw(g2,
613
                        mapControlManager.getAxisReferenceSymbol());
614
                }
615
                iterator = selection.iterator();
616
                rowEdited = (Feature) iterator.next();
617
                Geometry geom =
618
                    ((Geometry) rowEdited.getDefaultGeometry()).cloneGeometry();
619
                if (intersects(geom, new Point2D.Double(x, y))) {
620
                    renderer.draw(geom,
621
                        mapControlManager.getGeometrySelectionSymbol());
622
                }
623
            }
624
        } catch (ReadException e1) {
625
            // TODO Auto-generated catch block
626
            e1.printStackTrace();
627
        } catch (DataException e) {
628
            // TODO Auto-generated catch block
629
            e.printStackTrace();
630
        } finally {
631
            if (iterator != null) {
632
                iterator.dispose();
633
            }
634
        }
635

    
636
    }
637

    
638
    public boolean intersects(double x, double y) {
639
        Point2D p = new Point2D.Double(x, y);
640
        VectorialLayerEdited vle = getVLE();
641
        FeatureSet selection = null;
642
        DisposableIterator iterator = null;
643
        try {
644
            selection = (FeatureSet) vle.getFeatureStore().getSelection();
645

    
646
            if (selection.getSize() == 1) {
647
                iterator = selection.iterator();
648
                rowEdited = (Feature) iterator.next();
649
                Geometry g =
650
                    ((Geometry) rowEdited.getDefaultGeometry()).cloneGeometry();
651
                return intersects(g, p);
652
            }
653
        } catch (ReadException e) {
654
            // TODO Auto-generated catch block
655
            e.printStackTrace();
656
        } catch (DataException e) {
657
            // TODO Auto-generated catch block
658
            e.printStackTrace();
659
        } finally {
660
            if (iterator != null) {
661
                iterator.dispose();
662
            }
663
        }
664
        return false;
665
    }
666

    
667
    private boolean intersects(Geometry geom, Point2D p) {
668
        double tol = 1;
669
        tol =
670
            CADExtension.getEditionManager().getMapControl().getViewPort()
671
                .toMapDistance((int) tol);
672
        Rectangle2D r =
673
            new Rectangle2D.Double(p.getX() - tol / 2, p.getY() - tol / 2, tol,
674
                tol);
675
        return (geom.intersects(r) && !geom.getShape().contains(r));
676
    }
677

    
678
    /**
679
     * Add a diferent option.
680
     * 
681
     * @param s
682
     *            Diferent option.
683
     */
684
    public void addOption(String s) {
685
        if (s.equals(PluginServices.getText(this, "cancel")) || s.equals("c")
686
            || s.equals("C")) {
687
            init();
688
        }
689
    }
690

    
691
    /*
692
     * (non-Javadoc)
693
     * 
694
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
695
     */
696
    public void addValue(double d) {
697
    }
698

    
699
    public String getName() {
700
        return PluginServices.getText(this, "break_");
701
    }
702

    
703
    public String toString() {
704
        return "_break";
705
    }
706

    
707
    @Override
708
    protected int getSupportedPrimitiveGeometryType() {
709
        return CURVE;
710
    }
711

    
712
}